Skip to content

Commit 197a1ef

Browse files
committed
Move rules onto array vtable so we can optimize during construction without a session
Signed-off-by: Nicholas Gates <[email protected]>
1 parent 3333268 commit 197a1ef

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ mod tests {
329329
let get_item_expr = get_item("b", pack_expr);
330330

331331
let result = get_item_expr
332-
.simplify(&DType::Struct(StructFields::empty(), NonNullable))
332+
.optimize_recursive(&DType::Struct(StructFields::empty(), NonNullable))
333333
.unwrap();
334334

335335
assert_eq!(result, lit(2));
@@ -345,7 +345,7 @@ mod tests {
345345

346346
let dtype = DType::Primitive(PType::I32, NonNullable);
347347

348-
let result = get_z.simplify(&dtype).unwrap();
348+
let result = get_z.optimize_recursive(&dtype).unwrap();
349349
assert_eq!(result, lit(4));
350350
}
351351

@@ -365,7 +365,7 @@ mod tests {
365365

366366
let dtype = DType::Primitive(PType::I32, NonNullable);
367367

368-
let result = get_final.simplify(&dtype).unwrap();
368+
let result = get_final.optimize_recursive(&dtype).unwrap();
369369
assert_eq!(result, lit(42));
370370
}
371371

@@ -380,7 +380,7 @@ mod tests {
380380

381381
let dtype = DType::Primitive(PType::I32, NonNullable);
382382

383-
let result = get_result.simplify(&dtype).unwrap();
383+
let result = get_result.optimize_recursive(&dtype).unwrap();
384384
let expected = checked_add(lit(1), lit(10));
385385
assert_eq!(&result, &expected);
386386
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ mod tests {
584584
DuplicateHandling::RightMost,
585585
);
586586

587-
let result = e.optimize_root(&dtype).unwrap();
587+
let result = e.optimize(&dtype).unwrap();
588588

589589
assert!(result.is::<Pack>());
590590
assert_eq!(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ mod tests {
461461
);
462462
let e = select(["a", "b"], root());
463463

464-
let result = e.simplify(&dtype).unwrap();
464+
let result = e.optimize_recursive(&dtype).unwrap();
465465

466466
assert!(result.is::<Pack>());
467467
assert!(result.return_dtype(&dtype).unwrap().is_nullable());
@@ -480,7 +480,7 @@ mod tests {
480480
);
481481
let e = select_exclude(["c"], root());
482482

483-
let result = e.simplify(&dtype).unwrap();
483+
let result = e.optimize_recursive(&dtype).unwrap();
484484

485485
assert!(result.is::<Pack>());
486486

vortex-array/src/expr/optimize.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ impl Expression {
2828
/// 1. `simplify_untyped` - type-independent simplifications
2929
/// 2. `simplify` - type-aware simplifications
3030
/// 3. `reduce` - abstract reduction rules via `ReduceNode`/`ReduceCtx`
31-
pub fn optimize_root(&self, scope: &DType) -> VortexResult<Expression> {
31+
pub fn optimize(&self, scope: &DType) -> VortexResult<Expression> {
3232
Ok(self
3333
.clone()
34-
.try_optimize_root(scope)?
34+
.try_optimize(scope)?
3535
.unwrap_or_else(|| self.clone()))
3636
}
3737

3838
/// Try to optimize the root expression node only, returning None if no optimizations applied.
39-
pub fn try_optimize_root(&self, scope: &DType) -> VortexResult<Option<Expression>> {
39+
pub fn try_optimize(&self, scope: &DType) -> VortexResult<Option<Expression>> {
4040
let cache = SimplifyCache {
4141
scope,
4242
dtype_cache: RefCell::new(HashMap::new()),
@@ -118,7 +118,7 @@ impl Expression {
118118
let mut any_optimizations = false;
119119

120120
// First optimize the root
121-
if let Some(optimized) = current.clone().try_optimize_root(scope)? {
121+
if let Some(optimized) = current.clone().try_optimize(scope)? {
122122
current = optimized;
123123
any_optimizations = true;
124124
}
@@ -140,7 +140,7 @@ impl Expression {
140140
any_optimizations = true;
141141

142142
// After updating children, try to optimize root again
143-
if let Some(optimized) = current.clone().try_optimize_root(scope)? {
143+
if let Some(optimized) = current.clone().try_optimize(scope)? {
144144
current = optimized;
145145
}
146146
}
@@ -282,8 +282,9 @@ impl ReduceCtx for ExpressionReduceCtx {
282282
.iter()
283283
.map(|c| {
284284
c.as_any()
285-
.downcast_ref::<Expression>()
286-
.vortex_expect("ReduceNode not an Expression")
285+
.downcast_ref::<ExpressionReduceNode>()
286+
.vortex_expect("ReduceNode not an ExpressionReduceNode")
287+
.expression
287288
.clone()
288289
})
289290
.collect::<Vec<_>>(),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ mod tests {
284284

285285
let split_a = partitioned.find_partition(&"a".into()).unwrap();
286286
assert_eq!(
287-
&split_a.simplify(&dtype).unwrap(),
287+
&split_a.optimize_recursive(&dtype).unwrap(),
288288
&pack(
289289
[
290290
("a_0", get_item("x", get_item("a", root()))),
@@ -326,7 +326,7 @@ mod tests {
326326
get_item("y", get_item("a", root())),
327327
select(["a", "b"], root()),
328328
);
329-
let expr = expr.simplify(&dtype).unwrap();
329+
let expr = expr.optimize_recursive(&dtype).unwrap();
330330
let partitioned = partition(expr, &dtype, annotate_scope_access(fields)).unwrap();
331331

332332
// One for id.a and id.b

vortex-array/src/optimizer/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ impl ArrayOptimizer for ArrayRef {
2828
Ok(self.clone().try_optimize()?.unwrap_or_else(|| self.clone()))
2929
}
3030

31-
#[expect(clippy::cognitive_complexity)]
3231
fn try_optimize(&self) -> VortexResult<Option<ArrayRef>> {
3332
let mut current_array = self.clone();
3433
let mut any_optimizations = false;

0 commit comments

Comments
 (0)