be/src/exprs/function/array/function_array_pushback.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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/array/arrayIndex.h |
19 | | // and modified by Doris |
20 | | #include <stddef.h> |
21 | | |
22 | | #include <memory> |
23 | | #include <utility> |
24 | | |
25 | | #include "common/status.h" |
26 | | #include "core/block/block.h" |
27 | | #include "core/block/column_numbers.h" |
28 | | #include "core/block/column_with_type_and_name.h" |
29 | | #include "core/column/column.h" |
30 | | #include "core/column/column_array.h" |
31 | | #include "core/column/column_const.h" |
32 | | #include "core/column/column_nullable.h" |
33 | | #include "core/data_type/data_type.h" |
34 | | #include "core/data_type/data_type_nullable.h" |
35 | | #include "core/types.h" |
36 | | #include "exprs/aggregate/aggregate_function.h" |
37 | | #include "exprs/function/function.h" |
38 | | #include "exprs/function/simple_function_factory.h" |
39 | | |
40 | | namespace doris { |
41 | | class FunctionContext; |
42 | | } // namespace doris |
43 | | |
44 | | namespace doris { |
45 | | class FunctionArrayPushback : public IFunction { |
46 | | public: |
47 | | static constexpr auto name = "array_pushback"; |
48 | | |
49 | 2 | static FunctionPtr create() { return std::make_shared<FunctionArrayPushback>(); } |
50 | | |
51 | 1 | String get_name() const override { return name; } |
52 | | |
53 | 1 | bool is_variadic() const override { return false; } |
54 | | |
55 | 0 | size_t get_number_of_arguments() const override { return 2; } |
56 | | |
57 | 0 | bool use_default_implementation_for_nulls() const override { return false; } |
58 | | |
59 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
60 | 0 | return std::make_shared<DataTypeNullable>(remove_nullable(arguments[0])); |
61 | 0 | }; |
62 | | |
63 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
64 | 0 | uint32_t result, size_t input_rows_count) const override { |
65 | 0 | const auto& [src_column, src_const] = |
66 | 0 | unpack_if_const(block.get_by_position(arguments[0]).column); |
67 | 0 | const auto& [right_column, right_const] = |
68 | 0 | unpack_if_const(block.get_by_position(arguments[1]).column); |
69 | | // extract src array column |
70 | 0 | const ColumnArray* array_column = nullptr; |
71 | 0 | const UInt8* array_null_map = nullptr; |
72 | 0 | if (src_column->is_nullable()) { |
73 | 0 | auto nullable_array = static_cast<const ColumnNullable*>(src_column.get()); |
74 | 0 | array_column = assert_cast<const ColumnArray*>(&nullable_array->get_nested_column()); |
75 | 0 | array_null_map = nullable_array->get_null_map_column().get_data().data(); |
76 | 0 | } else { |
77 | 0 | array_column = assert_cast<const ColumnArray*>(src_column.get()); |
78 | 0 | } |
79 | 0 | auto& src_nested_data_col = array_column->get_data(); |
80 | 0 | auto& src_offset_col = array_column->get_offsets(); |
81 | |
|
82 | 0 | auto result_col = block.get_by_position(result).type->create_column(); |
83 | 0 | auto result_nullable_col = assert_cast<ColumnNullable*>(result_col.get()); |
84 | 0 | auto& result_null_map = result_nullable_col->get_null_map_data(); |
85 | 0 | auto result_array_col = |
86 | 0 | assert_cast<ColumnArray*>(result_nullable_col->get_nested_column_ptr().get()); |
87 | |
|
88 | 0 | auto& result_nested_data_col = result_array_col->get_data(); |
89 | 0 | auto& result_offset_col = result_array_col->get_offsets(); |
90 | |
|
91 | 0 | result_null_map.resize(input_rows_count); |
92 | 0 | result_offset_col.resize(input_rows_count); |
93 | 0 | result_nested_data_col.reserve(src_nested_data_col.size() + input_rows_count); |
94 | |
|
95 | 0 | size_t off = 0; |
96 | 0 | for (size_t i = 0; i < input_rows_count; ++i) { |
97 | 0 | if (array_null_map && array_null_map[i]) { |
98 | 0 | result_null_map[i] = 1; |
99 | 0 | result_offset_col[i] = off; |
100 | 0 | continue; |
101 | 0 | } |
102 | | |
103 | 0 | size_t src_off = src_offset_col[index_check_const(i, src_const) - 1]; |
104 | 0 | size_t src_len = src_offset_col[index_check_const(i, src_const)] - src_off; |
105 | 0 | result_nested_data_col.insert_range_from(src_nested_data_col, src_off, src_len); |
106 | |
|
107 | 0 | result_nested_data_col.insert((*right_column)[index_check_const(i, right_const)]); |
108 | 0 | off += src_len + 1; |
109 | 0 | result_null_map[i] = 0; |
110 | 0 | result_offset_col[i] = off; |
111 | 0 | } |
112 | 0 | block.replace_by_position(result, std::move(result_col)); |
113 | 0 | return Status::OK(); |
114 | 0 | } |
115 | | }; |
116 | | |
117 | 1 | void register_function_array_pushback(SimpleFunctionFactory& factory) { |
118 | 1 | factory.register_function<FunctionArrayPushback>(); |
119 | 1 | factory.register_alias("array_pushback", "array_append"); |
120 | 1 | } |
121 | | } // namespace doris |