Skip to content

Commit 840aff6

Browse files
julianhydeCoKueb
authored andcommitted
[CALCITE-6557] AggregateMergeRule throws 'type mismatch' AssertionError
The fix was to use the type of the upper aggregate call, rather than the lower.
1 parent c7a1ec4 commit 840aff6

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

core/src/main/java/org/apache/calcite/sql/SqlSplittableAggFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ abstract class AbstractSumSplitter implements SqlSplittableAggFunction {
304304
bottom.isDistinct(), bottom.isApproximate(), false,
305305
bottom.rexList, bottom.getArgList(), bottom.filterArg,
306306
bottom.distinctKeys, bottom.getCollation(),
307-
bottom.getType(), top.getName());
307+
top.getType(), top.getName());
308308
} else {
309309
return null;
310310
}

core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6545,6 +6545,25 @@ private HepProgram getTransitiveProgram() {
65456545
.check();
65466546
}
65476547

6548+
/** Test case for
6549+
* <a href="https://issues.apache.org/jira/browse/CALCITE-6557">[CALCITE-6557]
6550+
* AggregateMergeRule throws 'type mismatch' AssertionError</a>. The scenario
6551+
* has the same aggregate function (SUM) at multiple levels; the lower level
6552+
* is NOT NULL (because of GROUP BY) and the upper level is nullable. */
6553+
@Test void testAggregateMerge9() {
6554+
final String sql = "SELECT sum(deptno)\n"
6555+
+ "FROM (\n"
6556+
+ " SELECT sum(deptno) AS deptno\n"
6557+
+ " FROM dept\n"
6558+
+ " GROUP BY name)";
6559+
sql(sql)
6560+
.withPreRule(CoreRules.AGGREGATE_PROJECT_MERGE,
6561+
CoreRules.PROJECT_MERGE)
6562+
.withRule(CoreRules.AGGREGATE_PROJECT_MERGE,
6563+
CoreRules.AGGREGATE_MERGE)
6564+
.check();
6565+
}
6566+
65486567
/**
65496568
* Test case for AggregateRemoveRule, should remove aggregates since
65506569
* empno is unique and all aggregate functions are splittable.

core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,28 @@ LogicalAggregate(group=[{}], X=[SUM($0)], Z=[MIN($1)])
756756
<![CDATA[
757757
LogicalAggregate(group=[{}], X=[SUM($5)], Z=[MIN($5)])
758758
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
759+
]]>
760+
</Resource>
761+
</TestCase>
762+
<TestCase name="testAggregateMerge9">
763+
<Resource name="sql">
764+
<![CDATA[SELECT sum(deptno)
765+
FROM (
766+
SELECT sum(deptno) AS deptno
767+
FROM dept
768+
GROUP BY name)]]>
769+
</Resource>
770+
<Resource name="planBefore">
771+
<![CDATA[
772+
LogicalAggregate(group=[{}], EXPR$0=[SUM($1)])
773+
LogicalAggregate(group=[{1}], DEPTNO=[SUM($0)])
774+
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
775+
]]>
776+
</Resource>
777+
<Resource name="planAfter">
778+
<![CDATA[
779+
LogicalAggregate(group=[{}], EXPR$0=[SUM($0)])
780+
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
759781
]]>
760782
</Resource>
761783
</TestCase>

core/src/test/resources/sql/agg.iq

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ from (select deptno, count(*) from emp group by deptno);
147147

148148
!ok
149149

150+
# [CALCITE-6557] AggregateMergeRule throws 'type mismatch' AssertionError
151+
#
152+
# The scenario has the same aggregate function (SUM) at multiple levels; the
153+
# lower level is NOT NULL (because of GROUP BY) and the upper level is nullable.
154+
SELECT sum(deptno) AS s
155+
FROM (
156+
SELECT sum(deptno) AS deptno
157+
FROM dept
158+
GROUP BY dname);
159+
+-----+
160+
| S |
161+
+-----+
162+
| 100 |
163+
+-----+
164+
(1 row)
165+
166+
!ok
167+
150168
# [CALCITE-998] Exception when calling STDDEV_SAMP, STDDEV_POP
151169
# stddev_samp
152170
select stddev_samp(deptno) as s from emp;
@@ -3778,4 +3796,5 @@ select distinct sum(deptno + '1') as deptsum from dept order by 1;
37783796
(1 row)
37793797

37803798
!ok
3799+
37813800
# End agg.iq

0 commit comments

Comments
 (0)