be/src/exprs/function/array/function_array_concat.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 <glog/logging.h> |
19 | | |
20 | | #include <cstddef> |
21 | | #include <memory> |
22 | | #include <ostream> |
23 | | #include <string> |
24 | | #include <utility> |
25 | | #include <vector> |
26 | | |
27 | | #include "common/status.h" |
28 | | #include "core/assert_cast.h" |
29 | | #include "core/block/block.h" |
30 | | #include "core/block/column_numbers.h" |
31 | | #include "core/block/column_with_type_and_name.h" |
32 | | #include "core/column/column.h" |
33 | | #include "core/column/column_array.h" |
34 | | #include "core/data_type/data_type.h" |
35 | | #include "core/data_type/data_type_array.h" |
36 | | #include "core/types.h" |
37 | | #include "exprs/aggregate/aggregate_function.h" |
38 | | #include "exprs/function/function.h" |
39 | | #include "exprs/function/simple_function_factory.h" |
40 | | |
41 | | namespace doris { |
42 | | class FunctionContext; |
43 | | } // namespace doris |
44 | | |
45 | | namespace doris { |
46 | | |
47 | | // array_concat([1, 2], [7, 8], [5, 6]) -> [1, 2, 7, 8, 5, 6] |
48 | | class FunctionArrayConcat : public IFunction { |
49 | | public: |
50 | | static constexpr auto name = "array_concat"; |
51 | | |
52 | 2 | static FunctionPtr create() { return std::make_shared<FunctionArrayConcat>(); } |
53 | | |
54 | 0 | String get_name() const override { return name; } |
55 | | |
56 | 1 | bool is_variadic() const override { return true; } |
57 | | |
58 | 0 | size_t get_number_of_arguments() const override { return 1; } |
59 | | |
60 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
61 | 0 | DCHECK(arguments.size() > 0) |
62 | 0 | << "function: " << get_name() << ", arguments should not be empty"; |
63 | 0 | for (const auto& arg : arguments) { |
64 | 0 | DCHECK(arg->get_primitive_type() == TYPE_ARRAY) |
65 | 0 | << "argument for function array_concat should be DataTypeArray" |
66 | 0 | << " and argument is " << arg->get_name(); |
67 | 0 | } |
68 | 0 | return arguments[0]; |
69 | 0 | } |
70 | | |
71 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
72 | 0 | const uint32_t result, size_t input_rows_count) const override { |
73 | 0 | DataTypePtr column_type = block.get_by_position(arguments[0]).type; |
74 | 0 | auto nested_type = assert_cast<const DataTypeArray&>(*column_type).get_nested_type(); |
75 | 0 | auto result_column = ColumnArray::create(nested_type->create_column(), |
76 | 0 | ColumnArray::ColumnOffsets::create()); |
77 | 0 | IColumn& result_nested_col = result_column->get_data(); |
78 | 0 | ColumnArray::Offsets64& column_offsets = result_column->get_offsets(); |
79 | 0 | column_offsets.resize(input_rows_count); |
80 | |
|
81 | 0 | size_t total_size = 0; |
82 | 0 | for (size_t col : arguments) { |
83 | 0 | ColumnPtr src_column = |
84 | 0 | block.get_by_position(col).column->convert_to_full_column_if_const(); |
85 | 0 | const auto& src_column_array = check_and_get_column<ColumnArray>(*src_column); |
86 | 0 | total_size += src_column_array->get_data().size(); |
87 | 0 | } |
88 | 0 | result_nested_col.reserve(total_size); |
89 | |
|
90 | 0 | size_t off = 0; |
91 | 0 | for (size_t row = 0; row < input_rows_count; ++row) { |
92 | 0 | for (size_t col : arguments) { |
93 | 0 | ColumnPtr src_column = |
94 | 0 | block.get_by_position(col).column->convert_to_full_column_if_const(); |
95 | 0 | const auto& src_column_array = check_and_get_column<ColumnArray>(*src_column); |
96 | 0 | const auto& src_column_offsets = src_column_array->get_offsets(); |
97 | 0 | const size_t length = src_column_offsets[row] - src_column_offsets[row - 1]; |
98 | 0 | result_nested_col.insert_range_from(src_column_array->get_data(), |
99 | 0 | src_column_offsets[row - 1], length); |
100 | 0 | off += length; |
101 | 0 | } |
102 | 0 | column_offsets[row] = off; |
103 | 0 | } |
104 | |
|
105 | 0 | block.replace_by_position(result, std::move(result_column)); |
106 | 0 | return Status::OK(); |
107 | 0 | } |
108 | | }; |
109 | | |
110 | 1 | void register_function_array_concat(SimpleFunctionFactory& factory) { |
111 | 1 | factory.register_function<FunctionArrayConcat>(); |
112 | 1 | } |
113 | | |
114 | | } // namespace doris |