Coverage Report

Created: 2026-03-12 02:33

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