Skip to content

Harden the native ORC reader and writer against malformed input - #129345

Merged
Algunenano merged 16 commits into
ClickHouse:masterfrom
Algunenano:orc-native-hardening
Nov 6, 2026
Merged

Algunenano merged 16 commits into
ClickHouse:masterfrom
Algunenano:orc-native-hardening

Conversation

@Algunenano

@Algunenano Algunenano commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Hardening of the native ORC reader and writer, a follow-up to the replacement of the apache/orc library. Malformed files could cause signed overflows in the timestamp decoding and in the timestamp statistics used by the filter pushdown, silently decode an empty dictionary as empty strings with lazy dictionaries, allocate the declared compression block, codec chunk size or dictionary size before validating the data, and wrap around the stripe, stream and row group arithmetic. The writer had undefined conversions for a huge output_format_orc_dictionary_key_size_threshold and for extreme DateTime64 values. The repeated objects of the metadata are now limited by the numbers of columns, stripes and row groups.

The timestamps before the epoch are now normalized in the statistics and in the literals of the filter pushdown, as in the reference implementation: the writer stored negative sub-millisecond nanoseconds, and the comparison could prune matching rows. Nullable floats are read with one copy instead of byte by byte.

cc @alexey-milovidov

Closes: #129500
Related: #127690

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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


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

Version info

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

Algunenano and others added 7 commits November 4, 2026 17:27
Use checked arithmetic when decoding the nanoseconds and adjusting the seconds of ORC timestamps, and reject
malformed values. Malformed nanoseconds in the timestamp statistics disable the filter pushdown instead of
overflowing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Compare the dictionary threshold as a double, and reject `DateTime64` values whose seconds or millisecond
statistics overflow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The `LowCardinality` path returned empty strings for non-null rows of an empty dictionary, while the eager path
rejected them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Size the decompression buffer by the maximum ratio of the codec, and grow the string offsets and bytes as they are
decoded, so a small file cannot allocate the whole compression block or a huge dictionary up front.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Validate all stripes once when reading the tail, and check every stream length while summing the offsets, including
the streams of the columns that are not read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Round up the number of row groups without overflowing, and check it against the row indexes before allocating.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Limit the column statistics, stripe statistics, column encodings, row index entries and bloom filters by the numbers
of columns, stripes and row groups, so a small metadata message cannot allocate many objects.

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

clickhouse-gh Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f0c5420]

Summary: ✅


AI Review

Summary

Reviewed head f0c5420a98fa7ac362d6fac8ad971400461ec382. This PR hardens the native ORC reader and writer against malformed timestamps, oversized allocations, overflowing offsets and excessive metadata, and improves nullable floating-point reads. No outstanding findings remain. The three earlier concerns are addressed, including conservative probing of both timestamp bloom-filter rounding conventions and an external-writer regression fixture.

Consumers checked for the normalized timestamp representation:

  • PredicateLeaf::evaluatePredicateMinMax compares normalized literals with normalized statistics at file, stripe and row-group levels.
  • checkInBloomFilter accepts both millisecond keys for negative fractional milliseconds, covering equality and IN.
  • StatisticsBuilder::updateTimestamp, merge and toProto retain the millisecond/sub-millisecond ordering and serialize nonnegative remainders.
  • Proto parsing preserves unvalidated nanosecond fields until pruning validates them; HiveORCFile::buildRange consumes only the existing millisecond fields.
Missing context / blind spots
  • ⚠️ The current head's ORC-enabled CI jobs are still pending. No local ClickHouse binary or build directory is available, so the added stateless tests were reviewed but not executed here. Passing 05507–05515 in an ORC-enabled job would close this validation gap; the completed fast tests skip ORC.
Final Verdict

Status: ✅ Approve. No code changes requested; normal CI validation remains pending.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Nov 4, 2026
Algunenano and others added 2 commits November 4, 2026 17:47
The native ORC reader and writer are built in the fast test, only S3 is not.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Nov 4, 2026
ORC is not built in the fast test after all: it does not check out snappy, and the ORC writer test failed there.

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

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Changes look good.

@alexey-milovidov alexey-milovidov self-assigned this Nov 5, 2026
Algunenano and others added 4 commits November 5, 2026 15:05
Trust the sizes from the chunk headers only up to the maximum compression ratio of the codec, so a few bytes cannot request the whole compression block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The writer stored negative sub-millisecond nanoseconds in the statistics, which disabled the pruning, and the literals and statistics were compared with different signs of the nanoseconds, which could prune matching rows. Round the milliseconds down and keep the nanoseconds non-negative everywhere, as in the reference implementation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Read the non-null values at once and move them to their rows, instead of decoding every value byte by byte.

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

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f0c5420a9 with master 49270a6ea (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

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

Job report

The C++ writer of ORC rounds the negative milliseconds toward zero, while the Java writer rounds them down. Probe both keys, so the normalized literals keep the matching rows in files from either writer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Algunenano
Algunenano added this pull request to the merge queue Nov 6, 2026
Merged via the queue into ClickHouse:master with commit 1bec51c Nov 6, 2026
186 checks passed
@Algunenano
Algunenano deleted the orc-native-hardening branch November 6, 2026 15:32
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 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-not-for-changelog This PR should not be mentioned in the changelog 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.

Native ORC reader decodes Nullable(Float32/Float64) values byte by byte; one copy makes these reads 30-44% faster

3 participants