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 Mar 27, 2024
1 parent d5b6b5c commit 58c6e31
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 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 @@ -206,6 +206,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
26 changes: 11 additions & 15 deletions testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ void testCastStringToDateTime(CastType castType, SqlOperatorFixture f) {
"1945-02-24 12:42:25.34", "TIMESTAMP(2) NOT NULL");
}
// Remove the if condition and the else block once CALCITE-6053 is fixed
if (TestUtil.AVATICA_VERSION.startsWith("1.0.0-dev-main")) {
if (Bug.CALCITE_6053_FIXED) {
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 All @@ -1295,17 +1295,6 @@ void testCastStringToDateTime(CastType castType, SqlOperatorFixture f) {
f.checkNull("cast('1945-01-24 25:42:25.34' as TIMESTAMP)");
f.checkNull("cast('1945-1-24 12:23:34.454' as TIMESTAMP)");
}
} else {
f.checkScalar("cast('1945-2-2 12:2:5' as TIMESTAMP)",
"1945-02-02 12:02:05", "TIMESTAMP(0) NOT NULL");
f.checkScalar("cast('1241241' as TIMESTAMP)",
"1241-01-01 00:00:00", "TIMESTAMP(0) NOT NULL");
f.checkScalar("cast('1945-20-24 12:42:25.34' as TIMESTAMP)",
"1946-08-26 12:42:25", "TIMESTAMP(0) NOT NULL");
f.checkScalar("cast('1945-01-24 25:42:25.34' as TIMESTAMP)",
"1945-01-25 01:42:25", "TIMESTAMP(0) NOT NULL");
f.checkScalar("cast('1945-1-24 12:23:34.454' as TIMESTAMP)",
"1945-01-24 12:23:34", "TIMESTAMP(0) NOT NULL");
}
if (castType == CastType.CAST) {
f.checkFails("cast('nottime' as TIMESTAMP)", BAD_DATETIME_MESSAGE, true);
Expand All @@ -1318,7 +1307,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 @@ -1327,8 +1316,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 58c6e31

Please sign in to comment.