@@ -79,7 +79,9 @@ impl CommonSubexprEliminate {
7979 } )
8080 . collect :: < Result < Vec < _ > > > ( ) ?;
8181
82- let mut new_input = self . optimize ( input, optimizer_config) ?;
82+ let mut new_input = self
83+ . try_optimize ( input, optimizer_config) ?
84+ . unwrap_or_else ( || input. clone ( ) ) ;
8385 if !affected_id. is_empty ( ) {
8486 new_input = build_project_plan ( new_input, affected_id, expr_set) ?;
8587 }
@@ -94,6 +96,16 @@ impl OptimizerRule for CommonSubexprEliminate {
9496 plan : & LogicalPlan ,
9597 optimizer_config : & mut OptimizerConfig ,
9698 ) -> Result < LogicalPlan > {
99+ Ok ( self
100+ . try_optimize ( plan, optimizer_config) ?
101+ . unwrap_or_else ( || plan. clone ( ) ) )
102+ }
103+
104+ fn try_optimize (
105+ & self ,
106+ plan : & LogicalPlan ,
107+ optimizer_config : & mut OptimizerConfig ,
108+ ) -> Result < Option < LogicalPlan > > {
97109 let mut expr_set = ExprSet :: new ( ) ;
98110
99111 match plan {
@@ -113,11 +125,13 @@ impl OptimizerRule for CommonSubexprEliminate {
113125 optimizer_config,
114126 ) ?;
115127
116- Ok ( LogicalPlan :: Projection ( Projection :: try_new_with_schema (
117- pop_expr ( & mut new_expr) ?,
118- Arc :: new ( new_input) ,
119- schema. clone ( ) ,
120- ) ?) )
128+ Ok ( Some ( LogicalPlan :: Projection (
129+ Projection :: try_new_with_schema (
130+ pop_expr ( & mut new_expr) ?,
131+ Arc :: new ( new_input) ,
132+ schema. clone ( ) ,
133+ ) ?,
134+ ) ) )
121135 }
122136 LogicalPlan :: Filter ( filter) => {
123137 let input = filter. input ( ) ;
@@ -140,10 +154,10 @@ impl OptimizerRule for CommonSubexprEliminate {
140154 ) ?;
141155
142156 if let Some ( predicate) = pop_expr ( & mut new_expr) ?. pop ( ) {
143- Ok ( LogicalPlan :: Filter ( Filter :: try_new (
157+ Ok ( Some ( LogicalPlan :: Filter ( Filter :: try_new (
144158 predicate,
145159 Arc :: new ( new_input) ,
146- ) ?) )
160+ ) ?) ) )
147161 } else {
148162 Err ( DataFusionError :: Internal (
149163 "Failed to pop predicate expr" . to_string ( ) ,
@@ -166,11 +180,11 @@ impl OptimizerRule for CommonSubexprEliminate {
166180 optimizer_config,
167181 ) ?;
168182
169- Ok ( LogicalPlan :: Window ( Window {
183+ Ok ( Some ( LogicalPlan :: Window ( Window {
170184 input : Arc :: new ( new_input) ,
171185 window_expr : pop_expr ( & mut new_expr) ?,
172186 schema : schema. clone ( ) ,
173- } ) )
187+ } ) ) )
174188 }
175189 LogicalPlan :: Aggregate ( Aggregate {
176190 group_expr,
@@ -194,12 +208,14 @@ impl OptimizerRule for CommonSubexprEliminate {
194208 let new_aggr_expr = pop_expr ( & mut new_expr) ?;
195209 let new_group_expr = pop_expr ( & mut new_expr) ?;
196210
197- Ok ( LogicalPlan :: Aggregate ( Aggregate :: try_new_with_schema (
198- Arc :: new ( new_input) ,
199- new_group_expr,
200- new_aggr_expr,
201- schema. clone ( ) ,
202- ) ?) )
211+ Ok ( Some ( LogicalPlan :: Aggregate (
212+ Aggregate :: try_new_with_schema (
213+ Arc :: new ( new_input) ,
214+ new_group_expr,
215+ new_aggr_expr,
216+ schema. clone ( ) ,
217+ ) ?,
218+ ) ) )
203219 }
204220 LogicalPlan :: Sort ( Sort { expr, input, fetch } ) => {
205221 let input_schema = Arc :: clone ( input. schema ( ) ) ;
@@ -213,11 +229,11 @@ impl OptimizerRule for CommonSubexprEliminate {
213229 optimizer_config,
214230 ) ?;
215231
216- Ok ( LogicalPlan :: Sort ( Sort {
232+ Ok ( Some ( LogicalPlan :: Sort ( Sort {
217233 expr : pop_expr ( & mut new_expr) ?,
218234 input : Arc :: new ( new_input) ,
219235 fetch : * fetch,
220- } ) )
236+ } ) ) )
221237 }
222238 LogicalPlan :: Join ( _)
223239 | LogicalPlan :: CrossJoin ( _)
@@ -243,7 +259,11 @@ impl OptimizerRule for CommonSubexprEliminate {
243259 | LogicalPlan :: Extension ( _)
244260 | LogicalPlan :: Prepare ( _) => {
245261 // apply the optimization to all inputs of the plan
246- utils:: optimize_children ( self , plan, optimizer_config)
262+ Ok ( Some ( utils:: optimize_children (
263+ self ,
264+ plan,
265+ optimizer_config,
266+ ) ?) )
247267 }
248268 }
249269 }
0 commit comments