DonnyZone commented on a change in pull request #1381: [CALCITE-3245]
CompileException in Janino when a query contains a division between a Double
and a BigDecimal
URL: https://github.com/apache/calcite/pull/1381#discussion_r322057592
##########
File path:
core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
##########
@@ -2278,9 +2278,12 @@ public Expression implement(
final Type returnType =
translator.typeFactory.getJavaClass(call.getType());
- return Types.castIfNecessary(returnType,
- Expressions.makeBinary(expressionType, expressions.get(0),
- expressions.get(1)));
+ final Expression result = Expressions.makeBinary(expressionType,
+ expressions.get(0), expressions.get(1));
+ if (returnType == BigDecimal.class) {
+ return RexToLixTranslator.convert(result, returnType);
+ }
+ return Types.castIfNecessary(returnType, result);
}
Review comment:
Hi, @danny0405, I have updated the PR based on your recent changes.
There are some problems when move the following logic from
`RexToLixTranslator.convert` into `Types.castIfNecessary`.
(1) It relies on SqlFunctions, which is impossible to call it in Linq4j.
(2) Make the logic duplicate.
Do you have some suggestions to handle it? Maybe we can unify them?
```
else if (toType == BigDecimal.class) {
if (fromBox != null) {
// E.g. from "Integer" to "BigDecimal".
// Generate "x == null ? null : new BigDecimal(x.intValue())"
return Expressions.condition(
Expressions.equal(operand, RexImpTable.NULL_EXPR),
RexImpTable.NULL_EXPR,
Expressions.new_(
BigDecimal.class,
Expressions.unbox(operand, fromBox)));
}
if (fromPrimitive != null) {
// E.g. from "int" to "BigDecimal".
// Generate "new BigDecimal(x)"
// Fix CALCITE-2325, we should decide null here for int type.
return Expressions.condition(
Expressions.equal(operand, RexImpTable.NULL_EXPR),
RexImpTable.NULL_EXPR,
Expressions.new_(
BigDecimal.class,
operand));
}
// E.g. from "Object" to "BigDecimal".
// Generate "x == null ? null : SqlFunctions.toBigDecimal(x)"
return Expressions.condition(
Expressions.equal(operand, RexImpTable.NULL_EXPR),
RexImpTable.NULL_EXPR,
Expressions.call(
SqlFunctions.class,
"toBigDecimal",
operand));
}
```
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services