Skip to content

Preserve dictionary identity through ColumnArray::cut - #115961

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
bobrik:ivan/array-join-preserve-identity
Aug 26, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
bobrik:ivan/array-join-preserve-identity

Conversation

@bobrik

@bobrik bobrik commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

This was originally a part of #114993, but it works as a standalone improvement just as well.

It already happens in ColumnLowCardinality::cut. Benchmark results:

  • ARRAY JOIN: 0.8623s -> 0.7042s (-18.3%)
  • Flat control: 0.5579s -> 0.5603s (+0.4%, noise)
  • Relative ARRAY JOIN overhead: 54.6% -> 25.7%
  • Absolute overhead reduced by 52.8%

Changelog category (leave one):

  • Performance Improvement

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

Speed up Array(LowCardinality(T)) aggregations by preserving dictionary identity.


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

Version info

  • Merged into: 26.9.1.187 (included in 26.9 and later)

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Aug 23, 2026
Comment thread src/Columns/ColumnArray.cpp Resolved

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

.

@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [aa3a595]

Summary: ✅


AI Review

Summary

This PR teaches ColumnLowCardinality::insertRangeFrom to reuse a structurally compatible shared dictionary when populating an empty destination, which lets ColumnArray::cut preserve dictionary identity and removes the remapping work behind the reported Array(LowCardinality(T)) aggregation slowdown. On the current head, the earlier bounds regression risk in ColumnIndex::insertIndexesRange is fixed with an explicit PARAMETER_OUT_OF_BOUND check and a focused regression test, and I did not find any remaining correctness or contract issues in the changed code.

Final Verdict

✅ No blocking findings on the current head.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.30% 87.30% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 63/64 (98.44%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this Aug 23, 2026
@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Aug 23, 2026
@bobrik
bobrik force-pushed the ivan/array-join-preserve-identity branch from 062f41d to 1a34752 Compare August 23, 2026 02:50
@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing aa3a595c4 with master b5409abf6 (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
Binary Master PR Δ
programs/clickhouse-stripped 710.13 MiB 707.08 MiB -3.04 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

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

Job report

@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

Release-transition baseline: compared against the last 26.8 master because no 26.9 master baseline exists at or before the trusted base-commit anchor.

no significant changes detected. K_source=6 K_base=2 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 6bb7faec-d9d0-4161-ad73-2640fcd10fa3
  • MIRAI run: 8a5c2560-6700-4693-9dcd-773f8b23904f
  • PR check IDs:
    • clickbench_402132_1787710267
    • clickbench_402138_1787710267
    • clickbench_402146_1787710267
    • tpch_adapted_1_official_402160_1787710267
    • tpch_adapted_1_official_402174_1787710267
    • tpch_adapted_1_official_402203_1787710268

Comment thread src/Columns/ColumnArray.cpp Resolved
@bobrik
bobrik force-pushed the ivan/array-join-preserve-identity branch from 00d8057 to a5ca6d4 Compare August 23, 2026 07:02
@bobrik

bobrik commented Aug 23, 2026

Copy link
Copy Markdown
Contributor Author

I pushed a fix for 03605_dynamic_to_nullable_low_cardinality_bug + 03777_join_runtime_filter_with_nulls.

pull Bot pushed a commit to AnotherGenZ/ClickHouse that referenced this pull request Aug 23, 2026
…cast

The test asserts that two queries publish a shared fixed-hash-table JOIN
runtime filter. Under randomized settings the assertion could see zero
instead: with `query_plan_optimize_join_order_randomize` on, the join order
optimizer works from random cardinalities and can put the small table on the
probe side, and with `query_plan_join_swap_table` pinned to `false` the sides
cannot be swapped back. The estimated probe size then falls below
`join_runtime_filter_min_probe_rows` and no runtime filter is created at all,
which is not what the test is about.

Set `join_runtime_filter_min_probe_rows` to 0 in the test so the filter is
always created regardless of the chosen plan shape. Verified locally over the
whole `query_plan_join_swap_table` x `query_plan_optimize_join_order_randomize`
matrix, including the exact settings from the failed run, plus 20 runs with
fully randomized settings.

Also fix the leftover `04613_` prefix in the `log_comment` values, which no
longer matches the test's own number.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115961&sha=1a3475209c5db937dae51192124325b1a22a17b3&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
Related: ClickHouse#115961

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Columns/ColumnLowCardinality.cpp Resolved
bobrik added 2 commits August 25, 2026 16:51
Baseline:

* Plain: 0.556s
* LowCardinality: 0.867s
It already happens in `ColumnLowCardinality::cut`. Benchmark results:

* ARRAY JOIN: 0.8623s -> 0.7042s (-18.3%)
* Flat control: 0.5579s -> 0.5603s (+0.4%, noise)
* Relative ARRAY JOIN overhead: 54.6% -> 25.7%
* Absolute overhead reduced by 52.8%
@bobrik
bobrik force-pushed the ivan/array-join-preserve-identity branch from e4d0901 to aa3a595 Compare August 26, 2026 00:10
@bobrik

bobrik commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

I fixed the newly flagged bot issue + rebased on top of the latest master.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 26, 2026
Merged via the queue into ClickHouse:master with commit 9d8eed3 Aug 26, 2026
178 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 26, 2026
bobrik added a commit to bobrik/ClickHouse that referenced this pull request Aug 27, 2026
Shared dictionaries from Native input can contain signed zero and multiple
NaN payloads. Avoid sharing floating-point dictionaries so `ColumnUnique`
canonicalizes these values during different-dictionary insertion.

This is a regression from: ClickHouse#115961
@bobrik

bobrik commented Aug 27, 2026

Copy link
Copy Markdown
Contributor Author

While rebasing #114870, I noticed that I forgot about float weirdness. Here's the fix: #116637.

bobrik added a commit to bobrik/ClickHouse that referenced this pull request Sep 6, 2026
Shared dictionaries from Native input can contain signed zero and multiple
NaN payloads. Avoid sharing floating-point dictionaries so `ColumnUnique`
canonicalizes these values during different-dictionary insertion.

This is a regression from: ClickHouse#115961
bobrik added a commit to bobrik/ClickHouse that referenced this pull request Sep 6, 2026
Shared dictionaries reconstructed from on-disk `LowCardinality` dictionary
streams can contain signed zero and multiple NaN payloads.  Avoid sharing
floating-point dictionaries so `ColumnUnique` canonicalizes these values
during different-dictionary insertion.

This is a regression from: ClickHouse#115961
bobrik added a commit to bobrik/ClickHouse that referenced this pull request Sep 6, 2026
Shared dictionaries reconstructed from on-disk `LowCardinality` dictionary
streams can contain signed zero and multiple NaN payloads.  Avoid sharing
floating-point dictionaries so `ColumnUnique` canonicalizes these values
during different-dictionary insertion.

This is a regression from: ClickHouse#115961
bobrik added a commit to bobrik/ClickHouse that referenced this pull request Sep 13, 2026
Shared dictionaries reconstructed from on-disk `LowCardinality` dictionary
streams can contain signed zero and multiple NaN payloads.  Avoid sharing
floating-point dictionaries so `ColumnUnique` canonicalizes these values
during different-dictionary insertion.

This is a regression from: ClickHouse#115961
bobrik added a commit to bobrik/ClickHouse that referenced this pull request Sep 13, 2026
Shared dictionaries reconstructed from on-disk `LowCardinality` dictionary
streams can contain signed zero and multiple NaN payloads.  Avoid sharing
floating-point dictionaries so `ColumnUnique` canonicalizes these values
during different-dictionary insertion.

This is a regression from: ClickHouse#115961
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 7, 2026
Since ClickHouse#107189 the hash join emits consecutive same-block rows of a key with
one `insertRangeFrom` instead of one `insertFrom` per row. `RowRefList` keeps
a key's rows in nodes of up to 6 refs, so these runs are 2-6 rows long. When
the source block's dictionary differs from the output column's,
`ColumnLowCardinality::insertRangeFrom` builds about five temporary columns
per call whatever the length (`cut`, `mapUniqueIndex`, `index` of the used
keys, `uniqueInsertRangeFrom`, `index` of the positions), so such a run costs
~25x per row what `insertFrom` costs. ClickHouse#115961 avoids it only when the right
side has a single shared dictionary; several parts, `FINAL` or a
`LowCardinality` value computed per block still hit it on every run.

Ranges of at most 32 rows from a different dictionary are now translated row
by row with the per-key rules of `uniqueInsertRangeImpl`: the source NULL maps
to the destination NULL, a key that compares equal to the default (so `-0.0`
too) maps to the default slot, anything else goes through `uniqueInsertFrom`,
which canonicalizes NaN. Positions are computed before the indexes change and
the range is bounds-checked first, so a throw leaves the rows untouched, as on
the range path.

The threshold is the largest power of two at which the per-row path is not
slower than the range path in a microbenchmark of ns per call (RelWithDebInfo,
one core, median of 7): range/per-row at 32 rows is 1.99x for 7 short keys,
2.52x for 10000 random keys, 1.48x for two 200-byte keys and 3.61x for 1000
UInt64 keys; at 64 the 200-byte case drops to 0.87x.

A/B, RelWithDebInfo, n = 10 interleaved, medians (26.6.8.7 / parent / this):
issue repro 4 with `compatibility = '23.8'` 2.31 / 8.93 / 2.30 s; repro 1 with
the table in 4 parts 1.33 / 5.12 / 0.79 s, at default settings 1.00 / 4.40 /
0.50 s; a table-free `hash` join 0.18 / 0.75 / 0.15 s. Shared-dictionary and
`String` controls unchanged. Repro 4 at default settings does not reach this
path (the join sides are swapped and the time is in the row-store emit).

The gtest compares a short range against a 600-row range of the same rows for
every length 1-256; it fails when the default fold, the bounds check or the
NULL branch is removed.

Closes: ClickHouse#123874

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 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