Skip to content

Pin join-plan randomization in 04341_autopr_mode1_apply_join - #113904

Merged
nickitat merged 3 commits into
masterfrom
fix-04341-autopr-join-randomization
Aug 10, 2026
Merged

nickitat merged 3 commits into
masterfrom
fix-04341-autopr-join-randomization

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 8, 2026 •

Copy link
Copy Markdown
Member

Related: #76867

The test asserts that automatic parallel replicas (mode 1) collects statistics on the first run and applies parallel replicas on the second for INNER/LEFT/RIGHT joins. Under randomized settings, the combination of query_plan_optimize_join_order_randomize and a randomized join_runtime_filter_min_probe_rows changes the join plan, and AutoPR declines to apply for the RIGHT join (04341_join_right_apply 0 instead of 1). Either setting alone does not reproduce the failure. The test already pins query_plan_join_swap_table for the same reason; this also pins query_plan_optimize_join_order_randomize = 0, enable_join_runtime_filters = 1 and join_runtime_filter_min_probe_rows = 1000 (the default). Runtime filters stay enabled - the probe side of the RIGHT join (62500 rows) exceeds the pinned threshold, so the plan keeps BuildRuntimeFilter and this remains the only mode-1 APPLY coverage for the runtime-filtered RIGHT JOIN shape (per review).

Reproduced and verified locally against a real server with a parallel_replicas cluster: the culprit pair fails before the pin and matches the reference after, both with and without the culprit settings.

Seen in https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=76867&sha=0725a71d30d393d74b17f6d2a6208c14c69a5546&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29 on #76867, and CIDB shows the same failure on several other PRs (#113649, #69540, #71028) since 2026-08-06.

Changelog category (leave one):

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

Version info

  • Merged into: 26.8.1.1087 (included in 26.8 and later)

The test asserts that automatic parallel replicas (mode 1) collects
statistics on the first run and applies parallel replicas on the second
for INNER/LEFT/RIGHT joins. Under randomized settings the combination of
`query_plan_optimize_join_order_randomize` and a randomized
`join_runtime_filter_min_probe_rows` changes the join plan, and AutoPR
declines to apply for the RIGHT join (`04341_join_right_apply 0`
instead of `1`). Either setting alone does not reproduce it.

The test already pins `query_plan_join_swap_table` for the same reason;
also pin `query_plan_optimize_join_order_randomize = 0` and
`enable_join_runtime_filters = 0`.

Reproduced and verified locally against a real server with a
`parallel_replicas` cluster: the culprit pair fails before the pin and
matches the reference after, both with and without the culprit settings.

Seen in https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=76867&sha=0725a71d30d393d74b17f6d2a6208c14c69a5546&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
(PR #76867), also failing
on other PRs since 2026-08-06.
@clickhouse-gh

clickhouse-gh Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1e0fff0]

Summary: ✅


AI Review

Summary

This PR makes 04341_autopr_mode1_apply_join deterministic by pinning query_plan_optimize_join_order_randomize = 0 and join_runtime_filter_min_probe_rows = 1000 while keeping enable_join_runtime_filters = 1. That preserves the runtime-filtered RIGHT JOIN AutoPR apply shape the test is meant to cover, addresses the earlier concern about weakening coverage, and the current merge commit is green in CI.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 8, 2026
…hold instead

`04341_autopr_mode1_apply_join` is the only `automatic_parallel_replicas_mode=1`
APPLY test covering `RIGHT JOIN` through the runtime-filtered plan, so disabling
`enable_join_runtime_filters` removed that coverage. Keep it enabled and pin
`join_runtime_filter_min_probe_rows` to its default (1000): the probe side of the
RIGHT join (62500 rows) exceeds it, so `BuildRuntimeFilter` stays in the plan
(verified via `EXPLAIN`), while both randomized settings are now deterministic.
Verified against a real server with a 3-replica `parallel_replicas` cluster:
reference matches both plain and under the culprit randomization pair
(`query_plan_optimize_join_order_randomize=790753`,
`join_runtime_filter_min_probe_rows=8600`).

Co-Authored-By: Claude Fable 5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Aug 8, 2026
Address the AI review: with the companion
#113904 still open, this branch
alone left one AutoPR join test exposed to the same randomized-plan divergence.
Apply the identical pin from that PR (byte-for-byte, so both PRs merge cleanly
in either order): pin query_plan_optimize_join_order_randomize = 0 and keep
runtime filters enabled with join_runtime_filter_min_probe_rows pinned, since
this test is the only mode=1 APPLY coverage for the RIGHT JOIN plan shape with
BuildRuntimeFilterStep present.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The only red, Stress test (amd_tsan), is unrelated to this PR (the diff only pins settings in one stateless test). It is the known master regression from #113225: a logical error (exception) Bad cast from type DB::ColumnString to DB::ColumnNullable in SerializationNullable::enumerateStreams when reading a Nested member subcolumn from an unrewritten part, seen on ~30 PRs and master since 2026-08-07. The fix is already in progress: #113925. Once it merges, updating this branch will clear the red.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The AI Review verdict on 3212ff5ef874 flipped to ✅ Approve after keeping enable_join_runtime_filters = 1 with the threshold pinned. The sole remaining red, Stress test (amd_tsan) — logical error Bad cast from type DB::ColumnString to DB::ColumnNullable in SerializationNullable::enumerateStreams — is the known unrelated master regression from #113225 (dotted Nested member subcolumn from an unrewritten part), with the fix in progress in #113925. Merged the latest master (93 commits behind, zero conflicts) and pushed 1e0fff04beee to get a fresh CI run; the stress red may recur until #113925 lands, after which another remerge will clear it.

@clickhouse-gh

clickhouse-gh Bot commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1e0fff04b with master 9b6a2d734 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e2194b4a8; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 690.65 MiB 687.62 MiB -3.02 MiB (-0.44%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 CI on 1e0fff04beee (after the master merge): both remaining reds are unrelated to this test-only PR (the diff only pins settings in 04341_autopr_mode1_apply_join), and the AI Review verdict remains ✅ Approve.

@groeneai

Copy link
Copy Markdown
Collaborator

Investigated. This is not the PreparedSets GLOBAL IN gap I have been tracking under #107619, and no in-progress PR covers it, so I will send a separate one.

The exception is a side effect of an unrelated failure being swallowed during index analysis. From your arm_asan_ubsan run, query 66b8a461-0619-4a87-8876-c5e9a7f26247 on the AST-fuzzed 7th query of 01231_operator_null_in_2:

SELECT count() = 33333 FROM null_in_subquery
WHERE i GLOBAL IN (SELECT i FROM cluster('test_cluster_1_shard_3_replicas_1_unavailable',
                                         currentDatabase(), 'null_in_subquery') FINAL WHERE dt = 2)

The fuzzer added FINAL to the subquery. null_in_subquery is a plain MergeTree, so MergeTreeData::supportsFinal is false and the remote replica rejects the subquery with Code: 181 ILLEGAL_FINAL (QueryAnalyzer.cpp:858). The relevant sequence in the server log is:

15:46:17.472  <Debug> executeQuery: (initial_query_id: 66b8a461...) SELECT ... FROM null_in_subquery FINAL WHERE dt = 2
15:46:17.495  <Error> executeQuery: Code: 181 ... Storage MergeTree doesn't support FINAL (ILLEGAL_FINAL)
15:46:17.534  <Debug> (SelectExecutor): Failed to apply statistics pruning for part 0_1_16_1,
              skipping statistics pruning for this part: Code: 181 ... Received from 127.0.0.2:9000 ...
15:46:17.672  <Fatal> Logical error: 'Not-ready Set is passed as the second argument for function 'globalNullIn''

i is not in the sorting key, so the primary key declines it (Key condition: unknown) and nothing else builds the set. The only component that does reach it is statistics part pruning, which runs before primary-key analysis (ReadFromMergeTree.cpp:2854) and builds its own KeyCondition over the filter predicate inside checkPartCanMatch (StatisticsPartPruner.cpp:147); that constructor runs extractAtomFromTree, which for an IN-family function calls buildOrderedSetInplace (KeyCondition.cpp:2617). Because a GLOBAL IN set carries an external table, buildOrderedSetInplace takes the destructive branch that PreparedSets.cpp:499 selects: FutureSetFromSubquery::build does auto plan = std::move(source) (PreparedSets.cpp:379), so the subquery plan is consumed. The remote ILLEGAL_FINAL then propagates out of that build, and it lands in the per-part best-effort handler at MergeTreeDataSelectExecutor.cpp:855, which logs at debug level and continues. The set is left permanently unbuilt with its source gone, and FunctionIn::executeImpl aborts at in.cpp:116 when the main pipeline runs.

So the user-visible error is wrong in two ways: the query should have failed with ILLEGAL_FINAL, and instead the real error is discarded and replaced by a LOGICAL_ERROR that aborts sanitizer builds. That handler predates the statistics-pruning set-build path (added in f784b38ae0ea8cc, 2026-03-23; the wording at :858 is from 6509e7787b00de8), and it is the only such handler on this path. I confirmed the code is identical at the failing build 7c4fcedfd2fb85b.

That also explains the intermittency: use_statistics_for_part_pruning defaults to true, but the pruner is only non-useless when a filter column actually declares statistics, and CI randomizes auto_statistics_types ("" with probability 0.3) plus use_statistics and materialize_statistics_on_insert. The signature needs the fuzzer to produce a subquery that fails remotely and a run where the pruner engages on the IN column, which matches the roughly one hit per day for the globalNullIn spelling.

Existing work does not cover this. #102192 does not touch PreparedSets.cpp, external_table or GLOBAL at all (0 occurrences in its diff); #112968 fixes the object-storage _path iterator; #114058 fixes the Merge row-policy path; and #102308 replaces the PreparedSets.cpp:499 exclusion, which is a different mechanism from a swallowed exception. Widening the non-destructive clone would not help either, since the failure here is a genuine error that should surface, not a silent stop.

My intended direction is to stop discarding an exception that is not about corrupt statistics: keep the best-effort behaviour for statistics deserialization and estimate problems, and rethrow anything else, so this query reports ILLEGAL_FINAL as it should. I will check ConditionSelectivityEstimator.cpp:379, the one sibling best-effort handler on the estimation path, for the same problem. If you would rather statistics pruning not trigger a set build at all, say so and I will take that route instead.

I will post the PR link here.

@groeneai

Copy link
Copy Markdown
Collaborator

#114121 is the separate PR.

It stops statistics part pruning from executing an IN subquery during index analysis, which is
what consumed the subquery plan and turned the swallowed Code: 181 ILLEGAL_FINAL into the
Not-ready Set abort. Pruning is preserved for literal IN lists, StorageSet sets and
already-built subquery sets; only IN (subquery) on a non-key column with statistics loses it.

That is the statistics-pruning carrier only, not all of #107619.

@nickitat nickitat self-assigned this Aug 10, 2026
@nickitat
nickitat enabled auto-merge August 10, 2026 10:13
@nickitat
nickitat added this pull request to the merge queue Aug 10, 2026
Merged via the queue into master with commit 9baf584 Aug 10, 2026
354 of 356 checks passed
@nickitat
nickitat deleted the fix-04341-autopr-join-randomization branch August 10, 2026 11:39
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 10, 2026
pull Bot pushed a commit to AnotherGenZ/ClickHouse that referenced this pull request Aug 12, 2026
Under randomized settings, `query_plan_optimize_join_order_randomize` produces
different cardinality estimates in the single-replica and the candidate
parallel-replicas plan builds (and can flip the
`join_runtime_filter_min_probe_rows` threshold in only one of them), so the plan
shapes diverge, `findCorrespondingNodeInSingleNodePlan` finds no matching hash,
and AutoPR skips the instrumentation or declines to apply (fail-closed) - the
tests then report 0 instead of the expected value. Pin
`query_plan_optimize_join_order_randomize = 0` and
`enable_join_runtime_filters = 0`, as `04341_autopr_mode1_apply_join` does in
ClickHouse#113904.

Seen on ClickHouse#113412:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113412&sha=d882b451a59bc6e5b1baf8b21798757e9b1c122b&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%203%2F3%29

Co-Authored-By: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants