Coverage Report

Created: 2026-06-27 16:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/cast/cast_to_variant.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
#pragma once
19
20
#include "core/column/column_nullable.h"
21
#include "core/data_type/data_type_variant.h"
22
#include "exprs/function/cast/cast_base.h"
23
#include "exprs/function/cast/cast_to_string.h"
24
25
namespace doris::CastWrapper {
26
27
// shared implementation for casting from variant to arbitrary non-nullable target type
28
inline Status cast_from_variant_impl(FunctionContext* context, Block& block,
29
                                     const ColumnNumbers& arguments, uint32_t result,
30
                                     size_t input_rows_count, const NullMap::value_type* null_map,
31
23
                                     const DataTypePtr& data_type_to) {
32
23
    auto& col_with_type_and_name = block.get_by_position(arguments[0]);
33
23
    auto& col_from = col_with_type_and_name.column;
34
23
    const IColumn* variant_column = col_from.get();
35
23
    const auto* nullable = check_and_get_column<ColumnNullable>(*variant_column);
36
23
    if (nullable != nullptr) {
37
0
        variant_column = &nullable->get_nested_column();
38
0
    }
39
23
    const auto* variant = assert_cast<const ColumnVariant*>(variant_column);
40
23
    ColumnPtr col_to = data_type_to->create_column();
41
42
23
    ColumnPtr finalized_input_column;
43
23
    if (!variant->is_finalized()) {
44
        // Local exchange can share the same input block across multiple downstream tasks.
45
        // Finalize a private copy so variant casts never mutate shared input columns.
46
3
        auto finalized_variant = variant->clone_finalized();
47
3
        variant = assert_cast<const ColumnVariant*>(finalized_variant.get());
48
3
        if (nullable != nullptr) {
49
0
            finalized_input_column = ColumnNullable::create(std::move(finalized_variant),
50
0
                                                            nullable->get_null_map_column_ptr());
51
3
        } else {
52
3
            finalized_input_column = std::move(finalized_variant);
53
3
        }
54
3
    }
55
23
    auto execute_on_finalized_input = [&](auto&& executor) -> Status {
56
4
        if (!finalized_input_column) {
57
3
            return executor(block);
58
3
        }
59
1
        Block finalized_block = block;
60
1
        finalized_block.replace_by_position(arguments[0], finalized_input_column);
61
1
        RETURN_IF_ERROR(executor(finalized_block));
62
1
        block.replace_by_position(result, finalized_block.get_by_position(result).column);
63
1
        return Status::OK();
64
1
    };
_ZZN5doris11CastWrapper22cast_from_variant_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKhRKSt10shared_ptrIKNS_9IDataTypeEEENKUlOT_E_clIZNS0_22cast_from_variant_implES2_S4_S9_jmSB_SH_EUlS4_E_EENS_6StatusESJ_
Line
Count
Source
55
4
    auto execute_on_finalized_input = [&](auto&& executor) -> Status {
56
4
        if (!finalized_input_column) {
57
3
            return executor(block);
58
3
        }
59
1
        Block finalized_block = block;
60
1
        finalized_block.replace_by_position(arguments[0], finalized_input_column);
61
1
        RETURN_IF_ERROR(executor(finalized_block));
62
1
        block.replace_by_position(result, finalized_block.get_by_position(result).column);
63
1
        return Status::OK();
64
1
    };
Unexecuted instantiation: _ZZN5doris11CastWrapper22cast_from_variant_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKhRKSt10shared_ptrIKNS_9IDataTypeEEENKUlOT_E_clIZNS0_22cast_from_variant_implES2_S4_S9_jmSB_SH_EUlS4_E0_EENS_6StatusESJ_
65
66
    // It's important to convert as many elements as possible in this context. For instance,
67
    // if the root of this variant column is a number column, converting it to a number column
68
    // is acceptable. However, if the destination type is a string and root is none scalar root, then
69
    // we should convert the entire tree to a string.
70
23
    bool is_root_valuable = variant->is_scalar_variant() ||
71
23
                            (!variant->is_null_root() &&
72
10
                             variant->get_root_type()->get_primitive_type() != INVALID_TYPE &&
73
10
                             !is_string_type(data_type_to->get_primitive_type()) &&
74
10
                             data_type_to->get_primitive_type() != TYPE_JSONB);
75
76
23
    if (is_root_valuable) {
77
14
        ColumnPtr nested = variant->get_root();
78
14
        auto nested_from_type = variant->get_root_type();
79
        // DCHECK(nested_from_type->is_nullable());
80
14
        DCHECK(!data_type_to->is_nullable());
81
14
        auto new_context = context == nullptr ? nullptr : context->clone();
82
14
        if (new_context != nullptr) {
83
14
            new_context->set_jsonb_string_as_string(true);
84
            // Disable strict mode for the inner JSONB→target conversion.
85
            // The variant root column may contain null/empty JSONB entries for rows
86
            // where the subcolumn doesn't exist (e.g., mixed-schema variant data).
87
            // In strict mode (INSERT context), these null entries cause the ENTIRE
88
            // cast to fail and return all NULLs. Since this is an internal type
89
            // conversion within variant, not user-provided INSERT data validation,
90
            // strict mode should not apply here.
91
14
            new_context->set_enable_strict_mode(false);
92
14
        }
93
        // dst type nullable has been removed, so we should remove the inner nullable of root column
94
14
        auto wrapper =
95
14
                prepare_impl(new_context.get(), remove_nullable(nested_from_type), data_type_to);
96
14
        Block tmp_block {{remove_nullable(nested), remove_nullable(nested_from_type), ""}};
97
14
        tmp_block.insert({nullptr, data_type_to, ""});
98
        /// Perform the requested conversion.
99
14
        Status st = wrapper(new_context.get(), tmp_block, {0}, 1, input_rows_count, nullptr);
100
14
        if (!st.ok()) {
101
            // Fill with default values, which is null
102
0
            col_to->assert_mutable()->insert_many_defaults(input_rows_count);
103
0
            col_to = make_nullable(col_to, true);
104
14
        } else {
105
14
            col_to = tmp_block.get_by_position(1).column;
106
14
            col_to = wrap_in_nullable(col_to,
107
14
                                      Block({{nested, nested_from_type, ""},
108
14
                                             {col_from, col_with_type_and_name.type, ""},
109
14
                                             {col_to, data_type_to, ""}}),
110
14
                                      {0, 1}, input_rows_count);
111
14
        }
112
14
    } else {
113
9
        if (variant->only_have_default_values()) {
114
2
            col_to->assert_mutable()->insert_many_defaults(input_rows_count);
115
2
            col_to = make_nullable(col_to, true);
116
7
        } else if (is_string_type(data_type_to->get_primitive_type())) {
117
            // serialize to string
118
4
            return execute_on_finalized_input([&](Block& finalized_block) {
119
4
                return CastToStringFunction::execute_impl(context, finalized_block, arguments,
120
4
                                                          result, input_rows_count);
121
4
            });
122
4
        } else if (data_type_to->get_primitive_type() == TYPE_JSONB) {
123
            // serialize to json by parsing
124
0
            return execute_on_finalized_input([&](Block& finalized_block) {
125
0
                return cast_from_generic_to_jsonb(context, finalized_block, arguments, result,
126
0
                                                  input_rows_count);
127
0
            });
128
3
        } else if (!data_type_to->is_nullable() &&
129
3
                   !is_string_type(data_type_to->get_primitive_type())) {
130
            // other types
131
3
            col_to->assert_mutable()->insert_many_defaults(input_rows_count);
132
3
            col_to = make_nullable(col_to, true);
133
3
        } else {
134
0
            assert_cast<ColumnNullable&>(*col_to->assert_mutable())
135
0
                    .insert_many_defaults(input_rows_count);
136
0
        }
137
9
    }
138
139
19
    if (null_map == nullptr) {
140
9
        if (const auto* nullable_result = check_and_get_column<ColumnNullable>(*col_to);
141
9
            nullable_result != nullptr && !nullable_result->has_null()) {
142
3
            col_to = nullable_result->get_nested_column_ptr();
143
3
        }
144
9
    }
145
146
19
    if (col_to->size() != input_rows_count) {
147
0
        return Status::InternalError("Unmatched row count {}, expected {}", col_to->size(),
148
0
                                     input_rows_count);
149
0
    }
150
151
19
    block.replace_by_position(result, std::move(col_to));
152
19
    return Status::OK();
153
19
}
154
155
struct CastFromVariant {
156
    static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
157
                          uint32_t result, size_t input_rows_count,
158
0
                          const NullMap::value_type* null_map = nullptr) {
159
0
        auto& data_type_to = block.get_by_position(result).type;
160
0
        return cast_from_variant_impl(context, block, arguments, result, input_rows_count, null_map,
161
0
                                      data_type_to);
162
0
    }
163
};
164
165
struct CastToVariant {
166
    static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
167
                          uint32_t result, size_t input_rows_count,
168
3
                          const NullMap::value_type* null_map = nullptr) {
169
        // auto& data_type_to = block.get_by_position(result).type;
170
3
        const auto& col_with_type_and_name = block.get_by_position(arguments[0]);
171
3
        const auto& from_type = col_with_type_and_name.type;
172
3
        const auto& col_from = col_with_type_and_name.column;
173
        // set variant root column/type to from column/type
174
3
        const auto& data_type_to = block.get_by_position(result).type;
175
3
        const auto* variant_type =
176
3
                typeid_cast<const DataTypeVariant*>(remove_nullable(data_type_to).get());
177
3
        auto variant = ColumnVariant::create(
178
3
                variant_type ? variant_type->variant_max_subcolumns_count() : 0,
179
3
                variant_type ? variant_type->enable_doc_mode() : false);
180
3
        variant->create_root(from_type, IColumn::mutate(col_from));
181
3
        block.replace_by_position(result, std::move(variant));
182
3
        return Status::OK();
183
3
    }
184
};
185
186
// create corresponding variant value to wrap from_type
187
WrapperType create_cast_to_variant_wrapper(const DataTypePtr& from_type,
188
3
                                           const DataTypeVariant& to_type) {
189
3
    if (from_type->get_primitive_type() == TYPE_VARIANT) {
190
        // variant_max_subcolumns_count is not equal
191
0
        return create_unsupport_wrapper(from_type->get_name(), to_type.get_name());
192
0
    }
193
3
    return &CastToVariant::execute;
194
3
}
195
196
// create corresponding type convert from variant
197
WrapperType create_cast_from_variant_wrapper(const DataTypeVariant& from_type,
198
23
                                             const DataTypePtr& to_type) {
199
23
    if (to_type->get_primitive_type() == TYPE_VARIANT) {
200
        // variant_max_subcolumns_count is not equal
201
0
        return create_unsupport_wrapper(from_type.get_name(), to_type->get_name());
202
0
    }
203
    // Capture explicit target type to make the cast independent from Block[result].type.
204
23
    DataTypePtr captured_to_type = to_type;
205
23
    return [captured_to_type](FunctionContext* context, Block& block,
206
23
                              const ColumnNumbers& arguments, uint32_t result,
207
23
                              size_t input_rows_count,
208
23
                              const NullMap::value_type* null_map) -> Status {
209
23
        return cast_from_variant_impl(context, block, arguments, result, input_rows_count, null_map,
210
23
                                      captured_to_type);
211
23
    };
212
23
}
213
214
} // namespace doris::CastWrapper