Skip to content

Commit d0e0d0e

Browse files
committed
[CALCITE-7162] AggregateMergeRule type mismatch on MIN/MAX
1 parent 49631a0 commit d0e0d0e

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class CountSplitter implements SqlSplittableAggFunction {
189189
bottom.isDistinct(), bottom.isApproximate(), false,
190190
bottom.rexList, bottom.getArgList(), bottom.filterArg,
191191
bottom.distinctKeys, bottom.getCollation(),
192-
bottom.getType(), top.getName());
192+
top.getType(), top.getName());
193193
} else {
194194
return null;
195195
}
@@ -234,7 +234,7 @@ class SelfSplitter implements SqlSplittableAggFunction {
234234
bottom.isDistinct(), bottom.isApproximate(), false,
235235
bottom.rexList, bottom.getArgList(), bottom.filterArg,
236236
bottom.distinctKeys, bottom.getCollation(),
237-
bottom.getType(), top.getName());
237+
top.getType(), top.getName());
238238
} else {
239239
return null;
240240
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7583,6 +7583,26 @@ private HepProgram getTransitiveProgram() {
75837583
.check();
75847584
}
75857585

7586+
/** Test case for
7587+
* <a href="https://issues.apache.org/jira/browse/CALCITE-7162">[CALCITE-7162]
7588+
* AggregateMergeRule throws 'type mismatch' AssertionError</a>. The scenario
7589+
* has the same aggregate functions (MIN and MAX) at multiple levels; the
7590+
* lower level is NOT NULL (because of GROUP BY) and the upper level is
7591+
* nullable. */
7592+
@Test void testAggregateMerge10() {
7593+
final String sql = "SELECT min(mn), max(mx)\n"
7594+
+ "FROM (\n"
7595+
+ " SELECT min(deptno) mn, max(deptno) mx\n"
7596+
+ " FROM dept\n"
7597+
+ " GROUP BY name)";
7598+
sql(sql)
7599+
.withPreRule(CoreRules.AGGREGATE_PROJECT_MERGE,
7600+
CoreRules.PROJECT_MERGE)
7601+
.withRule(CoreRules.AGGREGATE_PROJECT_MERGE,
7602+
CoreRules.AGGREGATE_MERGE)
7603+
.check();
7604+
}
7605+
75867606
/**
75877607
* Test case for AggregateRemoveRule, should remove aggregates since
75887608
* 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
@@ -843,6 +843,28 @@ LogicalAggregate(group=[{}], EXPR$0=[SUM($1)])
843843
<![CDATA[
844844
LogicalAggregate(group=[{}], EXPR$0=[SUM($0)])
845845
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
846+
]]>
847+
</Resource>
848+
</TestCase>
849+
<TestCase name="testAggregateMerge10">
850+
<Resource name="sql">
851+
<![CDATA[SELECT min(mn), max(mx)
852+
FROM (
853+
SELECT min(deptno) mn, max(deptno) mx
854+
FROM dept
855+
GROUP BY name)]]>
856+
</Resource>
857+
<Resource name="planBefore">
858+
<![CDATA[
859+
LogicalAggregate(group=[{}], EXPR$0=[MIN($1)], EXPR$1=[MAX($2)])
860+
LogicalAggregate(group=[{1}], MN=[MIN($0)], MX=[MAX($0)])
861+
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
862+
]]>
863+
</Resource>
864+
<Resource name="planAfter">
865+
<![CDATA[
866+
LogicalAggregate(group=[{}], EXPR$0=[MIN($0)], EXPR$1=[MAX($0)])
867+
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
846868
]]>
847869
</Resource>
848870
</TestCase>

0 commit comments

Comments
 (0)