Coverage Report

Created: 2026-03-14 20:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/array/function_array_pushfront.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
21
#include <stddef.h>
22
23
#include <memory>
24
#include <utility>
25
26
#include "common/status.h"
27
#include "core/block/block.h"
28
#include "core/block/column_numbers.h"
29
#include "core/block/column_with_type_and_name.h"
30
#include "core/column/column.h"
31
#include "core/column/column_array.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
46
class FunctionArrayPushfront : public IFunction {
47
public:
48
    static constexpr auto name = "array_pushfront";
49
50
2
    static FunctionPtr create() { return std::make_shared<FunctionArrayPushfront>(); }
51
52
1
    String get_name() const override { return name; }
53
54
1
    bool is_variadic() const override { return false; }
55
56
0
    size_t get_number_of_arguments() const override { return 2; }
57
58
0
    bool use_default_implementation_for_nulls() const override { return false; }
59
60
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
61
        // the type of arguments[0] could be Array(Nullable(xxx)) or Nullable(Array(Nullable(xxx))),
62
        // and we always return Nullable(Array(Nullable(xxx)))
63
0
        return std::make_shared<DataTypeNullable>(remove_nullable(arguments[0]));
64
0
    }
65
66
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
67
0
                        uint32_t result, size_t input_rows_count) const override {
68
0
        auto src_column =
69
0
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
70
        // extract src array column
71
0
        const ColumnArray* array_column = nullptr;
72
0
        const UInt8* array_null_map = nullptr;
73
0
        if (src_column->is_nullable()) {
74
0
            auto nullable_array = static_cast<const ColumnNullable*>(src_column.get());
75
0
            array_column = static_cast<const ColumnArray*>(&nullable_array->get_nested_column());
76
0
            array_null_map = nullable_array->get_null_map_column().get_data().data();
77
0
        } else {
78
0
            array_column = static_cast<const ColumnArray*>(src_column.get());
79
0
        }
80
0
        auto& src_nested_data_col = array_column->get_data();
81
0
        auto& src_offset_col = array_column->get_offsets();
82
83
0
        auto right_column =
84
0
                block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
85
86
        // prepare dst nullable array column
87
0
        auto result_col = block.get_by_position(result).type->create_column();
88
0
        auto result_nullable_col = static_cast<ColumnNullable*>(result_col.get());
89
0
        auto& result_null_map = result_nullable_col->get_null_map_data();
90
0
        auto result_array_col =
91
0
                static_cast<ColumnArray*>(result_nullable_col->get_nested_column_ptr().get());
92
93
0
        auto& result_nested_data_col = result_array_col->get_data();
94
0
        auto& result_offset_col = result_array_col->get_offsets();
95
96
0
        result_null_map.resize(input_rows_count);
97
0
        result_offset_col.resize(input_rows_count);
98
0
        result_nested_data_col.reserve(src_nested_data_col.size() + input_rows_count);
99
100
0
        size_t off = 0;
101
0
        for (size_t i = 0; i < input_rows_count; ++i) {
102
0
            if (array_null_map && array_null_map[i]) {
103
0
                result_null_map[i] = 1;
104
0
                result_offset_col[i] = off;
105
0
                continue;
106
0
            }
107
108
0
            size_t length = src_offset_col[i] - src_offset_col[i - 1];
109
0
            result_nested_data_col.insert((*right_column)[i]);
110
0
            result_nested_data_col.insert_range_from(src_nested_data_col, src_offset_col[i - 1],
111
0
                                                     length);
112
113
0
            off += length + 1;
114
0
            result_null_map[i] = 0;
115
0
            result_offset_col[i] = off;
116
0
        }
117
118
0
        block.replace_by_position(result, std::move(result_col));
119
0
        return Status::OK();
120
0
    }
121
};
122
123
1
void register_function_array_pushfront(SimpleFunctionFactory& factory) {
124
1
    factory.register_function<FunctionArrayPushfront>();
125
1
}
126
127
} // namespace doris