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 " vec/aggregate_functions/aggregate_function.h"
20+ #include " vec/columns/column.h"
21+ #include " vec/columns/column_array.h"
22+ #include " vec/columns/column_nullable.h"
23+ #include " vec/common/assert_cast.h"
24+ #include " vec/core/block.h"
25+ #include " vec/core/column_numbers.h"
26+ #include " vec/core/column_with_type_and_name.h"
27+ #include " vec/core/types.h"
28+ #include " vec/data_types/data_type.h"
29+ #include " vec/data_types/data_type_array.h"
30+ #include " vec/functions/function.h"
31+ #include " vec/functions/simple_function_factory.h"
32+
33+ namespace doris ::vectorized {
34+ #include " common/compile_check_begin.h"
35+
36+ class FunctionArrayFlatten : public IFunction {
37+ public:
38+ static constexpr auto name = " array_flatten" ;
39+ static FunctionPtr create () { return std::make_shared<FunctionArrayFlatten>(); }
40+
41+ // / Get function name.
42+ String get_name () const override { return name; }
43+
44+ size_t get_number_of_arguments () const override { return 1 ; }
45+
46+ DataTypePtr get_return_type_impl (const DataTypes& arguments) const override {
47+ DataTypePtr arg = arguments[0 ];
48+ while (is_array (arg)) {
49+ arg = remove_nullable (assert_cast<const DataTypeArray*>(arg.get ())->get_nested_type ());
50+ }
51+ return std::make_shared<DataTypeArray>(make_nullable (arg));
52+ }
53+
54+ Status execute_impl (FunctionContext* context, Block& block, const ColumnNumbers& arguments,
55+ size_t result, size_t input_rows_count) const override {
56+ auto src_column =
57+ block.get_by_position (arguments[0 ]).column ->convert_to_full_column_if_const ();
58+ auto * src_column_array_ptr =
59+ assert_cast<ColumnArray*>(remove_nullable (src_column)->assume_mutable ().get ());
60+ ColumnArray* nested_src_column_array_ptr = src_column_array_ptr;
61+
62+ auto result_column_offsets =
63+ assert_cast<ColumnArray::ColumnOffsets&>(src_column_array_ptr->get_offsets_column ())
64+ .clone ();
65+ auto * offsets = assert_cast<ColumnArray::ColumnOffsets*>(result_column_offsets.get ())
66+ ->get_data ()
67+ .data ();
68+
69+ while (src_column_array_ptr->get_data_ptr ()->is_column_array ()) {
70+ nested_src_column_array_ptr = assert_cast<ColumnArray*>(
71+ remove_nullable (src_column_array_ptr->get_data_ptr ())->assume_mutable ().get ());
72+
73+ for (size_t i = 0 ; i < input_rows_count; ++i) {
74+ offsets[i] = nested_src_column_array_ptr->get_offsets ()[offsets[i] - 1 ];
75+ }
76+ src_column_array_ptr = nested_src_column_array_ptr;
77+ }
78+
79+ block.replace_by_position (
80+ result, ColumnArray::create (assert_cast<const ColumnNullable&>(
81+ nested_src_column_array_ptr->get_data ())
82+ .clone (),
83+ std::move (result_column_offsets)));
84+ return Status::OK ();
85+ }
86+ };
87+
88+ void register_function_array_flatten (SimpleFunctionFactory& factory) {
89+ factory.register_function <FunctionArrayFlatten>();
90+ }
91+
92+ } // namespace doris::vectorized
0 commit comments