Coverage Report

Created: 2026-03-23 17:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_struct.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/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_nullable.h"
32
#include "core/column/column_struct.h"
33
#include "core/column/column_vector.h"
34
#include "core/data_type/data_type.h"
35
#include "core/data_type/data_type_nullable.h"
36
#include "core/data_type/data_type_struct.h"
37
#include "core/typeid_cast.h"
38
#include "core/types.h"
39
#include "exprs/aggregate/aggregate_function.h"
40
#include "exprs/function/function.h"
41
#include "exprs/function/simple_function_factory.h"
42
43
namespace doris {
44
class FunctionContext;
45
} // namespace doris
46
47
namespace doris {
48
49
// construct a struct
50
template <typename Impl>
51
class FunctionStruct : public IFunction {
52
public:
53
    static constexpr auto name = Impl::name;
54
1.00k
    static FunctionPtr create() { return std::make_shared<FunctionStruct>(); }
_ZN5doris14FunctionStructINS_10StructImplEE6createEv
Line
Count
Source
54
380
    static FunctionPtr create() { return std::make_shared<FunctionStruct>(); }
_ZN5doris14FunctionStructINS_15NamedStructImplEE6createEv
Line
Count
Source
54
626
    static FunctionPtr create() { return std::make_shared<FunctionStruct>(); }
55
56
    /// Get function name.
57
0
    String get_name() const override { return name; }
Unexecuted instantiation: _ZNK5doris14FunctionStructINS_10StructImplEE8get_nameB5cxx11Ev
Unexecuted instantiation: _ZNK5doris14FunctionStructINS_15NamedStructImplEE8get_nameB5cxx11Ev
58
59
2
    bool is_variadic() const override { return true; }
_ZNK5doris14FunctionStructINS_10StructImplEE11is_variadicEv
Line
Count
Source
59
1
    bool is_variadic() const override { return true; }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE11is_variadicEv
Line
Count
Source
59
1
    bool is_variadic() const override { return true; }
60
61
1.98k
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris14FunctionStructINS_10StructImplEE36use_default_implementation_for_nullsEv
Line
Count
Source
61
750
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE36use_default_implementation_for_nullsEv
Line
Count
Source
61
1.23k
    bool use_default_implementation_for_nulls() const override { return false; }
62
63
0
    size_t get_number_of_arguments() const override { return 0; }
Unexecuted instantiation: _ZNK5doris14FunctionStructINS_10StructImplEE23get_number_of_argumentsEv
Unexecuted instantiation: _ZNK5doris14FunctionStructINS_15NamedStructImplEE23get_number_of_argumentsEv
64
65
990
    void check_number_of_arguments(size_t number_of_arguments) const override {
66
990
        DCHECK(number_of_arguments > 0)
67
0
                << "function: " << get_name() << ", arguments should not be empty.";
68
990
        return Impl::check_number_of_arguments(number_of_arguments);
69
990
    }
_ZNK5doris14FunctionStructINS_10StructImplEE25check_number_of_argumentsEm
Line
Count
Source
65
372
    void check_number_of_arguments(size_t number_of_arguments) const override {
66
372
        DCHECK(number_of_arguments > 0)
67
0
                << "function: " << get_name() << ", arguments should not be empty.";
68
372
        return Impl::check_number_of_arguments(number_of_arguments);
69
372
    }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE25check_number_of_argumentsEm
Line
Count
Source
65
618
    void check_number_of_arguments(size_t number_of_arguments) const override {
66
618
        DCHECK(number_of_arguments > 0)
67
0
                << "function: " << get_name() << ", arguments should not be empty.";
68
618
        return Impl::check_number_of_arguments(number_of_arguments);
69
618
    }
70
71
990
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
72
990
        return Impl::get_return_type_impl(arguments);
73
990
    }
_ZNK5doris14FunctionStructINS_10StructImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
71
372
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
72
372
        return Impl::get_return_type_impl(arguments);
73
372
    }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
71
618
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
72
618
        return Impl::get_return_type_impl(arguments);
73
618
    }
74
75
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
76
997
                        uint32_t result, size_t input_rows_count) const override {
77
997
        auto result_col = block.get_by_position(result).type->create_column();
78
997
        auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
79
997
        ColumnNumbers args_num;
80
8.35k
        for (size_t i = 0; i < arguments.size(); i++) {
81
7.35k
            if (Impl::pred(i)) {
82
5.47k
                args_num.push_back(arguments[i]);
83
5.47k
            }
84
7.35k
        }
85
997
        size_t num_element = args_num.size();
86
997
        if (num_element != struct_column->tuple_size()) {
87
0
            return Status::RuntimeError(
88
0
                    "function {} args number {} is not equal to result struct field number {}.",
89
0
                    get_name(), num_element, struct_column->tuple_size());
90
0
        }
91
997
        std::vector<ColumnPtr> arg(num_element);
92
6.47k
        for (size_t i = 0; i < num_element; ++i) {
93
5.47k
            auto& nested_col = struct_column->get_column(i);
94
5.47k
            nested_col.reserve(input_rows_count);
95
5.47k
            bool is_nullable = nested_col.is_nullable();
96
5.47k
            auto& col = block.get_by_position(args_num[i]).column;
97
5.47k
            col = col->convert_to_full_column_if_const();
98
5.47k
            arg[i] = col;
99
5.47k
            if (is_nullable && !col->is_nullable()) {
100
4.94k
                arg[i] = ColumnNullable::create(col, ColumnUInt8::create(col->size(), 0));
101
4.94k
            }
102
5.47k
        }
103
104
        // insert value into struct column by column
105
6.47k
        for (size_t i = 0; i < num_element; ++i) {
106
5.47k
            struct_column->get_column(i).insert_range_from(*arg[i], 0, input_rows_count);
107
5.47k
        }
108
997
        block.replace_by_position(result, std::move(result_col));
109
997
        return Status::OK();
110
997
    }
_ZNK5doris14FunctionStructINS_10StructImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
76
378
                        uint32_t result, size_t input_rows_count) const override {
77
378
        auto result_col = block.get_by_position(result).type->create_column();
78
378
        auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
79
378
        ColumnNumbers args_num;
80
3.97k
        for (size_t i = 0; i < arguments.size(); i++) {
81
3.59k
            if (Impl::pred(i)) {
82
3.59k
                args_num.push_back(arguments[i]);
83
3.59k
            }
84
3.59k
        }
85
378
        size_t num_element = args_num.size();
86
378
        if (num_element != struct_column->tuple_size()) {
87
0
            return Status::RuntimeError(
88
0
                    "function {} args number {} is not equal to result struct field number {}.",
89
0
                    get_name(), num_element, struct_column->tuple_size());
90
0
        }
91
378
        std::vector<ColumnPtr> arg(num_element);
92
3.97k
        for (size_t i = 0; i < num_element; ++i) {
93
3.59k
            auto& nested_col = struct_column->get_column(i);
94
3.59k
            nested_col.reserve(input_rows_count);
95
3.59k
            bool is_nullable = nested_col.is_nullable();
96
3.59k
            auto& col = block.get_by_position(args_num[i]).column;
97
3.59k
            col = col->convert_to_full_column_if_const();
98
3.59k
            arg[i] = col;
99
3.59k
            if (is_nullable && !col->is_nullable()) {
100
3.14k
                arg[i] = ColumnNullable::create(col, ColumnUInt8::create(col->size(), 0));
101
3.14k
            }
102
3.59k
        }
103
104
        // insert value into struct column by column
105
3.97k
        for (size_t i = 0; i < num_element; ++i) {
106
3.59k
            struct_column->get_column(i).insert_range_from(*arg[i], 0, input_rows_count);
107
3.59k
        }
108
378
        block.replace_by_position(result, std::move(result_col));
109
378
        return Status::OK();
110
378
    }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
76
619
                        uint32_t result, size_t input_rows_count) const override {
77
619
        auto result_col = block.get_by_position(result).type->create_column();
78
619
        auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
79
619
        ColumnNumbers args_num;
80
4.38k
        for (size_t i = 0; i < arguments.size(); i++) {
81
3.76k
            if (Impl::pred(i)) {
82
1.88k
                args_num.push_back(arguments[i]);
83
1.88k
            }
84
3.76k
        }
85
619
        size_t num_element = args_num.size();
86
619
        if (num_element != struct_column->tuple_size()) {
87
0
            return Status::RuntimeError(
88
0
                    "function {} args number {} is not equal to result struct field number {}.",
89
0
                    get_name(), num_element, struct_column->tuple_size());
90
0
        }
91
619
        std::vector<ColumnPtr> arg(num_element);
92
2.50k
        for (size_t i = 0; i < num_element; ++i) {
93
1.88k
            auto& nested_col = struct_column->get_column(i);
94
1.88k
            nested_col.reserve(input_rows_count);
95
1.88k
            bool is_nullable = nested_col.is_nullable();
96
1.88k
            auto& col = block.get_by_position(args_num[i]).column;
97
1.88k
            col = col->convert_to_full_column_if_const();
98
1.88k
            arg[i] = col;
99
1.88k
            if (is_nullable && !col->is_nullable()) {
100
1.80k
                arg[i] = ColumnNullable::create(col, ColumnUInt8::create(col->size(), 0));
101
1.80k
            }
102
1.88k
        }
103
104
        // insert value into struct column by column
105
2.50k
        for (size_t i = 0; i < num_element; ++i) {
106
1.88k
            struct_column->get_column(i).insert_range_from(*arg[i], 0, input_rows_count);
107
1.88k
        }
108
619
        block.replace_by_position(result, std::move(result_col));
109
619
        return Status::OK();
110
619
    }
111
};
112
113
// struct(value1, value2, value3) -> {value1, value2, value3}
114
struct StructImpl {
115
    static constexpr auto name = "struct";
116
3.59k
    static constexpr auto pred = [](size_t i) { return true; };
117
118
372
    static void check_number_of_arguments(size_t number_of_arguments) {}
119
120
372
    static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
121
372
        return std::make_shared<DataTypeStruct>(make_nullable(arguments));
122
372
    }
123
};
124
125
// named_struct(name1, value1, name2, value2) -> {name1:value1, name2:value2}
126
struct NamedStructImpl {
127
    static constexpr auto name = "named_struct";
128
3.76k
    static constexpr auto pred = [](size_t i) { return (i & 1) == 1; };
129
130
618
    static void check_number_of_arguments(size_t number_of_arguments) {
131
618
        DCHECK(number_of_arguments % 2 == 0)
132
0
                << "function: " << name << ", arguments size should be even number.";
133
618
    }
134
135
618
    static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
136
618
        DataTypes data_types(arguments.size() / 2);
137
618
        size_t even_idx = 1;
138
2.49k
        for (size_t i = 0; i < data_types.size(); i++) {
139
1.88k
            data_types[i] = arguments[even_idx];
140
1.88k
            even_idx += 2;
141
1.88k
        }
142
618
        return std::make_shared<DataTypeStruct>(make_nullable(data_types));
143
618
    }
144
};
145
146
7
void register_function_struct(SimpleFunctionFactory& factory) {
147
7
    factory.register_function<FunctionStruct<StructImpl>>();
148
7
    factory.register_function<FunctionStruct<NamedStructImpl>>();
149
7
}
150
151
} // namespace doris