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

Query with measure that applies AVG to SMALLINT throws AssertionError "Cannot add expression of different type to set"

    XMLWordPrintableJSON

Details

    Description

      LogicalCorrelate fails to produce the correct Type when decorrelating AS MEASURE queries

      Test case:

       

      # dummy.iq - Scratch space
      # ...
      !set outputformat mysql
      !use scott
      
      WITH tbl_with_null_dim AS (
          SELECT e.deptno, e.grade, AVG(e.grade) AS MEASURE avg_grade
          FROM (VALUES (1, 70),
                   (1, 50),
                   (NULL, 50),
                   ( 3, 82)) AS e(deptno, grade)
      )
      
      SELECT deptno, avg_grade FROM tbl_with_null_dim
      GROUP BY deptno;
      +--------+-----------+
      | DEPTNO | AVG_GRADE |
      +--------+-----------+
      |      1 |        60 |
      |      3 |        82 |
      |        |        50 |
      +--------+-----------+
      (3 rows)
      
      !ok
      
      # End dummy.iq 

      Error Message:

       

      > java.lang.AssertionError: Cannot add expression of different type to set:
      > set type is RecordType(INTEGER DEPTNO, MEASURE<INTEGER NOT NULL> NOT NULL $f1, INTEGER $f0) NOT NULL
      > expression type is RecordType(INTEGER DEPTNO, MEASURE<INTEGER NOT NULL> NOT NULL $f1, INTEGER NOT NULL $f0) NOT NULL
      > set is rel#86:LogicalCorrelate.NONE.[](left=HepRelVertex#78,right=HepRelVertex#85,correlation=$cor0,joinType=left,requiredColumns={0})
      > expression is LogicalProject(DEPTNO=[$0], $f1=[$1], $f0=[CAST(/($2, CASE(IS NULL($3), 0:BIGINT, $3))):INTEGER NOT NULL])
      >   LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{0}])
      >     LogicalAggregate(group=[{0}], agg#0=[AGG_M2M($1)])
      >       LogicalProject(DEPTNO=[$0], AVG_GRADE=[V2M(CAST(/(SUM($1), COUNT($1))):INTEGER NOT NULL)])
      >         LogicalValues(tuples=[[{ 1, 70 }, { 1, 50 }, { null, 50 }, { 3, 82 }]])
      >     LogicalAggregate(group=[{}], agg#0=[SUM($1)], agg#1=[COUNT()])
      >       LogicalFilter(condition=[=($0, $cor0.DEPTNO)])
      >         LogicalValues(tuples=[[{ 1, 70 }, { 1, 50 }, { null, 50 }, { 3, 82 }]])
      > Type mismatch:
      > rowtype of original rel: RecordType(INTEGER DEPTNO, MEASURE<INTEGER NOT NULL> NOT NULL $f1, INTEGER $f0) NOT NULL
      > rowtype of new rel: RecordType(INTEGER DEPTNO, MEASURE<INTEGER NOT NULL> NOT NULL $f1, INTEGER NOT NULL $f0) NOT NULL
      > Difference:
      > $f0: INTEGER -> INTEGER NOT NULL 

      Stack Trace:

       

      >     at org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:432)
      >     at org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
      >     at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:273)
      >     at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:288)
      >     at org.apache.calcite.sql2rel.RelDecorrelator$AdjustProjectForCountAggregateRule.onMatch2(RelDecorrelator.java:2709)
      >     at org.apache.calcite.sql2rel.RelDecorrelator$AdjustProjectForCountAggregateRule.onMatch(RelDecorrelator.java:2616)
      >     at org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:337)
      >     at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:556)
      >     at org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:420)
      >     at org.apache.calcite.plan.hep.HepPlanner.executeRuleInstance(HepPlanner.java:243)
      >     at org.apache.calcite.plan.hep.HepInstruction$RuleInstance$State.execute(HepInstruction.java:178)
      >     at org.apache.calcite.plan.hep.HepPlanner.lambda$executeProgram$0(HepPlanner.java:211)
      >     at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:422)
      >     at org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:210)
      >     at org.apache.calcite.plan.hep.HepProgram$State.execute(HepProgram.java:118)
      >     at org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:205)
      >     at org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:191)
      >     at org.apache.calcite.sql2rel.RelDecorrelator.decorrelate(RelDecorrelator.java:291)
      >     at org.apache.calcite.sql2rel.RelDecorrelator.decorrelateQuery(RelDecorrelator.java:230)
      >     at org.apache.calcite.tools.Programs$DecorrelateProgram.run(Programs.java:420)
      >     at org.apache.calcite.tools.Programs$SequenceProgram.run(Programs.java:373)
      >     at org.apache.calcite.tools.Programs$ConditionalProgram.run(Programs.java:395)
      >     at org.apache.calcite.tools.Programs$SequenceProgram.run(Programs.java:373)
      >     at org.apache.calcite.prepare.Prepare.optimize(Prepare.java:177)
      >     at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:314)
      >     at org.apache.calcite.prepare.Prepare.prepareSql(Prepare.java:220)
      >     at org.apache.calcite.prepare.CalcitePrepareImpl.prepare2_(CalcitePrepareImpl.java:673)
      >     at org.apache.calcite.prepare.CalcitePrepareImpl.prepare_(CalcitePrepareImpl.java:524)
      >     at org.apache.calcite.prepare.CalcitePrepareImpl.prepareSql(CalcitePrepareImpl.java:492)
      >     at org.apache.calcite.jdbc.CalciteConnectionImpl.parseQuery(CalciteConnectionImpl.java:237)
      >     at org.apache.calcite.jdbc.CalciteMetaImpl.prepareAndExecute(CalciteMetaImpl.java:702)
      >     at org.apache.calcite.avatica.AvaticaConnection.prepareAndExecuteInternal(AvaticaConnection.java:677)
      >     at org.apache.calcite.avatica.AvaticaStatement.executeInternal(AvaticaStatement.java:157)
      >     at org.apache.calcite.avatica.AvaticaStatement.executeQuery(AvaticaStatement.java:228)
      >     at net.hydromatic.quidem.Quidem.checkResult(Quidem.java:299)
      >     at net.hydromatic.quidem.Quidem.access$2600(Quidem.java:57)
      >     at net.hydromatic.quidem.Quidem$ContextImpl.checkResult(Quidem.java:1636)
      >     at net.hydromatic.quidem.Quidem$CheckResultCommand.execute(Quidem.java:941)
      >     at net.hydromatic.quidem.Quidem$CompositeCommand.execute(Quidem.java:1437)
      >     at net.hydromatic.quidem.Quidem.execute(Quidem.java:190)
      >     at org.apache.calcite.test.QuidemTest.checkRun(QuidemTest.java:178)
      >     at org.apache.calcite.test.QuidemTest.test(QuidemTest.java:239)
      >     at org.apache.calcite.test.CoreQuidemTest.main(CoreQuidemTest.java:42)
      
          at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:38)
          at org.junit.jupiter.api.Assertions.fail(Assertions.java:135)
          at org.apache.calcite.test.QuidemTest.checkRun(QuidemTest.java:186)
          at org.apache.calcite.test.QuidemTest.test(QuidemTest.java:239)
          at org.apache.calcite.test.CoreQuidemTest.main(CoreQuidemTest.java:42) 

       

       

      Attachments

        Issue Links

          Activity

            People

              julianhyde Julian Hyde
              amarkowitz Adam Markowitz
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: