1#pragma once
2
3#include <map>
4
5#include <Parsers/IAST.h>
6#include <Interpreters/Aliases.h>
7
8namespace DB
9{
10
11class ASTSelectQuery;
12class ASTIdentifier;
13struct ASTTablesInSelectQueryElement;
14class Context;
15
16
17class QueryNormalizer
18{
19 /// Extracts settings, mostly to show which are used and which are not.
20 struct ExtractedSettings
21 {
22 const UInt64 max_ast_depth;
23 const UInt64 max_expanded_ast_elements;
24
25 template <typename T>
26 ExtractedSettings(const T & settings)
27 : max_ast_depth(settings.max_ast_depth),
28 max_expanded_ast_elements(settings.max_expanded_ast_elements)
29 {}
30 };
31
32public:
33 struct Data
34 {
35 using SetOfASTs = std::set<const IAST *>;
36 using MapOfASTs = std::map<ASTPtr, ASTPtr>;
37
38 const Aliases & aliases;
39 const ExtractedSettings settings;
40
41 /// tmp data
42 size_t level;
43 MapOfASTs finished_asts; /// already processed vertices (and by what they replaced)
44 SetOfASTs current_asts; /// vertices in the current call stack of this method
45 std::string current_alias; /// the alias referencing to the ancestor of ast (the deepest ancestor with aliases)
46
47 Data(const Aliases & aliases_, ExtractedSettings && settings_)
48 : aliases(aliases_)
49 , settings(settings_)
50 , level(0)
51 {}
52 };
53
54 QueryNormalizer(Data & data)
55 : visitor_data(data)
56 {}
57
58 void visit(ASTPtr & ast)
59 {
60 visit(ast, visitor_data);
61 }
62
63private:
64 Data & visitor_data;
65
66 static void visit(ASTPtr & query, Data & data);
67
68 static void visit(ASTIdentifier &, ASTPtr &, Data &);
69 static void visit(ASTTablesInSelectQueryElement &, const ASTPtr &, Data &);
70 static void visit(ASTSelectQuery &, const ASTPtr &, Data &);
71
72 static void visitChildren(const ASTPtr &, Data & data);
73};
74
75}
76