Skip to content

Decode the ORC byte and boolean run-length streams in bulk - #130200

Merged
Algunenano merged 5 commits into
ClickHouse:masterfrom
groeneai:orc-bulk-byte-boolean-rle
Nov 17, 2026
Merged

Algunenano merged 5 commits into
ClickHouse:masterfrom
groeneai:orc-bulk-byte-boolean-rle

Conversation

@groeneai

@groeneai groeneai commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #127690
Related: #129345

Changelog category (leave one):

  • Performance Improvement

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

Faster reading of nullable, Bool and Int8 columns from ORC files: their run-length encoded streams are now decoded in bulk instead of one value at a time.

Description

The native ORC reader decodes booleans one value at a time: BooleanRleDecoder::next fetches each byte with a one-byte ByteRleDecoder::next call, and ByteRleDecoder::next with a null mask branches and calls readByte per value. The PRESENT stream of every nullable column, Bool data and Nullable(Int8) data go through them. They are the hot leaves of formats_columns_nullable #28 (sum(if(col_int8 IS NULL, 1, 0)) over ORC), which the arm performance comparison flags as a confirmed +40-47% regression on unrelated PRs (example job); in the CPU profiles of 9 such jobs the slow side spends 1.5-2.7x more samples in BooleanRleDecoder::next.

Now the boolean decoder decodes whole bytes in one call and spreads each to eight rows with shifts and masks, as the Parquet and Arrow IPC readers do (byteswapped: ORC stores the most significant bit first), and the masked paths of both decoders decode the non-null values without a mask and move them to their rows from the back, as FloatingColumnReader does since #129345. The stream consumption and the decoder state between calls do not change.

x86, max_threads = 1, this PR vs master, 8 interleaved rounds (95% CI within 2%): #28 -49% (44.6 -> 12.2 instructions per row), SELECT * -16%, avg(col_int32) -30%, sum over 10M Bool -77%, countIf(nb IS NULL) over 10M Nullable(Bool) -50%; no query is slower (measured with an earlier table-based expansion; the shift/mask one is not slower: #28 4.30 -> 4.00 ms). The arm numbers come from this PR's performance comparison.

05569_orc_byte_and_boolean_rle compares what is read back with the source for Bool, Int8, their Nullable forms, Array(Nullable(Bool)) and Nullable(Tuple(...)), at batch sizes 3 to 100000 and after row-group seeks into a partial byte. It passes on master, and three mutations of the new loops each fail it.


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

Version info

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

groeneai and others added 2 commits November 8, 2026 17:53
The native ORC reader decoded the boolean run-length streams one value at
a time: `BooleanRleDecoder::next` refilled its byte with a one-byte
`ByteRleDecoder::next` call (and a one-byte `readBytes`/`memcpy` for literal
runs) every 8 values and kept its bit state in members stored through
`char *`. The PRESENT stream of every nullable column goes through it, as
does the DATA stream of `Bool` columns. `ByteRleDecoder::next` with a null
mask (`Nullable(Int8)`, union tags) branched per position and called
`readByte` per value.

Both functions are the hot leaves of `formats_columns_nullable` query ClickHouse#28
(`sum(if(col_int8 IS NULL, 1, 0))` over ORC) and are where the slow
aarch64 builds lose 40% on it in the performance comparison
(`BooleanRleDecoder::next` x1.5-2.7 samples in 9 of 9 slow jobs).

Now the unmasked boolean path decodes whole bytes with one
`ByteRleDecoder::next` call per 1 KiB and expands them with a 256-entry
table, and both masked paths decode the non-null values with the unmasked
path and move them to their rows from the back, like `FloatingColumnReader`.
The stream consumption and the decoder state between calls are unchanged.

x86, RelWithDebInfo, max_threads = 1, 8 interleaved rounds of 20 runs,
mean of paired ratios (95% CI):
- ClickHouse#28 sum(if(col_int8 IS NULL)): 7.60 -> 3.90 ms, -48.8% (-49.7 .. -48.0)
- ClickHouse#4 SELECT *: 368.5 -> 309.9 ms, -16.2%
- ClickHouse#20 avg(col_int32): 8.10 -> 5.75 ms, -29.5%
- ClickHouse#36 groupArray(col_string): -9.2%; ClickHouse#60 toYear(col_datetime): -7.7%
- 10M rows sum(Bool): 16.85 -> 3.85 ms, -77.2%
- 10M rows countIf(Nullable(Bool) IS NULL): 45.95 -> 23.00 ms, -49.9%
ClickHouse#28: 44.6 -> 12.2 instructions per row, 12.3 -> 5.0 cycles per row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The seek arm compared results only, so if filter push-down stopped
skipping row groups it would silently become a full read and still pass.
`count()` with `indexHint(id >= 12345)` keeps all rows of the kept row
groups: 18011 with push-down (row groups 0..11 skipped), 30011 without.

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

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (Gate B) - 2 rounds, 1 finding, adjudicated
  • ⚠️ 05569 seek arm compared results only: AGREE, fixed in this PR (it now asserts that the row groups before the predicate are skipped: count() is 18011 with push-down, 30011 without).
  • ⚠️ "the test cannot detect a return to per-value decoding": DISAGREE, a stateless test checks results; speed is covered by tests/performance/formats_columns_nullable.xml and this PR's performance comparison.
  • 💡 countNotNull/moveToNotNullPositions repeat the 3-line idiom of FloatingColumnReader::next for char: kept, sharing it needs a header and an edit of the float reader.
  • 💡 IntRleDecoderV1/V2 keep their per-position masked loop: out of scope, no profile shows them hot.
  • Round 2 (the test-only fix): no findings.

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? The per-row cost is deterministic on x86: #28 takes 44.6 instructions per row on master and 12.2 with this PR (perf stat, 3 reps agree within 0.1%). The arm slow mode of the CI red is per binary and needs arm hardware; this PR's own arm performance comparison is the check for it.
b Root cause explained? BooleanRleDecoder::next decoded one value per iteration and fetched every byte with a one-byte ByteRleDecoder::next call; masked ByteRleDecoder::next branched and called readByte per value. These are the hot leaves of every nullable ORC read and the only frames that differ between slow and fast arm sides of #28 in 9 CI CPU profiles.
c Fix matches root cause? Yes: the two decoders now decode in bulk (whole bytes plus a 256-entry expansion table; masked paths decode the non-null values and move them from the back). No test or harness change.
d Test intent preserved / new tests added? The performance test is unchanged. New 05569_orc_byte_and_boolean_rle covers the unmasked and masked paths of both decoders, partial bytes and row-group seeks.
e Both directions demonstrated? Master vs this PR: #28 7.60 -> 3.90 ms, 10M Bool sum 16.85 -> 3.85 ms. The new test passes on both binaries (no behaviour change) and fails with each of three mutations: reversed bit order, a dropped partial byte, a missing move to the non-null rows.
f Fix is general across code paths? Every caller goes through the two changed functions: PRESENT of all columns, BOOLEAN/BYTE data, union tags, struct child masks, ColumnReader::skip. skip/seek and the integer RLE decoders are unchanged. 133 of 135 ORC-related stateless tests pass locally; the other 2 need the test.hits table or a pre-uploaded Paimon fixture.
g Fix generalizes across inputs (params/datatypes/wrappers)? Tested: Bool, Nullable(Bool), Int8 repeats and literals, Nullable(Int8), all-null, Array(Nullable(Bool)), Nullable(Tuple(Nullable(Bool), Nullable(Int8))), batch sizes 1/3/8/13/1000/100000, a row count that is not a multiple of 8, and seeks that land 5-6 bits into a byte.
h Backward compatible? (maintainer-approved exception only) Yes: only the decoding changes, with no setting and no format change.
i Invariants and contracts preserved? Each call consumes the same values from the stream and leaves the same decoder state, writes 0 at null positions, and data never aliases not_null. Truncated streams fail at the same missing value.

Session id: cron:clickhouse-impl-slot-7:20261108-170200

@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f3685d0]

Summary: ✅


AI Review

Summary

The PR decodes ORC byte and boolean streams in bulk while preserving null placement and decoder state between calls. No actionable findings remain at f3685d04ad398885e046228de3537fefb73fbc08. The supplied regression test and x86 measurements support the change; an independent check of the extracted decoder methods passed 26,982 calls and 61,395,331 scalar comparisons under ASan and UBSan.

Missing context / blind spots
  • ⚠️ This checkout has no ClickHouse binary, so the full ORC regression test was not run locally. The standalone check used a bounded stream substitute and an equivalent byte-swap builtin. Completion of the current-head functional and ARM performance CI jobs would close the remaining end-to-end validation gap.
Final Verdict

Status: ✅ Approve

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @Avogar, could you review this and add the can be tested label so CI runs? The native ORC reader decoded the boolean and masked byte RLE streams one value at a time; now whole bytes are decoded in one call and expanded with a 256-entry table, and masked reads decode the non-null values and move them to their rows from the back, as FloatingColumnReader does (formats_columns_nullable #28 is 49% faster on x86).

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Nov 8, 2026
@clickhouse-gh clickhouse-gh Bot added can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f3685d04a with master f6bdd226e (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

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

Job report

The flaky check runs the test with the thread fuzzer, which makes every
batch of the reader expensive. On `amd_binary`, `amd_debug` and
`amd_asan_ubsan` the test took 370-502 s (`Test runs too long (> 180s)`),
374-493 s of it in the reads of all 30011 rows with batch sizes 1, 3, 8,
13 and 1000. Locally with a debug build the batch size 1 read alone took
1201 s.

Now everything runs in one clickhouse-local, and the small batch sizes
3, 8 and 13 read only the row groups 12 and 13 (2000 rows) through the
filter push-down. Batch size 1 is dropped: 3 is coprime with 8, so it
already starts and ends calls at every bit offset, and batch size 1
never reaches the bulk decoding path. The whole-file reads with 1000 and
100000 rows per batch, the seek arm and the null counts are unchanged.

Measured with the thread fuzzer settings of the flaky check
(debug build, this box): 8 parallel runs 91-97 s each, previously
1201 s for the batch size 1 read alone; ASan single run 82 s. The three
mutation arms of the test (bit order reversed, tail forgets the partial
byte, masked byte decoder without the move) still fail it.

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

groeneai commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - c9ed2a0

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
AST fuzzer (amd_release, oracle) / AST fuzzer oracle mismatch wrong result, not from this PR (it changes only the ORC RLE.cpp decoder and its test): the Subquery wrap oracle changed the qualified join columns of SELECT a, t1.a, t2.a FROM t1 FULL OUTER JOIN t2 USING (a), the shape of issue #118986 #107127 (mine, open)
PR / Mergeable Check praktika rollups of the row above #107127 (the owner above)

Session id: cron:our-pr-ci-monitor:20261109-020005

@Algunenano Algunenano self-assigned this Nov 11, 2026
Comment thread src/Processors/Formats/Impl/ORC/RLE.cpp Resolved
The boolean RLE decoder expanded each byte to eight rows with a 2 KB
lookup table. Spread the bits with shifts and masks instead, as the
Parquet and Arrow IPC readers do, and byteswap on little-endian hosts
because ORC stores the most significant bit first.

clang-22 vectorizes the loop on both release targets:
- x86-64-v3: 4 bytes (32 rows) per iteration in ymm registers, the
  byteswap becomes vpshufb, no table loads.
- armv8.2-a+simd: 16 bytes (128 rows) per iteration, but the
  `x | (x << 7)` step is done in general registers (32 fmov/mov between
  NEON and GPRs per iteration).

In isolation the loop is slower than the table (x86 0.036 vs 0.022
ns/row measured; llvm-mca for Neoverse N1/V1/V2 gives about 2.2x the
cycles per row), but end to end the x86 release A/B against the table
version shows no regression: formats_columns_nullable ClickHouse#28 4.30 -> 4.00
ms, avg(col_int32) 6.28 -> 5.97 ms, the other ORC queries within noise.

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

@clickhouse-gh clickhouse-gh Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review approves this commit, see the AI Review comment.

@Algunenano
Algunenano enabled auto-merge November 16, 2026 21:56
@Algunenano
Algunenano added this pull request to the merge queue Nov 16, 2026
Merged via the queue into ClickHouse:master with commit 2b9a264 Nov 17, 2026
192 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 17, 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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). 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