Repository navigation
AST fuzzer oracle: walk the hidden APPLY members in every eligibility screen - #122055
Conversation
…n `APPLY` lambda
`QueryOracleChecker` already refuses a query whose `WHERE` or `HAVING` defines an
alias that the rest of the query uses, because an oracle which removes that clause
rebinds the identifier to a same-named table column and the two executions stop
being comparable. The guard, `clauseDefinesAliasUsedElsewhere` into
`referencesAnyAlias`, is a generic `children` walk, and
`ASTColumnsApplyTransformer` copies `parameters` and `lambda` by hand in `clone`
instead of registering them as `children`. A use inside an `APPLY` lambda is
therefore invisible to the guard, while the analyzer does resolve that lambda
against the parent query scope. The six clause-removing oracles then proceed and a
valid query is rejected with a false `AST_FUZZER_ORACLE_MISMATCH`.
Measured on `t(i Int32, a Int32)` holding `(1,100),(2,201),(3,302)`, where the
column deliberately differs from the alias so the rebinding changes rows rather
than spelling:
SELECT * APPLY (x -> toInt32(x) + a) FROM t WHERE (i * 100 AS a) > 1
raised `Code: 906. TLP WHERE oracle mismatch!` with
`Only in partitioned: 202 401` (the alias `i * 100`) against
`Only in reference: 203 402` (the column). Over 8 fuzzed rounds per shape,
`ASTFuzzerOracleMismatches` was 6 for that spelling, 6 for `i + 1 AS a` and 6 for
the `COLUMNS('i')` form, and 0 after this change, while the alias-free control
kept its oracle checks (19 before, 31 after) so no coverage is traded away. The
same before and after pair holds for a `Nullable` or `LowCardinality` shadowing
column, an alias used in a nested lambda or in a scalar subquery inside the
`APPLY` lambda, and two chained transformers.
The loop is added to the traversal rather than to any oracle, so all seven call
sites in this file, that is six clause-removing oracles, are covered once. Making
`lambda` and `parameters` real `children` would fix every `children` walk in the
server at once, but it would newly descend every AST visitor into the lambda, so
the narrower per-screen route of ClickHouse#118224 and ClickHouse#121897 is kept here.
`hiddenApplyMembers` is copied byte-identically from ClickHouse#121897, which was still open
when this was written, at the same insertion point, so whichever merges second
resolves a duplicate definition by deleting one copy.
A third carrier of the same blindness, `arrayJoin` hidden in an `APPLY` lambda,
defeats `expressionContainsArrayJoin` in the same way and is measured, but that
helper is shared by eight non-oracle callers such as row policies, TTL and
constraints, so teaching it this node has a different blast radius and belongs
with ClickHouse#117791.
Related: ClickHouse#121897
Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings;
Severity: ❌ blocker / ⚠ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are Session id: cron:clickhouse-review-self-slot-58:20260924-163700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20260924-153500 |
|
Workflow [PR], commit [06a34e6] Summary: ❌
AI ReviewSummary
Final Verdict✅ Approve. |
|
cc @qoega |
CI finish ledger - 0f9d4e4Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260924-210057 |
|
Fixing PR for the |
…ly-lambda Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master removed the stateless tests of the AST fuzzer oracle in 91dc673 (a testing tool, not server behavior; 04658 is kept as the only one), so this PR no longer adds one. Validated locally instead: the same queries fail 10/10 with `Code: 906` on master and pass 50/50 under randomized settings with the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@groeneai 🕵️ This fixes the alias screen, but the same blind spot is in most of the other screens in this file. On master, only
A lambda can hold a scalar subquery, so for example (from reading the code, not run): SELECT * APPLY (x -> x + (SELECT count() FROM system.processes)) FROM t WHERE i > 1;
SELECT * APPLY (x -> x + (SELECT count() FROM t2 SETTINGS max_rows_to_read = 10, read_overflow_mode = 'break')) FROM t WHERE i > 1;skip the system-table and truncating-settings screens, which leads to the same kind of false mismatch this PR fixes. Instead of a PR per screen, could |
`ASTColumnsApplyTransformer` keeps `lambda` and `parameters` outside
`children`, so every screen in `QueryOracleChecker.cpp` that recursed over
`children` missed what an `APPLY` lambda holds (a scalar subquery reading
`system`, `FINAL`, an `ANY` join, a view over `rand()`, inline `SETTINGS`,
`WITH FILL`, a `Distributed` table, a window without `ORDER BY`, an alias).
Only `hasNonDeterministicFunctionsImpl` and `scanAggregatesSafe` read them,
each with its own inline block.
`anyChild` / `forEachChild` visit `children` plus those two members, and
every recursive screen uses them, including the two that had inline blocks.
`scanAggregatesSafe` still records the transformer itself, scanning its
members into a separate result. `containsIdentifier` keeps walking
`children` only: it proves a window `ORDER BY` key is not constant, and the
identifiers of an `APPLY` lambda include its own parameter, so
`ORDER BY COLUMNS('a') APPLY (x -> 1)` would pass as ordered.
`referencesAnyAlias` also matches a function name and the bare `APPLY`
function name: an alias of a lambda defined in `WHERE` is callable, and
removing the `WHERE` rebinds `identity(x)` to the builtin `identity`.
Requested on ClickHouse#122055. Supersedes the per-screen loops of ClickHouse#121897.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
WHERE whose alias is used inside an APPLY lambdaAPPLY members in every eligibility screen
|
Done in 06a34e6. Measured on master and on this commit, 6 fuzzed rounds per query: your two examples were checked 12 and 17 times on master and 0 times now. The same holds for Two exceptions:
This supersedes #121897. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 12 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 06a34e6Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261015-183012 |
|
@qoega LGTM. Can you give a final look? |
2239bc3
`ASTColumnsApplyTransformer` keeps `lambda` and `parameters` outside `children`. Every recursive screen in this file now walks them through `forEachChild` (master, ClickHouse#122055); `collectReadParts` still walked `children` only, so a table read inside an `APPLY` lambda, e.g. `SELECT * APPLY (x -> (SELECT count() FROM t)) FROM s`, was missing from the part snapshot and a mismatch caused by a new part of `t` was reported instead of skipped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #121897
Related: #122118
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
ASTColumnsApplyTransformerkeeps itslambdaandparametersoutsidechildren. Only twoscreens of
QueryOracleCheckerread them, so whatever anAPPLYlambda holds passed every otherscreen. Oracle checks on master, 6 fuzzed rounds per query, with
t(i Int32, a Int32)=(1,100),(2,201),(3,302):APPLYlambdasystem.processes, withFINAL,ANY JOIN,WITH FILL, aDistributedtable or inlineSETTINGS(truncating,join_use_nulls,max_threads)rand()Code: 906WHEREalias:SELECT * APPLY (x -> toInt32(x) + a) FROM t WHERE (i * 100 AS a) > 1Code: 906Code: 906anyChild/forEachChildvisitchildrenplus those two members, and every recursive screenin the file uses them, including the inline blocks of
hasNonDeterministicFunctionsImplandscanAggregatesSafe.containsIdentifierkeepschildren: the lambda always holds its ownparameter, and walking it made
ORDER BY COLUMNS('a') APPLY (x -> 1)checkable (0 to 8 checks).The alias guard now also matches function names. With
WHERE arrayCount((y -> y > 100) AS identity, [101]) > 0, removing theWHERErebindsidentity(x)to the builtin:Code: 906in 2 to 6 of 12 rounds on master, with or withoutAPPLY, none with this PR.No stateless test, as in #125003. The fuzzer and oracle tests pass locally, except 3 that need
Keeper or transactions. This supersedes #121897.
arrayJoinin a lambda stays in #122118:expressionContainsArrayJoinis in another file and also serves row policies, constraints and TTL.Workflow [PR]
Sync PR [sync-upstream/pr/122055]
Version info
26.11.1.71-master(included in26.11and later)