Skip to content

Move UNIQUE KEY comparable serialization into IColumn - #109010

Merged
murphy-4o merged 19 commits into
ClickHouse:masterfrom
JingYanchao:johnjing/optimize-unique-key-serialization
Jul 29, 2026
Merged

murphy-4o merged 19 commits into
ClickHouse:masterfrom
JingYanchao:johnjing/optimize-unique-key-serialization

Conversation

@JingYanchao

@JingYanchao JingYanchao commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Not for changelog

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

Move UNIQUE KEY byte-comparable serialization from a centralized type-switch into per-column virtual methods on IColumn.

Descrption

The previous implementation in UniqueKeyEncoding.cpp placed all type-specific encoding logic in a single monolithic switch, requiring modifications to this central file for every new type.

This refactoring moves the encoding logic into the column classes themselves as virtual methods (serializeValueIntoMemoryAsComparable,batchSerializeComparableIntoMemory), following the same pattern as serializeValueIntoArena. Adding support for a new column type now only requires overriding these methods in the column class — no central dispatch table to maintain.

The encoding algorithm for every supported type is unchanged — same byte layout, same ordering semantics, fully wire-compatible.

Memory pre-allocation can be added in a follow-up if profiling shows the per-row append overhead is worth it.

Version info

  • Merged into: 26.8.1.334 (included in 26.8 and later)

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [aad58f1]

Summary: ✅


AI Review

Summary

This PR moves UNIQUE KEY comparable serialization out of UniqueKeyEncoding.cpp and into per-column IColumn virtual methods, with new batch overrides for the supported column families plus regression coverage for Nullable, ColumnConst, and wide numeric cases. The refactor itself looks consistent at the current head, but one previously dismissed correctness hole is still present in SSTIndexWriter::writeFromBlockUnsorted: malformed caller permutations can still produce out-of-bounds writes or wrong SST row numbers.

Missing context / blind spots
  • ⚠️ This change touches the SST-build hot path, but the performance-comparison shards were skipped because the PR is not labeled pr-performance, so I could only validate the refactor structurally, not from before/after measurements.
Findings
⚠️ Majors
  • [src/Storages/MergeTree/UniqueKey/SSTIndexWriter.cpp:475] [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/109010#discussion_r3614642857] writeFromBlockUnsorted still only checks permutation->size(). The inversion loop at lines 477-480 then trusts every entry to be an in-range bijection, so an out-of-range value writes past source_to_part_offset, and a duplicate value silently loses one source row's part_offset. That can still store wrong row numbers in the SST or trigger UB if a future caller hands this helper anything other than a true permutation.
    Suggested fix: validate permutation as a bijection before the inversion loop (same size, every entry < num_rows, no duplicates) and add a focused regression around malformed caller permutations.
Final Verdict

Request changes.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 2, 2026
Comment thread src/Columns/ColumnString.cpp Resolved
Comment thread src/Columns/ColumnNullable.cpp Resolved
@murphy-4o murphy-4o self-assigned this Jul 2, 2026
Comment thread src/Columns/IColumn.h Resolved
Comment thread src/Columns/IColumn.h Resolved
Comment thread src/Columns/ColumnVector.cpp Resolved
@JingYanchao
JingYanchao force-pushed the johnjing/optimize-unique-key-serialization branch from 3c4eb27 to 728e29a Compare July 9, 2026 08:47
Comment thread src/Columns/ColumnNullable.cpp Resolved
Comment thread src/Columns/ColumnDecimal.h Resolved
Comment thread src/Columns/ColumnNullable.cpp Resolved
Comment thread src/Columns/ColumnVector.cpp Resolved
Comment thread src/Columns/ColumnNullable.cpp Resolved
Comment thread src/Columns/ColumnDecimal.cpp Resolved
@JingYanchao
JingYanchao force-pushed the johnjing/optimize-unique-key-serialization branch from 293cbd1 to b4bb38d Compare July 17, 2026 03:31
Comment thread src/Columns/ColumnNullable.cpp Resolved
Comment thread src/Columns/ColumnNullable.cpp Resolved
Comment thread src/Columns/IColumn.h Resolved
Comment thread src/Columns/ColumnNullable.cpp Resolved
Comment thread src/Columns/ColumnConst.h Resolved
Comment thread src/Columns/IColumn.h Resolved
Comment thread src/Columns/ColumnString.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Jul 22, 2026
@JingYanchao
JingYanchao force-pushed the johnjing/optimize-unique-key-serialization branch from b6973c3 to a5fb6ff Compare July 27, 2026 06:07
Comment thread src/Columns/IColumn.h Resolved
@JingYanchao
JingYanchao requested a review from murphy-4o July 27, 2026 08:51
@JingYanchao
JingYanchao force-pushed the johnjing/optimize-unique-key-serialization branch from 7d34b10 to d12a034 Compare July 28, 2026 12:35
Comment thread src/Columns/IColumn.h Resolved
Comment thread src/Columns/IColumn.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.30% +0.00%
Functions 92.10% 92.00% -0.10%
Branches 78.50% 78.40% -0.10%

Changed lines: Changed C/C++ lines covered: 329/372 (88.44%) · Uncovered code

Full report · Diff report

@JingYanchao

Copy link
Copy Markdown
Contributor Author

@murphy-4o Could you please review it again? Thanks!

@murphy-4o
murphy-4o added this pull request to the merge queue Jul 29, 2026
Merged via the queue into ClickHouse:master with commit 1e31a05 Jul 29, 2026
353 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 29, 2026
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-improvement Pull request with some product 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.

4 participants