Skip to content

Retrospectively disable max_bytes_ratio_before_external_join - #111248

Closed
antaljanosbenjamin wants to merge 38 commits into
masterfrom
disable-max_bytes_ratio_before_external_join
Closed

antaljanosbenjamin wants to merge 38 commits into
masterfrom
disable-max_bytes_ratio_before_external_join

Conversation

@antaljanosbenjamin

@antaljanosbenjamin antaljanosbenjamin commented Jul 21, 2026 •

Copy link
Copy Markdown
Member

Revert #104285

Apart from breaking read-in-order through joins it might also have other issues, e.g. runPostBuildPhase is never run. CC @alexey-milovidov

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Retrospectively disable max_bytes_ratio_before_external_join, because it breaks read-in-order through joins.


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

…atio-before-external-join-by-default"

This reverts commit 7ad4085, reversing
changes made to 4d547b7.
@antaljanosbenjamin antaljanosbenjamin changed the title Revert https://github.com/ClickHouse/ClickHouse/pull/104285 Apart from breaking read-in-order through joins it might also have other issues, e.g. runPostBuildPhase is never run. CC @alexey-milovidov ### Changelog category (leave one): - Bug Fix (user-visible misbehavior in an official stable release) ### Changelog entry (a [user-readable short description](https://github.com/ClickHouse/ClickHouse/blob/master/docs/changelog_entry_guidelines.md) of the changes that goes into CHANGELOG.md): Retrospectively disable max_bytes_ratio_before_external_join, because it breaks read-in-order through joins. Retrospectively disable max_bytes_ratio_before_external_join, because it breaks read-in-order through joins. Jul 21, 2026
@antaljanosbenjamin antaljanosbenjamin changed the title Retrospectively disable max_bytes_ratio_before_external_join, because it breaks read-in-order through joins. Retrospectively disable max_bytes_ratio_before_external_join Jul 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0dfc751]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 4/6) FAIL Performance dashboard
joins_in_memory #20 FAIL dashboard
hash_join_gather #7 unstable query history
intersect_except_all #0 unstable query history
Fast test (arm_darwin) ERROR

AI Review

Summary

This PR reverts the default max_bytes_ratio_before_external_join spill trigger back to 0, restores the read-in-order-through-join behavior that motivated the revert, and updates the affected grace_hash tests/fuzzers plus the sharded-join regressions. I found one remaining user-visible issue: the checked-in generated settings docs still advertise the old 0.5 default, so the branch's documented contract does not match the runtime behavior.

Missing context / blind spots
  • ⚠️ Most build and sanitizer jobs on the current PR head are still in progress, so I could not validate whether any later failures appear beyond the already-discussed unrelated TSan timeout.
Findings
  • ⚠️ Majors
    • [src/Core/Settings.cpp:10019] The runtime default/history now say 0, but the checked-in generated settings docs still publish 0.5 (docs/reference/settings/session-settings/max-bytes.mdx:57, docs/reference/settings/session-settings/max-bytes.mdx:172, docs/reference/settings/session-settings/max-bytes.mdx:174, plus the session-settings explorer snippets). That leaves users with conflicting defaults between the server and the docs/UI.
    • Suggested fix: regenerate the settings docs/snippets from the updated Settings.cpp metadata via the autogenerated-docs workflow instead of leaving the generated output stale.
Final Verdict
  • Status: ⚠️ Request changes
  • Regenerate the autogenerated settings docs/snippets so the published default/history matches the reverted runtime behavior.

LLVM Coverage Report

Measured on commit 0dfc751.

Metric Baseline Current Δ
Lines 88.20% 88.30% +0.10%
Functions 92.10% 92.10% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 44/47 (93.62%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 21, 2026
Comment thread src/Core/Settings.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.20% 86.20% +0.00%
Functions 92.00% 92.10% +0.10%
Branches 78.30% 78.30% +0.00%

Changed lines: Changed C/C++ lines covered: 19/19 (100.00%) · Uncovered code

Full report · Diff report

Comment thread src/Core/SettingsChangesHistory.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 41 queries analysed

This PR is join-focused: it defaults max_bytes_ratio_before_external_join to 0 (disabling automatic hash-join spilling because it interferes with read-in-order-through-join) and adds shard-by-pk-ranges stats aggregation and a LEFT-join limit-propagation fix. The one flagged query, ClickBench Q18, is a single-table aggregation with no join, so its +5.8% (median 1052.5 to 1114.0 ms) cannot come from this change; the delta lands inside master's current variance band [531,1241] ms and CPU time is essentially flat (+1.8%), so the deterministic envelope suppression to not_sure is correct. No join-exercising benchmark query showed a real regression.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 18 not_sure 1052 1114 +5.8% <0.0001 Q18 is a single-table aggregation with no joins, and this PR only touches join code paths, so the +5.8% is off-path variance. It sits inside master's current band [531,1241] ms and CPU time is flat, so the envelope suppression is correct.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: abd7f5fe-6619-48af-9aba-d7fd093219db
  • MIRAI run: 5c1dd5b9-8012-4bc6-8c10-0c7020b1938d
  • PR check IDs:
    • clickbench_895126_1788361887
    • clickbench_895132_1788361887
    • clickbench_895138_1788361887
    • tpch_adapted_1_official_895149_1788361887
    • tpch_adapted_1_official_895182_1788361887
    • tpch_adapted_1_official_895196_1788361887

@clickhouse-gh

clickhouse-gh Bot commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 0dfc751ff155af490fafea2be46d77b07350d77b was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

antaljanosbenjamin and others added 9 commits August 25, 2026 19:14
`max_rows_to_read` with `read_overflow_mode = 'throw'` is checked against the row estimate the reading step announces, and `buildSortingDAG` dropped the LIMIT at every join, so read-in-order through a `LEFT JOIN` announced whole parts and failed the check despite reading only a few rows. A `LEFT ALL`/`LEFT ANY` join emits at least one row per left row, so the limit may be kept (`joinKeepsLeftSideLimit`). Restores the part of #111973 that the #116286 revert removed; resurfaced by this PR's `max_bytes_ratio_before_external_join = 0`, which unwraps `SpillingHashJoin` and makes the through-join pass the default path (caught by `04516_join_order_estimation_pruned_parts` in Fast test).
The assertion didn't work on blob storage because the read is structured differently.
…d by PK ranges

A join sharded by primary-key ranges runs on per-shard clones and the original join object stays empty, so `EXPLAIN ANALYZE` reported `unique keys 0.00 · memory 0.00 B` for it. Keep the clones in `JoinStep` and sum the quantities over them (the shards partition both inputs into disjoint key ranges). Unreachable on master because `SpillingHashJoin` is never sharded; revived by disabling `max_bytes_ratio_before_external_join`. Caught by `04904_explain_analyze_join_parallel_hash_unique_keys`.
alexey-milovidov and others added 2 commits September 17, 2026 14:24
…atio_before_external_join

# Conflicts:
#	src/Core/SettingsChangesHistory.cpp
#	src/Processors/QueryPlan/JoinStep.cpp
#	src/Processors/QueryPlan/JoinStep.h
#	tests/queries/0_stateless/03786_storage_join_type_conversion.sql
#	tests/queries/0_stateless/04357_join_order_dp_uniqcombined.reference
…` guard

The old query analysis cannot be enabled anymore: since v26.9 `SET enable_analyzer = 0`
fails with `SETTING_CONSTRAINT_VIOLATION`, so the legacy `JoinStep` branch that
`05153_convert_outer_join_to_inner_join_nondeterministic_old_analyzer` exercised is unreachable
and the test fails on its first statement. The guard itself stays as a mirror of the
`JoinStepLogical` path.

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

This comment was marked as low quality.

alexey-milovidov and others added 2 commits September 23, 2026 19:16
…atio_before_external_join

# Conflicts:
#	src/Core/Settings.cpp
… history block

The `settings_changes_history` style check requires the change to be
recorded under the current version block, which is `26.10` after the
version bump on `master`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111248&sha=10fde406f0f35e91ac195ecd43eb81f6e611ba8e&name_0=PR&name_1=Style%20check
PR: #111248

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

This comment was marked as low quality.

alexey-milovidov and others added 2 commits September 26, 2026 23:22
…atio_before_external_join

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	tests/queries/0_stateless/03130_convert_outer_join_to_inner_join.sql
#	tests/queries/0_stateless/03701_analyzer_correlated_subquery_plan_reference.sql
#	tests/queries/0_stateless/03773_semi_join_equivalent_sets.sql
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master again (head f4b611bdf1a8). There were conflicts in three tests: 03130_convert_outer_join_to_inner_join, 03701_analyzer_correlated_subquery_plan_reference and 03773_semi_join_equivalent_sets. Master made query_plan_optimize_prewhere obsolete and already sets max_bytes_before_external_join = 0, max_bytes_ratio_before_external_join = 0 in these tests, so I took master's version and they no longer appear in this PR's diff. The merged tree builds, and all three tests match their references locally.

The Fast test reds on the previous head (03006_join_on_inequal_expression_2, 04078_grace_hash_join_read_in_order, 04003_grace_hash_join_with_totals, and about 33 others) all fail with join_algorithm = 'grace_hash' is external from the first block and needs a spill threshold. That is the open design question from my previous comment: with this PR's 0 default, a bare grace_hash has no spill threshold. @antaljanosbenjamin, this still needs your decision on which option to take, so I haven't changed the tests or the code here.

I left the compatibility review thread alone, because you already answered it.

alexey-milovidov and others added 2 commits October 4, 2026 22:09
# Conflicts:
#	tests/queries/0_stateless/04105_limit_by_into_sort_explain.reference
…sh` is used

After merging master, standalone `grace_hash` requires a non-zero spill
threshold (`fc7383610ff`, "Unify the spill trigger for hash joins"). It used
to get one from the `0.5` default of `max_bytes_ratio_before_external_join`,
which this PR disables, so every test that runs `join_algorithm = 'grace_hash'`
without setting a threshold failed with `BAD_ARGUMENTS`.

Set `max_bytes_ratio_before_external_join = 0.5` (the value these tests ran
with on master) in the affected stateless tests, the performance tests that
run `grace_hash`, and the stress test when it selects `grace_hash`.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master and resolved the conflict in 04105_limit_by_into_sort_explain.reference: I kept this PR's non-spilling hash pipeline and took master's updated full_sorting_merge comment and plan.

All 36 Fast test failures came from one semantic conflict with master. Since fc73836 ("Unify the spill trigger for hash joins"), standalone join_algorithm = 'grace_hash' needs a non-zero spill threshold, or it throws BAD_ARGUMENTS. Until now it got that threshold from the 0.5 default of max_bytes_ratio_before_external_join. In dc22f67 I set max_bytes_ratio_before_external_join = 0.5 (the value they had on master) in the affected stateless tests, in the performance tests that run grace_hash, and in the stress test whenever it picks grace_hash. I ran the affected tests locally against the merged build, and they pass. The only failures left come from my local setup (missing test clusters and user storage).

Needs a decision from you: with this PR, SET join_algorithm = 'grace_hash' on its own fails by default unless the user also sets max_bytes_before_external_join or max_bytes_ratio_before_external_join. Lists like 'grace_hash,hash' still skip to the next algorithm. The setting docs already describe this, but it should probably go into the changelog entry.

alexey-milovidov and others added 2 commits October 5, 2026 20:02
# Conflicts:
#	src/Core/Settings.cpp
…ore_external_join`

- `gtest_join_spill_trigger_plan_setting`: the cases are written against a non-zero ratio out of the box, so
  `makeJoinSettings` now sets `max_bytes_ratio_before_external_join = 0.5` as its baseline.
- `01721_join_implicit_cast_long`: the `auto` branch resets the ratio to `0` again; the preceding `hash` iteration
  sets it to `0.5`, so the join kept spilling and hit `max_bytes_in_join` instead of switching to the merge join.
- `02273_full_sort_join`: `max_bytes_before_external_join` is reset to `0` inside the `grace_hash` iteration, which
  left `grace_hash` without a spill threshold; pin the ratio to `0.5`.
- `03514_grace_hash_join_logical_error`: tag as `long`; it takes 70-240 s under TSan on master too and
  fails only the run-time limit of the flaky check.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111248&sha=dc22f672b1a7f41d556e33f4ce1ac424faa34c1b&name_0=PR
PR: #111248

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Core/Settings.cpp Resolved
alexey-milovidov and others added 2 commits October 6, 2026 14:25
With `max_bytes_ratio_before_external_join` back at `0` by default, a standalone
`join_algorithm = 'grace_hash'` has no spill threshold and fails with `BAD_ARGUMENTS`
(or is skipped in favour of the next algorithm of the list). `BuzzHouse` generated
`join_algorithm` independently of the thresholds, so most of its `grace_hash`
queries stopped exercising the join. Usually add a non-zero
`max_bytes_before_external_join` alongside such a `join_algorithm`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master and pushed 479fa15 (review follow-up for BuzzHouse).

The only red check on the previous head was Performance Comparison (arm_release, master_head, 3/6). It flagged joins_in_memory #19/#20 (FULL JOIN ... USING i64,i32,i16,i8) as about 46% slower: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111248&sha=b9f678cdbe1b435cdce33bc51ea37ca3da0da9be&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%203%2F6%29

This slowdown comes from the revert itself. I reproduced it locally with clickhouse local, changing only max_bytes_ratio_before_external_join:

  • 0.5 (current master default): about 0.069 s per query.
  • 0 (this PR): about 0.105 s per query.

With a non-zero threshold, the hash join is wrapped in the spill-capable join. That wrapper emits the non-joined rows of a FULL join in parallel through DelayedJoinedBlocksWorkerTransform. The plain HashJoin pipeline does not do that, so this was the behaviour before #104285. The same happens to parallel_hash_join_various #0.

I didn't pin the threshold in that perf test, because it would hide a real cost of the revert. Whether to accept this trade-off for read-in-order through joins needs a human decision.

alexey-milovidov and others added 2 commits October 7, 2026 03:54
…he stress test

`max_bytes_ratio_before_external_join` resolves to zero when the server has no memory limit, so set `max_bytes_before_external_join` as well.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111248&sha=479fa153fc574bc28dfedff3f2a546138815679e&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20targeted%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. 00314_sample_factor_virtual_column (no JOIN) went over the 180 s limit under TSan with parallel replicas and max_threads = 1. In the last 30 days it took up to 296 s in Stateless tests (amd_tsan, s3 storage, parallel), so it is slow on master too and not caused by this PR.

I also merged master (clean) and addressed the stress.py review thread in 69a2013.

Comment thread src/Core/Settings.cpp
Has effect for every hash-based `join_algorithm`, including `grace_hash`, provided a temporary data path is configured.
)", 0, \
{"26.5", 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)."}) \
{"26.10", 0., 0., "Retrospectively disable automatic spilling, because it interferes with read-in-order-through-join optimization."}, \

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.

Changing the structured setting metadata here leaves the checked-in generated docs inconsistent with the runtime default: docs/reference/settings/session-settings/max-bytes.mdx:57 still says max_bytes_ratio_before_external_join defaults to 0.5, the SettingsInfoBlock / VersionHistory at docs/reference/settings/session-settings/max-bytes.mdx:172 and docs/reference/settings/session-settings/max-bytes.mdx:174 still expose 0.5, and the session-settings explorer snippets still list 0.5 as well. Because this PR changes the user-visible contract of the setting, it needs the matching autogenerated-docs update; otherwise the docs/UI ship a different default than the server.

@groeneai

groeneai commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Acknowledged. I see no fix in progress for this yet. I'm investigating the 00314_sample_factor_virtual_column timeout under TSan with parallel replicas and max_threads = 1; I'll follow up here with a separate PR (or my findings) shortly.

@groeneai

groeneai commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

The fix is in #124368 (test-only). The test created its tables with index_granularity 10 and inserted with min_insert_block_size_rows = 0, so every random max_block_size block became its own part with one compressed block per column per 10-row granule (about 600k blocks per run), and the random ZSTD server codec makes each block expensive under TSan. Parallel replicas were not active in the failing run (automatic_parallel_replicas_mode = 2). With granularity 8192 and a max_block_size floor, the TSan p50 went from 51 s to 8.3 s, and the asserted values are unchanged.

alexey-milovidov pushed a commit to amosbird/ClickHouse that referenced this pull request Oct 7, 2026
The test created its tables with an explicit index_granularity of 10 and
inserted 3M rows with min_insert_block_size_rows = 0, so every random
max_block_size block (down to 8000 rows) became its own compact part with
one compressed block per column per 10-row granule: ~600k compressed blocks
written, decoded and re-encoded again by the background merges, and ~600k
marks loaded by the SELECTs. Under TSan, with the random ZSTD server codec
(a fresh context per block), a run took up to 296 s in the s3 parallel lane
and failed the 180 s limit of the targeted check on PR ClickHouse#111248.

Use the default granularity 8192 and clamp max_block_size to at least 65409.
The asserted sum(_sample_factor) does not depend on granularity or part
layout (exact row count for the relative sample size, exact intHash64
filter): measured identical (2997580 over 199910 rows) for granularity 10
and 8192 and block sizes 8188, 65409 and 100000. TSan, 18 concurrent runs
on 32 CPUs: p50 51 s -> 8.3 s (ZSTD(1)), 32 s -> 7.1 s (LZ4), 51 s -> 9.6 s
(S3 on MinIO).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vdimir

vdimir commented Nov 11, 2026

Copy link
Copy Markdown
Member

Closed because Spilling Join now supports read in order: #124873

@antaljanosbenjamin @alexey-milovidov

Could we disable max_bytes_ratio_before_external_join in versions <26.11 so that read in order through join works again by default?

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-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants