Coverage Report

Created: 2026-03-15 17:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/array/function_array_exists.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
#include <stddef.h>
20
21
#include <memory>
22
#include <ostream>
23
#include <string>
24
#include <utility>
25
26
#include "common/status.h"
27
#include "core/assert_cast.h"
28
#include "core/block/block.h"
29
#include "core/block/column_numbers.h"
30
#include "core/block/column_with_type_and_name.h"
31
#include "core/column/column.h"
32
#include "core/column/column_array.h"
33
#include "core/column/column_nullable.h"
34
#include "core/column/column_vector.h"
35
#include "core/data_type/data_type.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_exists([1, 2, 3, 0]) -> [1, 1, 1, 0]
48
class FunctionArrayExists : public IFunction {
49
public:
50
    static constexpr auto name = "array_exists";
51
52
2
    static FunctionPtr create() { return std::make_shared<FunctionArrayExists>(); }
53
54
1
    String get_name() const override { return name; }
55
56
0
    size_t get_number_of_arguments() const override { return 1; }
57
58
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
59
0
        DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
60
0
                << "first argument for function: " << name << " should be DataTypeArray"
61
0
                << " and arguments[0] is " << arguments[0]->get_name();
62
0
        return arguments[0];
63
0
    }
64
65
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
66
0
                        uint32_t result, size_t input_rows_count) const override {
67
        // 1. get first array column
68
0
        const auto first_column =
69
0
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
70
0
        const ColumnArray& first_col_array = assert_cast<const ColumnArray&>(*first_column);
71
0
        const auto& first_off_data = assert_cast<const ColumnArray::ColumnOffsets&>(
72
0
                first_col_array.get_offsets_column());
73
74
0
        const auto& nested_nullable_column =
75
0
                assert_cast<const ColumnNullable&>(*first_col_array.get_data_ptr());
76
0
        const auto nested_column = nested_nullable_column.get_nested_column_ptr();
77
0
        const size_t nested_column_size = nested_column->size();
78
0
        MutableColumnPtr result_null_map =
79
0
                nested_nullable_column.get_null_map_column_ptr()->clone_resized(nested_column_size);
80
81
        // 2. compute result
82
0
        MutableColumnPtr result_column = ColumnUInt8::create(nested_column_size, 0);
83
0
        auto* __restrict result_column_data =
84
0
                assert_cast<ColumnUInt8&>(*result_column).get_data().data();
85
0
        MutableColumnPtr result_offset_column = first_off_data.clone_resized(first_off_data.size());
86
0
        const auto* __restrict nested_column_data =
87
0
                assert_cast<const ColumnUInt8&>(*nested_column).get_data().data();
88
89
0
        for (size_t row = 0; row < nested_column_size; ++row) {
90
0
            result_column_data[row] = nested_column_data[row] != 0;
91
0
        }
92
93
0
        ColumnPtr result_nullalble_column =
94
0
                ColumnNullable::create(std::move(result_column), std::move(result_null_map));
95
0
        ColumnPtr column_array =
96
0
                ColumnArray::create(result_nullalble_column, std::move(result_offset_column));
97
0
        block.replace_by_position(result, column_array);
98
0
        return Status::OK();
99
0
    }
100
};
101
102
1
void register_function_array_exists(SimpleFunctionFactory& factory) {
103
1
    factory.register_function<FunctionArrayExists>();
104
1
}
105
106
} // namespace doris