-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[Java]:Add support for portable timestamp logical types (micros_instant, nanos_instant) in BeamCalcRel #37277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 6 commits
d70b782
6d572e4
179a8f8
a19b39b
288216d
6b4b415
d6776d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,8 @@ | |
| import org.apache.beam.sdk.schemas.logicaltypes.FixedBytes; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.FixedPrecisionNumeric; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.FixedString; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.MicrosInstant; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.NanosInstant; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.PassThroughLogicalType; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.SqlTypes; | ||
| import org.apache.beam.sdk.schemas.logicaltypes.VariableBytes; | ||
|
|
@@ -416,6 +418,10 @@ static Object toBeamObject(Object value, FieldType fieldType, boolean verifyValu | |
| String identifier = logicalType.getIdentifier(); | ||
| if (TimeWithLocalTzType.IDENTIFIER.equals(identifier)) { | ||
| return Instant.ofEpochMilli(((Number) value).longValue()); | ||
| } else if (MicrosInstant.IDENTIFIER.equals(identifier) | ||
| || NanosInstant.IDENTIFIER.equals(identifier)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a loss of accuracy here, as micros instant and nanos instant are sub-millis. Also I'd need to confirm this would actually work when input Row has a schema that contains a MicrosInstant/NanoInstant logical type
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes nice catch but I think this is because calcite itself is limited to milliseconds (reference) and the sub-millis are lost in the Beam -> Calcite -> Beam round-trip. Can u confirm if there is a different fix for this? |
||
| // Portable instant logical types: treat calcite numeric as epoch milliseconds. | ||
| return java.time.Instant.ofEpochMilli(((Number) value).longValue()); | ||
| } else if (SqlTypes.DATE.getIdentifier().equals(identifier)) { | ||
| if (value instanceof Date) { | ||
| value = SqlFunctions.toInt((Date) value); | ||
|
|
@@ -574,6 +580,17 @@ private static Expression getBeamField( | |
| value = Expressions.call(expression, "getBytes", fieldName); | ||
| } else if (TimeWithLocalTzType.IDENTIFIER.equals(identifier)) { | ||
| value = Expressions.call(expression, "getDateTime", fieldName); | ||
| } else if (MicrosInstant.IDENTIFIER.equals(identifier) | ||
| || NanosInstant.IDENTIFIER.equals(identifier)) { | ||
| // Instant-like logical types: retrieve as Instant | ||
| value = | ||
| Expressions.convert_( | ||
| Expressions.call( | ||
| expression, | ||
| "getLogicalTypeValue", | ||
| fieldName, | ||
| Expressions.constant(java.time.Instant.class)), | ||
| java.time.Instant.class); | ||
| } else if (SqlTypes.DATE.getIdentifier().equals(identifier)) { | ||
| value = | ||
| Expressions.convert_( | ||
|
|
@@ -661,6 +678,13 @@ private static Expression toCalciteValue(Expression value, FieldType fieldType) | |
| } else if (TimeWithLocalTzType.IDENTIFIER.equals(identifier)) { | ||
| return nullOr( | ||
| value, Expressions.call(Expressions.convert_(value, DateTime.class), "getMillis")); | ||
| } else if (MicrosInstant.IDENTIFIER.equals(identifier) | ||
| || NanosInstant.IDENTIFIER.equals(identifier)) { | ||
| // Convert java.time.Instant to epoch milliseconds for Calcite | ||
| return nullOr( | ||
| value, | ||
| Expressions.call( | ||
| Expressions.convert_(value, java.time.Instant.class), "toEpochMilli")); | ||
| } else if (SqlTypes.DATE.getIdentifier().equals(identifier)) { | ||
| return nullOr( | ||
| value, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.