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

Check input type in AggregateUnionAggregateRule when remove the bottom Aggregate

    XMLWordPrintableJSON

Details

    Description

      In AggregateUnionAggregateRule, we may remove the bottom Aggregate and use it's input as the new input of Union directly. However, the bottom Aggregate and it's input may not share the same row type. As Union requires all inputs with a same row type, once we remove the bottom Aggregate, an exception will be thrown.

      The exception can be reproduced by the following test:

        @Test public void testPullAggregateThroughUnion() {
          HepProgram program = new HepProgramBuilder()
              .addRuleInstance(AggregateProjectMergeRule.INSTANCE)
              .addRuleInstance(AggregateUnionAggregateRule.INSTANCE)
              .build();
      
          final String sql = "select job, deptno from"
                  + " (select job, deptno from emp as e1"
                  + " group by job, deptno"
                  + "  union all"
                  + " select job, deptno from emp as e2"
                  + " group by job, deptno)"
                  + " group by job, deptno";
      
          sql(sql).with(program).check();
        }
      
      java.lang.IllegalArgumentException: Cannot compute compatible row type for arguments to set op: RecordType(VARCHAR(10) JOB, INTEGER DEPTNO), RecordType(INTEGER EMPNO, VARCHAR(20) ENAME, VARCHAR(10) JOB, INTEGER MGR, TIMESTAMP(0) HIREDATE, INTEGER SAL, INTEGER COMM, INTEGER DEPTNO, BOOLEAN SLACKER)
      
      	at org.apache.calcite.rel.core.SetOp.deriveRowType(SetOp.java:111)
      	at org.apache.calcite.rel.AbstractRelNode.getRowType(AbstractRelNode.java:222)
      	at org.apache.calcite.tools.RelBuilder$Frame.<init>(RelBuilder.java:2378)
      	at org.apache.calcite.tools.RelBuilder$Frame.<init>(RelBuilder.java:2363)
      	at org.apache.calcite.tools.RelBuilder.push(RelBuilder.java:260)
      	at org.apache.calcite.tools.RelBuilder.setOp(RelBuilder.java:1559)
      	at org.apache.calcite.tools.RelBuilder.union(RelBuilder.java:1579)
      	at org.apache.calcite.tools.RelBuilder.union(RelBuilder.java:1569)
      	at org.apache.calcite.rel.rules.AggregateUnionAggregateRule.onMatch(AggregateUnionAggregateRule.java:130)
      	at org.apache.calcite.plan.AbstractRelOptPlanner.fireRule(AbstractRelOptPlanner.java:319)
      	at org.apache.calcite.plan.hep.HepPlanner.applyRule(HepPlanner.java:559)
      	at org.apache.calcite.plan.hep.HepPlanner.applyRules(HepPlanner.java:418)
      	at org.apache.calcite.plan.hep.HepPlanner.executeInstruction(HepPlanner.java:255)
      	at org.apache.calcite.plan.hep.HepInstruction$RuleInstance.execute(HepInstruction.java:127)
      	at org.apache.calcite.plan.hep.HepPlanner.executeProgram(HepPlanner.java:214)
      	at org.apache.calcite.plan.hep.HepPlanner.findBestExp(HepPlanner.java:201)
      	at org.apache.calcite.test.RelOptTestBase.checkPlanning(RelOptTestBase.java:206)
      	at org.apache.calcite.test.RelOptTestBase$Sql.check(RelOptTestBase.java:334)
      	at org.apache.calcite.test.RelOptTestBase$Sql.check(RelOptTestBase.java:317)
      	at org.apache.calcite.test.RelOptRulesTest.testPullAggregateThroughUnion
      

      I see two option to solve the problem:

      • Check the input type in AggregateUnionAggregateRule and exit the rule directly.
      • Add a Project with a same row type to the bottom Aggregate.

      What do you guys think? Thanks a lot.

      Attachments

        Activity

          People

            hequn8128 Hequn Cheng
            hequn8128 Hequn Cheng
            Votes:
            0 Vote for this issue
            Watchers:
            4 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 - 10m
                10m