Repository navigation
Bind parameterized-view parameters only from top-level equals args - #112194
PedroTadim merged 2 commits into
Conversation
The legacy-AST parameter collector `analyzeFunctionParamValues` guarded with `name != "equals" && children.size() != 1`, which only rejects when both clauses hold. Every ordinary parsed function has exactly one child (its `arguments` list), so the name check never fired: a non-`equals` function such as `concat(name, '!')` was stored as if the user wrote `name = '!'`, and the view call itself passed the guard too, so a positional `pv(name, 'a')` had the view's own argument list read as an `equals` argument list. Independently the visitor recursed into rejected subtrees, so a nested assignment inside `tuple(...)`, `... AND 1` or `if(...)` was still collected. The query-tree collector accepts only genuine `equals` assignments among the top-level arguments of the call, so execution at `enable_analyzer = 0` and `EXPLAIN SYNTAX` at default settings returned wrong rows where the analyzer correctly raised UNKNOWN_QUERY_PARAMETER. Replace the free recursion and the broken guard with a top-level-arguments-only walk mirroring `QueryAnalyzer::resolveTableFunction`, so both paths bind the same parameter set by construction. The shape check keys on `ASTFunction::arguments` rather than on `children`, because a parametric `equals(7)(name, 'a')` stores its parameters as a second child and is accepted on both paths today; a child-count test would make the legacy path start rejecting it, which is a fresh divergence in the opposite direction. Argument spellings that are not assignments now raise UNKNOWN_QUERY_PARAMETER on those paths, aligning them with the default analyzer behaviour. The documented `pv(param = value)` spelling is unaffected. Related: ClickHouse#111790 Co-Authored-By: Claude Opus 5 <[email protected]>
The `///` block added above `analyzeFunctionParamValues` asserted two invariants that this branch's own test disproves. It said the legacy-AST and analyzer paths "bind exactly the same set of parameters". They agree on argument SHAPE, not on value ACCEPTANCE: this file requires the value to already be an `ASTLiteral`, `ASTFunction` or `ASTSubquery` (unchanged from master), so a bare identifier never reaches `evaluateConstantExpression`, while the analyzer resolves the value node and accepts anything that folds to a constant. That is exactly the `WITH 'a' AS s SELECT * FROM pv(name = s)` case, whose two current verdicts 04648_parameterized_view_non_equals_argument.sql pins as divergent because the difference is pre-existing and deliberately out of scope here. It also said an argument of any other shape "makes `ReplaceQueryParameterVisitor` report `UNKNOWN_QUERY_PARAMETER`". Not for a `Nullable` parameter: `resolveParameterValueAsField` throws only when the type is not nullable and otherwise substitutes `NULL`, as `03454_parameterized_views_null.sql` exercises. The comment now states shape parity, names both consumer outcomes accurately, and records the value-acceptance difference as pre-existing and pinned. The same two claims appear in the previous commit's message, which is left as written since this branch does not rewrite history: "bind the same parameter set by construction" there should be read as the argument SHAPES, and "raise UNKNOWN_QUERY_PARAMETER" as leaving the parameter unset, which a `Nullable` parameter turns into `NULL` rather than an error. Comment text only. The comment-stripped source is byte-identical to the previous commit (sha256 7a2bf9e80835adac, versus fb9b2c4f2d302541 on master), no test assertion changes, and the test still passes.
Internal second-model review (click to expand)Two review rounds over this change, each an independent cold read followed by a second-model
Checks run beyond the findings above: the two consumers of the collector were enumerated |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-43:20260727-185900 |
|
cc @pufit @george-larionov could you review this? It makes the legacy-AST parameterized-view parameter collector accept only top-level |
|
Workflow [PR], commit [a21581c] Summary: ✅
AI ReviewSummaryThis PR narrows the legacy parameterized-view argument collector to top-level Final Verdict
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 25/27 (92.59%) · Uncovered code |
CI finish ledger - a21581cCI has fully finished on this head: 174 check-runs, 0 queued, 0 in progress,
Session id: cron:our-pr-ci-monitor:20260728-140000 |
Related: #111790
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes wrong results when a parameterized view is called with an argument that is not a
parameter = valueassignment. Withenable_analyzer = 0, and inEXPLAIN SYNTAXat default settings, calls such aspv(concat(name, '!')),pv(name, 'a'),pv(name != 'a')andpv(tuple(name = 'a'))silently bound a view parameter to an unrelated expression instead of reportingUNKNOWN_QUERY_PARAMETERas the analyzer does. Such calls are now rejected identically on both execution paths.Description
analyzeFunctionParamValuesinsrc/Parsers/FunctionParameterValuesVisitor.cppis the legacy-AST parameterized-view parameter collector. It is reached fromContext::executeTableFunction(execution atenable_analyzer = 0) and fromInterpreterExplainQuery'sExpandParameterizedViewsMatcher(EXPLAIN SYNTAX, at default settings). The query-tree collector inQueryAnalyzer::resolveTableFunctionaccepts only genuineequalsassignments among the top-level arguments of the view call, so the two paths diverged: on the legacy paths a bogus argument silently satisfied or overrode a parameter and the view returned wrong rows, while the analyzer rejected the same query withUNKNOWN_QUERY_PARAMETER.Two independent mechanisms produced the divergence.
The outer guard read
if (parameter_function.name != "equals" && parameter_function.children.size() != 1) return false;. It rejects only when the name is notequalsand the child count is not 1, and every ordinary parsed function has exactly one child (itsargumentslist), so the second conjunct was always false and the name check never fired. Two consequences followed: a non-equalsbinary function such asconcat(name, '!')was read asname = '!'; and the view call itself was not rejected either, so for a positional callpv(name, 'a')the view's own argument list was read as if it were anequalsargument list, bindingname = 'a'.Separately, the visitor recursed into every child of a rejected node, so a nested well-formed assignment was still collected:
pv(tuple(name = 'a')),pv((name = 'a') AND 1)andpv(if(1, name = 'a', name = 'b'))all bound a parameter. Correcting the guard alone does not close this.The fix replaces the free recursion and the broken guard with an explicit walk that mirrors the analyzer: iterate the top-level arguments of the view call, and accept an argument only when it is an
ASTFunctionnamedequalswhoseargumentslist holds exactly two children, the first anASTIdentifierand the second a constant expression. Any other argument shape binds nothing, which leaves the parameter unset and gives it exactly the treatment the analyzer path already gives it. The value-serialization branch is unchanged.The shape check deliberately keys on
ASTFunction::argumentsrather than onchildren[0]/children.size(). A parametric spellingequals(7)(name, 'a')stores its parameters as a second child (ASTFunction.h,addParametersToAggregateFunction) and is accepted on both paths today, so a child-count test would make the legacy path start rejecting it: a fresh divergence in the opposite direction. Three positive controls in the new test pin that shape, two of which run through this collector, and the mutation matrix below confirms those two are the only rows a child-count implementation breaks (the third is served by the query-tree collector and is insensitive to any change here).Behavior change. Argument spellings that are not assignments now leave the parameter unset on the legacy path and in
EXPLAIN SYNTAX, where they were previously accepted, so the query raisesUNKNOWN_QUERY_PARAMETER(or, for aNullableparameter, substitutesNULL, exactly as on the analyzer path). That is the point of the change: it aligns those paths with the default-settings analyzer behavior, and the documentedpv(param = value)spelling has always worked on both. No setting default moves, so noSettingsChangesHistory.cppentry is needed (that file tracksSettingsonly, and this PR touches nothing undersrc/Core/). No serialization, format or protocol change.Out of scope, pinned as unchanged.
WITH 'a' AS s SELECT * FROM pv(name = s)also diverges (UNKNOWN_QUERY_PARAMETERatenable_analyzer = 0, correct rows at= 1), but for an unrelated reason: the legacy path cannot evaluate aWITHalias to a constant. That is constant-expression evaluation, not argument-shape validation, and the new test asserts both of its current verdicts so the behavior is not silently changed here.Verification. Debug build, Build ID
53b7b30946688c2b1425ff81ba7fbd7098dc8319; base measurements on the pristine master snapshot, Build ID9ebd11fe9bc8d54774fc02e726da1a5923b741c0(byte-identicalFunctionParameterValuesVisitor.cpp,QueryAnalyzer.cpp,Context.cpp,InterpreterExplainQuery.cppandStorageView.cpptoorigin/master). The head commit adds a comment-only edit on top of the measured commit and so builds toa4bfa6f1ed8481848c71881abc184e3821981243; the comment-stripped source hash is identical for the two, and the new test was re-run on the rebuilt binary.Both directions, driven statement by statement against a server of each binary: the new test's 68 statements produce 13 failures on pristine master and 0 with the fix. The 13 are the four non-
equalsbinary/unary shapes, the uppercaseEQUALSspelling, the two positional shapes, the three nested-assignment shapes and the threeEXPLAIN SYNTAXshapes.Mutation matrix (predictions written before building each mutant; all three matched exactly):
equals-name checkequalsshapes plus theirEXPLAIN SYNTAXrow regresschildren[0]/children.size()(the naive guard flip)equalsrows regressThe matrix shows the two halves of the fix are each load-bearing, and for different rows: the
equals-name check closes the non-equalsfunctions, while the top-level-only walk closes the positional and nested shapes. Restoring the source after the matrix rebuilt to the bit-identical Build ID.50 randomized runs of the new test: 50/50 pass.
Existing-test blast radius. First, a static sweep of all of
tests/, functional and integration: every view created with a{parameter:Type}placeholder is located, then every top-level argument of every call of that view name that is not anidentifier = ...assignment. Outside the new test itself there are exactly three such arguments tree-wide, and all three arenumbers(3),numbers(10)andnumbers(100)in04105_explain_syntax_parameterized_view, wherenumbersis a registered table function and takes precedence over the same-named view before this collector is reached (that precedence is itself asserted by04105).02428_parameterized_view'sSELECT * FROM test_02428_mv1(test)is a call on a MATERIALIZED view, whose reference already expectsERROR, and it was additionally checked on both binaries and still reportsERROR. In the integration suite onlytest_parameterized_viewdefines a placeholder view, and every call there is aname = valueassignment. So no existing.referencecan change.Second, executed: the 26 pre-existing stateless tests whose sources mention
parameterized/parametrizedwere run on both binaries and produced identical failure sets: 25 pass, and02428_parameterized_viewfails on both with aReplicatedMergeTreeasync-insert timeout against a bare local server (an environment limitation, not a regression).