-
Notifications
You must be signed in to change notification settings - Fork 919
/
Copy pathsemi_join.cu
303 lines (266 loc) · 12.9 KB
/
semi_join.cu
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
/*
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* Licensed 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 <join/join_common_utils.cuh>
#include <join/join_common_utils.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/gather.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/detail/null_mask.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/structs/utilities.hpp>
#include <cudf/dictionary/detail/update_keys.hpp>
#include <cudf/join.hpp>
#include <cudf/table/table.hpp>
#include <cudf/utilities/error.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/exec_policy.hpp>
#include <thrust/copy.h>
#include <thrust/distance.h>
#include <thrust/sequence.h>
#include <thrust/tuple.h>
namespace cudf {
namespace detail {
namespace {
/**
* @brief Device functor to create a pair of hash value and index for a given row.
*/
struct make_pair_function {
__device__ __forceinline__ cudf::detail::pair_type operator()(size_type i) const noexcept
{
// The value is irrelevant since we only ever use the hash map to check for
// membership of a particular row index.
return cuco::make_pair(static_cast<hash_value_type>(i), 0);
}
};
} // namespace
std::unique_ptr<rmm::device_uvector<cudf::size_type>> left_semi_anti_join(
join_kind const kind,
cudf::table_view const& left_keys,
cudf::table_view const& right_keys,
null_equality compare_nulls,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
{
CUDF_EXPECTS(0 != left_keys.num_columns(), "Left table is empty");
CUDF_EXPECTS(0 != right_keys.num_columns(), "Right table is empty");
if (is_trivial_join(left_keys, right_keys, kind)) {
return std::make_unique<rmm::device_uvector<cudf::size_type>>(0, stream, mr);
}
if ((join_kind::LEFT_ANTI_JOIN == kind) && (0 == right_keys.num_rows())) {
auto result =
std::make_unique<rmm::device_uvector<cudf::size_type>>(left_keys.num_rows(), stream, mr);
thrust::sequence(rmm::exec_policy(stream), result->begin(), result->end());
return result;
}
auto const left_num_rows = left_keys.num_rows();
auto const right_num_rows = right_keys.num_rows();
// flatten structs for the right and left and use that for the hash table
auto right_flattened_tables = structs::detail::flatten_nested_columns(
right_keys, {}, {}, structs::detail::column_nullability::FORCE);
auto left_flattened_tables = structs::detail::flatten_nested_columns(
left_keys, {}, {}, structs::detail::column_nullability::FORCE);
auto right_flattened_keys = right_flattened_tables.flattened_columns();
auto left_flattened_keys = left_flattened_tables.flattened_columns();
// Create hash table.
semi_map_type hash_table{compute_hash_table_size(right_num_rows),
std::numeric_limits<hash_value_type>::max(),
cudf::detail::JoinNoneValue,
hash_table_allocator_type{default_allocator<char>{}, stream},
stream.value()};
// Create hash table containing all keys found in right table
auto right_rows_d = table_device_view::create(right_flattened_keys, stream);
auto const right_nulls = cudf::nullate::DYNAMIC{cudf::has_nulls(right_flattened_keys)};
row_hash const hash_build{right_nulls, *right_rows_d};
row_equality equality_build{right_nulls, *right_rows_d, *right_rows_d, compare_nulls};
make_pair_function pair_func_build{};
auto iter = cudf::detail::make_counting_transform_iterator(0, pair_func_build);
// skip rows that are null here.
if ((compare_nulls == null_equality::EQUAL) or (not nullable(right_keys))) {
hash_table.insert(iter, iter + right_num_rows, hash_build, equality_build, stream.value());
} else {
thrust::counting_iterator<size_type> stencil(0);
auto const [row_bitmask, _] = cudf::detail::bitmask_and(right_flattened_keys, stream);
row_is_valid pred{static_cast<bitmask_type const*>(row_bitmask.data())};
// insert valid rows
hash_table.insert_if(
iter, iter + right_num_rows, stencil, pred, hash_build, equality_build, stream.value());
}
// Now we have a hash table, we need to iterate over the rows of the left table
// and check to see if they are contained in the hash table
auto left_rows_d = table_device_view::create(left_flattened_keys, stream);
auto const left_nulls = cudf::nullate::DYNAMIC{cudf::has_nulls(left_flattened_keys)};
row_hash hash_probe{left_nulls, *left_rows_d};
// Note: This equality comparator violates symmetry of equality and is
// therefore relying on the implementation detail of the order in which its
// operator is invoked. If cuco makes no promises about the order of
// invocation this seems a bit unsafe.
row_equality equality_probe{left_nulls, *right_rows_d, *left_rows_d, compare_nulls};
// For semi join we want contains to be true, for anti join we want contains to be false
bool const join_type_boolean = (kind == join_kind::LEFT_SEMI_JOIN);
auto hash_table_view = hash_table.get_device_view();
auto gather_map =
std::make_unique<rmm::device_uvector<cudf::size_type>>(left_num_rows, stream, mr);
// gather_map_end will be the end of valid data in gather_map
auto gather_map_end = thrust::copy_if(
rmm::exec_policy(stream),
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(left_num_rows),
gather_map->begin(),
[hash_table_view, join_type_boolean, hash_probe, equality_probe] __device__(
size_type const idx) {
// Look up this row. The hash function used here needs to map a (left) row index to the hash
// of the row, so it's a row hash. The equality check needs to verify
return hash_table_view.contains(idx, hash_probe, equality_probe) == join_type_boolean;
});
auto join_size = thrust::distance(gather_map->begin(), gather_map_end);
gather_map->resize(join_size, stream);
return gather_map;
}
/**
* @brief Performs a left semi or anti join on the specified columns of two
* tables (left, right)
*
* The semi and anti joins only return data from the left table. A left semi join
* returns rows that exist in the right table, a left anti join returns rows
* that do not exist in the right table.
*
* The basic approach is to create a hash table containing the contents of the right
* table and then select only rows that exist (or don't exist) to be included in
* the return set.
*
* @throws cudf::logic_error if number of columns in either `left` or `right` table is 0
* @throws cudf::logic_error if number of returned columns is 0
* @throws cudf::logic_error if number of elements in `right_on` and `left_on` are not equal
*
* @param kind Indicates whether to do LEFT_SEMI_JOIN or LEFT_ANTI_JOIN
* @param left The left table
* @param right The right table
* @param left_on The column indices from `left` to join on.
* The column from `left` indicated by `left_on[i]`
* will be compared against the column from `right`
* indicated by `right_on[i]`.
* @param right_on The column indices from `right` to join on.
* The column from `right` indicated by `right_on[i]`
* will be compared against the column from `left`
* indicated by `left_on[i]`.
* @param compare_nulls Controls whether null join-key values should match or not.
* @param stream CUDA stream used for device memory operations and kernel launches.
* @param mr Device memory resource to used to allocate the returned table
*
* @returns Result of joining `left` and `right` tables on the columns
* specified by `left_on` and `right_on`.
*/
std::unique_ptr<cudf::table> left_semi_anti_join(
join_kind const kind,
cudf::table_view const& left,
cudf::table_view const& right,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
null_equality compare_nulls,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
{
CUDF_EXPECTS(left_on.size() == right_on.size(), "Mismatch in number of columns to be joined on");
if ((left_on.empty() || right_on.empty()) || is_trivial_join(left, right, kind)) {
return empty_like(left);
}
if ((join_kind::LEFT_ANTI_JOIN == kind) && (0 == right.num_rows())) {
// Everything matches, just copy the proper columns from the left table
return std::make_unique<table>(left, stream, mr);
}
// Make sure any dictionary columns have matched key sets.
// This will return any new dictionary columns created as well as updated table_views.
auto matched = cudf::dictionary::detail::match_dictionaries(
{left.select(left_on), right.select(right_on)},
stream,
rmm::mr::get_current_device_resource()); // temporary objects returned
auto const left_selected = matched.second.front();
auto const right_selected = matched.second.back();
auto gather_vector =
left_semi_anti_join(kind, left_selected, right_selected, compare_nulls, stream);
// wrapping the device vector with a column view allows calling the non-iterator
// version of detail::gather, improving compile time by 10% and reducing the
// object file size by 2.2x without affecting performance
auto gather_map = column_view(data_type{type_id::INT32},
static_cast<size_type>(gather_vector->size()),
gather_vector->data(),
nullptr,
0);
auto const left_updated = scatter_columns(left_selected, left_on, left);
return cudf::detail::gather(left_updated,
gather_map,
out_of_bounds_policy::DONT_CHECK,
negative_index_policy::NOT_ALLOWED,
stream,
mr);
}
} // namespace detail
std::unique_ptr<cudf::table> left_semi_join(cudf::table_view const& left,
cudf::table_view const& right,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
null_equality compare_nulls,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::left_semi_anti_join(detail::join_kind::LEFT_SEMI_JOIN,
left,
right,
left_on,
right_on,
compare_nulls,
rmm::cuda_stream_default,
mr);
}
std::unique_ptr<rmm::device_uvector<cudf::size_type>> left_semi_join(
cudf::table_view const& left,
cudf::table_view const& right,
null_equality compare_nulls,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::left_semi_anti_join(
detail::join_kind::LEFT_SEMI_JOIN, left, right, compare_nulls, rmm::cuda_stream_default, mr);
}
std::unique_ptr<cudf::table> left_anti_join(cudf::table_view const& left,
cudf::table_view const& right,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
null_equality compare_nulls,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::left_semi_anti_join(detail::join_kind::LEFT_ANTI_JOIN,
left,
right,
left_on,
right_on,
compare_nulls,
rmm::cuda_stream_default,
mr);
}
std::unique_ptr<rmm::device_uvector<cudf::size_type>> left_anti_join(
cudf::table_view const& left,
cudf::table_view const& right,
null_equality compare_nulls,
rmm::mr::device_memory_resource* mr)
{
CUDF_FUNC_RANGE();
return detail::left_semi_anti_join(
detail::join_kind::LEFT_ANTI_JOIN, left, right, compare_nulls, rmm::cuda_stream_default, mr);
}
} // namespace cudf