@@ -8,7 +8,7 @@ use crate::{
88 } ,
99 transport:: { MetaContext , V1CubeMetaDimensionExt } ,
1010} ;
11- use egg:: { Analysis , CostFunction , EGraph , Id , Language , RecExpr } ;
11+ use egg:: { Analysis , EGraph , Id , Language , RecExpr } ;
1212use indexmap:: IndexSet ;
1313
1414#[ derive( Debug ) ]
@@ -21,7 +21,7 @@ impl BestCubePlan {
2121 Self { meta_context }
2222 }
2323
24- pub fn initial_cost ( & self , enode : & LogicalPlanLanguage , top_down : bool ) -> CubePlanCost {
24+ pub fn initial_cost ( & self , enode : & LogicalPlanLanguage ) -> CubePlanCost {
2525 let table_scans = match enode {
2626 LogicalPlanLanguage :: TableScan ( _) => 1 ,
2727 _ => 0 ,
@@ -48,8 +48,7 @@ impl BestCubePlan {
4848 } ;
4949
5050 let non_pushed_down_limit_sort = match enode {
51- LogicalPlanLanguage :: Limit ( _) if !top_down => 1 ,
52- LogicalPlanLanguage :: Sort ( _) if top_down => 1 ,
51+ LogicalPlanLanguage :: Sort ( _) => 1 ,
5352 _ => 0 ,
5453 } ;
5554
@@ -297,73 +296,13 @@ pub enum CubePlanState {
297296 Wrapper ,
298297}
299298
300- impl CubePlanState {
301- pub fn add_child ( & self , other : & Self ) -> Self {
302- match ( self , other) {
303- ( CubePlanState :: Wrapper , _) => CubePlanState :: Wrapper ,
304- ( _, CubePlanState :: Wrapped ) => CubePlanState :: Wrapped ,
305- ( CubePlanState :: Wrapped , _) => CubePlanState :: Wrapped ,
306- ( CubePlanState :: Unwrapped ( a) , _) => CubePlanState :: Unwrapped ( * a) ,
307- }
308- }
309- }
310-
311299#[ derive( Debug , Clone , Eq , Hash , PartialEq ) ]
312300pub enum SortState {
313301 None ,
314302 Current ,
315303 DirectChild ,
316304}
317305
318- impl SortState {
319- pub fn add_child ( & self , other : & Self ) -> Self {
320- match ( self , other) {
321- ( Self :: Current , _) => Self :: Current ,
322- ( _, Self :: Current ) | ( Self :: DirectChild , _) => Self :: DirectChild ,
323- _ => Self :: None ,
324- }
325- }
326- }
327-
328- #[ derive( Debug , Clone , Eq , PartialEq ) ]
329- pub struct CubePlanCostAndState {
330- pub cost : CubePlanCost ,
331- pub state : CubePlanState ,
332- pub sort_state : SortState ,
333- }
334-
335- impl PartialOrd for CubePlanCostAndState {
336- fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
337- Some ( self . cost . cmp ( & other. cost ) )
338- }
339- }
340-
341- impl Ord for CubePlanCostAndState {
342- fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
343- self . cost . cmp ( & other. cost )
344- }
345- }
346-
347- impl CubePlanCostAndState {
348- pub fn add_child ( & self , other : & Self ) -> Self {
349- Self {
350- cost : self . cost . add_child ( & other. cost ) ,
351- state : self . state . add_child ( & other. state ) ,
352- sort_state : self . sort_state . add_child ( & other. sort_state ) ,
353- }
354- }
355-
356- pub fn finalize ( & self , enode : & LogicalPlanLanguage ) -> Self {
357- Self {
358- cost : self
359- . cost
360- . finalize ( & self . state , & self . sort_state , enode, false ) ,
361- state : self . state . clone ( ) ,
362- sort_state : self . sort_state . clone ( ) ,
363- }
364- }
365- }
366-
367306impl CubePlanCost {
368307 pub fn add_child ( & self , other : & Self ) -> Self {
369308 Self {
@@ -419,7 +358,6 @@ impl CubePlanCost {
419358 state : & CubePlanState ,
420359 sort_state : & SortState ,
421360 enode : & LogicalPlanLanguage ,
422- top_down : bool ,
423361 ) -> Self {
424362 Self {
425363 replacers : self . replacers ,
@@ -440,7 +378,7 @@ impl CubePlanCost {
440378 } ,
441379 non_pushed_down_limit_sort : match sort_state {
442380 SortState :: DirectChild => self . non_pushed_down_limit_sort ,
443- SortState :: Current if top_down => self . non_pushed_down_limit_sort ,
381+ SortState :: Current => self . non_pushed_down_limit_sort ,
444382 _ => 0 ,
445383 } ,
446384 // Don't track state here: we want representation that have fewer wrappers with zero members _in total_
@@ -495,54 +433,6 @@ impl CubePlanCost {
495433 }
496434}
497435
498- impl CostFunction < LogicalPlanLanguage > for BestCubePlan {
499- type Cost = CubePlanCostAndState ;
500- fn cost < C > ( & mut self , enode : & LogicalPlanLanguage , mut costs : C ) -> Self :: Cost
501- where
502- C : FnMut ( Id ) -> Self :: Cost ,
503- {
504- let ast_size_outside_wrapper = match enode {
505- LogicalPlanLanguage :: Aggregate ( _) => 1 ,
506- LogicalPlanLanguage :: Projection ( _) => 1 ,
507- LogicalPlanLanguage :: Limit ( _) => 1 ,
508- LogicalPlanLanguage :: Sort ( _) => 1 ,
509- LogicalPlanLanguage :: Filter ( _) => 1 ,
510- LogicalPlanLanguage :: Join ( _) => 1 ,
511- LogicalPlanLanguage :: CrossJoin ( _) => 1 ,
512- LogicalPlanLanguage :: Union ( _) => 1 ,
513- LogicalPlanLanguage :: Window ( _) => 1 ,
514- LogicalPlanLanguage :: Subquery ( _) => 1 ,
515- LogicalPlanLanguage :: Distinct ( _) => 1 ,
516- _ => 0 ,
517- } ;
518-
519- let cost = self . initial_cost ( enode, false ) ;
520- let initial_cost = CubePlanCostAndState {
521- cost,
522- state : match enode {
523- LogicalPlanLanguage :: CubeScanWrapped ( CubeScanWrapped ( true ) ) => {
524- CubePlanState :: Wrapped
525- }
526- LogicalPlanLanguage :: CubeScanWrapper ( _) => CubePlanState :: Wrapper ,
527- _ => CubePlanState :: Unwrapped ( ast_size_outside_wrapper) ,
528- } ,
529- sort_state : match enode {
530- LogicalPlanLanguage :: Sort ( _) => SortState :: Current ,
531- _ => SortState :: None ,
532- } ,
533- } ;
534- let res = enode
535- . children ( )
536- . iter ( )
537- . fold ( initial_cost. clone ( ) , |cost, id| {
538- let child = costs ( * id) ;
539- cost. add_child ( & child)
540- } )
541- . finalize ( enode) ;
542- res
543- }
544- }
545-
546436pub trait TopDownCost : Clone + Debug + PartialOrd {
547437 fn add ( & self , other : & Self ) -> Self ;
548438}
@@ -871,7 +761,7 @@ impl TopDownState<LogicalPlanLanguage> for CubePlanTopDownState {
871761
872762impl TopDownCostFunction < LogicalPlanLanguage , CubePlanTopDownState , CubePlanCost > for BestCubePlan {
873763 fn cost ( & self , node : & LogicalPlanLanguage ) -> CubePlanCost {
874- self . initial_cost ( node, true )
764+ self . initial_cost ( node)
875765 }
876766
877767 fn finalize (
@@ -880,6 +770,6 @@ impl TopDownCostFunction<LogicalPlanLanguage, CubePlanTopDownState, CubePlanCost
880770 node : & LogicalPlanLanguage ,
881771 state : & CubePlanTopDownState ,
882772 ) -> CubePlanCost {
883- CubePlanCost :: finalize ( & cost, & state. wrapped , & state. limit , node, true )
773+ CubePlanCost :: finalize ( & cost, & state. wrapped , & state. limit , node)
884774 }
885775}
0 commit comments