Skip to content

Reintroduce join lazy column indexes - #106566

Merged
m-selmi merged 11 commits into
ClickHouse:masterfrom
m-selmi:reintroduce-98883-revisit-lazy-column-replication-usage
Jun 19, 2026
Merged

m-selmi merged 11 commits into
ClickHouse:masterfrom
m-selmi:reintroduce-98883-revisit-lazy-column-replication-usage

Conversation

@m-selmi

@m-selmi m-selmi commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Reintroduces #98883 reverted in #106095.

The issue was the following:

  • the change introduced per chunk compaction of replicated columns in compactReplicatedColumns(), that materializes the replicated column or removes unused rows from the underlaying data depending whether the replication is useful or not. So the same column could be replicated or not in two different chunks.
  • When such a column is used in FinishSortingTransform for sorting, a ColumnReplicated over a ColumnString can land at compareAt() of ColumnString, leading to Bad cast from type DB::ColumnReplicated to DB::ColumnString.

The fix materializes sorting columns in FinishSortingTransform similar to what is done in SortingTransform (1f69d10).

The PR also fixes a corner case in the serialization of ColumnReplicated caught by AI review in 19ab356.

Changelog category (leave one):

  • Performance Improvement

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

Reintroduce lazy application of selector and replication indexes in case of JOIN followed by a selective LIMIT or TopN or another JOIN. To control the number of payload columns for enabling lazy selector indexes use the setting query_plan_min_columns_for_join_lazy_indexing (0 means the optimization is disabled). To control the LIMIT for which the optimization is applied use the setting query_plan_max_limit_for_join_lazy_indexing.

@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d963df8]

Summary: ✅


AI Review

Summary

This PR reintroduces join lazy column indexing and changes ColumnReplicated to defer compaction until explicit boundaries such as sorting, buffering, caching, and NativeWriter. I reviewed the current code, the PR discussion, and all prior inline threads. The current implementation addresses the earlier shared-index, disabled-setting, and serialization stale-state concerns; I did not find any remaining issue that warrants an inline review comment.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 5, 2026
@m-selmi m-selmi added the can be tested Allows running workflows for external contributors label Jun 5, 2026
Comment thread src/Interpreters/SpillingHashJoin.cpp Resolved
Comment thread src/Columns/ColumnReplicated.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 3 query(s) improved out of 38 analysed

This PR adds a join lazy-indexing optimization (hash joins emit ColumnReplicated probe-side columns plus the supporting sort/chunk plumbing), enabled by default. The intended payoff shows clearly on the join-heavy TPC-H queries Q7 (-37%) and Q20 (-50%), which is the genuine, expected win. ClickBench contains no joins, so its flagged deltas sit off the PR's code path; the +7-15% regressions there track a trending master baseline and were downgraded as drift/variance rather than real PR effects. The one exception is ClickBench Q23 (-31%), a large, consistent delta that stands on its magnitude even though the mechanism on a join-less query is unclear.

clickbench

🟢 1 improved · ⚠️ 8 inconclusive

Flagged queries (9 of 43)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🟢 23 improvement 297 205 -31.0% <0.0001 Large clean delta stands on magnitude, but ClickBench has no joins so the PR mechanism is unclear here.
⚠️ 4 not_sure 264 292 +10.6% <0.0001 ClickBench has no joins; join lazy-indexing PR can't touch this aggregation. Trending master = drift, not PR.
⚠️ 13 not_sure 497 571 +14.9% <0.0001 Join-less ClickBench query; the PR only changes the join/sort path. Trending master points to drift.
⚠️ 17 not_sure 564 634 +12.4% <0.0001 Single-table query, no joins; off the PR's code path. Likely master drift (trending), not a real regression.
⚠️ 18 not_sure 1403 1564 +11.5% <0.0001 Join-less query; off the join-indexing path. Trending master indicates drift, not a PR effect.
⚠️ 22 not_sure 334 268 -19.8% <0.0001 Improvement on a join-less query the PR can't speed up; base was very noisy. Run-to-run variance.
⚠️ 32 not_sure 1367 1509 +10.4% <0.0001 Join-less query, off the PR path; borderline on one test and trending master. Looks like drift.
⚠️ 33 not_sure 1488 1586 +6.6% <0.0001 Join-less ClickBench query, off the PR's join path; small delta, likely variance.
⚠️ 34 not_sure 1486 1600 +7.7% <0.0001 Join-less query, off the PR's join path; likely run-to-run variance or master drift.

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

🟢 2 improved

Flagged queries (2 of 22)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🟢 7 improvement 113 71 -37.2% <0.0001 join_operator: Join-heavy query; lazy indexing avoids materializing probe-side columns. Improvement matches PR intent.
🟢 20 improvement 248 122 -50.8% <0.0001 join_operator: Join lazy indexing speeds the join chain; -50% matches PR target. Base was noisy but direction is clear.

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.

Debug info
  • StressHouse run: a025b5a6-d706-4da7-bcf1-2602a9910e9b
  • MIRAI run: 9940118e-76c1-4b13-b4b4-d608d2c96947
  • PR check IDs:
    • clickbench_177648_1781535633
    • clickbench_177681_1781535632
    • tpch_adapted_1_official_177687_1781535634
    • tpch_adapted_1_official_177700_1781535631
    • tpch_adapted_1_official_177703_1781535633

Comment thread src/Core/Settings.cpp Resolved
@m-selmi
m-selmi marked this pull request as ready for review June 12, 2026 17:04
@m-selmi
m-selmi requested a review from Avogar June 16, 2026 11:41
@Avogar Avogar self-assigned this Jun 16, 2026
@Avogar

Avogar commented Jun 16, 2026 •

Copy link
Copy Markdown
Member

Are failures in fast test related?

UPD: Seems like not related, it was broken widely

Comment thread src/Columns/ColumnReplicated.cpp Resolved
Comment thread src/Formats/NativeWriter.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.40% 77.40% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 286/302 (94.70%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 25 line(s) · Uncovered code

Full report · Diff report

@m-selmi
m-selmi added this pull request to the merge queue Jun 19, 2026
Merged via the queue into ClickHouse:master with commit 2c34ffa Jun 19, 2026
324 of 326 checks passed
@m-selmi
m-selmi deleted the reintroduce-98883-revisit-lazy-column-replication-usage branch June 19, 2026 08:27
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 19, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 9, 2026
ColumnReplicated::permute required perm.size() == size() exactly, which is
stricter than the general IColumn::permute contract. ORDER BY <LowCardinality
column> LIMIT N after a LEFT JOIN with lazy column replication passes a
permutation of size min(rows, limit) = LIMIT, so the check threw
SIZES_OF_COLUMNS_DOESNT_MATCH (Code 9) on 26.5 and 26.3 LTS.

Remove the redundant check: the underlying indexes column has the same size
as the ColumnReplicated, and its permute already enforces the correct
contract via getLimitForPermutation (throws when perm.size() < min(size, limit)).

Master already avoids the throw by routing sortBlock through
transformColumnsWithSharedIndex (ClickHouse#106566), so this is a minimal, backportable
fix for the LTS branches. Regression test pins the query on master.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 13, 2026
Remove author name and the PR ClickHouse#106566 reference from the in-file comment.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
pull Bot pushed a commit to AKJUS/ClickHouse that referenced this pull request Jul 30, 2026
…ckHouse#109877)

ORDER BY a LowCardinality column with LIMIT below the row count, after a
LEFT JOIN using lazy column replication, produced a permutation of size
min(rows, limit) that ColumnReplicated::permute rejected with
SIZES_OF_COLUMNS_DOESNT_MATCH. Fixed on master by ClickHouse#106566, which routes
sortBlock through transformColumnsWithSharedIndex. This adds den-crane's
minimal repro from the issue as a stateless regression test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
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-performance Pull request with some performance 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.

3 participants