tindzk commented on code in PR #3687:
URL: https://github.com/apache/calcite/pull/3687#discussion_r1502371721
##########
core/src/main/java/org/apache/calcite/adapter/enumerable/RexToLixTranslator.java:
##########
@@ -1374,11 +1374,35 @@ private Result toInnerStorageType(Result result, Type
storageType) {
}
final Type storageType = currentStorageType != null
? currentStorageType :
typeFactory.getJavaClass(dynamicParam.getType());
- final Expression valueExpression =
+
+ // For numeric types, get the value using the following functions on the
Review Comment:
Thanks for the suggestion! It greatly simplifies the fix.
I made the requested change, but kept the null check as the cast operation
may fail. For instance, `o.intValue()` requires `o` to be non-null. Otherwise,
`testPreparedStatement` would fail.
As for additional tests, do you have in mind assigning a non-numeric value
to a numeric value? These would still raise an exception:
```java
@Test void bindStringParameter() {
for (SqlTypeName tpe : SqlTypeName.INT_TYPES) {
final String sql =
"with cte as (select cast(100 as " + tpe.getName() + ") as empid)"
+ "select * from cte where empid = ?";
CalciteAssert.hr()
.query(sql)
.consumesPreparedStatement(p -> {
p.setString(1, "100");
})
.returnsUnordered("EMPID=100");
}
}
```
This throws a `ClassCastException` which we could rewrite into a more
user-friendly message. Alternatively, we could prevent the conversion happening
in the first place by introducing a validation check.
```
java.lang.ClassCastException: class java.lang.String cannot be cast to class
java.lang.Number
```
--
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]