From 2c135eb44ab260353bd87286185decbb2c6ba22e Mon Sep 17 00:00:00 2001 From: Bora <223137607@ge.com> Date: Tue, 9 Sep 2025 14:15:45 -0400 Subject: [PATCH 1/4] Allow ISO Bindings for date/time/timestamp --- .../apache/calcite/avatica/AvaticaSite.java | 39 +++++++++- .../calcite/avatica/AvaticaSiteTest.java | 75 +++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java diff --git a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java index 18e12f082..1c5797ff2 100644 --- a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java +++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java @@ -37,6 +37,8 @@ import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; +import java.time.*; +import java.time.format.DateTimeParseException; import java.util.Calendar; /** @@ -500,21 +502,52 @@ private static byte[] toBytes(Object x) { private static Date toDate(Object x) { if (x instanceof String) { - return Date.valueOf((String) x); + String s = (String) x; + try { + return Date.valueOf(LocalDate.parse(s)); + } catch (DateTimeParseException e) { + try { + return Date.valueOf(OffsetDateTime.parse(s).toLocalDate()); + } catch (DateTimeParseException e3) { + return Date.valueOf(s); + } + } } return new Date(toLong(x)); } private static Time toTime(Object x) { if (x instanceof String) { - return Time.valueOf((String) x); + String s = (String) x; + try { + return Time.valueOf(LocalTime.parse(s)); + } catch (DateTimeParseException e) { + try { + return Time.valueOf(OffsetTime.parse(s).toLocalTime()); + } catch (DateTimeParseException e2) { + return Time.valueOf(s); + } + } } return new Time(toLong(x)); } private static Timestamp toTimestamp(Object x) { if (x instanceof String) { - return Timestamp.valueOf((String) x); + String s = (String) x; + try { + return Timestamp.from(Instant.parse(s)); + } catch (DateTimeParseException e) { + try { + return Timestamp.from(OffsetDateTime.parse(s).toInstant()); + } catch (DateTimeParseException e2) { + try { + return Timestamp.valueOf(LocalDateTime.parse(s)); + } catch (DateTimeParseException e3) { + return Timestamp.valueOf(s); + } + } + } } return new Timestamp(toLong(x)); } diff --git a/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java b/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java new file mode 100644 index 000000000..0420f0040 --- /dev/null +++ b/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.calcite.avatica; + +import org.junit.Test; + +import java.lang.reflect.Method; +import java.sql.*; +import java.time.Instant; + +import static org.junit.Assert.*; + +public class AvaticaSiteTest { + @Test + public void testToDateIsoAndJdbc() throws Exception { + Method toDate = org.apache.calcite.avatica.AvaticaSite.class.getDeclaredMethod("toDate", + Object.class); + toDate.setAccessible(true); + + assertEquals(Date.valueOf("2025-04-01"), toDate.invoke(null, "2025-04-01")); + assertEquals(Date.valueOf("2025-04-01"), toDate.invoke(null, "2025-04-01T00:00:00Z")); + assertEquals(Date.valueOf("2025-04-01"), toDate.invoke(null, "2025-04-01T00:00:00+05:00")); + assertEquals(Date.valueOf("2025-04-01"), toDate.invoke(null, "2025-04-01T00:00:00.000+05:00")); + assertEquals(Date.valueOf("2025-04-01"), toDate.invoke(null, "2025-04-01T00:00:00.000Z")); + } + + @Test + public void testToTimeIsoAndJdbc() throws Exception { + Method toTime = org.apache.calcite.avatica.AvaticaSite.class.getDeclaredMethod("toTime", + Object.class); + toTime.setAccessible(true); + + assertEquals(Time.valueOf("21:39:50"), toTime.invoke(null, "21:39:50")); + assertEquals(Time.valueOf("21:39:50"), toTime.invoke(null, "21:39:50Z")); + assertEquals(Time.valueOf("21:39:50"), toTime.invoke(null, "21:39:50+05:00")); + } + + @Test + public void testToTimestampIsoAndJdbc() throws Exception { + Method toTimestamp = org.apache.calcite.avatica.AvaticaSite.class.getDeclaredMethod( + "toTimestamp", Object.class); + toTimestamp.setAccessible(true); + + assertEquals( + Timestamp.valueOf("2025-08-14 15:53:00.0"), + toTimestamp.invoke(null, "2025-08-14 15:53:00.0") + ); + assertEquals( + Timestamp.valueOf("2025-08-14 15:53:00.0"), + toTimestamp.invoke(null, "2025-08-14T15:53:00") + ); + assertEquals( + Timestamp.from(Instant.parse("2025-08-14T15:53:00.000Z")), + toTimestamp.invoke(null, "2025-08-14T15:53:00.000Z") + ); + assertEquals( + Timestamp.from(Instant.parse("2025-08-14T15:53:00+00:00")), + toTimestamp.invoke(null, "2025-08-14T15:53:00+00:00") + ); + } +} From fa4328cbc0b3fe11e84627a5b0be4f807f28a2d7 Mon Sep 17 00:00:00 2001 From: Bora <223137607@ge.com> Date: Tue, 9 Sep 2025 15:43:36 -0400 Subject: [PATCH 2/4] Made timestamp to default to JVM timezone --- .../main/java/org/apache/calcite/avatica/AvaticaSite.java | 8 ++++++-- .../java/org/apache/calcite/avatica/AvaticaSiteTest.java | 4 ---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java index 1c5797ff2..fb424ed01 100644 --- a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java +++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java @@ -536,10 +536,14 @@ private static Timestamp toTimestamp(Object x) { if (x instanceof String) { String s = (String) x; try { - return Timestamp.from(Instant.parse(s)); + Instant instant = Instant.parse(s); + LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); + return Timestamp.valueOf(ldt); } catch (DateTimeParseException e) { try { - return Timestamp.from(OffsetDateTime.parse(s).toInstant()); + OffsetDateTime odt = OffsetDateTime.parse(s); + LocalDateTime ldt = odt.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); + return Timestamp.valueOf(ldt); } catch (DateTimeParseException e2) { try { return Timestamp.valueOf(LocalDateTime.parse(s)); diff --git a/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java b/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java index 0420f0040..f4cb5e5fb 100644 --- a/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java +++ b/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java @@ -67,9 +67,5 @@ public void testToTimestampIsoAndJdbc() throws Exception { Timestamp.from(Instant.parse("2025-08-14T15:53:00.000Z")), toTimestamp.invoke(null, "2025-08-14T15:53:00.000Z") ); - assertEquals( - Timestamp.from(Instant.parse("2025-08-14T15:53:00+00:00")), - toTimestamp.invoke(null, "2025-08-14T15:53:00+00:00") - ); } } From bcedb5958ed79482c72f0fe8050cbdbca26c8650 Mon Sep 17 00:00:00 2001 From: Bora <223137607@ge.com> Date: Wed, 10 Sep 2025 14:15:02 -0400 Subject: [PATCH 3/4] Enhanced methods to support ISO formsts: DateTimeFormatter for better code readability --- .../apache/calcite/avatica/AvaticaSite.java | 67 ++++++++++--------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java index fb424ed01..3854c396a 100644 --- a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java +++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java @@ -38,6 +38,7 @@ import java.sql.Timestamp; import java.sql.Types; import java.time.*; +import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Calendar; @@ -504,14 +505,21 @@ private static Date toDate(Object x) { if (x instanceof String) { String s = (String) x; try { - return Date.valueOf(LocalDate.parse(s)); - } catch (DateTimeParseException e) { - try { - return Date.valueOf(OffsetDateTime.parse(s).toLocalDate()); - } catch (DateTimeParseException e3) { - return Date.valueOf(s); - } - } + LocalDate d = LocalDate.parse(s, DateTimeFormatter.ISO_LOCAL_DATE); + return Date.valueOf(d); + } catch (DateTimeParseException ignored) { } + try { + OffsetDateTime odt = OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME); + return Date.valueOf(odt.toLocalDate()); + } catch (DateTimeParseException ignored) { } + try { + Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(s)); + LocalDate d = instant.atZone(ZoneId.systemDefault()).toLocalDate(); + return Date.valueOf(d); + } catch (DateTimeParseException | IllegalArgumentException ignored) { } + try { + return Date.valueOf(s); + } catch (IllegalArgumentException ignored) { } } return new Date(toLong(x)); } @@ -520,14 +528,14 @@ private static Time toTime(Object x) { if (x instanceof String) { String s = (String) x; try { - return Time.valueOf(LocalTime.parse(s)); - } catch (DateTimeParseException e) { - try { - return Time.valueOf(OffsetTime.parse(s).toLocalTime()); - } catch (DateTimeParseException e2) { - return Time.valueOf(s); - } - } + OffsetTime ot = OffsetTime.parse(s, DateTimeFormatter.ISO_OFFSET_TIME); + return Time.valueOf(ot.toLocalTime()); + } catch (DateTimeParseException ignored) { } + try { + LocalTime lt = LocalTime.parse(s, DateTimeFormatter.ISO_LOCAL_TIME); + return Time.valueOf(lt); + } catch (DateTimeParseException ignored) { } + return Time.valueOf(s); } return new Time(toLong(x)); } @@ -536,22 +544,17 @@ private static Timestamp toTimestamp(Object x) { if (x instanceof String) { String s = (String) x; try { - Instant instant = Instant.parse(s); - LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); - return Timestamp.valueOf(ldt); - } catch (DateTimeParseException e) { - try { - OffsetDateTime odt = OffsetDateTime.parse(s); - LocalDateTime ldt = odt.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); - return Timestamp.valueOf(ldt); - } catch (DateTimeParseException e2) { - try { - return Timestamp.valueOf(LocalDateTime.parse(s)); - } catch (DateTimeParseException e3) { - return Timestamp.valueOf(s); - } - } - } + ZonedDateTime zdt = ZonedDateTime.parse(s, DateTimeFormatter.ISO_ZONED_DATE_TIME); + return Timestamp.from(zdt.toInstant()); + } catch (DateTimeParseException ignored) { } + try { + OffsetDateTime odt = OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME); + return Timestamp.from(odt.toInstant()); + } catch (DateTimeParseException ignored) { } + try { + return Timestamp.valueOf(LocalDateTime.parse(s)); + } catch (DateTimeParseException ignored) { } + return Timestamp.valueOf(s); } return new Timestamp(toLong(x)); } From c9b8a3676a8d1fb5c0e4f2eaa433337ae9119cfe Mon Sep 17 00:00:00 2001 From: Bora <223137607@ge.com> Date: Wed, 10 Sep 2025 14:43:19 -0400 Subject: [PATCH 4/4] Improvement on readability --- .../org/apache/calcite/avatica/AvaticaSite.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java index 3854c396a..198fe03ce 100644 --- a/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java +++ b/core/src/main/java/org/apache/calcite/avatica/AvaticaSite.java @@ -504,22 +504,24 @@ private static byte[] toBytes(Object x) { private static Date toDate(Object x) { if (x instanceof String) { String s = (String) x; + try { LocalDate d = LocalDate.parse(s, DateTimeFormatter.ISO_LOCAL_DATE); return Date.valueOf(d); } catch (DateTimeParseException ignored) { } + try { OffsetDateTime odt = OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME); return Date.valueOf(odt.toLocalDate()); } catch (DateTimeParseException ignored) { } + try { Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse(s)); LocalDate d = instant.atZone(ZoneId.systemDefault()).toLocalDate(); return Date.valueOf(d); } catch (DateTimeParseException | IllegalArgumentException ignored) { } - try { - return Date.valueOf(s); - } catch (IllegalArgumentException ignored) { } + + return Date.valueOf(s); } return new Date(toLong(x)); } @@ -531,10 +533,12 @@ private static Time toTime(Object x) { OffsetTime ot = OffsetTime.parse(s, DateTimeFormatter.ISO_OFFSET_TIME); return Time.valueOf(ot.toLocalTime()); } catch (DateTimeParseException ignored) { } + try { LocalTime lt = LocalTime.parse(s, DateTimeFormatter.ISO_LOCAL_TIME); return Time.valueOf(lt); } catch (DateTimeParseException ignored) { } + return Time.valueOf(s); } return new Time(toLong(x)); @@ -547,13 +551,16 @@ private static Timestamp toTimestamp(Object x) { ZonedDateTime zdt = ZonedDateTime.parse(s, DateTimeFormatter.ISO_ZONED_DATE_TIME); return Timestamp.from(zdt.toInstant()); } catch (DateTimeParseException ignored) { } + try { OffsetDateTime odt = OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME); return Timestamp.from(odt.toInstant()); } catch (DateTimeParseException ignored) { } + try { return Timestamp.valueOf(LocalDateTime.parse(s)); } catch (DateTimeParseException ignored) { } + return Timestamp.valueOf(s); } return new Timestamp(toLong(x));