Skip to content

Make DateTime a portable logical type with a precision - #41550

Open
ahmedabu98 wants to merge 4 commits into
apache:masterfrom
ahmedabu98:datetime-logicaltype
Open

ahmedabu98 wants to merge 4 commits into
apache:masterfrom
ahmedabu98:datetime-logicaltype

Conversation

@ahmedabu98

@ahmedabu98 ahmedabu98 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Makes beam:logical_type:datetime:v1 a standard portable logical type with an optional precision (0 to 9). SqlTypes.DATETIME has none, so it keeps every digit, as before. Python gets a matching apache_beam.utils.timestamp.DateTime.

The Python DateTime has no arithmetic for now. To compute with it, convert it to datetime.datetime with to_datetime() or to Arrow with to_arrow().

This takes a similar approach to logical Timestamp (#36705, #39537). Design: https://s.apache.org/beam-timestamp-strategy

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.

@github-actions github-actions Bot 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.

📋 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> with Time in nanoseconds) are consistent between Java and Python.
  • Backward compatibility with older SDK encodings (both Java object serialization via readResolve and legacy proto STRING "" 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

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.31034% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 59.41%. Comparing base (6b9bec4) to head (ace8989).
⚠️ Report is 162 commits behind head on master.

Files with missing lines Patch % Lines
sdks/python/apache_beam/utils/timestamp.py 99.00% 1 Missing ⚠️
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              
Flag Coverage Δ
python 80.65% <99.31%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.
Backup reviewer: damccorm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

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 assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@github-actions

Copy link
Copy Markdown
Contributor

Reminder, please take a look at this pr: @tvalentyn

public static DateTime of(int precision) {
return new DateTime(precision);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

Suggested change
/**
* 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;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed it so that SqlTypes.DATETIME has no precision anymore, so it still keeps all 9 digits

Comment thread CHANGES.md Outdated

[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)).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 field

POJOs with a LocalDateTime field get SqlTypes.DATETIME automatically and can't switch to DateTime.NANOS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Was this test triggered?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thx for the catch, just triggered the ITs

@ahmedabu98
ahmedabu98 requested a review from claudevdm December 2, 2026 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants