Skip to content

[format] Keep ORC TIMESTAMP values independent of the JVM zone and calendar - #11528

Open
leo7martins wants to merge 2 commits into
apache:masterfrom
leo7martins:fix/11468-orc-timestamp-dst-julian
Open

leo7martins wants to merge 2 commits into
apache:masterfrom
leo7martins:fix/11468-orc-timestamp-dst-julian

Conversation

@leo7martins

Copy link
Copy Markdown

Purpose

ORC TIMESTAMP values (without time zone) did not round-trip when the JVM default zone was not UTC. A wall-clock time in a DST gap came back one hour later (2020-03-08 02:30:00 in America/New_York became 03:30:00). Dates before the Gregorian cutover shifted by the Julian/Gregorian difference (0001-01-01 became 0000-12-30, 1582-10-04 became 1582-10-14, 1582-10-10 became 1582-10-20). 1582-10-15 and later normal values were unchanged. Parquet does not show this.

Root cause: FieldWriterFactory.visit(TimestampType) stored java.sql.Timestamp.valueOf(LocalDateTime). That uses the JVM default zone and the hybrid Julian/Gregorian calendar, so a local time that does not exist in the zone is moved forward and dates before 1582-10-15 are not the proleptic millis Paimon uses. The reader rebuilt the value with DateTimeUtils.toInternal, which adds the default-zone offset using proleptic arithmetic. Those two conversions are not inverses for a DST gap or a pre-cutover date, so filters and aggregates on the column changed silently.

The writer now stores the proleptic epoch millis of the wall clock and opens the ORC writer with useUTCTimestamp(true). The reader uses the same UTC setting and rebuilds a TIMESTAMP with Timestamp.fromEpochMillis, without adding the default-zone offset. Column statistics and equality-filter literals use those same millis. TIMESTAMP_LTZ is unchanged: orc.timestamp-ltz.legacy.type=true still uses the historical local-zone encoding, and the non-legacy path already stores an instant. This does not change the CHAR padding behavior in #11467.

A file written the old way still reads as before. A normal wall clock comes back unchanged. A DST-gap or pre-1582 value that was already stored shifted stays shifted.

Tests

Run on JDK 21.0.13.

Before the fix:

mvn -pl paimon-format -am -Pfast-build -DfailIfNoTests=false -DwildcardSuites=none -Dtest=OrcFormatReadWriteTest#testTimestampWithoutZoneRoundTripIgnoresDefaultZoneAndCutover test

Tests run: 1, Failures: 1. Under America/New_York the DST-gap value moved one hour later and the pre-1582 values shifted as in the issue.

After the fix, the same command: Tests run: 1, Failures: 0. The test writes and reads under America/New_York and Europe/Lisbon, reads a file written in one zone from the other, and checks column statistics and an equality filter on the DST-gap value.

mvn -pl paimon-format -DfailIfNoTests=false -DwildcardSuites=none test

Checkstyle, Spotless, and Enforcer ran in this build. Tests run: 860, Failures: 0, Errors: 0, Skipped: 6.

mvn -pl paimon-format -Pfast-build -DfailIfNoTests=false -DwildcardSuites=none -Dtest=OrcFormatReadWriteTest test

Tests run: 16, Failures: 0. This includes the new round trip, a read of a file written with the old java.sql.Timestamp.valueOf encoding, and the existing TIMESTAMP_LTZ cases.

mvn -pl paimon-format -Dspotless.check.skip=false spotless:check and mvn -pl paimon-format -Dcheckstyle.skip=false checkstyle:check both succeeded.

Fixes #11468

This was written with help from an AI coding assistant.

…lendar

TIMESTAMP without time zone was written through java.sql.Timestamp.valueOf,
which applies the JVM default zone and the hybrid Julian/Gregorian calendar.
Store and read the proleptic wall clock as UTC instead.

Fixes apache#11468

@JingsongLi JingsongLi 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.

All 100 ORC tests passed locally on JDK 8 with America/New_York. A separate write/read matrix using both master and this revision found two data changes outside the existing cases.

// Store the proleptic wall clock. The writer is opened with useUTCTimestamp, so ORC
// keeps these millis instead of shifting them by the JVM zone or the Julian cutover.
vector.setIsUTC(true);
vector.set(rowId, OrcTimestampUtil.toSqlTimestamp(timestamp));

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.

[P1] Preserve fractional timestamps in the last second before the epoch

I round-tripped a TIMESTAMP(6) ORC file in America/New_York and Asia/Shanghai. Master preserves 1969-12-31 23:59:59.999999. With the new writer, both Paimon readers return 1970-01-01 00:00:00.999999, and an equality predicate for the original value prunes the file. ORC 1.9.8 divides the UTC vector milliseconds by 1000, truncating -1 to zero. Please preserve the negative second in the encoding and cover readback and equality pruning on both sides of the epoch.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 6c49822. The writer now floor-divides the proleptic millis into the ORC timestamp vector, so the second before the epoch stays negative. On read and in column statistics, the extra second ORC subtracts for a negative fractional value is added back, and only for files whose footer is proleptic. Equality and IN pushdown include both the wall-clock timestamp and the statistics timestamp when they differ, so a predicate for 1969-12-31 23:59:59.999999 keeps a file that contains it and drops a file that contains only a value after the epoch. testTimestampAroundEpochRoundTripAndEqualityPruning covers readback and that pruning in America/New_York and Asia/Shanghai.

// TIMESTAMP without time zone is a proleptic wall clock (see OrcTimestampUtil). Writing
// the stripe in UTC keeps that value stable across JVM default zones. TIMESTAMP_LTZ is an
// ORC instant and already uses UTC, so this does not change its encoding.
writerOptions.useUTCTimestamp(true);

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.

[P1] Keep the written calendar and the ORC footer in agreement

The file contains proleptic wall-clock millis with writerUsedProlepticGregorian=false. With stock ORC's convertToProlepticGregorian(true), a new file written with 0001-01-01 and 1582-10-04 reads as 0001-01-03 and 1582-09-24. The same ORC reader returns the original values from the master-written control. Paimon-to-Paimon tests pass because the new reader interprets the stored millis directly. Please make the vector calendar and footer consistent and add a stock ORC read regression alongside old-file compatibility coverage.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 6c49822. The writer now sets the proleptic Gregorian option, and each batch marks date and timestamp vectors proleptic before addRowBatch, so the footer and the stored millis name the same calendar. The reader sets convertToProlepticGregorian from writerUsedProlepticGregorian(), so a new file is not converted again and an older hybrid file is not converted either. testStockOrcReadsProlepticWallClock reads a file written with 0001-01-01 and 1582-10-04 through stock ORC (useUTCTimestamp and convertToProlepticGregorian true, not Paimon's reader); the footer is proleptic and those values come back unchanged. testHistoricalLocalTimestampEncodingStillReads still covers the older encoding. mvn -pl paimon-format test: 863 run, 0 failures, 6 skipped.

…eptic footer

ORC 1.9.8 divides vector milliseconds by 1000 toward zero, so a fractional
timestamp in the last second before the epoch was stored as the following
second. Floor the second into the vector and restore the second ORC subtracts
on read and in statistics. Equality pushdown includes both the wall-clock
literal and the statistics timestamp so files on either side of the epoch are
pruned correctly, including files written before this change.

The writer now sets the proleptic Gregorian footer and marks date and timestamp
vectors proleptic before each batch. The reader converts to the proleptic
calendar only when the file footer says the writer used it, so older hybrid
files still read as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ORC format: TIMESTAMP values are shifted in DST gaps of the JVM zone and before 1582-10-15 (java.sql.Timestamp conversion)

2 participants