-
Notifications
You must be signed in to change notification settings - Fork 10
/
NestedLoopsJoinOperator.cpp
493 lines (456 loc) · 23.6 KB
/
NestedLoopsJoinOperator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#include "relational_operators/NestedLoopsJoinOperator.hpp"
#include <cstddef>
#include <memory>
#include <utility>
#include <vector>
#include "catalog/CatalogRelationSchema.hpp"
#include "expressions/predicate/Predicate.hpp"
#include "expressions/scalar/Scalar.hpp"
#include "query_execution/QueryContext.hpp"
#include "query_execution/WorkOrderProtosContainer.hpp"
#include "query_execution/WorkOrdersContainer.hpp"
#include "relational_operators/WorkOrder.pb.h"
#include "storage/InsertDestination.hpp"
#include "storage/StorageBlock.hpp"
#include "storage/StorageBlockInfo.hpp"
#include "storage/StorageManager.hpp"
#include "storage/TupleStorageSubBlock.hpp"
#include "storage/ValueAccessor.hpp"
#include "types/containers/ColumnVectorsValueAccessor.hpp"
#include "utility/ColumnVectorCache.hpp"
#include "glog/logging.h"
#include "tmb/id_typedefs.h"
using std::unique_ptr;
using std::vector;
namespace quickstep {
bool NestedLoopsJoinOperator::getAllWorkOrders(
WorkOrdersContainer *container,
QueryContext *query_context,
StorageManager *storage_manager,
const tmb::client_id scheduler_client_id,
tmb::MessageBus *bus) {
if (left_relation_is_stored_ && right_relation_is_stored_) {
// Make sure we generate workorders only once.
if (all_workorders_generated_) {
return true;
}
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
for (const block_id left_block_id : left_relation_block_ids_[part_id]) {
for (const block_id right_block_id : right_relation_block_ids_) {
container->addNormalWorkOrder(
new NestedLoopsJoinWorkOrder(
query_id_,
left_input_relation_,
right_input_relation_,
part_id,
left_block_id,
right_block_id,
query_context->getPredicate(join_predicate_index_),
query_context->getScalarGroup(selection_index_),
query_context->getInsertDestination(
output_destination_index_),
storage_manager),
op_index_);
}
}
}
all_workorders_generated_ = true;
return true;
} else if (!(left_relation_is_stored_ || right_relation_is_stored_)) {
// Both relations are not stored.
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
std::vector<block_id>::size_type new_left_blocks
= left_relation_block_ids_[part_id].size() - num_left_workorders_generated_[part_id];
std::vector<block_id>::size_type new_right_blocks
= right_relation_block_ids_.size() - num_right_workorders_generated_[part_id];
std::size_t new_workorders = 0;
if (new_left_blocks > 0 && new_right_blocks > 0) {
// Blocks added to both left and right relations.
// First generate (left + new_left_blocks) * (new_right_blocks).
new_workorders = getAllWorkOrdersHelperBothNotStored(container,
query_context,
storage_manager,
part_id,
0,
left_relation_block_ids_[part_id].size(),
num_right_workorders_generated_[part_id],
right_relation_block_ids_.size());
// Now generate new_left_blocks * (right).
new_workorders += getAllWorkOrdersHelperBothNotStored(container,
query_context,
storage_manager,
part_id,
num_left_workorders_generated_[part_id],
left_relation_block_ids_[part_id].size(),
0,
num_right_workorders_generated_[part_id]);
} else if (new_left_blocks == 0 && new_right_blocks > 0) {
// Only new right blocks are added. Generate left * new_right_blocks.
new_workorders = getAllWorkOrdersHelperBothNotStored(container,
query_context,
storage_manager,
part_id,
0,
left_relation_block_ids_[part_id].size(),
num_right_workorders_generated_[part_id],
right_relation_block_ids_.size());
} else if (new_left_blocks > 0 && new_right_blocks == 0) {
// Generate new_left_blocks * right
new_workorders = getAllWorkOrdersHelperBothNotStored(container,
query_context,
storage_manager,
part_id,
num_left_workorders_generated_[part_id],
left_relation_block_ids_[part_id].size(),
0,
right_relation_block_ids_.size());
}
if (new_workorders > 0) {
num_left_workorders_generated_[part_id] = left_relation_block_ids_[part_id].size();
num_right_workorders_generated_[part_id] = right_relation_block_ids_.size();
}
}
return done_feeding_left_relation_ && done_feeding_right_relation_;
} else {
// Only one relation is a stored relation.
return getAllWorkOrdersHelperOneStored(container, query_context, storage_manager);
}
}
bool NestedLoopsJoinOperator::getAllWorkOrderProtos(WorkOrderProtosContainer *container) {
if (left_relation_is_stored_ && right_relation_is_stored_) {
// Make sure we generate workorders only once.
if (all_workorders_generated_) {
return true;
}
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
for (const block_id left_block_id : left_relation_block_ids_[part_id]) {
for (const block_id right_block_id : right_relation_block_ids_) {
container->addWorkOrderProto(createWorkOrderProto(part_id, left_block_id, right_block_id),
op_index_);
}
}
}
all_workorders_generated_ = true;
return true;
} else if (!(left_relation_is_stored_ || right_relation_is_stored_)) {
// Both relations are not stored.
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
const std::vector<block_id>::size_type new_left_blocks
= left_relation_block_ids_[part_id].size() - num_left_workorders_generated_[part_id];
const std::vector<block_id>::size_type new_right_blocks
= right_relation_block_ids_.size() - num_right_workorders_generated_[part_id];
std::size_t new_workorders = 0;
if (new_left_blocks > 0 && new_right_blocks > 0) {
// Blocks added to both left and right relations.
// First generate (left + new_left_blocks) * (new_right_blocks).
new_workorders =
getAllWorkOrderProtosHelperBothNotStored(container,
part_id,
0,
left_relation_block_ids_[part_id].size(),
num_right_workorders_generated_[part_id],
right_relation_block_ids_.size());
// Now generate new_left_blocks * (right).
new_workorders +=
getAllWorkOrderProtosHelperBothNotStored(container,
part_id,
num_left_workorders_generated_[part_id],
left_relation_block_ids_[part_id].size(),
0,
num_right_workorders_generated_[part_id]);
} else if (new_left_blocks == 0 && new_right_blocks > 0) {
// Only new right blocks are added. Generate left * new_right_blocks.
new_workorders =
getAllWorkOrderProtosHelperBothNotStored(container,
part_id,
0,
left_relation_block_ids_[part_id].size(),
num_right_workorders_generated_[part_id],
right_relation_block_ids_.size());
} else if (new_left_blocks > 0 && new_right_blocks == 0) {
// Generate new_left_blocks * right
new_workorders =
getAllWorkOrderProtosHelperBothNotStored(container,
part_id,
num_left_workorders_generated_[part_id],
left_relation_block_ids_[part_id].size(),
0,
right_relation_block_ids_.size());
}
if (new_workorders > 0) {
num_left_workorders_generated_[part_id] = left_relation_block_ids_[part_id].size();
num_right_workorders_generated_[part_id] = right_relation_block_ids_.size();
}
}
return done_feeding_left_relation_ && done_feeding_right_relation_;
} else {
// Only one relation is a stored relation.
return getAllWorkOrderProtosHelperOneStored(container);
}
}
std::size_t NestedLoopsJoinOperator::getAllWorkOrdersHelperBothNotStored(WorkOrdersContainer *container,
QueryContext *query_context,
StorageManager *storage_manager,
const partition_id part_id,
std::vector<block_id>::size_type left_min,
std::vector<block_id>::size_type left_max,
std::vector<block_id>::size_type right_min,
std::vector<block_id>::size_type right_max) {
DCHECK(!(left_relation_is_stored_ || right_relation_is_stored_));
DCHECK(left_min <= left_max);
DCHECK(right_min <= right_max);
for (std::vector<block_id>::size_type left_index = left_min;
left_index < left_max;
++left_index) {
for (std::vector<block_id>::size_type right_index = right_min;
right_index < right_max;
++right_index) {
container->addNormalWorkOrder(
new NestedLoopsJoinWorkOrder(
query_id_,
left_input_relation_,
right_input_relation_,
part_id,
left_relation_block_ids_[part_id][left_index],
right_relation_block_ids_[right_index],
query_context->getPredicate(join_predicate_index_),
query_context->getScalarGroup(selection_index_),
query_context->getInsertDestination(output_destination_index_),
storage_manager),
op_index_);
}
}
// Return the number of workorders produced.
return (left_max - left_min) * (right_max - right_min);
}
bool NestedLoopsJoinOperator::getAllWorkOrdersHelperOneStored(WorkOrdersContainer *container,
QueryContext *query_context,
StorageManager *storage_manager) {
DCHECK(left_relation_is_stored_ ^ right_relation_is_stored_);
DCHECK(query_context != nullptr);
const Predicate *join_predicate = query_context->getPredicate(join_predicate_index_);
const vector<unique_ptr<const Scalar>> &selection =
query_context->getScalarGroup(selection_index_);
InsertDestination *output_destination =
query_context->getInsertDestination(output_destination_index_);
if (left_relation_is_stored_) {
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
for (std::vector<block_id>::size_type right_index = num_right_workorders_generated_[part_id];
right_index < right_relation_block_ids_.size();
++right_index) {
for (const block_id left_block_id : left_relation_block_ids_[part_id]) {
container->addNormalWorkOrder(
new NestedLoopsJoinWorkOrder(
query_id_,
left_input_relation_,
right_input_relation_,
part_id,
left_block_id,
right_relation_block_ids_[right_index],
join_predicate,
selection,
output_destination,
storage_manager),
op_index_);
}
}
num_right_workorders_generated_[part_id] = right_relation_block_ids_.size();
}
return done_feeding_right_relation_;
} else {
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
for (std::vector<block_id>::size_type left_index = num_left_workorders_generated_[part_id];
left_index < left_relation_block_ids_[part_id].size();
++left_index) {
for (const block_id right_block_id : right_relation_block_ids_) {
container->addNormalWorkOrder(
new NestedLoopsJoinWorkOrder(query_id_,
left_input_relation_,
right_input_relation_,
part_id,
left_relation_block_ids_[part_id][left_index],
right_block_id,
join_predicate,
selection,
output_destination,
storage_manager),
op_index_);
}
}
num_left_workorders_generated_[part_id] = left_relation_block_ids_[part_id].size();
}
return done_feeding_left_relation_;
}
}
std::size_t NestedLoopsJoinOperator::getAllWorkOrderProtosHelperBothNotStored(
WorkOrderProtosContainer *container,
const partition_id part_id,
const std::vector<block_id>::size_type left_min,
const std::vector<block_id>::size_type left_max,
const std::vector<block_id>::size_type right_min,
const std::vector<block_id>::size_type right_max) {
DCHECK(!(left_relation_is_stored_ || right_relation_is_stored_));
DCHECK_LE(left_min, left_max);
DCHECK_LE(right_min, right_max);
for (std::vector<block_id>::size_type left_index = left_min;
left_index < left_max;
++left_index) {
for (std::vector<block_id>::size_type right_index = right_min;
right_index < right_max;
++right_index) {
container->addWorkOrderProto(
createWorkOrderProto(part_id, left_relation_block_ids_[part_id][left_index],
right_relation_block_ids_[right_index]),
op_index_);
}
}
// Return the number of workorders produced.
return (left_max - left_min) * (right_max - right_min);
}
bool NestedLoopsJoinOperator::getAllWorkOrderProtosHelperOneStored(WorkOrderProtosContainer *container) {
DCHECK(left_relation_is_stored_ ^ right_relation_is_stored_);
if (left_relation_is_stored_) {
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
for (std::vector<block_id>::size_type right_index = num_right_workorders_generated_[part_id];
right_index < right_relation_block_ids_.size();
++right_index) {
for (const block_id left_block_id : left_relation_block_ids_[part_id]) {
container->addWorkOrderProto(
createWorkOrderProto(part_id, left_block_id, right_relation_block_ids_[right_index]),
op_index_);
}
}
num_right_workorders_generated_[part_id] = right_relation_block_ids_.size();
}
return done_feeding_right_relation_;
} else {
for (partition_id part_id = 0; part_id < num_partitions_; ++part_id) {
for (std::vector<block_id>::size_type left_index = num_left_workorders_generated_[part_id];
left_index < left_relation_block_ids_[part_id].size();
++left_index) {
for (const block_id right_block_id : right_relation_block_ids_) {
container->addWorkOrderProto(
createWorkOrderProto(part_id, left_relation_block_ids_[part_id][left_index], right_block_id),
op_index_);
}
}
num_left_workorders_generated_[part_id] = left_relation_block_ids_[part_id].size();
}
return done_feeding_left_relation_;
}
}
serialization::WorkOrder* NestedLoopsJoinOperator::createWorkOrderProto(const partition_id part_id,
const block_id left_block,
const block_id right_block) {
serialization::WorkOrder *proto = new serialization::WorkOrder;
proto->set_work_order_type(serialization::NESTED_LOOP_JOIN);
proto->set_query_id(query_id_);
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::nested_loops_join_index, nested_loops_join_index_);
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::left_relation_id, left_input_relation_.getID());
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::right_relation_id, right_input_relation_.getID());
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::partition_id, part_id);
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::left_block_id, left_block);
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::right_block_id, right_block);
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::insert_destination_index,
output_destination_index_);
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::join_predicate_index, join_predicate_index_);
proto->SetExtension(serialization::NestedLoopsJoinWorkOrder::selection_index, selection_index_);
return proto;
}
template <bool LEFT_PACKED, bool RIGHT_PACKED>
void NestedLoopsJoinWorkOrder::executeHelper(const TupleStorageSubBlock &left_store,
const TupleStorageSubBlock &right_store) {
const tuple_id left_max_tid = left_store.getMaxTupleID();
const tuple_id right_max_tid = right_store.getMaxTupleID();
std::unique_ptr<ValueAccessor> left_accessor(left_store.createValueAccessor());
std::unique_ptr<ValueAccessor> right_accessor(right_store.createValueAccessor());
// Check each pair of tuples in the two blocks and build up a list of
// matches.
//
// TODO(chasseur): Vectorize evaluation of this join predicate.
std::vector<std::pair<tuple_id, tuple_id>> joined_tuple_ids;
for (tuple_id left_tid = 0; left_tid <= left_max_tid; ++left_tid) {
if (LEFT_PACKED || left_store.hasTupleWithID(left_tid)) {
// For each tuple in the left block...
for (tuple_id right_tid = 0; right_tid <= right_max_tid; ++right_tid) {
if (RIGHT_PACKED || right_store.hasTupleWithID(right_tid)) {
// For each tuple in the right block...
if (join_predicate_->matchesForJoinedTuples(*left_accessor,
left_tid,
*right_accessor,
right_tid)) {
joined_tuple_ids.emplace_back(left_tid, right_tid);
}
}
}
}
}
if (!joined_tuple_ids.empty()) {
// TODO(chasseur): If all the output expressions are ScalarAttributes,
// we could implement a similar fast-path to StorageBlock::selectSimple()
// that avoids a copy.
//
// TODO(chasseur): Depending on the selectivity of the join predicate,
// 'joined_tuple_ids' might be very long (up to the full cross-product of
// tuples in a pair of blocks in the most extreme case). The temporary
// result could then wind up taking a great deal of memory (significantly
// more than what would fit in a typical StorageBlock). It may be
// worthwhile to implement a heuristic where we only materialize temporary
// results N tuples at a time instead of all at once (where N is some
// number tuned high enough to good benefits from vectorized expression
// evaluation and data movement, but low enough that temporary memory
// requirements don't get out of hand).
ColumnVectorsValueAccessor temp_result;
std::unique_ptr<ColumnVectorCache> cv_cache = std::make_unique<ColumnVectorCache>();
for (vector<unique_ptr<const Scalar>>::const_iterator selection_cit = selection_.begin();
selection_cit != selection_.end();
++selection_cit) {
temp_result.addColumn((*selection_cit)->getAllValuesForJoin(left_accessor.get(),
right_accessor.get(),
joined_tuple_ids,
cv_cache.get()));
}
cv_cache.reset();
output_destination_->bulkInsertTuples(&temp_result);
}
}
void NestedLoopsJoinWorkOrder::execute() {
BlockReference left(
storage_manager_->getBlock(left_block_id_, left_input_relation_));
BlockReference right(
storage_manager_->getBlock(right_block_id_, right_input_relation_));
const TupleStorageSubBlock &left_store = left->getTupleStorageSubBlock();
const TupleStorageSubBlock &right_store = right->getTupleStorageSubBlock();
if (left_store.isPacked()) {
if (right_store.isPacked()) {
executeHelper<true, true>(left_store, right_store);
} else {
executeHelper<true, false>(left_store, right_store);
}
} else {
if (right_store.isPacked()) {
executeHelper<false, true>(left_store, right_store);
} else {
executeHelper<false, false>(left_store, right_store);
}
}
}
} // namespace quickstep