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 fb3da2df549..60f37808f51 100644 --- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java +++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java @@ -1255,18 +1255,27 @@ void testCastStringToDateTime(CastType castType, SqlOperatorFixture f) { "1945-02-24 12:42:25.34", "TIMESTAMP(2) NOT NULL"); } if (Bug.CALCITE_5678_FIXED) { - f.checkFails("cast('1945-2-2 12:2:5' as TIMESTAMP)", - "Invalid DATE value, '1945-2-2 12:2:5'", true); + 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); + f.checkFails("cast('1241241' as TIMESTAMP)", + "Invalid DATE value, '1241241'", true); + f.checkFails("cast('1945-20-24 12:42:25.34' as TIMESTAMP)", + "Invalid DATE value, '1945-20-24 12:42:25.34'", true); + f.checkFails("cast('1945-01-24 25:42:25.34' as TIMESTAMP)", + "Value of HOUR field is out of range in '1945-01-24 25:42:25.34'", true); + f.checkFails("cast('1945-1-24 12:23:34.454' as TIMESTAMP)", + "Invalid DATE value, '1945-1-24 12:23:34.454'", true); + } else { + // test cases for 'SAFE_CAST' and 'TRY_CAST' + f.checkNull("cast('1945-2-2 12:2:5' as TIMESTAMP)"); + f.checkNull("cast('1241241' as TIMESTAMP)"); + f.checkNull("cast('1945-20-24 12:42:25.34' as TIMESTAMP)"); + f.checkNull("cast('1945-01-24 25:42:25.34' as TIMESTAMP)"); + f.checkNull("cast('1945-1-24 12:23:34.454' as TIMESTAMP)"); + } } f.checkFails("cast('nottime' as TIMESTAMP)", BAD_DATETIME_MESSAGE, true); - 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"); // date <-> string f.checkCastToString("DATE '1945-02-24'", null, "1945-02-24", castType);