-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy patharrayFirstLast.cpp
More file actions
298 lines (249 loc) · 14.4 KB
/
Copy patharrayFirstLast.cpp
File metadata and controls
298 lines (249 loc) · 14.4 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
#include <Columns/ColumnsNumber.h>
#include <Columns/ColumnNullable.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/array/FunctionArrayMapped.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
}
enum class ArrayFirstLastStrategy : uint8_t
{
First,
Last
};
enum class ArrayFirstLastElementNotExistsStrategy : uint8_t
{
Default,
Null
};
template <ArrayFirstLastStrategy strategy, ArrayFirstLastElementNotExistsStrategy element_not_exists_strategy>
struct ArrayFirstLastImpl
{
static bool needBoolean() { return true; }
static bool needExpression() { return true; }
static bool needOneArray() { return false; }
static DataTypePtr getReturnType(const DataTypePtr & /*expression_return*/, const DataTypePtr & array_element)
{
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
return makeNullable(array_element);
return array_element;
}
static ColumnPtr createNullableColumn(MutableColumnPtr && column, ColumnUInt8::MutablePtr && null_map)
{
if (auto * nullable_column = typeid_cast<ColumnNullable *>(column.get()))
{
nullable_column->applyNullMap(*null_map);
return std::move(column);
}
return ColumnNullable::create(std::move(column), std::move(null_map));
}
static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
{
const auto * 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 auto & offsets = array.getOffsets();
size_t offsets_size = offsets.size();
const auto & data = array.getData();
auto out = data.cloneEmpty();
out->reserve(offsets_size);
ColumnUInt8::MutablePtr col_null_map_to;
ColumnUInt8::Container * vec_null_map_to = nullptr;
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
{
col_null_map_to = ColumnUInt8::create(offsets_size, false);
vec_null_map_to = &col_null_map_to->getData();
}
for (size_t offset_index = 0; offset_index < offsets_size; ++offset_index)
{
size_t start_offset = offsets[offset_index - 1];
size_t end_offset = offsets[offset_index];
if (end_offset > start_offset)
{
if constexpr (strategy == ArrayFirstLastStrategy::First)
out->insertFrom(data, start_offset);
else
out->insertFrom(data, end_offset - 1);
}
else
{
out->insertDefault();
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
(*vec_null_map_to)[offset_index] = true;
}
}
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
return createNullableColumn(std::move(out), std::move(col_null_map_to));
return out;
}
auto out = array.getData().cloneEmpty();
out->insertManyDefaults(array.size());
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
{
auto col_null_map_to = ColumnUInt8::create(out->size(), true);
return createNullableColumn(std::move(out), std::move(col_null_map_to));
}
return out;
}
const auto & filter = column_filter->getData();
const auto & offsets = array.getOffsets();
size_t offsets_size = offsets.size();
const auto & data = array.getData();
auto out = data.cloneEmpty();
out->reserve(offsets_size);
ColumnUInt8::MutablePtr col_null_map_to;
ColumnUInt8::Container * vec_null_map_to = nullptr;
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
{
col_null_map_to = ColumnUInt8::create(offsets_size, false);
vec_null_map_to = &col_null_map_to->getData();
}
for (size_t offset_index = 0; offset_index < offsets_size; ++offset_index)
{
size_t start_offset = offsets[offset_index - 1];
size_t end_offset = offsets[offset_index];
bool exists = false;
if constexpr (strategy == ArrayFirstLastStrategy::First)
{
for (; start_offset != end_offset; ++start_offset)
{
if (filter[start_offset])
{
out->insertFrom(data, start_offset);
exists = true;
break;
}
}
}
else
{
for (; end_offset != start_offset; --end_offset)
{
if (filter[end_offset - 1])
{
out->insertFrom(data, end_offset - 1);
exists = true;
break;
}
}
}
if (!exists)
{
out->insertDefault();
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
(*vec_null_map_to)[offset_index] = true;
}
}
if constexpr (element_not_exists_strategy == ArrayFirstLastElementNotExistsStrategy::Null)
return createNullableColumn(std::move(out), std::move(col_null_map_to));
return out;
}
};
struct NameArrayFirst { static constexpr auto name = "arrayFirst"; };
using ArrayFirstImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::First, ArrayFirstLastElementNotExistsStrategy::Default>;
using FunctionArrayFirst = FunctionArrayMapped<ArrayFirstImpl, NameArrayFirst>;
struct NameArrayFirstOrNull { static constexpr auto name = "arrayFirstOrNull"; };
using ArrayFirstOrNullImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::First, ArrayFirstLastElementNotExistsStrategy::Null>;
using FunctionArrayFirstOrNull = FunctionArrayMapped<ArrayFirstOrNullImpl, NameArrayFirstOrNull>;
struct NameArrayLast { static constexpr auto name = "arrayLast"; };
using ArrayLastImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::Last, ArrayFirstLastElementNotExistsStrategy::Default>;
using FunctionArrayLast = FunctionArrayMapped<ArrayLastImpl, NameArrayLast>;
struct NameArrayLastOrNull { static constexpr auto name = "arrayLastOrNull"; };
using ArrayLastOrNullImpl = ArrayFirstLastImpl<ArrayFirstLastStrategy::Last, ArrayFirstLastElementNotExistsStrategy::Null>;
using FunctionArrayLastOrNull = FunctionArrayMapped<ArrayLastOrNullImpl, NameArrayLastOrNull>;
REGISTER_FUNCTION(ArrayFirst)
{
/// arrayFirst
FunctionDocumentation::Description description = R"(
Returns the first element in the source array for which `func(x[, y1, y2, ... yN])` returns true, otherwise it returns a default value.
)";
FunctionDocumentation::Syntax syntax = "arrayFirst(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](/reference/functions/regular-functions/overview#arrow-operator-and-lambda)."},
{"source_arr", "The source array to process. [`Array(T)`](/reference/data-types/array)."},
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/reference/data-types/array)."}
};
FunctionDocumentation::ReturnedValue returned_value = {"Returns the first element of the source array for which `λ` is true, otherwise returns the default value of `T`."};
FunctionDocumentation::Examples examples = {
{"Usage example", "SELECT arrayFirst(x, y -> x=y, ['a', 'b', 'c'], ['c', 'b', 'a'])", "b"},
{"No match", "SELECT arrayFirst(x, y -> x=y, [0, 1, 2], [3, 3, 3]) AS res, toTypeName(res)", "0\tUInt8"}
};
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<FunctionArrayFirst>(documentation);
/// arrayFirstOrNull
FunctionDocumentation::Description description_orNull = R"(
Returns the first element in the source array for which `func(x[, y1, y2, ... yN])` returns true, otherwise it returns `NULL`.
)";
FunctionDocumentation::Syntax syntax_orNull = "arrayFirstOrNull(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_orNull = {
{"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_orNull = {"Returns the first element of the source array for which `func` is true, otherwise returns `NULL`."};
FunctionDocumentation::Examples examples_orNull = {
{"Usage example", "SELECT arrayFirstOrNull(x, y -> x=y, ['a', 'b', 'c'], ['c', 'b', 'a'])", "b"},
{"No match", "SELECT arrayFirstOrNull(x, y -> x=y, [0, 1, 2], [3, 3, 3]) AS res, toTypeName(res)", "\\N\tNullable(UInt8)"}
};
FunctionDocumentation::IntroducedIn introduced_in_orNull = {1, 1};
FunctionDocumentation::Category category_orNull = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_orNull = {description_orNull, syntax_orNull, arguments_orNull, {}, returned_value_orNull, examples_orNull, introduced_in_orNull, category_orNull};
factory.registerFunction<FunctionArrayFirstOrNull>(documentation_orNull);
/// arrayLast
FunctionDocumentation::Description description_last = R"(
Returns the last element in the source array for which a lambda `func(x [, y1, y2, ... yN])` returns true, otherwise it returns a default value.
)";
FunctionDocumentation::Syntax syntax_last = "arrayLast(func(x[, y1, ..., yN]), source[, cond1, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_last = {
{"func(x[, y1, ..., yN])", "A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/reference/functions/regular-functions/overview#arrow-operator-and-lambda)."},
{"source", "The source array to process. [`Array(T)`](/reference/data-types/array)."},
{"[, cond1, ... , condN]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/reference/data-types/array)."}
};
FunctionDocumentation::ReturnedValue returned_value_last = {"Returns the last element of the source array for which `func` is true, otherwise returns the default value of `T`."};
FunctionDocumentation::Examples examples_last = {
{"Usage example", "SELECT arrayLast(x, y -> x=y, ['a', 'b', 'c'], ['a', 'b', 'c'])", "c"},
{
"No match",
"SELECT arrayFirst(x, y -> x=y, [0, 1, 2], [3, 3, 3]) AS res, toTypeName(res)",
"0\tUInt8"
}
};
FunctionDocumentation::IntroducedIn introduced_in_last = {1, 1};
FunctionDocumentation::Category category_last = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_last = {description_last, syntax_last, arguments_last, {}, returned_value_last, examples_last, introduced_in_last, category_last};
factory.registerFunction<FunctionArrayLast>(documentation_last);
/// arrayLastOrNull
FunctionDocumentation::Description description_last_null = R"(
Returns the last element in the source array for which a lambda `func(x [, y1, y2, ... yN])` returns true, otherwise it returns `NULL`.
)";
FunctionDocumentation::Syntax syntax_last_null = "arrayLastOrNull(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_last_null = {
{"func(x [, y1, ..., yN])", "A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/reference/functions/regular-functions/overview#arrow-operator-and-lambda)."},
{"source_arr", "The source array to process. [`Array(T)`](/reference/data-types/array)."},
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/reference/data-types/array)."}
};
FunctionDocumentation::ReturnedValue returned_value_last_null = {"Returns the last element of the source array for which `λ` is not true, otherwise returns `NULL`."};
FunctionDocumentation::Examples examples_last_null = {
{"Usage example", "SELECT arrayLastOrNull(x, y -> x=y, ['a', 'b', 'c'], ['a', 'b', 'c'])", "c"},
{
"No match",
"SELECT arrayLastOrNull(x, y -> x=y, [0, 1, 2], [3, 3, 3]) AS res, toTypeName(res)",
"\\N\tNullable(UInt8)"
}
};
FunctionDocumentation::IntroducedIn introduced_in_last_null = {1, 1};
FunctionDocumentation::Category category_last_null = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_last_null = {description_last_null, syntax_last_null, arguments_last_null, {}, returned_value_last_null, examples_last_null, introduced_in_last_null, category_last_null};
factory.registerFunction<FunctionArrayLastOrNull>(documentation_last_null);
}
}