Skip to content

Enable max_bytes_ratio_before_external_join by default - #104285

Merged
alexey-milovidov merged 6 commits into
masterfrom
enable-max-bytes-ratio-before-external-join-by-default
May 8, 2026
Merged

alexey-milovidov merged 6 commits into
masterfrom
enable-max-bytes-ratio-before-external-join-by-default

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented May 7, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #103862, which added the max_bytes_ratio_before_external_join setting with a default of 0. (disabled). The merged PR's description noted that the default would later be flipped to 0.5, mirroring how max_bytes_ratio_before_external_group_by and max_bytes_ratio_before_external_sort were rolled out (both currently default to 0.5).

This PR makes that flip: by default, hash joins now spill to grace hash join once the right-side data exceeds half of the available system memory (when memory limits are configured). When no system memory limits are configured (e.g. local test runs), the ratio is logged and ignored, just as for the analogous group-by and sort settings — so behavior in unconstrained environments is unchanged.

The SettingsChangesHistory.cpp entry for 26.5 is updated in place (from 0., 0. to 0., 0.5) since the setting was introduced in this same release.

Changelog category (leave one):

  • Improvement

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

The new `max_bytes_ratio_before_external_join` setting is now enabled by default at `0.5`, mirroring `max_bytes_ratio_before_external_group_by` and `max_bytes_ratio_before_external_sort`. Hash joins automatically spill to grace hash join once the right-side data exceeds half of the available system memory (when memory limits are configured).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.446

PR #103862 added the new `max_bytes_ratio_before_external_join` setting
with a default of `0.` (disabled), with a note that the default would
later be flipped to `0.5` to mirror how `max_bytes_ratio_before_external_group_by`
and `max_bytes_ratio_before_external_sort` were rolled out.

This is the follow-up: switch the default to `0.5` so that hash joins
automatically spill to grace hash join once the right-side data exceeds
half of available system memory (when memory limits are configured).
The behavior is unchanged for environments without configured system
memory limits — the ratio is logged and ignored in that case, just as
for the analogous group-by and sort settings.

Original PR: #103862

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [59eb5b8]

Summary: ✅


AI Review

Summary

This PR enables max_bytes_ratio_before_external_join by default (0.5), updates the related setting documentation and 26.5 history entry, and adjusts performance/stateless tests to explicitly set max_bytes_ratio_before_external_join = 0 where non-spilling behavior is required. I did not find correctness, safety, or compatibility issues in the current diff.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ✅
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 7, 2026
With `max_bytes_ratio_before_external_join` defaulting to `0.5`, the
fast-test environment (which has memory limits configured) now wraps
hash joins as `SpillingHashJoin(HashJoin)` and `SpillingHashJoin(ConcurrentHashJoin)`,
which breaks tests that:

  * compare `EXPLAIN` output containing the join algorithm name,
  * rely on optimizer rules that match a bare `HashJoin` (e.g. legacy
    join swap, read-in-order through join), or
  * exercise behaviour specific to the non-spilling hash join (e.g.
    `join_any_take_last_row`, on-the-fly fixed hash table conversion).

Replace the previously preemptive `SET max_bytes_before_external_join = 0`
(which is now a no-op since `0` is the default) with
`SET max_bytes_ratio_before_external_join = 0` to disable automatic
spilling for these tests, restoring their previous behaviour.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104285&sha=43fe2750b01ab4a0256eb5957efb1e927b86bcfd&name_0=PR&name_1=Fast%20test
PR: #104285

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…spilling

The previous fix (commit 4ca1d29) replaced
`SET max_bytes_before_external_join = 0` with
`SET max_bytes_ratio_before_external_join = 0` in tests that need to
disable automatic spilling hash join. However, the test settings
randomization in `tests/clickhouse-test` independently sets
`max_bytes_before_external_join` to either `0` or `1000000000`. When the
randomizer picks `1000000000`, the absolute threshold arms `SpillingHashJoin`
even though the ratio is `0`, because `JoinSettings::getMaxBytesBeforeExternalJoin`
combines the two and the smaller non-zero of them applies.

This breaks tests in two ways:

  * `EXPLAIN`-based tests still see `SpillingHashJoin(...)` in the output.
  * Tests like `02302_join_auto_lc_nullable_bug` that rely on
    `join_algorithm = 'auto'` falling back to merge join when
    `max_bytes_in_join` is exceeded instead get
    `SET_SIZE_LIMIT_EXCEEDED`, because the planner returns
    `SpillingHashJoin` (which doesn't fall back) when the absolute
    threshold is non-zero.

Set both `max_bytes_before_external_join = 0` and
`max_bytes_ratio_before_external_join = 0` in the affected tests, which
fully disables automatic spilling regardless of which setting the
randomizer chooses to override. Also patch
`00163_shard_join_with_empty_table` and the `01721_join_implicit_cast_long*`
tests, which were already setting only the absolute setting.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104285&sha=4ca1d2965a77344e72b6a6fe7f957e23ad4a6e4f
PR: #104285

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
{"output_format_parquet_unsupported_types_as_binary", false, false, "Obsolete setting, the native writer always throws UNKNOWN_TYPE for unsupported types."},
{"input_format_parquet_use_native_reader_v3", true, true, "Obsolete setting, the native reader v3 is now always used."},
{"max_bytes_ratio_before_external_join", 0., 0., "New setting: ratio of available memory used as the spill threshold for hash joins. Combined with the absolute `max_bytes_before_external_join` (the smaller of the two applies)."},
{"max_bytes_ratio_before_external_join", 0., 0.5, "New setting: ratio of available memory used as the spill threshold for hash joins. Enabled by default at `0.5`, mirroring `max_bytes_ratio_before_external_group_by` and `max_bytes_ratio_before_external_sort`. Combined with the absolute `max_bytes_before_external_join` (the smaller of the two applies)."},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After enabling max_bytes_ratio_before_external_join by default, the description of max_bytes_before_external_join in Settings.cpp became inaccurate: it still says “When set to 0 (default), automatic spilling is disabled.”

Now max_bytes_before_external_join = 0 only disables the absolute byte threshold; spilling can still happen via the ratio default (0.5). Please update that sentence to reflect the new behavior.

alexey-milovidov and others added 3 commits May 8, 2026 09:33
With `max_bytes_ratio_before_external_join` defaulting to `0.5`, the
performance comparison environment (which has memory limits configured)
now wraps hash joins in `SpillingHashJoin`. For perf tests that
specifically benchmark in-memory hash join behavior, this causes the
"new" side to use grace hash join while comparing against an "old"
master that still uses pure hash join, reporting them as regressions.

Set `max_bytes_ratio_before_external_join = 0` in the affected tests to
keep them measuring the same in-memory join code path as before:

  * `parallel_hash_join_various.xml` — benchmarks `parallel_hash`.
  * `join_convert_to_fixed_hash_table.xml` — benchmarks
    `enable_join_fixed_hash_table_conversion` with `hash`/`grace_hash`.
  * `join_runtime_filter.xml` — benchmarks runtime filter behavior.
  * `join_used_flags.xml` — benchmarks `RIGHT JOIN` used-flag tracking.
  * `tpch.xml` — keeps the TPC-H benchmark comparable across runs;
    adds an inline `<settings>` block that overrides
    `../benchmarks/tpc-h/settings.json`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104285&sha=c0886206419503145375bf5672cbe990b49ae40b&name_0=PR
PR: #104285

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
After enabling `max_bytes_ratio_before_external_join = 0.5` by default,
the description of `max_bytes_before_external_join` was inaccurate: it
still claimed that setting it to `0` (the default) disables automatic
spilling, but spilling can now still occur via the ratio default.

Clarify that `0` only disables the absolute byte threshold and that
both settings need to be set to `0` to fully disable automatic spilling.

Addresses review feedback on PR #104285.

PR: #104285

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (18/18) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this May 8, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 8, 2026
Merged via the queue into master with commit 7ad4085 May 8, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the enable-max-bytes-ratio-before-external-join-by-default branch May 8, 2026 14:01
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 8, 2026
antaljanosbenjamin added a commit that referenced this pull request Jul 21, 2026
…atio-before-external-join-by-default"

This reverts commit 7ad4085, reversing
changes made to 4d547b7.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

2 participants