Repository navigation
Integrate hash join row store into new column gather framework - #121981
Conversation
|
Workflow [PR], commit [59846e7] Summary: ✅
AI ReviewSummaryThis PR folds the hash-join row store into the newer gather framework, removes the duplicated row-store emit path, and retunes when the row-major layout is enabled. The core gather refactor looks sound, and the previously reported empty-row-store / per-batch-flattening issues are fixed in the current code. I still do not think the PR fully meets its own “skip narrow payloads” contract on Findings
Tests
Final Verdict
LLVM Coverage Report |
Build profile diff (arm_release)Comparing Binary sizes
The official master build is compiled with Object file sizes
|
| Object file | Master | PR | Δ |
|---|---|---|---|
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/HashJoin.cpp.o |
4.03 MiB | 3.25 MiB | -796.22 KiB (-19.28%) |
src/CMakeFiles/dbms.dir/Columns/IColumn.cpp.o |
1.98 MiB | 1.88 MiB | -96.33 KiB (-4.76%) |
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/gatherJoinOutputColumns.cpp.o |
100.42 KiB | 125.57 KiB | +25.14 KiB (+25.04%) |
src/CMakeFiles/dbms.dir/Columns/ColumnVector.cpp.o |
5.35 MiB | 5.33 MiB | -18.12 KiB (-0.33%) |
src/CMakeFiles/dbms.dir/Interpreters/HashJoin/AddedColumns.cpp.o |
71.62 KiB | 54.75 KiB | -16.88 KiB (-23.56%) |
720 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.
Compile time of recompiled translation units
2547 translation units recompiled, 14551 s compile time in total, 2547 of them have a recent master baseline.
|
@m-selmi Can you include benchmark for larger build sides (~ 100M rows distinct)? |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only changes the hash join row store: right-side columns are now emitted through the shared gather path, the store keeps only columns that appear in the join output, the store is skipped unless it saves at least two cache misses per row, and min_rows_ratio_for_hash_join_row_store drops from 5.0 to 3.0. The only flagged query is TPC-H Q1 (tpch_adapted_1_official), which got -16.9% faster (65 ms to 54 ms). Q1 is a single-table scan and aggregation with no join, so it never runs the changed code. Its new 54 ms median sits at the bottom of master's current 54-70 ms range, and the query has a noisy history, so it is downgraded to not sure. No join-heavy query moved enough to be flagged, so this run shows no measurable effect of the PR in either direction. clickbench🟢 No significant changes tpch_adapted_1_officialFlagged queries (1 of 22)
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. Debug info
|
@harikrishnan94 the diagrams I included above summarizing the benchmarks I did include a 100M rows build side in the last column (all experiments are with distinct row). |
| extern const int SIZES_OF_COLUMNS_DOESNT_MATCH; | ||
| } | ||
|
|
||
| static constexpr UInt64 BYTES_IN_CACHE_LINE = 64; |
There was a problem hiding this comment.
Could we use DB::CH_CACHE_LINE_SIZE from Common/CacheLine.h here?
| /// Add each field's offset, size and nullability to the row store access indexes. | ||
| RowDataStore::RowLayoutPtr layout = RowDataStore::computeLayout(row_store_columns, row_store_types); | ||
|
|
||
| if (RowDataStore::cacheMissReduction(*layout) < MIN_CACHE_MISSES_SAVED_BY_ROW_STORE) |
There was a problem hiding this comment.
The fixed 5x reuse gate predates this PR. Since this changes the gather costs, would it be worth revisiting that decision here?
A rough break-even model could be:
gathered_rows * gather_time_saved_per_row > build_rows * construction_time_per_row
Here, gathered_rows counts output rows that read the stored payload. The saving is columnar gather time minus row-store gather time; the right side is the extra construction cost.
Three UInt64 columns and four FixedString(32) columns have the same estimated cache-line saving, but pack 24 vs 128 bytes per build row. This could account for that difference instead of giving both the same reuse cutoff.
Could we run a few experiments measuring construction time and gather savings separately across payload widths, build sizes and reuse ratios, to estimate these costs and check the cutoff?
There was a problem hiding this comment.
I ran the suggested experiment with the following parameters:
- build rows = 100k, 1m, 10m, 50m
- payload length per row = 16, 32, 64, 128, 256, 512, 1024 B (not every length exists at every size)
- payload columns = 4, 8, 16, 32 (not every count exists at every width)
- reuse ratio = 2, 4, 5, 6, 8, 10
The full results are here.
The break-even model somewhat tracks the wall improvement but not perfectly. If we ignore small sizes where the wall diff is basically noise, there are still some cases at 50M build size for example where wall time shows a great improvement but the model doesn't.

Regarding the model itself, construction_time_per_row can be predicted well from row length, but it's also affected by number of columns.

The gather_time_saved_per_row however varies depending on build size, cache misses saved, row length. So it's hard to predicat with a simple formula. One interesting thing is that a larger payload row also leads to higher gather_time_saved_per_row, probably because the column grows out of cache faster. So this somewhat compensates for the cache line saving estimation being blind to row length.

Nevertheless if we still derive the break even gathered_rows / build_rows ratios of the model per case, they at least fall under 5 for the meaningful build sizes.

A ratio of 5 seems more on the conservative side, but I doubt we can come up with a more meaningful threshold. We are anyway depending on optimizer estimates and the exact behavior is hardware dependent, so it makes sense to be conservative. wdt?
One thing we could try in the future is to measure if the row store was useful for a specific join during execution and disable it in future executions (but not sure about the other direction).
There was a problem hiding this comment.
Can we reduce the threshold to 3x?
There was a problem hiding this comment.
yeah that makes sense, then we don't miss out on some of the speedups seen in the benchmark. Done.
I will also investigate if there are ways to make the choice adaptive in follow-ups.
Follow-up to #104884
Follow-up to #117460
In #117460 an optimized gather framework was added for the columnar part of the hash join output while the row store optimization stayed in the old framework. This PR integrates the row store into the new gather code.
This gives the row store the same optimizations the gather code includes: prefetching and more optimal copying.
Performance results from
hash_join_with_row_store.xmlshow this:The PR also includes re-tuning for when the row store is enabled.
With the new optimized gather path, columnar and row store share the exact same gather implementation. The remaining advantage of the row store can be described in terms of improved cache utilization.
The performance analysis below shows this. For small build sides, the row store basically has no effect on performance since build the row store is almost free but everything is cache resident anyway. For large build sides that are narrow (very few columns) the row store can be negative since it does not save enough cache misses to hide the cost of building it.
So a new condition is added to only enable the row store only when the estimated cache misses per row are reduced here. The affected lines are marked in the analysis below.
The minimum output to build size ratio gate is left as is (5x), the numbers show it's still conservative, but given we rely on optimizer estimations when no statistics are cached, that still makes sense.
In terms of absolute runtime:


In terms of relative speedup:


Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Speed up hash join output construction in the row-major layout: output reconstruction is up to 2x faster for joins with many rows per key and about 1.5x for wide or
Nullablepayloads. The row-major layout is now also skipped for payloads too narrow to benefit from it.Workflow [PR]
Sync PR [sync-upstream/pr/121981]
Version info
26.10.1.1638-master(included in26.10and later)