forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_struct.cpp
More file actions
151 lines (129 loc) · 5.67 KB
/
Copy pathfunction_struct.cpp
File metadata and controls
151 lines (129 loc) · 5.67 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
// 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 <glog/logging.h>
#include <stddef.h>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include "common/status.h"
#include "vec/aggregate_functions/aggregate_function.h"
#include "vec/columns/column.h"
#include "vec/columns/column_nullable.h"
#include "vec/columns/column_struct.h"
#include "vec/columns/column_vector.h"
#include "vec/common/typeid_cast.h"
#include "vec/core/block.h"
#include "vec/core/column_numbers.h"
#include "vec/core/column_with_type_and_name.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type.h"
#include "vec/data_types/data_type_nullable.h"
#include "vec/data_types/data_type_struct.h"
#include "vec/functions/function.h"
#include "vec/functions/simple_function_factory.h"
namespace doris {
class FunctionContext;
} // namespace doris
namespace doris::vectorized {
// construct a struct
template <typename Impl>
class FunctionStruct : public IFunction {
public:
static constexpr auto name = Impl::name;
static FunctionPtr create() { return std::make_shared<FunctionStruct>(); }
/// Get function name.
String get_name() const override { return name; }
bool is_variadic() const override { return true; }
bool use_default_implementation_for_nulls() const override { return false; }
size_t get_number_of_arguments() const override { return 0; }
void check_number_of_arguments(size_t number_of_arguments) const override {
DCHECK(number_of_arguments > 0)
<< "function: " << get_name() << ", arguments should not be empty.";
return Impl::check_number_of_arguments(number_of_arguments);
}
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
return Impl::get_return_type_impl(arguments);
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
auto result_col = block.get_by_position(result).type->create_column();
auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
ColumnNumbers args_num;
for (size_t i = 0; i < arguments.size(); i++) {
if (Impl::pred(i)) {
args_num.push_back(arguments[i]);
}
}
size_t num_element = args_num.size();
if (num_element != struct_column->tuple_size()) {
return Status::RuntimeError(
"function {} args number {} is not equal to result struct field number {}.",
get_name(), num_element, struct_column->tuple_size());
}
std::vector<ColumnPtr> arg(num_element);
for (size_t i = 0; i < num_element; ++i) {
auto& nested_col = struct_column->get_column(i);
nested_col.reserve(input_rows_count);
bool is_nullable = nested_col.is_nullable();
auto& col = block.get_by_position(args_num[i]).column;
col = col->convert_to_full_column_if_const();
arg[i] = col;
if (is_nullable && !col->is_nullable()) {
arg[i] = ColumnNullable::create(col, ColumnUInt8::create(col->size(), 0));
}
}
// insert value into struct column by column
for (size_t i = 0; i < num_element; ++i) {
struct_column->get_column(i).insert_range_from(*arg[i], 0, input_rows_count);
}
block.replace_by_position(result, std::move(result_col));
return Status::OK();
}
};
// struct(value1, value2, value3) -> {value1, value2, value3}
struct StructImpl {
static constexpr auto name = "struct";
static constexpr auto pred = [](size_t i) { return true; };
static void check_number_of_arguments(size_t number_of_arguments) {}
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
return std::make_shared<DataTypeStruct>(make_nullable(arguments));
}
};
// named_struct(name1, value1, name2, value2) -> {name1:value1, name2:value2}
struct NamedStructImpl {
static constexpr auto name = "named_struct";
static constexpr auto pred = [](size_t i) { return (i & 1) == 1; };
static void check_number_of_arguments(size_t number_of_arguments) {
DCHECK(number_of_arguments % 2 == 0)
<< "function: " << name << ", arguments size should be even number.";
}
static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
DataTypes data_types(arguments.size() / 2);
size_t even_idx = 1;
for (size_t i = 0; i < data_types.size(); i++) {
data_types[i] = arguments[even_idx];
even_idx += 2;
}
return std::make_shared<DataTypeStruct>(make_nullable(data_types));
}
};
void register_function_struct(SimpleFunctionFactory& factory) {
factory.register_function<FunctionStruct<StructImpl>>();
factory.register_function<FunctionStruct<NamedStructImpl>>();
}
} // namespace doris::vectorized