1#include <Parsers/ParserQueryWithOutput.h>
2#include <Parsers/ParserShowTablesQuery.h>
3#include <Parsers/ParserSelectWithUnionQuery.h>
4#include <Parsers/ParserTablePropertiesQuery.h>
5#include <Parsers/ParserDescribeTableQuery.h>
6#include <Parsers/ParserShowProcesslistQuery.h>
7#include <Parsers/ParserCheckQuery.h>
8#include <Parsers/ParserCreateQuery.h>
9#include <Parsers/ParserRenameQuery.h>
10#include <Parsers/ParserAlterQuery.h>
11#include <Parsers/ParserDropQuery.h>
12#include <Parsers/ParserKillQueryQuery.h>
13#include <Parsers/ParserOptimizeQuery.h>
14#include <Parsers/ParserWatchQuery.h>
15#include <Parsers/ParserSetQuery.h>
16#include <Parsers/ASTExplainQuery.h>
17#include <Parsers/ParserShowCreateAccessEntityQuery.h>
18#include <Parsers/ParserShowQuotasQuery.h>
19#include <Parsers/ParserShowRowPoliciesQuery.h>
20
21
22namespace DB
23{
24
25bool ParserQueryWithOutput::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
26{
27 ParserShowTablesQuery show_tables_p;
28 ParserSelectWithUnionQuery select_p;
29 ParserTablePropertiesQuery table_p;
30 ParserDescribeTableQuery describe_table_p;
31 ParserShowProcesslistQuery show_processlist_p;
32 ParserCreateQuery create_p;
33 ParserAlterQuery alter_p;
34 ParserRenameQuery rename_p;
35 ParserDropQuery drop_p;
36 ParserCheckQuery check_p;
37 ParserOptimizeQuery optimize_p;
38 ParserKillQueryQuery kill_query_p;
39 ParserWatchQuery watch_p;
40 ParserShowCreateAccessEntityQuery show_create_access_entity_p;
41 ParserShowQuotasQuery show_quotas_p;
42 ParserShowRowPoliciesQuery show_row_policies_p;
43
44 ASTPtr query;
45
46 ParserKeyword s_ast("AST");
47 ParserKeyword s_analyze("ANALYZE");
48 bool explain_ast = false;
49 bool analyze_syntax = false;
50
51 if (enable_explain && s_ast.ignore(pos, expected))
52 explain_ast = true;
53
54 if (enable_explain && s_analyze.ignore(pos, expected))
55 analyze_syntax = true;
56
57 bool parsed = select_p.parse(pos, query, expected)
58 || show_create_access_entity_p.parse(pos, query, expected) /// should be before `show_tables_p`
59 || show_tables_p.parse(pos, query, expected)
60 || table_p.parse(pos, query, expected)
61 || describe_table_p.parse(pos, query, expected)
62 || show_processlist_p.parse(pos, query, expected)
63 || create_p.parse(pos, query, expected)
64 || alter_p.parse(pos, query, expected)
65 || rename_p.parse(pos, query, expected)
66 || drop_p.parse(pos, query, expected)
67 || check_p.parse(pos, query, expected)
68 || kill_query_p.parse(pos, query, expected)
69 || optimize_p.parse(pos, query, expected)
70 || watch_p.parse(pos, query, expected)
71 || show_quotas_p.parse(pos, query, expected)
72 || show_row_policies_p.parse(pos, query, expected);
73
74 if (!parsed)
75 return false;
76
77 /// FIXME: try to prettify this cast using `as<>()`
78 auto & query_with_output = dynamic_cast<ASTQueryWithOutput &>(*query);
79
80 ParserKeyword s_into_outfile("INTO OUTFILE");
81 if (s_into_outfile.ignore(pos, expected))
82 {
83 ParserStringLiteral out_file_p;
84 if (!out_file_p.parse(pos, query_with_output.out_file, expected))
85 return false;
86
87 query_with_output.children.push_back(query_with_output.out_file);
88 }
89
90 ParserKeyword s_format("FORMAT");
91
92 if (s_format.ignore(pos, expected))
93 {
94 ParserIdentifier format_p;
95
96 if (!format_p.parse(pos, query_with_output.format, expected))
97 return false;
98 setIdentifierSpecial(query_with_output.format);
99
100 query_with_output.children.push_back(query_with_output.format);
101 }
102
103 // SETTINGS key1 = value1, key2 = value2, ...
104 ParserKeyword s_settings("SETTINGS");
105 if (s_settings.ignore(pos, expected))
106 {
107 ParserSetQuery parser_settings(true);
108 if (!parser_settings.parse(pos, query_with_output.settings_ast, expected))
109 return false;
110 query_with_output.children.push_back(query_with_output.settings_ast);
111 }
112
113 if (explain_ast)
114 {
115 node = std::make_shared<ASTExplainQuery>(ASTExplainQuery::ParsedAST);
116 node->children.push_back(query);
117 }
118 else if (analyze_syntax)
119 {
120 node = std::make_shared<ASTExplainQuery>(ASTExplainQuery::AnalyzedSyntax);
121 node->children.push_back(query);
122 }
123 else
124 node = query;
125
126 return true;
127}
128
129}
130