|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use vortex_dtype::DType; |
| 5 | +use vortex_error::VortexResult; |
| 6 | + |
| 7 | +use crate::expr::Expression; |
| 8 | +use crate::expr::session::ExprSession; |
| 9 | +use crate::expr::transform::{RewriteContext, SimpleRewriteContext}; |
| 10 | +use crate::expr::traversal::{NodeExt, Transformed}; |
| 11 | + |
| 12 | +/// Simplify an expression using registered rewrite rules from a session. |
| 13 | +/// |
| 14 | +/// This applies all rules registered in the session's `RewriteRuleRegistry`: |
| 15 | +/// 1. Child rules (bottom-up traversal) |
| 16 | +/// 2. Parent rules (top-down traversal) |
| 17 | +/// |
| 18 | +/// This is the primary entry point for extensible expression optimization. |
| 19 | +pub fn simplify_with_session( |
| 20 | + expr: Expression, |
| 21 | + dtype: &DType, |
| 22 | + session: &ExprSession, |
| 23 | +) -> VortexResult<Expression> { |
| 24 | + let ctx = SimpleRewriteContext { dtype }; |
| 25 | + |
| 26 | + // First bottom-up (child rules) |
| 27 | + let expr = apply_child_rules_impl(expr, &ctx, session)?; |
| 28 | + |
| 29 | + // Then top-down (parent rules) |
| 30 | + apply_parent_rules_impl(expr, &ctx, session) |
| 31 | +} |
| 32 | + |
| 33 | +/// Apply only child reduction rules from the session (bottom-up traversal). |
| 34 | +/// |
| 35 | +/// This performs a post-order traversal, applying rules after children are processed. |
| 36 | +/// Useful for optimizations like `pack(...).get_item(field) -> field_expr`. |
| 37 | +pub fn apply_child_rules( |
| 38 | + expr: Expression, |
| 39 | + dtype: &DType, |
| 40 | + session: &ExprSession, |
| 41 | +) -> VortexResult<Expression> { |
| 42 | + let ctx = SimpleRewriteContext { dtype }; |
| 43 | + apply_child_rules_impl(expr, &ctx, session) |
| 44 | +} |
| 45 | + |
| 46 | +/// Apply only parent reduction rules from the session (top-down traversal). |
| 47 | +/// |
| 48 | +/// This performs a pre-order traversal, applying rules based on parent context. |
| 49 | +/// Only called for non-root expressions. |
| 50 | +pub fn apply_parent_rules( |
| 51 | + expr: Expression, |
| 52 | + dtype: &DType, |
| 53 | + session: &ExprSession, |
| 54 | +) -> VortexResult<Expression> { |
| 55 | + let ctx = SimpleRewriteContext { dtype }; |
| 56 | + apply_parent_rules_impl(expr, &ctx, session) |
| 57 | +} |
| 58 | + |
| 59 | +/// Internal implementation: Apply child rules in a bottom-up manner. |
| 60 | +fn apply_child_rules_impl( |
| 61 | + expr: Expression, |
| 62 | + ctx: &dyn RewriteContext, |
| 63 | + session: &ExprSession, |
| 64 | +) -> VortexResult<Expression> { |
| 65 | + expr.transform_up(|node| apply_child_rules_to_node(node, ctx, session)) |
| 66 | + .map(|t| t.into_inner()) |
| 67 | +} |
| 68 | + |
| 69 | +/// Apply child rules to a single node. |
| 70 | +fn apply_child_rules_to_node( |
| 71 | + expr: Expression, |
| 72 | + ctx: &dyn RewriteContext, |
| 73 | + session: &ExprSession, |
| 74 | +) -> VortexResult<Transformed<Expression>> { |
| 75 | + let expr_id = expr.id(); |
| 76 | + |
| 77 | + // First try generic reduce rules (no context needed) |
| 78 | + if let Some(rules) = session.rewrite_rules().reduce_rules_for(&expr_id) { |
| 79 | + for rule in rules { |
| 80 | + if let Some(new_expr) = rule.reduce_dyn(&expr, ctx)? { |
| 81 | + return Ok(Transformed::yes(new_expr)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Then try child-context rules |
| 87 | + if let Some(rules) = session.rewrite_rules().child_rules_for(&expr_id) { |
| 88 | + // Try each child and each rule |
| 89 | + for (child_idx, child) in expr.children().iter().enumerate() { |
| 90 | + for rule in rules { |
| 91 | + if let Some(new_expr) = rule.reduce_child_dyn(&expr, child, child_idx, ctx)? { |
| 92 | + return Ok(Transformed::yes(new_expr)); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + Ok(Transformed::no(expr)) |
| 99 | +} |
| 100 | + |
| 101 | +/// Internal implementation: Apply parent rules in a top-down manner. |
| 102 | +fn apply_parent_rules_impl( |
| 103 | + expr: Expression, |
| 104 | + ctx: &dyn RewriteContext, |
| 105 | + session: &ExprSession, |
| 106 | +) -> VortexResult<Expression> { |
| 107 | + apply_parent_rules_recursive(expr, None, ctx, session) |
| 108 | +} |
| 109 | + |
| 110 | +/// Recursive helper for applying parent rules. |
| 111 | +fn apply_parent_rules_recursive( |
| 112 | + expr: Expression, |
| 113 | + parent: Option<&Expression>, |
| 114 | + ctx: &dyn RewriteContext, |
| 115 | + session: &ExprSession, |
| 116 | +) -> VortexResult<Expression> { |
| 117 | + // Apply parent rules if we have a parent |
| 118 | + let expr = if let Some(parent) = parent { |
| 119 | + let expr_id = expr.id(); |
| 120 | + if let Some(rules) = session.rewrite_rules().parent_rules_for(&expr_id) { |
| 121 | + let mut current = expr; |
| 122 | + for rule in rules { |
| 123 | + if let Some(new_expr) = rule.reduce_parent_dyn(¤t, parent, ctx)? { |
| 124 | + current = new_expr; |
| 125 | + } |
| 126 | + } |
| 127 | + current |
| 128 | + } else { |
| 129 | + expr |
| 130 | + } |
| 131 | + } else { |
| 132 | + expr |
| 133 | + }; |
| 134 | + |
| 135 | + // Recursively apply to children |
| 136 | + let new_children: Result<Vec<_>, _> = expr |
| 137 | + .children() |
| 138 | + .iter() |
| 139 | + .map(|child| apply_parent_rules_recursive(child.clone(), Some(&expr), ctx, session)) |
| 140 | + .collect(); |
| 141 | + |
| 142 | + expr.with_children(new_children?) |
| 143 | +} |
0 commit comments