You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[FEATURE] Prune old RelNode in push down rules #4931
Is your feature request related to a problem?
Calcite's planner supports RelNode pruning, which reduces unnecessary exploration of candidate plans within a RelSet. This optimization should be applied when a new plan is definitively cheaper than existing alternatives and offers no additional value for discovering the optimal execution plan.
What solution would you like?
A clear and concise description of what you want to happen.
What alternatives have you considered?
A clear and concise description of any alternative solutions or features you've considered.
Do you have any additional context?
Calcite has interface SubstitutionRule, where there is API autoPruneOld determines whether the planner should automatically prune old node when there is at least 1 equivalent rel generated by the rule. The default behavior is returning false and substitution rules will be executed first in the rule on-match queue.
After some tests on pruning old in our rules, I've found some bad cases:
Prune old in project/filter push down rules will prevent aggregate push down cases: testExplainMaxOnStringField, testAliasTypeField, testCountAggPushDownExplain, .... root-cause: Since we've pruned the old RelNodes, the pattern of agg push down rules cannot match a valid RelSubSets. fix way:
Make agg push down extends SubstitutionRule so it will get higher priority in the rule match queue
Extends agg push down rules to support pattern AGG-SCAN so we can still push down agg after project and filter push down.
Prune old may cause exception in the plan preparing process. cases: org.opensearch.sql.calcite.remote.CalciteExplainIT#testExplainScalarCorrelatedSubqueryInSelect
org.opensearch.sql.calcite.remote.CalciteExplainIT#testJoinWithPushdownSortIntoAgg
org.opensearch.sql.calcite.remote.CalciteExplainIT#testExplainAppendCommand
org.opensearch.sql.calcite.remote.CalciteExplainIT#testSortComplexExprMixedWithSimpleExpr
org.opensearch.sql.calcite.remote.CalciteExplainIT#testComplexSortExpressionProjectThenSort root-cause: some old RelNodes has multiple parents, if we prune it directly in a rule, it cannot be converted to a valid enumerable node in the other branches. In the end, the planner cannot generate a valid final plan. fix way: Detect its parents before pruning a RelNode.
Prune old in agg push down rule will prevent other agg optimization rules taking effect cases: testExplainCompositeMultiBucketsAutoDateThenSortOnMeasureNotPushdown, ... root causes: If the agg push down rule matches first and we prune the agg RelNode after transformation, we don't have any chance to optimize the old agg RelNode anymore. The agg optimization rules include: PPLAggregateConvertRule, PPLAggGroupMergeRule, calcite's AggregateRemoveRule, which is skipped in the above bad case. fix way:
Make our agg optimization rules extend SubstitutionRule
Give up pruning old in agg push down rule.
Sort expr cannot take effect in sort merge join. New case introduced by Support sort expression pushdown for SortMergeJoin #4830 cases: testComplexSortExprPushdownForSMJ root-cause: Without pruning, SortExprIndexScanRule will be triggered twice in physical plan. The cheapest plan is generated from the second match. 2 rule match share the same EnuemrableSort, if we prune it in the first, the second match will be skipped.
FIRST MATCH RELS: EnuemrableSort -> Project1 -> Scan1 without push down
SECOND MATACH RELS: EnumerableSort -> Project2 -> Scan2 with project push down. fix way: work around by skipping pruning all rels if they are physical plan.
Limit cannot push down into scan cases: keyword_terms_low_cardinality, keyword_terms in Big5IT root cause: SortAggregateMeasureRule prune the sort, which will later trigger SortRmoveRule if not pruned. And SortRmoveRule will contribute to the RelSet merge of sort's input and another derived RelSet from EnumerableLimitRule. That merge plays as the key point of producing the LimiteIndexScanRule match. fix way: Remove the SubstitutionRule trait from SortAggregateMeasureRule. Then it will get triggered after SortRmoveRule.
Cannot remove ISNOTNULL filter derived from aggregate when the aggregate is pushed down. Conflict with Remove redundant push-down-filters derived for bucket-non-null agg #4843 case: testExplainSortOnMeasureMultiBucketsNotMultiTermsNotPushDown root cause: Since we've pruned the old in FilterIndexScanRule, it can no longer match Agg-Project-Filter-Project-Scan pattern in AggregateIndexScanRule. fix way: Tried to remove the push down filter when pushing down aggregate. It could over most cases. But for the IT case above, it's hard to identify whether the filter is derived from the aggregate because there is multiple project between them and some of them are also pushed down.
Activity
RyanL1997 commentedon Dec 9, 2025
@opensearch-project/triage , Can we move this issue to sql repo? https://github.com/opensearch-project/sql. Thanks in advance.
qianheng-aws commentedon Dec 12, 2025
After some tests on pruning old in our rules, I've found some bad cases:
Prune old in project/filter push down rules will prevent aggregate push down
cases: testExplainMaxOnStringField, testAliasTypeField, testCountAggPushDownExplain, ....
root-cause: Since we've pruned the old RelNodes, the pattern of agg push down rules cannot match a valid RelSubSets.
fix way:
AGG-SCANso we can still push down agg after project and filter push down.Prune old may cause exception in the plan preparing process.
cases: org.opensearch.sql.calcite.remote.CalciteExplainIT#testExplainScalarCorrelatedSubqueryInSelect
org.opensearch.sql.calcite.remote.CalciteExplainIT#testJoinWithPushdownSortIntoAgg
org.opensearch.sql.calcite.remote.CalciteExplainIT#testExplainAppendCommand
org.opensearch.sql.calcite.remote.CalciteExplainIT#testSortComplexExprMixedWithSimpleExpr
org.opensearch.sql.calcite.remote.CalciteExplainIT#testComplexSortExpressionProjectThenSort
root-cause: some old RelNodes has multiple parents, if we prune it directly in a rule, it cannot be converted to a valid enumerable node in the other branches. In the end, the planner cannot generate a valid final plan.
fix way: Detect its parents before pruning a RelNode.
Prune old in agg push down rule will prevent other agg optimization rules taking effect
cases: testExplainCompositeMultiBucketsAutoDateThenSortOnMeasureNotPushdown, ...
root causes: If the agg push down rule matches first and we prune the agg RelNode after transformation, we don't have any chance to optimize the old agg RelNode anymore. The agg optimization rules include:
PPLAggregateConvertRule,PPLAggGroupMergeRule, calcite'sAggregateRemoveRule, which is skipped in the above bad case.fix way:
Sort expr cannot take effect in sort merge join. New case introduced by Support sort expression pushdown for SortMergeJoin #4830
cases: testComplexSortExprPushdownForSMJ
root-cause: Without pruning,
SortExprIndexScanRulewill be triggered twice in physical plan. The cheapest plan is generated from the second match. 2 rule match share the sameEnuemrableSort, if we prune it in the first, the second match will be skipped.FIRST MATCH RELS: EnuemrableSort -> Project1 -> Scan1 without push down
SECOND MATACH RELS: EnumerableSort -> Project2 -> Scan2 with project push down.
fix way: work around by skipping pruning all rels if they are physical plan.
Limit cannot push down into scan
cases: keyword_terms_low_cardinality, keyword_terms in
Big5ITroot cause:
SortAggregateMeasureRuleprune the sort, which will later triggerSortRmoveRuleif not pruned. AndSortRmoveRulewill contribute to the RelSet merge of sort's input and another derived RelSet fromEnumerableLimitRule. That merge plays as the key point of producing theLimiteIndexScanRulematch.fix way: Remove the
SubstitutionRuletrait fromSortAggregateMeasureRule. Then it will get triggered afterSortRmoveRule.Cannot remove
ISNOTNULLfilter derived from aggregate when the aggregate is pushed down. Conflict with Remove redundant push-down-filters derived for bucket-non-null agg #4843case: testExplainSortOnMeasureMultiBucketsNotMultiTermsNotPushDown
root cause: Since we've pruned the old in
FilterIndexScanRule, it can no longer matchAgg-Project-Filter-Project-Scanpattern inAggregateIndexScanRule.fix way: Tried to remove the push down filter when pushing down aggregate. It could over most cases. But for the IT case above, it's hard to identify whether the filter is derived from the aggregate because there is multiple project between them and some of them are also pushed down.