-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy patharrayExcept.cpp
More file actions
429 lines (358 loc) · 16.1 KB
/
Copy patharrayExcept.cpp
File metadata and controls
429 lines (358 loc) · 16.1 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <base/types.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnVector.h>
#include <Columns/IColumn.h>
#include <Common/assert_cast.h>
#include <Common/Exception.h>
#include <Common/FunctionDocumentation.h>
#include <Common/HashTable/ClearableHashSet.h>
#include <Common/HashTable/Hash.h>
#include <Common/PODArray_fwd.h>
#include <Common/register_objects.h>
#include <Common/typeid_cast.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/IDataType.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>
#include <Interpreters/Context_fwd.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int LOGICAL_ERROR;
}
namespace
{
template <typename ColT, bool is_const = false>
struct ColumnInfoImpl
{
using ColumnType = std::conditional_t<is_const, const ColT, ColT>;
using OffsetsType = std::conditional_t<is_const, const ColumnArray::Offsets, ColumnArray::Offsets>;
ColumnType & col;
OffsetsType & offsets;
std::conditional_t<is_const, const PaddedPODArray<UInt8>, PaddedPODArray<UInt8>> * null_map;
};
template <typename T>
using ColVecType = ColumnVector<T>;
template <typename T>
using ColumnInfo = ColumnInfoImpl<T, false>;
template <typename T>
using ConstColumnInfo = ColumnInfoImpl<T, true>;
template <typename ColumnType>
struct ValueHandler;
template <typename T>
struct ValueHandler<ColumnVector<T>>
{
using ValueType = T;
static ValueType getValue(const ColumnVector<T> & col, size_t pos) { return col.getData()[pos]; }
static void insertValue(ColumnVector<T> & col, ValueType value) { col.insertValue(value); }
static void insertDefault(ColumnVector<T> & col) { col.insertDefault(); }
};
template <>
struct ValueHandler<ColumnString>
{
using ValueType = std::string_view;
static std::string_view getValue(const ColumnString & col, size_t pos) { return col.getDataAt(pos); }
static void insertValue(ColumnString & col, std::string_view value) { col.insertData(value.data(), value.size()); }
static void insertDefault(ColumnString & col) { col.insertDefault(); }
};
template <>
struct ValueHandler<ColumnFixedString>
{
using ValueType = std::string_view;
static std::string_view getValue(const ColumnFixedString & col, size_t pos)
{
const size_t fixed_size = col.getN();
return std::string_view{reinterpret_cast<const char *>(&col.getChars()[pos * fixed_size]), fixed_size};
}
static void insertValue(ColumnFixedString & col, std::string_view value) { col.insertData(value.data(), value.size()); }
static void insertDefault(ColumnFixedString & col) { col.insertDefault(); }
};
template <bool nullable, bool exclude_single_row, typename ColumnT>
void processImpl(ConstColumnInfo<ColumnT> source, ConstColumnInfo<ColumnT> exclude, ColumnInfo<ColumnT> result, size_t input_rows_count)
{
using ValueType = typename ValueHandler<ColumnT>::ValueType;
using Handler = ValueHandler<ColumnT>;
constexpr size_t initial_size_degree = 9;
using Set = ClearableHashSetWithStackMemory<ValueType, DefaultHash<ValueType>, initial_size_degree>;
Set exclude_set;
// Initialize result columns
result.offsets.resize(input_rows_count);
if constexpr (std::is_same_v<ColumnT, ColumnString>)
{
result.col.reserve(source.col.size());
result.col.getChars().reserve(source.col.getChars().size());
}
else if constexpr (std::is_same_v<ColumnT, ColumnFixedString>)
{
result.col.getChars().reserve(source.col.size() * source.col.getN());
}
else
{
result.col.getData().reserve(source.col.getData().size());
}
if constexpr (nullable)
result.null_map->resize(source.null_map->size());
size_t current_source_offset = 0;
size_t current_exclude_offset = 0;
size_t current_result_offset = 0;
bool exclude_has_null = false;
auto reset = [&]
{
exclude_set.clear();
if constexpr (nullable)
exclude_has_null = false;
};
auto populate_excludes = [&](size_t offset, size_t size)
{
for (size_t i = 0; i != size; ++i)
{
if (exclude.null_map && (*exclude.null_map)[offset + i])
{
if constexpr (nullable)
exclude_has_null = true;
else
continue; // No need to bother - input is not nullable
}
else
{
exclude_set.insert(Handler::getValue(exclude.col, offset + i));
}
}
};
if constexpr (exclude_single_row)
populate_excludes(0, exclude.col.size());
for (size_t row = 0; row < input_rows_count; ++row)
{
const size_t source_size = source.offsets[row] - current_source_offset;
if constexpr (!exclude_single_row)
{
reset();
const size_t exclude_size = exclude.offsets[row] - current_exclude_offset;
populate_excludes(current_exclude_offset, exclude_size);
}
// Process source elements
for (size_t idx = 0; idx < source_size; ++idx)
{
const size_t abs_pos = current_source_offset + idx;
if constexpr (nullable)
{
if ((*source.null_map)[abs_pos])
{
// Deal with source NULL
if (!exclude_has_null)
{
(*result.null_map)[current_result_offset] = 1;
Handler::insertDefault(result.col);
++current_result_offset;
}
}
else
{
// Deal with normal value
const auto current = Handler::getValue(source.col, abs_pos);
if (!exclude_set.has(current))
{
(*result.null_map)[current_result_offset] = 0;
Handler::insertValue(result.col, current);
++current_result_offset;
}
}
}
else
{
const auto current = Handler::getValue(source.col, abs_pos);
if (!exclude_set.has(current))
{
Handler::insertValue(result.col, current);
++current_result_offset;
}
}
}
result.offsets[row] = current_result_offset;
current_source_offset = source.offsets[row];
if constexpr (!exclude_single_row)
current_exclude_offset = exclude.offsets[row];
}
if constexpr (nullable)
result.null_map->resize(current_result_offset);
}
class FunctionArrayExcept final : public IFunction
{
public:
static constexpr auto name = "arrayExcept";
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionArrayExcept>(); }
String getName() const override { return name; }
size_t getNumberOfArguments() const override { return 2; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
auto mandatory_args = FunctionArgumentDescriptors{
{"source", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isArray), nullptr, "Array"},
{"exclude", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isArray), nullptr, "Array"},
};
validateFunctionArguments(*this, arguments, mandatory_args);
auto get_nested_type = [](const DataTypePtr & type)
{
const DataTypeArray * array_type = assert_cast<const DataTypeArray *>(type.get());
if (const DataTypeNullable * nullable = typeid_cast<const DataTypeNullable *>(array_type->getNestedType().get()))
return nullable->getNestedType();
return array_type->getNestedType();
};
DataTypePtr source_nested = get_nested_type(arguments[0].type);
DataTypePtr exclude_nested = get_nested_type(arguments[1].type);
if (!source_nested->equals(*exclude_nested))
{
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Function {} requires both arrays to have exactly the same element type. "
"Got {} and {}",
getName(),
arguments[0].type->getName(),
arguments[1].type->getName());
}
const auto is_supported_type = [](const DataTypePtr & type)
{
const WhichDataType column_type(type->getColumnType());
return column_type.isInteger() || column_type.isFloat() || column_type.isStringOrFixedString();
};
if (!is_supported_type(source_nested))
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Unsupported type {}. Consider arrayFilter(x -> NOT has (exclude), source)", arguments[0].type->getName());
return arguments[0].type;
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & return_type, size_t input_rows_count) const override
{
if (return_type->onlyNull())
return return_type->createColumnConstWithDefaultValue(input_rows_count);
const bool exclude_is_const = isColumnConst(*arguments[1].column);
// We expect some form of arrays for both params
const ColumnArray * exclude_col = exclude_is_const
? typeid_cast<const ColumnArray *>(&typeid_cast<const ColumnConst *>(arguments[1].column.get())->getDataColumn())
: checkAndGetColumn<ColumnArray>(arguments[1].column.get());
if (!exclude_col)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Arguments must be arrays");
if (exclude_col->getData().empty())
{
return exclude_is_const ? arguments[0].column : arguments[0].column->convertToFullColumnIfConst();
}
auto source_full_col = arguments[0].column->convertToFullColumnIfConst();
const ColumnArray * source_col = checkAndGetColumn<ColumnArray>(source_full_col.get());
if (!source_col)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Arguments must be arrays");
// With a little twist that they might be nullable...
const IColumn * source_data = &source_col->getData();
const IColumn * exclude_data = &exclude_col->getData();
const ColumnUInt8::Container * source_null_map = nullptr;
const ColumnUInt8::Container * exclude_null_map = nullptr;
if (const auto * n = checkAndGetColumn<ColumnNullable>(source_data))
{
source_data = &n->getNestedColumn();
source_null_map = &n->getNullMapData();
}
if (const auto * n = checkAndGetColumn<ColumnNullable>(exclude_data))
{
exclude_data = &n->getNestedColumn();
exclude_null_map = &n->getNullMapData();
}
const ColumnArray::Offsets & source_offsets = source_col->getOffsets();
const ColumnArray::Offsets & exclude_offsets = exclude_col->getOffsets();
auto const exclude_single_row = exclude_offsets.size() == 1;
// Prepare the result column
auto actual_return_type = arguments[0].type;
auto result = return_type->createColumn();
ColumnArray * result_col = assert_cast<ColumnArray *>(result.get());
IColumn * result_data = &result_col->getData();
ColumnUInt8::Container * result_null_map = nullptr;
if (ColumnNullable * n = typeid_cast<ColumnNullable *>(result_data))
{
result_data = &n->getNestedColumn();
result_null_map = &n->getNullMapData();
}
ColumnArray::Offsets & result_offsets = result_col->getOffsets();
// NOLINTBEGIN(bugprone-macro-parentheses)
#define DISPATCH(COLTYPE, PROCESSOR, TYPE) \
if (const auto * source_tcol##TYPE = typeid_cast<const COLTYPE *>(source_data)) \
{ \
const auto * exclude_tcol = typeid_cast<const COLTYPE *>(exclude_data); \
auto * result_tcol = typeid_cast<COLTYPE *>(result_data); \
ConstColumnInfo<COLTYPE> s{*source_tcol##TYPE, source_offsets, source_null_map}; \
ConstColumnInfo<COLTYPE> e{*exclude_tcol, exclude_offsets, exclude_null_map}; \
ColumnInfo<COLTYPE> r{*result_tcol, result_offsets, result_null_map}; \
if (source_null_map) \
if (exclude_single_row) \
PROCESSOR<true, true>(s, e, r, input_rows_count); \
else \
PROCESSOR<true, false>(s, e, r, input_rows_count); \
else if (exclude_single_row) \
PROCESSOR<false, true>(s, e, r, input_rows_count); \
else \
PROCESSOR<false, false>(s, e, r, input_rows_count); \
}
#define DISPATCH_VEC(TYPE) DISPATCH(ColumnVector<TYPE>, processImpl, TYPE)
// clang-format off
DISPATCH_VEC(Int8)
else DISPATCH_VEC(Int16)
else DISPATCH_VEC(Int32)
else DISPATCH_VEC(Int64)
else DISPATCH_VEC(Int128)
else DISPATCH_VEC(Int256)
else DISPATCH_VEC(UInt8)
else DISPATCH_VEC(UInt16)
else DISPATCH_VEC(UInt32)
else DISPATCH_VEC(UInt64)
else DISPATCH_VEC(UInt128)
else DISPATCH_VEC(UInt256)
else DISPATCH_VEC(BFloat16)
else DISPATCH_VEC(Float32)
else DISPATCH_VEC(Float64)
else DISPATCH(ColumnString, processImpl, String)
else DISPATCH(ColumnFixedString, processImpl, FixedString)
else
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Unsupported type {}. Consider arrayFilter(x -> NOT has (exclude), source)", arguments[0].type->getName());
// clang-format on
#undef DISPATCH_VEC
#undef DISPATCH
// NOLINTEND(bugprone-macro-parentheses)
return result;
}
};
}
REGISTER_FUNCTION(FunctionArrayExcept)
{
FunctionDocumentation::Description description = R"(
Returns an array containing elements from `source` that are not present in `except`, preserving the original order.
This function performs a set difference operation between two arrays. For each element in `source`, it checks if the element exists in `except` (using exact comparison). If not, the element is included in the result.
The operation maintains these properties:
1. Order of elements from `source` is preserved
2. Duplicates in `source` are preserved if they don't exist in `except`
3. NULL is handled as a separate value
)";
FunctionDocumentation::Syntax syntax = "arrayExcept(source, except)";
FunctionDocumentation::Arguments arguments
= {{"source",
"The source array containing elements to filter. ",
{"Array(T)"}
},
{"except",
"The array containing elements to exclude from the result. ",
{"Array(T)"}
}};
FunctionDocumentation::ReturnedValue returned_value = {"Returns an array of the same type as the input array containing elements from `source` that weren't found in `except`. ", {"Array(T)"}};
FunctionDocumentation::Examples examples
= {{"basic", "SELECT arrayExcept([1, 2, 3, 2, 4], [3, 5])", "[1,2,2,4]"},
{"with_nulls1", "SELECT arrayExcept([1, NULL, 2, NULL], [2])", "[1,NULL,NULL]"},
{"with_nulls2", "SELECT arrayExcept([1, NULL, 2, NULL], [NULL, 2, NULL])", "[1]"},
{"strings", "SELECT arrayExcept(['apple', 'banana', 'cherry'], ['banana', 'date'])", "['apple','cherry']"}};
FunctionDocumentation::IntroducedIn introduced_in = {25, 9};
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation = {description, syntax, arguments, {}, returned_value, examples, introduced_in, category};
factory.registerFunction<FunctionArrayExcept>(documentation);
}
}