Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-3979

Simplification might have removed CAST expression(s) incorrectly

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 1.22.0
    • 1.23.0
    • core

    Description

      @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();
      }
      

      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:

      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)
      

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            kgyrtkirk Zoltan Haindrich
            icshuo Shuo Cheng
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 40m
                40m

                Slack

                  Issue deployment