Skip to content

Commit

Permalink
[CALCITE-6333] NullPointerException in AggregateExpandDistinctAggrega…
Browse files Browse the repository at this point in the history
…tesRule.doRewrite when rewriting filtered distinct aggregation

Fix test order

Fix one more
  • Loading branch information
abhishekagarwal87 authored and mihaibudiu committed Apr 9, 2024
1 parent 9bd3713 commit 283c1c1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -741,21 +741,20 @@ private static void doRewrite(RelBuilder relBuilder, Aggregate aggregate, int n,
if (!aggCall.getArgList().equals(argList)) {
continue;
}
if (aggCall.filterArg != filterArg) {
continue;
}

// Re-map arguments.
final int argCount = aggCall.getArgList().size();
final List<Integer> newArgs = new ArrayList<>(argCount);
for (Integer arg : aggCall.getArgList()) {
newArgs.add(requireNonNull(sourceOf.get(arg), () -> "sourceOf.get(" + arg + ")"));
}
final int newFilterArg =
aggCall.filterArg < 0 ? -1
: requireNonNull(sourceOf.get(aggCall.filterArg),
() -> "sourceOf.get(" + aggCall.filterArg + ")");
final AggregateCall newAggCall =
AggregateCall.create(aggCall.getAggregation(), false,
aggCall.isApproximate(), aggCall.ignoreNulls(), aggCall.rexList,
newArgs, newFilterArg, aggCall.distinctKeys, aggCall.collation,
newArgs, -1, aggCall.distinctKeys, aggCall.collation,
aggCall.getType(), aggCall.getName());
assert refs.get(i) == null;
if (leftFields == null) {
Expand Down
21 changes: 21 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 @@ -2175,6 +2175,27 @@ private void checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
.check();
}

@Test void testDistinctWithFilterWithoutGroupByUsingJoin() {
final String sql = "SELECT SUM(comm), COUNT(DISTINCT sal) FILTER (WHERE sal > 1000)\n"
+ "FROM emp";
sql(sql)
.withRule(CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN)
.check();
}

@Test void testMultipleDistinctWithSameArgsDifferentFilterUsingJoin() {
final String sql = "select deptno, "
+ "count(distinct sal) FILTER (WHERE sal > 1000), "
+ "count(distinct sal) FILTER (WHERE sal > 500) "
+ "from sales.emp group by deptno";
sql(sql)
.withRule(
CoreRules.AGGREGATE_EXPAND_DISTINCT_AGGREGATES_TO_JOIN,
CoreRules.AGGREGATE_PROJECT_MERGE
)
.check();
}

@Test void testDistinctWithFilterWithoutGroupBy() {
final String sql = "SELECT SUM(comm), COUNT(DISTINCT sal) FILTER (WHERE sal > 1000)\n"
+ "FROM emp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2873,6 +2873,32 @@ LogicalAggregate(group=[{}], EXPR$0=[MIN($1) FILTER $3], EXPR$1=[COUNT($0) FILTE
LogicalAggregate(group=[{1, 2}], groups=[[{1, 2}, {}]], EXPR$0=[SUM($0)], $g=[GROUPING($1, $2)])
LogicalProject(COMM=[$6], SAL=[$5], $f2=[>($5, 1000)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testDistinctWithFilterWithoutGroupByUsingJoin">
<Resource name="sql">
<![CDATA[SELECT SUM(comm), COUNT(DISTINCT sal) FILTER (WHERE sal > 1000)
FROM emp]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalAggregate(group=[{}], EXPR$0=[SUM($0)], EXPR$1=[COUNT(DISTINCT $1) FILTER $2])
LogicalProject(COMM=[$6], SAL=[$5], $f2=[>($5, 1000)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalJoin(condition=[true], joinType=[inner])
LogicalAggregate(group=[{}], EXPR$0=[SUM($0)])
LogicalProject(COMM=[$6], SAL=[$5], $f2=[>($5, 1000)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalAggregate(group=[{}], EXPR$1=[COUNT($0)])
LogicalAggregate(group=[{0}])
LogicalProject(i$SAL=[CASE($2, $1, null:INTEGER)])
LogicalProject(COMM=[$6], SAL=[$5], $f2=[>($5, 1000)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down Expand Up @@ -7279,6 +7305,34 @@ LogicalProject(SAL=[$0], EXPR$1=[$1], EXPR$2=[$3], EXPR$3=[$5])
LogicalProject(SAL=[$0])
LogicalProject(SAL=[$5], COMM=[$6])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testMultipleDistinctWithSameArgsDifferentFilterUsingJoin">
<Resource name="sql">
<![CDATA[select deptno, count(distinct sal) FILTER (WHERE sal > 1000), count(distinct sal) FILTER (WHERE sal > 500) from sales.emp group by deptno]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalAggregate(group=[{0}], EXPR$1=[COUNT(DISTINCT $1) FILTER $2], EXPR$2=[COUNT(DISTINCT $1) FILTER $3])
LogicalProject(DEPTNO=[$7], SAL=[$5], $f2=[>($5, 1000)], $f3=[>($5, 500)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(DEPTNO=[$0], EXPR$1=[$1], EXPR$2=[$3])
LogicalJoin(condition=[IS NOT DISTINCT FROM($0, $2)], joinType=[inner])
LogicalAggregate(group=[{0}], EXPR$1=[COUNT($1)])
LogicalAggregate(group=[{0, 1}])
LogicalProject(DEPTNO=[$0], i$SAL=[CASE($2, $1, null:INTEGER)])
LogicalProject(DEPTNO=[$7], SAL=[$5], $f2=[>($5, 1000)], $f3=[>($5, 500)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalAggregate(group=[{0}], EXPR$2=[COUNT($1)])
LogicalAggregate(group=[{0, 1}])
LogicalProject(DEPTNO=[$0], i$SAL=[CASE($3, $1, null:INTEGER)])
LogicalProject(DEPTNO=[$7], SAL=[$5], $f2=[>($5, 1000)], $f3=[>($5, 500)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 283c1c1

Please sign in to comment.