Skip to main content

vortex_layout/plan/optimizer/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Static rewrite rules for physical plans.
5
6mod rules;
7
8pub use rules::DynPlanParentReduceRule;
9pub use rules::DynPlanReduceRule;
10pub use rules::PlanParentReduceRule;
11pub use rules::PlanParentReduceRuleAdapter;
12pub use rules::PlanParentRuleSet;
13pub use rules::PlanReduceRule;
14pub use rules::PlanReduceRuleAdapter;
15pub use rules::PlanRuleSet;
16use vortex_error::VortexResult;
17
18use super::Concat;
19use super::Eval;
20use super::Pack;
21use super::PlanRef;
22use super::Take;
23use super::plans::EvalIdentityRule;
24use super::plans::ExpressionConcatRule;
25use super::plans::ExpressionPackRule;
26use super::plans::ExpressionTakeRule;
27
28static EVAL_IDENTITY_RULE: PlanReduceRuleAdapter<Eval, EvalIdentityRule> =
29    PlanReduceRuleAdapter::new(EvalIdentityRule);
30
31static PLAN_RULES: PlanRuleSet = PlanRuleSet::new(&[&EVAL_IDENTITY_RULE]);
32
33static EXPRESSION_CONCAT_RULE: PlanParentReduceRuleAdapter<Concat, ExpressionConcatRule> =
34    PlanParentReduceRuleAdapter::new(ExpressionConcatRule);
35static EXPRESSION_TAKE_RULE: PlanParentReduceRuleAdapter<Take, ExpressionTakeRule> =
36    PlanParentReduceRuleAdapter::new(ExpressionTakeRule);
37static EXPRESSION_PACK_RULE: PlanParentReduceRuleAdapter<Pack, ExpressionPackRule> =
38    PlanParentReduceRuleAdapter::new(ExpressionPackRule);
39
40static PARENT_RULES: PlanParentRuleSet = PlanParentRuleSet::new(&[
41    &EXPRESSION_CONCAT_RULE,
42    &EXPRESSION_TAKE_RULE,
43    &EXPRESSION_PACK_RULE,
44]);
45
46/// Attempts a static rewrite for `plan`.
47pub(crate) fn reduce_plan(plan: &PlanRef) -> VortexResult<Option<PlanRef>> {
48    PLAN_RULES.evaluate(plan)
49}
50
51/// Attempts a static rewrite for `parent` and its child at `child_idx`.
52pub(crate) fn reduce_parent(parent: &PlanRef, child_idx: usize) -> VortexResult<Option<PlanRef>> {
53    let Some(child) = parent.child(child_idx)? else {
54        return Ok(None);
55    };
56    PARENT_RULES.evaluate(&child, parent, child_idx)
57}