1
- use std:: {
2
- collections:: { hash_map:: Entry , HashMap , HashSet } ,
3
- sync :: Arc ,
4
- } ;
1
+ use std:: any :: Any ;
2
+ use std :: collections:: hash_map:: Entry ;
3
+ use std :: collections :: { HashMap , HashSet } ;
4
+ use std :: sync :: Arc ;
5
5
6
6
use anyhow:: { bail, Context , Result } ;
7
7
use itertools:: Itertools ;
8
- use std:: any:: Any ;
9
8
use tracing:: trace;
10
9
11
- use crate :: {
12
- cost:: { Cost , Statistics } ,
13
- nodes:: { ArcPlanNode , ArcPredNode , NodeType , PlanNode , PlanNodeOrGroup } ,
14
- property:: PropertyBuilderAny ,
15
- } ;
16
-
17
10
use super :: optimizer:: { ExprId , GroupId , PredId } ;
11
+ use crate :: cost:: { Cost , Statistics } ;
12
+ use crate :: nodes:: { ArcPlanNode , ArcPredNode , NodeType , PlanNode , PlanNodeOrGroup } ;
13
+ use crate :: property:: PropertyBuilderAny ;
18
14
19
15
pub type ArcMemoPlanNode < T > = Arc < MemoPlanNode < T > > ;
20
16
21
- /// The RelNode representation in the memo table. Store children as group IDs. Equivalent to MExpr in Columbia/Cascades.
17
+ /// The RelNode representation in the memo table. Store children as group IDs. Equivalent to MExpr
18
+ /// in Columbia/Cascades.
22
19
#[ derive( Clone , Debug , PartialEq , Eq , Hash ) ]
23
20
pub struct MemoPlanNode < T : NodeType > {
24
21
pub typ : T ,
@@ -92,12 +89,13 @@ pub struct Group {
92
89
93
90
/// Trait for memo table implementations.
94
91
pub trait Memo < T : NodeType > : ' static + Send + Sync {
95
- /// Add an expression to the memo table. If the expression already exists, it will return the existing group id and
96
- /// expr id. Otherwise, a new group and expr will be created.
92
+ /// Add an expression to the memo table. If the expression already exists, it will return the
93
+ /// existing group id and expr id. Otherwise, a new group and expr will be created.
97
94
fn add_new_expr ( & mut self , rel_node : ArcPlanNode < T > ) -> ( GroupId , ExprId ) ;
98
95
99
- /// Add a new expression to an existing gruop. If the expression is a group, it will merge the two groups. Otherwise,
100
- /// it will add the expression to the group. Returns the expr id if the expression is not a group.
96
+ /// Add a new expression to an existing gruop. If the expression is a group, it will merge the
97
+ /// two groups. Otherwise, it will add the expression to the group. Returns the expr id if
98
+ /// the expression is not a group.
101
99
fn add_expr_to_group (
102
100
& mut self ,
103
101
rel_node : PlanNodeOrGroup < T > ,
@@ -126,8 +124,8 @@ pub trait Memo<T: NodeType>: 'static + Send + Sync {
126
124
/// Update the group info.
127
125
fn update_group_info ( & mut self , group_id : GroupId , group_info : GroupInfo ) ;
128
126
129
- /// Estimated plan space for the memo table, only useful when plan exploration budget is enabled.
130
- /// Returns number of expressions in the memo table.
127
+ /// Estimated plan space for the memo table, only useful when plan exploration budget is
128
+ /// enabled. Returns number of expressions in the memo table.
131
129
fn estimated_plan_space ( & self ) -> usize ;
132
130
133
131
// The below functions can be overwritten by the memo table implementation if there
@@ -155,15 +153,17 @@ pub trait Memo<T: NodeType>: 'static + Send + Sync {
155
153
get_best_group_binding_inner ( self , group_id, & mut post_process)
156
154
}
157
155
158
- /// Get all bindings of a predicate group. Will panic if the group contains more than one bindings. Note that we
159
- /// are currently in the refactor process of having predicates as a separate entity. If the representation stores
160
- /// predicates in the rel node children, the repr should use this function to get the predicate binding. Otherwise,
161
- /// use `ger_pred` for those predicates stored within the `predicates` field.
156
+ /// Get all bindings of a predicate group. Will panic if the group contains more than one
157
+ /// bindings. Note that we are currently in the refactor process of having predicates as a
158
+ /// separate entity. If the representation stores predicates in the rel node children, the
159
+ /// repr should use this function to get the predicate binding. Otherwise, use `ger_pred`
160
+ /// for those predicates stored within the `predicates` field.
162
161
fn get_predicate_binding ( & self , group_id : GroupId ) -> Option < ArcPlanNode < T > > {
163
162
get_predicate_binding_group_inner ( self , group_id, true )
164
163
}
165
164
166
- /// Get all bindings of a predicate group. Returns None if the group contains zero or more than one bindings.
165
+ /// Get all bindings of a predicate group. Returns None if the group contains zero or more than
166
+ /// one bindings.
167
167
fn try_get_predicate_binding ( & self , group_id : GroupId ) -> Option < ArcPlanNode < T > > {
168
168
get_predicate_binding_group_inner ( self , group_id, false )
169
169
}
@@ -381,21 +381,24 @@ impl<T: NodeType> NaiveMemo<T> {
381
381
}
382
382
}
383
383
384
- /// Get the next group id. Group id and expr id shares the same counter, so as to make it easier to debug...
384
+ /// Get the next group id. Group id and expr id shares the same counter, so as to make it easier
385
+ /// to debug...
385
386
fn next_group_id ( & mut self ) -> GroupId {
386
387
let id = self . group_expr_counter ;
387
388
self . group_expr_counter += 1 ;
388
389
GroupId ( id)
389
390
}
390
391
391
- /// Get the next expr id. Group id and expr id shares the same counter, so as to make it easier to debug...
392
+ /// Get the next expr id. Group id and expr id shares the same counter, so as to make it easier
393
+ /// to debug...
392
394
fn next_expr_id ( & mut self ) -> ExprId {
393
395
let id = self . group_expr_counter ;
394
396
self . group_expr_counter += 1 ;
395
397
ExprId ( id)
396
398
}
397
399
398
- /// Get the next pred id. Group id and expr id shares the same counter, so as to make it easier to debug...
400
+ /// Get the next pred id. Group id and expr id shares the same counter, so as to make it easier
401
+ /// to debug...
399
402
fn next_pred_id ( & mut self ) -> PredId {
400
403
let id = self . group_expr_counter ;
401
404
self . group_expr_counter += 1 ;
@@ -487,16 +490,18 @@ impl<T: NodeType> NaiveMemo<T> {
487
490
. insert ( * expr_id, Arc :: new ( new_expr. clone ( ) ) ) ;
488
491
self . expr_node_to_expr_id . remove ( & old_expr) ;
489
492
if let Some ( dup_expr) = self . expr_node_to_expr_id . get ( & new_expr) {
490
- // If new_expr == some_other_old_expr in the memo table, unless they belong to the same group,
491
- // we should merge the two groups. This should not happen. We should simply drop this expression.
493
+ // If new_expr == some_other_old_expr in the memo table, unless they belong
494
+ // to the same group, we should merge the two
495
+ // groups. This should not happen. We should simply drop this expression.
492
496
let dup_group_id = self . expr_id_to_group_id [ dup_expr] ;
493
497
if dup_group_id != * group_id {
494
498
pending_recursive_merge. push ( ( dup_group_id, * group_id) ) ;
495
499
}
496
500
self . expr_id_to_expr_node . remove ( expr_id) ;
497
501
self . expr_id_to_group_id . remove ( expr_id) ;
498
502
self . dup_expr_mapping . insert ( * expr_id, * dup_expr) ;
499
- new_expr_list. insert ( * dup_expr) ; // adding this temporarily -- should be removed once recursive merge finishes
503
+ new_expr_list. insert ( * dup_expr) ; // adding this temporarily -- should be
504
+ // removed once recursive merge finishes
500
505
} else {
501
506
self . expr_node_to_expr_id . insert ( new_expr, * expr_id) ;
502
507
new_expr_list. insert ( * expr_id) ;
@@ -509,7 +514,8 @@ impl<T: NodeType> NaiveMemo<T> {
509
514
group. group_exprs = new_expr_list;
510
515
}
511
516
for ( merge_from, merge_into) in pending_recursive_merge {
512
- // We need to reduce because each merge would probably invalidate some groups in the last loop iteration.
517
+ // We need to reduce because each merge would probably invalidate some groups in the
518
+ // last loop iteration.
513
519
let merge_from = self . reduce_group ( merge_from) ;
514
520
let merge_into = self . reduce_group ( merge_into) ;
515
521
self . merge_group_inner ( merge_into, merge_from) ;
@@ -526,9 +532,11 @@ impl<T: NodeType> NaiveMemo<T> {
526
532
. iter ( )
527
533
. map ( |child| {
528
534
match child {
529
- PlanNodeOrGroup :: Group ( group) => self . reduce_group ( * group) , // TODO: can I remove reduce?
535
+ // TODO: can I remove reduce?
536
+ PlanNodeOrGroup :: Group ( group) => self . reduce_group ( * group) ,
530
537
PlanNodeOrGroup :: PlanNode ( child) => {
531
- // No merge / modification to the memo should occur for the following operation
538
+ // No merge / modification to the memo should occur for the following
539
+ // operation
532
540
let ( group, _) = self
533
541
. add_new_group_expr_inner ( child. clone ( ) , None )
534
542
. expect ( "should not trigger merge group" ) ;
@@ -569,8 +577,8 @@ impl<T: NodeType> NaiveMemo<T> {
569
577
Ok ( ( group_id, expr_id) )
570
578
}
571
579
572
- /// This is inefficient: usually the optimizer should have a MemoRef instead of passing the full rel node. Should
573
- /// be only used for debugging purpose.
580
+ /// This is inefficient: usually the optimizer should have a MemoRef instead of passing the full
581
+ /// rel node. Should be only used for debugging purpose.
574
582
#[ cfg( test) ]
575
583
pub ( crate ) fn get_expr_info ( & self , rel_node : ArcPlanNode < T > ) -> ( GroupId , ExprId ) {
576
584
let children_group_ids = rel_node
@@ -662,9 +670,8 @@ impl<T: NodeType> NaiveMemo<T> {
662
670
#[ cfg( test) ]
663
671
mod tests {
664
672
665
- use crate :: nodes:: { PredNode , Value } ;
666
-
667
673
use super :: * ;
674
+ use crate :: nodes:: { PredNode , Value } ;
668
675
669
676
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
670
677
enum MemoTestRelTyp {
0 commit comments