-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy patharrayExists.cpp
More file actions
85 lines (71 loc) · 3.22 KB
/
Copy patharrayExists.cpp
File metadata and controls
85 lines (71 loc) · 3.22 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
#include <Functions/array/arrayExists.h>
#include <Functions/FunctionFactory.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
}
ColumnPtr ArrayExistsImpl::execute(const ColumnArray & array, ColumnPtr mapped)
{
const ColumnUInt8 * column_filter = typeid_cast<const ColumnUInt8 *>(&*mapped);
if (!column_filter)
{
const auto * column_filter_const = checkAndGetColumnConst<ColumnUInt8>(&*mapped);
if (!column_filter_const)
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Unexpected type of filter column: {}; The result of the lambda is expected to be a UInt8", mapped->getDataType());
if (column_filter_const->getValue<UInt8>())
{
const IColumn::Offsets & offsets = array.getOffsets();
auto out_column = ColumnUInt8::create(offsets.size());
ColumnUInt8::Container & out_exists = out_column->getData();
size_t pos = 0;
for (size_t i = 0; i < offsets.size(); ++i)
{
out_exists[i] = offsets[i] - pos > 0;
pos = offsets[i];
}
return out_column;
}
return DataTypeUInt8().createColumnConst(array.size(), 0u);
}
const IColumn::Filter & filter = column_filter->getData();
const IColumn::Offsets & offsets = array.getOffsets();
auto out_column = ColumnUInt8::create(offsets.size());
ColumnUInt8::Container & out_exists = out_column->getData();
size_t pos = 0;
for (size_t i = 0; i < offsets.size(); ++i)
{
UInt8 exists = 0;
for (; pos < offsets[i]; ++pos)
{
if (filter[pos])
{
exists = 1;
pos = offsets[i];
break;
}
}
out_exists[i] = exists;
}
return out_column;
}
REGISTER_FUNCTION(ArrayExists)
{
FunctionDocumentation::Description description = R"(
Returns `1` if there is at least one element in a source array for which `func(x[, y1, y2, ... yN])` returns true. Otherwise, it returns `0`.
)";
FunctionDocumentation::Syntax syntax = "arrayExists(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments = {
{"func(x[, y1, ..., yN])", "A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`).", {"Lambda function"}},
{"source_arr", "The source array to process.", {"Array(T)"}},
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function.", {"Array(T)"}}
};
FunctionDocumentation::ReturnedValue returned_value = {"Returns `1` if the lambda function returns true for at least one element, `0` otherwise", {"UInt8"}};
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayExists(x, y -> x=y, [1, 2, 3], [0, 0, 0])", "0"}};
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation = {description, syntax, arguments, {}, returned_value, examples, introduced_in, category};
factory.registerFunction<FunctionArrayExists>(documentation);
}
}