Skip to content

AST fuzzer oracle: skip queries with WITH TOTALS in a subquery or view - #127335

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:oracle-skip-nested-with-totals
Oct 22, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:oracle-skip-nested-with-totals

Conversation

@groeneai

@groeneai groeneai commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Stop the AST fuzzer oracles from reporting false mismatches for queries that read a WITH TOTALS result through a subquery, a CTE, a JOIN or a view.

Description

AST fuzzer (amd_release, oracle) failed on an unrelated PR with a TLP WHERE mismatch
(report)
on SELECT x, y, x, y FROM (SELECT DISTINCT -1 AS y, isNotNull(true AS x), 1 AS x, x GROUP BY GROUPING SETS ((y)) WITH TOTALS LIMIT 376) WHERE isNull(x).

A WITH TOTALS in a subquery puts a totals block into the outer result, and it is not distributive over a WHERE
partition: a filter pushed below TotalsHavingStep recomputes the totals over the filtered rows (by design), and
UNION ALL keeps the totals of its first branch only (Pipe::uniteTotals). With
T = (SELECT n % 3 AS y, count() AS c FROM t GROUP BY y WITH TOTALS) over 6 rows, SELECT y, c FROM T has totals
0 6 and the TLP union over y = 1 has 0 2.

The change applies a recursive hasWithTotalsAnywhere in QueryOracleChecker::check()
and to stored view bodies. Nested ROLLUP / CUBE / GROUPING SETS without TOTALS stay checked. The true vs 1
half of the mismatch is a separate analyzer issue (true AS x and 1 AS x accepted as one alias, addressed by
#117161) and stays visible to the oracle. #117187 has a wider screen; this is the minimal fix.

No test, as in #125003. Code: 906 mismatches per attempt of 10 fuzzed rounds:

seed master this PR
subquery with TOTALS 44/50 0/50
the CI query 48/50 3/50, alias clash only, no TOTALS
view with TOTALS 45/50 0/50
CTE with TOTALS 46/50 0/50
JOIN of a TOTALS subquery 26/30 0/30
control: nested GROUP BY, no TOTALS 1/20 (fuzzer added TOTALS) 0/20

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/127335]

Version info

  • Merged into: 26.11.1.548-master (included in 26.11 and later)

groeneai and others added 2 commits October 22, 2026 06:21
A `WITH TOTALS` inside a FROM subquery, a CTE, a JOIN side or a view body
puts a totals block into the outer query's result, which the oracles
compare. That block is not distributive over a WHERE partition: a filter
pushed below `TotalsHavingStep` recomputes the totals over the filtered
rows (deliberately, see filterPushDown.cpp), and `UNION ALL` keeps the
totals of its first branch only (`Pipe::uniteTotals`). So TLP WHERE's
reference and its partitioned union disagree on the totals row, and the
same holds for TLP DISTINCT and the distributed-plan oracle.

The oracles already rejected a top-level `WITH TOTALS`. Add a recursive
`hasWithTotalsAnywhere` screen to `check()`, next to the FINAL and WITH
FILL screens, and to the stored-definition screen so a view hiding the
same text is skipped too. Nested ROLLUP / CUBE / GROUPING SETS without
TOTALS are left checked: their data rows stay partitionable.

Seen on PR ClickHouse#110321 in `AST fuzzer (amd_release, oracle)`; reproduces on
master. Mismatching attempts (10 fuzzed rounds each, clickhouse local),
master build vs this change: subquery 44/50 -> 0/50, view 45/50 -> 0/50,
CTE 46/50 -> 0/50, JOIN 26/30 -> 0/30.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the hasWithTotalsAnywhere comment to two lines; drop the clause narrating why the check recurses.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings; 1 full pass).
My own review found no blocker or major issue.

Session id: cron:clickhouse-review-slot-8:20261022-062600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Engine level: SELECT y, c FROM T has totals 0 6, the TLP union over y = 1 has 0 2, every run. Oracle level: 44-48 of 50 clickhouse local attempts (10 fuzzed rounds each) throw AST_FUZZER_ORACLE_MISMATCH on master.
b Root cause explained? A nested WITH TOTALS puts a totals block into the compared output. A filter pushed below TotalsHavingStep recomputes it over the filtered rows and UNION ALL keeps the first branch's totals, so it differs between the reference and the partitioned union. Only a top-level WITH TOTALS was screened.
c Fix matches root cause? Yes. The engine behaviour is intended, so the fix is in the oracle's eligibility screen: the totals block is outside the partition invariant.
d Test intent preserved / new tests added? No new test (oracle fix, as in #125003). Existing oracle tests 04658_* and 05339_* pass on the fix build.
e Both directions demonstrated? Yes. Master build vs fix build, same harness: subquery 44/50 -> 0/50, view 45/50 -> 0/50, CTE 46/50 -> 0/50, JOIN 26/30 -> 0/30. Trace logs show Oracle skip: WITH TOTALS firing on the fix build.
f Fix is general across code paths? One recursive walk in check() covers every oracle and every place a SELECT can sit (FROM, JOIN, CTE, APPLY). Stored view bodies are covered by the definition screen.
g Fix generalizes across inputs (params/datatypes/wrappers)? Subquery, CTE, view and JOIN measured, at any depth; a TOTALS the fuzzer inserts is caught too. Nested ROLLUP / CUBE / GROUPING SETS without TOTALS stay checked. A TOTALS-free nested GROUP BY still reaches TLP WHERE, NoREC and Identity WHERE.
h Backward compatible? (maintainer-approved exception only) Yes. Oracle-only (ast_fuzzer_oracle = 1); no setting, format or user-visible change.
i Invariants and contracts preserved? check() still returns false (skip) before any oracle runs; the helper is a read-only AST walk with a null guard, same shape as usesFinalAnywhere; the definition screen stays fail-closed.

Session id: cron:clickhouse-impl-slot-4:20261022-060200

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @PedroTadim @qoega, could you review this and add the can be tested label so CI runs? The oracles skipped only a top-level WITH TOTALS, but a nested one (subquery, CTE, JOIN, view) also puts a totals block into the compared output, which a pushed-down filter recomputes and UNION ALL takes from one branch only, so check() and the view-definition screen now skip such queries.

@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6b14e94]

Summary: ✅


AI Review

Summary

PR #127335 prevents false AST fuzzer oracle mismatches by screening for WITH TOTALS throughout nested query ASTs and stored view definitions before running comparisons. No actionable findings at commit 6b14e940648aa318d2c96fd06b8b92f731938ef5. The reported before/after fuzzing provides focused validation for this CI-only change; the decision to avoid additional expensive oracle tests is consistent with #125003.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Oct 22, 2026
@PedroTadim PedroTadim self-assigned this Oct 22, 2026
@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Oct 22, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Oct 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6b14e9406 with master 31d10e5ef (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 10106881f; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 12 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim
PedroTadim added this pull request to the merge queue Oct 22, 2026
Merged via the queue into ClickHouse:master with commit 238ca00 Oct 22, 2026
526 of 528 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2026
@clickhouse-gh clickhouse-gh Bot added comp-fuzzing Fuzzers: BuzzHouse (the query generator in the client), the AST fuzzer (QueryFuzzer), and the lib... and removed comp-testing Test content only (no production code). labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-fuzzing Fuzzers: BuzzHouse (the query generator in the client), the AST fuzzer (QueryFuzzer), and the lib... pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants