-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathHashTableBuilder.cc
More file actions
384 lines (338 loc) · 14 KB
/
Copy pathHashTableBuilder.cc
File metadata and controls
384 lines (338 loc) · 14 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "operators/hashjoin/HashTableBuilder.h"
#include <algorithm>
#include <iostream>
#include "velox/exec/OperatorUtils.h"
#include "utils/CudfVectorUtils.h"
namespace gluten {
namespace {
facebook::velox::RowTypePtr hashJoinTableType(
const std::vector<facebook::velox::core::FieldAccessTypedExprPtr>& joinKeys,
const facebook::velox::RowTypePtr& inputType,
bool includeDependents) {
const auto numKeys = joinKeys.size();
std::vector<std::string> names;
names.reserve(includeDependents ? inputType->size() : numKeys);
std::vector<facebook::velox::TypePtr> types;
types.reserve(includeDependents ? inputType->size() : numKeys);
std::unordered_set<uint32_t> keyChannelSet;
keyChannelSet.reserve(inputType->size());
for (int i = 0; i < numKeys; ++i) {
auto& key = joinKeys[i];
auto channel = facebook::velox::exec::exprToChannel(key.get(), inputType);
keyChannelSet.insert(channel);
names.emplace_back(inputType->nameOf(channel));
types.emplace_back(inputType->childAt(channel));
}
if (!includeDependents) {
return ROW(std::move(names), std::move(types));
}
for (auto i = 0; i < inputType->size(); ++i) {
if (keyChannelSet.find(i) == keyChannelSet.end()) {
names.emplace_back(inputType->nameOf(i));
types.emplace_back(inputType->childAt(i));
}
}
return ROW(std::move(names), std::move(types));
}
bool isLeftNullAwareJoinWithFilter(facebook::velox::core::JoinType joinType, bool nullAware, bool withFilter) {
return (isAntiJoin(joinType) || isLeftSemiProjectJoin(joinType) || isLeftSemiFilterJoin(joinType)) && nullAware &&
withFilter;
}
} // namespace
HashTableBuilder::HashTableBuilder(
facebook::velox::core::JoinType joinType,
bool nullAware,
bool withFilter,
int64_t bloomFilterPushdownSize,
const std::vector<facebook::velox::core::FieldAccessTypedExprPtr>& joinKeys,
const std::vector<column_index_t>& filterInputChannels,
bool filterPropagatesNulls,
const facebook::velox::RowTypePtr& inputType,
facebook::velox::memory::MemoryPool* pool,
uint32_t minTableRowsForParallelJoinBuild,
uint32_t joinBuildVectorHasherMaxNumDistinct,
uint32_t abandonHashBuildDedupMinRows,
uint32_t abandonHashBuildDedupMinPct)
: joinType_{joinType},
nullAware_{nullAware},
withFilter_(withFilter),
keyChannelMap_(joinKeys.size()),
inputType_(inputType),
bloomFilterPushdownSize_(bloomFilterPushdownSize),
pool_(pool),
minTableRowsForParallelJoinBuild_(minTableRowsForParallelJoinBuild),
joinBuildVectorHasherMaxNumDistinct_(joinBuildVectorHasherMaxNumDistinct),
abandonHashBuildDedupMinRows_(abandonHashBuildDedupMinRows),
abandonHashBuildDedupMinPct_(abandonHashBuildDedupMinPct),
filterPropagatesNulls_(filterPropagatesNulls) {
dropDuplicates_ =
!withFilter_ && (isLeftSemiFilterJoin(joinType_) || isLeftSemiProjectJoin(joinType_) || isAntiJoin(joinType_));
const auto numKeys = joinKeys.size();
keyChannels_.reserve(numKeys);
for (int i = 0; i < numKeys; ++i) {
auto& key = joinKeys[i];
auto channel = facebook::velox::exec::exprToChannel(key.get(), inputType_);
keyChannelMap_[channel] = i;
keyChannels_.emplace_back(channel);
}
// Identify the non-key build side columns and make a decoder for each.
if (!dropDuplicates_) {
const int32_t numDependents = inputType_->size() - numKeys;
if (numDependents > 0) {
// Number of join keys (numKeys) may be less then number of input columns
// (inputType->size()). In this case numDependents is negative and cannot
// be used to call 'reserve'. This happens when we join different probe
// side keys with the same build side key: SELECT * FROM t LEFT JOIN u ON
// t.k1 = u.k AND t.k2 = u.k.
dependentChannels_.reserve(numDependents);
decoders_.reserve(numDependents);
}
for (auto i = 0; i < inputType->size(); ++i) {
if (keyChannelMap_.find(i) == keyChannelMap_.end()) {
dependentChannels_.emplace_back(i);
decoders_.emplace_back(std::make_unique<facebook::velox::DecodedVector>());
}
}
}
tableType_ = hashJoinTableType(joinKeys, inputType, !dropDuplicates_);
setupTable();
if (isAntiJoin(joinType_) && withFilter_ && filterPropagatesNulls_) {
setupFilterForAntiJoins(filterInputChannels);
}
}
void HashTableBuilder::setupFilterForAntiJoins(const std::vector<column_index_t>& filterInputChannels) {
VELOX_DCHECK(std::is_sorted(dependentChannels_.begin(), dependentChannels_.end()));
for (auto channel : filterInputChannels) {
auto keyIter = keyChannelMap_.find(channel);
if (keyIter != keyChannelMap_.end()) {
keyFilterChannels_.push_back(keyIter->second);
continue;
}
auto dependentIter = std::lower_bound(dependentChannels_.begin(), dependentChannels_.end(), channel);
if (dependentIter == dependentChannels_.end() || *dependentIter != channel) {
continue;
}
dependentFilterChannels_.push_back(dependentIter - dependentChannels_.begin());
}
}
void HashTableBuilder::removeInputRowsForAntiJoinFilter() {
bool changed = false;
auto* rawActiveRows = activeRows_.asMutableRange().bits();
auto removeNulls = [&](facebook::velox::DecodedVector& decoded) {
if (decoded.mayHaveNulls()) {
changed = true;
facebook::velox::bits::andBits(rawActiveRows, decoded.nulls(&activeRows_), 0, activeRows_.end());
}
};
for (auto channel : keyFilterChannels_) {
removeNulls(uniqueTable_->hashers()[channel]->decodedVector());
}
for (auto channel : dependentFilterChannels_) {
removeNulls(*decoders_[channel]);
}
if (changed) {
activeRows_.updateBounds();
}
}
bool HashTableBuilder::abandonHashBuildDedupEarly(int64_t numDistinct) const {
VELOX_CHECK(dropDuplicates_);
return numHashInputRows_ > abandonHashBuildDedupMinRows_ &&
(100 * numDistinct / numHashInputRows_) >= abandonHashBuildDedupMinPct_;
}
void HashTableBuilder::abandonHashBuildDedup() {
abandonHashBuildDedup_ = true;
if (!dropDuplicates_) {
uniqueTable_->setAllowDuplicates(true);
}
lookup_.reset();
}
// Invoked to set up hash table to build.
void HashTableBuilder::setupTable() {
VELOX_CHECK_NULL(uniqueTable_);
const auto numKeys = keyChannels_.size();
std::vector<std::unique_ptr<facebook::velox::exec::VectorHasher>> keyHashers;
keyHashers.reserve(numKeys);
for (vector_size_t i = 0; i < numKeys; ++i) {
keyHashers.emplace_back(facebook::velox::exec::VectorHasher::create(tableType_->childAt(i), keyChannels_[i]));
}
const auto numDependents = tableType_->size() - numKeys;
std::vector<facebook::velox::TypePtr> dependentTypes;
dependentTypes.reserve(numDependents);
for (int i = numKeys; i < tableType_->size(); ++i) {
dependentTypes.emplace_back(tableType_->childAt(i));
}
if (isRightJoin(joinType_) || isFullJoin(joinType_) || isRightSemiProjectJoin(joinType_)) {
// Do not ignore null keys.
uniqueTable_ = facebook::velox::exec::HashTable<false>::createForJoin(
std::move(keyHashers),
dependentTypes,
true, // allowDuplicates
true, // hasProbedFlag
false, // hasCountFlag
minTableRowsForParallelJoinBuild_,
pool_);
} else {
// Right semi join needs to tag build rows that were probed.
const bool needProbedFlag = isRightSemiFilterJoin(joinType_);
const bool hasCountFlag = facebook::velox::core::isCountingJoin(joinType_);
if (isLeftNullAwareJoinWithFilter(joinType_, nullAware_, withFilter_)) {
// We need to check null key rows in build side in case of null-aware anti
// or left semi project join with filter set.
uniqueTable_ = facebook::velox::exec::HashTable<false>::createForJoin(
std::move(keyHashers),
dependentTypes,
!dropDuplicates_, // allowDuplicates
needProbedFlag, // hasProbedFlag
hasCountFlag, // hasCountFlag
minTableRowsForParallelJoinBuild_,
pool_);
} else {
// Ignore null keys
uniqueTable_ = facebook::velox::exec::HashTable<true>::createForJoin(
std::move(keyHashers),
dependentTypes,
!dropDuplicates_, // allowDuplicates
needProbedFlag, // hasProbedFlag
hasCountFlag, // hasCountFlag
minTableRowsForParallelJoinBuild_,
pool_,
bloomFilterPushdownSize_);
}
}
analyzeKeys_ = uniqueTable_->hashMode() != facebook::velox::exec::BaseHashTable::HashMode::kHash;
if (dropDuplicates_) {
if (!facebook::velox::core::isCountingJoin(joinType_) && abandonHashBuildDedupMinPct_ == 0) {
abandonHashBuildDedup();
return;
}
lookup_ = std::make_unique<facebook::velox::exec::HashLookup>(uniqueTable_->hashers(), pool_);
}
}
void HashTableBuilder::addInput(facebook::velox::RowVectorPtr input) {
// The build side (including broadcast build) may arrive as a GPU-resident
// CudfVector, which has no host-side children. Materialize it to a host
// RowVector before reading key channels below; otherwise childAt() below
// dereferences a null child and crashes.
input = materializeVeloxRowVector(input, pool_);
activeRows_.resize(input->size());
activeRows_.setAll();
auto& hashers = uniqueTable_->hashers();
for (auto i = 0; i < hashers.size(); ++i) {
auto key = input->childAt(hashers[i]->channel())->loadedVector();
hashers[i]->decode(*key, activeRows_);
}
if (!isRightJoin(joinType_) && !isFullJoin(joinType_) && !isRightSemiProjectJoin(joinType_) &&
!isLeftNullAwareJoinWithFilter(joinType_, nullAware_, withFilter_)) {
deselectRowsWithNulls(hashers, activeRows_);
if (nullAware_ && !joinHasNullKeys_ && activeRows_.countSelected() < input->size()) {
joinHasNullKeys_ = true;
}
} else if (nullAware_ && !joinHasNullKeys_) {
for (auto& hasher : hashers) {
auto& decoded = hasher->decodedVector();
if (decoded.mayHaveNulls()) {
auto* nulls = decoded.nulls(&activeRows_);
if (nulls && facebook::velox::bits::countNulls(nulls, 0, activeRows_.end()) > 0) {
joinHasNullKeys_ = true;
break;
}
}
}
}
if (isAntiJoin(joinType_) && nullAware_ && joinHasNullKeys_ && !withFilter_) {
noMoreInput_ = true;
return;
}
if (!activeRows_.hasSelections()) {
return;
}
if (dropDuplicates_ && !abandonHashBuildDedup_) {
// Counting joins must not abandon dedup — accurate counts are required.
VELOX_CHECK_NOT_NULL(lookup_);
const bool abandonEarly =
!facebook::velox::core::isCountingJoin(joinType_) && abandonHashBuildDedupEarly(uniqueTable_->numDistinct());
if (!abandonEarly) {
numHashInputRows_ += activeRows_.countSelected();
uniqueTable_->prepareForGroupProbe(
*lookup_, input, activeRows_, facebook::velox::exec::BaseHashTable::kNoSpillInputStartPartitionBit);
if (lookup_->rows.empty()) {
return;
}
uniqueTable_->groupProbe(*lookup_, facebook::velox::exec::BaseHashTable::kNoSpillInputStartPartitionBit);
// For counting joins, increment the count for duplicate rows.
// New rows are initialized with count = 1 by initializeRow.
// Increment count for all rows, then decrement for new rows to
// correct the over-counting.
if (facebook::velox::core::isCountingJoin(joinType_)) {
auto* rows = uniqueTable_->rows();
for (auto row : lookup_->rows) {
rows->incrementCount(lookup_->hits[row]);
}
for (auto newRow : lookup_->newGroups) {
rows->decrementCount(lookup_->hits[newRow]);
}
}
return;
}
abandonHashBuildDedup();
}
for (auto i = 0; i < dependentChannels_.size(); ++i) {
decoders_[i]->decode(*input->childAt(dependentChannels_[i])->loadedVector(), activeRows_);
}
if (isAntiJoin(joinType_) && withFilter_ && filterPropagatesNulls_) {
removeInputRowsForAntiJoinFilter();
}
if (!activeRows_.hasSelections()) {
return;
}
if (analyzeKeys_ && hashes_.size() < activeRows_.end()) {
hashes_.resize(activeRows_.end());
}
// As long as analyzeKeys is true, we keep running the keys through
// the Vectorhashers so that we get a possible mapping of the keys
// to small ints for array or normalized key. When mayUseValueIds is
// false for the first time we stop. We do not retain the value ids
// since the final ones will only be known after all data is
// received.
for (auto& hasher : hashers) {
// TODO: Load only for active rows, except if right/full outer join.
if (analyzeKeys_) {
hasher->computeValueIds(activeRows_, hashes_);
analyzeKeys_ = hasher->mayUseValueIds();
}
}
auto rows = uniqueTable_->rows();
auto nextOffset = rows->nextOffset();
activeRows_.applyToSelected([&](auto rowIndex) {
char* newRow = rows->newRow();
if (nextOffset) {
*reinterpret_cast<char**>(newRow + nextOffset) = nullptr;
}
// Store the columns for each row in sequence. At probe time
// strings of the row will probably be in consecutive places, so
// reading one will prime the cache for the next.
for (auto i = 0; i < hashers.size(); ++i) {
rows->store(hashers[i]->decodedVector(), rowIndex, newRow, i);
}
for (auto i = 0; i < dependentChannels_.size(); ++i) {
rows->store(*decoders_[i], rowIndex, newRow, i + hashers.size());
}
});
}
} // namespace gluten