Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-6248] Illegal dates are accepted by casts #3708

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add new unit tests instead of changing existing test cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afraid we have to change the tests because they are wrong.

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
Loading