@@ -5,7 +5,6 @@ use std::borrow::Cow;
55use std:: sync:: Arc ;
66
77use vortex_array:: dtype:: DType ;
8- use vortex_array:: expr:: Expression ;
98use vortex_array:: expr:: label_tree;
109use vortex_error:: VortexResult ;
1110
@@ -16,6 +15,7 @@ use crate::plan::LazyPlanChildren;
1615use crate :: plan:: Plan ;
1716use crate :: plan:: PlanRef ;
1817use crate :: plan:: new_plan;
18+ use crate :: plan:: optimizer:: PlanParentReduceRule ;
1919
2020/// A physical plan with one child per row chunk.
2121pub struct ChunkedPlan {
@@ -63,26 +63,6 @@ impl Plan for ChunkedPlan {
6363 Ok ( Arc :: new ( self . with_chunks ( self . dtype . clone ( ) , chunks) ) )
6464 }
6565
66- fn optimize_expression ( & self , expression : & Expression ) -> VortexResult < Option < PlanRef > > {
67- let references_row_idx = label_tree (
68- expression,
69- |node| node. is :: < RowIdx > ( ) ,
70- |acc, & child| acc | child,
71- )
72- . get ( expression)
73- . copied ( )
74- . unwrap_or ( false ) ;
75- if references_row_idx {
76- return Ok ( None ) ;
77- }
78-
79- let dtype = expression. return_dtype ( & self . dtype ) ?;
80- let chunks = self
81- . chunks
82- . try_map ( |_, chunk| ExpressionPlan :: try_new ( expression. clone ( ) , chunk) ?. optimize ( ) ) ?;
83- Ok ( Some ( Arc :: new ( self . with_chunks ( dtype, chunks) ) ) )
84- }
85-
8666 fn dtype ( & self ) -> & DType {
8767 & self . dtype
8868 }
@@ -106,3 +86,37 @@ impl Plan for ChunkedPlan {
10686 Cow :: Owned ( format ! ( "chunks[{index}]" ) )
10787 }
10888}
89+
90+ /// Pushes an expression through every chunk of a chunked plan.
91+ #[ derive( Debug ) ]
92+ pub ( crate ) struct ExpressionChunkedRule ;
93+
94+ impl PlanParentReduceRule < ChunkedPlan > for ExpressionChunkedRule {
95+ type Parent = ExpressionPlan ;
96+
97+ fn reduce_parent (
98+ & self ,
99+ child : & ChunkedPlan ,
100+ parent : & ExpressionPlan ,
101+ _child_idx : usize ,
102+ ) -> VortexResult < Option < PlanRef > > {
103+ let expression = parent. expression ( ) ;
104+ let references_row_idx = label_tree (
105+ expression,
106+ |node| node. is :: < RowIdx > ( ) ,
107+ |acc, & child| acc | child,
108+ )
109+ . get ( expression)
110+ . copied ( )
111+ . unwrap_or ( false ) ;
112+ if references_row_idx {
113+ return Ok ( None ) ;
114+ }
115+
116+ let dtype = expression. return_dtype ( & child. dtype ) ?;
117+ let chunks = child
118+ . chunks
119+ . try_map ( |_, chunk| ExpressionPlan :: try_new ( expression. clone ( ) , chunk) ?. optimize ( ) ) ?;
120+ Ok ( Some ( Arc :: new ( child. with_chunks ( dtype, chunks) ) ) )
121+ }
122+ }
0 commit comments