Repository navigation
Support query parameters in the SETTINGS clause and SET queries - #108760
Conversation
Query parameters ({name:Type} substitutions) can now be used as setting
values, both in the SETTINGS clause of a query and in standalone SET
queries, e.g.
SELECT ... SETTINGS max_threads = {threads:UInt64}
SET max_threads = {threads:UInt64}
The parser stores such a value as an ASTQueryParameter wrapped into a
Field (the same mechanism already used for `disk(...)` settings). It is
resolved into the concrete value by ReplaceQueryParameterVisitor, both
during server-side query-parameter substitution and when settings from a
query are applied on the client side.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [02437e2] Summary: ✅
AI ReviewSummaryThis PR extends query-parameter substitution to setting values in Findings
Final Verdict
|
- Resolve setting query parameters on a copy of the changes instead of mutating the AST, so the placeholders are preserved for the old-server compatibility rewrite in ClientBase::processOrdinaryQuery (review major #1). - Pass the changes to checkSettingsConstraints as const: the non-const overload rewrites the list and drops no-op changes, which lost the "changed" flag for a setting explicitly set to its current value (e.g. SET log_queries = 1) and broke 03595_set_query_no_eq_set_to_one. - Teach QueryParameterVisitor / analyzeReceiveQueryParams to discover query parameters used as setting values (stored inside FieldFromAST), so predefined HTTP handlers accept param_<name> for them (review major #2). - Renumber the stateless test to 04416 to satisfy the test-numbers style check. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the review feedback in 9d31e4f:
I also fixed the root cause of the |
Address review feedback: add focused coverage for the
`analyzeReceiveQueryParams` / predefined HTTP handler path. A predefined
HTTP handler builds `receive_params` from `analyzeReceiveQueryParams` over
its query and only substitutes `param_<name>` values that the scanner
discovers, so a setting placeholder hidden inside `FieldFromAST` must be
found by the new `QueryParameterVisitor::visitSetQuery`.
The new predefined handler runs
`SELECT getSetting('max_threads') SETTINGS max_threads = {threads:UInt64}`,
and the test passes `param_threads=5` over HTTP and asserts the setting is
substituted. Without the `visitSetQuery` discovery, `threads` would not be
in `receive_params` and the parameter would be silently dropped.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The query-parameter substitution for setting values is wired only through `ParserSetQuery` (`parseNameValuePairWithParameterOrDefault`), which covers standalone `SET` and the `SETTINGS` clause of regular queries (`SELECT`, `INSERT`, `EXPLAIN`, ...). The `SETTINGS` lists of `BACKUP` / `RESTORE` (and other carriers such as `CREATE TABLE ... SETTINGS`) go through the separate `ParserSetQuery::parseNameValuePair` path and their settings AST is stored outside `children`, so query parameters are neither parsed, discovered, nor resolved there. Rather than thread the placeholder through every settings carrier, narrow the documented contract to the supported carriers and lock in the boundary with a regression test: a query parameter as a setting value in `BACKUP` / `RESTORE` `SETTINGS` is rejected with a `SYNTAX_ERROR` at parse time instead of silently misbehaving. Also renumber the stateless test from `04416` to `04489` to avoid colliding with `04416_*` tests added on master. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…E settings
The new test `04489_query_parameters_in_settings` asserted that a query
parameter used as a setting value in the `SETTINGS` clause of `BACKUP` /
`RESTORE` is rejected at parse time with a `SYNTAX_ERROR`. That boundary is
false: `ParserBackupQuery` is parsed as part of `ParserQueryWithOutput`, whose
trailing `SETTINGS` clause is parsed by `ParserSetQuery` (the same
`parseNameValuePairWithParameterOrDefault` this PR extends). So
`BACKUP ... SETTINGS max_threads = {threads:UInt64}` actually parses fine and
the parameter is accepted; it only fails later at execution if the backup disk
is not configured. In the `Fast test` environment the backup disk exists, so
the query did not emit `SYNTAX_ERROR` and the test failed.
The same is true for the engine/storage `SETTINGS` of `CREATE TABLE`, which are
also parsed by `ParserSetQuery` (`ParserStorage`): a query parameter there is
accepted and resolved (verified via `SHOW CREATE TABLE`).
The genuine boundary is the separate bare `ParserSetQuery::parseNameValuePair`
path used by `CREATE NAMED COLLECTION`, dictionaries, and similar, where a
placeholder is still rejected with a `SYNTAX_ERROR`.
Update the test to assert the real behavior (a positive `CREATE TABLE` case, a
parse-level `EXPLAIN AST` check for `BACKUP` / `RESTORE` that avoids running a
backup, and a negative `CREATE NAMED COLLECTION` case) and correct the
documentation in `docs/en/sql-reference/syntax.md` accordingly.
Verified locally against a server (16/16 lines match; 5/5 runs pass with random
settings).
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108760&sha=b1aae2a8e54d4aef501f34919db0ad7d4184e61b&name_0=PR&name_1=Fast%20test
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Fixed the Root cause — the documented
So The same applies to the engine/storage The genuine boundary is the separate bare What I changed:
Verified locally against a server: 16/16 lines match, and 5/5 runs pass with random settings. If you'd rather actually exclude |
… regression The AI review (#108760) noted that the documented `BACKUP` / `RESTORE` support is broader than what is implemented: a query parameter as a setting value reaches the trailing `SETTINGS` clause (parsed by `ParserSetQuery`) only when that clause contains exclusively core query settings. `BACKUP` / `RESTORE` parse their own `SETTINGS` through the separate bare `ParserSetQuery::parseNameValuePair` path, which does not understand the `{name:Type}` placeholder. When a backup/restore-specific setting (such as `async` or `base_backup`) is combined with a parameterized core setting in the same clause, the backup-specific setting is consumed, the placeholder remainder is left unparsed, and the query is rejected with a `SYNTAX_ERROR` at parse time. That bare path is unchanged by this PR, so the rejection is identical with or without the feature. Narrow the documentation in `docs/en/sql-reference/syntax.md` to state this boundary precisely, and add a focused regression to `04489_query_parameters_in_settings` asserting that `BACKUP ... SETTINGS async = true, max_threads = {threads:UInt64}` is rejected with a `SYNTAX_ERROR`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Pushed AI Review Major —
|
…_threads_auto_parsing_compat `04489_max_threads_auto_parsing_compat` was merged to `master` after this branch's last `master` merge, so `04489_query_parameters_in_settings` now shares its number prefix. Renumber the test (and its internal `04489_*` table / named-collection identifiers) to `04492`, which is free on `master`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Renumbered the stateless test |
Address the review comment on `docs/en/sql-reference/syntax.md`: the added `<details>` block describing query parameters as setting values (including the `BACKUP` / `RESTORE` boundary) and the extra sentence in the `:::note` only complicate the docs. Revert all changes in this file so it matches `master`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
| # A query parameter (a core query setting) cannot be combined with a backup/restore-specific setting | ||
| # (such as `async` or `base_backup`) in the same BACKUP / RESTORE SETTINGS clause: those settings are | ||
| # parsed by the separate ParserSetQuery::parseNameValuePair path, which does not understand the | ||
| # {name:Type} placeholder. Here `async = true` is consumed as a backup setting and the remainder | ||
| # `, max_threads = {threads:UInt64}` is left unparsed, so the query is rejected with a SYNTAX_ERROR at | ||
| # parse time rather than misbehaving. (This path is unchanged by this PR, so the rejection is identical | ||
| # with or without the feature.) | ||
| $CLICKHOUSE_CLIENT --param_threads=4 -q "BACKUP TABLE system.one TO Disk('backups', 'b') SETTINGS async = true, max_threads = {threads:UInt64}" 2>&1 | grep -o "SYNTAX_ERROR" | head -n1 |
There was a problem hiding this comment.
The mixed BACKUP / RESTORE boundary is still order-dependent. This regression only covers SETTINGS async = true, max_threads = {threads:UInt64}, where ParserBackupQuery::parseSettings consumes async first and then leaves the placeholder remainder unparsed. If the parameterized core setting comes first (SETTINGS max_threads = {threads:UInt64}, async = true), parseSettings bails out on the first item, ParserQueryWithOutput reparses the whole clause as trailing query settings_ast, and InterpreterSetQuery::executeForCurrentContext later tries to apply async as a query setting instead of rejecting the mixed clause at parse time. That violates the narrowed contract and leaves the behavior dependent on setting order. Please either reject both orderings consistently in the parser or narrow the promise and add a regression for the reversed order.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 92/93 (98.92%) · Uncovered code |
… regression The AI review (ClickHouse#108760) noted that the documented `BACKUP` / `RESTORE` support is broader than what is implemented: a query parameter as a setting value reaches the trailing `SETTINGS` clause (parsed by `ParserSetQuery`) only when that clause contains exclusively core query settings. `BACKUP` / `RESTORE` parse their own `SETTINGS` through the separate bare `ParserSetQuery::parseNameValuePair` path, which does not understand the `{name:Type}` placeholder. When a backup/restore-specific setting (such as `async` or `base_backup`) is combined with a parameterized core setting in the same clause, the backup-specific setting is consumed, the placeholder remainder is left unparsed, and the query is rejected with a `SYNTAX_ERROR` at parse time. That bare path is unchanged by this PR, so the rejection is identical with or without the feature. Narrow the documentation in `docs/en/sql-reference/syntax.md` to state this boundary precisely, and add a focused regression to `04489_query_parameters_in_settings` asserting that `BACKUP ... SETTINGS async = true, max_threads = {threads:UInt64}` is rejected with a `SYNTAX_ERROR`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…arams Support query parameters in the SETTINGS clause and SET queries
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Query parameters (
{name:Type}substitutions) can now be used as setting values, both in theSETTINGSclause of a query (such asSELECTorINSERT) and in standaloneSETqueries, e.g.SELECT ... SETTINGS max_threads = {threads:UInt64}andSET max_threads = {threads:UInt64}.Description
Previously, query parameters could be used in place of identifiers and literals in
SELECTstatements, but not as setting values —SELECT ... SETTINGS max_threads = {threads:UInt64}failed with a syntax error. This PR makes them work as setting values.How it works (reusing the existing mechanism for
disk = disk(...)settings):ParserSetQuery): a setting value may now be a{name:Type}substitution, stored as anASTQueryParameterwrapped into aField(FieldFromAST).ReplaceQueryParameterVisitor): a newvisitSetQuery/visitSettingsChangesresolves such wrapped parameters into concreteFieldvalues during the normal server-side query-parameter substitution pass inexecuteQuery.InterpreterSetQuery,ClientBase): the client applies settings from a query before the server substitutes parameters, so settings are also resolved at the settings-application layer using the context's query parameters. This makes the feature work throughclickhouse-clientandclickhouse-local.disk = disk(...)settings (the other AST-valued setting) are explicitly left untouched.Scope. The feature works wherever setting values are parsed by
ParserSetQuery— standaloneSET, theSETTINGSclause of a query (including the trailingSETTINGSclause attached byParserQueryWithOutput, which coversSELECT/INSERT/EXPLAIN), and the engine/storageSETTINGSofCREATE TABLE.For
BACKUP/RESTORE, a query parameter works only when theSETTINGSclause contains exclusively core query settings (such asmax_threads), which fall through to that trailingSETTINGSclause. It cannot be combined with backup/restore-specific settings (such asasyncorbase_backup) in the same clause: those settings are parsed by the separate bareParserSetQuery::parseNameValuePairpath, which does not understand the{name:Type}placeholder, so a mixed clause is rejected with aSYNTAX_ERRORat parse time.The other boundary is that same bare
ParserSetQuery::parseNameValuePairpath used byCREATE NAMED COLLECTIONand dictionarySETTINGS: a query parameter as a setting value there is rejected with aSYNTAX_ERRORat parse time rather than silently misbehaving. Both boundaries are covered by the regression test.Examples:
Parameters passed via
--param_*/ the HTTPparam_*interface / a precedingSET param_*are all supported. Type mismatches raiseBAD_QUERY_PARAMETER; missing parameters raiseUNKNOWN_QUERY_PARAMETER.Added stateless test
04492_query_parameters_in_settingscovering the supported carriers (SELECT/SET/CREATE TABLEstorage settings /BACKUP/RESTORE) and theCREATE NAMED COLLECTIONboundary case, an integration case intest_http_handlers_configcovering query parameters in theSETTINGSclause of a predefined HTTP handler query (so thatanalyzeReceiveQueryParamsdiscovers the placeholder and the handler acceptsparam_<name>).🤖 Generated with Claude Code