forked from daphne-project/daphne
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInnerJoin.h
More file actions
224 lines (207 loc) · 6.67 KB
/
Copy pathInnerJoin.h
File metadata and controls
224 lines (207 loc) · 6.67 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#ifndef SRC_RUNTIME_LOCAL_KERNELS_INNERJOIN_H
#define SRC_RUNTIME_LOCAL_KERNELS_INNERJOIN_H
#include <runtime/local/context/DaphneContext.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Frame.h>
#include <runtime/local/datastructures/ValueTypeCode.h>
#include <runtime/local/datastructures/ValueTypeUtils.h>
#include <stdexcept>
#include <tuple>
#include <unordered_set>
#include <cstddef>
#include <cstdint>
// ****************************************************************************
// Helper functions
// ****************************************************************************
template<typename VTCol>
void innerJoinSetValue(
DenseMatrix<VTCol> * res,
const DenseMatrix<VTCol> * arg,
const int64_t targetRow,
const int64_t fromRow,
DCTX(ctx)
){
const VTCol argValue = arg->get(fromRow, 0);
res->set(targetRow, 0, argValue);
}
template<typename VTCol>
void innerJoinSet(
ValueTypeCode vtcType,
Frame *&res,
const Frame * arg,
const int64_t toRow,
const int64_t toCol,
const int64_t fromRow,
const int64_t fromCol,
DCTX(ctx)
) {
if(vtcType == ValueTypeUtils::codeFor<VTCol>){
innerJoinSetValue<VTCol>(
res->getColumn<VTCol>(toCol),
arg->getColumn<VTCol>(fromCol),
toRow,
fromRow,
ctx
);
}
}
template<typename VTLhs, typename VTRhs>
bool innerJoinEqual(
// results
Frame *& res,
// arguments
const DenseMatrix<VTLhs> * argLhs,
const DenseMatrix<VTRhs> * argRhs,
const int64_t targetLhs,
const int64_t targetRhs,
// context
DCTX(ctx)
){
const VTLhs l = argLhs->get(targetLhs, 0);
const VTRhs r = argRhs->get(targetRhs, 0);
return l == r;
}
template<typename VTLhs, typename VTRhs>
bool innerJoinProbeIf(
// value type known only at run-time
ValueTypeCode vtcLhs,
ValueTypeCode vtcRhs,
// results
Frame *& res,
// input frames
const Frame * lhs, const Frame * rhs,
// input column names
const char * lhsOn, const char * rhsOn,
// input rows
const int64_t targetL, const int64_t targetR,
// context
DCTX(ctx)
){
if(vtcLhs == ValueTypeUtils::codeFor<VTLhs> && vtcRhs == ValueTypeUtils::codeFor<VTRhs>) {
return innerJoinEqual<VTLhs, VTRhs>(
res,
lhs->getColumn<VTLhs>(lhsOn),
rhs->getColumn<VTRhs>(rhsOn),
targetL,
targetR,
ctx
);
}
return false;
}
// ****************************************************************************
// Convenience function
// ****************************************************************************
void innerJoin(
// results
Frame *& res,
// input frames
const Frame * lhs, const Frame * rhs,
// input column names
const char * lhsOn, const char * rhsOn,
// context
DCTX(ctx)
) {
// Find out the value types of the columns to process.
ValueTypeCode vtcLhsOn = lhs->getColumnType(lhsOn);
ValueTypeCode vtcRhsOn = rhs->getColumnType(rhsOn);
// Perhaps check if res already allocated.
const size_t numRowRhs = rhs->getNumRows();
const size_t numRowLhs = lhs->getNumRows();
const size_t totalRows = numRowRhs * numRowLhs;
const size_t numColRhs = rhs->getNumCols();
const size_t numColLhs = lhs->getNumCols();
const size_t totalCols = numColRhs + numColLhs;
const std::string * oldlabels_l = lhs->getLabels();
const std::string * oldlabels_r = rhs->getLabels();
int64_t col_idx_res = 0;
int64_t row_idx_res = 0;
ValueTypeCode schema[totalCols];
std::string newlabels[totalCols];
// Setting Schema and Labels
for(size_t col_idx_l = 0; col_idx_l < numColLhs; col_idx_l++){
schema[col_idx_res] = lhs->getColumnType(col_idx_l);
newlabels[col_idx_res] = oldlabels_l[col_idx_l];
col_idx_res++;
}
for(size_t col_idx_r = 0; col_idx_r < numColRhs; col_idx_r++){
schema[col_idx_res] = rhs->getColumnType(col_idx_r);
newlabels[col_idx_res] = oldlabels_r[col_idx_r];
col_idx_res++;
}
// Creating Result Frame
res = DataObjectFactory::create<Frame>(totalRows, totalCols, schema, newlabels, false);
for(size_t row_idx_l = 0; row_idx_l < numRowLhs; row_idx_l++){
for(size_t row_idx_r = 0; row_idx_r < numRowRhs; row_idx_r++){
col_idx_res = 0;
//PROBE ROWS
bool hit = false;
hit = hit || innerJoinProbeIf<int64_t, int64_t>(
vtcLhsOn, vtcRhsOn,
res,
lhs, rhs,
lhsOn, rhsOn,
row_idx_l, row_idx_r,
ctx);
hit = hit || innerJoinProbeIf<double, double>(
vtcLhsOn, vtcRhsOn,
res,
lhs, rhs,
lhsOn, rhsOn,
row_idx_l, row_idx_r,
ctx);
if(hit){
for(size_t idx_c = 0; idx_c < numColLhs; idx_c++){
innerJoinSet<int64_t>(
schema[col_idx_res],
res,
lhs,
row_idx_res,
col_idx_res,
row_idx_l,
idx_c,
ctx
);
innerJoinSet<double>(
schema[col_idx_res],
res,
lhs,
row_idx_res,
col_idx_res,
row_idx_l,
idx_c,
ctx
);
col_idx_res++;
}
for(size_t idx_c = 0; idx_c < numColRhs; idx_c++){
innerJoinSet<int64_t>(
schema[col_idx_res],
res,
rhs,
row_idx_res,
col_idx_res,
row_idx_r,
idx_c,
ctx
);
innerJoinSet<double>(
schema[col_idx_res],
res,
rhs,
row_idx_res,
col_idx_res,
row_idx_r,
idx_c,
ctx
);
col_idx_res++;
}
row_idx_res++;
}
}
}
res->shrinkNumRows(row_idx_res);
}
#endif //SRC_RUNTIME_LOCAL_KERNELS_INNERJOIN_H