Coverage Report

Created: 2026-04-24 20:42

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 "core/column/column_struct.h"
19
#include "core/data_type/data_type_struct.h"
20
#include "core/types.h"
21
#include "exprs/function/cast/cast_base.h"
22
23
namespace doris::CastWrapper {
24
// check struct value type and get to_type value
25
// TODO: need handle another type to cast struct
26
WrapperType create_struct_wrapper(FunctionContext* context, const DataTypePtr& from_type,
27
2
                                  const DataTypeStruct& to_type) {
28
    // support CAST AS Struct from string
29
2
    if (is_string_type(from_type->get_primitive_type())) {
30
2
        if (context->enable_strict_mode()) {
31
0
            return cast_from_string_to_complex_type_strict_mode;
32
2
        } else {
33
2
            return cast_from_string_to_complex_type;
34
2
        }
35
2
    }
36
37
    // only support CAST AS Struct from struct or string types
38
0
    const auto* from = check_and_get_data_type<DataTypeStruct>(from_type.get());
39
0
    if (!from) {
40
0
        return CastWrapper::create_unsupport_wrapper(
41
0
                fmt::format("CAST AS Struct can only be performed between struct types or from "
42
0
                            "String. Left type: {}, right type: {}",
43
0
                            from_type->get_name(), to_type.get_name()));
44
0
    }
45
46
0
    const auto& from_element_types = from->get_elements();
47
0
    const auto& to_element_types = to_type.get_elements();
48
    // only support CAST AS Struct from struct type with same number of elements
49
0
    if (from_element_types.size() != to_element_types.size()) {
50
0
        return CastWrapper::create_unsupport_wrapper(
51
0
                fmt::format("CAST AS Struct can only be performed between struct types with "
52
0
                            "the same number of elements. Left type: {}, right type: {}",
53
0
                            from_type->get_name(), to_type.get_name()));
54
0
    }
55
56
0
    auto element_wrappers = get_element_wrappers(context, from_element_types, to_element_types);
57
0
    return [element_wrappers, from_element_types, to_element_types](
58
0
                   FunctionContext* context, Block& block, const ColumnNumbers& arguments,
59
0
                   uint32_t result, size_t /*input_rows_count*/,
60
0
                   const NullMap::value_type* null_map = nullptr) -> Status {
61
0
        auto& from_column = block.get_by_position(arguments.front()).column;
62
0
        const auto* from_col_struct = check_and_get_column<ColumnStruct>(from_column.get());
63
0
        if (!from_col_struct) {
64
0
            return Status::RuntimeError("Illegal column {} for function CAST AS Struct",
65
0
                                        from_column->get_name());
66
0
        }
67
68
0
        size_t elements_num = to_element_types.size();
69
0
        Columns converted_columns(elements_num);
70
0
        for (size_t i = 0; i < elements_num; ++i) {
71
0
            ColumnWithTypeAndName from_element_column {from_col_struct->get_column_ptr(i),
72
0
                                                       from_element_types[i], ""};
73
0
            ColumnNumbers element_arguments {block.columns()};
74
0
            block.insert(from_element_column);
75
76
0
            auto element_result = block.columns();
77
0
            block.insert({to_element_types[i], ""});
78
79
0
            RETURN_IF_ERROR(element_wrappers[i](context, block, element_arguments, element_result,
80
0
                                                from_col_struct->get_column(i).size(), null_map));
81
0
            converted_columns[i] = block.get_by_position(element_result).column;
82
0
        }
83
84
0
        block.get_by_position(result).column = ColumnStruct::create(converted_columns);
85
0
        return Status::OK();
86
0
    };
87
0
}
88
} // namespace doris::CastWrapper