forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathASTFromJSON.cpp
More file actions
473 lines (415 loc) · 21 KB
/
Copy pathASTFromJSON.cpp
File metadata and controls
473 lines (415 loc) · 21 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <Parsers/ASTFromJSON.h>
#include <Parsers/IAST.h>
#include <Core/Defines.h>
#include <algorithm>
#include <limits>
/// Include ALL AST types for the factory.
#include <Parsers/ASTAsterisk.h>
#include <Parsers/ASTAlterQuery.h>
#include <Parsers/ASTAssignment.h>
#include <Parsers/ASTBackupQuery.h>
#include <Parsers/ASTCheckQuery.h>
#include <Parsers/ASTCollation.h>
#include <Parsers/ASTColumnDeclaration.h>
#include <Parsers/ASTColumnsMatcher.h>
#include <Parsers/ASTColumnsTransformers.h>
#include <Parsers/ASTConstraintDeclaration.h>
#include <Parsers/ASTCreateIndexQuery.h>
#include <Parsers/ASTCreateQuery.h>
#include <Parsers/ASTCreateSQLFunctionQuery.h>
#include <Parsers/ASTCreateWasmFunctionQuery.h>
#include <Parsers/ASTDataType.h>
#include <Parsers/ASTEnumDataType.h>
#include <Parsers/ASTTupleDataType.h>
#include <Parsers/ASTDeleteQuery.h>
#include <Parsers/ASTDropIndexQuery.h>
#include <Parsers/ASTDictionary.h>
#include <Parsers/ASTDictionaryAttributeDeclaration.h>
#include <Parsers/ASTDropQuery.h>
#include <Parsers/ASTExplainQuery.h>
#include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTForeignKeyDeclaration.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTFunctionWithKeyValueArguments.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTIdentifierTypePair.h>
#include <Parsers/ASTIndexDeclaration.h>
#include <Parsers/ASTInsertQuery.h>
#include <Parsers/ASTInterpolateElement.h>
#include <Parsers/ASTKillQueryQuery.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ASTNameTypePair.h>
#include <Parsers/ASTOptimizeQuery.h>
#include <Parsers/ASTOrderByElement.h>
#include <Parsers/ASTPartition.h>
#include <Parsers/ASTProjectionDeclaration.h>
#include <Parsers/ASTProjectionSelectQuery.h>
#include <Parsers/ASTQualifiedAsterisk.h>
#include <Parsers/ASTQueryParameter.h>
#include <Parsers/ASTRefreshStrategy.h>
#include <Parsers/ASTRenameQuery.h>
#include <Parsers/ASTSQLSecurity.h>
#include <Parsers/ASTSampleRatio.h>
#include <Parsers/ASTSelectIntersectExceptQuery.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTSetQuery.h>
#include <Parsers/ASTShowColumnsQuery.h>
#include <Parsers/ASTShowTablesQuery.h>
#include <Parsers/ASTStatisticsDeclaration.h>
#include <Parsers/ASTStreamSettings.h>
#include <Parsers/ASTSubquery.h>
#include <Parsers/ASTSystemQuery.h>
#include <Parsers/ASTTTLElement.h>
#include <Parsers/ASTTableOverrides.h>
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTTimeInterval.h>
#include <Parsers/ASTTransactionControl.h>
#include <Parsers/ASTUpdateQuery.h>
#include <Parsers/ASTUseQuery.h>
#include <Parsers/ASTViewTargets.h>
#include <Parsers/ASTWindowDefinition.h>
#include <Parsers/ASTWithElement.h>
#include <Parsers/Access/ASTUserNameWithHost.h>
#include <Parsers/CommonParsers.h>
#include <Parsers/Lexer.h>
#include <Common/Exception.h>
#include <base/scope_guard.h>
#include <Poco/Exception.h>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Parser.h>
#include <unordered_map>
#include <functional>
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int TOO_DEEP_AST;
extern const int TOO_BIG_AST;
}
/// Thread-local limits for JSON AST deserialization depth and element count.
/// These are set by the top-level createFromJSON overload with limits,
/// and checked in the recursive createFromJSON(Object) overload.
static thread_local size_t json_deser_max_depth = 0;
static thread_local size_t json_deser_max_elements = 0;
static thread_local size_t json_deser_current_depth = 0;
static thread_local size_t json_deser_current_elements = 0;
namespace
{
using ASTCreator = std::function<ASTPtr()>;
/// Registry: maps JSON type name -> factory function that creates an empty AST node.
/// After creation, readJSON is called on the node to populate it.
const std::unordered_map<String, ASTCreator> & getASTFactory()
{
static const std::unordered_map<String, ASTCreator> factory =
{
/// Expression types
{"ExpressionList", [] { return make_intrusive<ASTExpressionList>(); }},
{"Literal", [] { return make_intrusive<ASTLiteral>(Field{}); }},
{"Identifier", [] { return make_intrusive<ASTIdentifier>("_placeholder"); }},
{"TableIdentifier", [] { return make_intrusive<ASTTableIdentifier>("_placeholder"); }},
{"Function", [] { return make_intrusive<ASTFunction>(); }},
{"Subquery", [] { return make_intrusive<ASTSubquery>(); }},
{"QueryParameter", [] { return make_intrusive<ASTQueryParameter>("", ""); }},
{"Asterisk", [] { return make_intrusive<ASTAsterisk>(); }},
{"QualifiedAsterisk", [] { return make_intrusive<ASTQualifiedAsterisk>(); }},
/// Query types
{"SelectQuery", [] { return make_intrusive<ASTSelectQuery>(); }},
{"SelectWithUnionQuery", [] { return make_intrusive<ASTSelectWithUnionQuery>(); }},
{"SelectIntersectExceptQuery", [] { return make_intrusive<ASTSelectIntersectExceptQuery>(); }},
{"ProjectionSelectQuery", [] { return make_intrusive<ASTProjectionSelectQuery>(); }},
{"SetQuery", [] { return make_intrusive<ASTSetQuery>(); }},
{"ExplainQuery", [] { return make_intrusive<ASTExplainQuery>(ASTExplainQuery::ParsedAST); }},
{"WithElement", [] { return make_intrusive<ASTWithElement>(); }},
/// Table/Join types
{"TablesInSelectQuery", [] { return make_intrusive<ASTTablesInSelectQuery>(); }},
{"TablesInSelectQueryElement", [] { return make_intrusive<ASTTablesInSelectQueryElement>(); }},
{"TableExpression", [] { return make_intrusive<ASTTableExpression>(); }},
{"TableJoin", [] { return make_intrusive<ASTTableJoin>(); }},
{"ArrayJoin", [] { return make_intrusive<ASTArrayJoin>(); }},
{"OrderByElement", [] { return make_intrusive<ASTOrderByElement>(); }},
{"StorageOrderByElement", [] { return make_intrusive<ASTStorageOrderByElement>(); }},
{"WindowDefinition", [] { return make_intrusive<ASTWindowDefinition>(); }},
{"WindowListElement", [] { return make_intrusive<ASTWindowListElement>(); }},
{"SampleRatio", [] { return make_intrusive<ASTSampleRatio>(); }},
{"InterpolateElement", [] { return make_intrusive<ASTInterpolateElement>(); }},
{"Collation", [] { return make_intrusive<ASTCollation>(); }},
/// Column matcher/transformer types
{"ColumnsRegexpMatcher", [] { return make_intrusive<ASTColumnsRegexpMatcher>(); }},
{"ColumnsListMatcher", [] { return make_intrusive<ASTColumnsListMatcher>(); }},
{"QualifiedColumnsRegexpMatcher", [] { return make_intrusive<ASTQualifiedColumnsRegexpMatcher>(); }},
{"QualifiedColumnsListMatcher", [] { return make_intrusive<ASTQualifiedColumnsListMatcher>(); }},
{"ColumnsTransformerList", [] { return make_intrusive<ASTColumnsTransformerList>(); }},
{"ColumnsApplyTransformer", [] { return make_intrusive<ASTColumnsApplyTransformer>(); }},
{"ColumnsExceptTransformer", [] { return make_intrusive<ASTColumnsExceptTransformer>(); }},
{"ColumnsReplaceTransformer", [] { return make_intrusive<ASTColumnsReplaceTransformer>(); }},
{"ColumnsReplaceTransformerReplacement", [] { return make_intrusive<ASTColumnsReplaceTransformer::Replacement>(); }},
/// DDL types
{"ColumnDeclaration", [] { return make_intrusive<ASTColumnDeclaration>(); }},
{"IndexDeclaration", [] { return make_intrusive<ASTIndexDeclaration>(); }},
{"ConstraintDeclaration", [] { return make_intrusive<ASTConstraintDeclaration>(); }},
{"ProjectionDeclaration", [] { return make_intrusive<ASTProjectionDeclaration>(); }},
{"StatisticsDeclaration", [] { return make_intrusive<ASTStatisticsDeclaration>(); }},
{"Columns definition", [] { return make_intrusive<ASTColumns>(); }},
{"Storage", [] { return make_intrusive<ASTStorage>(); }},
{"CreateQuery", [] { return make_intrusive<ASTCreateQuery>(); }},
{"CreateIndexQuery", [] { return make_intrusive<ASTCreateIndexQuery>(); }},
{"DropIndexQuery", [] { return make_intrusive<ASTDropIndexQuery>(); }},
{"CreateSQLFunctionQuery", [] { return make_intrusive<ASTCreateSQLFunctionQuery>(); }},
{"CreateWasmFunctionQuery", [] { return make_intrusive<ASTCreateWasmFunctionQuery>(); }},
{"DropQuery", [] { return make_intrusive<ASTDropQuery>(); }},
{"InsertQuery", [] { return make_intrusive<ASTInsertQuery>(); }},
{"AlterCommand", [] { return make_intrusive<ASTAlterCommand>(); }},
{"AlterQuery", [] { return make_intrusive<ASTAlterQuery>(); }},
{"RenameQuery", [] { return make_intrusive<ASTRenameQuery>(); }},
{"NameTypePair", [] { return make_intrusive<ASTNameTypePair>(); }},
{"DataType", [] { return make_intrusive<ASTDataType>(); }},
{"EnumDataType", [] { return make_intrusive<ASTEnumDataType>(); }},
{"TupleDataType", [] { return make_intrusive<ASTTupleDataType>(); }},
{"FunctionWithKeyValueArguments", [] { return make_intrusive<ASTFunctionWithKeyValueArguments>(); }},
{"Pair", [] { return make_intrusive<ASTPair>(); }},
{"TTLElement", [] { return make_intrusive<ASTTTLElement>(); }},
{"Partition", [] { return make_intrusive<ASTPartition>(); }},
/// Dictionary types
{"DictionaryRange", [] { return make_intrusive<ASTDictionaryRange>(); }},
{"DictionaryLifetime", [] { return make_intrusive<ASTDictionaryLifetime>(); }},
{"DictionaryLayout", [] { return make_intrusive<ASTDictionaryLayout>(); }},
{"DictionarySettings", [] { return make_intrusive<ASTDictionarySettings>(); }},
{"Dictionary", [] { return make_intrusive<ASTDictionary>(); }},
{"DictionaryAttributeDeclaration", [] { return make_intrusive<ASTDictionaryAttributeDeclaration>(); }},
/// System/Show/Control types
{"SystemQuery", [] { return make_intrusive<ASTSystemQuery>(); }},
{"ShowTablesQuery", [] { return make_intrusive<ASTShowTablesQuery>(); }},
{"ShowColumnsQuery", [] { return make_intrusive<ASTShowColumnsQuery>(); }},
{"KillQueryQuery", [] { return make_intrusive<ASTKillQueryQuery>(); }},
{"OptimizeQuery", [] { return make_intrusive<ASTOptimizeQuery>(); }},
{"DeleteQuery", [] { return make_intrusive<ASTDeleteQuery>(); }},
{"UpdateQuery", [] { return make_intrusive<ASTUpdateQuery>(); }},
{"UseQuery", [] { return make_intrusive<ASTUseQuery>(); }},
{"TransactionControl", [] { return make_intrusive<ASTTransactionControl>(); }},
{"CheckTableQuery", [] { return make_intrusive<ASTCheckTableQuery>(); }},
{"CheckAllTablesQuery", [] { return make_intrusive<ASTCheckAllTablesQuery>(); }},
/// Misc types
{"BackupQuery", [] { return make_intrusive<ASTBackupQuery>(); }},
{"ViewTargets", [] { return make_intrusive<ASTViewTargets>(); }},
{"SQLSecurity", [] { return make_intrusive<ASTSQLSecurity>(); }},
{"UserNameWithHost", [] { return make_intrusive<ASTUserNameWithHost>(); }},
{"UserNamesWithHost", [] { return make_intrusive<ASTUserNamesWithHost>(); }},
{"StreamSettings", [] { return make_intrusive<ASTStreamSettings>(); }},
{"RefreshStrategy", [] { return make_intrusive<ASTRefreshStrategy>(); }},
{"TimeInterval", [] { return make_intrusive<ASTTimeInterval>(); }},
{"Assignment", [] { return make_intrusive<ASTAssignment>(); }},
{"TableOverride", [] { return make_intrusive<ASTTableOverride>(); }},
{"TableOverrideList", [] { return make_intrusive<ASTTableOverrideList>(); }},
{"ForeignKeyDeclaration", [] { return make_intrusive<ASTForeignKeyDeclaration>(); }},
{"IdentifierTypePair", [] { return make_intrusive<ASTIdentifierTypePair>(); }},
};
return factory;
}
}
namespace
{
/// The serialized JSON wraps every AST edge in extra brackets (a `children` array plus the
/// child object — about two bracket levels per AST level) and nests structured `Field` literals
/// further (about two more bracket levels per `Field` level, see `readFieldFromObjectImpl`).
/// So the raw-text bracket depth of a *valid* serialized AST can reach a small multiple of its
/// AST-node depth. The pre-scan below is only a stack-overflow guard for the Poco JSON parser
/// (the real AST-node and `Field` depth limits are enforced during construction), so its budget
/// must be a safe over-approximation of that multiple — otherwise lowering `max_ast_depth` would
/// reject ASTs that `checkDepth(max_ast_depth)` accepts and break the
/// `parseQueryToJSON` -> `formatQueryFromJSON` round trip. A factor of 8 leaves ample headroom
/// over the ~4x worst case.
constexpr size_t JSON_NESTING_BUDGET_PER_AST_LEVEL = 8;
}
/// Stack-safety ceiling on JSON AST deserialization nesting, applied when `max_ast_depth = 0`
/// disables the semantic AST depth check. Deserialization recurses over the JSON document
/// (`Poco::JSON::Parser`, `createFromJSON`, structured `Field` values, `Field::restoreFromDump`),
/// so an unbounded payload would exhaust the stack rather than fail with an exception. `0` therefore
/// means "no semantic AST depth limit", not "no recursion bound at all".
static constexpr size_t JSON_DESERIALIZATION_SAFETY_MAX_DEPTH = DBMS_DEFAULT_MAX_PARSER_DEPTH;
/// The depth bound actually enforced during JSON AST deserialization: the configured
/// `max_ast_depth`, or the safety ceiling above when it is disabled. Never 0.
static size_t effectiveJSONDeserializationMaxDepth()
{
return json_deser_max_depth ? json_deser_max_depth : JSON_DESERIALIZATION_SAFETY_MAX_DEPTH;
}
namespace
{
/// JSON bracket-nesting budget for the raw pre-scan, derived from the AST depth limit.
/// Saturates instead of overflowing when `max_ast_depth` is set close to `SIZE_MAX`.
size_t jsonNestingBudget(size_t max_ast_depth)
{
if (max_ast_depth > std::numeric_limits<size_t>::max() / JSON_NESTING_BUDGET_PER_AST_LEVEL)
return std::numeric_limits<size_t>::max();
return max_ast_depth * JSON_NESTING_BUDGET_PER_AST_LEVEL;
}
/// Maximum nesting depth of `{`/`[` in a raw JSON string, ignoring brackets inside string
/// literals. Used to bound recursion before handing the text to the JSON parser.
size_t computeJSONNestingDepth(const String & json)
{
size_t depth = 0;
size_t max_depth = 0;
bool in_string = false;
bool escaped = false;
for (char c : json)
{
if (in_string)
{
if (escaped)
escaped = false;
else if (c == '\\')
escaped = true;
else if (c == '"')
in_string = false;
continue;
}
if (c == '"')
in_string = true;
else if (c == '{' || c == '[')
{
++depth;
max_depth = std::max(max_depth, depth);
}
else if ((c == '}' || c == ']') && depth > 0)
--depth;
}
return max_depth;
}
}
ASTPtr IAST::createFromJSON(const String & json)
{
/// `Poco::JSON::Parser::setDepth` does not actually bound recursion in our Poco fork
/// (`ParserImpl` stores `_depth` but `handle`/`handleObject`/`handleArray` never read it),
/// so a hostile deeply-nested payload would recurse through the parser and overflow the
/// stack before any AST-level depth check runs. Enforce a raw-text bracket budget first.
/// The budget is a safe multiple of the effective depth limit (the JSON encoding adds bracket
/// levels per AST/`Field` level), not the limit itself, so a valid serialized AST is never
/// rejected here — the constructed AST depth is still bounded by the counter check below.
/// The effective limit falls back to the stack-safety ceiling when `max_ast_depth = 0`, so the
/// guard cannot be turned off by a setting.
const size_t json_nesting_budget = jsonNestingBudget(effectiveJSONDeserializationMaxDepth());
if (computeJSONNestingDepth(json) > json_nesting_budget)
throw Exception(ErrorCodes::TOO_DEEP_AST,
"JSON nesting depth exceeds the limit derived from max_ast_depth ({}) during JSON AST deserialization", json_nesting_budget);
Poco::JSON::Parser parser;
/// Also request the parser-level bound (kept for forward compatibility if the fork starts
/// honouring it); the pre-scan above is the actual enforcement.
parser.setDepth(json_nesting_budget);
Poco::Dynamic::Var result;
try
{
result = parser.parse(json);
}
catch (const Poco::Exception & e)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Failed to parse JSON for AST deserialization: {}", e.displayText());
}
Poco::JSON::Object::Ptr obj;
try
{
obj = result.extract<Poco::JSON::Object::Ptr>();
}
catch (const Poco::Exception &)
{
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Expected a JSON object for AST deserialization, got a different type");
}
if (!obj)
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Expected a JSON object for AST deserialization");
return createFromJSON(*obj);
}
ASTPtr IAST::createFromJSON(const Poco::JSON::Object & json)
{
/// Check depth limit during recursive construction. When `max_ast_depth = 0` the semantic limit
/// is disabled, but the stack-safety ceiling still applies.
if (const size_t max_depth = effectiveJSONDeserializationMaxDepth(); json_deser_current_depth >= max_depth)
throw Exception(ErrorCodes::TOO_DEEP_AST,
"JSON AST deserialization exceeded maximum depth limit ({})", max_depth);
/// Check element count limit.
if (json_deser_max_elements && json_deser_current_elements >= json_deser_max_elements)
throw Exception(ErrorCodes::TOO_BIG_AST,
"JSON AST deserialization exceeded maximum element count limit ({})", json_deser_max_elements);
++json_deser_current_depth;
++json_deser_current_elements;
/// Decrement the depth counter on every exit path (including exceptions),
/// so a failed `readJSON` does not leak depth state for subsequent calls.
SCOPE_EXIT({ --json_deser_current_depth; });
if (!json.has("type"))
throw Exception(ErrorCodes::BAD_ARGUMENTS, "JSON object missing 'type' field for AST deserialization");
String type = json.getValue<String>("type");
const auto & factory = getASTFactory();
auto it = factory.find(type);
if (it == factory.end())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unknown AST node type in JSON: '{}'", type);
/// Create empty instance.
ASTPtr node = it->second();
/// Populate from JSON via virtual dispatch.
/// readJSON may recursively call createFromJSON for child nodes.
node->readJSON(json);
return node;
}
ASTPtr IAST::createFromJSON(const String & json, size_t max_depth, size_t max_elements)
{
/// Set thread-local limits for recursive deserialization.
json_deser_max_depth = max_depth;
json_deser_max_elements = max_elements;
json_deser_current_depth = 0;
json_deser_current_elements = 0;
try
{
auto result = createFromJSON(json);
/// Reset limits after successful deserialization.
json_deser_max_depth = 0;
json_deser_max_elements = 0;
return result;
}
catch (...)
{
/// Reset limits on failure too.
json_deser_max_depth = 0;
json_deser_max_elements = 0;
throw;
}
}
size_t getJSONDeserializationMaxDepth()
{
return effectiveJSONDeserializationMaxDepth();
}
void countJSONDeserializationElement()
{
if (json_deser_max_elements && json_deser_current_elements >= json_deser_max_elements)
throw Exception(ErrorCodes::TOO_BIG_AST,
"JSON AST deserialization exceeded maximum element count limit ({})", json_deser_max_elements);
++json_deser_current_elements;
}
size_t getJSONDeserializationRemainingElements()
{
if (!json_deser_max_elements)
return std::numeric_limits<size_t>::max();
if (json_deser_current_elements >= json_deser_max_elements)
return 0;
return json_deser_max_elements - json_deser_current_elements;
}
bool isClickHouseJSONSetEscape(const char * begin, const char * end, size_t max_query_size)
{
/// In the `clickhouse_json` dialect, a leading `SET` query is an escape hatch back to a
/// SQL dialect. Detect it via the lexer so that leading SQL comments and whitespace are
/// skipped exactly as normal parsing would (e.g. `-- switch back\nSET dialect = 'clickhouse'`
/// or `/* */ SET ...`), rather than only stripping ASCII whitespace.
/// `max_query_size` bounds the lexer so that an oversized leading comment cannot make it scan
/// far past the per-query limit before the caller's own `max_query_size` guard runs.
Lexer lexer(begin, end, max_query_size);
Token token = lexer.nextToken();
while (!token.isEnd() && !token.isError() && !token.isSignificant())
token = lexer.nextToken();
if (token.type != TokenType::BareWord)
return false;
std::string_view text(token.begin, token.size());
if (text.size() != 3)
return false;
return (text[0] == 'S' || text[0] == 's')
&& (text[1] == 'E' || text[1] == 'e')
&& (text[2] == 'T' || text[2] == 't');
}
}