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 @@ -101,6 +101,7 @@ private RelOptRules() {
PruneEmptyRules.MINUS_INSTANCE,
PruneEmptyRules.PROJECT_INSTANCE,
PruneEmptyRules.FILTER_INSTANCE,
PruneEmptyRules.CALC_INSTANCE,
PruneEmptyRules.SORT_INSTANCE,
PruneEmptyRules.AGGREGATE_INSTANCE,
PruneEmptyRules.WINDOW_INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.SingleRel;
import org.apache.calcite.rel.core.Aggregate;
import org.apache.calcite.rel.core.Calc;
import org.apache.calcite.rel.core.Correlate;
import org.apache.calcite.rel.core.Filter;
import org.apache.calcite.rel.core.Intersect;
Expand Down Expand Up @@ -192,6 +193,19 @@ private static boolean isEmpty(RelNode node) {
public static final RelOptRule FILTER_INSTANCE =
RemoveEmptySingleRule.RemoveEmptySingleRuleConfig.FILTER.toRule();

/**
* Rule that converts a {@link org.apache.calcite.rel.core.Calc}
* to empty if its child is empty.
*
* <p>Examples:
*
* <ul>
* <li>Calc(Empty) becomes Empty
* </ul>
*/
public static final RelOptRule CALC_INSTANCE =
RemoveEmptySingleRule.RemoveEmptySingleRuleConfig.CALC.toRule();

/**
* Rule that converts a {@link org.apache.calcite.rel.core.Sort}
* to empty if its child is empty.
Expand Down Expand Up @@ -374,6 +388,9 @@ public interface RemoveEmptySingleRuleConfig extends PruneEmptyRule.Config {
RemoveEmptySingleRuleConfig FILTER = ImmutableRemoveEmptySingleRuleConfig.of()
.withDescription("PruneEmptyFilter")
.withOperandFor(Filter.class, singleRel -> true);
RemoveEmptySingleRuleConfig CALC = ImmutableRemoveEmptySingleRuleConfig.of()
.withDescription("PruneEmptyCalc")
.withOperandFor(Calc.class, singleRel -> true);
RemoveEmptySingleRuleConfig SORT = ImmutableRemoveEmptySingleRuleConfig.of()
.withDescription("PruneEmptySort")
.withOperandFor(Sort.class, singleRel -> true);
Expand Down
16 changes: 16 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 @@ -5321,6 +5321,22 @@ RelOptFixture checkDynamicFunctions(boolean treatDynamicCallsAsConstant) {
.check();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-7672">[CALCITE-7672]
* PruneEmptyRules should support pruning empty Calc</a>.
*/
@Test void testEmptyCalc() {
final String sql = "select z + x from (\n"
+ " select x + y as z, x from (\n"
+ " select * from (values (10, 1), (30, 3)) as t (x, y)\n"
+ " where x + y > 50))";
sql(sql)
.withRule(CoreRules.FILTER_VALUES_MERGE,
CoreRules.PROJECT_TO_CALC,
PruneEmptyRules.CALC_INSTANCE)
.check();
}

@Test void testEmptyIntersect() {
final String sql = "select * from (values (30, 3))"
+ "intersect\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3977,6 +3977,26 @@ LogicalAggregate(group=[{}], EXPR$0=[COUNT()], EXPR$1=[SUM($0)])
<Resource name="planAfter">
<![CDATA[
LogicalValues(tuples=[[{ 0, null }]])
]]>
</Resource>
</TestCase>
<TestCase name="testEmptyCalc">
<Resource name="sql">
<![CDATA[select z + x from (
select x + y as z, x from (
select * from (values (10, 1), (30, 3)) as t (x, y)
where x + y > 50))]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(EXPR$0=[+(+($0, $1), $0)])
LogicalFilter(condition=[>(+($0, $1), 50)])
LogicalValues(tuples=[[{ 10, 1 }, { 30, 3 }]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalValues(tuples=[[]])
]]>
</Resource>
</TestCase>
Expand Down
Loading