-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin.cpp
More file actions
201 lines (154 loc) · 6.03 KB
/
join.cpp
File metadata and controls
201 lines (154 loc) · 6.03 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
#include "dataForm.h"
#include "hashTable.h"
#include "partitioner.h"
#include "simple_vector.h"
#include "job_scheduler.h"
extern JobScheduler js;
result_mt PartitionedHashJoin(relation& r, relation& s, int forceDepth,
int bits_pass1, int bits_pass2) {
// partitioning phase
Partitioner rpartitioner, spartitioner;
relation r_ = rpartitioner.partition(r, forceDepth, bits_pass1, bits_pass2);
// force S relationship to be partitioned as many times as R was
int64_t forcePartitioning = rpartitioner.getPartitioningLevel();
relation s_ =
spartitioner.partition(s, forcePartitioning, bits_pass1, bits_pass2);
// if partitioning has occured
if (forcePartitioning != 0) {
Histogram* rHist = rpartitioner.getHistogram();
const int64_t* rpsum = rHist->getPsum();
int64_t partitions = rHist->getSize();
hashTable** partitionsHT = new hashTable*[partitions];
// build phase
for (int i = 0; i < partitions; i++) {
int64_t entries = (*rHist)[i];
int64_t start = rpsum[i];
int64_t end = (i < (partitions - 1)) ? (rpsum[i + 1]) : (r_.getAmount());
// create hashtable with size = as many as the partitions in the tuple
partitionsHT[i] = new hashTable(entries);
js.add_job(new BuildJob(r_, start, end, partitionsHT[i]));
}
Histogram* sHist = spartitioner.getHistogram();
const int64_t* spsum = sHist->getPsum();
js.wait_all();
result* thread_results = new result[partitions];
for (int j = 0; j < partitions; j++) {
int64_t start = spsum[j];
int64_t end = (j < (partitions - 1)) ? (spsum[j + 1]) : (s_.getAmount());
js.add_job(
new JoinJob(s_, start, end, partitionsHT[j], thread_results[j]));
}
js.wait_all();
for (int i = 0; i < partitions; i++) delete partitionsHT[i];
delete[] partitionsHT;
return result_mt{thread_results, (int)partitions};
}
// no partitioning case
else {
// use the vector inside for the results in the case where no partitioning
result* result_join = new result[THREAD_COUNT];
int64_t r_entries = r.getAmount();
int64_t s_entries = s.getAmount();
hashTable h{r_entries};
for (int64_t i = 0; i < r_entries; i++) h.insert(&r[i]);
int64_t per_thread = s_entries / THREAD_COUNT;
for (int i = 0; i < THREAD_COUNT; i++) {
int64_t start = i * per_thread;
int64_t end = (i + 1) * per_thread;
if ((i == THREAD_COUNT - 1) && (end < s_entries)) end = s_entries;
js.add_job(new JoinJob(s, start, end, &h, result_join[i]));
}
js.wait_all();
return result_mt{result_join, THREAD_COUNT};
}
}
void buildHT(relation& r, int64_t start, int64_t end, hashTable* partitionHT) {
for (int64_t j = start; j < end; j++) partitionHT->insert(&r[j]);
}
void joinBuckets(relation& s_, int64_t start, int64_t end,
hashTable* partitionHT, result& result_join) {
for (int64_t k = start; k < end; k++) {
List* tuple_list = partitionHT->findEntry(s_[k].getKey());
if (tuple_list) {
Node* traverse = tuple_list->getRoot();
while (traverse) {
// result is [rowid_r,rowid_s]
result_item entry{traverse->mytuple->getPayload(), s_[k].getPayload()};
result_join.push(entry);
traverse = traverse->next;
}
}
}
}
// only used for unit testing
result PartitionedHashJoin_ST(relation& r, relation& s, int64_t forceDepth,
int64_t bits_pass1, int64_t bits_pass2) {
// partitioning phase
Partitioner rpartitioner, spartitioner;
relation r_ = rpartitioner.partition(r, forceDepth, bits_pass1, bits_pass2);
// force S relationship to be partitioned as many times as R was
int64_t forcePartitioning = rpartitioner.getPartitioningLevel();
relation s_ =
spartitioner.partition(s, forcePartitioning, bits_pass1, bits_pass2);
// use the vector inside for the results
result result_join;
// if partitioning has occured
if (forcePartitioning != 0) {
Histogram* rHist = rpartitioner.getHistogram();
const int64_t* rpsum = rHist->getPsum();
int64_t partitions = rHist->getSize();
// initialize all to nullptr
hashTable** partitionsHT = new hashTable* [partitions] {};
// build phase
for (int64_t i = 0; i < partitions; i++) {
int64_t entries = (*rHist)[i];
// create hashtable with size = as many as the partitions in the tuple
partitionsHT[i] = new hashTable(entries);
int64_t start = rpsum[i];
int64_t end = (i < (partitions - 1)) ? (rpsum[i + 1]) : (r_.getAmount());
for (int64_t j = start; j < end; j++) partitionsHT[i]->insert(&r_[j]);
}
Histogram* sHist = spartitioner.getHistogram();
const int64_t* spsum = sHist->getPsum();
for (int64_t j = 0; j < partitions; j++) {
int64_t start = spsum[j];
int64_t end = (j < (partitions - 1)) ? (spsum[j + 1]) : (s_.getAmount());
for (int64_t k = start; k < end; k++) {
List* tuple_list = partitionsHT[j]->findEntry(s_[k].getKey());
if (tuple_list) {
Node* traverse = tuple_list->getRoot();
while (traverse) {
// result is [rowid_r,rowid_s]
result_item entry{traverse->mytuple->getPayload(),
s_[k].getPayload()};
result_join.push(entry);
traverse = traverse->next;
}
}
}
}
for (int64_t i = 0; i < partitions; i++) {
delete partitionsHT[i];
}
delete[] partitionsHT;
}
// no partitioning case
else {
int64_t r_entries = r.getAmount();
int64_t s_entries = s.getAmount();
hashTable h{r_entries};
for (int64_t i = 0; i < r_entries; i++) h.insert(&r[i]);
for (int64_t j = 0; j < s_entries; j++) {
List* tuple_list = h.findEntry(s[j].getKey());
if (tuple_list) {
Node* traverse = tuple_list->getRoot();
while (traverse) {
result_item entry{traverse->mytuple->getPayload(), s[j].getPayload()};
result_join.push(entry);
traverse = traverse->next;
}
}
}
}
return result_join;
}