ClickHouse/src/Interpreters/TranslateQualifiedNamesVisitor.h

86 lines
2.9 KiB
C++
Raw Normal View History

2018-09-28 15:01:13 +00:00
#pragma once
#include <vector>
2019-01-16 17:26:14 +00:00
#include <Core/Names.h>
#include <Interpreters/DatabaseAndTableWithAlias.h>
#include <Interpreters/InDepthNodeVisitor.h>
namespace DB
{
class ASTIdentifier;
class ASTQualifiedAsterisk;
struct ASTTableJoin;
class ASTSelectQuery;
class ASTExpressionList;
class ASTFunction;
/// Visit one node for names qualification. @sa InDepthNodeVisitor.
class TranslateQualifiedNamesMatcher
{
public:
2019-02-22 13:33:56 +00:00
using Visitor = InDepthNodeVisitor<TranslateQualifiedNamesMatcher, true>;
struct Data
{
const NameSet source_columns;
const TablesWithColumns & tables;
std::unordered_set<String> join_using_columns;
bool has_columns;
NameToNameMap parameter_values;
Data(const NameSet & source_columns_, const TablesWithColumns & tables_, bool has_columns_ = true, const NameToNameMap & parameter_values_ = {})
: source_columns(source_columns_)
, tables(tables_)
, has_columns(has_columns_)
, parameter_values(parameter_values_)
{}
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;
static bool matchColumnName(std::string_view name, const String & column_name, DataTypePtr column_type);
};
2019-02-22 13:33:56 +00:00
static void visit(ASTPtr & ast, Data & data);
static bool needChildVisit(ASTPtr & node, const ASTPtr & child);
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 &);
static void visit(ASTExpressionList &, const ASTPtr &, Data &);
static void visit(ASTFunction &, const ASTPtr &, Data &);
static void extractJoinUsingColumns(ASTPtr ast, Data & data);
2022-06-17 07:13:31 +00:00
};
/// Visits AST for names qualification.
/// 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;
/// Restore ASTIdentifiers to long form, change table name in case of distributed.
struct RestoreQualifiedNamesMatcher
{
struct Data
{
DatabaseAndTableWithAlias distributed_table;
DatabaseAndTableWithAlias remote_table;
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);
};
using RestoreQualifiedNamesVisitor = InDepthNodeVisitor<RestoreQualifiedNamesMatcher, true>;
}