1919namespace peloton {
2020namespace optimizer {
2121
22-
2322// ===--------------------------------------------------------------------===//
2423// Group Binding Iterator
2524// ===--------------------------------------------------------------------===//
26- GroupBindingIterator::GroupBindingIterator (Memo& memo, GroupID id,
25+ GroupBindingIterator::GroupBindingIterator (Memo & memo, GroupID id,
2726 std::shared_ptr<Pattern> pattern)
2827 : BindingIterator(memo),
2928 group_id_ (id),
@@ -52,7 +51,8 @@ bool GroupBindingIterator::HasNext() {
5251 // Keep checking item iterators until we find a match
5352 while (current_item_index_ < num_group_items_) {
5453 current_iterator_.reset (new GroupExprBindingIterator (
55- memo_, target_group_->GetLogicalExpressions ()[current_item_index_].get (),
54+ memo_,
55+ target_group_->GetLogicalExpressions ()[current_item_index_].get (),
5656 pattern_));
5757
5858 if (current_iterator_->HasNext ()) {
@@ -78,24 +78,29 @@ std::shared_ptr<OperatorExpression> GroupBindingIterator::Next() {
7878// ===--------------------------------------------------------------------===//
7979// Item Binding Iterator
8080// ===--------------------------------------------------------------------===//
81- GroupExprBindingIterator::GroupExprBindingIterator (Memo& memo,
82- GroupExpression *gexpr,
83- std::shared_ptr<Pattern> pattern)
81+ GroupExprBindingIterator::GroupExprBindingIterator (
82+ Memo &memo, GroupExpression *gexpr, std::shared_ptr<Pattern> pattern)
8483 : BindingIterator(memo),
8584 gexpr_(gexpr),
8685 pattern_(pattern),
8786 first_(true ),
8887 has_next_(false ),
8988 current_binding_(std::make_shared<OperatorExpression>(gexpr->Op ())) {
90- LOG_TRACE ( " Attempting to bind on group %d with expression of type %s " ,
91- gexpr-> GetGroupID (), gexpr-> Op (). GetName (). c_str ()) ;
92- if (gexpr-> Op (). GetType () != pattern-> Type ()) return ;
89+ if (gexpr-> Op (). GetType () != pattern-> Type ()) {
90+ return ;
91+ }
9392
9493 const std::vector<GroupID> &child_groups = gexpr->GetChildGroupIDs ();
9594 const std::vector<std::shared_ptr<Pattern>> &child_patterns =
9695 pattern->Children ();
9796
98- if (child_groups.size () != child_patterns.size ()) return ;
97+ if (child_groups.size () != child_patterns.size ()) {
98+ return ;
99+ }
100+ LOG_TRACE (
101+ " Attempting to bind on group %d with expression of type %s, children "
102+ " size %lu" ,
103+ gexpr->GetGroupID (), gexpr->Op ().GetName ().c_str (), child_groups.size ());
99104
100105 // Find all bindings for children
101106 children_bindings_.resize (child_groups.size (), {});
@@ -104,15 +109,16 @@ GroupExprBindingIterator::GroupExprBindingIterator(Memo& memo,
104109 // Try to find a match in the given group
105110 std::vector<std::shared_ptr<OperatorExpression>> &child_bindings =
106111 children_bindings_[i];
107- GroupBindingIterator iterator (memo_, child_groups[i],
108- child_patterns[i]);
112+ GroupBindingIterator iterator (memo_, child_groups[i], child_patterns[i]);
109113
110114 // Get all bindings
111115 while (iterator.HasNext ()) {
112116 child_bindings.push_back (iterator.Next ());
113117 }
114118
115- if (child_bindings.size () == 0 ) return ;
119+ if (child_bindings.size () == 0 ) {
120+ return ;
121+ }
116122
117123 current_binding_->PushChild (child_bindings[0 ]);
118124 }
@@ -128,33 +134,37 @@ bool GroupExprBindingIterator::HasNext() {
128134 }
129135
130136 if (has_next_) {
131- size_t size = children_bindings_pos_. size ();
132- size_t i ;
133- for (i = 0 ; i < size; ++i ) {
137+ // The first child to be modified
138+ int first_modified_idx = children_bindings_pos_. size () - 1 ;
139+ for (; first_modified_idx > = 0 ; --first_modified_idx ) {
134140 const std::vector<std::shared_ptr<OperatorExpression>> &child_binding =
135- children_bindings_[size - 1 - i ];
141+ children_bindings_[first_modified_idx ];
136142
137- size_t new_pos = ++children_bindings_pos_[size - 1 - i];
143+ // Try to increment idx from the back
144+ size_t new_pos = ++children_bindings_pos_[first_modified_idx];
138145 if (new_pos < child_binding.size ()) {
139146 break ;
140147 } else {
141- children_bindings_pos_[size - 1 - i ] = 0 ;
148+ children_bindings_pos_[first_modified_idx ] = 0 ;
142149 }
143150 }
144151
145- if (i == size ) {
152+ if (first_modified_idx < 0 ) {
146153 // We have explored all combinations of the child bindings
147154 has_next_ = false ;
148155 } else {
149- for (size_t j = 0 ; j<i; j++)
156+ // Pop all updated childrens
157+ for (size_t idx = first_modified_idx; idx < children_bindings_pos_.size ();
158+ idx++) {
150159 current_binding_->PopChild ();
151- // Replay to end
152- size_t offset = size - 1 - i;
153- for (size_t j = 0 ; j < i + 1 ; ++j) {
160+ }
161+ // Add new children to end
162+ for (size_t offset = first_modified_idx;
163+ offset < children_bindings_pos_.size (); ++offset) {
154164 const std::vector<std::shared_ptr<OperatorExpression>> &child_binding =
155- children_bindings_[offset + j ];
165+ children_bindings_[offset];
156166 std::shared_ptr<OperatorExpression> binding =
157- child_binding[children_bindings_pos_[offset + j ]];
167+ child_binding[children_bindings_pos_[offset]];
158168 current_binding_->PushChild (binding);
159169 }
160170 }
0 commit comments