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 | | |
35 | | class FunctionArrayFlatten : public IFunction { |
36 | | public: |
37 | | static constexpr auto name = "array_flatten"; |
38 | 2 | static FunctionPtr create() { return std::make_shared<FunctionArrayFlatten>(); } |
39 | | |
40 | | /// Get function name. |
41 | 1 | String get_name() const override { return name; } |
42 | | |
43 | 0 | size_t get_number_of_arguments() const override { return 1; } |
44 | | |
45 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
46 | 0 | DataTypePtr arg = arguments[0]; |
47 | 0 | while (arg->get_primitive_type() == TYPE_ARRAY) { |
48 | 0 | arg = remove_nullable(assert_cast<const DataTypeArray*>(arg.get())->get_nested_type()); |
49 | 0 | } |
50 | 0 | return std::make_shared<DataTypeArray>(make_nullable(arg)); |
51 | 0 | } |
52 | | |
53 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
54 | 0 | uint32_t result, size_t input_rows_count) const override { |
55 | 0 | auto src_column = |
56 | 0 | block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); |
57 | 0 | auto* src_column_array_ptr = |
58 | 0 | assert_cast<ColumnArray*>(remove_nullable(src_column)->assume_mutable().get()); |
59 | 0 | ColumnArray* nested_src_column_array_ptr = src_column_array_ptr; |
60 | |
|
61 | 0 | DataTypePtr src_data_type = block.get_by_position(arguments[0]).type; |
62 | 0 | auto* src_data_type_array = |
63 | 0 | assert_cast<const DataTypeArray*>(remove_nullable(src_data_type).get()); |
64 | |
|
65 | 0 | auto result_column_offsets = |
66 | 0 | assert_cast<ColumnArray::ColumnOffsets&>(src_column_array_ptr->get_offsets_column()) |
67 | 0 | .clone(); |
68 | 0 | auto* offsets = assert_cast<ColumnArray::ColumnOffsets*>(result_column_offsets.get()) |
69 | 0 | ->get_data() |
70 | 0 | .data(); |
71 | |
|
72 | 0 | while (src_data_type_array->get_nested_type()->get_primitive_type() == TYPE_ARRAY) { |
73 | 0 | nested_src_column_array_ptr = assert_cast<ColumnArray*>( |
74 | 0 | remove_nullable(src_column_array_ptr->get_data_ptr())->assume_mutable().get()); |
75 | |
|
76 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { |
77 | 0 | offsets[i] = nested_src_column_array_ptr->get_offsets()[offsets[i] - 1]; |
78 | 0 | } |
79 | 0 | src_column_array_ptr = nested_src_column_array_ptr; |
80 | 0 | src_data_type_array = assert_cast<const DataTypeArray*>( |
81 | 0 | remove_nullable(src_data_type_array->get_nested_type()).get()); |
82 | 0 | } |
83 | |
|
84 | 0 | block.replace_by_position( |
85 | 0 | result, ColumnArray::create(assert_cast<const ColumnNullable&>( |
86 | 0 | nested_src_column_array_ptr->get_data()) |
87 | 0 | .clone(), |
88 | 0 | std::move(result_column_offsets))); |
89 | 0 | return Status::OK(); |
90 | 0 | } |
91 | | }; |
92 | | |
93 | 1 | void register_function_array_flatten(SimpleFunctionFactory& factory) { |
94 | 1 | factory.register_function<FunctionArrayFlatten>(); |
95 | 1 | } |
96 | | |
97 | | } // namespace doris |