herunkang2018 commented on code in PR #3364:
URL: https://github.com/apache/calcite/pull/3364#discussion_r1359843557
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -1253,15 +1254,28 @@ void testCastStringToDateTime(CastType castType,
SqlOperatorFixture f) {
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 (Bug.CALCITE_5678_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);
+ 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)");
+ }
Review Comment:
I think `f.checkCastFails` cannot be used to refactor the code. The reason
is that`f.checkCastFails` can only check exceptions, but 'SAFE_CAST' and
'TRY_CAST' will return `NULL` instead of throwing exception if the input is not
a valid value when interpreted as the desired type.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]