Skip to content

Commit

Permalink
[core] Fix PredicateBuilder.convertJavaObject with timestamp with loc…
Browse files Browse the repository at this point in the history
…al zone (#3752)
  • Loading branch information
JingsongLi authored Jul 15, 2024
1 parent 7acae4b commit e4ba677
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,32 @@ public static Object convertJavaObject(DataType literalType, Object o) {
int scale = decimalType.getScale();
return Decimal.fromBigDecimal((BigDecimal) o, precision, scale);
case TIMESTAMP_WITHOUT_TIME_ZONE:
Timestamp timestamp;
if (o instanceof java.sql.Timestamp) {
timestamp = Timestamp.fromSQLTimestamp((java.sql.Timestamp) o);
return Timestamp.fromSQLTimestamp((java.sql.Timestamp) o);
} else if (o instanceof Instant) {
Instant o1 = (Instant) o;
LocalDateTime dateTime = o1.atZone(ZoneId.systemDefault()).toLocalDateTime();
timestamp = Timestamp.fromLocalDateTime(dateTime);
return Timestamp.fromLocalDateTime(dateTime);
} else if (o instanceof LocalDateTime) {
timestamp = Timestamp.fromLocalDateTime((LocalDateTime) o);
return Timestamp.fromLocalDateTime((LocalDateTime) o);
} else {
throw new UnsupportedOperationException("Unsupported object: " + o);
throw new UnsupportedOperationException(
String.format(
"Unsupported class %s for timestamp without timezone ",
o.getClass()));
}
return timestamp;
case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
if (o instanceof java.sql.Timestamp) {
timestamp = Timestamp.fromSQLTimestamp((java.sql.Timestamp) o);
java.sql.Timestamp timestamp = (java.sql.Timestamp) o;
return Timestamp.fromInstant(timestamp.toInstant());
} else if (o instanceof Instant) {
timestamp = Timestamp.fromInstant((Instant) o);
} else if (o instanceof LocalDateTime) {
timestamp = Timestamp.fromLocalDateTime((LocalDateTime) o);
return Timestamp.fromInstant((Instant) o);
} else {
throw new UnsupportedOperationException("Unsupported object: " + o);
throw new UnsupportedOperationException(
String.format(
"Unsupported class %s for timestamp with local time zone ",
o.getClass()));
}
return timestamp;
default:
throw new UnsupportedOperationException(
"Unsupported predicate leaf type " + literalType.getTypeRoot().name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public void testLiteral() {
java.sql.Timestamp.valueOf("2022-05-17 16:25:53"),
DataTypes.TIMESTAMP(3),
Timestamp.fromSQLTimestamp(java.sql.Timestamp.valueOf("2022-05-17 16:25:53")));
testLiteral(
PredicateLeaf.Type.TIMESTAMP,
java.sql.Timestamp.valueOf("2022-05-17 16:25:53"),
DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE(3),
Timestamp.fromInstant(
java.sql.Timestamp.valueOf("2022-05-17 16:25:53").toInstant()));
}

private void testLiteral(
Expand Down

0 comments on commit e4ba677

Please sign in to comment.