Skip to content
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Expand Down Expand Up @@ -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_(
Expand Down Expand Up @@ -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,
Expand Down
Loading