-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathfunction_array_pop.cpp
More file actions
179 lines (154 loc) · 7.49 KB
/
Copy pathfunction_array_pop.cpp
File metadata and controls
179 lines (154 loc) · 7.49 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
// 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 <fmt/format.h>
#include <glog/logging.h>
#include <cstddef>
#include <memory>
#include <ostream>
#include <utility>
#include "common/compiler_util.h"
#include "common/status.h"
#include "core/assert_cast.h"
#include "core/block/block.h"
#include "core/block/column_numbers.h"
#include "core/block/column_with_type_and_name.h"
#include "core/column/column.h"
#include "core/column/column_vector.h"
#include "core/data_type/data_type.h"
#include "core/types.h"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/array/function_array_utils.h"
#include "exprs/function/function.h"
#include "exprs/function/simple_function_factory.h"
namespace doris {
class FunctionContext;
} // namespace doris
namespace doris {
template <typename PopType>
class FunctionArrayPop : public IFunction {
public:
static FunctionPtr create() { return std::make_shared<PopType>(); }
/// Get function name.
String get_name() const override { return PopType::name; }
bool is_variadic() const override { return false; }
size_t get_number_of_arguments() const override { return 1; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
<< "First argument for function: " << PopType::name
<< " should be DataTypeArray but it has type " << arguments[0]->get_name() << ".";
return arguments[0];
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
auto array_column =
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
// extract src array column
ColumnArrayExecutionData src;
if (!extract_column_array_info(*array_column, src)) {
return Status::RuntimeError(
fmt::format("execute failed, unsupported types for function {}({})", get_name(),
block.get_by_position(arguments[0]).type->get_name()));
}
// prepare dst array column
bool is_nullable = src.nested_nullmap_data != nullptr;
ColumnArrayMutableData dst = create_mutable_data(src.nested_col.get(), is_nullable);
dst.offsets_ptr->reserve(input_rows_count);
// start from index depending on the PopType::start_offset
auto offset_column = ColumnInt64::create(array_column->size(), PopType::start_offset);
// len - 1
auto length_column = ColumnInt64::create();
for (size_t row = 0; row < src.offsets_ptr->size(); ++row) {
size_t off = (*src.offsets_ptr)[row - 1];
size_t len = (*src.offsets_ptr)[row] - off;
length_column->insert_value(len - 1);
}
slice_array(dst, src, *offset_column, length_column.get());
ColumnPtr res_column = assemble_column_array(dst);
block.replace_by_position(result, std::move(res_column));
return Status::OK();
}
};
class FunctionArrayPopback : public FunctionArrayPop<FunctionArrayPopback> {
public:
static constexpr auto name = "array_popback";
static constexpr int start_offset = 1;
};
class FunctionArrayPopfront : public FunctionArrayPop<FunctionArrayPopfront> {
public:
static constexpr auto name = "array_popfront";
static constexpr int start_offset = 2;
};
class FunctionArrayTrim : public IFunction {
public:
static constexpr auto name = "trim_array";
static FunctionPtr create() { return std::make_shared<FunctionArrayTrim>(); }
String get_name() const override { return name; }
bool is_variadic() const override { return false; }
size_t get_number_of_arguments() const override { return 2; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
<< "First argument for function: " << name
<< " should be DataTypeArray but it has type " << arguments[0]->get_name() << ".";
DCHECK(arguments[1]->get_primitive_type() == TYPE_BIGINT)
<< "Second argument for function: " << name << " should be BigInt but it has type "
<< arguments[1]->get_name() << ".";
return arguments[0];
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
auto array_column =
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
auto size_column =
block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
ColumnArrayExecutionData src;
if (!extract_column_array_info(*array_column, src)) {
return Status::RuntimeError(
fmt::format("execute failed, unsupported types for function {}({}, {})",
get_name(), block.get_by_position(arguments[0]).type->get_name(),
block.get_by_position(arguments[1]).type->get_name()));
}
auto length_column = ColumnInt64::create();
length_column->reserve(input_rows_count);
const auto& sizes = assert_cast<const ColumnInt64&>(*size_column).get_data();
for (size_t row = 0; row < input_rows_count; ++row) {
const auto size = sizes[row];
const size_t offset = (*src.offsets_ptr)[row - 1];
const size_t cardinality = (*src.offsets_ptr)[row] - offset;
if (UNLIKELY(size < 0)) {
return Status::InvalidArgument("size must not be negative: {}", size);
}
if (UNLIKELY(static_cast<size_t>(size) > cardinality)) {
return Status::InvalidArgument("size must not exceed array cardinality {}: {}",
cardinality, size);
}
length_column->insert_value(static_cast<Int64>(cardinality) - size);
}
const bool nested_is_nullable = src.nested_nullmap_data != nullptr;
ColumnArrayMutableData dst = create_mutable_data(src.nested_col.get(), nested_is_nullable);
dst.offsets_ptr->reserve(input_rows_count);
auto offset_column = ColumnInt64::create(input_rows_count, 1);
slice_array(dst, src, *offset_column, length_column.get());
block.replace_by_position(result, assemble_column_array(dst));
return Status::OK();
}
};
void register_function_array_pop(SimpleFunctionFactory& factory) {
factory.register_function<FunctionArrayPopback>();
factory.register_function<FunctionArrayPopfront>();
factory.register_function<FunctionArrayTrim>();
}
} // namespace doris