Repository navigation
Read LowCardinality dictionary keys in batches instead of allocating for the key count read from the stream - #128366
Conversation
`SerializationLowCardinality` read the dictionary size from the stream and
passed it as `limit` to the keys' `deserializeBinaryBulk`, which reserves
(`String`) or resizes (numbers, `UUID`, `IPv4`, `IPv6`) the column for `limit`
before reading. A malformed block with a huge key count (a 51-byte `Native`
input, or the serialization_lowcardinality_fuzzer crash input) made the
allocator throw `LOGICAL_ERROR` ("Too large size passed to allocator"), and a
count of 2^40 requested 16 TiB.
The single-dictionary prefix read, the shared dictionary read and the
additional keys read now use one helper that reads keys in batches of at most
`DEFAULT_BLOCK_SIZE` and throws `CANNOT_READ_ALL_DATA` when the stream ends
before the declared count. The check also ends the loop at end of stream,
and the nullable null map is sized by the keys actually read. A dictionary of
up to 65409 keys is read by one call with the same arguments as before.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the deserializeDictionaryKeys comment to one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus a separate cold review (0 blockers, 0 majors).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261031-111900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-7:20261031-103800 |
|
cc @CurtizJ @KochetovNicolai, could you review this and add the |
|
Workflow [PR], commit [a20ff86] Summary: ✅
AI ReviewSummaryThe PR reads Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 8 s compile time in total, 7 of them have a recent master baseline. |
47f313c
Related: #117385
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reading a malformed
LowCardinalitycolumn (for example fromNativeinput) whose dictionary declares more keys than the data contains now fails withCANNOT_READ_ALL_DATAinstead of aLOGICAL_ERRORor an attempt to allocate terabytes of memory.Description
Found by
serialization_lowcardinality_fuzzerin the master NightlyFuzzerslibFuzzer testsjob: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=9f79cc172306dbf177534ef194cafc034a598a33&name_0=NightlyFuzzers&name_1=libFuzzer%20testsSerializationLowCardinalityreads the dictionary size from the stream and passes it aslimitto the keys'deserializeBinaryBulk, which reserves (String) or resizes (numbers,UUID,IPv4,IPv6) the column forlimitbefore reading anything. A 51-byteNativeblock is enough:On 26.3 and 26.8 this throws
LOGICAL_ERROR(Too large size passed to allocator; debug and sanitizer builds abort), and with a key count of 2^40 it asks for a 16 TiB allocation.The three places that read a dictionary (the single-dictionary prefix read, the shared dictionary, the additional keys) now share one helper that reads keys in batches of at most
DEFAULT_BLOCK_SIZEand throwsCANNOT_READ_ALL_DATAwhen the stream ends before the declared count. The nullable null map is sized by the keys actually read. A dictionary of up to 65409 keys is still read by one call with the same arguments, so valid data is read exactly as before.The new test covers
StringandNullable(UInt64)keys and a 200000-key block that spans several batches. Locally, the wholeLowCardinalitytest family also passes with the batch size set to 3.Workflow [PR]
Sync PR [sync-upstream/pr/128366]
Version info
26.11.1.978-master(included in26.11and later)