19
19
namespace peloton {
20
20
namespace optimizer {
21
21
22
-
23
22
// ===--------------------------------------------------------------------===//
24
23
// Group Binding Iterator
25
24
// ===--------------------------------------------------------------------===//
26
- GroupBindingIterator::GroupBindingIterator (Memo& memo, GroupID id,
25
+ GroupBindingIterator::GroupBindingIterator (Memo & memo, GroupID id,
27
26
std::shared_ptr<Pattern> pattern)
28
27
: BindingIterator(memo),
29
28
group_id_ (id),
@@ -52,7 +51,8 @@ bool GroupBindingIterator::HasNext() {
52
51
// Keep checking item iterators until we find a match
53
52
while (current_item_index_ < num_group_items_) {
54
53
current_iterator_.reset (new GroupExprBindingIterator (
55
- memo_, target_group_->GetLogicalExpressions ()[current_item_index_].get (),
54
+ memo_,
55
+ target_group_->GetLogicalExpressions ()[current_item_index_].get (),
56
56
pattern_));
57
57
58
58
if (current_iterator_->HasNext ()) {
@@ -78,24 +78,29 @@ std::shared_ptr<OperatorExpression> GroupBindingIterator::Next() {
78
78
// ===--------------------------------------------------------------------===//
79
79
// Item Binding Iterator
80
80
// ===--------------------------------------------------------------------===//
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)
84
83
: BindingIterator(memo),
85
84
gexpr_(gexpr),
86
85
pattern_(pattern),
87
86
first_(true ),
88
87
has_next_(false ),
89
88
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
+ }
93
92
94
93
const std::vector<GroupID> &child_groups = gexpr->GetChildGroupIDs ();
95
94
const std::vector<std::shared_ptr<Pattern>> &child_patterns =
96
95
pattern->Children ();
97
96
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 ());
99
104
100
105
// Find all bindings for children
101
106
children_bindings_.resize (child_groups.size (), {});
@@ -104,15 +109,16 @@ GroupExprBindingIterator::GroupExprBindingIterator(Memo& memo,
104
109
// Try to find a match in the given group
105
110
std::vector<std::shared_ptr<OperatorExpression>> &child_bindings =
106
111
children_bindings_[i];
107
- GroupBindingIterator iterator (memo_, child_groups[i],
108
- child_patterns[i]);
112
+ GroupBindingIterator iterator (memo_, child_groups[i], child_patterns[i]);
109
113
110
114
// Get all bindings
111
115
while (iterator.HasNext ()) {
112
116
child_bindings.push_back (iterator.Next ());
113
117
}
114
118
115
- if (child_bindings.size () == 0 ) return ;
119
+ if (child_bindings.size () == 0 ) {
120
+ return ;
121
+ }
116
122
117
123
current_binding_->PushChild (child_bindings[0 ]);
118
124
}
@@ -128,33 +134,37 @@ bool GroupExprBindingIterator::HasNext() {
128
134
}
129
135
130
136
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 ) {
134
140
const std::vector<std::shared_ptr<OperatorExpression>> &child_binding =
135
- children_bindings_[size - 1 - i ];
141
+ children_bindings_[first_modified_idx ];
136
142
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];
138
145
if (new_pos < child_binding.size ()) {
139
146
break ;
140
147
} else {
141
- children_bindings_pos_[size - 1 - i ] = 0 ;
148
+ children_bindings_pos_[first_modified_idx ] = 0 ;
142
149
}
143
150
}
144
151
145
- if (i == size ) {
152
+ if (first_modified_idx < 0 ) {
146
153
// We have explored all combinations of the child bindings
147
154
has_next_ = false ;
148
155
} 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++) {
150
159
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) {
154
164
const std::vector<std::shared_ptr<OperatorExpression>> &child_binding =
155
- children_bindings_[offset + j ];
165
+ children_bindings_[offset];
156
166
std::shared_ptr<OperatorExpression> binding =
157
- child_binding[children_bindings_pos_[offset + j ]];
167
+ child_binding[children_bindings_pos_[offset]];
158
168
current_binding_->PushChild (binding);
159
169
}
160
170
}
@@ -163,6 +173,7 @@ bool GroupExprBindingIterator::HasNext() {
163
173
}
164
174
165
175
std::shared_ptr<OperatorExpression> GroupExprBindingIterator::Next () {
176
+ LOG_DEBUG (" Current_binding size :%lu" , current_binding_->Children ().size ());
166
177
return current_binding_;
167
178
}
168
179
0 commit comments