Skip to content

Fix flaky 03915_spilling_hash_join by pinning query_plan_join_swap_table - #111500

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-03915-spilling-hash-join-swap-table
Jul 26, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-03915-spilling-hash-join-swap-table

Conversation

@groeneai

@groeneai groeneai commented Jul 22, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #111192

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

Fixes flaky 03915_spilling_hash_join ("result differs with reference", 0 master / 19 hits across 7 unrelated PRs in 30d; e.g. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111192&sha=740ce3bcdecf657aabf88e39ca1a647709b1ac3f&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29).

Root cause: the tail profile events block asserts per-query JoinNonJoinedTransform* counters. For LEFT/RIGHT/FULL joins the physical side that emits non-joined rows depends on whether the planner swaps the join's build/probe sides. With query_plan_join_swap_table = auto (default) the swap fires when the left row estimate is smaller than the right (optimizeJoin.cpp). Those estimates come from the join-reordering statistics cache (use_hash_table_stats_for_join_reordering, randomized and on by default), so under randomized settings the cached estimates flip the swap decision, reverse the join kind, and move the counters between queries (q12/q13/q15), producing the reference diff. Query results (COUNT/SUM) stay correct; only the ProfileEvents shift. The test already pinned query_plan_optimize_join_order_randomize = 0 but not the swap decision itself.

Fix: pin query_plan_join_swap_table = 0 so the physical plan is deterministic regardless of the cached estimates. The counter assertions and spill checks are unchanged.

Version info

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

The profile-events assertions depend on which physical side of a join emits
non-joined rows. With query_plan_join_swap_table=auto the planner swaps
build/probe sides based on row-count estimates, which randomized settings can
tip, moving the JoinNonJoinedTransform counters between queries and causing
result diffs. Pin the setting so the physical plan is deterministic.

Related: ClickHouse#111192

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

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. With an adversarial session default query_plan_join_swap_table=1 (emulating the cached-estimate swap), q13 returns 0 0 and q15 0 0 every run; the reference expects 1 50 / 1 5000. On demand, 100% reproducible.
b Root cause explained? Yes. The JoinNonJoinedTransform* counters depend on which physical side emits non-joined rows. With query_plan_join_swap_table=auto the planner swaps build/probe when the left row estimate < right; those estimates come from the join-reordering statistics cache (use_hash_table_stats_for_join_reordering, randomized, default on), so randomized runs flip the swap, reverse the join kind, and move the counters between queries.
c Fix matches root cause? Yes. Pins query_plan_join_swap_table = 0 so the physical plan is deterministic regardless of the cached estimates. Not a bounds-widen / blanket no-random / data-shrink band-aid.
d Test intent preserved / new tests added? Yes. swap=0 keeps RIGHT joins physically RIGHT, so right-side non-joined rows are still counted (exactly the current reference). Spill assertions (q05-10, q17-22) unchanged. No assertion weakened, no .reference edit, no test removed.
e Both directions demonstrated? Yes. Without the pin: q13/q15 = 0 0 (CI failure signature). With the pin: q13=1 50, q15=1 5000 (reference). Full test 50/50 with randomization on a fresh master binary.
f Fix is general across code paths? N/A (flaky-test setting fix, not a code bug). The pin covers all swap-sensitive queries in the file.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (flaky-test setting fix, not a code bug).
h Backward compatible? (maintainer-approved exception only) N/A (test-only; no product behavior, setting default, or format changed).
i Invariants and contracts preserved? N/A (test-only; no source touched).

Session id: cron:clickhouse-author-slot-1:20260722-222600

@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; 5 findings across 1 full pass + 1 delta recheck; bounded).

# Sev Finding Verdict Evidence / action
1 ⚠️ Fix lacks a discriminating post-change replay under the triggering state DISAGREE The discriminating replay was already done: adversarial session default query_plan_join_swap_table=1 gives q13/q15=0 0 unpinned vs 1 50/1 5000 pinned; re-validated on fresh master binary + 50/50 randomized runs. "auto replay still matched" is the flake's rarity, not a gap.
2 💡 Comment says auto swaps on "tie"; the comparison is strict < AGREE — fixed optimizeJoin.cpp uses strict <; comment reworded to "based on row-count estimates" and trimmed.
3 💡 Related: #111192 should use the full GitHub URL AGREE — fixed PR body + commit body now use the full https://github.com/ClickHouse/ClickHouse/pull/111192 URL.
4 💡 Commit message omits the S3 CI-report URL DISAGREE Pre-push amend is legitimate (commit never pushed); the S3 report URL lives in the PR description per the CI-provenance convention, which carries it. Commit body carries the full PR URL.
5 💡 Root cause is cached statistics, not a live "near-tie" AGREE — fixed use_hash_table_stats_for_join_reordering is randomized + default on; the CI log shows cached estimates 100 vs 5000 driving the swap. PR body updated to attribute the swap to cached join-reordering statistics. Fix unchanged (correct regardless of cache contents).

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-author-slot-1:20260722-222600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nickitat - could you review this? It pins query_plan_join_swap_table = 0 in 03915_spilling_hash_join so the join build/probe orientation (and the JoinNonJoinedTransform* counters the test asserts) is deterministic; under randomized settings the cached join-reordering estimates were flipping the swap and moving the counters between queries. Test-only, one line.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3901b7f]

Summary: ✅


AI Review

Summary

This PR pins query_plan_join_swap_table = 0 in 03915_spilling_hash_join.sql so the RIGHT/FULL join queries in the profile-events section keep a deterministic physical orientation and stop intermittently moving the JoinNonJoinedTransform* counters between queries. I checked the touched test, the current reference output, the planner swap logic in optimizeJoinLegacy, the existing PR discussion, and the current CI report; I did not find a correctness, coverage, or metadata issue in the current patch.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 24, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 3901b7f

CI fully finished (Config + Finish Workflow SUCCESS, Mergeable Check SUCCESS, >20min buffer). No failing tests on this head. Every failed check has an owner; only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
(no failed tests on this head) - -
Sync - CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260724-063000

@alexey-milovidov alexey-milovidov self-assigned this Jul 26, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 26, 2026
Merged via the queue into ClickHouse:master with commit aef8433 Jul 26, 2026
179 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 26, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-hash-join-swap-table

Fix flaky 03915_spilling_hash_join by pinning query_plan_join_swap_table
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 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