Skip to content

Commit

Permalink
[CALCITE-3076] AggregateJoinTransposeRule throws error for unique und…
Browse files Browse the repository at this point in the history
…er aggregate keys when generating merged calls

AggregateJoinTransposeRule generates wrong mapping for under agg
calls with unique aggregate keys.

Close #1223
  • Loading branch information
danny0405 committed May 21, 2019
1 parent 1a5fca0 commit 50cd134
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ public void onMatch(RelOptRuleCall call) {
final int index = ((RexInputRef) singleton).getIndex();
if (!belowAggregateKey.get(index)) {
projects.add(singleton);
side.split.put(aggCall.i, projects.size() - 1);
} else {
side.split.put(aggCall.i, index);
}
side.split.put(aggCall.i, index);
} else {
projects.add(singleton);
side.split.put(aggCall.i, projects.size() - 1);
Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4085,6 +4085,28 @@ private void transitiveInference(RelOptRule... extraRules) throws Exception {
checkPlanning(tester, preProgram, new HepPlanner(program), sql);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-3076">[CALCITE-3076]
* AggregateJoinTransposeRule throws error for unique under aggregate keys when
* generating merged calls</a>.*/
@Test public void testPushAggregateThroughJoinOnEmptyLogicalValues() {
final HepProgram preProgram = new HepProgramBuilder()
.addRuleInstance(AggregateProjectMergeRule.INSTANCE)
.addRuleInstance(ReduceExpressionsRule.FilterReduceExpressionsRule.FILTER_INSTANCE)
.build();

final HepProgram program = new HepProgramBuilder()
.addRuleInstance(AggregateJoinTransposeRule.EXTENDED)
.build();

final String sql =
"select count(*) volume, sum(C1.sal) C1_sum_sal "
+ "from (select sal, ename from sales.emp where 1=2) C1 "
+ "inner join (select ename from sales.emp) C2 "
+ "on C1.ename = C2.ename ";
sql(sql).withPre(preProgram).with(program).check();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-2249">[CALCITE-2249]
* AggregateJoinTransposeRule generates inequivalent nodes if Aggregate relNode contains
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7692,6 +7692,34 @@ LogicalAggregate(group=[{}], EXPR$0=[$SUM0($4)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalAggregate(group=[{1}], EXPR$0=[COUNT()])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
]]>
</Resource>
</TestCase>
<TestCase name="testPushAggregateThroughJoinOnEmptyLogicalValues">
<Resource name="sql">
<![CDATA[select count(*) from sales.emp join sales.dept on job = name]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalAggregate(group=[{}], VOLUME=[COUNT()], C1_SUM_SAL=[SUM($0)])
LogicalJoin(condition=[=($1, $2)], joinType=[inner])
LogicalProject(SAL=[$5], ENAME=[$1])
LogicalValues(tuples=[[]])
LogicalProject(ENAME=[$1])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalAggregate(group=[{}], VOLUME=[$SUM0($3)], C1_SUM_SAL=[SUM($4)])
LogicalProject(ENAME=[$0], SAL=[$1], ENAME0=[$2], VOLUME=[$3], $f4=[CAST(*($1, $3)):INTEGER])
LogicalJoin(condition=[=($0, $2)], joinType=[inner])
LogicalProject(ENAME=[$1], SAL=[$0])
LogicalProject(SAL=[$5], ENAME=[$1])
LogicalValues(tuples=[[]])
LogicalAggregate(group=[{0}], VOLUME=[COUNT()])
LogicalProject(ENAME=[$1])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 50cd134

Please sign in to comment.