Skip to content

[FEATURE] Prune old RelNode in push down rules #4931

Closed

Description

@qianheng-aws

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.

Activity

  1. RyanL1997 commented on Dec 9, 2025

    @RyanL1997
    Collaborator

    @opensearch-project/triage , Can we move this issue to sql repo? https://github.com/opensearch-project/sql. Thanks in advance.

  2. self-assigned this
    on Dec 12, 2025
  3. added
    PPL
    calcite
    pushdown
    and removed on Dec 12, 2025
  4. qianheng-aws commented on Dec 12, 2025

    @qianheng-aws
    CollaboratorAuthor

    After some tests on pruning old in our rules, I've found some bad cases:

    1. 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.
    2. 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.

    3. 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.
    4. 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.

    5. 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.

    6. 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.

  5. moved this from Not Started to Done in PPL Trackeron Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

PPLcalciteenhancementpushdown

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions