Skip to content

Run AST fuzzer oracle sub-queries on a thread group of their own context - #124547

Merged
PedroTadim merged 6 commits into
ClickHouse:masterfrom
groeneai:oracle-subquery-own-thread-group
Oct 15, 2026
Merged

PedroTadim merged 6 commits into
ClickHouse:masterfrom
groeneai:oracle-subquery-own-thread-group

Conversation

@groeneai

@groeneai groeneai commented Oct 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #114043 (comment)

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):

...

Description

Requested by @ alexey-milovidov on #114043: AST fuzzer (amd_release, oracle) reported TLP Aggregate oracle mismatch for SELECT count() FROM tab__fuzz_0 WHERE val = 'hit' WINDOW w AS () with make_distributed_plan = 1 (report). No related open issue found. The master TLP Aggregate rows of 2026-10-06 ran without the distributed plan and are a different defect (#113332).

Root cause. executeASTFuzzerQueries pins max_rows_to_read = 100000, read_overflow_mode = 'break' on fuzz_context and keeps the thread attached to its thread group while the oracle runs. Each oracle sub-query has its own context from makeOracleContext (caps off, throw, max_threads = 1), but executeQuery does not switch the thread group for an internal query. QueryPlan::convertToDistributed and ReadFromDistributedPlanSource take the context from the thread, so every stage task of an oracle sub-query read under the fuzzed query's cap: in the CI log each reader task stopped after 130818-196227 of 900000 rows, while the reference was the exact trivial count.

Change. Each sub-query in executeAndCollectSortedRows, executeScalar and executeWithSettings runs under a nested ThreadGroupSwitcher on ThreadGroup::createForQuery(oracle_context), the mechanism the fuzzed query already uses. This also makes the oracle's max_threads = 1 and DQP's per-variant settings reach the stage tasks. The comment claiming the oracle runs on fuzz_context is removed.

Validation. 05339_ast_fuzzer_oracle_distributed_plan_read_cap fails 10 of 10 randomized runs without the change and passes 50 of 50 with it; the existing oracle tests pass. If #117187 lands first, its executors need the same switch.


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


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

Version info

  • Merged into: 26.10.1.2514-master (included in 26.10 and later)

groeneai and others added 3 commits October 8, 2026 00:51
`executeASTFuzzerQueries` attaches the thread to a thread group of
`fuzz_context`, which pins `max_rows_to_read = 100000` and
`read_overflow_mode = 'break'`, and keeps it attached while
`QueryOracleChecker::check` runs. Each oracle sub-query runs on a separate
context from `makeOracleContext` (read caps off, overflow modes `throw`,
`max_threads = 1`), but `executeQuery` does not switch the thread group for
an internal query, so the thread's query context stays `fuzz_context`.

With `make_distributed_plan = 1`, `QueryPlan::convertToDistributed` and
`ReadFromDistributedPlanSource` take the context from the thread, and every
stage task copies it. The stage tasks of an oracle sub-query then read under
the fuzzed query's 100000-row 'break' cap: on the CI failure each of the 3
reader tasks stopped after 130818-196227 of 900000 rows, while the TLP
reference was answered exactly by the trivial count, so the job reported a
false `TLP Aggregate oracle mismatch`. The same leak hid the oracle's
`max_threads = 1` pin and DQP's per-variant settings from the stage tasks.

Attach each sub-query in `executeAndCollectSortedRows`, `executeScalar` and
`executeWithSettings` to `ThreadGroup::createForQuery(oracle_context)` with a
nested `ThreadGroupSwitcher` (`allow_existing_group = true`), which restores
the fuzz group when the sub-query finishes, including on exceptions. Drop
the comment in `executeASTFuzzerQueries` that claimed the oracle's nested
queries run on `fuzz_context`.

Related: ClickHouse#114043

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… DQP

The TLP Aggregate guard now also requires `DistributedPlanLocalExecution`
on the metamorphic `countMerge` sub-query, so a plan that silently falls
back to local execution no longer satisfies it (with
`make_distributed_plan = 0` the guard prints 0).

Each round also runs a seed without aggregates, so NoREC runs and its
sub-queries go through `executeScalar`; a second guard asserts that NoREC
ran. Removing the thread group switch from `executeScalar` now reds the
test 10 of 10 (only NoREC mismatches). Rounds go from 8 to 16 so that the
probability-gated DQP oracle also pins the `executeWithSettings` switch:
removing it reds the test 9 of 10 (only DQP mismatches).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each fuzzed seed runs the whole oracle suite, and every oracle sub-query is
bounded only by the oracle's `max_execution_time = 10`, so one heavy fuzzer
mutation can take close to a minute on a debug build. The flaky check runs
the test in parallel copies of itself and fails a non-`long` run over 180 s,
so the test is tagged `long`, like 05099.

DQP only detects a missing thread-group switch in `executeWithSettings`
when a query reads past the fuzzed query's 100000-row cap. The second seed
reads only the primary-key-pruned tail and never could, so the mutant that
drops that switch went undetected in 3 of 20 runs. A full-table `max(id)`
seed gives DQP a read past the cap in every round (`max` is not in the
oracle's excluded aggregate lists, unlike `sum` and `avg`). With it the
mutant is red in 29 of 30 runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 8, 2026
@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (rounds 1-3 of review)
  • ⚠️ Test liveness (AGREE, fixed): the guard proved TLP Aggregate ran but not that it reached the distributed plan, and the test did not cover the NoREC (executeScalar) or DQP (executeWithSettings) sub-query paths. The guard now also requires DistributedPlanLocalExecution; disabling distributed_plan_fallback_to_local_execution was not used because the trivial-count reference cannot be distributed and would throw. A non-aggregate seed covers NoREC and a full-table max(id) seed gives the probability-gated DQP oracle a query that reads past the cap.
  • ⚠️ DQP not forced deterministically (DISAGREE): DQP runs on a random quarter of queries (thread_local_rng() % 4), and SQL cannot force it without a test-only setting. Removing only the executeWithSettings switch fails 62 of 63 runs. A "DQP ran" guard would fail the fixed build at the same rate.
  • ⚠️ Test runtime (AGREE, fixed): one heavy fuzzer mutation runs the whole oracle suite at up to 10 s per sub-query, so the test is tagged long for the flaky check's 180 s limit.
  • 💡 Commit-message wording, the guard vs the oracle's 10 s cap under load, [[nodiscard]] on the helper, fewer flaky-check repetitions for a long test: noted, not blocking.

@groeneai

groeneai commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 900000-row MergeTree table, make_distributed_plan = 1, distributed_plan_execute_locally = 1, distributed_plan_default_reader_bucket_count = 3, ast_fuzzer_runs = 1, ast_fuzzer_oracle = 1, 40 rounds of SELECT count() FROM tab WHERE val = 'hit': 27/40 (master snapshot) and 32/40 (unpatched build) TLP Aggregate oracle mismatch; 0/40 with make_distributed_plan = 0.
b Root cause explained? Oracle sub-queries ran on their own makeOracleContext context, but the thread stayed attached to the fuzzed query's thread group. The distributed plan takes the context from the thread, so each stage task read under the fuzzed query's max_rows_to_read = 100000, read_overflow_mode = 'break': 3 x 130818 of 900000 rows, while the reference was the exact trivial count.
c Fix matches root cause? Yes. Each oracle sub-query now runs on ThreadGroup::createForQuery(oracle_context), so the thread context is the oracle's own. No setting pinned, no oracle disabled, the distributed plan stays covered.
d Test intent preserved / new tests added? New 05339_ast_fuzzer_oracle_distributed_plan_read_cap.sh; via system.query_log it also asserts that the TLP Aggregate sub-query reached the distributed plan (DistributedPlanLocalExecution, 0 with make_distributed_plan = 0) and that NoREC ran.
e Both directions demonstrated? Test fails 10/10 randomized runs on the unpatched build (oracle mismatches: 16..23), passes 50/50 on the fix. Stage reads with the fix: 301984 + 303104 + 294912 = 900000.
f Fix is general across code paths? All three nested executors are switched (executeAndCollectSortedRows, executeScalar, executeWithSettings), the only executeQuery call sites in the checker. The test pins each: dropping the switch from executeScalar reds it 10/10 (NoREC), from executeWithSettings 29/30 (DQP).
g Fix generalizes across inputs (params/datatypes/wrappers)? Harness change, no data-type dimension. Validated under the full random settings and MergeTree settings matrix.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change; ast_fuzzer_* are testing settings.
i Invariants and contracts preserved? ThreadGroupSwitcher (allow_existing_group = true) restores the fuzz group and thread name on every exit, including the TOO_MANY_ROWS/TOO_MANY_BYTES path; in executeScalar it outlives the BlockIO and the pulling executor.

Session id: cron:clickhouse-impl-slot-40:20261008-030400

@clickhouse-gh

clickhouse-gh Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ef0e915]

Summary: ✅


AI Review

Summary

The change runs each AST fuzzer oracle sub-query in a thread group bound to its own context, so distributed stages receive the oracle's read limits, thread limits, and DQP settings. The regression test exercises the three execution helpers. No actionable findings in the current diff.

Missing context / blind spots
  • ⚠️ No local build or executable was available for independent reproduction. The current head's functional and flaky-check jobs are still pending in the CI report; completed results would close this validation gap. The PR documents repeated regression runs and tests with individual switches removed.
Final Verdict

Status: ✅ Approve. No code changes requested; current-head CI validation remains pending.

PedroTadim and others added 2 commits October 15, 2026 12:01
On ASan with S3 storage in the flaky check, every TLP `countMerge` sub-query of one run exceeded the
oracle's own 10 s `max_execution_time` (`TIMEOUT_EXCEEDED`), so no `QueryFinish` row matched and the
test printed `TLP Aggregate checked 0`. Such a sub-query still ran under the distributed plan, so it
counts too. The row count stays at 900000: with fewer rows a reader bucket would fit under the
fuzzed query's cap, and the test would stop catching the bug.

CI report: https://d1k2gkhrlfqv31.cloudfront.net/clickhouse-test-reports-private/praktika.html?PR=80005&sha=7ce92c97e2f259e87ff1ce0bf1b1999f9fa58f5d&name_0=PR
PR: ClickHouse/clickhouse-private#80005
Related: ClickHouse#124547

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PedroTadim
PedroTadim enabled auto-merge October 15, 2026 12:06
@PedroTadim
PedroTadim added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit 6d6e3e2 Oct 15, 2026
178 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 15, 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-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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