[
https://issues.apache.org/jira/browse/CALCITE-3979?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17103696#comment-17103696
]
Zoltan Haindrich commented on CALCITE-3979:
-------------------------------------------
I've fixed this; but I see another issue with the simplified version.
for the above query the runtime *may* throw an exception when it tries to cast
the varchar into a double
The new issue I see now is that the explicit cast is being removed and a >($1,
5) is created in which $1 is a varchar - I think that this is just as bad for
the runtime as an explicit casted one; however in this case I feel that the
RexNode is somewhat invalid; it has incompatible left/right type families in
the comparision....
After some digging it turned out that RER is doing some changes to remove
CAST(CAST(x,T),T) like casts; which had it's time when RexSimplify was not
handling that kind of things - but right now ; the replacement may also change
the inner cast type...instead of fixing this I think we can just switch off
this feature in RER and let RexSimplify handle it.
> Unexpected removing of CAST in ReduceExpressionsRule
> ----------------------------------------------------
>
> Key: CALCITE-3979
> URL: https://issues.apache.org/jira/browse/CALCITE-3979
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.22.0
> Reporter: Shuo Cheng
> Priority: Major
> Fix For: 1.23.0
>
>
> {code:java}
> @Test public void testCastRemove() throws Exception {
> final String sql = "select\n" +
> "case when cast(ename as double) < 5 then 0.0\n" +
> " else coalesce(cast(ename as double), 1.0)\n" +
> " end as t\n" +
> " from (\n" +
> " select\n" +
> " case when ename > 'abc' then ename\n" +
> " else null\n" +
> " end as ename from emp\n" +
> " )";
> sql(sql).withRule(ReduceExpressionsRule.PROJECT_INSTANCE).check();
> }
> {code}
> As shown in the above test case, `cast(ename as double) < 5` was reduced as
> `ename < 5` when reducing expression, which lead to the following exception:
> {code:java}
> java.math.BigDecimal cannot be cast to
> org.apache.calcite.util.NlsStringjava.math.BigDecimal cannot be cast to
> org.apache.calcite.util.NlsStringjava.lang.ClassCastException:
> java.math.BigDecimal cannot be cast to org.apache.calcite.util.NlsString at
> org.apache.calcite.util.NlsString.compareTo(NlsString.java:47) at
> com.google.common.collect.Range.compareOrThrow(Range.java:672) at
> com.google.common.collect.Cut.compareTo(Cut.java:79) at
> com.google.common.collect.Range.isConnected(Range.java:526) at
> org.apache.calcite.rex.RexSimplify.residue(RexSimplify.java:1702) at
> org.apache.calcite.rex.RexSimplify.simplifyUsingPredicates(RexSimplify.java:1636)
> at
> org.apache.calcite.rex.RexSimplify.simplifyComparison(RexSimplify.java:451)
> at
> org.apache.calcite.rex.RexSimplify.simplifyComparison(RexSimplify.java:321)
> at org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:292) at
> org.apache.calcite.rex.RexSimplify.simplifyAndTerms(RexSimplify.java:492) at
> org.apache.calcite.rex.RexSimplify.simplifyAnd(RexSimplify.java:1275) at
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:264) at
> org.apache.calcite.rex.RexSimplify.simplifyCase(RexSimplify.java:992) at
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:270) at
> org.apache.calcite.rex.RexSimplify.simplifyCase(RexSimplify.java:926) at
> org.apache.calcite.rex.RexSimplify.simplify(RexSimplify.java:270) at
> org.apache.calcite.rex.RexSimplify.simplifyUnknownAs(RexSimplify.java:235) at
> org.apache.calcite.rex.RexSimplify.simplifyPreservingType(RexSimplify.java:174)
> at
> org.apache.calcite.rel.rules.ReduceExpressionsRule.reduceExpressions(ReduceExpressionsRule.java:629)
> at
> org.apache.calcite.rel.rules.ReduceExpressionsRule$ProjectReduceExpressionsRule.onMatch(ReduceExpressionsRule.java:305)
> at
> org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:338)
> {code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)