Coverage Report

Created: 2026-03-16 19:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/cast/cast_to_map.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_map.h"
19
#include "core/column/column_nullable.h"
20
#include "core/data_type/data_type_map.h"
21
#include "exprs/function/cast/cast_base.h"
22
23
namespace doris::CastWrapper {
24
#include "common/compile_check_begin.h"
25
//TODO(Amory) . Need support more cast for key , value for map
26
WrapperType create_map_wrapper(FunctionContext* context, const DataTypePtr& from_type,
27
3
                               const DataTypeMap& to_type) {
28
3
    if (is_string_type(from_type->get_primitive_type())) {
29
3
        if (context->enable_strict_mode()) {
30
0
            return cast_from_string_to_complex_type_strict_mode;
31
3
        } else {
32
3
            return cast_from_string_to_complex_type;
33
3
        }
34
3
    }
35
0
    const auto* from = check_and_get_data_type<DataTypeMap>(from_type.get());
36
0
    if (!from) {
37
0
        return CastWrapper::create_unsupport_wrapper(
38
0
                fmt::format("CAST AS Map can only be performed between Map types or from "
39
0
                            "String. from type: {}, to type: {}",
40
0
                            from_type->get_name(), to_type.get_name()));
41
0
    }
42
0
    DataTypes from_kv_types;
43
0
    DataTypes to_kv_types;
44
0
    from_kv_types.reserve(2);
45
0
    to_kv_types.reserve(2);
46
0
    from_kv_types.push_back(from->get_key_type());
47
0
    from_kv_types.push_back(from->get_value_type());
48
0
    to_kv_types.push_back(to_type.get_key_type());
49
0
    to_kv_types.push_back(to_type.get_value_type());
50
51
0
    auto kv_wrappers = get_element_wrappers(context, from_kv_types, to_kv_types);
52
0
    return [kv_wrappers, from_kv_types, to_kv_types](
53
0
                   FunctionContext* context, Block& block, const ColumnNumbers& arguments,
54
0
                   uint32_t result, size_t /*input_rows_count*/,
55
0
                   const NullMap::value_type* null_map = nullptr) -> Status {
56
0
        auto& from_column = block.get_by_position(arguments.front()).column;
57
0
        const auto* from_col_map = check_and_get_column<ColumnMap>(from_column.get());
58
0
        if (!from_col_map) {
59
0
            return Status::RuntimeError("Illegal column {} for function CAST AS MAP",
60
0
                                        from_column->get_name());
61
0
        }
62
63
0
        Columns converted_columns(2);
64
0
        ColumnsWithTypeAndName columnsWithTypeAndName(2);
65
0
        columnsWithTypeAndName[0] = {from_col_map->get_keys_ptr(), from_kv_types[0], ""};
66
0
        columnsWithTypeAndName[1] = {from_col_map->get_values_ptr(), from_kv_types[1], ""};
67
68
0
        for (size_t i = 0; i < 2; ++i) {
69
0
            ColumnNumbers element_arguments {block.columns()};
70
0
            block.insert(columnsWithTypeAndName[i]);
71
0
            auto element_result = block.columns();
72
0
            block.insert({to_kv_types[i], ""});
73
0
            RETURN_IF_ERROR(kv_wrappers[i](context, block, element_arguments, element_result,
74
0
                                           columnsWithTypeAndName[i].column->size(), null_map));
75
0
            converted_columns[i] = block.get_by_position(element_result).column;
76
0
        }
77
78
0
        auto map_column = ColumnMap::create(converted_columns[0], converted_columns[1],
79
0
                                            from_col_map->get_offsets_ptr());
80
0
        static_cast<void>(assert_cast<ColumnMap&>(*map_column).deduplicate_keys());
81
0
        block.get_by_position(result).column = std::move(map_column);
82
0
        return Status::OK();
83
0
    };
84
0
}
85
#include "common/compile_check_end.h"
86
} // namespace doris::CastWrapper