Repository navigation
Make DateTime a portable logical type with a precision - #41550
ahmedabu98 wants to merge 4 commits into
Conversation
Java's DateTime now takes a precision (0 to 9, default 6) and is the standard beam:logical_type:datetime:v1 type, so SqlTypes.DATETIME is DateTime(6). Python gets a matching apache_beam.utils.timestamp.DateTime that converts to and from datetime.datetime and Arrow timestamps.
There was a problem hiding this comment.
📋 Review Summary
This PR promotes beam:logical_type:datetime:v1 to a standard portable logical type parameterized by subsecond precision (0–9, defaulting to 6) across the model proto, Java SDK, and Python SDK. The implementation is clean, well-structured, and thoroughly tested—including backward-compatibility handling for Java-serialized DateTime instances, legacy empty-string proto arguments, standard coder YAML test cases, and conversions between Python DateTime, datetime.datetime/pandas.Timestamp, and pyarrow.TimestampScalar.
🔍 General Feedback
- The precision validation, lossless/lossy conversion semantics, and representation invariants (
ROW<Date: INT64, Time: INT64>withTimein nanoseconds) are consistent between Java and Python. - Backward compatibility with older SDK encodings (both Java object serialization via
readResolveand legacy protoSTRING ""arguments in Python) is well covered by unit tests.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #41550 +/- ##
============================================
+ Coverage 59.38% 59.41% +0.03%
Complexity 5133 5133
============================================
Files 1469 1469
Lines 187488 187668 +180
Branches 3816 3816
============================================
+ Hits 111342 111507 +165
- Misses 72741 72756 +15
Partials 3405 3405
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Assigning reviewers: R: @tvalentyn for label python. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
|
Reminder, please take a look at this pr: @tvalentyn |
| public static DateTime of(int precision) { | ||
| return new DateTime(precision); | ||
| } | ||
|
|
There was a problem hiding this comment.
Beam 2.78 and earlier write this type's argument as an empty string. Java on this branch can't read those schemas (does not have a static of('java.lang.String') method), while master can. That would break, for example, --transformsToUpgrade from an older pipeline. Python here already accepts the empty string. Would something like this work for Java?
| /** | |
| * Reads the STRING argument that Beam 2.78 and earlier write for this type, so their schemas | |
| * still decode. That argument is always empty, and means the default precision. | |
| */ | |
| public static DateTime of(String legacyArgument) { | |
| checkArgument( | |
| legacyArgument.isEmpty(), | |
| "DateTime precision must be an INT32, but got the STRING %s.", | |
| legacyArgument); | |
| return MICROS; | |
| } | |
There was a problem hiding this comment.
Added a static DateTime.of(String) and marked it as internal. It reads the empty argument as a DateTime with no precision, so no precision check will apply for it. This keeps backwards compatibility for SqlTypes.DATETIME.
There was a problem hiding this comment.
I also kept the Java payload in the schema, so 2.78 and earlier can read what we write.
|
|
||
| // Instances that earlier versions serialized have neither field set, and they meant micros. | ||
| private Object readResolve() { | ||
| return scalingFactor == 0 ? MICROS : this; |
There was a problem hiding this comment.
Before this PR, DateTime had no precision and kept all 9 digits. When a DateTime saved by an earlier release (in a savepoint, for example) is loaded, this line makes it precision 6. Values that job already stored with 9 digits then can't be read?
There was a problem hiding this comment.
Changed it so that SqlTypes.DATETIME has no precision anymore, so it still keeps all 9 digits
|
|
||
| [comment]: # "generic breaking changes if not covered in following sections." | ||
| * (Java) Flink runner: batch pipelines now assign source splits to readers statically by default instead of lazily. Set `--sourceStaticSplitThresholdMb=0` to restore lazy assignment ([#40607](https://github.com/apache/beam/issues/40607)). | ||
| * (Java) `SqlTypes.DATETIME` is now `DateTime` with precision 6, so a `LocalDateTime` with digits below a microsecond throws instead of being stored. Use `DateTime.NANOS` for those values ([#41550](https://github.com/apache/beam/pull/41550)). |
There was a problem hiding this comment.
On Linux with JDK 15+, LocalDateTime.now() has 9 digits, so this works on master and throws on this branch:
Row.withSchema(s).addValue(LocalDateTime.now()).build(); // s has a SqlTypes.DATETIME fieldPOJOs with a LocalDateTime field get SqlTypes.DATETIME automatically and can't switch to DateTime.NANOS.
There was a problem hiding this comment.
See previous comment
| date_=datetime.date.today() - datetime.timedelta(days=num), | ||
| timestamp_=Timestamp(123 * num, 456 * num)) | ||
| timestamp_=Timestamp(123 * num, 456 * num), | ||
| datetime_=DateTime.from_datetime( |
There was a problem hiding this comment.
Was this test triggered?
There was a problem hiding this comment.
Thx for the catch, just triggered the ITs
# Conflicts: # CHANGES.md
Makes
beam:logical_type:datetime:v1a standard portable logical type with an optional precision (0 to 9).SqlTypes.DATETIMEhas none, so it keeps every digit, as before. Python gets a matchingapache_beam.utils.timestamp.DateTime.The Python
DateTimehas no arithmetic for now. To compute with it, convert it todatetime.datetimewithto_datetime()or to Arrow withto_arrow().This takes a similar approach to logical Timestamp (#36705, #39537). Design: https://s.apache.org/beam-timestamp-strategy