@@ -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 ) ]
@@ -25,7 +25,7 @@ impl BestCubePlan {
2525 }
2626 }
2727
28- pub fn initial_cost ( & self , enode : & LogicalPlanLanguage , top_down : bool ) -> CubePlanCost {
28+ pub fn initial_cost ( & self , enode : & LogicalPlanLanguage ) -> CubePlanCost {
2929 let table_scans = match enode {
3030 LogicalPlanLanguage :: TableScan ( _) => 1 ,
3131 _ => 0 ,
@@ -52,8 +52,7 @@ impl BestCubePlan {
5252 } ;
5353
5454 let non_pushed_down_limit_sort = match enode {
55- LogicalPlanLanguage :: Limit ( _) if !top_down => 1 ,
56- LogicalPlanLanguage :: Sort ( _) if top_down => 1 ,
55+ LogicalPlanLanguage :: Sort ( _) => 1 ,
5756 _ => 0 ,
5857 } ;
5958
@@ -247,7 +246,6 @@ impl BestCubePlan {
247246
248247#[ derive( Clone , Copy ) ]
249248pub struct CubePlanCostOptions {
250- top_down : bool ,
251249 penalize_post_processing : bool ,
252250}
253251
@@ -311,73 +309,13 @@ pub enum CubePlanState {
311309 Wrapper ,
312310}
313311
314- impl CubePlanState {
315- pub fn add_child ( & self , other : & Self ) -> Self {
316- match ( self , other) {
317- ( CubePlanState :: Wrapper , _) => CubePlanState :: Wrapper ,
318- ( _, CubePlanState :: Wrapped ) => CubePlanState :: Wrapped ,
319- ( CubePlanState :: Wrapped , _) => CubePlanState :: Wrapped ,
320- ( CubePlanState :: Unwrapped ( a) , _) => CubePlanState :: Unwrapped ( * a) ,
321- }
322- }
323- }
324-
325312#[ derive( Debug , Clone , Eq , Hash , PartialEq ) ]
326313pub enum SortState {
327314 None ,
328315 Current ,
329316 DirectChild ,
330317}
331318
332- impl SortState {
333- pub fn add_child ( & self , other : & Self ) -> Self {
334- match ( self , other) {
335- ( Self :: Current , _) => Self :: Current ,
336- ( _, Self :: Current ) | ( Self :: DirectChild , _) => Self :: DirectChild ,
337- _ => Self :: None ,
338- }
339- }
340- }
341-
342- #[ derive( Debug , Clone , Eq , PartialEq ) ]
343- pub struct CubePlanCostAndState {
344- pub cost : CubePlanCost ,
345- pub state : CubePlanState ,
346- pub sort_state : SortState ,
347- }
348-
349- impl PartialOrd for CubePlanCostAndState {
350- fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
351- Some ( self . cost . cmp ( & other. cost ) )
352- }
353- }
354-
355- impl Ord for CubePlanCostAndState {
356- fn cmp ( & self , other : & Self ) -> std:: cmp:: Ordering {
357- self . cost . cmp ( & other. cost )
358- }
359- }
360-
361- impl CubePlanCostAndState {
362- pub fn add_child ( & self , other : & Self ) -> Self {
363- Self {
364- cost : self . cost . add_child ( & other. cost ) ,
365- state : self . state . add_child ( & other. state ) ,
366- sort_state : self . sort_state . add_child ( & other. sort_state ) ,
367- }
368- }
369-
370- pub fn finalize ( & self , enode : & LogicalPlanLanguage , options : CubePlanCostOptions ) -> Self {
371- Self {
372- cost : self
373- . cost
374- . finalize ( & self . state , & self . sort_state , enode, options) ,
375- state : self . state . clone ( ) ,
376- sort_state : self . sort_state . clone ( ) ,
377- }
378- }
379- }
380-
381319impl CubePlanCost {
382320 pub fn add_child ( & self , other : & Self ) -> Self {
383321 Self {
@@ -468,7 +406,7 @@ impl CubePlanCost {
468406 } ,
469407 non_pushed_down_limit_sort : match sort_state {
470408 SortState :: DirectChild => self . non_pushed_down_limit_sort ,
471- SortState :: Current if options . top_down => self . non_pushed_down_limit_sort ,
409+ SortState :: Current => self . non_pushed_down_limit_sort ,
472410 _ => 0 ,
473411 } ,
474412 // Don't track state here: we want representation that have fewer wrappers with zero members _in total_
@@ -519,60 +457,6 @@ impl CubePlanCost {
519457 }
520458}
521459
522- impl CostFunction < LogicalPlanLanguage > for BestCubePlan {
523- type Cost = CubePlanCostAndState ;
524- fn cost < C > ( & mut self , enode : & LogicalPlanLanguage , mut costs : C ) -> Self :: Cost
525- where
526- C : FnMut ( Id ) -> Self :: Cost ,
527- {
528- let ast_size_outside_wrapper = match enode {
529- LogicalPlanLanguage :: Aggregate ( _) => 1 ,
530- LogicalPlanLanguage :: Projection ( _) => 1 ,
531- LogicalPlanLanguage :: Limit ( _) => 1 ,
532- LogicalPlanLanguage :: Sort ( _) => 1 ,
533- LogicalPlanLanguage :: Filter ( _) => 1 ,
534- LogicalPlanLanguage :: Join ( _) => 1 ,
535- LogicalPlanLanguage :: CrossJoin ( _) => 1 ,
536- LogicalPlanLanguage :: Union ( _) => 1 ,
537- LogicalPlanLanguage :: Window ( _) => 1 ,
538- LogicalPlanLanguage :: Subquery ( _) => 1 ,
539- LogicalPlanLanguage :: Distinct ( _) => 1 ,
540- _ => 0 ,
541- } ;
542-
543- let cost = self . initial_cost ( enode, false ) ;
544- let initial_cost = CubePlanCostAndState {
545- cost,
546- state : match enode {
547- LogicalPlanLanguage :: CubeScanWrapped ( CubeScanWrapped ( true ) ) => {
548- CubePlanState :: Wrapped
549- }
550- LogicalPlanLanguage :: CubeScanWrapper ( _) => CubePlanState :: Wrapper ,
551- _ => CubePlanState :: Unwrapped ( ast_size_outside_wrapper) ,
552- } ,
553- sort_state : match enode {
554- LogicalPlanLanguage :: Sort ( _) => SortState :: Current ,
555- _ => SortState :: None ,
556- } ,
557- } ;
558- let res = enode
559- . children ( )
560- . iter ( )
561- . fold ( initial_cost. clone ( ) , |cost, id| {
562- let child = costs ( * id) ;
563- cost. add_child ( & child)
564- } )
565- . finalize (
566- enode,
567- CubePlanCostOptions {
568- top_down : false ,
569- penalize_post_processing : self . penalize_post_processing ,
570- } ,
571- ) ;
572- res
573- }
574- }
575-
576460pub trait TopDownCost : Clone + Debug + PartialOrd {
577461 fn add ( & self , other : & Self ) -> Self ;
578462}
@@ -901,7 +785,7 @@ impl TopDownState<LogicalPlanLanguage> for CubePlanTopDownState {
901785
902786impl TopDownCostFunction < LogicalPlanLanguage , CubePlanTopDownState , CubePlanCost > for BestCubePlan {
903787 fn cost ( & self , node : & LogicalPlanLanguage ) -> CubePlanCost {
904- self . initial_cost ( node, true )
788+ self . initial_cost ( node)
905789 }
906790
907791 fn finalize (
@@ -916,7 +800,6 @@ impl TopDownCostFunction<LogicalPlanLanguage, CubePlanTopDownState, CubePlanCost
916800 & state. limit ,
917801 node,
918802 CubePlanCostOptions {
919- top_down : true ,
920803 penalize_post_processing : self . penalize_post_processing ,
921804 } ,
922805 )
0 commit comments