1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#pragma once
#include <map>
#include <Parsers/IAST.h>
#include <Interpreters/Aliases.h>
#include <Core/Names.h>
namespace DB
{
class ASTSelectQuery;
class ASTIdentifier;
struct ASTTablesInSelectQueryElement;
class Context;
class ASTQueryParameter;
class QueryNormalizer
{
/// Extracts settings, mostly to show which are used and which are not.
struct ExtractedSettings
{
const UInt64 max_ast_depth;
const UInt64 max_expanded_ast_elements;
bool prefer_column_name_to_alias;
template <typename T>
ExtractedSettings(const T & settings) /// NOLINT
: max_ast_depth(settings.max_ast_depth)
, max_expanded_ast_elements(settings.max_expanded_ast_elements)
, prefer_column_name_to_alias(settings.prefer_column_name_to_alias)
{
}
};
public:
struct Data
{
using SetOfASTs = std::set<const IAST *>;
using MapOfASTs = std::map<ASTPtr, ASTPtr>;
Aliases & aliases;
const NameSet & source_columns_set;
ExtractedSettings settings;
NameSet query_parameters;
/// tmp data
size_t level;
MapOfASTs finished_asts; /// already processed vertices (and by what they replaced)
SetOfASTs current_asts; /// vertices in the current call stack of this method
std::string current_alias; /// the alias referencing to the ancestor of ast (the deepest ancestor with aliases)
const bool ignore_alias; /// normalize query without any aliases
/// It's Ok to have "c + 1 AS c" in queries, but not in table definition
const bool allow_self_aliases; /// for constructs like "SELECT column + 1 AS column"
bool is_create_parameterized_view;
Data(Aliases & aliases_, const NameSet & source_columns_set_, bool ignore_alias_, ExtractedSettings && settings_, bool allow_self_aliases_, bool is_create_parameterized_view_ = false)
: aliases(aliases_)
, source_columns_set(source_columns_set_)
, settings(settings_)
, level(0)
, ignore_alias(ignore_alias_)
, allow_self_aliases(allow_self_aliases_)
, is_create_parameterized_view(is_create_parameterized_view_)
{}
};
explicit QueryNormalizer(Data & data)
: visitor_data(data)
{}
void visit(ASTPtr & ast)
{
visit(ast, visitor_data);
}
private:
Data & visitor_data;
static void visit(ASTPtr & ast, Data & data);
static void visit(ASTIdentifier &, ASTPtr &, Data &);
static void visit(ASTTablesInSelectQueryElement &, const ASTPtr &, Data &);
static void visit(ASTSelectQuery &, const ASTPtr &, Data &);
static void visitChildren(IAST * node, Data & data);
};
}
|