Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.calcite.rel.core.AggregateCall;
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.tools.RelBuilder;

import org.immutables.value.Value;
Expand Down Expand Up @@ -76,6 +77,9 @@ private AggregateFilterToFilteredAggregateRule(Config config) {
return;
}
RexNode condition = filter.getCondition();
if (condition.getType().isNullable()) {
condition = builder.call(SqlStdOperatorTable.IS_TRUE, condition);
}
// If the aggregate call has its own filter, combine it with the filter condition.
if (aggCall.hasFilter()) {
condition = builder.and(condition, builder.field(aggCall.filterArg));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ private static RelOptFixture sql(String sql) {
.withRule(AGGREGATE_FILTER_TO_FILTERED_AGGREGATE).check();
}

@Test void testSingleColumnAggregateWithFilterOnNullableColumn() {
String sql = "select sum(sal) from emp where mgr = 10";
sql(sql).withPreRule(AGGREGATE_PROJECT_MERGE)
.withRule(AGGREGATE_FILTER_TO_FILTERED_AGGREGATE).check();
}

@Test void testSingleStarAggregate() {
String sql = "select count(*) from emp where deptno = 10";
sql(sql).withPreRule(AGGREGATE_PROJECT_MERGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,25 @@ LogicalAggregate(group=[{}], EXPR$0=[SUM($5)])
LogicalAggregate(group=[{}], EXPR$0=[SUM($0) FILTER $1])
LogicalProject(SAL=[$5], $f9=[=($7, 10)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testSingleColumnAggregateWithFilterOnNullableColumn">
<Resource name="sql">
<![CDATA[select sum(sal) from emp where mgr = 10]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalAggregate(group=[{}], EXPR$0=[SUM($5)])
LogicalFilter(condition=[=($3, 10)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalAggregate(group=[{}], EXPR$0=[SUM($0) FILTER $1])
LogicalProject(SAL=[$5], $f9=[IS TRUE(=($3, 10))])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down
Loading