rubenada commented on a change in pull request #2555:
URL: https://github.com/apache/calcite/pull/2555#discussion_r718695171
##########
File path:
core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
##########
@@ -1595,6 +1598,17 @@ protected static Calendar getCalendarNotTooNear(int
timeUnit) {
tester.checkFails(
"cast(cast('blah' as varchar(10)) as boolean)", INVALID_CHAR_MESSAGE,
true);
+
+ tester.checkBoolean("cast(0 as boolean)", false);
Review comment:
For consistency, it should be `Boolean.FALSE` instead of `false`
##########
File path:
core/src/main/java/org/apache/calcite/sql/type/SqlTypeCoercionRule.java
##########
@@ -202,7 +202,8 @@ private SqlTypeCoercionRule(Map<SqlTypeName,
ImmutableSet<SqlTypeName>> map) {
coerceRules.copyValues(SqlTypeName.BOOLEAN)
.add(SqlTypeName.CHAR)
.add(SqlTypeName.VARCHAR)
- .addAll(SqlTypeName.NUMERIC_TYPES)
+ .addAll(SqlTypeName.INT_TYPES)
+ .addAll(SqlTypeName.APPROX_TYPES)
Review comment:
`SqlTypeName.APPROX_TYPES` include `FLOAT`, `REAL` and `DOUBLE`. Are we
sure casting these types into boolean always work?
It's strange, it seems that `SELECT CAST(CAST(0 AS float) AS BOOLEAN)` works
fine (returns `false`). But if we try a more "complex" query: `select
cast(cast(p0 as float) as boolean) from (values (0)) as t(p0)` ; it will fail
at runtime with `CompileException: Line 21, Column 31: Cannot cast "double" to
"boolean"`.
This type of query will be generated if we add these tests into
`SqlOperatorBaseTest#testCastToBoolean`:
```
tester.checkBoolean("cast(cast(0 as float) as boolean)", Boolean.FALSE);
tester.checkBoolean("cast(cast(0 as real) as boolean)", Boolean.FALSE);
tester.checkBoolean("cast(cast(0 as double) as boolean)", Boolean.FALSE);
```
which, if I am not mistaken, will fail if we add them to
`SqlOperatorBaseTest#testCastToBoolean`. Therefore, perhaps we should consider
`FLOAT`, `REAL` and `DOUBLE` (together with `DECIMAL`) as non-castable into
boolean?
##########
File path:
core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
##########
@@ -1595,6 +1598,17 @@ protected static Calendar getCalendarNotTooNear(int
timeUnit) {
tester.checkFails(
"cast(cast('blah' as varchar(10)) as boolean)", INVALID_CHAR_MESSAGE,
true);
+
+ tester.checkBoolean("cast(0 as boolean)", false);
+
Review comment:
Perhaps we could add also the test for the "other int types"?
```
tester.checkBoolean("cast(cast(0 as tinyint) as boolean)", Boolean.FALSE);
tester.checkBoolean("cast(cast(0 as smallint) as boolean)", Boolean.FALSE);
tester.checkBoolean("cast(cast(0 as bigint) as boolean)", Boolean.FALSE);
```
--
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]