Skip to content

Cherry pick #127690 to 26.8: Replace the apache/orc library with a native ORC reader and writer - #129299

Merged
robot-ch-test-poll3 merged 24 commits into
backport/26.8/127690from
cherrypick/26.8/127690
Nov 6, 2026
Merged

robot-ch-test-poll3 merged 24 commits into
backport/26.8/127690from
cherrypick/26.8/127690

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #127690

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 22 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]>
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]>
…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]>
… 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]>
…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]>
…ngth`

`Writer::close` set `footer.content_length` after writing the metadata section
with the stripe statistics, so it was larger than the total size of the stripes,
unlike the reference implementation, where it covers only the stripes.

The references of `01308_orc_output_format_arrays` and `02426_orc_bug` hash the
written ORC bytes, so they change. A new test parses the footer and checks that
`content_length` equals the size of the stripes.

#127690

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The native ORC reader read the selected columns of a whole stripe at once even
when the search argument selected only a few row groups. Without prefetching
(local files), liborc read only the compression chunks around the selected row
groups, so the point lookups of the `orc_filter_push_down` performance test were
slower.

Now, with a search argument, only the index streams are read at once; if some row
groups of the stripe are skipped, the data streams are loaded on demand in windows
of at least 1 MiB (from the prefetched data if any). Otherwise the whole stripe is
read at once, as before. Also, `RowIndex::parse` constructs the entries in place.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127690&sha=latest&name_0=PR&name_1=Performance%20Comparison%20(arm_release%2C%20master_head%2C%203%2F6)
#127690

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

In the files with the format version 1.9999 (`UNSTABLE_PRE_2_0` in apache/orc),
the DECIMAL columns with the precision up to 18 are written as signed RLEv2
integers in the DATA stream without the SECONDARY stream of scales. The
replaced library supported this layout, so keep supporting it: the stripe
streams report `isDecimalAsLong` from the postscript version and the new
`Decimal64V2ColumnReader` decodes such columns.

The test uses `decimal64_v2.orc` from the examples of apache/orc.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Replace the apache/orc library with a native ORC reader and writer
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Nov 4, 2026
@Algunenano Algunenano closed this Nov 4, 2026
Algunenano and others added 2 commits November 6, 2026 16:47
…rypick/26.8/127690

# Conflicts:
#	contrib/CMakeLists.txt
#	contrib/arrow-cmake/CMakeLists.txt
#	contrib/orc
#	src/Processors/Formats/Impl/NativeORCBlockInputFormat.cpp
#	tests/queries/0_stateless/05036_orc_unimplemented_codec_no_abort.reference
#	tests/queries/0_stateless/05036_orc_unimplemented_codec_no_abort.sh
#	tests/queries/0_stateless/05317_orc_corrupt_stripe_footer_stream_column.reference
#	tests/queries/0_stateless/05317_orc_corrupt_stripe_footer_stream_column.sh
#	tests/queries/0_stateless/05407_orc_timezone_traversal_abort.reference
#	tests/queries/0_stateless/05407_orc_timezone_traversal_abort.sh
@Algunenano Algunenano reopened this Nov 6, 2026
@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Dear @alexey-milovidov, this PR is opened while #127690 was marked as backported. The pr-backports-created is removed, so the original PR can be processed again.

If the cherry-pick is not needed anymore, then just close this PR.

@robot-ch-test-poll3
robot-ch-test-poll3 merged commit 765e93e into backport/26.8/127690 Nov 6, 2026
2 checks passed
@robot-ch-test-poll3
robot-ch-test-poll3 deleted the cherrypick/26.8/127690 branch November 6, 2026 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants