-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathfunction_fake.cpp
More file actions
287 lines (257 loc) · 13.6 KB
/
function_fake.cpp
File metadata and controls
287 lines (257 loc) · 13.6 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
// 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 "exprs/function/function_fake.h"
#include <glog/logging.h>
#include <algorithm>
#include <boost/iterator/iterator_facade.hpp>
#include <memory>
#include <ostream>
#include <string>
#include "core/data_type/data_type_array.h"
#include "core/data_type/data_type_jsonb.h"
#include "core/data_type/data_type_map.h"
#include "core/data_type/data_type_nullable.h"
#include "core/data_type/data_type_number.h"
#include "core/data_type/data_type_string.h"
#include "core/data_type/data_type_struct.h"
#include "core/data_type/data_type_variant.h"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/function_helpers.h"
#include "exprs/function/simple_function_factory.h"
#include "exprs/table_function/table_function.h"
namespace doris {
template <typename ReturnType, bool AlwaysNullable = false, bool VARIADIC = false>
struct FunctionFakeBaseImpl {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
if constexpr (AlwaysNullable) {
return make_nullable(std::make_shared<ReturnType>());
}
return std::make_shared<ReturnType>();
}
static DataTypes get_variadic_argument_types() {
if constexpr (VARIADIC) {
return {std::make_shared<ReturnType>()};
} else {
return {};
}
}
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
struct FunctionExplode {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DORIS_CHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY);
return make_nullable(
check_and_get_data_type<DataTypeArray>(arguments[0].get())->get_nested_type());
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
struct FunctionExplodeV2 {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DataTypes fieldTypes(arguments.size());
for (int i = 0; i < arguments.size(); i++) {
if (arguments[i]->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
if (arguments[i]->is_nullable()) {
fieldTypes[i] = arguments[i];
} else {
fieldTypes[i] = make_nullable(arguments[i]);
}
} else {
auto nestedType = check_and_get_data_type<DataTypeArray>(arguments[i].get())
->get_nested_type();
if (nestedType->is_nullable()) {
fieldTypes[i] = nestedType;
} else {
fieldTypes[i] = make_nullable(nestedType);
}
}
}
if (fieldTypes.size() > 1) {
return make_nullable(std::make_shared<DataTypeStruct>(fieldTypes));
} else {
return make_nullable(fieldTypes[0]);
}
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
// explode map: make map k,v as struct field
struct FunctionExplodeMap {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DORIS_CHECK(arguments[0]->get_primitive_type() == TYPE_MAP);
DataTypes fieldTypes(2);
fieldTypes[0] = check_and_get_data_type<DataTypeMap>(arguments[0].get())->get_key_type();
fieldTypes[1] = check_and_get_data_type<DataTypeMap>(arguments[0].get())->get_value_type();
return make_nullable(std::make_shared<DataTypeStruct>(fieldTypes));
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
template <bool AlwaysNullable = false>
struct FunctionPoseExplode {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DataTypes fieldTypes(arguments.size() + 1);
fieldTypes[0] = std::make_shared<DataTypeInt32>();
for (int i = 0; i < arguments.size(); i++) {
DORIS_CHECK(arguments[i]->get_primitive_type() == TYPE_ARRAY);
auto nestedType =
check_and_get_data_type<DataTypeArray>(arguments[i].get())->get_nested_type();
fieldTypes[i + 1] = make_nullable(nestedType);
}
auto struct_type = std::make_shared<DataTypeStruct>(fieldTypes);
if constexpr (AlwaysNullable) {
return make_nullable(struct_type);
} else {
return struct_type;
}
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
// explode json-object: expands json-object to struct with a pair of key and value in column string
struct FunctionExplodeJsonObject {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DORIS_CHECK(arguments[0]->get_primitive_type() == PrimitiveType::TYPE_JSONB);
DataTypes fieldTypes(2);
fieldTypes[0] = make_nullable(std::make_shared<DataTypeString>());
fieldTypes[1] = make_nullable(std::make_shared<DataTypeJsonb>());
return make_nullable(std::make_shared<DataTypeStruct>(fieldTypes));
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
// json_each(json) -> Nullable(Struct(key Nullable(String), value Nullable(JSONB)))
struct FunctionJsonEach {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DORIS_CHECK(arguments[0]->get_primitive_type() == PrimitiveType::TYPE_JSONB);
DataTypes fieldTypes(2);
fieldTypes[0] = make_nullable(std::make_shared<DataTypeString>());
fieldTypes[1] = make_nullable(std::make_shared<DataTypeJsonb>());
return make_nullable(std::make_shared<DataTypeStruct>(fieldTypes));
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
// json_each_text(json) -> Nullable(Struct(key Nullable(String), value Nullable(String)))
struct FunctionJsonEachText {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DORIS_CHECK(arguments[0]->get_primitive_type() == PrimitiveType::TYPE_JSONB);
DataTypes fieldTypes(2);
fieldTypes[0] = make_nullable(std::make_shared<DataTypeString>());
fieldTypes[1] = make_nullable(std::make_shared<DataTypeString>());
return make_nullable(std::make_shared<DataTypeStruct>(fieldTypes));
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "Fake function do not support execute"; }
};
struct FunctionEsquery {
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return FunctionFakeBaseImpl<DataTypeUInt8>::get_return_type_impl(arguments);
}
static DataTypes get_variadic_argument_types() { return {}; }
static std::string get_error_msg() { return "esquery only supported on es table"; }
};
template <typename FunctionImpl>
void register_function(SimpleFunctionFactory& factory, const std::string& name) {
factory.register_function<FunctionFake<FunctionImpl>>(name);
};
template <typename FunctionImpl>
void register_table_function_expand(SimpleFunctionFactory& factory, const std::string& name,
const std::string& suffix) {
factory.register_function<FunctionFake<FunctionImpl>>(name);
factory.register_function<FunctionFake<FunctionImpl>>(name + suffix);
};
template <typename FunctionImpl>
void register_table_alternative_function_expand(SimpleFunctionFactory& factory,
const std::string& name,
const std::string& suffix) {
factory.register_alternative_function<FunctionFake<FunctionImpl>>(name);
factory.register_alternative_function<FunctionFake<FunctionImpl>>(name + suffix);
};
template <typename ReturnType, bool VARIADIC>
void register_table_function_expand_default(SimpleFunctionFactory& factory, const std::string& name,
const std::string& suffix) {
factory.register_function<FunctionFake<FunctionFakeBaseImpl<ReturnType, false, VARIADIC>>>(
name);
factory.register_function<FunctionFake<FunctionFakeBaseImpl<ReturnType, true, VARIADIC>>>(
name + suffix);
};
template <typename ReturnType, bool VARIADIC>
void register_table_alternative_function_expand_default(SimpleFunctionFactory& factory,
const std::string& name,
const std::string& suffix) {
factory.register_alternative_function<
FunctionFake<FunctionFakeBaseImpl<ReturnType, false, VARIADIC>>>(name);
factory.register_alternative_function<
FunctionFake<FunctionFakeBaseImpl<ReturnType, true, VARIADIC>>>(name + suffix);
};
template <typename FunctionImpl>
void register_table_function_expand_outer(SimpleFunctionFactory& factory, const std::string& name) {
register_table_function_expand<FunctionImpl>(factory, name, COMBINATOR_SUFFIX_OUTER);
};
template <typename FunctionImpl>
void register_table_alternative_function_expand_outer(SimpleFunctionFactory& factory,
const std::string& name) {
register_table_alternative_function_expand<FunctionImpl>(factory, name,
COMBINATOR_SUFFIX_OUTER);
};
template <typename ReturnType, bool VARIADIC>
void register_table_function_expand_outer_default(SimpleFunctionFactory& factory,
const std::string& name) {
register_table_function_expand_default<ReturnType, VARIADIC>(factory, name,
COMBINATOR_SUFFIX_OUTER);
};
template <typename ReturnType, bool VARIADIC>
void register_table_alternative_function_expand_outer_default(SimpleFunctionFactory& factory,
const std::string& name) {
register_table_alternative_function_expand_default<ReturnType, VARIADIC>(
factory, name, COMBINATOR_SUFFIX_OUTER);
};
template <typename FunctionImpl>
void register_table_function_with_impl(SimpleFunctionFactory& factory, const std::string& name,
const std::string& suffix = "") {
factory.register_function<FunctionFake<FunctionImpl>>(name + suffix);
};
void register_function_fake(SimpleFunctionFactory& factory) {
register_function<FunctionEsquery>(factory, "esquery");
register_table_function_expand_outer<FunctionExplodeV2>(factory, "explode");
register_table_alternative_function_expand_outer<FunctionExplode>(factory, "explode");
register_table_function_expand_outer<FunctionExplodeMap>(factory, "explode_map");
register_table_function_expand_outer<FunctionExplodeJsonObject>(factory, "explode_json_object");
register_table_function_expand_outer<FunctionJsonEach>(factory, "json_each");
register_table_function_expand_outer<FunctionJsonEachText>(factory, "json_each_text");
register_table_function_expand_outer_default<DataTypeString, false>(factory, "explode_split");
register_table_function_expand_outer_default<DataTypeInt32, false>(factory, "explode_numbers");
register_table_function_expand_outer_default<DataTypeInt64, false>(factory,
"explode_json_array_int");
register_table_function_expand_outer_default<DataTypeString, false>(
factory, "explode_json_array_string");
register_table_function_expand_outer_default<DataTypeJsonb, true>(factory,
"explode_json_array_json");
register_table_function_expand_outer_default<DataTypeString, true>(factory,
"explode_json_array_json");
register_table_function_expand_outer_default<DataTypeFloat64, false>(
factory, "explode_json_array_double");
register_table_function_expand_outer_default<DataTypeInt64, false>(factory, "explode_bitmap");
register_table_function_with_impl<FunctionPoseExplode<false>>(factory, "posexplode");
register_table_function_with_impl<FunctionPoseExplode<true>>(factory, "posexplode",
COMBINATOR_SUFFIX_OUTER);
register_table_alternative_function_expand_outer_default<DataTypeVariant, false>(
factory, "explode_variant_array");
register_table_function_with_impl<FunctionExplodeV2>(factory, "explode_variant_array");
}
} // namespace doris