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;
|
2019-10-15 14:42:57 +00:00
|
|
|
const std::vector<TableWithColumnNames> tables;
|
2019-02-11 14:19:09 +00:00
|
|
|
std::unordered_set<String> join_using_columns;
|
|
|
|
bool has_columns;
|
2019-02-07 19:18:40 +00:00
|
|
|
|
2019-10-15 14:42:57 +00:00
|
|
|
Data(const NameSet & source_columns_, std::vector<TableWithColumnNames> && tables_, bool has_columns_ = true)
|
2019-02-11 14:19:09 +00:00
|
|
|
: source_columns(source_columns_)
|
|
|
|
, tables(tables_)
|
|
|
|
, has_columns(has_columns_)
|
|
|
|
{}
|
|
|
|
|
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; }
|
2019-11-13 16:49:29 +00:00
|
|
|
bool unknownColumn(size_t table_pos, const ASTIdentifier & node) const;
|
2019-11-13 15:00:21 +00:00
|
|
|
|
2019-02-11 14:19:09 +00:00
|
|
|
static std::vector<TableWithColumnNames> tablesOnly(const std::vector<DatabaseAndTableWithAlias> & tables)
|
2019-02-07 19:18:40 +00:00
|
|
|
{
|
2019-02-11 14:19:09 +00:00
|
|
|
std::vector<TableWithColumnNames> tables_with_columns;
|
2019-02-07 19:18:40 +00:00
|
|
|
tables_with_columns.reserve(tables.size());
|
2019-02-11 14:19:09 +00:00
|
|
|
|
2019-02-07 19:18:40 +00:00
|
|
|
for (const auto & table : tables)
|
|
|
|
tables_with_columns.emplace_back(TableWithColumnNames{table, {}});
|
2019-02-11 14:19:09 +00:00
|
|
|
return tables_with_columns;
|
2019-02-07 19:18:40 +00:00
|
|
|
}
|
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:
|
2019-02-22 13:33:56 +00:00
|
|
|
static void visit(ASTIdentifier & node, ASTPtr & ast, Data &);
|
|
|
|
static void visit(const ASTQualifiedAsterisk & node, const ASTPtr & ast, Data &);
|
|
|
|
static void visit(ASTTableJoin & node, const ASTPtr & ast, Data &);
|
|
|
|
static void visit(ASTSelectQuery & node, 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 &);
|
|
|
|
|
|
|
|
static void extractJoinUsingColumns(const ASTPtr ast, Data & data);
|
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
|
|
|
|
2019-07-26 17:43:42 +00:00
|
|
|
/// Restore ASTIdentifiers to long form
|
|
|
|
struct RestoreQualifiedNamesData
|
|
|
|
{
|
|
|
|
using TypeToVisit = ASTIdentifier;
|
|
|
|
|
|
|
|
void visit(ASTIdentifier & identifier, ASTPtr & ast);
|
|
|
|
};
|
|
|
|
|
|
|
|
using RestoreQualifiedNamesMatcher = OneTypeMatcher<RestoreQualifiedNamesData>;
|
|
|
|
using RestoreQualifiedNamesVisitor = InDepthNodeVisitor<RestoreQualifiedNamesMatcher, true>;
|
|
|
|
|
2018-09-27 19:25:18 +00:00
|
|
|
}
|