Skip to content

Check LowCardinality indexes against the dictionary when reading data parts - #128476

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:lc-check-indexes-global-dictionary
Nov 1, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:lc-check-indexes-global-dictionary

Conversation

@groeneai

@groeneai groeneai commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #128366

Changelog category (leave one):

  • Improvement

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

Reading a LowCardinality column from a corrupted data part whose indexes point past the dictionary now throws INCORRECT_DATA instead of reading past the end of the keys, which could crash the server.

Description

Found by serialization_lowcardinality_fuzzer in the master NightlyFuzzers libFuzzer tests job: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=c189bd99fbda59c78489a6734c49397533bdd591&name_0=NightlyFuzzers&name_1=libFuzzer%20tests

SerializationLowCardinality reads the indexes of each granule from the stream, but only the branch without a global dictionary (the only one Native may use) checked them, via checkIndexesAreLimited. With a global dictionary the indexes were attached to it unchecked, and with additional keys an index past them went straight into additional_keys->index(...). On a data part whose index stream points past the keys (crafted or mis-written: random corruption fails the block checksum first), the 26.3, 26.8 and 26.9 releases crash with SIGSEGV (debug builds abort in PODArray or ColumnString).

Both global-dictionary branches now check the indexes right after reading them against the dictionary size plus the number of additional keys, reusing checkIndexesAreLimited (now a static member of ColumnLowCardinality), and throw INCORRECT_DATA. This also bounds the overflow map that mapIndexWithAdditionalKeys allocates. checkIndexesAreLimited now takes the maximum in the index's own type so the loop vectorizes (vpmaxub): a standalone loop over 8192 UInt8 indexes went from 1398 to 49 ns. Native input is not affected, it already rejects a global dictionary.

Validation: a new gtest round-trips LowCardinality(String) and LowCardinality(Nullable(UInt64)) with and without additional keys and rejects the first index past the keys and a maximal UInt64 index (both fail on master in all four cases). The fuzzer input now throws INCORRECT_DATA instead of aborting. Valid MergeTree parts of six LowCardinality column types, written with three dictionary sizes and with one or many dictionaries per part, read back identically through full scans, granule-skipping reads and merges, and the LowCardinality stateless tests pass.

🤖 Generated with Claude Code


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

Version info

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

groeneai and others added 2 commits November 1, 2026 11:31
… parts

`SerializationLowCardinality::deserializeBinaryBulkWithMultipleStreams` reads
the indexes of each granule from the stream, but only the branch without a
global dictionary (the only one `Native` may use, since 108137a)
checked them, through `checkIndexesAreLimited` in
`insertRangeFromDictionaryEncodedColumn`. With a global dictionary the
indexes were attached to it unchecked (`ColumnLowCardinality::create` +
`insertRangeFrom` on the shared dictionary copies them as they are), and with
additional keys `mapIndexWithAdditionalKeys` mapped every index past the
dictionary to `index - dict_size` and passed that to
`additional_keys->index(...)` without comparing it to the number of
additional keys. `serialization_lowcardinality_fuzzer` found the second case
(NightlyFuzzers, master c189bd9: an empty additional-keys column and
UInt8 indexes up to 255). On a MergeTree part whose index stream points past
the keys, 26.3, 26.8 and 26.9 release builds crash with SIGSEGV, and debug
builds abort in `PODArray` or `ColumnString`.

Check the indexes once, right after they are read, against the dictionary
size plus the number of additional keys of the granule, which is exactly the
range `uniqueInsertRangeWithOverflow` writes and `mapIndexWithAdditionalKeys`
already assumes. The check is done before `mapIndexWithAdditionalKeys`, so it
also bounds its overflow map, which was sized from the largest index in the
stream. `checkIndexesAreLimited` becomes a public static member of
`ColumnLowCardinality` so the deserializer reuses it, and it now takes the
maximum in the index's own type instead of `UInt64`: the loop then
vectorizes to `vpmaxub`/`umax`, which keeps the extra pass over the indexes
of a granule cheap (a standalone loop over 8192 UInt8 indexes: 1398 ns ->
49 ns at -march=x86-64-v3). Native input is unaffected.

The new gtest serializes `LowCardinality(String)` and
`LowCardinality(Nullable(UInt64))` with and without additional keys, checks
the round trip, and replaces the last index with the first value past the
keys, which must be rejected with INCORRECT_DATA.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`IndexPastTheKeysIsRejected` used only `UInt8` indexes and only the first
value past the keys, so it stayed green if the check were moved after
`mapIndexWithAdditionalKeys`, whose overflow map is sized from the largest
index. Each case now also rewrites its indexes as `UInt64`, checks that the
widened stream reads back the same values, and sets the last index to
`0xFFFFFFFFFFFFFFFF`, which must be rejected with INCORRECT_DATA. Without
the check that index fails with CANNOT_ALLOCATE_MEMORY (with additional
keys) or is accepted (without them).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 3 findings, 0 in the second) plus a separate cold review (0 blockers, 0 majors, 3 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ No test case reddens if the check moves after mapIndexWithAdditionalKeys, which sizes its overflow map by the largest index; the UInt64 index width is never exercised Agree, fixed The test now also widens the indexes to UInt64 (the widened stream reads back the same values) and rejects a maximal last index; it fails on master in all four cases and when the check runs after the overflow map is sized.
2 ⚠️ The extra pass over the indexes on the global-dictionary read path needs performance evidence Disagree Measured: a standalone loop over 8192 indexes at the default -march=x86-64-v3 takes 49 ns (UInt8), 95 ns (UInt16), 184 ns (UInt32) with the max kept in the index type, vs about 1400 ns before (vpmaxub instead of widening to 64 bits; umax on aarch64). The PR's performance comparison is the end-to-end check.
3 💡 Category should be Critical Bug Fix Disagree Only a crafted or mis-written part reaches this code: Native rejects a global dictionary, and random corruption fails the block checksum first. Kept Improvement, like #128366.
4 💡 With additional keys, the reused message calls the limit "Dictionary size" Noted, not blocking The existing message is pinned by 02010_lc_native; not reworded here.
5 💡 The test's comment on the maximal index names mapIndexWithAdditionalKeys, which only runs in the cases with additional keys Noted, not blocking The assertion holds in all four cases.

Severity: ❌ blocker / ⚠️ major / 💡 nit.

@groeneai

groeneai commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @KochetovNicolai, could you review this and add the can be tested label so CI runs? With a global dictionary, SerializationLowCardinality used the indexes read from a data part without checking them, so an index past the dictionary and the additional keys read out of bounds; it now checks them with the existing checkIndexesAreLimited and throws INCORRECT_DATA (found by serialization_lowcardinality_fuzzer).

@clickhouse-gh

clickhouse-gh Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e808c74]

Summary: ✅


AI Review

Summary

The PR validates streamed LowCardinality indexes against the global dictionary and additional keys before dictionary access or overflow-map allocation, and adds regression tests for valid streams, out-of-range indexes, and maximal UInt64 indexes. Reviewed head e808c74043f08b0075282e1914aa8d713489da7c; no actionable findings.

Missing context / blind spots
  • ⚠️ The new SerializationLowCardinality unit tests are still pending in CI. Fast test passed, but does not run these unit tests. No local build was available to independently reproduce the author's reported results; passing unit-test CI would close this verification gap.
Final Verdict

Status: ✅ Approve. No code changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 1, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 1, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). label Nov 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e808c7404 with master 2898c5432 (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.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 1, 2026
@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 1, 2026
Merged via the queue into ClickHouse:master with commit 118e960 Nov 1, 2026
354 of 355 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 1, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 1, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 1, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 2, 2026
Backport #128476 to 26.10: Check `LowCardinality` indexes against the dictionary when reading data parts
clickhouse-gh Bot added a commit that referenced this pull request Nov 2, 2026
Backport #128476 to 26.9: Check `LowCardinality` indexes against the dictionary when reading data parts
PedroTadim added a commit that referenced this pull request Nov 2, 2026
Backport #128476 to 26.8: Check `LowCardinality` indexes against the dictionary when reading data parts
PedroTadim added a commit that referenced this pull request Nov 2, 2026
Backport #128476 to 26.3: Check `LowCardinality` indexes against the dictionary when reading data parts
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 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 comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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