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

RelDecorrelator's RemoveSingleAggregateRule can produce result with wrong row type

    XMLWordPrintableJSON

Details

    Description

      The problem can be reproduced with the following test (to be added to unnest.iq):

      SELECT unnested_outer.val, COUNT(1) AS count_val
      FROM
      (
          SELECT *
          FROM (VALUES (1, array [3, 5]), (2, array [6, 4, 6]))
          AS u(x, y)
      ) AS t, UNNEST(t.y) AS unnested_outer(val)
      WHERE
      (
          SELECT COUNT(unnested_inner.val) > 0
          FROM UNNEST(t.y) AS unnested_inner(val)
          WHERE unnested_inner.val = 4
      )
      GROUP BY unnested_outer.val
      ORDER BY count_val DESC, unnested_outer.val ASC;
      +-----+-----------+
      | VAL | COUNT_VAL |
      +-----+-----------+
      |   6 |         2 |
      |   4 |         1 |
      +-----+-----------+
      (2 rows)
      
      !ok
      

      When the test is executed, it fails with the following error:

      > java.lang.AssertionError: Cannot add expression of different type to set:
      > set type is RecordType(BOOLEAN NOT NULL $f0) NOT NULL
      > expression type is RecordType(BOOLEAN $f0) NOT NULL
      > set is rel#24436:LogicalAggregate.NONE.[](input=HepRelVertex#24435,group={},agg#0=SINGLE_VALUE($0))
      > expression is LogicalProject($f0=[CAST(>($0, 0)):BOOLEAN])
      >   LogicalAggregate(group=[{}], agg#0=[COUNT()])
      >     LogicalFilter(condition=[=($0, 4)])
      >       LogicalProject(VAL=[$0])
      >         Uncollect
      >           LogicalProject(Y=[$cor1.Y])
      >             LogicalValues(tuples=[[{ 0 }]])
      249a254,325
      > 	at org.apache.calcite.plan.RelOptUtil.verifyTypeEquivalence(RelOptUtil.java:391)
      > 	at org.apache.calcite.plan.hep.HepRuleCall.transformTo(HepRuleCall.java:60)
      > 	at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:269)
      > 	at org.apache.calcite.plan.RelOptRuleCall.transformTo(RelOptRuleCall.java:284)
      > 	at org.apache.calcite.sql2rel.RelDecorrelator$RemoveSingleAggregateRule.onMatch(RelDecorrelator.java:1923)
      > 	at org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:341)
      > 	at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:565)
      > 	at org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:428)
      > 	at org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:251)
      > 	at org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:130)
      > 	at org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:208)
      > 	at org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:195)
      > 	at org.apache.calcite.sql2rel.RelDecorrelator.removeCorrelationViaRule(RelDecorrelator.java:378)
      > 	at org.apache.calcite.sql2rel.RelDecorrelator.decorrelateQuery(RelDecorrelator.java:221)
      ...
      

      The root cause seems to be in this piece of code inside RelDecorrelator's RemoveSingleAggregateRule#onMatch:

      // singleAggRel produces a nullable type, so create the new
      // projection that casts proj expr to a nullable type.
      final RelBuilder relBuilder = call.builder();
      final RelDataType type =
        relBuilder.getTypeFactory()
      	  .createTypeWithNullability(projExprs.get(0).getType(), true);
      final RexNode cast = relBuilder.getRexBuilder().makeCast(type, projExprs.get(0));
      relBuilder.push(aggregate).project(cast);
      call.transformTo(relBuilder.build());
      

      Note that the comment assumes that "singleAggRel produces a nullable type", but in this particular case, it seems to produce a non-nullable type, so probably this piece of code needs to be adapted.

      Attachments

        Issue Links

          Activity

            People

              rubenql Ruben Q L
              rubenql Ruben Q L
              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 - 0.5h
                  0.5h