2018-09-28 15:01:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-09-27 19:25:18 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-01-16 17:26:14 +00:00
|
|
|
#include <Core/Names.h>
|
2018-10-30 16:31:21 +00:00
|
|
|
#include <Interpreters/DatabaseAndTableWithAlias.h>
|
2018-12-06 15:29:55 +00:00
|
|
|
#include <Interpreters/InDepthNodeVisitor.h>
|
2018-09-27 19:25:18 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
class ASTIdentifier;
|
|
|
|
class ASTQualifiedAsterisk;
|
|
|
|
struct ASTTableJoin;
|
|
|
|
class ASTSelectQuery;
|
2019-02-11 14:19:09 +00:00
|
|
|
class ASTExpressionList;
|
|
|
|
class ASTFunction;
|
2018-09-27 19:25:18 +00:00
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
/// Visit one node for names qualification. @sa InDepthNodeVisitor.
|
|
|
|
class TranslateQualifiedNamesMatcher
|
2018-09-27 19:25:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-02-22 13:33:56 +00:00
|
|
|
using Visitor = InDepthNodeVisitor<TranslateQualifiedNamesMatcher, true>;
|
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
struct Data
|
2018-09-27 19:25:18 +00:00
|
|
|
{
|
2019-07-24 15:37:37 +00:00
|
|
|
const NameSet source_columns;
|
2020-06-05 21:17:00 +00:00
|
|
|
const TablesWithColumns & tables;
|
2019-02-11 14:19:09 +00:00
|
|
|
std::unordered_set<String> join_using_columns;
|
|
|
|
bool has_columns;
|
2022-11-28 18:05:01 +00:00
|
|
|
NameToNameMap parameter_values;
|
2019-02-07 19:18:40 +00:00
|
|
|
|
2022-12-16 10:11:19 +00:00
|
|
|
Data(const NameSet & source_columns_, const TablesWithColumns & tables_, bool has_columns_ = true, const NameToNameMap & parameter_values_ = {})
|
2019-02-11 14:19:09 +00:00
|
|
|
: source_columns(source_columns_)
|
|
|
|
, tables(tables_)
|
|
|
|
, has_columns(has_columns_)
|
2022-11-28 18:05:01 +00:00
|
|
|
, parameter_values(parameter_values_)
|
2019-02-11 14:19:09 +00:00
|
|
|
{}
|
|
|
|
|
2019-10-16 14:47:58 +00:00
|
|
|
bool hasColumn(const String & name) const { return source_columns.count(name); }
|
|
|
|
bool hasTable() const { return !tables.empty(); }
|
|
|
|
bool processAsterisks() const { return hasTable() && has_columns; }
|
2020-03-09 00:28:05 +00:00
|
|
|
bool unknownColumn(size_t table_pos, const ASTIdentifier & identifier) const;
|
2022-07-14 16:11:35 +00:00
|
|
|
static bool matchColumnName(std::string_view name, const String & column_name, DataTypePtr column_type);
|
2018-12-06 15:29:55 +00:00
|
|
|
};
|
2018-09-27 19:25:18 +00:00
|
|
|
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(ASTPtr & ast, Data & data);
|
2018-12-06 15:29:55 +00:00
|
|
|
static bool needChildVisit(ASTPtr & node, const ASTPtr & child);
|
2018-09-27 19:25:18 +00:00
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
private:
|
2020-03-09 00:28:05 +00:00
|
|
|
static void visit(ASTIdentifier & identifier, ASTPtr & ast, Data &);
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(const ASTQualifiedAsterisk & node, const ASTPtr & ast, Data &);
|
2020-03-09 00:28:05 +00:00
|
|
|
static void visit(ASTTableJoin & join, const ASTPtr & ast, Data &);
|
|
|
|
static void visit(ASTSelectQuery & select, const ASTPtr & ast, Data &);
|
2019-02-11 14:19:09 +00:00
|
|
|
static void visit(ASTExpressionList &, const ASTPtr &, Data &);
|
|
|
|
static void visit(ASTFunction &, const ASTPtr &, Data &);
|
|
|
|
|
2022-03-13 11:59:20 +00:00
|
|
|
static void extractJoinUsingColumns(ASTPtr ast, Data & data);
|
2022-06-17 07:13:31 +00:00
|
|
|
|
2018-09-27 19:25:18 +00:00
|
|
|
};
|
|
|
|
|
2018-12-06 15:29:55 +00:00
|
|
|
/// Visits AST for names qualification.
|
2019-02-11 14:19:09 +00:00
|
|
|
/// It finds columns and translate their names to the normal form. Expand asterisks and qualified asterisks with column names.
|
2019-02-22 13:33:56 +00:00
|
|
|
using TranslateQualifiedNamesVisitor = TranslateQualifiedNamesMatcher::Visitor;
|
2018-12-06 15:29:55 +00:00
|
|
|
|
2020-04-01 14:21:37 +00:00
|
|
|
|
|
|
|
/// Restore ASTIdentifiers to long form, change table name in case of distributed.
|
|
|
|
struct RestoreQualifiedNamesMatcher
|
2019-07-26 17:43:42 +00:00
|
|
|
{
|
2020-04-01 14:21:37 +00:00
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
DatabaseAndTableWithAlias distributed_table;
|
|
|
|
DatabaseAndTableWithAlias remote_table;
|
2019-07-26 17:43:42 +00:00
|
|
|
|
2020-04-01 14:21:37 +00:00
|
|
|
void changeTable(ASTIdentifier & identifier) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool needChildVisit(ASTPtr & node, const ASTPtr & child);
|
|
|
|
static void visit(ASTPtr & ast, Data & data);
|
|
|
|
static void visit(ASTIdentifier & identifier, ASTPtr & ast, Data & data);
|
2019-07-26 17:43:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
using RestoreQualifiedNamesVisitor = InDepthNodeVisitor<RestoreQualifiedNamesMatcher, true>;
|
|
|
|
|
2018-09-27 19:25:18 +00:00
|
|
|
}
|