Skip to content

Commit

Permalink
[CALCITE-6248] Illegal dates are accepted by casts
Browse files Browse the repository at this point in the history
Signed-off-by: Mihai Budiu <[email protected]>
  • Loading branch information
mihaibudiu committed Apr 1, 2024
1 parent 0d21496 commit ef81185
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
12 changes: 12 additions & 0 deletions core/src/main/java/org/apache/calcite/util/Bug.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <a href="https://issues.apache.org/jira/browse/CALCITE-6053">[CALCITE-6053]
* Upgrade Calcite to Avatica 1.24.0</a> is fixed.
*/
public static final boolean CALCITE_6053_FIXED = false;

/** Whether
* <a href="https://issues.apache.org/jira/browse/CALCITE-6248">[CALCITE-6248]
* Illegal dates are accepted by casts</a> 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.
*/
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/apache/calcite/test/JdbcTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8256,7 +8256,7 @@ private void checkGetTimestamp(Connection con) throws SQLException {
* TIMESTAMP elements</a>. */
@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");
}

Expand Down
14 changes: 10 additions & 4 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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)");
Expand Down

0 comments on commit ef81185

Please sign in to comment.