forked from Uradouby/cs660-pa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoin.cpp
96 lines (84 loc) · 2.43 KB
/
Join.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
#include <db/Join.h>
using namespace db;
Join::Join(JoinPredicate *p, DbIterator *child1, DbIterator *child2) {
// TODO pa3.1: some code goes here
predicate = p;
outer = child1;
inner = child2;
td = TupleDesc::merge(outer->getTupleDesc(), inner->getTupleDesc());
}
JoinPredicate *Join::getJoinPredicate() {
// TODO pa3.1: some code goes here
return predicate;
}
std::string Join::getJoinField1Name() {
// TODO pa3.1: some code goes here
return outer->getTupleDesc().getFieldName(predicate->getField1());
}
std::string Join::getJoinField2Name() {
// TODO pa3.1: some code goes here
return inner->getTupleDesc().getFieldName(predicate->getField2());
}
const TupleDesc &Join::getTupleDesc() const {
// TODO pa3.1: some code goes here
return td;
}
void Join::open() {
// TODO pa3.1: some code goes here
outer->open();
inner->open();
Operator::open();
}
void Join::close() {
// TODO pa3.1: some code goes here
outer->close();
inner->close();
Operator::close();
}
void Join::rewind() {
// TODO pa3.1: some code goes here
outer->rewind();
inner->rewind();
}
std::vector<DbIterator *> Join::getChildren() {
// TODO pa3.1: some code goes here
return {outer, inner};
}
void Join::setChildren(std::vector<DbIterator *> children) {
// TODO pa3.1: some code goes here
outer = children[0];
inner = children[1];
td = TupleDesc::merge(outer->getTupleDesc(), inner->getTupleDesc());
}
std::optional<Tuple> Join::fetchNext() {
// TODO pa3.1: some code goes here
while (outerTuple.has_value() || outer->hasNext()) {
if (!outerTuple.has_value()) {
outerTuple = outer->next();
}
while (inner->hasNext()) {
Tuple innerTuple = inner->next();
if (!predicate->filter(&outerTuple.value(), &innerTuple)) {
continue;
}
Tuple tuple(getTupleDesc());
int i = 0;
auto it1 = outerTuple->begin();
while (it1 != outerTuple->end()) {
tuple.setField(i, *it1);
++i;
++it1;
}
auto it2 = innerTuple.begin();
while (it2 != innerTuple.end()) {
tuple.setField(i, *it2);
++i;
++it2;
}
return tuple;
}
outerTuple = std::nullopt;
inner->rewind();
}
return std::nullopt;
}