Skip to content

GH-3972: Add configurable precision for unwrapped protobuf Timestamps - #3982

Open
Goldloli wants to merge 2 commits into
apache:masterfrom
Goldloli:parquet-proto-unwrapped-timestamp-unit
Open

Goldloli wants to merge 2 commits into
apache:masterfrom
Goldloli:parquet-proto-unwrapped-timestamp-unit

Conversation

@Goldloli

Copy link
Copy Markdown

Rationale for this change

Unwrapped google.protobuf.Timestamp fields are always written as TIMESTAMP(NANOS,true) columns, which makes them unreadable for consumers capped at microsecond or millisecond resolution — Iceberg v2 clients and Athena among them, since nanosecond timestamp types arrive only in Iceberg v3 (GH-3972).

What changes are included in this PR?

Adds the parquet.proto.timestampUnit setting, accepting MILLIS, MICROS (default is still NANOS):

  • ProtoWriteSupport reads the setting and converts the unwrapped Timestamp value to that unit's epoch count.
  • ProtoSchemaConverter annotates the column as TIMESTAMP(unit,true) so the footer schema matches the stored values.
  • An unrecognized value fails at write time with a BadConfigurationException naming the setting instead of silently producing a mismatched column.
  • parquet-protobuf/README.md documents the setting.

Values are truncated toward negative infinity rather than rounded, so the stored value stays a lower bound of the original instant and stays monotonic.

Default behavior is unchanged: with the setting absent the column stays TIMESTAMP(NANOS,true) and the value is the nanosecond count, exactly as before.

Are these changes tested?

Yes. New coverage:

  • ProtoSchemaConverterTest: TIMESTAMP(MICROS,true) / TIMESTAMP(MILLIS,true) schema output, the setting read from a Configuration, and the NANOS default.
  • ProtoWriteSupportTest: micros and millis round trips through a real Parquet file, the exact addLong value written for a millis column, truncation toward negative infinity before the epoch, rejection of an unknown unit, and case-insensitive parsing of the setting.

Commands run locally (JDK 17, Maven 3.10):

mvn -pl parquet-protobuf test        # 138 tests, 0 failures
mvn -pl parquet-protobuf spotless:check

Are there any user-facing changes?

Yes, additively: parquet.proto.timestampUnit is a new optional setting. Behavior without it is unchanged.

Closes #3972

…stamps

Unwrapped google.protobuf.Timestamp fields always became
TIMESTAMP(NANOS,true) columns, so consumers limited to microsecond or
millisecond resolution (Iceberg v2 clients, for example) could not read
them without a schema rewrite.

Add parquet.proto.timestampUnit (MILLIS, MICROS, NANOS) so the writer
and the schema converter agree on the column's logical type unit. The
default stays NANOS, so existing files and callers are unaffected.

Values are truncated toward negative infinity rather than rounded, which
keeps the stored value a lower bound of the original instant and matches
what a reader recovers at that resolution.
@Goldloli

Goldloli commented Dec 2, 2026

Copy link
Copy Markdown
Author

Friendly ping — this PR is ready for review and CI approval when a maintainer has a moment.

* recovers, since the low-order digits are below the column's resolution.
*/
static long toTimestampUnit(Timestamp timestamp, TimeUnit unit) {
long nanos = Timestamps.toNanos(timestamp);

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.

For 2500-01-01T00:00:00Z, Timestamps.toNanos calls Math.multiplyExact(16_725_225_600L, 1_000_000_000L) and throws ArithmeticException before we select the unit. Every instant after 2262-04-11T23:47:16.854775807Z exceeds Long.MAX_VALUE in nanoseconds, even though milliseconds and microseconds fit through year 9999. Could the MILLIS and MICROS branches call Timestamps.toMillis and Timestamps.toMicros directly and cover year 2500 in both round-trip tests?

}

unwrapProtoWrappers = configuration.getBoolean(PB_UNWRAP_PROTO_WRAPPERS, unwrapProtoWrappers);
timestampUnit = parseTimestampUnit(configuration.get(PB_TIMESTAMP_UNIT));

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.

The setter says Ignored unless unwrapping is enabled above, but an invalid value still prevents writer initialization. Could both configuration entry points honor the unwrap flag?

Comment thread parquet-protobuf/README.md Outdated
Comment on lines +27 to +33
With `parquet.proto.unwrapProtoWrappers` set, wrapper messages such as `google.protobuf.Timestamp`,
`google.type.Date`, `google.protobuf.*Value` and `google.type.TimeOfDay` are stored as the
primitive value they carry instead of a nested group, annotated with the matching logical type.
Unwrapped `Timestamp` columns use microsecond (`parquet.proto.timestampUnit=MICROS`) or
millisecond (`MILLIS`) resolution when a reader or platform cannot consume nanosecond timestamps,
for example Iceberg v2 clients. The default stays `NANOS`. Values are truncated toward negative
infinity to the configured unit, so the stored value never lands after the original instant.

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.

Suggested change
With `parquet.proto.unwrapProtoWrappers` set, wrapper messages such as `google.protobuf.Timestamp`,
`google.type.Date`, `google.protobuf.*Value` and `google.type.TimeOfDay` are stored as the
primitive value they carry instead of a nested group, annotated with the matching logical type.
Unwrapped `Timestamp` columns use microsecond (`parquet.proto.timestampUnit=MICROS`) or
millisecond (`MILLIS`) resolution when a reader or platform cannot consume nanosecond timestamps,
for example Iceberg v2 clients. The default stays `NANOS`. Values are truncated toward negative
infinity to the configured unit, so the stored value never lands after the original instant.
Set `parquet.proto.unwrapProtoWrappers=true` to write supported protobuf message fields as
Parquet primitives. For example, `google.protobuf.BoolValue` becomes a `BOOLEAN` column,
and `google.protobuf.Timestamp` becomes an `INT64` column with a UTC-adjusted `TIMESTAMP` annotation.
For unwrapped `Timestamp` fields, set `parquet.proto.timestampUnit` to `NANOS` (the default),
`MICROS`, or `MILLIS`. The writer rounds each timestamp down to the configured unit.

@Goldloli

Goldloli commented Dec 5, 2026

Copy link
Copy Markdown
Author

Fixed in 41f1271: MILLIS/MICROS use their direct protobuf conversions, both round-trip tests cover year 2500, and both configuration entry points ignore the unit when unwrapping is off. Applied the README suggestion too.

The full protobuf module passes on Java 17: 141 tests, no failures or skips; Spotless and RAT pass.

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.

Parquet-protobuf, support configurable(but limited!) time precision during unwrap

2 participants