Skip to content

Unify the spill trigger for hash joins - #117356

Merged
yariks5s merged 40 commits into
masterfrom
yarik/unify-spilling-hash-join
Sep 10, 2026
Merged

yariks5s merged 40 commits into
masterfrom
yarik/unify-spilling-hash-join

Conversation

@yariks5s

@yariks5s yariks5s commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Closes: #110521

One control for when a hash join spills. How it spills is still your choice of algorithm.

grace_hash spilled on max_rows_in_join / max_bytes_in_join and ignored max_bytes_before_external_join; every other hash algorithm did the opposite. Now, for all of them:

  • max_bytes_before_external_join / max_bytes_ratio_before_external_join — the spill threshold.
  • max_rows_in_join / max_bytes_in_join with join_overflow_mode — a hard cap. It never causes spilling, and a join that spills no longer exceeds it (GraceHashJoin ignored the check_limits argument of addBlockToJoin entirely).

Every algorithm stays selectable. grace_hash is external from the first block, so it needs a non-zero spill threshold: without one it is passed over for the next algorithm in join_algorithm, and rejected with a clear error when it is the only one listed.

Also fixed on the way: FillingRightJoinSideTransform detected spillability with typeid_cast<GraceHashJoin *>, so MemorySpillScheduler never reached the adaptive SpillingHashJoin path. It now goes through the IJoin virtuals canSpillToDisk / getSpillableBytes / requestSpill.

legacy_join_size_limits_trigger_spilling (default false, enabled by compatibility with 26.8 and older) restores the earlier meaning: for the part of a join that runs on disk the two limits trigger further spilling instead of stopping the query, and standalone grace_hash accepts a zero threshold again.

Changelog category (leave one):

  • Backward Incompatible Change

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

max_bytes_before_external_join and max_bytes_ratio_before_external_join are now the spill threshold for every hash-based join_algorithm, including grace_hash, which previously ignored them and spilled on max_rows_in_join / max_bytes_in_join instead. Those two are a hard cap again for every algorithm: they no longer cause spilling, and a join that spills no longer exceeds them. All join algorithms remain available; join_algorithm = 'grace_hash' now needs a non-zero spill threshold, and is skipped in favour of the next algorithm in the list when it has none. Set legacy_join_size_limits_trigger_spilling = 1 (or compatibility to 26.8 or older) to restore the previous behavior.


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

@mintlify

mintlify Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🟢 Ready View Preview Aug 31, 2026, 4:33 PM

…g-hash-join

# Conflicts:
#	src/Core/Settings.cpp
#	src/Interpreters/SpillingHashJoin.cpp
#	tests/queries/0_stateless/01721_join_implicit_cast_long_oldanalyzer.sql.j2
@CLAassistant

CLAassistant commented Aug 31, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ alexey-milovidov
❌ yariks5s


yariks5s seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9793a31]

Summary: ✅


AI Review

Summary

This PR aligns hash-join spilling around max_bytes_before_external_join / max_bytes_ratio_before_external_join, adds the legacy_join_size_limits_trigger_spilling compatibility flag, and wires MemorySpillScheduler through IJoin. The overall direction looks right, but the current HashJoin -> GraceHashJoin handoff still drops the new hard-cap contract on one adaptive path, and the legacy-compatibility coverage is still incomplete, so I would not merge it yet.

Findings

❌ Blockers

  • [src/Interpreters/SpillingHashJoin.cpp:243] The single-thread adaptive spill path still replays every buffered block into GraceHashJoin with check_limits = false, and GraceHashJoin::onBuildPhaseFinish does not re-run checkSizeLimits() for the current bucket. If the switch happens on the last build block, or via MemorySpillScheduler with no later right-side input, bucket 0 can finish the build phase already over max_rows_in_join / max_bytes_in_join, even though the PR now documents those settings as hard caps for every hash-based join.
    Suggested fix: after the replay, or in GraceHashJoin::onBuildPhaseFinish, run the same current-bucket hard-cap check that standalone GraceHashJoin::addBlockToJoin uses, and map join_overflow_mode = 'break' to stop_after_current_bucket instead of skipping the check.

💡 Nits

  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/117356#discussion_r3898152930] [PR body] The prose description still says compatibility with 26.7 and older enables legacy_join_size_limits_trigger_spilling, but the new settings-history entry is keyed on 26.9 and 04698_legacy_join_size_limits_spill_trigger.sql proves the cutoff is 26.8. The changelog entry is already correct; only the earlier body sentence needs the same update.
Tests
  • ⚠️ 04698_legacy_join_size_limits_spill_trigger.sql still exercises only standalone join_algorithm = 'grace_hash'. The compatibility contract now also changes the adaptive hash / parallel_hash path after SpillingHashJoin has already switched into GraceHashJoin, so please add one focused case with a low spill threshold and a lower max_bytes_in_join / max_rows_in_join to prove the on-disk phase spills again instead of honoring join_overflow_mode.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  1. Re-enable the hard-cap check on the adaptive HashJoin -> GraceHashJoin handoff, including the "spill on the last build block" / scheduler-driven path.
  2. Add the missing regression for legacy_join_size_limits_trigger_spilling on the adaptive hash / parallel_hash spill path.

LLVM Coverage Report

Measured on commit 9793a31.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 336/354 (94.92%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-backward-incompatible Pull request with backwards incompatible changes label Aug 31, 2026
Comment thread src/Interpreters/GraceHashJoin.cpp Resolved
Comment thread src/Interpreters/GraceHashJoin.cpp Resolved
Comment thread src/Interpreters/GraceHashJoin.cpp Resolved
…f the threshold, move the JOIN doc text to its source
Comment thread src/Interpreters/GraceHashJoin.cpp Resolved
Comment thread src/Interpreters/SpillingHashJoin.cpp Resolved
Comment thread src/Interpreters/SpillingHashJoin.cpp Resolved
@yariks5s
yariks5s added this pull request to the merge queue Sep 10, 2026
Merged via the queue into master with commit a023513 Sep 10, 2026
307 of 345 checks passed
@yariks5s
yariks5s deleted the yarik/unify-spilling-hash-join branch September 10, 2026 17:23
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 10, 2026
pull Bot pushed a commit to Lobinson1/ClickHouse that referenced this pull request Sep 10, 2026
…ify-spilling-hash-join"

This reverts commit a023513, reversing
changes made to cb4187e.
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 11, 2026
Picks up the revert of ClickHouse#117356 (revert PR ClickHouse#119334, merge c46c19c), which
is what reddened 05141_time_series_select_join_settings on this PR's CI run: the
merge commit CI built was dispatched before that revert landed on master.
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 11, 2026
05141_time_series_select_join_settings failed on this branch's CI because of
master content, not this change: praktika builds the merge commit, and this head
was dispatched after ClickHouse#117356 landed but before its revert ClickHouse#119334. Master now
carries the revert.
alexey-milovidov added a commit that referenced this pull request Sep 13, 2026
Edited by hand following .claude/skills/edit-changelog/SKILL.md, consolidating
the nine raw blocks generated between 2026-09-06 and 2026-09-13. The
NightlyChangelog job rejected its own edits every night in that period: the
verifier did not know that #116566 (reverted by #116586) had itself reverted
#109710, so it demanded that no trace of #116586 remain while the rules put
its link on the CatBoost entry. Fixed in the job by
#119866; this edit passes the
patched verifier, and the trailers below give the ledger the missing chain.

Failed runs: https://github.com/ClickHouse/ClickHouse/actions/runs/34006912773
through https://github.com/ClickHouse/ClickHouse/actions/runs/34668133756

Changelog-revert: 116566 109710 Revert "Remove CatBoost integration"
Changelog-revert: 116586 116566 Revert "Revert "Remove CatBoost integration""
Changelog-revert: 117629 117295 Revert "Skip Darwin fast test in PRs unless labeled ci-macos"
Changelog-revert: 118525 118218 Revert "Do not wait for the remote drain when cancelling a remote query"
Changelog-revert: 119043 117866 Revert "Add `ci-coverage` label to force LLVM coverage jobs on tests-only PRs"
Changelog-revert: 119269 118239 Revert "Require agents to run the style check locally before pushing"
Changelog-revert: 119270 115343 Revert "Resume a NATS JetStream subscription after a reconnect"
Changelog-revert: 119274 118919 Revert "Carry the mutation version onto a rewritten lightweight `DELETE`"
Changelog-revert: 119275 114817 Revert "Convert OUTER join to INNER join by null-rejecting join conditions"
Changelog-revert: 119276 110939 Revert "Support real hard links for plain_rewritable metadata."
Changelog-revert: 119279 117449 Revert "Print file offsets in the bare fatal stack traces and build the release binary as PIE"
Changelog-revert: 119281 116629 Revert "Check access rights in the TimeSeries table functions"
Changelog-revert: 119298 118606 Revert "Proper partition pruning with dates"
Changelog-revert: 119323 117768 Revert "Docs: Skip private sync for documentation-only pull requests"
Changelog-revert: 119334 117356 Revert "Unify the spill trigger for hash joins"
Changelog-revert: 119429 114562 Revert "Fix NO_SUCH_COLUMN_IN_TABLE naming a column absent from the table when a part's columns were all renamed or all dropped"
Changelog-deleted-entry: 110939 [New Feature] [raw] * Disks with the `plain_rewritable` metadata (`s3_plain_rewritable`, and `azure`/`local` object storages with `metadata_type = plain_rewritable`) support hard links if configured with the new `enable_hard_links` setting, which enables `ALTER` operations and mutations on `MergeTree` tables stored on them. Mutations and partition operations share the unchanged blobs between parts instead of copying them. The setting is off by default, because a disk that has hard links cannot be read by older servers. [#110939](#110939) ([yanglongwei](https://github.com/ylw510)).
Changelog-deleted-entry: 114562 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Fixed `NO_SUCH_COLUMN_IN_TABLE` naming a column that is not in the table, when reading or merging a part whose columns had all been renamed or all dropped by a mutation that was not applied yet. The merge kept being retried, so the table stopped compacting. Also fixed the columns of a `MergeTree` part produced by such a merge reading as their default values until the mutation was applied. [#114562](#114562) ([Shaohua Wang](https://github.com/tiandiwonder)).
Changelog-deleted-entry: 114817 [Performance Improvement] [raw] * Convert `OUTER JOIN` to `INNER JOIN` when a later JOIN condition filters out its NULL-extended rows, and filter NULL join keys directly at the table scan when column statistics show it is selective. Only conditions of the form `expr1` OP `expr2` are considered, where OP is one of `=`, `<`, `<=`, `>`, `>=`. Each side can be a column or an expression that propagates NULLs. Controlled by the new settings `query_plan_derive_not_null_filters_from_joins` (enabled by default), `query_plan_allow_derived_not_null_filters_execution` (enabled by default) and `query_plan_max_selectivity_for_not_null_filters_execution` (0.7 by default). [#114817](#114817) ([Hechem Selmi](https://github.com/m-selmi)).
Changelog-deleted-entry: 115343 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Fixed a `NATS` engine table reading from a JetStream stream (`nats_stream`) silently stopping to consume messages after the NATS server restarted or the connection was re-established. [#115343](#115343) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
Changelog-deleted-entry: 116629 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * The table functions reading a `TimeSeries` table (`timeSeriesSamples`, `timeSeriesTags`, `timeSeriesMetrics`, `timeSeriesSelector`, `prometheusQuery`, `prometheusQueryRange`) checked no privilege on it: a user holding only `CREATE TEMPORARY TABLE` could read its target tables' column definitions and rows, and write into them. Each now requires the corresponding privilege on the `TimeSeries` table and on the target table, following an `Alias` target through to the table it points at. `timeSeriesSamples`, `timeSeriesTags` and `timeSeriesMetrics` check the target at table level, since they return it whole and cannot restrict the columns, so a column-scoped grant on the target is not enough; the other three run ordinary target queries, which the planner still checks per column. An external target is authorized before it is looked up, so its existence is not disclosed either. A read fails with `ACCESS_DENIED` when the user's effective `SELECT` row-policy filter on the `TimeSeries` table is not always true, since such a policy cannot be enforced on the target's rows, and the three target functions can no longer back a `CREATE TABLE ... AS` (`BAD_ARGUMENTS`). PromQL over the Prometheus HTTP and remote-read endpoints reaches `timeSeriesSelector` whenever it selects series, so those requests now need these privileges for the user the endpoint authenticated. [#116629](#116629) ([Groene AI](https://github.com/groeneai)).
Changelog-deleted-entry: 117356 [Backward Incompatible Change] [raw] * `max_bytes_before_external_join` and `max_bytes_ratio_before_external_join` are now the spill threshold for every hash-based `join_algorithm`, including `grace_hash`, which previously ignored them and spilled on `max_rows_in_join` / `max_bytes_in_join` instead. Those two are a hard cap again for every algorithm: they no longer cause spilling, and a join that spills no longer exceeds them. All join algorithms remain available; `join_algorithm = 'grace_hash'` now needs a non-zero spill threshold, and is skipped in favour of the next algorithm in the list when it has none. Set `legacy_join_size_limits_trigger_spilling = 1` (or `compatibility` to 26.8 or older) to restore the previous behavior. [#117356](#117356) ([Yarik Briukhovetskyi](https://github.com/yariks5s)).
Changelog-deleted-entry: 117449 [Build/Testing/Packaging Improvement] [raw] * ClickHouse is now built as a position independent executable on Linux amd64 and aarch64 with glibc, including the release builds, so the text segment is subject to ASLR. Addresses in the bare stack trace of a fatal log are now file offsets relative to the object that contains them, annotated with that object for frames outside the main executable, so they can be resolved with `addr2line -e <object> <hex>` regardless of where the binary was loaded. For the same reason `system.errors.last_error_trace`, `system.error_log.last_error_trace` and `system.crash_log.trace` now store a frame of the main executable as its file offset, keeping runtime addresses for frames in other objects. [#117449](#117449) ([Groene AI](https://github.com/groeneai)).
Changelog-deleted-entry: 117768 [NOT FOR CHANGELOG / INSIGNIFICANT] [raw] * Not applicable. [#117768](#117768) ([Dominic Tran](https://github.com/dhtclk)).
Changelog-deleted-entry: 118218 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Fixed cancelling a distributed, `s3Cluster` or parallel-replicas `SELECT` taking up to `receive_timeout` (300 s by default) to take effect, and never reaching some of the remote sources, when one of them was draining the packets left over after a `LIMIT` was satisfied. [#118218](#118218) ([Groene AI](https://github.com/groeneai)).
Changelog-deleted-entry: 118239 [NOT FOR CHANGELOG / INSIGNIFICANT] [raw] * Require agents to run the style check locally before pushing. [#118239](#118239) ([Nikita Fomichev](https://github.com/fm4v)).
Changelog-deleted-entry: 118606 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Proper partition pruning with dates. Closes [#103433](#103433). [#118606](#118606) ([Konstantin Vedernikov](https://github.com/scanhex12)).
Changelog-deleted-entry: 118919 [Bug Fix (user-visible misbehavior in an official stable release)] [raw] * Fixed a pending lightweight `DELETE` read with `apply_mutations_on_fly = 1` evaluating its predicate over patch parts created after it, which made rows disappear from the result and reappear after the mutation materialized. [#118919](#118919) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
alexey-milovidov added a commit that referenced this pull request Sep 24, 2026
`master` (#117356, "Unify the spill trigger for hash joins") turned `max_rows_in_join` /
`max_bytes_in_join` into a hard cap for every hash algorithm, `grace_hash` included; the spill
threshold is now `max_bytes_before_external_join`. `04659` still set `max_bytes_in_join` to make
the join spill, so it failed with `SET_SIZE_LIMIT_EXCEEDED` before reaching the temporary data
limit it checks, and the `grace_hash` part of `04646` would be rejected for having no spill
threshold. Use the spill threshold instead, the same change `master` made to `03772`.

Fast test report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=110613&sha=99480273abb6ad6ef9e69ebd13c43f93d00d2013&name_0=PR&name_1=Fast%20test
PR: #110613

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-backward-incompatible Pull request with backwards incompatible changes 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.

Unify the two disk-spilling hash-join paths: make SpillingHashJoin the single implementation

5 participants