be/src/exprs/function/array/function_array_flatten.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "common/status.h" |
19 | | #include "core/assert_cast.h" |
20 | | #include "core/block/block.h" |
21 | | #include "core/block/column_numbers.h" |
22 | | #include "core/block/column_with_type_and_name.h" |
23 | | #include "core/column/column.h" |
24 | | #include "core/column/column_array.h" |
25 | | #include "core/column/column_nullable.h" |
26 | | #include "core/data_type/data_type.h" |
27 | | #include "core/data_type/data_type_array.h" |
28 | | #include "core/types.h" |
29 | | #include "exprs/aggregate/aggregate_function.h" |
30 | | #include "exprs/function/function.h" |
31 | | #include "exprs/function/simple_function_factory.h" |
32 | | |
33 | | namespace doris { |
34 | | #include "common/compile_check_begin.h" |
35 | | |
36 | | class FunctionArrayFlatten : public IFunction { |
37 | | public: |
38 | | static constexpr auto name = "array_flatten"; |
39 | 2 | static FunctionPtr create() { return std::make_shared<FunctionArrayFlatten>(); } |
40 | | |
41 | | /// Get function name. |
42 | 1 | String get_name() const override { return name; } |
43 | | |
44 | 0 | size_t get_number_of_arguments() const override { return 1; } |
45 | | |
46 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
47 | 0 | DataTypePtr arg = arguments[0]; |
48 | 0 | while (arg->get_primitive_type() == TYPE_ARRAY) { |
49 | 0 | arg = remove_nullable(assert_cast<const DataTypeArray*>(arg.get())->get_nested_type()); |
50 | 0 | } |
51 | 0 | return std::make_shared<DataTypeArray>(make_nullable(arg)); |
52 | 0 | } |
53 | | |
54 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
55 | 0 | uint32_t result, size_t input_rows_count) const override { |
56 | 0 | auto src_column = |
57 | 0 | block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); |
58 | 0 | auto* src_column_array_ptr = |
59 | 0 | assert_cast<ColumnArray*>(remove_nullable(src_column)->assume_mutable().get()); |
60 | 0 | ColumnArray* nested_src_column_array_ptr = src_column_array_ptr; |
61 | |
|
62 | 0 | DataTypePtr src_data_type = block.get_by_position(arguments[0]).type; |
63 | 0 | auto* src_data_type_array = |
64 | 0 | assert_cast<const DataTypeArray*>(remove_nullable(src_data_type).get()); |
65 | |
|
66 | 0 | auto result_column_offsets = |
67 | 0 | assert_cast<ColumnArray::ColumnOffsets&>(src_column_array_ptr->get_offsets_column()) |
68 | 0 | .clone(); |
69 | 0 | auto* offsets = assert_cast<ColumnArray::ColumnOffsets*>(result_column_offsets.get()) |
70 | 0 | ->get_data() |
71 | 0 | .data(); |
72 | |
|
73 | 0 | while (src_data_type_array->get_nested_type()->get_primitive_type() == TYPE_ARRAY) { |
74 | 0 | nested_src_column_array_ptr = assert_cast<ColumnArray*>( |
75 | 0 | remove_nullable(src_column_array_ptr->get_data_ptr())->assume_mutable().get()); |
76 | |
|
77 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { |
78 | 0 | offsets[i] = nested_src_column_array_ptr->get_offsets()[offsets[i] - 1]; |
79 | 0 | } |
80 | 0 | src_column_array_ptr = nested_src_column_array_ptr; |
81 | 0 | src_data_type_array = assert_cast<const DataTypeArray*>( |
82 | 0 | remove_nullable(src_data_type_array->get_nested_type()).get()); |
83 | 0 | } |
84 | |
|
85 | 0 | block.replace_by_position( |
86 | 0 | result, ColumnArray::create(assert_cast<const ColumnNullable&>( |
87 | 0 | nested_src_column_array_ptr->get_data()) |
88 | 0 | .clone(), |
89 | 0 | std::move(result_column_offsets))); |
90 | 0 | return Status::OK(); |
91 | 0 | } |
92 | | }; |
93 | | |
94 | 1 | void register_function_array_flatten(SimpleFunctionFactory& factory) { |
95 | 1 | factory.register_function<FunctionArrayFlatten>(); |
96 | 1 | } |
97 | | |
98 | | } // namespace doris |