Coverage Report

Created: 2026-04-17 21:24

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
83.0k
inline bool CastToIPv4::from_string(const StringRef& from, IPv4& to, CastParameters&) {
32
83.0k
    return IPv4Value::from_string(to, from.data, from.size);
33
83.0k
}
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
59.7k
inline bool CastToIPv6::from_string(const StringRef& from, IPv6& to, CastParameters&) {
41
59.7k
    return IPv6Value::from_string(to, from.data, from.size);
42
59.7k
}
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 = check_and_get_column<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 serde = remove_nullable(to_type)->get_serde();
61
62
        // by default framework, to_type is already unwrapped nullable
63
16
        MutableColumnPtr column_to = to_type->create_column();
64
16
        ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create(
65
16
                std::move(column_to), ColumnUInt8::create(input_rows_count, 0));
66
67
16
        if constexpr (Mode == CastModeType::NonStrictMode) {
68
            // may write nulls to nullable_col_to
69
4
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
70
12
        } else if constexpr (Mode == CastModeType::StrictMode) {
71
            // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows
72
12
            RETURN_IF_ERROR(serde->from_string_strict_mode_batch(
73
12
                    *col_from, nullable_col_to->get_nested_column(), {}, null_map));
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
6
        block.get_by_position(result).column = std::move(nullable_col_to);
79
16
        return Status::OK();
80
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 = check_and_get_column<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 serde = remove_nullable(to_type)->get_serde();
61
62
        // by default framework, to_type is already unwrapped nullable
63
5
        MutableColumnPtr column_to = to_type->create_column();
64
5
        ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create(
65
5
                std::move(column_to), ColumnUInt8::create(input_rows_count, 0));
66
67
        if constexpr (Mode == CastModeType::NonStrictMode) {
68
            // may write nulls to nullable_col_to
69
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
70
5
        } else if constexpr (Mode == CastModeType::StrictMode) {
71
            // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows
72
5
            RETURN_IF_ERROR(serde->from_string_strict_mode_batch(
73
5
                    *col_from, nullable_col_to->get_nested_column(), {}, null_map));
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
1
        block.get_by_position(result).column = std::move(nullable_col_to);
79
5
        return Status::OK();
80
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 = check_and_get_column<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 serde = remove_nullable(to_type)->get_serde();
61
62
        // by default framework, to_type is already unwrapped nullable
63
2
        MutableColumnPtr column_to = to_type->create_column();
64
2
        ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create(
65
2
                std::move(column_to), ColumnUInt8::create(input_rows_count, 0));
66
67
2
        if constexpr (Mode == CastModeType::NonStrictMode) {
68
            // may write nulls to nullable_col_to
69
2
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
70
        } else if constexpr (Mode == CastModeType::StrictMode) {
71
            // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows
72
            RETURN_IF_ERROR(serde->from_string_strict_mode_batch(
73
                    *col_from, nullable_col_to->get_nested_column(), {}, null_map));
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
2
        block.get_by_position(result).column = std::move(nullable_col_to);
79
2
        return Status::OK();
80
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 = check_and_get_column<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 serde = remove_nullable(to_type)->get_serde();
61
62
        // by default framework, to_type is already unwrapped nullable
63
7
        MutableColumnPtr column_to = to_type->create_column();
64
7
        ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create(
65
7
                std::move(column_to), ColumnUInt8::create(input_rows_count, 0));
66
67
        if constexpr (Mode == CastModeType::NonStrictMode) {
68
            // may write nulls to nullable_col_to
69
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
70
7
        } else if constexpr (Mode == CastModeType::StrictMode) {
71
            // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows
72
7
            RETURN_IF_ERROR(serde->from_string_strict_mode_batch(
73
7
                    *col_from, nullable_col_to->get_nested_column(), {}, null_map));
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
1
        block.get_by_position(result).column = std::move(nullable_col_to);
79
7
        return Status::OK();
80
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 = check_and_get_column<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 serde = remove_nullable(to_type)->get_serde();
61
62
        // by default framework, to_type is already unwrapped nullable
63
2
        MutableColumnPtr column_to = to_type->create_column();
64
2
        ColumnNullable::MutablePtr nullable_col_to = ColumnNullable::create(
65
2
                std::move(column_to), ColumnUInt8::create(input_rows_count, 0));
66
67
2
        if constexpr (Mode == CastModeType::NonStrictMode) {
68
            // may write nulls to nullable_col_to
69
2
            RETURN_IF_ERROR(serde->from_string_batch(*col_from, *nullable_col_to, {}));
70
        } else if constexpr (Mode == CastModeType::StrictMode) {
71
            // WON'T write nulls to nullable_col_to, just raise errors. null_map is only used to skip invalid rows
72
            RETURN_IF_ERROR(serde->from_string_strict_mode_batch(
73
                    *col_from, nullable_col_to->get_nested_column(), {}, null_map));
74
        } else {
75
            return Status::InternalError("Unsupported cast mode");
76
        }
77
78
2
        block.get_by_position(result).column = std::move(nullable_col_to);
79
2
        return Status::OK();
80
2
    }
81
};
82
83
template <CastModeType AllMode>
84
class CastToImpl<AllMode, DataTypeIPv4, DataTypeIPv6> : public CastToBase {
85
public:
86
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
87
                        uint32_t result, size_t input_rows_count,
88
1
                        const NullMap::value_type* null_map = nullptr) const override {
89
1
        const auto* col_from = check_and_get_column<DataTypeIPv4::ColumnType>(
90
1
                block.get_by_position(arguments[0]).column.get());
91
1
        const auto size = col_from->size();
92
1
        auto col_to = DataTypeIPv6::ColumnType::create(size);
93
1
        auto& to_data = col_to->get_data();
94
1
        const auto& from_data = col_from->get_data();
95
1
        CastParameters params;
96
1
        params.is_strict = (AllMode == CastModeType::StrictMode);
97
98
5
        for (size_t i = 0; i < size; ++i) {
99
4
            CastToIPv6::from_ipv4(from_data[i], to_data[i], params);
100
4
        }
101
102
1
        block.get_by_position(result).column = std::move(col_to);
103
1
        return Status::OK();
104
1
    }
Unexecuted instantiation: _ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_12DataTypeIPv4ENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_12DataTypeIPv4ENS_12DataTypeIPv6EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
88
1
                        const NullMap::value_type* null_map = nullptr) const override {
89
1
        const auto* col_from = check_and_get_column<DataTypeIPv4::ColumnType>(
90
1
                block.get_by_position(arguments[0]).column.get());
91
1
        const auto size = col_from->size();
92
1
        auto col_to = DataTypeIPv6::ColumnType::create(size);
93
1
        auto& to_data = col_to->get_data();
94
1
        const auto& from_data = col_from->get_data();
95
1
        CastParameters params;
96
1
        params.is_strict = (AllMode == CastModeType::StrictMode);
97
98
5
        for (size_t i = 0; i < size; ++i) {
99
4
            CastToIPv6::from_ipv4(from_data[i], to_data[i], params);
100
4
        }
101
102
1
        block.get_by_position(result).column = std::move(col_to);
103
1
        return Status::OK();
104
1
    }
105
};
106
} // namespace doris