Coverage Report

Created: 2026-03-16 19:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/array/function_array_slice.h
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
#pragma once
18
19
#include <fmt/format.h>
20
#include <glog/logging.h>
21
#include <stddef.h>
22
23
#include <memory>
24
#include <ostream>
25
#include <string>
26
#include <utility>
27
28
#include "common/status.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/data_type/data_type.h"
34
#include "core/types.h"
35
#include "exprs/function/array/function_array_utils.h"
36
#include "exprs/function/function.h"
37
38
namespace doris {
39
class FunctionContext;
40
} // namespace doris
41
42
namespace doris {
43
44
class FunctionArraySlice : public IFunction {
45
public:
46
    static constexpr auto name = "array_slice";
47
2
    static FunctionPtr create() { return std::make_shared<FunctionArraySlice>(); }
48
49
    /// Get function name.
50
0
    String get_name() const override { return name; }
51
52
1
    bool is_variadic() const override { return true; }
53
54
0
    size_t get_number_of_arguments() const override { return 0; }
55
56
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
57
0
        DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
58
0
                << "First argument for function: " << name
59
0
                << " should be DataTypeArray but it has type " << arguments[0]->get_name() << ".";
60
0
        DCHECK(is_int_or_bool(arguments[1]->get_primitive_type()))
61
0
                << "Second argument for function: " << name << " should be Integer but it has type "
62
0
                << arguments[1]->get_name() << ".";
63
0
        if (arguments.size() > 2) {
64
0
            DCHECK(is_int_or_bool(arguments[2]->get_primitive_type()))
65
0
                    << "Third argument for function: " << name
66
0
                    << " should be Integer but it has type " << arguments[2]->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
                        uint32_t result, size_t input_rows_count) const override {
73
0
        auto array_column =
74
0
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
75
0
        auto offset_column =
76
0
                block.get_by_position(arguments[1]).column->convert_to_full_column_if_const();
77
0
        ColumnPtr length_column = nullptr;
78
0
        if (arguments.size() > 2) {
79
0
            length_column =
80
0
                    block.get_by_position(arguments[2]).column->convert_to_full_column_if_const();
81
0
        }
82
        // extract src array column
83
0
        ColumnArrayExecutionData src;
84
0
        if (!extract_column_array_info(*array_column, src)) {
85
0
            return Status::RuntimeError(
86
0
                    fmt::format("execute failed, unsupported types for function {}({}, {})",
87
0
                                get_name(), block.get_by_position(arguments[0]).type->get_name(),
88
0
                                block.get_by_position(arguments[1]).type->get_name()));
89
0
        }
90
        // prepare dst array column
91
0
        bool is_nullable = src.nested_nullmap_data ? true : false;
92
0
        ColumnArrayMutableData dst = create_mutable_data(src.nested_col.get(), is_nullable);
93
0
        dst.offsets_ptr->reserve(input_rows_count);
94
        // execute
95
0
        slice_array(dst, src, *offset_column, length_column.get());
96
0
        ColumnPtr res_column = assemble_column_array(dst);
97
0
        block.replace_by_position(result, std::move(res_column));
98
0
        return Status::OK();
99
0
    }
100
};
101
102
} // namespace doris