1#include <Parsers/ASTAlterQuery.h>
2#include <Parsers/ASTCheckQuery.h>
3#include <Parsers/ASTCreateQuery.h>
4#include <Parsers/ASTCreateQuotaQuery.h>
5#include <Parsers/ASTCreateRowPolicyQuery.h>
6#include <Parsers/ASTDropAccessEntityQuery.h>
7#include <Parsers/ASTDropQuery.h>
8#include <Parsers/ASTInsertQuery.h>
9#include <Parsers/ASTKillQueryQuery.h>
10#include <Parsers/ASTOptimizeQuery.h>
11#include <Parsers/ASTRenameQuery.h>
12#include <Parsers/ASTSelectQuery.h>
13#include <Parsers/ASTSelectWithUnionQuery.h>
14#include <Parsers/ASTSetQuery.h>
15#include <Parsers/ASTShowCreateAccessEntityQuery.h>
16#include <Parsers/ASTShowProcesslistQuery.h>
17#include <Parsers/ASTShowQuotasQuery.h>
18#include <Parsers/ASTShowRowPoliciesQuery.h>
19#include <Parsers/ASTShowTablesQuery.h>
20#include <Parsers/ASTUseQuery.h>
21#include <Parsers/ASTExplainQuery.h>
22#include <Parsers/TablePropertiesQueriesASTs.h>
23#include <Parsers/ASTWatchQuery.h>
24
25#include <Interpreters/InterpreterAlterQuery.h>
26#include <Interpreters/InterpreterCheckQuery.h>
27#include <Interpreters/InterpreterCreateQuery.h>
28#include <Interpreters/InterpreterCreateQuotaQuery.h>
29#include <Interpreters/InterpreterCreateRowPolicyQuery.h>
30#include <Interpreters/InterpreterDescribeQuery.h>
31#include <Interpreters/InterpreterExplainQuery.h>
32#include <Interpreters/InterpreterDropAccessEntityQuery.h>
33#include <Interpreters/InterpreterDropQuery.h>
34#include <Interpreters/InterpreterExistsQuery.h>
35#include <Interpreters/InterpreterFactory.h>
36#include <Interpreters/InterpreterInsertQuery.h>
37#include <Interpreters/InterpreterKillQueryQuery.h>
38#include <Interpreters/InterpreterOptimizeQuery.h>
39#include <Interpreters/InterpreterRenameQuery.h>
40#include <Interpreters/InterpreterSelectQuery.h>
41#include <Interpreters/InterpreterSelectWithUnionQuery.h>
42#include <Interpreters/InterpreterSetQuery.h>
43#include <Interpreters/InterpreterShowCreateAccessEntityQuery.h>
44#include <Interpreters/InterpreterShowCreateQuery.h>
45#include <Interpreters/InterpreterShowProcesslistQuery.h>
46#include <Interpreters/InterpreterShowQuotasQuery.h>
47#include <Interpreters/InterpreterShowRowPoliciesQuery.h>
48#include <Interpreters/InterpreterShowTablesQuery.h>
49#include <Interpreters/InterpreterSystemQuery.h>
50#include <Interpreters/InterpreterUseQuery.h>
51#include <Interpreters/InterpreterWatchQuery.h>
52
53#include <Parsers/ASTSystemQuery.h>
54
55#include <Common/typeid_cast.h>
56#include <Common/ProfileEvents.h>
57
58
59namespace ProfileEvents
60{
61 extern const Event Query;
62 extern const Event SelectQuery;
63 extern const Event InsertQuery;
64}
65
66
67namespace DB
68{
69namespace ErrorCodes
70{
71 extern const int READONLY;
72 extern const int UNKNOWN_TYPE_OF_QUERY;
73 extern const int QUERY_IS_PROHIBITED;
74}
75
76
77static void throwIfNoAccess(Context & context)
78{
79 if (context.getSettingsRef().readonly)
80 {
81 const auto & client_info = context.getClientInfo();
82 if (client_info.interface == ClientInfo::Interface::HTTP && client_info.http_method == ClientInfo::HTTPMethod::GET)
83 throw Exception("Cannot execute query in readonly mode. "
84 "For queries over HTTP, method GET implies readonly. You should use method POST for modifying queries.", ErrorCodes::READONLY);
85 else
86 throw Exception("Cannot execute query in readonly mode", ErrorCodes::READONLY);
87 }
88 else if (!context.getSettingsRef().allow_ddl)
89 throw Exception("Cannot execute query. DDL queries are prohibited for the user", ErrorCodes::QUERY_IS_PROHIBITED);
90}
91
92
93std::unique_ptr<IInterpreter> InterpreterFactory::get(ASTPtr & query, Context & context, QueryProcessingStage::Enum stage)
94{
95 ProfileEvents::increment(ProfileEvents::Query);
96
97 if (query->as<ASTSelectQuery>())
98 {
99 /// This is internal part of ASTSelectWithUnionQuery.
100 /// Even if there is SELECT without union, it is represented by ASTSelectWithUnionQuery with single ASTSelectQuery as a child.
101 return std::make_unique<InterpreterSelectQuery>(query, context, SelectQueryOptions(stage));
102 }
103 else if (query->as<ASTSelectWithUnionQuery>())
104 {
105 ProfileEvents::increment(ProfileEvents::SelectQuery);
106 return std::make_unique<InterpreterSelectWithUnionQuery>(query, context, SelectQueryOptions(stage));
107 }
108 else if (query->as<ASTInsertQuery>())
109 {
110 ProfileEvents::increment(ProfileEvents::InsertQuery);
111 /// readonly is checked inside InterpreterInsertQuery
112 bool allow_materialized = static_cast<bool>(context.getSettingsRef().insert_allow_materialized_columns);
113 return std::make_unique<InterpreterInsertQuery>(query, context, allow_materialized);
114 }
115 else if (query->as<ASTCreateQuery>())
116 {
117 /// readonly and allow_ddl are checked inside InterpreterCreateQuery
118 return std::make_unique<InterpreterCreateQuery>(query, context);
119 }
120 else if (query->as<ASTDropQuery>())
121 {
122 /// readonly and allow_ddl are checked inside InterpreterDropQuery
123 return std::make_unique<InterpreterDropQuery>(query, context);
124 }
125 else if (query->as<ASTRenameQuery>())
126 {
127 throwIfNoAccess(context);
128 return std::make_unique<InterpreterRenameQuery>(query, context);
129 }
130 else if (query->as<ASTShowTablesQuery>())
131 {
132 return std::make_unique<InterpreterShowTablesQuery>(query, context);
133 }
134 else if (query->as<ASTUseQuery>())
135 {
136 return std::make_unique<InterpreterUseQuery>(query, context);
137 }
138 else if (query->as<ASTSetQuery>())
139 {
140 /// readonly is checked inside InterpreterSetQuery
141 return std::make_unique<InterpreterSetQuery>(query, context);
142 }
143 else if (query->as<ASTOptimizeQuery>())
144 {
145 throwIfNoAccess(context);
146 return std::make_unique<InterpreterOptimizeQuery>(query, context);
147 }
148 else if (query->as<ASTExistsTableQuery>())
149 {
150 return std::make_unique<InterpreterExistsQuery>(query, context);
151 }
152 else if (query->as<ASTExistsDictionaryQuery>())
153 {
154 return std::make_unique<InterpreterExistsQuery>(query, context);
155 }
156 else if (query->as<ASTShowCreateTableQuery>())
157 {
158 return std::make_unique<InterpreterShowCreateQuery>(query, context);
159 }
160 else if (query->as<ASTShowCreateDatabaseQuery>())
161 {
162 return std::make_unique<InterpreterShowCreateQuery>(query, context);
163 }
164 else if (query->as<ASTShowCreateDictionaryQuery>())
165 {
166 return std::make_unique<InterpreterShowCreateQuery>(query, context);
167 }
168 else if (query->as<ASTDescribeQuery>())
169 {
170 return std::make_unique<InterpreterDescribeQuery>(query, context);
171 }
172 else if (query->as<ASTExplainQuery>())
173 {
174 return std::make_unique<InterpreterExplainQuery>(query, context);
175 }
176 else if (query->as<ASTShowProcesslistQuery>())
177 {
178 return std::make_unique<InterpreterShowProcesslistQuery>(query, context);
179 }
180 else if (query->as<ASTAlterQuery>())
181 {
182 throwIfNoAccess(context);
183 return std::make_unique<InterpreterAlterQuery>(query, context);
184 }
185 else if (query->as<ASTCheckQuery>())
186 {
187 return std::make_unique<InterpreterCheckQuery>(query, context);
188 }
189 else if (query->as<ASTKillQueryQuery>())
190 {
191 return std::make_unique<InterpreterKillQueryQuery>(query, context);
192 }
193 else if (query->as<ASTSystemQuery>())
194 {
195 throwIfNoAccess(context);
196 return std::make_unique<InterpreterSystemQuery>(query, context);
197 }
198 else if (query->as<ASTWatchQuery>())
199 {
200 return std::make_unique<InterpreterWatchQuery>(query, context);
201 }
202 else if (query->as<ASTCreateQuotaQuery>())
203 {
204 return std::make_unique<InterpreterCreateQuotaQuery>(query, context);
205 }
206 else if (query->as<ASTCreateRowPolicyQuery>())
207 {
208 return std::make_unique<InterpreterCreateRowPolicyQuery>(query, context);
209 }
210 else if (query->as<ASTDropAccessEntityQuery>())
211 {
212 return std::make_unique<InterpreterDropAccessEntityQuery>(query, context);
213 }
214 else if (query->as<ASTShowCreateAccessEntityQuery>())
215 {
216 return std::make_unique<InterpreterShowCreateAccessEntityQuery>(query, context);
217 }
218 else if (query->as<ASTShowQuotasQuery>())
219 {
220 return std::make_unique<InterpreterShowQuotasQuery>(query, context);
221 }
222 else if (query->as<ASTShowRowPoliciesQuery>())
223 {
224 return std::make_unique<InterpreterShowRowPoliciesQuery>(query, context);
225 }
226 else
227 throw Exception("Unknown type of query: " + query->getID(), ErrorCodes::UNKNOWN_TYPE_OF_QUERY);
228}
229}
230