-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnested_loop_join_executor.cpp
More file actions
69 lines (64 loc) · 2.32 KB
/
Copy pathnested_loop_join_executor.cpp
File metadata and controls
69 lines (64 loc) · 2.32 KB
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
#include "../include/nested_loop_join_executor.h"
NestedLoopJoinExecutor::NestedLoopJoinExecutor(
AbstractExecutor *left_child_executor,
AbstractExecutor *right_child_executor, const std::string join_key)
: left_(left_child_executor),
right_(right_child_executor),
join_key_(join_key){};
void NestedLoopJoinExecutor::Init() {
outerTuplePresent = true;
innerTuplePresent = false;
left_->Init();
right_->Init();
}
// Check if key is same while joining in tables
bool NestedLoopJoinExecutor::checkKeyIsSameInJoin(const Tuple *inner_tuple, const Tuple *outer_tuple) {
return (("id" == join_key_ && inner_tuple->id == outer_tuple->id) ||
("val1" == join_key_ && inner_tuple->val1 == outer_tuple->val1) ||
("val2" == join_key_ && inner_tuple->val2 == outer_tuple->val2));
}
// Extract Next tuple. If tuple is present -> return true
// If tuple if not present -> return false
bool NestedLoopJoinExecutor::Next(Tuple *tuple) {
if (!outerTuplePresent && !innerTuplePresent) {
return false;
}
// If tuple of outer table is present
if(outerTuplePresent) {
// If the right table has next tuple
while (right_->Next(tuple)) {
Tuple innerTableTuple;
// If the left table has next tuple
while (left_->Next(&innerTableTuple)) {
// check if the table row is same after the join
if (checkKeyIsSameInJoin(&innerTableTuple, tuple)) {
*tuple = Tuple(innerTableTuple);
outerTuplePresent = false;
innerTuplePresent = true;
return true;
}
}
// Re-initiate the left database index
left_->Init();
innerTuplePresent = false;
}
outerTuplePresent = true;
innerTuplePresent = false;
} else {
// If tuple is present in left Child
Tuple innerTableTuple = *tuple;
while (left_->Next(&innerTableTuple)) {
if (checkKeyIsSameInJoin(&innerTableTuple, tuple)) {
*tuple = Tuple(innerTableTuple);
outerTuplePresent = false;
innerTuplePresent = true;
return true;
}
}
left_->Init();
outerTuplePresent = true;
innerTuplePresent = false;
if (Next(tuple)) return true;
}
return false;
}