Skip to content

AST fuzzer oracle: skip a nested LIMIT in a distributed plan - #130656

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:oracle-skip-nested-limit-distributed-plan
Nov 12, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:oracle-skip-nested-limit-distributed-plan

Conversation

@groeneai

@groeneai groeneai commented Nov 10, 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 a query with LIMIT in a subquery when make_distributed_plan is enabled.

Description

AST fuzzer (amd_release, oracle) failed on master with a TLP Aggregate mismatch
(report)
on SELECT count() IGNORE NULLS FROM (SELECT day + 365 AS day, * ... FROM (SELECT * FROM testoffset LIMIT 328)) WHERE ....
A seed test's SET make_distributed_plan = 1 had stayed on in the fuzzer session.

Under the distributed plan the LIMIT of the subquery takes rows from one stream per reader bucket
(GatherReceiveStep keeps one stream per source task, even with max_threads = 1), and which rows fill the 328
changes with the query built around the subquery (deterministic per query, measured locally). The TLP partitions are
three reads of that subquery under three different filters, so each keeps a different subset: 30 + 222 + 0 = 252 rows
against 328 in the reference. Every subset is a legal result of LIMIT without ORDER BY, so the engine is fine; the
oracle's assumption that a re-read keeps the same rows does not hold here. Without the distributed plan the oracle
reads one stream and the LIMIT keeps the same rows each time.

The change skips the oracle for a query with LIMIT, OFFSET, LIMIT BY or LIMIT AFTER/UNTIL in any subquery
when make_distributed_plan is on, and applies the same check to the view bodies, column expressions and row policies
the query reads. A top-level row limit was already rejected. Nothing changes without the distributed plan, so
nested-LIMIT queries that found real bugs there stay covered.

No test, as for #127078. Validation with clickhouse local, 60 fuzzer rounds each of the seed and of the CI query:
Code: 906 mismatches with make_distributed_plan = 1: master 25/120, this PR 0/120; with it off: 0/120 on both,
with a similar number of TLP Aggregate checks (33 and 36).


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

Version info

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

groeneai and others added 2 commits November 10, 2026 23:28
`AST fuzzer (amd_release, oracle)` reported a false TLP Aggregate mismatch on
master for a query reading `(SELECT * FROM testoffset LIMIT 328)` after a seed
test left `make_distributed_plan = 1` on in the fuzzer session.

Under the distributed plan the subquery's LIMIT takes rows from one stream per
reader bucket (`GatherReceiveStep` keeps one stream per source task even with
`max_threads = 1`), and which rows fill the limit changes with the query built
around it. The TLP partitions are three reads of the subquery under three
filters, so each kept a different subset (30 + 222 + 0 = 252 vs 328). Every
subset is a legal result of LIMIT without ORDER BY. Without the distributed plan
the oracle reads one stream and the LIMIT keeps the same rows on each read.

Skip the oracle when the query has LIMIT, OFFSET, LIMIT BY or LIMIT AFTER/UNTIL
in any subquery and `make_distributed_plan` is on, and apply the same check in
the definitions screen (view bodies, column expressions, row policies). A
top-level row limit was already rejected. An unconditional nested-LIMIT gate was
not used: nested-LIMIT queries without the distributed plan found real
wrong-result bugs in the last 30 days.

Validation (`clickhouse local`, 60 fuzzer rounds of the seed and 60 of the CI
query): with `make_distributed_plan = 1`, 25 mismatches on master and 0 with
this change; with it off, 0 on both and the same order of TLP Aggregate checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the clause list from the hasRowLimitAnywhere comment; the condition below names the clauses.

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) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 💡 The new gate also skips a nested row limit that is deterministic under the distributed plan (ORDER BY on a unique key) DISAGREE Same fail-closed rule as the existing top-level gate, which rejects LIMIT even with ORDER BY (non-unique sort keys). Queries without a row limit stay checked under the distributed plan.
2 💡 A remote() table function in a subquery is another multi-stream source for a nested LIMIT DISAGREE Different mechanism, independent of make_distributed_plan, never seen as an oracle mismatch. Not changed here.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-11:20261110-233001

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @qoega, could you review this? With make_distributed_plan on, a LIMIT without ORDER BY in a subquery keeps rows that depend on the query built around it, so the three TLP partitions read different subsets and report a false mismatch; the oracle now skips a nested LIMIT/OFFSET/LIMIT BY in that mode, including in view bodies and row policies.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The oracle's TLP Aggregate shape over (SELECT * FROM testoffset LIMIT 328) with make_distributed_plan = 1 gives 252 instead of 328 in 200/200 runs (328 with it off); 120 fuzzer rounds of the seed and the CI query give 25 Code: 906 on master.
b Root cause explained? A seed's SET make_distributed_plan = 1 stays on in the fuzzer session. The subquery's LIMIT then takes rows from one stream per reader bucket, and which rows fill the limit changes with the filter above it, so the three TLP partitions keep three different subsets (30 + 222 + 0 = 252). Each is a legal LIMIT result without ORDER BY.
c Fix matches root cause? Yes. The defect is the oracle's eligibility gate comparing reads whose rows the engine may choose per read; the gate now rejects a row-limit clause in any subquery when the plan is distributed. No engine change, no setting pinned.
d Test intent preserved / new tests added? No new test (oracle-eligibility fix, as #127078 / #126703). The existing oracle tests 04658_ast_fuzzer_oracle_tlp_aggregate_counter and 05339_ast_fuzzer_oracle_distributed_plan_read_cap pass.
e Both directions demonstrated? Yes. Fuzzer rounds with the distributed plan: 25 -> 0 mismatches; 5 seed rounds: 2 -> 0; the same subquery behind a view: 3 -> 0.
f Fix is general across code paths? Yes. Every oracle goes through check(); the walk reaches FROM subqueries, JOIN sides, IN, EXISTS, scalar subqueries, CTEs, UNION branches and APPLY lambdas; view bodies, column expressions and row policies go through the existing definitions screen.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. LIMIT, OFFSET, LIMIT BY, LIMIT AFTER/UNTIL, with or without ORDER BY / WITH TIES. Without the distributed plan the checks run as before (36 vs 33 TLP Aggregate checks, 0 mismatches); with it, a query without a row limit is still checked (33 checks, 0 mismatches).
h Backward compatible? (maintainer-approved exception only) Yes. Only the AST fuzzer oracle's eligibility changes; no setting, default or format.
i Invariants and contracts preserved? Yes. Every SELECT the oracle compares across reads must keep the same rows on each read; the new check is a side-effect-free AST walk that runs before any oracle sub-query and can only skip.

Session id: cron:clickhouse-impl-slot-40:20261110-231200

@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2fc28db]

Summary: ✅


AI Review

Summary

The PR prevents false AST fuzzer oracle mismatches by skipping queries with row limits, including limits in stored definitions, when make_distributed_plan is enabled. No actionable findings at head 2fc28dbaefde7b323a6c77282fa27d4b760d6fa3. Validation relies on the documented before/after fuzzer runs; no local ClickHouse binary was available for an independent runtime check.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-testing Test content only (no production code). labels Nov 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2fc28dbae with master b8c902a6c (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.89, programs/clickhouse-keeper ×1.07 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 8.6 s 16.7 s +9.1 s (+118%)
Compile time of recompiled translation units

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 12, 2026
Merged via the queue into ClickHouse:master with commit fce5e5a Nov 12, 2026
551 of 553 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 12, 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
qoega added a commit that referenced this pull request Nov 24, 2026
…e monolith

The branch's previous master merge used a stale `origin/master`, so the PR still
conflicted with the real current master. Merge current `origin/master` and
re-resolve the two conflicted files (`QueryOracleChecker.cpp`/`.h`), keeping the
branch's extracted `QueryOracles/` architecture and porting the genuinely-new
master changes:

- Ported `hasRowLimitInDistributedPlan` / `hasRowLimitAnywhere` and the
  `make_distributed_plan` setting extern, and wired the gate into `check` and the
  stored-definition screen (master #130656: a nested `LIMIT` under a distributed
  plan feeds one stream per source task, so the oracle's rewritten reads keep
  different rows — a false `TLP Aggregate` mismatch).
- Ported the new `SHUFFLE` screen in the unstable-identifier gate (`SHUFFLE`
  returns rows in random order, a random subset with `LIMIT`).
- Kept the branch's `referencesNonDeterministicEngineAnywhere` (covers
  `Distributed`/`Buffer`, superseding master's `referencesDistributedTableAnywhere`)
  and dropped master's duplicate top-level `WITH TOTALS` skip (the branch already
  has `hasGroupByTotalsModifierAnywhere`).
- Dropped master's `recordReadParts`/`readPartsChanged`/`read_parts` from the
  header (superseded by `OracleExec::isStable`, as in the earlier merge).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
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