Skip to content

Replace the apache/orc library with a native ORC reader and writer - #127690

Merged
alexey-milovidov merged 21 commits into
masterfrom
native-orc
Nov 1, 2026
Merged

alexey-milovidov merged 21 commits into
masterfrom
native-orc

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

The ORC format no longer uses the apache/orc C++ library. The reader and the writer are implemented in src/Processors/Formats/Impl/ORC, similarly to the native Parquet and Arrow IPC implementations, and the contrib/orc submodule is removed.

What liborc was used for

  • NativeORCBlockInputFormat: file tail and stripe parsing, all column encodings (RLE v1/v2, byte/boolean RLE, direct and dictionary strings, decimals, timestamps, nested types and unions), lazy dictionary decoding for LowCardinality, stripe prefetching, the writer/reader time zone adjustment of TIMESTAMP, and predicate pushdown by file, stripe and row group statistics and bloom filters.
  • ORCBlockOutputFormat: the encodings, the row index, the statistics, five codecs, and the type attributes for the Iceberg field ids.
  • HiveFile: the file and stripe statistics for the min/max indexes.

Approach

The new code follows the semantics of liborc, so the behavior is kept:

  • The values are decoded directly into ClickHouse columns of their natural types (Int8...Int64, Float32, Float64, Bool, String), the lengths of lists and maps directly into the offsets of ColumnArray, and the string dictionaries into ColumnString, which is used as is for LowCardinality. The type conversion code in NativeORCBlockInputFormat.cpp (type hints, unions, overflow checks) is kept and takes these columns without copying. DATE, TIMESTAMP and DECIMAL keep intermediate values, which are needed for the range and scale checks.
  • The writer encodes directly from the ClickHouse columns, with the same value conversions as before (unsigned integers as signed ones of the same width, DateTime64 as seconds and nanoseconds, FixedString, IPv6 and big integers as raw bytes, Variant as UNION).
  • Column selection, search arguments (with the same normalization: push down NOT, fold MAYBE, CNF) and their evaluation against the statistics and bloom filters are the same, so rows_read with filter pushdown is the same.
  • The file layout, statistics and time zone handling on write are the same as in liborc's C++ writer.
  • The metadata is parsed and serialized with a small hand-written Protocol Buffers implementation, so the build does not need protoc for ORC anymore.

Codecs: none, zlib, snappy, LZ4, zstd for reading and writing; LZO for reading only (as before). BROTLI is rejected with NOT_IMPLEMENTED, as before.

Compatibility checks

  • The files written by the new and the old writer read identically by the new and the old reader, for all codecs and 26 column types (including unsigned integers, IPv4, IPv6, Int128, FixedString, DateTime, nullable tuples with nullable fields, Variant, LowCardinality(Nullable), nested arrays).
  • Files written by PyArrow 26 (dictionary encoding, row index, timestamps before the epoch with negative nanoseconds) read the same as with liborc, and PyArrow reads the files of the new writer correctly.
  • With a non-GMT writer time zone, all combinations of writer, reader and reader time zone agree.
  • 400 runs of the reader on randomly corrupted files: no crashes or logical errors (release build).

Performance

20M rows of mixed types, zstd, best of 5 (on a loaded machine, so the ratios matter more than the absolute numbers):

native liborc
Full scan 3.7 s 8.6 s
Two columns 0.62 s 0.87 s
Integer columns 0.25 s 0.46 s
String columns 0.62 s 1.05 s
Write 3.1 s 4.0 s

Peak memory is somewhat higher than with liborc (about 50 MB for reading, because the selected columns of a stripe are loaded at once; about 15% for writing, because the strings of a stripe are buffered). The files are 0.3% larger with zstd; with LZ4 they are half the size, because liborc used the maximum LZ4 acceleration.

Bug fix

Date values after 2059-09-18 were written as negative numbers of days (as the signed 16-bit numbers that the unsigned integers are written as), so they read back as dates before 1970, e.g. 2100-01-01 as 1920-07-28, and their statistics were wrong as well. liborc had the same behavior. They are now written correctly (test 05448_orc_date_after_2059).

Test changes

  • 01308_orc_output_format_arrays and 02426_orc_bug compare MD5 of the output bytes. They differ only in the metadata: the software version and the collection statistics, which liborc filled only when a batch had nulls and are now always correct.
  • 04856, 05036, 05317 matched liborc's error messages or exception class names and now match the new messages.
  • Added 05447_orc_native_reader_writer.
  • The Hive build was conditioned on the _orc target; it now requires snappy and Parquet.

Changelog category (leave one):

  • Improvement

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

The ORC format is now read and written by a native implementation instead of the Apache ORC C++ library, with the same behavior and the same files on output. The values are read and written directly into and from ClickHouse columns: reading is 1.4 to 2.3 times faster and writing is about 25% faster. The build does not need protoc for ORC anymore. Fixed writing Date values after 2059-09-18 to ORC, which were read back as dates before 1970.

🤖 Generated with Claude Code


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

Version info

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

alexey-milovidov and others added 2 commits October 24, 2026 05:50
The ORC reader and writer are implemented in `src/Processors/Formats/Impl/ORC`,
similarly to the native Parquet and Arrow IPC implementations. They follow the
semantics of the C++ ORC library that was used before, so the existing behavior
is kept: the same type mapping (the conversion code is unchanged), the same
column selection, the same predicate pushdown (search arguments with the same
normalization, file, stripe and row group statistics, bloom filters), the same
time zone handling of `TIMESTAMP`, and the same file layout and statistics on
write (the files are read identically by the old reader, and the old files by
the new one).

The metadata is parsed and serialized with a small hand-written Protocol
Buffers implementation, so the build does not run `protoc` for ORC anymore.
All codecs are supported for reading (including LZO) and all codecs except LZO
and BROTLI for writing, as before. The `contrib/orc` submodule is removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflict on `contrib/orc` in favor of its deletion. The new test
`05407_orc_timezone_traversal_abort` checked the error message of the ORC
library; the native reader resolves the time zones only from the embedded time
zone database, so it now checks that such a time zone is rejected.

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

clickhouse-gh Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f0e7321]

Summary: ✅


AI Review

Summary

The PR replaces Apache ORC with native ClickHouse reading and writing. Review of f0e73213e1b2064a02bee838f06e3d34fc28dc43 found no new actionable issues. All nine previous findings are addressed in the current code.

Missing context / blind spots
  • ⚠️ Final-head CI is still in progress. Local tests 05447_orc_native_reader_writer, 05448_orc_date_after_2059, and 05473_orc_footer_content_length passed against the available 9fa26644803a PR binary. The subsequent decimal64-v2 change and its regression test were reviewed statically; completed final-head CI would close this verification gap.
Final Verdict

✅ Approve — no unresolved review findings.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements submodule changed At least one submodule changed in this PR. comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Oct 24, 2026
alexey-milovidov and others added 5 commits October 24, 2026 07:20
The reader decodes the values of the primitive types directly into the
ClickHouse columns of their natural types (`Int8`, `Int16`, `Int32`, `Int64`,
`Float32`, `Float64`, `Bool`, `String`), the lengths of lists and maps into the
offsets of `ColumnArray`, and the string dictionaries into `ColumnString`, which
is used as is for `LowCardinality`. The conversion to the requested types takes
these columns without copying. `DATE`, `TIMESTAMP` and `DECIMAL` keep their
intermediate values, which are needed to check the ranges and the scales.

The writer encodes the values directly from the ClickHouse columns, with the
same conversions as before (the unsigned integers as the signed ones of the
same width, `DateTime64` as seconds and nanoseconds, `FixedString`, `IPv6` and
the big integers as their raw bytes, `Variant` as UNION), instead of copying
them into the intermediate batches first. The output is byte-identical.

On 20 million rows of mixed types, reading is 1.2 to 2 times faster than with
the intermediate batches (depending on the columns), and writing is 10% faster,
with less memory in both cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Date` is the unsigned number of days, but it was written to ORC as a signed
16-bit number, as the unsigned integer types are, so the dates after
2059-09-18 (more than 32767 days) were written as negative numbers of days and
read back as the dates before 1970: for example, 2100-01-01 as 1920-07-28. The
statistics were wrong in the same way, so the filter pushdown could skip the
matching rows. The C++ ORC library had the same behavior.

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

clickhouse-gh Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f0e73213e with master 4c45be687 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

⚠️ Significant changes: object file sizes.

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.

Object file sizes ⚠️

7 object files changed (+1.64 MiB total), 11 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Writer.cpp.o new 338.84 KiB +338.84 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Reader.cpp.o new 271.04 KiB +271.04 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/ColumnReader.cpp.o new 260.90 KiB +260.90 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/SearchArgument.cpp.o new 184.77 KiB +184.77 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Proto.cpp.o new 181.77 KiB +181.77 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Type.cpp.o new 108.12 KiB +108.12 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/RLE.cpp.o new 90.38 KiB +90.38 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Vector.cpp.o new 82.11 KiB +82.11 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Stream.cpp.o new 81.08 KiB +81.08 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Compression.cpp.o new 75.83 KiB +75.83 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORC/Statistics.cpp.o new 44.59 KiB +44.59 KiB
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ORCBlockOutputFormat.cpp.o 256.33 KiB 230.43 KiB -25.90 KiB (-10.10%)
src/Storages/System/CMakeFiles/clickhouse_storages_system.dir/StorageSystemLicenses.generated.cpp.o 2.10 MiB 2.09 MiB -16.87 KiB (-0.78%)

766 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Median compile-time ratio to the baselines is ×1.07 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +4.6 s (+8%) in total before that adjustment.

Job report

alexey-milovidov and others added 2 commits October 26, 2026 14:02
…nts, allow nested dictionaries

Remove the bound of a string dictionary by the number of rows of the stripe, because a column nested in a list or a map
can have more values. Validate the precision and the scale of the decimal type and the per-value scales from the file,
so the differences of the scales cannot overflow. Reject bloom filters with more than 256 hash functions.

Fix the tests: `05447_orc_native_reader_writer` printed timestamps in the random session time zone, and
`04337_orc_schema_inference_deep_recursion` failed under TSan where the stack size check fires before the depth limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment thread src/Storages/Hive/HiveFile.cpp Resolved
Comment thread src/Storages/Hive/HiveFile.cpp Resolved
alexey-milovidov and others added 2 commits October 27, 2026 12:44
… ORC statistics conservative

`Reader::preBuffer` scheduled the asynchronous read before the cache owned
the buffer, so an exception from `cache.push_back` could free the buffer
while the background read was still writing into it. Insert the entry into
the cache first, and remove it only if scheduling the read fails.

In `HiveORCFile::buildRange`, a missing `has_null` now means that the column
may contain nulls, and the deprecated local-time timestamp bounds are no
longer used for the file index, because the writer time zone is unknown at
the file level. This matches the behavior of the replaced library.

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

Copy link
Copy Markdown
Member Author

🕵 Merged master and addressed the three review threads in 4c88afb. The new tests 05447_orc_native_reader_writer and 05448_orc_date_after_2059 pass locally.

The remaining CI failures are performance regressions in the native reader. They are caused by this PR and are not fixed yet:

Locally, subfield pruning does work: Tuple(c) takes 0.042 s, the full tuple takes 0.095 s. So the slowdown is likely in the per-batch decoding and conversion of nested/nullable columns, not in reading extra streams. This needs profiling before merge.

alexey-milovidov and others added 4 commits October 28, 2026 08:32
…th incorrect hashing

The C++ writer of ORC 1.6.0 - 1.6.11 and 1.7.0 hashed the integers incorrectly (ORC-1024),
and liborc did not use such bloom filters (`hasBadBloomFilters`). The native reader used them,
so the equality and `IN` pushdown could skip row groups with matching rows. Restore the same
exclusion by the writer id and `software_version`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The files `bad_bloom_filter_1.6.0.orc` and `bad_bloom_filter_1.6.11.orc` are from
https://github.com/apache/orc/tree/main/examples. Without ignoring their bloom filters,
`id = 18000000000` and `id IN (...)` return no rows with `input_format_orc_filter_push_down`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`IntRleDecoderV2::readInts` read every byte with `readByte`, which checks the end of the
chunk each time. It took 60% of the time of reading wide integer columns, so the native
reader was slower than liborc in `orc_tuple_field_prune` (up to 2x locally). Decode the
values that are in the current chunk without the checks, with the byte width known at
compile time.

Locally: `orc_tuple_field_prune` queries 26 -> 11 ms, 40 -> 39 ms, 83 -> 44 ms
(liborc: 12, 47, 70 ms).

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

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127690&sha=4c88afbedf85e1d51a153ceed0845f338d34463f&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%207%2F8%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The server of test_storage_kafka/test_batch_fast.py stopped on a ThreadSanitizer data race inside librdkafka (rd_kafka_toppar_set_fetch_state at rdkafka_partition.c:387 vs rd_kafka_toppar_fetch_backoff at rdkafka_fetcher.c:86), so every later test in the module failed. This is unrelated to ORC.

The performance regressions in orc_tuple_field_prune and probably formats_columns_sampling (ORC) were caused by IntRleDecoderV2::readInts reading byte by byte. 15ebe49 fixes that (locally the orc_tuple_field_prune queries take 11, 39 and 44 ms; liborc takes 12, 47 and 70 ms). orc_filter_push_down #1 is about 9 ms with both readers locally. has_any_constant_string_needles does not use ORC.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is already in progress: ClickHouse/librdkafka#24 (base ClickHouse/v2.15.1, the branch contrib/librdkafka is pinned to). It is open and mergeable, and needs a review.

rd_kafka_toppar_fetch_backoff writes rktp_ts_fetch_backoff on the broker thread without the toppar lock (here from rd_kafka_fetch_reply_handle_partition_error), while rd_kafka_toppar_set_fetch_state writes it under the lock on the main thread. The patch takes the lock in rd_kafka_toppar_fetch_backoff; the two calls in rd_kafka_toppar_fetch_decide, which already hold it, use a lock-held variant. Once it and ClickHouse/librdkafka#23 (another race in the same Kafka TSan jobs) merge, I'll send the ClickHouse PR that bumps the submodule.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 6, 2026
@robot-ch-test-poll robot-ch-test-poll removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 6, 2026
robot-ch-test-poll added a commit that referenced this pull request Nov 6, 2026
Cherry pick #127690 to 26.3: Replace the apache/orc library with a native ORC reader and writer
robot-clickhouse added a commit that referenced this pull request Nov 6, 2026
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 6, 2026
Backport #127690 to 26.10: Replace the apache/orc library with a native ORC reader and writer
clickhouse-gh Bot added a commit that referenced this pull request Nov 8, 2026
Backport #127690 to 26.8: Replace the apache/orc library with a native ORC reader and writer
clickhouse-gh Bot added a commit that referenced this pull request Nov 8, 2026
Backport #127690 to 26.9: Replace the apache/orc library with a native ORC reader and writer
Algunenano added a commit that referenced this pull request Nov 9, 2026
Backport #127690 to 26.3: Replace the apache/orc library with a native ORC reader and writer
@watson-ch watson-ch Bot added v26.4-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Nov 12, 2026
@watson-ch

watson-ch Bot commented Nov 12, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/1344

Labels added:

  • v26.4-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 12, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 12, 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
leshikus pushed a commit that referenced this pull request Nov 13, 2026
@watson-ch watson-ch Bot added v26.2-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Nov 16, 2026
@watson-ch

watson-ch Bot commented Nov 16, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/1384

Labels added:

  • v26.2-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@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 16, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/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 submodule changed At least one submodule changed in this PR. v26.2-must-backport v26.4-must-backport v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants