Repository navigation
Expand file tree
/
Copy pathfunction_array_flatten.cpp
More file actions
116 lines (101 loc) · 5.38 KB
/
Copy pathfunction_array_flatten.cpp
File metadata and controls
116 lines (101 loc) · 5.38 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
// 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 "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_array.h"
#include "core/column/column_nullable.h"
#include "core/data_type/data_type.h"
#include "core/data_type/data_type_array.h"
#include "core/types.h"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/function.h"
#include "exprs/function/simple_function_factory.h"
namespace doris {
#include "common/compile_check_begin.h"
class FunctionArrayFlatten : public IFunction {
public:
static constexpr auto name = "array_flatten";
static FunctionPtr create() { return std::make_shared<FunctionArrayFlatten>(); }
/// Get function name.
String get_name() const override { return name; }
size_t get_number_of_arguments() const override { return 1; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
DataTypePtr arg = arguments[0];
while (arg->get_primitive_type() == TYPE_ARRAY) {
arg = remove_nullable(assert_cast<const DataTypeArray*>(arg.get())->get_nested_type());
}
return std::make_shared<DataTypeArray>(make_nullable(arg));
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
auto src_column =
block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
auto* src_column_array_ptr =
assert_cast<const ColumnArray*>(remove_nullable(src_column).get());
ColumnPtr nested_src_column_array = remove_nullable(src_column);
DataTypePtr src_data_type = block.get_by_position(arguments[0]).type;
auto* src_data_type_array =
assert_cast<const DataTypeArray*>(remove_nullable(src_data_type).get());
auto result_column_offsets = src_column_array_ptr->get_offsets_column().clone();
auto* offsets = assert_cast<ColumnArray::ColumnOffsets*>(result_column_offsets.get())
->get_data()
.data();
while (src_data_type_array->get_nested_type()->get_primitive_type() == TYPE_ARRAY) {
const auto* nullable =
check_and_get_column<ColumnNullable>(src_column_array_ptr->get_data());
auto nested_array = remove_nullable(src_column_array_ptr->get_data_ptr());
const auto& array = assert_cast<const ColumnArray&>(*nested_array);
// IF can leave [101, 102] beneath a NULL subarray. Remove that range before
// composing offsets, so [NULL, [9]] flattens to [9] at every nesting level.
if (nullable && nullable->has_null()) {
auto data = array.get_data().clone_empty();
auto nested_offsets = ColumnArray::ColumnOffsets::create();
auto& nested_offsets_data = nested_offsets->get_data();
nested_offsets_data.reserve(array.size());
for (size_t i = 0; i < array.size(); ++i) {
// NULL subarrays may own nonempty ranges; their children are invisible.
if (!nullable->is_null_at(i)) {
data->insert_range_from(array.get_data(), array.offset_at(i),
array.size_at(i));
}
nested_offsets_data.push_back(data->size());
}
nested_array = ColumnArray::create(std::move(data), std::move(nested_offsets));
}
const auto& nested_array_offsets =
assert_cast<const ColumnArray&>(*nested_array).get_offsets();
for (size_t i = 0; i < input_rows_count; ++i) {
offsets[i] = nested_array_offsets[offsets[i] - 1];
}
nested_src_column_array = std::move(nested_array);
src_column_array_ptr = assert_cast<const ColumnArray*>(nested_src_column_array.get());
src_data_type_array = assert_cast<const DataTypeArray*>(
remove_nullable(src_data_type_array->get_nested_type()).get());
}
block.replace_by_position(result, ColumnArray::create(src_column_array_ptr->get_data_ptr(),
std::move(result_column_offsets)));
return Status::OK();
}
};
void register_function_array_flatten(SimpleFunctionFactory& factory) {
factory.register_function<FunctionArrayFlatten>();
}
} // namespace doris