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..198fe03ce 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,9 @@ import java.sql.Time; 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; /** @@ -500,21 +503,65 @@ 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 { + 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) { } + + 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 { + 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)); } private static Timestamp toTimestamp(Object x) { if (x instanceof String) { - return Timestamp.valueOf((String) x); + String s = (String) x; + try { + 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)); } 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..f4cb5e5fb --- /dev/null +++ b/core/src/test/java/org/apache/calcite/avatica/AvaticaSiteTest.java @@ -0,0 +1,71 @@ +/* + * 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") + ); + } +}