Coverage Report

Created: 2026-07-31 13:05

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
3.68k
    static FunctionPtr create() { return std::make_shared<FunctionStruct>(); }
_ZN5doris14FunctionStructINS_10StructImplEE6createEv
Line
Count
Source
54
893
    static FunctionPtr create() { return std::make_shared<FunctionStruct>(); }
_ZN5doris14FunctionStructINS_15NamedStructImplEE6createEv
Line
Count
Source
54
2.79k
    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
7.81k
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris14FunctionStructINS_10StructImplEE36use_default_implementation_for_nullsEv
Line
Count
Source
61
1.79k
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE36use_default_implementation_for_nullsEv
Line
Count
Source
61
6.01k
    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
3.66k
    void check_number_of_arguments(size_t number_of_arguments) const override {
66
3.66k
        DCHECK(number_of_arguments > 0)
67
0
                << "function: " << get_name() << ", arguments should not be empty.";
68
3.66k
        return Impl::check_number_of_arguments(number_of_arguments);
69
3.66k
    }
_ZNK5doris14FunctionStructINS_10StructImplEE25check_number_of_argumentsEm
Line
Count
Source
65
884
    void check_number_of_arguments(size_t number_of_arguments) const override {
66
884
        DCHECK(number_of_arguments > 0)
67
0
                << "function: " << get_name() << ", arguments should not be empty.";
68
884
        return Impl::check_number_of_arguments(number_of_arguments);
69
884
    }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE25check_number_of_argumentsEm
Line
Count
Source
65
2.78k
    void check_number_of_arguments(size_t number_of_arguments) const override {
66
2.78k
        DCHECK(number_of_arguments > 0)
67
0
                << "function: " << get_name() << ", arguments should not be empty.";
68
2.78k
        return Impl::check_number_of_arguments(number_of_arguments);
69
2.78k
    }
70
71
3.66k
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
72
3.66k
        return Impl::get_return_type_impl(arguments);
73
3.66k
    }
_ZNK5doris14FunctionStructINS_10StructImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
71
884
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
72
884
        return Impl::get_return_type_impl(arguments);
73
884
    }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
71
2.78k
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
72
2.78k
        return Impl::get_return_type_impl(arguments);
73
2.78k
    }
74
75
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
76
4.14k
                        uint32_t result, size_t input_rows_count) const override {
77
4.14k
        auto result_col = block.get_by_position(result).type->create_column();
78
4.14k
        auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
79
4.14k
        ColumnNumbers args_num;
80
22.7k
        for (size_t i = 0; i < arguments.size(); i++) {
81
18.5k
            if (Impl::pred(i)) {
82
11.9k
                args_num.push_back(arguments[i]);
83
11.9k
            }
84
18.5k
        }
85
4.14k
        size_t num_element = args_num.size();
86
4.14k
        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
4.14k
        std::vector<ColumnPtr> arg(num_element);
92
16.0k
        for (size_t i = 0; i < num_element; ++i) {
93
11.9k
            auto& nested_col = struct_column->get_column(i);
94
11.9k
            nested_col.reserve(input_rows_count);
95
11.9k
            bool is_nullable = nested_col.is_nullable();
96
11.9k
            auto& col = block.get_by_position(args_num[i]).column;
97
11.9k
            col = col->convert_to_full_column_if_const();
98
11.9k
            arg[i] = col;
99
11.9k
            if (is_nullable && !col->is_nullable()) {
100
3
                arg[i] = ColumnNullable::create(col, ColumnUInt8::create(col->size(), 0));
101
3
            }
102
11.9k
        }
103
104
        // insert value into struct column by column
105
16.0k
        for (size_t i = 0; i < num_element; ++i) {
106
11.9k
            struct_column->get_column(i).insert_range_from(*arg[i], 0, input_rows_count);
107
11.9k
        }
108
4.14k
        block.replace_by_position(result, std::move(result_col));
109
4.14k
        return Status::OK();
110
4.14k
    }
_ZNK5doris14FunctionStructINS_10StructImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
76
910
                        uint32_t result, size_t input_rows_count) const override {
77
910
        auto result_col = block.get_by_position(result).type->create_column();
78
910
        auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
79
910
        ColumnNumbers args_num;
80
6.15k
        for (size_t i = 0; i < arguments.size(); i++) {
81
5.24k
            if (Impl::pred(i)) {
82
5.24k
                args_num.push_back(arguments[i]);
83
5.24k
            }
84
5.24k
        }
85
910
        size_t num_element = args_num.size();
86
910
        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
910
        std::vector<ColumnPtr> arg(num_element);
92
6.15k
        for (size_t i = 0; i < num_element; ++i) {
93
5.24k
            auto& nested_col = struct_column->get_column(i);
94
5.24k
            nested_col.reserve(input_rows_count);
95
5.24k
            bool is_nullable = nested_col.is_nullable();
96
5.24k
            auto& col = block.get_by_position(args_num[i]).column;
97
5.24k
            col = col->convert_to_full_column_if_const();
98
5.24k
            arg[i] = col;
99
5.24k
            if (is_nullable && !col->is_nullable()) {
100
0
                arg[i] = ColumnNullable::create(col, ColumnUInt8::create(col->size(), 0));
101
0
            }
102
5.24k
        }
103
104
        // insert value into struct column by column
105
6.15k
        for (size_t i = 0; i < num_element; ++i) {
106
5.24k
            struct_column->get_column(i).insert_range_from(*arg[i], 0, input_rows_count);
107
5.24k
        }
108
910
        block.replace_by_position(result, std::move(result_col));
109
910
        return Status::OK();
110
910
    }
_ZNK5doris14FunctionStructINS_15NamedStructImplEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
76
3.23k
                        uint32_t result, size_t input_rows_count) const override {
77
3.23k
        auto result_col = block.get_by_position(result).type->create_column();
78
3.23k
        auto struct_column = assert_cast<ColumnStruct*>(result_col.get());
79
3.23k
        ColumnNumbers args_num;
80
16.5k
        for (size_t i = 0; i < arguments.size(); i++) {
81
13.3k
            if (Impl::pred(i)) {
82
6.67k
                args_num.push_back(arguments[i]);
83
6.67k
            }
84
13.3k
        }
85
3.23k
        size_t num_element = args_num.size();
86
3.23k
        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
3.23k
        std::vector<ColumnPtr> arg(num_element);
92
9.91k
        for (size_t i = 0; i < num_element; ++i) {
93
6.67k
            auto& nested_col = struct_column->get_column(i);
94
6.67k
            nested_col.reserve(input_rows_count);
95
6.67k
            bool is_nullable = nested_col.is_nullable();
96
6.67k
            auto& col = block.get_by_position(args_num[i]).column;
97
6.67k
            col = col->convert_to_full_column_if_const();
98
6.67k
            arg[i] = col;
99
6.67k
            if (is_nullable && !col->is_nullable()) {
100
3
                arg[i] = ColumnNullable::create(col, ColumnUInt8::create(col->size(), 0));
101
3
            }
102
6.67k
        }
103
104
        // insert value into struct column by column
105
9.91k
        for (size_t i = 0; i < num_element; ++i) {
106
6.67k
            struct_column->get_column(i).insert_range_from(*arg[i], 0, input_rows_count);
107
6.67k
        }
108
3.23k
        block.replace_by_position(result, std::move(result_col));
109
3.23k
        return Status::OK();
110
3.23k
    }
111
};
112
113
// struct(value1, value2, value3) -> {value1, value2, value3}
114
struct StructImpl {
115
    static constexpr auto name = "struct";
116
5.24k
    static constexpr auto pred = [](size_t i) { return true; };
117
118
884
    static void check_number_of_arguments(size_t number_of_arguments) {}
119
120
884
    static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
121
        // FE plans the same child-nullability contract, so widening fields here makes the
122
        // serialized plan disagree with BE's inferred result type.
123
884
        return std::make_shared<DataTypeStruct>(arguments);
124
884
    }
125
};
126
127
// named_struct(name1, value1, name2, value2) -> {name1:value1, name2:value2}
128
struct NamedStructImpl {
129
    static constexpr auto name = "named_struct";
130
13.3k
    static constexpr auto pred = [](size_t i) { return (i & 1) == 1; };
131
132
2.78k
    static void check_number_of_arguments(size_t number_of_arguments) {
133
2.78k
        DCHECK(number_of_arguments % 2 == 0)
134
0
                << "function: " << name << ", arguments size should be even number.";
135
2.78k
    }
136
137
2.78k
    static DataTypePtr get_return_type_impl(const DataTypes& arguments) {
138
2.78k
        DataTypes data_types(arguments.size() / 2);
139
2.78k
        size_t even_idx = 1;
140
8.43k
        for (size_t i = 0; i < data_types.size(); i++) {
141
5.65k
            data_types[i] = arguments[even_idx];
142
5.65k
            even_idx += 2;
143
5.65k
        }
144
        // Preserve value nullability just like struct(); field-name arguments do not affect it.
145
2.78k
        return std::make_shared<DataTypeStruct>(data_types);
146
2.78k
    }
147
};
148
149
8
void register_function_struct(SimpleFunctionFactory& factory) {
150
8
    factory.register_function<FunctionStruct<StructImpl>>();
151
8
    factory.register_function<FunctionStruct<NamedStructImpl>>();
152
8
}
153
154
} // namespace doris