Coverage Report

Created: 2026-03-13 19:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_size.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 "core/column/column_array.h"
19
#include "core/column/column_map.h"
20
#include "core/data_type/data_type_array.h"
21
#include "core/data_type/data_type_number.h"
22
#include "exprs/function/array/function_array_utils.h"
23
#include "exprs/function/function.h"
24
#include "exprs/function/function_helpers.h"
25
#include "exprs/function/simple_function_factory.h"
26
27
namespace doris {
28
29
// size function for size with map and array
30
class FunctionSize : public IFunction {
31
public:
32
    static constexpr auto name = "size";
33
8
    static FunctionPtr create() { return std::make_shared<FunctionSize>(); }
34
0
    String get_name() const override { return name; }
35
7
    bool is_variadic() const override { return true; }
36
0
    size_t get_number_of_arguments() const override { return 0; }
37
12
    bool use_default_implementation_for_constants() const override { return false; }
38
39
6
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
40
6
        DataTypePtr datatype = arguments[0];
41
6
        DCHECK(datatype->get_primitive_type() == TYPE_MAP ||
42
0
               datatype->get_primitive_type() == TYPE_ARRAY)
43
0
                << "first argument for function: " << name
44
0
                << " should be DataTypeMap or DataTypeArray";
45
6
        return std::make_shared<DataTypeInt64>();
46
6
    }
47
48
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
49
6
                        uint32_t result, size_t input_rows_count) const override {
50
6
        const auto& [left_column, left_const] =
51
6
                unpack_if_const(block.get_by_position(arguments[0]).column);
52
6
        const auto type = block.get_by_position(arguments[0]).type;
53
6
        const ColumnArray* array_column = nullptr;
54
6
        const ColumnMap* map_column = nullptr;
55
6
        if (type->get_primitive_type() == TYPE_ARRAY) {
56
6
            array_column = check_and_get_column<ColumnArray>(*left_column.get());
57
6
        } else if (type->get_primitive_type() == TYPE_MAP) {
58
0
            map_column = check_and_get_column<ColumnMap>(*left_column.get());
59
0
        }
60
61
6
        auto dst_column = ColumnInt64::create(input_rows_count);
62
6
        auto& dst_data = dst_column->get_data();
63
64
6
        if (left_const && map_column) {
65
0
            for (size_t i = 0; i < input_rows_count; i++) {
66
0
                dst_data[i] = map_column->size_at(0);
67
0
            }
68
6
        } else if (left_const && array_column) {
69
0
            for (size_t i = 0; i < input_rows_count; i++) {
70
0
                dst_data[i] = array_column->size_at(0);
71
0
            }
72
6
        } else if (map_column) {
73
0
            for (size_t i = 0; i < map_column->size(); i++) {
74
0
                dst_data[i] = map_column->size_at(i);
75
0
            }
76
6
        } else if (array_column) {
77
27
            for (size_t i = 0; i < array_column->size(); i++) {
78
21
                dst_data[i] = array_column->size_at(i);
79
21
            }
80
6
        } else {
81
0
            return Status::RuntimeError("unsupported types for function {}({})", get_name(),
82
0
                                        block.get_by_position(arguments[0]).type->get_name());
83
0
        }
84
85
6
        block.replace_by_position(result, std::move(dst_column));
86
6
        return Status::OK();
87
6
    }
88
};
89
90
1
void register_function_size(SimpleFunctionFactory& factory) {
91
1
    factory.register_function<FunctionSize>();
92
1
    factory.register_alias(FunctionSize::name, "map_size");
93
1
    factory.register_alias(FunctionSize::name, "cardinality");
94
1
    factory.register_alias(FunctionSize::name, "array_size");
95
1
}
96
} // namespace doris