Skip to content

Commit

Permalink
[CALCITE-4345] Avoid NPE in AggregateCaseToFilterRule when converts S…
Browse files Browse the repository at this point in the history
…UM(CASE WHEN c THEN 1 END) (Jiatao Tao)
  • Loading branch information
Aaaaaaron committed Oct 19, 2020
1 parent bda7fb1 commit 4821c43
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,11 @@ && isThreeArgCase(project.getProjects().get(singleArg))) {
RelCollations.EMPTY, aggregateCall.getType(),
aggregateCall.getName());
} else if (kind == SqlKind.SUM // Case B
&& isIntLiteral(arg1) && RexLiteral.intValue(arg1) == 1
&& isIntLiteral(arg2) && RexLiteral.intValue(arg2) == 0) {
&& isIntLiteral(arg1)
&& RexLiteral.intValue(arg1) == 1
&& isIntLiteral(arg2)
&& !RexLiteral.isNullLiteral(arg2)
&& RexLiteral.intValue(arg2) == 0) {

newProjects.add(filter);
final RelDataTypeFactory typeFactory = cluster.getTypeFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3825,7 +3825,8 @@ public boolean test(Project project) {
+ " sum(case when deptno = 10 then sal end) as sum_sal_d10,\n"
+ " sum(case when deptno = 20 then sal else 0 end) as sum_sal_d20,\n"
+ " sum(case when deptno = 30 then 1 else 0 end) as count_d30,\n"
+ " count(case when deptno = 40 then 'x' end) as count_d40,\n"
+ " sum(case when deptno = 40 then 1 end) as count_d40,\n"
+ " count(case when deptno = 50 then 'x' end) as count_d50,\n"
+ " count(case when deptno = 20 then 1 end) as count_d20\n"
+ "from emp";
sql(sql).withRule(CoreRules.AGGREGATE_CASE_TO_FILTER).check();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@
sum(case when deptno = 10 then sal end) as sum_sal_d10,
sum(case when deptno = 20 then sal else 0 end) as sum_sal_d20,
sum(case when deptno = 30 then 1 else 0 end) as count_d30,
count(case when deptno = 40 then 'x' end) as count_d40,
sum(case when deptno = 40 then 1 end) as count_d40,
count(case when deptno = 50 then 'x' end) as count_d50,
count(case when deptno = 20 then 1 end) as count_d20
from emp]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalAggregate(group=[{}], SUM_SAL=[SUM($0)], COUNT_DISTINCT_CLERK=[COUNT(DISTINCT $1)], SUM_SAL_D10=[SUM($2)], SUM_SAL_D20=[SUM($3)], COUNT_D30=[SUM($4)], COUNT_D40=[COUNT($5)], COUNT_D20=[COUNT($6)])
LogicalProject(SAL=[$5], $f1=[CASE(=($2, 'CLERK'), $7, null:INTEGER)], $f2=[CASE(=($7, 10), $5, null:INTEGER)], $f3=[CASE(=($7, 20), $5, 0)], $f4=[CASE(=($7, 30), 1, 0)], $f5=[CASE(=($7, 40), 'x', null:CHAR(1))], $f6=[CASE(=($7, 20), 1, null:INTEGER)])
LogicalAggregate(group=[{}], SUM_SAL=[SUM($0)], COUNT_DISTINCT_CLERK=[COUNT(DISTINCT $1)], SUM_SAL_D10=[SUM($2)], SUM_SAL_D20=[SUM($3)], COUNT_D30=[SUM($4)], COUNT_D40=[SUM($5)], COUNT_D50=[COUNT($6)], COUNT_D20=[COUNT($7)])
LogicalProject(SAL=[$5], $f1=[CASE(=($2, 'CLERK'), $7, null:INTEGER)], $f2=[CASE(=($7, 10), $5, null:INTEGER)], $f3=[CASE(=($7, 20), $5, 0)], $f4=[CASE(=($7, 30), 1, 0)], $f5=[CASE(=($7, 40), 1, null:INTEGER)], $f6=[CASE(=($7, 50), 'x', null:CHAR(1))], $f7=[CASE(=($7, 20), 1, null:INTEGER)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(SUM_SAL=[$0], COUNT_DISTINCT_CLERK=[$1], SUM_SAL_D10=[$2], SUM_SAL_D20=[$3], COUNT_D30=[CAST($4):INTEGER], COUNT_D40=[$5], COUNT_D20=[$6])
LogicalAggregate(group=[{}], SUM_SAL=[SUM($0)], COUNT_DISTINCT_CLERK=[COUNT(DISTINCT $1) FILTER $2], SUM_SAL_D10=[SUM($3) FILTER $4], SUM_SAL_D20=[SUM($5) FILTER $6], COUNT_D30=[COUNT() FILTER $7], COUNT_D40=[COUNT() FILTER $8], COUNT_D20=[COUNT() FILTER $9])
LogicalProject(SAL=[$5], DEPTNO=[$7], $f8=[=($2, 'CLERK')], SAL0=[$5], $f10=[=($7, 10)], SAL1=[$5], $f12=[=($7, 20)], $f13=[=($7, 30)], $f14=[=($7, 40)], $f15=[=($7, 20)])
LogicalProject(SUM_SAL=[$0], COUNT_DISTINCT_CLERK=[$1], SUM_SAL_D10=[$2], SUM_SAL_D20=[$3], COUNT_D30=[CAST($4):INTEGER], COUNT_D40=[$5], COUNT_D50=[$6], COUNT_D20=[$7])
LogicalAggregate(group=[{}], SUM_SAL=[SUM($0)], COUNT_DISTINCT_CLERK=[COUNT(DISTINCT $1) FILTER $2], SUM_SAL_D10=[SUM($3) FILTER $4], SUM_SAL_D20=[SUM($5) FILTER $6], COUNT_D30=[COUNT() FILTER $7], COUNT_D40=[SUM($8) FILTER $9], COUNT_D50=[COUNT() FILTER $10], COUNT_D20=[COUNT() FILTER $11])
LogicalProject(SAL=[$5], DEPTNO=[$7], $f9=[=($2, 'CLERK')], SAL0=[$5], $f11=[=($7, 10)], SAL1=[$5], $f13=[=($7, 20)], $f14=[=($7, 30)], $f15=[1], $f16=[=($7, 40)], $f17=[=($7, 50)], $f18=[=($7, 20)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
Expand Down

0 comments on commit 4821c43

Please sign in to comment.