From ef8118520e8e3ded29e05aa32cba1215570e16d5 Mon Sep 17 00:00:00 2001 From: Mihai Budiu Date: Tue, 26 Mar 2024 20:33:33 -0500 Subject: [PATCH] [CALCITE-6248] Illegal dates are accepted by casts Signed-off-by: Mihai Budiu --- .../src/main/java/org/apache/calcite/util/Bug.java | 12 ++++++++++++ .../java/org/apache/calcite/test/JdbcTest.java | 2 +- .../org/apache/calcite/test/SqlOperatorTest.java | 14 ++++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/util/Bug.java b/core/src/main/java/org/apache/calcite/util/Bug.java index c9a57d4d4af6..84c16420a051 100644 --- a/core/src/main/java/org/apache/calcite/util/Bug.java +++ b/core/src/main/java/org/apache/calcite/util/Bug.java @@ -200,6 +200,18 @@ public abstract class Bug { * Fix to be available with Avatica 1.24.0 [CALCITE-6053] */ public static final boolean CALCITE_6092_FIXED = false; + /** Whether + * [CALCITE-6053] + * Upgrade Calcite to Avatica 1.24.0 is fixed. + */ + public static final boolean CALCITE_6053_FIXED = false; + + /** Whether + * [CALCITE-6248] + * Illegal dates are accepted by casts is fixed. + * Fix to be available with Avatica 1.25.0 */ + public static final boolean CALCITE_6248_FIXED = false; + /** * Use this to flag temporary code. */ diff --git a/core/src/test/java/org/apache/calcite/test/JdbcTest.java b/core/src/test/java/org/apache/calcite/test/JdbcTest.java index af4bf13ae897..087b3854d13d 100644 --- a/core/src/test/java/org/apache/calcite/test/JdbcTest.java +++ b/core/src/test/java/org/apache/calcite/test/JdbcTest.java @@ -8256,7 +8256,7 @@ private void checkGetTimestamp(Connection con) throws SQLException { * TIMESTAMP elements. */ @Test void testArrayOfDates() { CalciteAssert.that() - .query("select array[cast('1900-01-01' as date)]") + .query("select array[cast('1900-1-1' as date)]") .returns("EXPR$0=[1900-01-01]\n"); } diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java index 7c9fef60c0da..69523906db6c 100644 --- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java +++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java @@ -1273,7 +1273,6 @@ void testCastStringToDateTime(CastType castType, SqlOperatorFixture f) { if (Bug.CALCITE_6282_FIXED) { f.checkScalar("cast('1945-02-24 12:42:25.34' as TIMESTAMP(2))", "1945-02-24 12:42:25.34", "TIMESTAMP(2) NOT NULL"); - if (castType == CastType.CAST) { f.checkFails("cast('1945-2-2 12:2:5' as TIMESTAMP)", "Invalid DATE value, '1945-2-2 12:2:5'", true); @@ -1305,7 +1304,7 @@ void testCastStringToDateTime(CastType castType, SqlOperatorFixture f) { f.checkCastToString("DATE '1945-2-24'", null, "1945-02-24", castType); f.checkScalar("cast('1945-02-24' as DATE)", "1945-02-24", "DATE NOT NULL"); - f.checkScalar("cast(' 1945-02-04 ' as DATE)", "1945-02-04", "DATE NOT NULL"); + f.checkScalar("cast(' 1945-2-4 ' as DATE)", "1945-02-04", "DATE NOT NULL"); f.checkScalar("cast(' 1945-02-24 ' as DATE)", "1945-02-24", "DATE NOT NULL"); if (castType == CastType.CAST) { @@ -1314,8 +1313,15 @@ void testCastStringToDateTime(CastType castType, SqlOperatorFixture f) { f.checkNull("cast('notdate' as DATE)"); } - f.checkScalar("cast('52534253' as DATE)", "7368-10-13", "DATE NOT NULL"); - f.checkScalar("cast('1945-30-24' as DATE)", "1947-06-26", "DATE NOT NULL"); + if (Bug.CALCITE_6248_FIXED) { + if (castType == CastType.CAST) { + f.checkFails("cast('52534253' as DATE)", BAD_DATETIME_MESSAGE, true); + f.checkFails("cast('1945-30-24' as DATE)", BAD_DATETIME_MESSAGE, true); + } else { + f.checkNull("cast('52534253' as DATE)"); + f.checkNull("cast('1945-30-24' as DATE)"); + } + } // cast null f.checkNull("cast(null as date)");