Coverage Report

Created: 2026-03-16 01:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/array/function_array_pop.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 <fmt/format.h>
19
#include <glog/logging.h>
20
21
#include <cstddef>
22
#include <memory>
23
#include <ostream>
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_vector.h"
32
#include "core/data_type/data_type.h"
33
#include "core/types.h"
34
#include "exprs/aggregate/aggregate_function.h"
35
#include "exprs/function/array/function_array_utils.h"
36
#include "exprs/function/function.h"
37
#include "exprs/function/simple_function_factory.h"
38
39
namespace doris {
40
class FunctionContext;
41
} // namespace doris
42
43
namespace doris {
44
45
template <typename PopType>
46
class FunctionArrayPop : public IFunction {
47
public:
48
4
    static FunctionPtr create() { return std::make_shared<PopType>(); }
_ZN5doris16FunctionArrayPopINS_20FunctionArrayPopbackEE6createEv
Line
Count
Source
48
2
    static FunctionPtr create() { return std::make_shared<PopType>(); }
_ZN5doris16FunctionArrayPopINS_21FunctionArrayPopfrontEE6createEv
Line
Count
Source
48
2
    static FunctionPtr create() { return std::make_shared<PopType>(); }
49
50
    /// Get function name.
51
2
    String get_name() const override { return PopType::name; }
_ZNK5doris16FunctionArrayPopINS_20FunctionArrayPopbackEE8get_nameB5cxx11Ev
Line
Count
Source
51
1
    String get_name() const override { return PopType::name; }
_ZNK5doris16FunctionArrayPopINS_21FunctionArrayPopfrontEE8get_nameB5cxx11Ev
Line
Count
Source
51
1
    String get_name() const override { return PopType::name; }
52
53
2
    bool is_variadic() const override { return false; }
_ZNK5doris16FunctionArrayPopINS_20FunctionArrayPopbackEE11is_variadicEv
Line
Count
Source
53
1
    bool is_variadic() const override { return false; }
_ZNK5doris16FunctionArrayPopINS_21FunctionArrayPopfrontEE11is_variadicEv
Line
Count
Source
53
1
    bool is_variadic() const override { return false; }
54
55
0
    size_t get_number_of_arguments() const override { return 1; }
Unexecuted instantiation: _ZNK5doris16FunctionArrayPopINS_20FunctionArrayPopbackEE23get_number_of_argumentsEv
Unexecuted instantiation: _ZNK5doris16FunctionArrayPopINS_21FunctionArrayPopfrontEE23get_number_of_argumentsEv
56
57
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
58
0
        DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
59
0
                << "First argument for function: " << PopType::name
60
0
                << " should be DataTypeArray but it has type " << arguments[0]->get_name() << ".";
61
0
        return arguments[0];
62
0
    }
Unexecuted instantiation: _ZNK5doris16FunctionArrayPopINS_20FunctionArrayPopbackEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Unexecuted instantiation: _ZNK5doris16FunctionArrayPopINS_21FunctionArrayPopfrontEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
63
64
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
65
0
                        uint32_t result, size_t input_rows_count) const override {
66
0
        auto array_column =
67
0
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
68
        // extract src array column
69
0
        ColumnArrayExecutionData src;
70
0
        if (!extract_column_array_info(*array_column, src)) {
71
0
            return Status::RuntimeError(
72
0
                    fmt::format("execute failed, unsupported types for function {}({})", get_name(),
73
0
                                block.get_by_position(arguments[0]).type->get_name()));
74
0
        }
75
        // prepare dst array column
76
0
        bool is_nullable = src.nested_nullmap_data != nullptr;
77
0
        ColumnArrayMutableData dst = create_mutable_data(src.nested_col.get(), is_nullable);
78
0
        dst.offsets_ptr->reserve(input_rows_count);
79
        // start from index depending on the PopType::start_offset
80
0
        auto offset_column = ColumnInt64::create(array_column->size(), PopType::start_offset);
81
        // len - 1
82
0
        auto length_column = ColumnInt64::create();
83
0
        for (size_t row = 0; row < src.offsets_ptr->size(); ++row) {
84
0
            size_t off = (*src.offsets_ptr)[row - 1];
85
0
            size_t len = (*src.offsets_ptr)[row] - off;
86
0
            length_column->insert_value(len - 1);
87
0
        }
88
0
        slice_array(dst, src, *offset_column, length_column.get());
89
0
        ColumnPtr res_column = assemble_column_array(dst);
90
0
        block.replace_by_position(result, std::move(res_column));
91
0
        return Status::OK();
92
0
    }
Unexecuted instantiation: _ZNK5doris16FunctionArrayPopINS_20FunctionArrayPopbackEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Unexecuted instantiation: _ZNK5doris16FunctionArrayPopINS_21FunctionArrayPopfrontEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
93
};
94
95
class FunctionArrayPopback : public FunctionArrayPop<FunctionArrayPopback> {
96
public:
97
    static constexpr auto name = "array_popback";
98
    static constexpr int start_offset = 1;
99
};
100
101
class FunctionArrayPopfront : public FunctionArrayPop<FunctionArrayPopfront> {
102
public:
103
    static constexpr auto name = "array_popfront";
104
    static constexpr int start_offset = 2;
105
};
106
107
1
void register_function_array_pop(SimpleFunctionFactory& factory) {
108
1
    factory.register_function<FunctionArrayPopback>();
109
1
    factory.register_function<FunctionArrayPopfront>();
110
1
}
111
112
} // namespace doris