1#pragma once
2
3#include <Parsers/IAST.h>
4#include <Core/Names.h>
5
6
7namespace DB
8{
9
10struct ASTTablesInSelectQueryElement;
11
12
13/** SELECT query
14 */
15class ASTSelectQuery : public IAST
16{
17public:
18 enum class Expression : UInt8
19 {
20 WITH,
21 SELECT,
22 TABLES,
23 PREWHERE,
24 WHERE,
25 GROUP_BY,
26 HAVING,
27 ORDER_BY,
28 LIMIT_BY_OFFSET,
29 LIMIT_BY_LENGTH,
30 LIMIT_BY,
31 LIMIT_OFFSET,
32 LIMIT_LENGTH,
33 SETTINGS
34 };
35
36 /** Get the text that identifies this element. */
37 String getID(char) const override { return "SelectQuery"; }
38
39 ASTPtr clone() const override;
40
41 bool distinct = false;
42 bool group_by_with_totals = false;
43 bool group_by_with_rollup = false;
44 bool group_by_with_cube = false;
45 bool limit_with_ties = false;
46
47 ASTPtr & refSelect() { return getExpression(Expression::SELECT); }
48 ASTPtr & refTables() { return getExpression(Expression::TABLES); }
49 ASTPtr & refPrewhere() { return getExpression(Expression::PREWHERE); }
50 ASTPtr & refWhere() { return getExpression(Expression::WHERE); }
51 ASTPtr & refHaving() { return getExpression(Expression::HAVING); }
52
53 const ASTPtr with() const { return getExpression(Expression::WITH); }
54 const ASTPtr select() const { return getExpression(Expression::SELECT); }
55 const ASTPtr tables() const { return getExpression(Expression::TABLES); }
56 const ASTPtr prewhere() const { return getExpression(Expression::PREWHERE); }
57 const ASTPtr where() const { return getExpression(Expression::WHERE); }
58 const ASTPtr groupBy() const { return getExpression(Expression::GROUP_BY); }
59 const ASTPtr having() const { return getExpression(Expression::HAVING); }
60 const ASTPtr orderBy() const { return getExpression(Expression::ORDER_BY); }
61 const ASTPtr limitByOffset() const { return getExpression(Expression::LIMIT_BY_OFFSET); }
62 const ASTPtr limitByLength() const { return getExpression(Expression::LIMIT_BY_LENGTH); }
63 const ASTPtr limitBy() const { return getExpression(Expression::LIMIT_BY); }
64 const ASTPtr limitOffset() const { return getExpression(Expression::LIMIT_OFFSET); }
65 const ASTPtr limitLength() const { return getExpression(Expression::LIMIT_LENGTH); }
66 const ASTPtr settings() const { return getExpression(Expression::SETTINGS); }
67
68 /// Set/Reset/Remove expression.
69 void setExpression(Expression expr, ASTPtr && ast);
70
71 ASTPtr getExpression(Expression expr, bool clone = false) const
72 {
73 auto it = positions.find(expr);
74 if (it != positions.end())
75 return clone ? children[it->second]->clone() : children[it->second];
76 return {};
77 }
78
79 /// Compatibility with old parser of tables list. TODO remove
80 ASTPtr sample_size() const;
81 ASTPtr sample_offset() const;
82 ASTPtr array_join_expression_list(bool & is_left) const;
83 ASTPtr array_join_expression_list() const;
84 const ASTTablesInSelectQueryElement * join() const;
85 bool final() const;
86 bool withFill() const;
87 void replaceDatabaseAndTable(const String & database_name, const String & table_name);
88 void addTableFunction(ASTPtr & table_function_ptr);
89
90protected:
91 void formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
92
93private:
94 std::unordered_map<Expression, size_t> positions;
95
96 ASTPtr & getExpression(Expression expr);
97};
98
99}
100