-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathAggregateFunctionSparkArrayFold.cpp
More file actions
297 lines (261 loc) · 12.8 KB
/
AggregateFunctionSparkArrayFold.cpp
File metadata and controls
297 lines (261 loc) · 12.8 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
/*
* 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 <Columns/ColumnArray.h>
#include <Columns/ColumnFunction.h>
#include <Columns/ColumnNullable.h>
#include <Core/Field.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeFunction.h>
#include <DataTypes/DataTypeNullable.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>
#include <Interpreters/castColumn.h>
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
}
}
namespace local_engine
{
using namespace DB;
class SparkFunctionArrayFold : public IFunction
{
public:
static constexpr auto name = "sparkArrayFold";
static FunctionPtr create(ContextPtr /*context*/) { return std::make_shared<SparkFunctionArrayFold>(); }
bool isVariadic() const override { return true; }
size_t getNumberOfArguments() const override { return 0; }
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo &) const override { return true; }
void getLambdaArgumentTypes(DataTypes & arguments) const override
{
if (arguments.size() != 3 && arguments.size() != 4)
throw Exception(
ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION,
"Function {} requires a lambda function, an array, and an initial value, with an optional finish lambda",
getName());
const auto * merge_lambda = checkAndGetDataType<DataTypeFunction>(arguments[0].get());
if (!merge_lambda || merge_lambda->getArgumentTypes().size() != 2)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"First argument of function {} must be a lambda function with 2 arguments, found {} instead.",
getName(),
arguments[0]->getName());
const auto * array_type = checkAndGetDataType<DataTypeArray>(removeNullable(arguments[1]).get());
if (!array_type)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Second argument of function {} must be an array, found {} instead.",
getName(),
arguments[1]->getName());
DataTypes merge_lambda_args = {arguments[2], array_type->getNestedType()};
arguments[0] = std::make_shared<DataTypeFunction>(merge_lambda_args, merge_lambda->getReturnType());
if (arguments.size() == 4)
{
const auto * finish_lambda = checkAndGetDataType<DataTypeFunction>(arguments[3].get());
if (!finish_lambda || finish_lambda->getArgumentTypes().size() != 1)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Fourth argument of function {} must be a lambda function with 1 argument, found {} instead.",
getName(),
arguments[3]->getName());
DataTypes finish_lambda_args = {arguments[2]};
arguments[3] = std::make_shared<DataTypeFunction>(finish_lambda_args, finish_lambda->getReturnType());
}
}
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
if (arguments.size() != 3 && arguments.size() != 4)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Function {} requires 3 or 4 arguments", getName());
const auto * array_type = checkAndGetDataType<DataTypeArray>(removeNullable(arguments[1].type).get());
if (!array_type)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Second argument of function {} must be an array, found {} instead.",
getName(),
arguments[1].type->getName());
const auto * merge_lambda = checkAndGetDataType<DataTypeFunction>(arguments[0].type.get());
if (!merge_lambda)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"First argument of function {} must be a lambda function, found {} instead.",
getName(),
arguments[0].type->getName());
DataTypePtr result_type;
if (arguments.size() == 3)
{
result_type = arguments[2].type;
}
else
{
const auto * finish_lambda = checkAndGetDataType<DataTypeFunction>(arguments[3].type.get());
if (!finish_lambda)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Fourth argument of function {} must be a lambda function, found {} instead.",
getName(),
arguments[3].type->getName());
result_type = finish_lambda->getReturnType();
}
if (arguments[1].type->isNullable() && !result_type->isNullable())
result_type = makeNullable(result_type);
return result_type;
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{
const auto * merge_lambda = checkAndGetColumn<ColumnFunction>(arguments[0].column.get());
if (!merge_lambda)
throw Exception(
ErrorCodes::ILLEGAL_COLUMN,
"First argument of function {} must be a lambda function column, found {} instead.",
getName(),
arguments[0].column->getName());
ColumnPtr array_column = arguments[1].column->convertToFullColumnIfConst();
DataTypePtr array_type = arguments[1].type;
const NullMap * array_null_map = nullptr;
if (const auto * nullable_array_column = checkAndGetColumn<ColumnNullable>(array_column.get()))
{
array_column = nullable_array_column->getNestedColumnPtr();
array_type = removeNullable(array_type);
array_null_map = &nullable_array_column->getNullMapData();
}
const auto * array_col = checkAndGetColumn<ColumnArray>(array_column.get());
if (!array_col)
throw Exception(
ErrorCodes::ILLEGAL_COLUMN,
"Second argument of function {} must be an array column, found {} instead.",
getName(),
array_column->getName());
ColumnPtr init_column = arguments[2].column->convertToFullColumnIfConst();
DataTypePtr init_type = arguments[2].type;
const auto * merge_lambda_type = checkAndGetDataType<DataTypeFunction>(arguments[0].type.get());
if (!merge_lambda_type || merge_lambda_type->getArgumentTypes().size() != 2)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"First argument of function {} must be a lambda function with 2 arguments, found {} instead.",
getName(),
arguments[0].type->getName());
const auto & merge_lambda_args = merge_lambda_type->getArgumentTypes();
DataTypePtr acc_type = merge_lambda_args[0];
DataTypePtr element_type = merge_lambda_args[1];
if (!init_type->equals(*acc_type))
{
auto init_arg = ColumnWithTypeAndName(init_column, init_type, "acc");
init_column = castColumn(init_arg, acc_type);
init_type = acc_type;
}
const ColumnFunction * finish_lambda = nullptr;
if (arguments.size() == 4)
{
finish_lambda = checkAndGetColumn<ColumnFunction>(arguments[3].column.get());
if (!finish_lambda)
throw Exception(
ErrorCodes::ILLEGAL_COLUMN,
"Fourth argument of function {} must be a lambda function column, found {} instead.",
getName(),
arguments[3].column->getName());
}
const auto & offsets = array_col->getOffsets();
const auto & nested_data = array_col->getData();
auto nested_type = assert_cast<const DataTypeArray &>(*removeNullable(array_type)).getNestedType();
const bool needs_element_cast = !nested_type->equals(*element_type);
auto to_const_column = [](const MutableColumnPtr & column) -> ColumnPtr {
const IColumn & column_ref = *column;
return column_ref.getPtr();
};
auto make_single_value_column = [&](const DataTypePtr & type, const Field & value) {
auto col = type->createColumn();
col->insert(value);
return to_const_column(col);
};
auto result_column = result_type->createColumn();
result_column->reserve(input_rows_count);
Field acc_field;
size_t previous_offset = 0;
for (size_t row = 0; row < input_rows_count; ++row)
{
if (array_null_map && (*array_null_map)[row])
{
result_column->insertDefault();
continue;
}
init_column->get(row, acc_field);
size_t end_offset = offsets[row];
for (size_t i = previous_offset; i < end_offset; ++i)
{
auto acc_col = make_single_value_column(init_type, acc_field);
auto element_col_mut = nested_data.cloneEmpty();
element_col_mut->insertFrom(nested_data, i);
auto element_col = to_const_column(element_col_mut);
if (needs_element_cast)
{
auto element_arg = ColumnWithTypeAndName(element_col, nested_type, "element");
element_col = castColumn(element_arg, element_type);
}
auto lambda_clone = merge_lambda->cloneResized(1);
auto * lambda_col = typeid_cast<ColumnFunction *>(lambda_clone.get());
lambda_col->appendArguments(
{ColumnWithTypeAndName(std::move(acc_col), init_type, "acc"),
ColumnWithTypeAndName(std::move(element_col), element_type, "element")});
auto merged_col = lambda_col->reduce();
merged_col.column->get(0, acc_field);
}
if (finish_lambda)
{
const auto * finish_lambda_type = checkAndGetDataType<DataTypeFunction>(arguments[3].type.get());
if (!finish_lambda_type || finish_lambda_type->getArgumentTypes().size() != 1)
throw Exception(
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Fourth argument of function {} must be a lambda function with 1 argument, found {} instead.",
getName(),
arguments[3].type->getName());
auto finish_arg_type = finish_lambda_type->getArgumentTypes().front();
auto acc_col = make_single_value_column(init_type, acc_field);
if (!init_type->equals(*finish_arg_type))
acc_col = castColumn(ColumnWithTypeAndName(acc_col, init_type, "acc"), finish_arg_type);
auto lambda_clone = finish_lambda->cloneResized(1);
auto * lambda_col = typeid_cast<ColumnFunction *>(lambda_clone.get());
lambda_col->appendArguments({ColumnWithTypeAndName(std::move(acc_col), finish_arg_type, "acc")});
auto finished_col = lambda_col->reduce();
Field finished_field;
finished_col.column->get(0, finished_field);
result_column->insert(finished_field);
}
else
{
result_column->insert(acc_field);
}
previous_offset = end_offset;
}
return result_column;
}
String getName() const override { return name; }
};
REGISTER_FUNCTION(SparkArrayFold)
{
factory.registerFunction<SparkFunctionArrayFold>();
}
}