Repository navigation
Unpack bit-packed Parquet dictionary indexes and levels with a loop specialized for each bit width - #128262
Conversation
…per bit width `BitPackedRLEDecoder` unpacked bit-packed runs one value at a time with a run-time bit width, and kept the bit cursor in a member. The load goes through `const char *`, which may alias `*this`, so the compiler stored the cursor to memory for every value, and no shift or byte offset was constant. Add `unpackBitPacked<W, T>`, instantiated for each width (1..32 for indexes, 1..8 for levels) and chosen once per page from a table. It unpacks each byte-aligned block of 8 values with constant offsets and shifts and keeps the cursor in a register. It reads 8 bytes at the first byte of each value, exactly the bytes the per-value loop read, so the padding requirement is unchanged. The bounds check becomes a separate max-reduction over the unpacked batch. The filtered decode path is unchanged. Used by `decodeAndIndex` (fused decode and gather of dictionary indexes) and by the unfiltered `skipOrDecode` (indexes, definition/repetition levels, RLE booleans). Analysis and patch by @ snelheidai in ClickHouse#128232. New test 05482 reads dictionary indexes of every width from 1 to 18 bits, a String dictionary, and definition/repetition levels of widths 1 and 2, with default blocks, 777-row blocks (runs split at bit positions that are not byte aligned) and PREWHERE, against the source data, and checks that a definition level out of range in a bit-packed run is rejected. 05320 also reads its corrupt file without PREWHERE, which covers the bounds check of the fused decode-and-gather path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Make the unpackBitPacked read-footprint comment a standalone invariant. 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; 2 findings; 1 full pass).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261030-162900 |
|
cc @al13n321 @grantholly-clickhouse, could you review this and add the |
|
Workflow [PR], commit [9d96f32] Summary: ✅
AI ReviewSummaryThe PR specializes bit-packed Parquet dictionary-index and repetition/definition-level decoding by bit width while preserving bounds checks and the existing read footprint. Reviewed commit Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+28.19 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline. |
|
For the "whichever is merged second applies this unpacking to the other's loop" step, here is that change for #124859's --- a/src/Processors/Formats/Impl/Parquet/Decoding.cpp
+++ b/src/Processors/Formats/Impl/Parquet/Decoding.cpp
@@ -293,7 +293,6 @@ struct BitPackedRLEDecoder : public PageDecoder
chassert(to_span.size() == value_size * num_values);
char * to = to_span.data();
const char * dict = dictionary.data.data();
- const T value_mask = T((1ul << bit_width) - 1);
const size_t width = bit_width;
const size_t max_index = limit - 1;
@@ -315,22 +314,25 @@ struct BitPackedRLEDecoder : public PageDecoder
}
else
{
- /// A corrupt index is clamped so that the gather stays inside the dictionary; the run is rejected below.
- const char * run_data = data;
- size_t pos = bit_idx;
- size_t max_seen = 0;
- for (size_t i = 0; i < n; ++i)
+ /// Unpack a batch of indexes with the loop specialized for the bit width, bounds-check the batch,
+ /// then gather it.
+ for (size_t done = 0; done < n;)
{
- size_t x = 0;
- memcpy(&x, run_data + (pos >> 3), 8);
- x = (x >> (pos & 7)) & value_mask;
- max_seen = std::max(max_seen, x);
- memcpy(to + i * value_size, dict + std::min(x, max_index) * value_size, value_size);
- pos += width;
+ constexpr size_t batch = 256;
+ T buf[batch];
+ const size_t m = std::min(n - done, batch);
+ unpack(data, bit_idx, m, buf);
+ bit_idx += m * width;
+ T max_seen = 0;
+ for (size_t i = 0; i < m; ++i)
+ max_seen = std::max(max_seen, buf[i]);
+ if (max_seen > max_index)
+ throw Exception(ErrorCodes::INCORRECT_DATA, "Dict index or rep/def level out of bounds (bp)");
+ char * dst = to + done * value_size;
+ for (size_t i = 0; i < m; ++i)
+ memcpy(dst + i * value_size, dict + size_t(buf[i]) * value_size, value_size);
+ done += m;
}
- bit_idx = pos;
- if (max_seen >= limit)
- throw Exception(ErrorCodes::INCORRECT_DATA, "Dict index or rep/def level out of bounds (bp)");
if (!run_length)
data += run_bytes;Time per query, geometric mean of new/old with 95% CI (ClickBench
Q1, Q2, Q8, Q10 and Q21 were unchanged (intervals contain 1). Q11 was 1.015 [1.006, 1.025] over 8 pairs and 0.996 [0.981, 1.011] in a 12-pair rerun. Both builds were master e4afc4f with both PRs and other open Parquet reader and aggregation PRs applied, so absolute times differ from master; the builds differ only by this diff. Query results were identical, and 05339 (including its corrupt-index cases), 05482 and 05320 pass. |
|
@groeneai, check the comment above. |
|
Thanks, I checked it. The diff changes |
b8bb109
…dSize Use the bit-width-specialized `unpack` from ClickHouse#128262 for the bit-packed runs of the fixed-size dictionary path: unpack up to 256 indexes into a stack buffer, check the batch maximum against the dictionary size, then gather. The check now precedes the gather, so the per-value clamp is no longer needed. Patch proposed by snelheidai in ClickHouse#128262 (comment). The `Decoding.h` comment is restored to the master wording, which describes both paths again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #128232
Related: #124859
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster reading of dictionary-encoded,
Nullableand nested columns from Parquet: bit-packed dictionary indexes and definition/repetition levels are now unpacked by a loop specialized for each bit width. ClickBench queries overhits.parquetsuch asSELECT AVG(UserID) FROM hitsare 8-15% faster.Description
The native Parquet reader unpacked bit-packed runs of dictionary indexes and definition/repetition levels one value at a time, with the bit width known only at run time and the bit position stored to memory after every value (the load through
const char *may alias the decoder). NowunpackBitPacked<W>is instantiated for each width (1-32 for indexes, 1-8 for levels) and chosen once per page: it unpacks each byte-aligned block of 8 values with constant offsets and shifts, keeps the position in a register, and the bounds check is a separate loop over the batch. It reads exactly the bytes the per-value loop read, so the padding requirement is unchanged. The filtered decode path is unchanged.Measured locally (x86 Xeon 6975P-C, Release, one
clickhouse localprocess per run, 4 threads, paired runs, geometric mean of new/old with 95% CI):SUM(AdvEngineID), COUNT(*), AVG(ResolutionWidth): 0.850 (0.817-0.885);AVG(UserID): 0.924 (0.918-0.930);SUM(ResolutionWidth + k): 0.854 (0.831-0.877); with 1 thread 0.79 / 0.92 / 0.79.COUNT(*)andURL LIKE '%google%': unchanged (0.975 and 0.999, intervals contain 1).Nullablecolumns: dictionary-encoded 0.772, plain (levels only) 0.953.The analysis and patch are by @ snelheidai in #128232, measured at 0.905-0.910 on Graviton3 for the same three queries.
Correctness: hashes of all 105
hits.parquetcolumns are identical. The new test reads index widths 1-18 and level widths 1 and 2, with blocks that start inside a run and under PREWHERE, and checks that an out-of-range level is rejected; widths 19-32 were compared exhaustively against the old loop locally.Arrow's
unpack, used by the ALP decoder, is not reused because #126477 makes the native reader independent of Arrow. #124859 changes the same function; whichever is merged second applies this unpacking to the other's loop.Workflow [PR]
Sync PR [sync-upstream/pr/128262]
Version info
26.11.1.1089-master(included in26.11and later)