Skip to content

Add string column support in hash join row store - #125979

Merged
m-selmi merged 21 commits into
masterfrom
row-store-strings
Nov 9, 2026
Merged

m-selmi merged 21 commits into
masterfrom
row-store-strings

Conversation

@m-selmi

@m-selmi m-selmi commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Related: #104884
Related: #121981

Store String and Nullable(String) columns of the right table in the hash join row store.

  • A value is kept in a 16-byte UnhashedPackedStringRef: up to 15 bytes inline, longer values copied to a per-store overflow buffer.
  • A column is admitted when its average size from basic statistics is at most 23 bytes (15 chars + offset). Without statistics, it stays columnar.
  • Small string columns (below 8MiB) are skipped because the overhead of the row store can not be compensated.
  • min_string_column_bytes_for_hash_join_row_store = 0 keeps String columns out of the row store.

Benchmark results:

from hash_join_with_row_store_strings.xml

image image

Breakdown by column count, build size, and string length distribution:

image

The row layout is not worth it at small string columns because the columns already fit in cache and no cache misses can be saved compared to the columnar format. In contrast, row store has the overhead of extracting the packed string refs during output column gather.

image

Memory behavior:

  • A stored value takes a 16-byte ref (17 for Nullable) instead of the columnar n + 8 bytes (chars + offset), a value longer than 15 bytes is also copied to the store's overflow buffer.
    Per value, that is 8 − n bytes more for n ≤ 8, n - 8 bytes less for n > 8 and 8 bytes more for longer values.
  • The store's size, overflow buffer included, is counted in the join size, so max_bytes_in_join and spilling see it.
  • The output gather stages 16 bytes per output row and stored string column, bounded by max_joined_block_size_rows and the number of probing streams. Like the output block, it is tracked by the memory tracker but not counted in the join size.

Measured peak memory in above experiment: +1% to +18% at 10M and 50M build rows
image

The worst case from the perspective of memory occurs when all string values are 1 byte long. For 6 columns of 10M rows: memory usage increased by +57% (1,102 → 1,728 MiB), and runtime improved by 1.89x (637 -> 337 ms).

Changelog category (leave one):

  • Performance Improvement

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

Hash joins now store String and Nullable(String) columns of the right table in the row store when their average value size (from basic column statistics) is at most 15 bytes. Joins that output several such columns from a large right table can achieve speedups of 1.4x to 2.4x or higher with more columns, with about 9% to 15% more memory for the right table. Controlled by min_string_column_bytes_for_hash_join_row_store (default 8 MiB, 0 disables).


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

Version info

  • Merged into: 26.11.1.1770-master (included in 26.11 and later)

@m-selmi m-selmi added the can be tested Allows running workflows for external contributors label Oct 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f7c2dca]

Summary: ✅


AI Review

Summary

Reviewed PR #125979 at f7c2dca716d695b60360eb0671794ce9f826dc10, including all prior discussion and the current implementation. The PR extends the hash join row store to statistics-selected String and Nullable(String) payloads, with inline short values and owned overflow storage. No actionable findings remain. The previous serialization finding is addressed by the version gate and focused coverage; the compatibility history restores the previous behavior. The description supplies performance measurements and explains the memory tradeoff.

Consumers checked for the new representation:

  • LazyOutput and NotJoinedHash use the string-specific gather path, including default rows and appended unmatched-right output.
  • materializeStoredBlock, used by spill/release paths, reconstructs owned string columns through scatterRows and ColumnString::fillFromRowStorePtrs before releasing the store.
  • StoredBlock::allocatedBytes includes overflow storage; LazyOutput::buildOutputFromBlocksLimitAndOffset includes long-value bytes through RowDataStore::byteSizeAt.
  • Adaptive aggregation uses gatherSelectedIntoRows for fixed-width arguments; variable-width string arguments retain their separate encoding.
  • StorageJoin and joinGet retain columnar storage because their build path does not populate row-store candidates.

The new 05345_hash_join_row_store_strings test passed in Fast test CI. It compares enabled/disabled results and checks admission, nullable values, empty strings, inline boundaries, outer joins, parallel hash joins, and spilling.

Missing context / blind spots
  • ⚠️ No local build or tests were run: this checkout has no ClickHouse binary or configured build. Current-head full sanitizer and performance jobs have no results yet because the workflow was limited by the review-thread gate. Their completed runs would provide the remaining runtime validation.
Final Verdict

✅ Approve. No new inline findings or thread changes are required. Normal CI completion remains pending.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Oct 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Oct 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f7c2dca71 with master 1d607cb1a (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

481 object files changed (+288.81 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Columns/IColumn.cpp.o 1.84 MiB 1.93 MiB +92.32 KiB (+4.89%)
src/CMakeFiles/dbms.dir/Interpreters/RowDataStore.cpp.o 158.26 KiB 85.18 KiB -73.08 KiB (-46.18%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/JoinStepLogical.cpp.o 1.01 MiB 1.05 MiB +39.06 KiB (+3.77%)
src/CMakeFiles/dbms.dir/Columns/ColumnVector.cpp.o 5.32 MiB 5.34 MiB +23.00 KiB (+0.42%)
src/CMakeFiles/dbms.dir/Columns/ColumnUnique.cpp.o 2.94 MiB 2.97 MiB +21.82 KiB (+0.72%)
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/optimizeJoin.cpp.o 725.51 KiB 741.53 KiB +16.02 KiB (+2.21%)

719 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

2689 translation units recompiled, 16682 s compile time in total, 2689 of them have a recent master baseline.

Job report

@m-selmi
m-selmi marked this pull request as ready for review October 16, 2026 07:34
@harikrishnan94 harikrishnan94 self-assigned this Oct 16, 2026

@harikrishnan94 harikrishnan94 left a comment

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.

Can this be one template <size_t HASH_BYTES> struct PackedStringRefT? no base, no protected. PackedStringRef / UnhashedPackedStringRef become aliases for <4> / <0>, and the hash and medium members get requires. also add a copyTo for <0>, the same copy loop sits in ColumnString.cpp and gatherColumnStringChars.

Comment thread src/Interpreters/JoinOperator.cpp Resolved
@m-selmi
m-selmi added this pull request to stack #127274 October 21, 2026 21:13
Comment thread src/Interpreters/RowDataStore.cpp Resolved
Comment thread src/Core/Settings.cpp Resolved
@cursor

cursor Bot commented Nov 2, 2026

Copy link
Copy Markdown

Thanks for the PR, @m-selmi! few comments inline.

  1. changelog entry: pls give the numbers and the knob. 31-58% faster in hash_join_with_row_store_strings on amd and arm, about 8-12% more build-side memory, needs basic statistics, controlled by min_string_column_bytes_for_hash_join_row_store. "small" and "faster" tell a user nothing.
  2. description: the store keeps 16 bytes per value (17 for Nullable) plus long values again in the overflow buffer, so it's bigger than columnar for values under 8 or over 15 bytes. pls state the memory cost. the staging array in gatherJoinOutputColumns (16 bytes per output row per string column) isn't in the join size either.
  3. pls add Related: lines for Transform JOIN hash table payload to row major #104884 and Integrate hash join row store into new column gather framework #121981, and fix the typos ("componsated", "destribution").

@m-selmi

m-selmi commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@harikrishnan94 thanks for the comments! Please check the Memory behavior sections added to the description for a breakdown of the increase in memory usage when strings are transformed.

Comment thread src/Interpreters/RowDataStore.cpp Resolved
Comment thread src/Interpreters/RowDataStore.cpp Resolved

@harikrishnan94 harikrishnan94 left a comment

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.

Thanks Hechem for the changes, LGTM

harikrishnan94 added a commit that referenced this pull request Nov 5, 2026
`PackedStringRef` becomes `PackedStringRefT<4>`, and the new `UnhashedPackedStringRef` is `PackedStringRefT<0>`. The unhashed value has no hash and no medium encoding. A string of up to 15 bytes is stored in the value itself, and a longer string as its length and its address.

`buildShort` builds the value of a string of up to 15 bytes with one 16-byte load and a mask, without a branch on the length. `buildLong` builds the value of a longer string. `build(ptr, len)` calls one of them by the length. `size` and `copyTo` read a string back.

`operator==` compares two values as strings: small values by their 16 bytes, and large values by their bytes when their addresses differ. `heapSize`, `getHeapPtr` and `setHeapPointer` read and rebind the address of a large string, as a key holder needs when it moves a key into an arena. The conversion to `std::string_view` views a small string in the value and a large string where it is.

The unhashed value is 4-byte aligned, so that a row can hold it at any multiple of 4 bytes. `PackedStringRef` keeps the 8-byte alignment of its hash table cells.

The unit tests check the layout, the alignment and the access of the new type: the empty string, zero bytes in a string, the longest small string, the large encoding, equality and the heap pointer.

The type and the code of `buildShort` come from #125979, where the hash join row store uses them.

Co-Authored-By: Hechem Selmi <[email protected]>
Co-Authored-By: Nihal Z. Miaji <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@m-selmi
m-selmi added this pull request to the merge queue Nov 9, 2026
Merged via the queue into master with commit 1b62031 Nov 9, 2026
369 of 370 checks passed
@m-selmi
m-selmi deleted the row-store-strings branch November 9, 2026 23:03
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 9, 2026
nickitat added a commit that referenced this pull request Nov 12, 2026
…lysis

The row store for `String` keys (#125979),
merged from master, estimates the right input of a hash join for the average
bytes of its columns with `estimateReadRowsCount`, without
`find_single_node_read`. In the probe plan of automatic parallel replicas that
analyzed the read again, which `05508_throwing_limit_swap` caught after the
merge: 6 index analysis rounds against 5 without automatic parallel replicas.
Pass `find_single_node_read` there as well, like the other join estimates.

Co-Authored-By: Claude Opus 5.5 <[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 comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... 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