Coverage Report

Created: 2026-09-16 02:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/cast/cast_to_struct.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
18
#include <vector>
19
20
#include "core/column/column_struct.h"
21
#include "core/data_type/data_type_struct.h"
22
#include "core/types.h"
23
#include "exprs/function/cast/cast_base.h"
24
25
namespace doris::CastWrapper {
26
// check struct value type and get to_type value
27
// TODO: need handle another type to cast struct
28
WrapperType create_struct_wrapper(FunctionContext* context, const DataTypePtr& from_type,
29
16
                                  const DataTypeStruct& to_type) {
30
    // support CAST AS Struct from string
31
16
    if (is_string_type(from_type->get_primitive_type())) {
32
2
        if (context->enable_strict_mode()) {
33
0
            return cast_from_string_to_complex_type_strict_mode;
34
2
        } else {
35
2
            return cast_from_string_to_complex_type;
36
2
        }
37
2
    }
38
39
    // only support CAST AS Struct from struct or string types
40
14
    const auto* from = check_and_get_data_type<DataTypeStruct>(from_type.get());
41
14
    if (!from) {
42
0
        return CastWrapper::create_unsupport_wrapper(
43
0
                fmt::format("CAST AS Struct can only be performed between struct types or from "
44
0
                            "String. Left type: {}, right type: {}",
45
0
                            from_type->get_name(), to_type.get_name()));
46
0
    }
47
48
14
    const auto& from_element_types = from->get_elements();
49
14
    const auto& to_element_types = to_type.get_elements();
50
    // only support CAST AS Struct from struct type with same number of elements
51
14
    if (from_element_types.size() != to_element_types.size()) {
52
0
        return CastWrapper::create_unsupport_wrapper(
53
0
                fmt::format("CAST AS Struct can only be performed between struct types with "
54
0
                            "the same number of elements. Left type: {}, right type: {}",
55
0
                            from_type->get_name(), to_type.get_name()));
56
0
    }
57
58
14
    auto element_wrappers = get_element_wrappers(context, from_element_types, to_element_types);
59
    /// A field whose type does not change is passed through, so it needs neither a child mask nor a
60
    /// call into its (identity) wrapper.
61
14
    std::vector<bool> unchanged_fields;
62
14
    unchanged_fields.reserve(from_element_types.size());
63
29
    for (size_t i = 0; i < from_element_types.size(); ++i) {
64
15
        unchanged_fields.push_back(from_element_types[i]->equals(*to_element_types[i]));
65
15
    }
66
14
    return [element_wrappers, from_element_types, to_element_types,
67
14
            unchanged_fields = std::move(unchanged_fields)](
68
14
                   FunctionContext* context, Block& block, const ColumnNumbers& arguments,
69
14
                   uint32_t result, size_t /*input_rows_count*/,
70
14
                   const NullMap::value_type* null_map = nullptr) -> Status {
71
14
        auto& from_column = block.get_by_position(arguments.front()).column;
72
14
        const auto* from_col_struct = check_and_get_column<ColumnStruct>(from_column.get());
73
14
        if (!from_col_struct) {
74
0
            return Status::RuntimeError("Illegal column {} for function CAST AS Struct",
75
0
                                        from_column->get_name());
76
0
        }
77
78
14
        size_t elements_num = to_element_types.size();
79
14
        Columns converted_columns(elements_num);
80
        // Fields share the rows of their parent, so the parent mask is scanned once for all of them.
81
14
        const NullMap::value_type* inherited_null_map =
82
14
                (null_map != nullptr && has_masked_row(null_map, from_col_struct->size()))
83
14
                        ? null_map
84
14
                        : nullptr;
85
24
        for (size_t i = 0; i < elements_num; ++i) {
86
15
            if (unchanged_fields[i]) {
87
                /// The field is not converted, so it cannot validate the hidden payload of a NULL
88
                /// row, and the NULL of the row hides the field anyway.
89
1
                converted_columns[i] = from_col_struct->get_column_ptr(i);
90
1
                continue;
91
1
            }
92
            /// A field of a row that is NULL is a hidden payload as well. Fields share the rows of
93
            /// their parent, so the mask of the parent can be used as is.
94
14
            auto child_mask = build_child_null_mask(inherited_null_map, nullptr,
95
14
                                                    from_col_struct->get_column_ptr(i));
96
14
            ColumnWithTypeAndName from_element_column {child_mask.column, from_element_types[i],
97
14
                                                       ""};
98
14
            ColumnNumbers element_arguments {block.columns()};
99
14
            block.insert(from_element_column);
100
101
14
            auto element_result = block.columns();
102
14
            block.insert({to_element_types[i], ""});
103
104
14
            RETURN_IF_ERROR(element_wrappers[i](context, block, element_arguments, element_result,
105
14
                                                child_mask.column->size(), child_mask.null_map));
106
9
            converted_columns[i] = block.get_by_position(element_result).column;
107
9
        }
108
109
9
        block.get_by_position(result).column = ColumnStruct::create(converted_columns);
110
9
        return Status::OK();
111
14
    };
112
14
}
113
} // namespace doris::CastWrapper