11// SPDX-License-Identifier: Apache-2.0
22// SPDX-FileCopyrightText: Copyright the Vortex contributors
33
4- use std:: sync:: Arc ;
5-
64use vortex_error:: VortexResult ;
75
6+ use crate :: ArrayRef ;
87use crate :: vtable:: VTable ;
9- use crate :: { Array , ArrayRef } ;
10-
11- impl dyn Array + ' _ {
12- /// Optimize this array by applying optimization rules recursively to its children in a single
13- /// bottom-up pass.
14- pub fn optimize ( & self ) -> VortexResult < ArrayRef > {
15- let slf = self . to_array ( ) ;
16- let children = self . children ( ) ;
17-
18- let mut new_children = Vec :: with_capacity ( children. len ( ) ) ;
19- let mut children_modified = false ;
20- for ( idx, child) in children. iter ( ) . enumerate ( ) {
21- let child = child. optimize ( ) ?;
22-
23- // Check if the child can reduce us (its parent), and if so bail early.
24- if let Some ( reduced) = child. reduce_parent ( & slf, idx) ? {
25- return Ok ( reduced) ;
26- }
27-
28- if !Arc :: ptr_eq ( & child, & children[ idx] ) {
29- children_modified = true ;
30- }
31- new_children. push ( child) ;
32- }
33-
34- if children_modified {
35- return self . with_children ( & new_children) ;
36- }
37-
38- Ok ( slf)
39- }
40- }
418
429/// An optimizer rule that tries to reduce/replace a parent array where the implementer is a
4310/// child array in the `CHILD_IDX` position of the parent array.
@@ -63,9 +30,11 @@ mod tests {
6330 use vortex_dtype:: PTypeDowncast ;
6431 use vortex_vector:: VectorOps ;
6532
66- use crate :: IntoArray ;
6733 use crate :: arrays:: { BoolArray , MaskedArray , PrimitiveArray } ;
34+ use crate :: expr:: session:: ExprSession ;
35+ use crate :: expr:: transform:: ExprOptimizer ;
6836 use crate :: validity:: Validity ;
37+ use crate :: { ArraySession , IntoArray } ;
6938
7039 #[ test]
7140 fn test_masked_pushdown ( ) {
@@ -78,8 +47,16 @@ mod tests {
7847 )
7948 . unwrap ( ) ;
8049
81- let result = masked. optimize ( ) . unwrap ( ) ;
82- assert_eq ! ( masked. dtype( ) , result. dtype( ) ) ;
50+ let masked_dtype = masked. dtype ( ) . clone ( ) ;
51+
52+ // Use the new ArrayOptimizer via ArraySession
53+ let array_session = ArraySession :: default ( ) ;
54+ let expr_session = ExprSession :: default ( ) ;
55+ let expr_optimizer = ExprOptimizer :: new ( & expr_session) ;
56+ let optimizer = array_session. optimizer ( expr_optimizer) ;
57+
58+ let result = optimizer. optimize_array ( masked. into_array ( ) ) . unwrap ( ) ;
59+ assert_eq ! ( & masked_dtype, result. dtype( ) ) ;
8360 assert ! ( result. dtype( ) . is_nullable( ) ) ;
8461
8562 let vector = result. execute ( ) . unwrap ( ) . into_primitive ( ) . into_u32 ( ) ;
0 commit comments