Coverage Report

Created: 2026-05-25 21:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/cast/cast_to_ip.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_ipv4.h"
22
#include "core/data_type/primitive_type.h"
23
#include "exprs/function/cast/cast_base.h"
24
25
namespace doris {
26
27
struct CastToIPv4 {
28
    static bool from_string(const StringRef& from, IPv4& to, CastParameters&);
29
};
30
31
85.3k
inline bool CastToIPv4::from_string(const StringRef& from, IPv4& to, CastParameters&) {
32
85.3k
    return IPv4Value::from_string(to, from.data, from.size);
33
85.3k
}
34
35
struct CastToIPv6 {
36
    static bool from_string(const StringRef& from, IPv6& to, CastParameters&);
37
    static bool from_ipv4(const IPv4& from, IPv6& to, CastParameters&);
38
};
39
40
61.2k
inline bool CastToIPv6::from_string(const StringRef& from, IPv6& to, CastParameters&) {
41
61.2k
    return IPv6Value::from_string(to, from.data, from.size);
42
61.2k
}
43
44
4
inline bool CastToIPv6::from_ipv4(const IPv4& from, IPv6& to, CastParameters&) {
45
4
    map_ipv4_to_ipv6(from, reinterpret_cast<UInt8*>(&to));
46
4
    return true;
47
4
}
48
49
template <CastModeType Mode, typename IpDataType>
50
    requires(IsIPType<IpDataType>)
51
class CastToImpl<Mode, DataTypeString, IpDataType> : public CastToBase {
52
public:
53
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
54
                        uint32_t result, size_t /*input_rows_count*/,
55
16
                        const NullMap::value_type* null_map = nullptr) const override {
56
16
        const auto* col_from = assert_cast<const DataTypeString::ColumnType*>(
57
16
                block.get_by_position(arguments[0]).column.get());
58
59
16
        auto to_type = block.get_by_position(result).type;
60
16
        auto nested_to_type = remove_nullable(to_type);
61
16
        auto serde = nested_to_type->get_serde();
62
63
16
        if constexpr (Mode == CastModeType::NonStrictMode) {
64
4
            auto nullable_col_to = create_empty_nullable_column(nested_to_type);
65
            // may write nulls to nullable_col_to
66
4
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
67
4
            block.get_by_position(result).column = std::move(nullable_col_to);
68
12
        } else if constexpr (Mode == CastModeType::StrictMode) {
69
12
            MutableColumnPtr column_to = nested_to_type->create_column();
70
            // WON'T write nulls to the result column, just raise errors. null_map is only used to skip invalid rows
71
12
            RETURN_IF_ERROR(
72
12
                    serde->from_string_strict_mode_batch(*col_from, *column_to, {}, null_map));
73
2
            block.get_by_position(result).column = std::move(column_to);
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
0
        return Status::OK();
79
16
    }
_ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_14DataTypeStringENS_12DataTypeIPv4EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
55
5
                        const NullMap::value_type* null_map = nullptr) const override {
56
5
        const auto* col_from = assert_cast<const DataTypeString::ColumnType*>(
57
5
                block.get_by_position(arguments[0]).column.get());
58
59
5
        auto to_type = block.get_by_position(result).type;
60
5
        auto nested_to_type = remove_nullable(to_type);
61
5
        auto serde = nested_to_type->get_serde();
62
63
        if constexpr (Mode == CastModeType::NonStrictMode) {
64
            auto nullable_col_to = create_empty_nullable_column(nested_to_type);
65
            // may write nulls to nullable_col_to
66
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
67
            block.get_by_position(result).column = std::move(nullable_col_to);
68
5
        } else if constexpr (Mode == CastModeType::StrictMode) {
69
5
            MutableColumnPtr column_to = nested_to_type->create_column();
70
            // WON'T write nulls to the result column, just raise errors. null_map is only used to skip invalid rows
71
5
            RETURN_IF_ERROR(
72
5
                    serde->from_string_strict_mode_batch(*col_from, *column_to, {}, null_map));
73
1
            block.get_by_position(result).column = std::move(column_to);
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
0
        return Status::OK();
79
5
    }
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_14DataTypeStringENS_12DataTypeIPv4EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
55
2
                        const NullMap::value_type* null_map = nullptr) const override {
56
2
        const auto* col_from = assert_cast<const DataTypeString::ColumnType*>(
57
2
                block.get_by_position(arguments[0]).column.get());
58
59
2
        auto to_type = block.get_by_position(result).type;
60
2
        auto nested_to_type = remove_nullable(to_type);
61
2
        auto serde = nested_to_type->get_serde();
62
63
2
        if constexpr (Mode == CastModeType::NonStrictMode) {
64
2
            auto nullable_col_to = create_empty_nullable_column(nested_to_type);
65
            // may write nulls to nullable_col_to
66
2
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
67
2
            block.get_by_position(result).column = std::move(nullable_col_to);
68
        } else if constexpr (Mode == CastModeType::StrictMode) {
69
            MutableColumnPtr column_to = nested_to_type->create_column();
70
            // WON'T write nulls to the result column, just raise errors. null_map is only used to skip invalid rows
71
            RETURN_IF_ERROR(
72
                    serde->from_string_strict_mode_batch(*col_from, *column_to, {}, null_map));
73
            block.get_by_position(result).column = std::move(column_to);
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
0
        return Status::OK();
79
2
    }
_ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_14DataTypeStringENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
55
7
                        const NullMap::value_type* null_map = nullptr) const override {
56
7
        const auto* col_from = assert_cast<const DataTypeString::ColumnType*>(
57
7
                block.get_by_position(arguments[0]).column.get());
58
59
7
        auto to_type = block.get_by_position(result).type;
60
7
        auto nested_to_type = remove_nullable(to_type);
61
7
        auto serde = nested_to_type->get_serde();
62
63
        if constexpr (Mode == CastModeType::NonStrictMode) {
64
            auto nullable_col_to = create_empty_nullable_column(nested_to_type);
65
            // may write nulls to nullable_col_to
66
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
67
            block.get_by_position(result).column = std::move(nullable_col_to);
68
7
        } else if constexpr (Mode == CastModeType::StrictMode) {
69
7
            MutableColumnPtr column_to = nested_to_type->create_column();
70
            // WON'T write nulls to the result column, just raise errors. null_map is only used to skip invalid rows
71
7
            RETURN_IF_ERROR(
72
7
                    serde->from_string_strict_mode_batch(*col_from, *column_to, {}, null_map));
73
1
            block.get_by_position(result).column = std::move(column_to);
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
0
        return Status::OK();
79
7
    }
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_14DataTypeStringENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
55
2
                        const NullMap::value_type* null_map = nullptr) const override {
56
2
        const auto* col_from = assert_cast<const DataTypeString::ColumnType*>(
57
2
                block.get_by_position(arguments[0]).column.get());
58
59
2
        auto to_type = block.get_by_position(result).type;
60
2
        auto nested_to_type = remove_nullable(to_type);
61
2
        auto serde = nested_to_type->get_serde();
62
63
2
        if constexpr (Mode == CastModeType::NonStrictMode) {
64
2
            auto nullable_col_to = create_empty_nullable_column(nested_to_type);
65
            // may write nulls to nullable_col_to
66
2
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
67
2
            block.get_by_position(result).column = std::move(nullable_col_to);
68
        } else if constexpr (Mode == CastModeType::StrictMode) {
69
            MutableColumnPtr column_to = nested_to_type->create_column();
70
            // WON'T write nulls to the result column, just raise errors. null_map is only used to skip invalid rows
71
            RETURN_IF_ERROR(
72
                    serde->from_string_strict_mode_batch(*col_from, *column_to, {}, null_map));
73
            block.get_by_position(result).column = std::move(column_to);
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
0
        return Status::OK();
79
2
    }
80
};
81
82
template <CastModeType AllMode>
83
class CastToImpl<AllMode, DataTypeIPv4, DataTypeIPv6> : public CastToBase {
84
public:
85
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
86
                        uint32_t result, size_t input_rows_count,
87
1
                        const NullMap::value_type* null_map = nullptr) const override {
88
1
        const auto* col_from = assert_cast<const DataTypeIPv4::ColumnType*>(
89
1
                block.get_by_position(arguments[0]).column.get());
90
1
        const auto size = col_from->size();
91
1
        auto col_to = DataTypeIPv6::ColumnType::create(size);
92
1
        auto& to_data = col_to->get_data();
93
1
        const auto& from_data = col_from->get_data();
94
1
        CastParameters params;
95
1
        params.is_strict = (AllMode == CastModeType::StrictMode);
96
97
5
        for (size_t i = 0; i < size; ++i) {
98
4
            CastToIPv6::from_ipv4(from_data[i], to_data[i], params);
99
4
        }
100
101
1
        block.get_by_position(result).column = std::move(col_to);
102
1
        return Status::OK();
103
1
    }
Unexecuted instantiation: _ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_12DataTypeIPv4ENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_12DataTypeIPv4ENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
87
1
                        const NullMap::value_type* null_map = nullptr) const override {
88
1
        const auto* col_from = assert_cast<const DataTypeIPv4::ColumnType*>(
89
1
                block.get_by_position(arguments[0]).column.get());
90
1
        const auto size = col_from->size();
91
1
        auto col_to = DataTypeIPv6::ColumnType::create(size);
92
1
        auto& to_data = col_to->get_data();
93
1
        const auto& from_data = col_from->get_data();
94
1
        CastParameters params;
95
1
        params.is_strict = (AllMode == CastModeType::StrictMode);
96
97
5
        for (size_t i = 0; i < size; ++i) {
98
4
            CastToIPv6::from_ipv4(from_data[i], to_data[i], params);
99
4
        }
100
101
1
        block.get_by_position(result).column = std::move(col_to);
102
1
        return Status::OK();
103
1
    }
104
};
105
} // namespace doris