Skip to content

Commit d9d862d

Browse files
committed
wip
Signed-off-by: Joe Isaacs <[email protected]>
1 parent 9c8c0a7 commit d9d862d

File tree

9 files changed

+152
-306
lines changed

9 files changed

+152
-306
lines changed

vortex-array/src/expr/exprs/get_item/transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use vortex_error::VortexResult;
55

66
use crate::expr::exprs::get_item::GetItem;
77
use crate::expr::exprs::pack::Pack;
8-
use crate::expr::transform::traits::{ChildReduceRule, RewriteContext};
8+
use crate::expr::transform::rules::{ChildReduceRule, RewriteContext};
99
use crate::expr::{Expression, ExpressionView};
1010

1111
/// Rewrite rule: `pack(l_1: e_1, ..., l_i: e_i, ..., l_n: e_n).get_item(l_i) = e_i`

vortex-array/src/expr/exprs/merge/transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use vortex_utils::aliases::hash_set::HashSet;
88
use crate::expr::exprs::get_item::get_item;
99
use crate::expr::exprs::merge::{DuplicateHandling, Merge};
1010
use crate::expr::exprs::pack::pack;
11-
use crate::expr::transform::traits::{ReduceRule, RewriteContext};
11+
use crate::expr::transform::rules::{ReduceRule, RewriteContext};
1212
use crate::expr::{Expression, ExpressionView};
1313

1414
/// Rule that removes Merge expressions by converting them to Pack + GetItem.

vortex-array/src/expr/exprs/select/transform.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use vortex_error::{VortexResult, vortex_err};
66
use crate::expr::exprs::get_item::get_item;
77
use crate::expr::exprs::pack::pack;
88
use crate::expr::exprs::select::Select;
9-
use crate::expr::transform::traits::{ReduceRule, RewriteContext};
9+
use crate::expr::transform::rules::{ReduceRule, RewriteContext};
1010
use crate::expr::{Expression, ExpressionView};
1111

1212
/// Rule that removes Select expressions by converting them to Pack + GetItem.
@@ -61,7 +61,7 @@ mod tests {
6161
use crate::expr::exprs::pack::Pack;
6262
use crate::expr::exprs::root::root;
6363
use crate::expr::exprs::select::{Select, select};
64-
use crate::expr::transform::traits::{ReduceRule, SimpleRewriteContext};
64+
use crate::expr::transform::rules::{ReduceRule, SimpleRewriteContext};
6565

6666
#[test]
6767
fn test_remove_select_rule() {

vortex-array/src/expr/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::expr::exprs::pack::Pack;
2424
use crate::expr::exprs::root::Root;
2525
use crate::expr::exprs::select::Select;
2626
use crate::expr::exprs::select::transform::RemoveSelectRule;
27-
use crate::expr::transform::traits::{
27+
use crate::expr::transform::rules::{
2828
ChildReduceRule, ParentReduceRule, ReduceRule, RewriteContext,
2929
};
3030
use crate::expr::{ExprId, ExprVTable, Expression, VTable};

vortex-array/src/expr/transform/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ pub mod annotations;
66
pub mod immediate_access;
77
pub(crate) mod match_between;
88
mod partition;
9+
mod reducer;
910
mod replace;
10-
pub mod simplify;
11+
pub mod rules;
1112
mod simplify_typed;
12-
pub mod traits;
1313

1414
pub use partition::*;
1515
pub use replace::*;
16-
pub use simplify::*;
16+
pub use rules::*;
1717
pub use simplify_typed::*;
18-
pub use traits::*;
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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(&current, 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+
}

vortex-array/src/expr/transform/simplify.rs

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)