Skip to content

Write non-negative nanoseconds for ORC timestamps before the epoch - #131036

Merged
Algunenano merged 4 commits into
ClickHouse:masterfrom
Algunenano:orc-timestamp-negative-nanoseconds
Nov 13, 2026
Merged

Algunenano merged 4 commits into
ClickHouse:masterfrom
Algunenano:orc-timestamp-negative-nanoseconds

Conversation

@Algunenano

@Algunenano Algunenano commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

The ORC writer rounded the seconds of a timestamp toward zero, so a timestamp before 1970 with a fractional part had negative nanoseconds, stored wrapped in the unsigned nanoseconds stream. ClickHouse and PyArrow read it back correctly, but Java readers (Hive, Spark, orc-tools) reject the file with nanos > 999999999 or < 0.

The nanoseconds are now always the non-negative fraction of the second, and the seconds are stored as the Apache ORC Java writer stores them (TimestampTreeWriter): the milliseconds of java.sql.Timestamp divided by 1000, rounded toward zero. The readers subtract a second when the stored seconds are negative and the nanoseconds are more than 999999. For these timestamps the stored streams are now identical to the Java writer's.

A timestamp strictly between -1 and 0 seconds (from 1969-12-31 23:59:59.001 to 1969-12-31 23:59:59.999999999 UTC) cannot be stored in ORC: this is a gap in the specification, which does not allow negative nanoseconds, and the readers subtract a second only when the stored seconds are negative. The reference implementations write it differently: the Apache ORC Java and C++ writers store it so that it is read back one second later, after the epoch, and the Arrow ORC adapter (PyArrow) writes negative nanoseconds, which the Java reader rejects. ClickHouse now writes such a timestamp as 1970-01-01 00:00:00, which all readers read back the same way. The format documentation describes this, and the difference between the Java and C++ readers when the writer and reader time zones differ.

Files written with negative nanoseconds are still read correctly.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix ORC files with DateTime64 values before 1970 with a fractional part being rejected by Java readers (Hive, Spark): the nanoseconds were stored negative.


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

Version info

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

The seconds were rounded toward zero, so the nanoseconds of the timestamps before 1970 were negative and Java
readers rejected the files. Store them as the Java writer of Apache ORC does. A timestamp between -1 and 0 seconds
cannot be stored that way, so it keeps negative nanoseconds instead of being read back as positive.

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

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [50bd408]

Summary: ✅


AI Review

Summary

The PR writes non-negative nanoseconds for fractional pre-1970 DateTime64 values in ORC, matching the Apache ORC writers and avoiding Java reader rejection. At head 50bd408a6199f3cb94c1e663efda6a8fb008fe0e, two previously reported correctness concerns remain. Both were deliberately dismissed by the author; their threads remain closed, and no duplicate inline comments were posted.

Findings

❌ Blockers

  • src/Processors/Formats/Impl/ORC/Writer.cpp:1107–1110 — Successful exports silently replace valid timestamps. [dismissed by author -- https://github.com/Write non-negative nanoseconds for ORC timestamps before the epoch #131036#discussion_r4552996498] 1969-12-31 23:59:59.5 reaches this branch with secs = -1 and nano = 500000000; both become zero. The original value cannot be recovered, and the new reference explicitly expects this loss. The representation gap and the author's preference to avoid failing an export explain the decision, but do not preserve the input data. Reject unrepresentable timestamps with a descriptive exception, or require an explicit setting to permit lossy output.

  • src/Processors/Formats/Impl/ORC/Writer.cpp:1112 — Existing C++/PyArrow reads can silently shift by one second. [dismissed by author -- https://github.com/Write non-negative nanoseconds for ORC timestamps before the epoch #131036#discussion_r4549835963] With input 1969-12-31 23:59:58.5 UTC and output_format_orc_writer_time_zone_name = 'Asia/Shanghai', the new streams reconstruct writer seconds -1 and nanoseconds 500000000. The Apache ORC C++ reader converts seconds to 28799 before its negative-seconds correction, producing 1970-01-01 07:59:59.5; ClickHouse/Java produce 1970-01-01 07:59:58.5, as the previous encoding did in PyArrow. The author correctly identifies an upstream reader disagreement, but matching the reference writers and documenting it do not prevent the changed result for existing exports. Reject affected timezone-crossing cases or require explicit acceptance of this interoperability loss.

Tests
  • ⚠️ The new tests use UTC and the ClickHouse reader, so they cannot detect the timezone-dependent cross-reader discrepancy. Add a focused non-UTC writer case checked with Java and C++/PyArrow readers.
  • ⚠️ For the unrepresentable interval, test explicit rejection or the chosen opt-in setting, including the 999999/1000000 nanosecond boundary, instead of accepting silent replacement by default.
Missing context / blind spots
  • ⚠️ No local ClickHouse binary was available, so runtime reproductions were not repeated. Findings were rechecked against the current code, test references, and Apache ORC v2.2.1 writer/reader implementations. The current Praktika report exposed no individual result for 05622; a focused run would close that execution-evidence gap.
Final Verdict

❌ Block. Make lossy timestamp substitution explicit and prevent silent timezone-dependent export regressions, with focused coverage. These are existing dismissed findings; neither thread was reopened or replied to.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 12, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Nov 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 50bd408a6 with master 401380a35 (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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.57, programs/clickhouse-keeper ×0.64 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 6.2 s 8.8 s +5.3 s (+148%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.6 s 7.3 s +5.3 s (+258%)
Compile time of recompiled translation units

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

Job report

ORC cannot store them: the stored seconds would be 0 and the readers would not subtract the second. Write the epoch,
which all readers read the same way, instead of negative nanoseconds, which the specification does not allow. Document
the gap in the specification and how the reference implementations differ.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test wrote timestamps between -1 and 0 seconds, which the ORC writer now stores as the epoch, so most rows lost their values. Use timestamps from -2.000000001 to -1.001000001 seconds, which are stored unchanged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@Algunenano
Algunenano added this pull request to the merge queue Nov 13, 2026
Merged via the queue into ClickHouse:master with commit 1c05c7a Nov 13, 2026
204 checks passed
@Algunenano
Algunenano deleted the orc-timestamp-negative-nanoseconds branch November 13, 2026 17:55
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 13, 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-bugfix Pull request with bugfix, not backported by default 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.

2 participants