Repository navigation
AST fuzzer oracle: recognise an aggregate hidden in an APPLY column transformer - #118224
Conversation
… transformer `SELECT DISTINCT a.* APPLY any FROM t WHERE 100` aggregates every column, but the aggregate is not an `ASTFunction`: `ASTColumnsApplyTransformer` keeps only the function name (or a lambda that is not among its children). `scanAggregatesSafe` missed it, so `hasAggregates` returned false and the query went through the TLP DISTINCT oracle, where the `WHERE not(100)` partition still yields the single row every aggregate returns on empty input, and the oracle reported a false `AST_FUZZER_ORACLE_MISMATCH`. Record such a transformer as an aggregate when its function name resolves to an aggregate function (combinators included) or its lambda contains one. `hasAggregates` then reports it, the non-aggregate oracles skip the query, and `checkTLPAggregate` rejects it since the node is not a rewritable function. Seen in https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111327&sha=54552ebb872436c9c49b255a436fbef738a967b8&name_0=PR&name_1=AST%20fuzzer%20(amd_release,%20oracle)
|
Workflow [PR], commit [cca33c9] Summary: ✅
AI ReviewSummaryThis PR teaches Findings
Final VerdictStatus:
LLVM Coverage ReportMeasured on commit cca33c9.
Changed lines: Changed C/C++ lines covered: 13/14 (92.86%) · Uncovered code |
| /// every aggregate returns on no input — a false mismatch. Record the | ||
| /// transformer itself as an aggregate: `hasAggregates` then reports it, and | ||
| /// `checkTLPAggregate` rejects it because it is not a rewritable function. | ||
| if (const auto * apply = ast->as<ASTColumnsApplyTransformer>()) |
There was a problem hiding this comment.
This still leaves ASTColumnsApplyTransformer only partially visible to the oracle safety gates. QueryOracleChecker runs on the raw fuzzed_ast, but execution expands APPLY later (translateQualifiedNames / QueryTreeBuilder) and then resolves SQL UDFs. So a shape like CREATE FUNCTION f AS x -> any(x); SELECT DISTINCT a.* APPLY f FROM t AS a WHERE 100 is still classified as non-aggregate here and can reach the non-aggregate TLP oracles even though the executed query contains an aggregate. The same carrier problem also remains for hidden non-deterministic bodies such as APPLY rand or APPLY (x -> rand()), because hasNonDeterministicFunctionsImpl still only walks children.
I think this needs a shared traversal for the non-child func_name / parameters / lambda fields (or a post-expansion oracle check) instead of fixing only the literal aggregate-name branch here.
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, 10 s compile time in total, 7 of them have a recent master baseline. |
…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]>
Related: #111327
Related: #99980
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
The
AST fuzzer (amd_release, oracle)job on #111327 failed with a falseTLP DISTINCT oracle mismatchonThe reference query returns one row, the partitioned
UNION DISTINCTreturns two: theWHERE not(100)partition has no input rows, and an aggregate withoutGROUP BYstill returns one row on empty input, which the reference never produces.The query is an aggregation, but
QueryOracleChecker::hasAggregatesdid not see it.scanAggregatesSafeonly recognisesASTFunctionnodes, whileSELECT * APPLY anystores the aggregate in anASTColumnsApplyTransformeras a bare function name (or as a lambda that is not among the node's children). So the query was routed to the non-aggregate TLP / DISTINCT oracles, whose partitioning is invalid for aggregates.The fix records such a transformer as an aggregate when its function name resolves to an aggregate function (combinators included, via
AggregateFunctionFactory::isAggregateFunctionName) or when its lambda body contains one.hasAggregatesthen reports it, the non-aggregate oracles skip the query, andcheckTLPAggregaterejects it because the node is not a rewritableASTFunction. Window functions found inside the lambda are propagated too. A non-aggregateAPPLYsuch asAPPLY toStringstays eligible for the oracles.The new test
05097_ast_fuzzer_oracle_apply_aggregateruns the reported shape (and theAPPLY max, lambda andminOrNullcombinator forms) withast_fuzzer_oracle = 1; the server trace on the private build shows the queries classified asagg=trueand only the aggregate-safe identity / subquery-wrap oracles running.APPLY toStringstill goes through the TLP WHERE oracle and passes.Failing report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111327&sha=54552ebb872436c9c49b255a436fbef738a967b8&name_0=PR&name_1=AST%20fuzzer%20(amd_release,%20oracle)
Workflow [PR]
Sync PR [sync-upstream/pr/118224]
Version info
26.9.1.779(included in26.9and later)