twalthr commented on a change in pull request #11694:
[FLINK-17064][table-planner] Improve literals conversion in ExpressionConverter
URL: https://github.com/apache/flink/pull/11694#discussion_r408869185
##########
File path:
flink-table/flink-table-common/src/main/java/org/apache/flink/table/expressions/ValueLiteralExpression.java
##########
@@ -85,90 +97,105 @@ public boolean isNull() {
return Optional.empty();
}
- final Class<?> valueClass = value.getClass();
-
Object convertedValue = null;
if (clazz.isInstance(value)) {
convertedValue = clazz.cast(value);
+ } else if (clazz == Period.class) {
+ convertedValue = convertToPeriod(value);
+ } else if (clazz == Duration.class) {
+ convertedValue = convertToDuration(value);
+ } else if (clazz == LocalDate.class) {
+ convertedValue = convertToLocalDate(value);
+ } else if (clazz == LocalTime.class) {
+ convertedValue = convertToLocalTime(value);
+ } else if (clazz == LocalDateTime.class) {
+ convertedValue = convertToLocalDateTime(value);
+ } else if (clazz == OffsetDateTime.class) {
+ convertedValue = convertToOffsetDateTime(value);
+ } else if (clazz == Instant.class) {
+ convertedValue = convertToInstant(value);
+ } else if (clazz == BigDecimal.class) {
+ convertedValue = convertToBigDecimal(value);
}
- else if (valueClass == Duration.class && clazz == Long.class) {
- final Duration duration = (Duration) value;
- convertedValue = duration.toMillis();
- }
+ return Optional.ofNullable((T) convertedValue);
+ }
- else if (valueClass == Long.class && clazz == Duration.class) {
- final Long longVal = (Long) value;
- convertedValue = Duration.ofMillis(longVal);
+ private @Nullable LocalDate convertToLocalDate(Object value) {
+ Class<?> valueClass = value.getClass();
+ if (valueClass == java.sql.Date.class) {
+ return ((Date) value).toLocalDate();
+ } else if (valueClass == Integer.class) {
+ return LocalDate.ofEpochDay((int) value);
}
+ return null;
+ }
- else if (valueClass == Period.class && clazz == Integer.class) {
- final Period period = (Period) value;
- convertedValue = (int) period.toTotalMonths();
+ private @Nullable LocalTime convertToLocalTime(Object value) {
+ Class<?> valueClass = value.getClass();
+ if (valueClass == java.sql.Time.class) {
+ return ((Time) value).toLocalTime();
+ } else if (valueClass == Integer.class) {
+ return LocalTime.ofNanoOfDay((int) value * 1_000_000L);
+ } else if (valueClass == Long.class) {
+ return LocalTime.ofNanoOfDay((long) value);
}
+ return null;
+ }
- else if (valueClass == Integer.class && clazz == Period.class) {
- final Integer integer = (Integer) value;
- convertedValue = Period.ofMonths(integer);
+ private @Nullable LocalDateTime convertToLocalDateTime(Object value) {
+ Class<?> valueClass = value.getClass();
Review comment:
nit: move this line out of the methods?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services