-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortMergeJoin.java
More file actions
210 lines (186 loc) · 7.66 KB
/
SortMergeJoin.java
File metadata and controls
210 lines (186 loc) · 7.66 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
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
package qp.operators;
import qp.utils.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class SortMergeJoin extends Join {
private ExternalSort left_sorter = null; // Operator to sort tuples
private ExternalSort right_sorter = null; // Operator to sort tuples
int batchsize; // Number of tuples per out batch
Batch leftInbatch = null, rightInbatch = null; // Input batches
ArrayList<Integer> leftindex; // Indices of the join attributes in left table
ArrayList<Integer> rightindex; // Indices of the join attributes in right table
ArrayList<Tuple> rightMatchingTuples = new ArrayList<>(); // Right tuples being matched currently
int rightIndex = 0; // Current index to rightMatchingTuples
Tuple currLeftTuple = null;
int lcurs; // Cursor for left side buffer
int rcurs; // Cursor for right side buffer
boolean eosl; // Whether end of stream (left table) is reached
boolean eosr; // Whether end of stream (right table) is reached
public SortMergeJoin(Join jn) {
super(jn.getLeft(), jn.getRight(), jn.getConditionList(), jn.getOpType());
schema = jn.getSchema();
jointype = jn.getJoinType();
numBuff = jn.getNumBuff();
}
public boolean open() {
int tuplesize = schema.getTupleSize();
batchsize = Batch.getPageSize() / tuplesize;
eosl = false;
eosr = false;
leftindex = new ArrayList<>();
rightindex = new ArrayList<>();
for (Condition con : conditionList) {
Attribute leftattr = con.getLhs();
Attribute rightattr = (Attribute) con.getRhs();
leftindex.add(left.getSchema().indexOf(leftattr));
rightindex.add(right.getSchema().indexOf(rightattr));
}
// Create the sort attributes (join conditions) for left
List<OrderByClause> left_sort_cond = new ArrayList<>();
for (Condition cond : conditionList) {
left_sort_cond.add(new OrderByClause(cond.getLhs(), OrderByClause.ASC));
}
// Create the sort attributes (join conditions) for right
List<OrderByClause> right_sort_cond = new ArrayList<>();
for (Condition cond : conditionList) {
Attribute a = (Attribute) cond.getRhs();
right_sort_cond.add(new OrderByClause(a, OrderByClause.ASC));
}
// initialise the ExternalSort
left_sorter = new ExternalSort(left, left_sort_cond, numBuff);
left_sorter.setSchema(left.getSchema());
right_sorter = new ExternalSort(right, right_sort_cond, numBuff);
right_sorter.setSchema(right.getSchema());
return left_sorter.open() && right_sorter.open();
}
private boolean readLeft() {
leftInbatch = left_sorter.next();
if (leftInbatch == null) {
eosl = true;
return false;
}
return true;
}
private boolean readRight() {
rightInbatch = right_sorter.next();
if (rightInbatch == null) {
eosr = true;
return false;
}
return true;
}
private boolean incrementLcurs() {
lcurs++;
if (lcurs == leftInbatch.size()) {
lcurs = 0;
if (!readLeft()) {
return false;
}
}
return true;
}
private boolean incrementRcurs() {
rcurs++;
if (rcurs == rightInbatch.size()) {
rcurs = 0;
if (!readRight()) {
return false;
}
}
return true;
}
public Batch next() {
// Check if end of either tables has been reached
if (rightMatchingTuples.isEmpty() && currLeftTuple == null && (eosl || eosr)) {
return null;
}
// initialise output buffer
Batch result = new Batch(batchsize);
// Keep on checking the incoming pages until result is full
while (!result.isFull()) {
if (rightMatchingTuples.size() > 0) {
// The current left tuple is not done joining with tuples from the right
if (currLeftTuple != null) {
Tuple outtuple = currLeftTuple.joinWith(rightMatchingTuples.get(rightIndex));
result.add(outtuple);
rightIndex++;
// This current left tuple is done joining
if (rightIndex == rightMatchingTuples.size()) {
rightIndex = 0;
currLeftTuple = null;
}
}
// There are still matching tuples from right table
// The next left tuple may or may not match with these current tuples from the right
else {
if (eosl) {
currLeftTuple = null;
rightMatchingTuples.clear();
if (result.isEmpty()) {
return null;
}
return result;
}
Tuple lefttuple = leftInbatch.get(lcurs);
Tuple righttuple = rightMatchingTuples.get(0);
int comp = Tuple.compareTuples(lefttuple, righttuple, leftindex, rightindex);
// Can match
if (comp == 0) {
currLeftTuple = lefttuple;
incrementLcurs();
}
// If cannot match, then we reset the variables and continue
else {
currLeftTuple = null;
rightIndex = 0;
rightMatchingTuples.clear();
}
}
}
// No current join is being done. We read in the next tuples
else {
// Read in a left page
if (leftInbatch == null || lcurs == leftInbatch.size()) {
if (!readLeft()) {
return result;
}
lcurs = 0;
}
// Read in a right page
if (rightInbatch == null || rcurs == rightInbatch.size()) {
if (!readRight()) {
return result;
}
rcurs = 0;
}
Tuple lefttuple = leftInbatch.get(lcurs);
Tuple righttuple = rightInbatch.get(rcurs);
int comp = Tuple.compareTuples(lefttuple, righttuple, leftindex, rightindex);
// Tuples can be joined
// Store all matching right tuples into `rightMatchingTuples`
if (comp == 0) {
while (Tuple.compareTuples(lefttuple, righttuple, leftindex, rightindex) == 0) {
rightMatchingTuples.add(righttuple);
if (!incrementRcurs()) {
break;
}
righttuple = rightInbatch.get(rcurs);
}
currLeftTuple = lefttuple;
incrementLcurs();
} else if (comp > 0) {
incrementRcurs();
} else {
incrementLcurs();
}
}
}
return result;
}
public boolean close() {
left_sorter.close();
right_sorter.close();
return true;
}
}