Coverage Report

Created: 2026-08-23 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_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
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/FunctionsCodingIP.cpp
19
// and modified by Doris
20
21
#pragma once
22
#include <glog/logging.h>
23
24
#include <cstddef>
25
#include <memory>
26
27
#include "common/cast_set.h"
28
#include "core/assert_cast.h"
29
#include "core/block/column_with_type_and_name.h"
30
#include "core/column/column.h"
31
#include "core/column/column_const.h"
32
#include "core/column/column_nullable.h"
33
#include "core/column/column_string.h"
34
#include "core/column/column_struct.h"
35
#include "core/column/column_vector.h"
36
#include "core/data_type/data_type.h"
37
#include "core/data_type/data_type_ipv4.h"
38
#include "core/data_type/data_type_ipv6.h"
39
#include "core/data_type/data_type_nullable.h"
40
#include "core/data_type/data_type_number.h"
41
#include "core/data_type/data_type_string.h"
42
#include "core/data_type/data_type_struct.h"
43
#include "core/field.h"
44
#include "core/types.h"
45
#include "core/value/ip_address_cidr.h"
46
#include "exec/common/endian.h"
47
#include "exec/common/format_ip.h"
48
#include "exec/common/ipv6_to_binary.h"
49
#include "exprs/function/function.h"
50
#include "exprs/function/function_helpers.h"
51
#include "storage/index/index_reader_helper.h"
52
#include "storage/index/inverted/inverted_index_iterator.h"
53
54
namespace doris {
55
56
class FunctionIPv4NumToString : public IFunction {
57
private:
58
    template <PrimitiveType ArgPType>
59
32
    Status execute_type(Block& block, const ColumnWithTypeAndName& argument, size_t result) const {
60
32
        using ColumnType = ColumnVector<ArgPType>;
61
32
        const ColumnPtr& column = argument.column;
62
63
32
        const auto* col = assert_cast<const ColumnType*>(column.get());
64
32
        const typename ColumnType::Container& vec_in = col->get_data();
65
32
        auto col_res = ColumnString::create();
66
67
32
        ColumnString::Chars& vec_res = col_res->get_chars();
68
32
        ColumnString::Offsets& offsets_res = col_res->get_offsets();
69
70
32
        vec_res.resize(vec_in.size() *
71
32
                       (IPV4_MAX_TEXT_LENGTH + 1)); /// the longest value is: 255.255.255.255\0
72
32
        offsets_res.resize(vec_in.size());
73
32
        char* begin = reinterpret_cast<char*>(vec_res.data());
74
32
        char* pos = begin;
75
76
32
        auto null_map = ColumnUInt8::create(vec_in.size(), 0);
77
32
        size_t src_size =
78
32
                std::min(sizeof(typename PrimitiveTypeTraits<ArgPType>::CppType), (unsigned long)4);
79
446
        for (size_t i = 0; i < vec_in.size(); ++i) {
80
414
            auto value = vec_in[i];
81
414
            if (value < IPV4_MIN_NUM_VALUE || value > IPV4_MAX_NUM_VALUE) {
82
6
                null_map->get_data()[i] = 1;
83
408
            } else {
84
408
                format_ipv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), src_size, pos);
85
408
            }
86
414
            offsets_res[i] = cast_set<uint32_t>(pos - begin);
87
414
        }
88
89
32
        vec_res.resize(pos - begin);
90
32
        block.replace_by_position(result,
91
32
                                  ColumnNullable::create(std::move(col_res), std::move(null_map)));
92
32
        return Status::OK();
93
32
    }
_ZNK5doris23FunctionIPv4NumToString12execute_typeILNS_13PrimitiveTypeE3EEENS_6StatusERNS_5BlockERKNS_21ColumnWithTypeAndNameEm
Line
Count
Source
59
5
    Status execute_type(Block& block, const ColumnWithTypeAndName& argument, size_t result) const {
60
5
        using ColumnType = ColumnVector<ArgPType>;
61
5
        const ColumnPtr& column = argument.column;
62
63
5
        const auto* col = assert_cast<const ColumnType*>(column.get());
64
5
        const typename ColumnType::Container& vec_in = col->get_data();
65
5
        auto col_res = ColumnString::create();
66
67
5
        ColumnString::Chars& vec_res = col_res->get_chars();
68
5
        ColumnString::Offsets& offsets_res = col_res->get_offsets();
69
70
5
        vec_res.resize(vec_in.size() *
71
5
                       (IPV4_MAX_TEXT_LENGTH + 1)); /// the longest value is: 255.255.255.255\0
72
5
        offsets_res.resize(vec_in.size());
73
5
        char* begin = reinterpret_cast<char*>(vec_res.data());
74
5
        char* pos = begin;
75
76
5
        auto null_map = ColumnUInt8::create(vec_in.size(), 0);
77
5
        size_t src_size =
78
5
                std::min(sizeof(typename PrimitiveTypeTraits<ArgPType>::CppType), (unsigned long)4);
79
10
        for (size_t i = 0; i < vec_in.size(); ++i) {
80
5
            auto value = vec_in[i];
81
5
            if (value < IPV4_MIN_NUM_VALUE || value > IPV4_MAX_NUM_VALUE) {
82
3
                null_map->get_data()[i] = 1;
83
3
            } else {
84
2
                format_ipv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), src_size, pos);
85
2
            }
86
5
            offsets_res[i] = cast_set<uint32_t>(pos - begin);
87
5
        }
88
89
5
        vec_res.resize(pos - begin);
90
5
        block.replace_by_position(result,
91
5
                                  ColumnNullable::create(std::move(col_res), std::move(null_map)));
92
5
        return Status::OK();
93
5
    }
Unexecuted instantiation: _ZNK5doris23FunctionIPv4NumToString12execute_typeILNS_13PrimitiveTypeE4EEENS_6StatusERNS_5BlockERKNS_21ColumnWithTypeAndNameEm
_ZNK5doris23FunctionIPv4NumToString12execute_typeILNS_13PrimitiveTypeE5EEENS_6StatusERNS_5BlockERKNS_21ColumnWithTypeAndNameEm
Line
Count
Source
59
4
    Status execute_type(Block& block, const ColumnWithTypeAndName& argument, size_t result) const {
60
4
        using ColumnType = ColumnVector<ArgPType>;
61
4
        const ColumnPtr& column = argument.column;
62
63
4
        const auto* col = assert_cast<const ColumnType*>(column.get());
64
4
        const typename ColumnType::Container& vec_in = col->get_data();
65
4
        auto col_res = ColumnString::create();
66
67
4
        ColumnString::Chars& vec_res = col_res->get_chars();
68
4
        ColumnString::Offsets& offsets_res = col_res->get_offsets();
69
70
4
        vec_res.resize(vec_in.size() *
71
4
                       (IPV4_MAX_TEXT_LENGTH + 1)); /// the longest value is: 255.255.255.255\0
72
4
        offsets_res.resize(vec_in.size());
73
4
        char* begin = reinterpret_cast<char*>(vec_res.data());
74
4
        char* pos = begin;
75
76
4
        auto null_map = ColumnUInt8::create(vec_in.size(), 0);
77
4
        size_t src_size =
78
4
                std::min(sizeof(typename PrimitiveTypeTraits<ArgPType>::CppType), (unsigned long)4);
79
8
        for (size_t i = 0; i < vec_in.size(); ++i) {
80
4
            auto value = vec_in[i];
81
4
            if (value < IPV4_MIN_NUM_VALUE || value > IPV4_MAX_NUM_VALUE) {
82
0
                null_map->get_data()[i] = 1;
83
4
            } else {
84
4
                format_ipv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), src_size, pos);
85
4
            }
86
4
            offsets_res[i] = cast_set<uint32_t>(pos - begin);
87
4
        }
88
89
4
        vec_res.resize(pos - begin);
90
4
        block.replace_by_position(result,
91
4
                                  ColumnNullable::create(std::move(col_res), std::move(null_map)));
92
4
        return Status::OK();
93
4
    }
_ZNK5doris23FunctionIPv4NumToString12execute_typeILNS_13PrimitiveTypeE6EEENS_6StatusERNS_5BlockERKNS_21ColumnWithTypeAndNameEm
Line
Count
Source
59
23
    Status execute_type(Block& block, const ColumnWithTypeAndName& argument, size_t result) const {
60
23
        using ColumnType = ColumnVector<ArgPType>;
61
23
        const ColumnPtr& column = argument.column;
62
63
23
        const auto* col = assert_cast<const ColumnType*>(column.get());
64
23
        const typename ColumnType::Container& vec_in = col->get_data();
65
23
        auto col_res = ColumnString::create();
66
67
23
        ColumnString::Chars& vec_res = col_res->get_chars();
68
23
        ColumnString::Offsets& offsets_res = col_res->get_offsets();
69
70
23
        vec_res.resize(vec_in.size() *
71
23
                       (IPV4_MAX_TEXT_LENGTH + 1)); /// the longest value is: 255.255.255.255\0
72
23
        offsets_res.resize(vec_in.size());
73
23
        char* begin = reinterpret_cast<char*>(vec_res.data());
74
23
        char* pos = begin;
75
76
23
        auto null_map = ColumnUInt8::create(vec_in.size(), 0);
77
23
        size_t src_size =
78
23
                std::min(sizeof(typename PrimitiveTypeTraits<ArgPType>::CppType), (unsigned long)4);
79
428
        for (size_t i = 0; i < vec_in.size(); ++i) {
80
405
            auto value = vec_in[i];
81
405
            if (value < IPV4_MIN_NUM_VALUE || value > IPV4_MAX_NUM_VALUE) {
82
3
                null_map->get_data()[i] = 1;
83
402
            } else {
84
402
                format_ipv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), src_size, pos);
85
402
            }
86
405
            offsets_res[i] = cast_set<uint32_t>(pos - begin);
87
405
        }
88
89
23
        vec_res.resize(pos - begin);
90
23
        block.replace_by_position(result,
91
23
                                  ColumnNullable::create(std::move(col_res), std::move(null_map)));
92
23
        return Status::OK();
93
23
    }
94
95
public:
96
    static constexpr auto name = "ipv4_num_to_string";
97
33
    static FunctionPtr create() { return std::make_shared<FunctionIPv4NumToString>(); }
98
99
1
    String get_name() const override { return name; }
100
101
24
    size_t get_number_of_arguments() const override { return 1; }
102
103
24
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
104
24
        return make_nullable(std::make_shared<DataTypeString>());
105
24
    }
106
107
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
108
32
                        uint32_t result, size_t input_rows_count) const override {
109
32
        ColumnWithTypeAndName& argument = block.get_by_position(arguments[0]);
110
111
32
        switch (argument.type->get_primitive_type()) {
112
5
        case PrimitiveType::TYPE_TINYINT:
113
5
            return execute_type<TYPE_TINYINT>(block, argument, result);
114
0
            break;
115
0
        case PrimitiveType::TYPE_SMALLINT:
116
0
            return execute_type<TYPE_SMALLINT>(block, argument, result);
117
0
            break;
118
4
        case PrimitiveType::TYPE_INT:
119
4
            return execute_type<TYPE_INT>(block, argument, result);
120
0
            break;
121
23
        case PrimitiveType::TYPE_BIGINT:
122
23
            return execute_type<TYPE_BIGINT>(block, argument, result);
123
0
            break;
124
0
        default:
125
0
            break;
126
32
        }
127
128
0
        return Status::InternalError(
129
0
                "Illegal column {} of argument of function {}, expected Int8 or Int16 or Int32 or "
130
0
                "Int64",
131
0
                argument.name, get_name());
132
32
    }
133
};
134
135
/// Since IPExceptionMode means wider scope, we use more specific name here.
136
enum class IPConvertExceptionMode : uint8_t { Throw, Default, Null };
137
138
3.46k
static inline bool try_parse_ipv4(const char* pos, Int64& result_value) {
139
3.46k
    return parse_ipv4_whole(pos, reinterpret_cast<unsigned char*>(&result_value));
140
3.46k
}
141
142
template <IPConvertExceptionMode exception_mode, typename ToColumn>
143
117
ColumnPtr convert_to_ipv4(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
144
117
    const auto* column_string = assert_cast<const ColumnString*>(column.get());
145
146
117
    size_t column_size = column_string->size();
147
148
117
    ColumnUInt8::MutablePtr col_null_map_to;
149
117
    ColumnUInt8::Container* vec_null_map_to = nullptr;
150
151
117
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
152
48
        col_null_map_to = ColumnUInt8::create(column_size, false);
153
48
        vec_null_map_to = &col_null_map_to->get_data();
154
48
    }
155
156
117
    auto col_res = ToColumn::create(column_size, 0);
157
117
    auto& vec_res = col_res->get_data();
158
159
117
    const ColumnString::Chars& vec_src = column_string->get_chars();
160
117
    const ColumnString::Offsets& offsets_src = column_string->get_offsets();
161
117
    size_t prev_offset = 0;
162
163
1.71k
    for (size_t i = 0; i < vec_res.size(); ++i) {
164
1.59k
        if (null_map && (*null_map)[i]) {
165
18
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
166
2
                throw Exception(
167
2
                        ErrorCode::INVALID_ARGUMENT,
168
2
                        "Null Input, you may consider convert it to a valid default IPv4 value "
169
2
                        "like '0.0.0.0' first");
170
2
            }
171
0
            vec_res[i] = 0;
172
18
            prev_offset = offsets_src[i];
173
18
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
174
5
                (*vec_null_map_to)[i] = true;
175
5
            }
176
18
            continue;
177
18
        }
178
1.57k
        const char* src_start = reinterpret_cast<const char*>(&vec_src[prev_offset]);
179
1.57k
        size_t src_length = (i < vec_res.size() - 1) ? (offsets_src[i] - prev_offset)
180
1.57k
                                                     : (vec_src.size() - prev_offset);
181
1.57k
        std::string src(src_start, src_length);
182
1.57k
        bool parse_result = try_parse_ipv4(src.c_str(), vec_res[i]);
183
184
1.57k
        if (!parse_result) {
185
524
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
186
8
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value");
187
257
            } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
188
257
                vec_res[i] = 0;
189
259
            } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
190
259
                (*vec_null_map_to)[i] = true;
191
259
                vec_res[i] = 0;
192
259
            }
193
524
        }
194
195
0
        prev_offset = offsets_src[i];
196
1.57k
    }
197
198
117
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
199
48
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
200
48
    }
201
0
    return col_res;
202
117
}
_ZN5doris15convert_to_ipv4ILNS_22IPConvertExceptionModeE0ENS_12ColumnVectorILNS_13PrimitiveTypeE6EEEEENS_3COWINS_7IColumnEE13immutable_ptrIS6_EES9_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
143
19
ColumnPtr convert_to_ipv4(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
144
19
    const auto* column_string = assert_cast<const ColumnString*>(column.get());
145
146
19
    size_t column_size = column_string->size();
147
148
19
    ColumnUInt8::MutablePtr col_null_map_to;
149
19
    ColumnUInt8::Container* vec_null_map_to = nullptr;
150
151
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
152
        col_null_map_to = ColumnUInt8::create(column_size, false);
153
        vec_null_map_to = &col_null_map_to->get_data();
154
    }
155
156
19
    auto col_res = ToColumn::create(column_size, 0);
157
19
    auto& vec_res = col_res->get_data();
158
159
19
    const ColumnString::Chars& vec_src = column_string->get_chars();
160
19
    const ColumnString::Offsets& offsets_src = column_string->get_offsets();
161
19
    size_t prev_offset = 0;
162
163
189
    for (size_t i = 0; i < vec_res.size(); ++i) {
164
170
        if (null_map && (*null_map)[i]) {
165
2
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
166
2
                throw Exception(
167
2
                        ErrorCode::INVALID_ARGUMENT,
168
2
                        "Null Input, you may consider convert it to a valid default IPv4 value "
169
2
                        "like '0.0.0.0' first");
170
2
            }
171
0
            vec_res[i] = 0;
172
2
            prev_offset = offsets_src[i];
173
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
174
                (*vec_null_map_to)[i] = true;
175
            }
176
2
            continue;
177
2
        }
178
168
        const char* src_start = reinterpret_cast<const char*>(&vec_src[prev_offset]);
179
168
        size_t src_length = (i < vec_res.size() - 1) ? (offsets_src[i] - prev_offset)
180
168
                                                     : (vec_src.size() - prev_offset);
181
168
        std::string src(src_start, src_length);
182
168
        bool parse_result = try_parse_ipv4(src.c_str(), vec_res[i]);
183
184
168
        if (!parse_result) {
185
8
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
186
8
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value");
187
            } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
188
                vec_res[i] = 0;
189
            } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
190
                (*vec_null_map_to)[i] = true;
191
                vec_res[i] = 0;
192
            }
193
8
        }
194
195
0
        prev_offset = offsets_src[i];
196
168
    }
197
198
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
199
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
200
    }
201
19
    return col_res;
202
19
}
_ZN5doris15convert_to_ipv4ILNS_22IPConvertExceptionModeE1ENS_12ColumnVectorILNS_13PrimitiveTypeE6EEEEENS_3COWINS_7IColumnEE13immutable_ptrIS6_EES9_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
143
50
ColumnPtr convert_to_ipv4(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
144
50
    const auto* column_string = assert_cast<const ColumnString*>(column.get());
145
146
50
    size_t column_size = column_string->size();
147
148
50
    ColumnUInt8::MutablePtr col_null_map_to;
149
50
    ColumnUInt8::Container* vec_null_map_to = nullptr;
150
151
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
152
        col_null_map_to = ColumnUInt8::create(column_size, false);
153
        vec_null_map_to = &col_null_map_to->get_data();
154
    }
155
156
50
    auto col_res = ToColumn::create(column_size, 0);
157
50
    auto& vec_res = col_res->get_data();
158
159
50
    const ColumnString::Chars& vec_src = column_string->get_chars();
160
50
    const ColumnString::Offsets& offsets_src = column_string->get_offsets();
161
50
    size_t prev_offset = 0;
162
163
954
    for (size_t i = 0; i < vec_res.size(); ++i) {
164
904
        if (null_map && (*null_map)[i]) {
165
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
166
                throw Exception(
167
                        ErrorCode::INVALID_ARGUMENT,
168
                        "Null Input, you may consider convert it to a valid default IPv4 value "
169
                        "like '0.0.0.0' first");
170
            }
171
11
            vec_res[i] = 0;
172
11
            prev_offset = offsets_src[i];
173
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
174
                (*vec_null_map_to)[i] = true;
175
            }
176
11
            continue;
177
11
        }
178
893
        const char* src_start = reinterpret_cast<const char*>(&vec_src[prev_offset]);
179
893
        size_t src_length = (i < vec_res.size() - 1) ? (offsets_src[i] - prev_offset)
180
893
                                                     : (vec_src.size() - prev_offset);
181
893
        std::string src(src_start, src_length);
182
893
        bool parse_result = try_parse_ipv4(src.c_str(), vec_res[i]);
183
184
893
        if (!parse_result) {
185
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
186
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value");
187
257
            } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
188
257
                vec_res[i] = 0;
189
            } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
190
                (*vec_null_map_to)[i] = true;
191
                vec_res[i] = 0;
192
            }
193
257
        }
194
195
893
        prev_offset = offsets_src[i];
196
893
    }
197
198
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
199
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
200
    }
201
50
    return col_res;
202
50
}
_ZN5doris15convert_to_ipv4ILNS_22IPConvertExceptionModeE2ENS_12ColumnVectorILNS_13PrimitiveTypeE6EEEEENS_3COWINS_7IColumnEE13immutable_ptrIS6_EES9_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
143
48
ColumnPtr convert_to_ipv4(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
144
48
    const auto* column_string = assert_cast<const ColumnString*>(column.get());
145
146
48
    size_t column_size = column_string->size();
147
148
48
    ColumnUInt8::MutablePtr col_null_map_to;
149
48
    ColumnUInt8::Container* vec_null_map_to = nullptr;
150
151
48
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
152
48
        col_null_map_to = ColumnUInt8::create(column_size, false);
153
48
        vec_null_map_to = &col_null_map_to->get_data();
154
48
    }
155
156
48
    auto col_res = ToColumn::create(column_size, 0);
157
48
    auto& vec_res = col_res->get_data();
158
159
48
    const ColumnString::Chars& vec_src = column_string->get_chars();
160
48
    const ColumnString::Offsets& offsets_src = column_string->get_offsets();
161
48
    size_t prev_offset = 0;
162
163
568
    for (size_t i = 0; i < vec_res.size(); ++i) {
164
520
        if (null_map && (*null_map)[i]) {
165
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
166
                throw Exception(
167
                        ErrorCode::INVALID_ARGUMENT,
168
                        "Null Input, you may consider convert it to a valid default IPv4 value "
169
                        "like '0.0.0.0' first");
170
            }
171
5
            vec_res[i] = 0;
172
5
            prev_offset = offsets_src[i];
173
5
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
174
5
                (*vec_null_map_to)[i] = true;
175
5
            }
176
5
            continue;
177
5
        }
178
515
        const char* src_start = reinterpret_cast<const char*>(&vec_src[prev_offset]);
179
515
        size_t src_length = (i < vec_res.size() - 1) ? (offsets_src[i] - prev_offset)
180
515
                                                     : (vec_src.size() - prev_offset);
181
515
        std::string src(src_start, src_length);
182
515
        bool parse_result = try_parse_ipv4(src.c_str(), vec_res[i]);
183
184
515
        if (!parse_result) {
185
            if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
186
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value");
187
            } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
188
                vec_res[i] = 0;
189
259
            } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
190
259
                (*vec_null_map_to)[i] = true;
191
259
                vec_res[i] = 0;
192
259
            }
193
259
        }
194
195
515
        prev_offset = offsets_src[i];
196
515
    }
197
198
48
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
199
48
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
200
48
    }
201
0
    return col_res;
202
48
}
203
204
template <IPConvertExceptionMode exception_mode>
205
class FunctionIPv4StringToNum : public IFunction {
206
public:
207
    static constexpr auto name = exception_mode == IPConvertExceptionMode::Throw
208
                                         ? "ipv4_string_to_num"
209
                                         : (exception_mode == IPConvertExceptionMode::Default
210
                                                    ? "ipv4_string_to_num_or_default"
211
                                                    : "ipv4_string_to_num_or_null");
212
213
70
    static FunctionPtr create() {
214
70
        return std::make_shared<FunctionIPv4StringToNum<exception_mode>>();
215
70
    }
_ZN5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE0EE6createEv
Line
Count
Source
213
16
    static FunctionPtr create() {
214
16
        return std::make_shared<FunctionIPv4StringToNum<exception_mode>>();
215
16
    }
_ZN5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE1EE6createEv
Line
Count
Source
213
26
    static FunctionPtr create() {
214
26
        return std::make_shared<FunctionIPv4StringToNum<exception_mode>>();
215
26
    }
_ZN5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE2EE6createEv
Line
Count
Source
213
28
    static FunctionPtr create() {
214
28
        return std::make_shared<FunctionIPv4StringToNum<exception_mode>>();
215
28
    }
216
217
3
    String get_name() const override { return name; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE0EE8get_nameB5cxx11Ev
Line
Count
Source
217
1
    String get_name() const override { return name; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE1EE8get_nameB5cxx11Ev
Line
Count
Source
217
1
    String get_name() const override { return name; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE2EE8get_nameB5cxx11Ev
Line
Count
Source
217
1
    String get_name() const override { return name; }
218
219
43
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE0EE23get_number_of_argumentsEv
Line
Count
Source
219
7
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE1EE23get_number_of_argumentsEv
Line
Count
Source
219
17
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE2EE23get_number_of_argumentsEv
Line
Count
Source
219
19
    size_t get_number_of_arguments() const override { return 1; }
220
221
43
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
222
43
        auto result_type = std::make_shared<DataTypeInt64>();
223
224
43
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
225
19
            return make_nullable(result_type);
226
19
        }
227
228
0
        return result_type;
229
43
    }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE0EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
221
7
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
222
7
        auto result_type = std::make_shared<DataTypeInt64>();
223
224
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
225
            return make_nullable(result_type);
226
        }
227
228
7
        return result_type;
229
7
    }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE1EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
221
17
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
222
17
        auto result_type = std::make_shared<DataTypeInt64>();
223
224
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
225
            return make_nullable(result_type);
226
        }
227
228
17
        return result_type;
229
17
    }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE2EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
221
19
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
222
19
        auto result_type = std::make_shared<DataTypeInt64>();
223
224
19
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
225
19
            return make_nullable(result_type);
226
19
        }
227
228
0
        return result_type;
229
19
    }
230
231
160
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE0EE36use_default_implementation_for_nullsEv
Line
Count
Source
231
26
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE1EE36use_default_implementation_for_nullsEv
Line
Count
Source
231
67
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE2EE36use_default_implementation_for_nullsEv
Line
Count
Source
231
67
    bool use_default_implementation_for_nulls() const override { return false; }
232
233
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
234
115
                        uint32_t result, size_t input_rows_count) const override {
235
115
        ColumnPtr column =
236
115
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
237
115
        ColumnPtr null_map_column;
238
115
        const NullMap* null_map = nullptr;
239
115
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
240
80
            column = column_nullable->get_nested_column_ptr();
241
80
            null_map_column = column_nullable->get_null_map_column_ptr();
242
80
            null_map = &column_nullable->get_null_map_data();
243
80
        }
244
245
115
        auto col_res = convert_to_ipv4<exception_mode, ColumnInt64>(column, null_map);
246
247
115
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
248
36
            block.replace_by_position(
249
36
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
250
79
        } else {
251
79
            block.replace_by_position(result, std::move(col_res));
252
79
        }
253
115
        return Status::OK();
254
115
    }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE0EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
234
19
                        uint32_t result, size_t input_rows_count) const override {
235
19
        ColumnPtr column =
236
19
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
237
19
        ColumnPtr null_map_column;
238
19
        const NullMap* null_map = nullptr;
239
19
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
240
13
            column = column_nullable->get_nested_column_ptr();
241
13
            null_map_column = column_nullable->get_null_map_column_ptr();
242
13
            null_map = &column_nullable->get_null_map_data();
243
13
        }
244
245
19
        auto col_res = convert_to_ipv4<exception_mode, ColumnInt64>(column, null_map);
246
247
19
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
248
0
            block.replace_by_position(
249
0
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
250
19
        } else {
251
19
            block.replace_by_position(result, std::move(col_res));
252
19
        }
253
19
        return Status::OK();
254
19
    }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE1EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
234
49
                        uint32_t result, size_t input_rows_count) const override {
235
49
        ColumnPtr column =
236
49
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
237
49
        ColumnPtr null_map_column;
238
49
        const NullMap* null_map = nullptr;
239
49
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
240
32
            column = column_nullable->get_nested_column_ptr();
241
32
            null_map_column = column_nullable->get_null_map_column_ptr();
242
32
            null_map = &column_nullable->get_null_map_data();
243
32
        }
244
245
49
        auto col_res = convert_to_ipv4<exception_mode, ColumnInt64>(column, null_map);
246
247
49
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
248
0
            block.replace_by_position(
249
0
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
250
49
        } else {
251
49
            block.replace_by_position(result, std::move(col_res));
252
49
        }
253
49
        return Status::OK();
254
49
    }
_ZNK5doris23FunctionIPv4StringToNumILNS_22IPConvertExceptionModeE2EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
234
47
                        uint32_t result, size_t input_rows_count) const override {
235
47
        ColumnPtr column =
236
47
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
237
47
        ColumnPtr null_map_column;
238
47
        const NullMap* null_map = nullptr;
239
47
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
240
35
            column = column_nullable->get_nested_column_ptr();
241
35
            null_map_column = column_nullable->get_null_map_column_ptr();
242
35
            null_map = &column_nullable->get_null_map_data();
243
35
        }
244
245
47
        auto col_res = convert_to_ipv4<exception_mode, ColumnInt64>(column, null_map);
246
247
47
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
248
36
            block.replace_by_position(
249
36
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
250
36
        } else {
251
11
            block.replace_by_position(result, std::move(col_res));
252
11
        }
253
47
        return Status::OK();
254
47
    }
255
};
256
257
template <typename T>
258
void process_ipv6_column(const ColumnPtr& column, size_t input_rows_count,
259
                         ColumnString::Chars& vec_res, ColumnString::Offsets& offsets_res,
260
94
                         ColumnUInt8::MutablePtr& null_map, unsigned char* ipv6_address_data) {
261
94
    auto* begin = reinterpret_cast<char*>(vec_res.data());
262
94
    auto* pos = begin;
263
264
94
    const auto* col = assert_cast<const T*>(column.get());
265
266
979
    for (size_t i = 0; i < input_rows_count; ++i) {
267
885
        bool is_empty = false;
268
269
885
        if constexpr (std::is_same_v<T, ColumnIPv6>) {
270
26
            const auto& vec_in = col->get_data();
271
26
            memcpy(ipv6_address_data, reinterpret_cast<const unsigned char*>(&vec_in[i]),
272
26
                   IPV6_BINARY_LENGTH);
273
859
        } else { // ColumnString
274
859
            const auto str_ref = col->get_data_at(i);
275
859
            const char* value = str_ref.data;
276
859
            size_t value_size = str_ref.size;
277
278
859
            if (value_size > IPV6_BINARY_LENGTH || value == nullptr || value_size == 0) {
279
276
                is_empty = true;
280
583
            } else {
281
583
                memcpy(ipv6_address_data, value, value_size);
282
583
                memset(ipv6_address_data + value_size, 0, IPV6_BINARY_LENGTH - value_size);
283
583
            }
284
859
        }
285
286
885
        if (is_empty) {
287
276
            null_map->get_data()[i] = 1;
288
609
        } else {
289
609
            if constexpr (std::is_same_v<T, ColumnIPv6>) {
290
                // ipv6 is little-endian byte order storage in doris
291
                // so parsing ipv6 in little-endian byte order
292
26
                format_ipv6(ipv6_address_data, pos);
293
583
            } else {
294
                // 16 bytes ipv6 string is big-endian byte order storage in doris
295
                // so transfer to little-endian firstly
296
583
                std::reverse(ipv6_address_data, ipv6_address_data + IPV6_BINARY_LENGTH);
297
583
                format_ipv6(ipv6_address_data, pos);
298
583
            }
299
609
        }
300
885
        offsets_res[i] = cast_set<uint32_t>(pos - begin);
301
885
    }
302
94
}
_ZN5doris19process_ipv6_columnINS_12ColumnVectorILNS_13PrimitiveTypeE37EEEEEvRKNS_3COWINS_7IColumnEE13immutable_ptrIS5_EEmRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSB_IjLm4096ESE_Lm16ELm15EEERNS6_11mutable_ptrINS1_ILS2_2EEEEEPh
Line
Count
Source
260
10
                         ColumnUInt8::MutablePtr& null_map, unsigned char* ipv6_address_data) {
261
10
    auto* begin = reinterpret_cast<char*>(vec_res.data());
262
10
    auto* pos = begin;
263
264
10
    const auto* col = assert_cast<const T*>(column.get());
265
266
36
    for (size_t i = 0; i < input_rows_count; ++i) {
267
26
        bool is_empty = false;
268
269
26
        if constexpr (std::is_same_v<T, ColumnIPv6>) {
270
26
            const auto& vec_in = col->get_data();
271
26
            memcpy(ipv6_address_data, reinterpret_cast<const unsigned char*>(&vec_in[i]),
272
26
                   IPV6_BINARY_LENGTH);
273
        } else { // ColumnString
274
            const auto str_ref = col->get_data_at(i);
275
            const char* value = str_ref.data;
276
            size_t value_size = str_ref.size;
277
278
            if (value_size > IPV6_BINARY_LENGTH || value == nullptr || value_size == 0) {
279
                is_empty = true;
280
            } else {
281
                memcpy(ipv6_address_data, value, value_size);
282
                memset(ipv6_address_data + value_size, 0, IPV6_BINARY_LENGTH - value_size);
283
            }
284
        }
285
286
26
        if (is_empty) {
287
0
            null_map->get_data()[i] = 1;
288
26
        } else {
289
26
            if constexpr (std::is_same_v<T, ColumnIPv6>) {
290
                // ipv6 is little-endian byte order storage in doris
291
                // so parsing ipv6 in little-endian byte order
292
26
                format_ipv6(ipv6_address_data, pos);
293
            } else {
294
                // 16 bytes ipv6 string is big-endian byte order storage in doris
295
                // so transfer to little-endian firstly
296
                std::reverse(ipv6_address_data, ipv6_address_data + IPV6_BINARY_LENGTH);
297
                format_ipv6(ipv6_address_data, pos);
298
            }
299
26
        }
300
26
        offsets_res[i] = cast_set<uint32_t>(pos - begin);
301
26
    }
302
10
}
_ZN5doris19process_ipv6_columnINS_9ColumnStrIjEEEEvRKNS_3COWINS_7IColumnEE13immutable_ptrIS4_EEmRNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEERNSA_IjLm4096ESD_Lm16ELm15EEERNS5_11mutable_ptrINS_12ColumnVectorILNS_13PrimitiveTypeE2EEEEEPh
Line
Count
Source
260
84
                         ColumnUInt8::MutablePtr& null_map, unsigned char* ipv6_address_data) {
261
84
    auto* begin = reinterpret_cast<char*>(vec_res.data());
262
84
    auto* pos = begin;
263
264
84
    const auto* col = assert_cast<const T*>(column.get());
265
266
943
    for (size_t i = 0; i < input_rows_count; ++i) {
267
859
        bool is_empty = false;
268
269
        if constexpr (std::is_same_v<T, ColumnIPv6>) {
270
            const auto& vec_in = col->get_data();
271
            memcpy(ipv6_address_data, reinterpret_cast<const unsigned char*>(&vec_in[i]),
272
                   IPV6_BINARY_LENGTH);
273
859
        } else { // ColumnString
274
859
            const auto str_ref = col->get_data_at(i);
275
859
            const char* value = str_ref.data;
276
859
            size_t value_size = str_ref.size;
277
278
859
            if (value_size > IPV6_BINARY_LENGTH || value == nullptr || value_size == 0) {
279
276
                is_empty = true;
280
583
            } else {
281
583
                memcpy(ipv6_address_data, value, value_size);
282
583
                memset(ipv6_address_data + value_size, 0, IPV6_BINARY_LENGTH - value_size);
283
583
            }
284
859
        }
285
286
859
        if (is_empty) {
287
276
            null_map->get_data()[i] = 1;
288
583
        } else {
289
            if constexpr (std::is_same_v<T, ColumnIPv6>) {
290
                // ipv6 is little-endian byte order storage in doris
291
                // so parsing ipv6 in little-endian byte order
292
                format_ipv6(ipv6_address_data, pos);
293
583
            } else {
294
                // 16 bytes ipv6 string is big-endian byte order storage in doris
295
                // so transfer to little-endian firstly
296
583
                std::reverse(ipv6_address_data, ipv6_address_data + IPV6_BINARY_LENGTH);
297
583
                format_ipv6(ipv6_address_data, pos);
298
583
            }
299
583
        }
300
859
        offsets_res[i] = cast_set<uint32_t>(pos - begin);
301
859
    }
302
84
}
303
304
class FunctionIPv6NumToString : public IFunction {
305
public:
306
    static constexpr auto name = "ipv6_num_to_string";
307
69
    static FunctionPtr create() { return std::make_shared<FunctionIPv6NumToString>(); }
308
309
1
    String get_name() const override { return name; }
310
311
60
    size_t get_number_of_arguments() const override { return 1; }
312
313
60
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
314
60
        return make_nullable(std::make_shared<DataTypeString>());
315
60
    }
316
317
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
318
94
                        uint32_t result, size_t input_rows_count) const override {
319
94
        const ColumnPtr& column = block.get_by_position(arguments[0]).column;
320
321
94
        auto col_res = ColumnString::create();
322
94
        ColumnString::Chars& vec_res = col_res->get_chars();
323
94
        ColumnString::Offsets& offsets_res = col_res->get_offsets();
324
94
        vec_res.resize(input_rows_count * (IPV6_MAX_TEXT_LENGTH + 1));
325
94
        offsets_res.resize(input_rows_count);
326
327
94
        auto null_map = ColumnUInt8::create(input_rows_count, 0);
328
329
94
        unsigned char ipv6_address_data[IPV6_BINARY_LENGTH];
330
331
94
        if (check_and_get_column<ColumnIPv6>(column.get())) {
332
10
            process_ipv6_column<ColumnIPv6>(column, input_rows_count, vec_res, offsets_res,
333
10
                                            null_map, ipv6_address_data);
334
84
        } else { //ColumnString
335
84
            process_ipv6_column<ColumnString>(column, input_rows_count, vec_res, offsets_res,
336
84
                                              null_map, ipv6_address_data);
337
84
        }
338
94
        vec_res.resize(offsets_res[offsets_res.size() - 1]);
339
340
94
        block.replace_by_position(result,
341
94
                                  ColumnNullable::create(std::move(col_res), std::move(null_map)));
342
94
        return Status::OK();
343
94
    }
344
};
345
346
namespace detail {
347
template <IPConvertExceptionMode exception_mode, typename ToColumn = ColumnIPv6,
348
          typename StringColumnType>
349
ColumnPtr convert_to_ipv6(const StringColumnType& string_column,
350
202
                          const PaddedPODArray<UInt8>* null_map = nullptr) {
351
202
    const size_t column_size = string_column.size();
352
353
202
    ColumnUInt8::MutablePtr col_null_map_to;
354
202
    ColumnUInt8::Container* vec_null_map_to = nullptr;
355
356
202
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
357
112
        col_null_map_to = ColumnUInt8::create(column_size, false);
358
112
        vec_null_map_to = &col_null_map_to->get_data();
359
112
    }
360
361
202
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
202
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
189
            auto column_string = ColumnString::create();
364
189
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
189
            column_string->get_offsets().reserve(column_size);
366
189
            return column_string;
367
189
        } else {
368
13
            return ColumnIPv6::create();
369
13
        }
370
202
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlmE_clEm
Line
Count
Source
361
35
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
35
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
35
            auto column_string = ColumnString::create();
364
35
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
35
            column_string->get_offsets().reserve(column_size);
366
35
            return column_string;
367
        } else {
368
            return ColumnIPv6::create();
369
        }
370
35
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE1ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlmE_clEm
Line
Count
Source
361
42
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
42
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
42
            auto column_string = ColumnString::create();
364
42
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
42
            column_string->get_offsets().reserve(column_size);
366
42
            return column_string;
367
        } else {
368
            return ColumnIPv6::create();
369
        }
370
42
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE2ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlmE_clEm
Line
Count
Source
361
112
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
112
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
112
            auto column_string = ColumnString::create();
364
112
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
112
            column_string->get_offsets().reserve(column_size);
366
112
            return column_string;
367
        } else {
368
            return ColumnIPv6::create();
369
        }
370
112
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_12ColumnVectorILNS_13PrimitiveTypeE37EEENS_9ColumnStrIjEEEENS_3COWINS_7IColumnEE13immutable_ptrIS9_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlmE_clEm
Line
Count
Source
361
13
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
            auto column_string = ColumnString::create();
364
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
            column_string->get_offsets().reserve(column_size);
366
            return column_string;
367
13
        } else {
368
13
            return ColumnIPv6::create();
369
13
        }
370
13
    };
371
372
202
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
202
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
189
            auto& vec_res = col_res->get_chars();
375
189
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
189
            return (vec_res);
377
189
        } else {
378
13
            auto& vec_res = col_res->get_data();
379
13
            vec_res.resize(col_size);
380
13
            return (vec_res);
381
13
        }
382
202
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlRT_mE_clINS7_11mutable_ptrIS4_EEEEDcSL_m
Line
Count
Source
372
35
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
35
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
35
            auto& vec_res = col_res->get_chars();
375
35
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
35
            return (vec_res);
377
        } else {
378
            auto& vec_res = col_res->get_data();
379
            vec_res.resize(col_size);
380
            return (vec_res);
381
        }
382
35
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE1ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlRT_mE_clINS7_11mutable_ptrIS4_EEEEDcSL_m
Line
Count
Source
372
42
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
42
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
42
            auto& vec_res = col_res->get_chars();
375
42
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
42
            return (vec_res);
377
        } else {
378
            auto& vec_res = col_res->get_data();
379
            vec_res.resize(col_size);
380
            return (vec_res);
381
        }
382
42
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE2ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlRT_mE_clINS7_11mutable_ptrIS4_EEEEDcSL_m
Line
Count
Source
372
112
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
112
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
112
            auto& vec_res = col_res->get_chars();
375
112
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
112
            return (vec_res);
377
        } else {
378
            auto& vec_res = col_res->get_data();
379
            vec_res.resize(col_size);
380
            return (vec_res);
381
        }
382
112
    };
_ZZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_12ColumnVectorILNS_13PrimitiveTypeE37EEENS_9ColumnStrIjEEEENS_3COWINS_7IColumnEE13immutable_ptrIS9_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEENKUlRT_mE_clINSA_11mutable_ptrIS5_EEEEDcSO_m
Line
Count
Source
372
13
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
            auto& vec_res = col_res->get_chars();
375
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
            return (vec_res);
377
13
        } else {
378
13
            auto& vec_res = col_res->get_data();
379
13
            vec_res.resize(col_size);
380
13
            return (vec_res);
381
13
        }
382
13
    };
383
384
202
    auto col_res = column_create(column_size);
385
202
    auto& vec_res = get_vector(col_res, column_size);
386
387
202
    using Chars = typename StringColumnType::Chars;
388
202
    const Chars& vec_src = string_column.get_chars();
389
390
202
    size_t src_offset = 0;
391
392
    /// ColumnString contains not null terminated strings. But functions parseIPv6, parseIPv4 expect null terminated string.
393
    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be parsed in-place
394
202
    std::string string_buffer;
395
396
202
    int offset_inc = 1;
397
202
    ColumnString* column_string = nullptr;
398
202
    if constexpr (std::is_same_v<ToColumn, ColumnString>) {
399
189
        offset_inc = IPV6_BINARY_LENGTH;
400
189
        column_string = col_res.get();
401
189
    }
402
403
2.13k
    for (size_t out_offset = 0, i = 0; i < column_size; out_offset += offset_inc, ++i) {
404
1.94k
        char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = "::ffff:";
405
1.94k
        size_t src_next_offset = src_offset;
406
407
1.94k
        const char* src_value = nullptr;
408
1.94k
        auto* res_value = reinterpret_cast<unsigned char*>(&vec_res[out_offset]);
409
410
1.94k
        if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
411
1.94k
            src_value = reinterpret_cast<const char*>(&vec_src[src_offset]);
412
1.94k
            src_next_offset = string_column.get_offsets()[i];
413
414
1.94k
            string_buffer.assign(src_value, src_next_offset - src_offset);
415
1.94k
            src_value = string_buffer.c_str();
416
1.94k
        }
417
418
1.94k
        if (null_map && (*null_map)[i]) {
419
46
            if (exception_mode == IPConvertExceptionMode::Throw) {
420
4
                throw Exception(
421
4
                        ErrorCode::INVALID_ARGUMENT,
422
4
                        "Null Input, you may consider convert it to a valid default IPv6 value "
423
4
                        "like '::' first");
424
42
            } else if (exception_mode == IPConvertExceptionMode::Default) {
425
24
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
426
24
            } else {
427
18
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
428
18
                (*vec_null_map_to)[i] = true;
429
18
            }
430
42
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
431
42
                DCHECK(column_string != nullptr);
432
42
                column_string->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
433
42
            }
434
42
            src_offset = src_next_offset;
435
42
            continue;
436
46
        }
437
438
1.90k
        bool parse_result = false;
439
1.90k
        Int64 dummy_result = 0;
440
441
        /// For both cases below: In case of failure, the function parseIPv6 fills vec_res with zero bytes.
442
443
        /// If the source IP address is parsable as an IPv4 address, then transform it into a valid IPv6 address.
444
        /// Keeping it simple by just prefixing `::ffff:` to the IPv4 address to represent it as a valid IPv6 address.
445
1.90k
        size_t string_length = src_next_offset - src_offset;
446
1.90k
        if (string_length != 0) {
447
1.89k
            if (try_parse_ipv4(src_value, dummy_result)) {
448
55
                strncat(src_ipv4_buf, src_value, sizeof(src_ipv4_buf) - strlen(src_ipv4_buf) - 1);
449
55
                parse_result = parse_ipv6_whole(src_ipv4_buf, res_value);
450
1.83k
            } else {
451
1.83k
                parse_result = parse_ipv6_whole(src_value, res_value);
452
1.83k
            }
453
1.89k
        }
454
455
1.90k
        if (parse_result && string_length != 0) {
456
1.06k
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
457
                // handling 16 bytes ipv6 string in the big-endian byte order
458
                // is aimed at conforming to human reading habits
459
1.05k
                std::reverse(res_value, res_value + IPV6_BINARY_LENGTH);
460
1.05k
            }
461
1.06k
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
462
1.05k
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
463
1.05k
                std::copy(res_value, res_value + IPV6_BINARY_LENGTH,
464
1.05k
                          column_string_res->get_chars().begin() + i * IPV6_BINARY_LENGTH);
465
1.05k
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
466
1.05k
            } else {
467
14
                col_res->insert_data(reinterpret_cast<const char*>(res_value), IPV6_BINARY_LENGTH);
468
14
            }
469
1.06k
        } else {
470
836
            if (exception_mode == IPConvertExceptionMode::Throw) {
471
12
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value");
472
12
            }
473
824
            std::fill_n(&vec_res[out_offset], offset_inc, 0);
474
824
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
475
824
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
476
824
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
477
824
            }
478
824
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
479
621
                (*vec_null_map_to)[i] = true;
480
621
            }
481
824
        }
482
1.89k
        src_offset = src_next_offset;
483
1.89k
    }
484
485
186
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
486
112
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
487
112
    }
488
0
    return col_res;
489
202
}
_ZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
350
35
                          const PaddedPODArray<UInt8>* null_map = nullptr) {
351
35
    const size_t column_size = string_column.size();
352
353
35
    ColumnUInt8::MutablePtr col_null_map_to;
354
35
    ColumnUInt8::Container* vec_null_map_to = nullptr;
355
356
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
357
        col_null_map_to = ColumnUInt8::create(column_size, false);
358
        vec_null_map_to = &col_null_map_to->get_data();
359
    }
360
361
35
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
35
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
35
            auto column_string = ColumnString::create();
364
35
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
35
            column_string->get_offsets().reserve(column_size);
366
35
            return column_string;
367
35
        } else {
368
35
            return ColumnIPv6::create();
369
35
        }
370
35
    };
371
372
35
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
35
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
35
            auto& vec_res = col_res->get_chars();
375
35
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
35
            return (vec_res);
377
35
        } else {
378
35
            auto& vec_res = col_res->get_data();
379
35
            vec_res.resize(col_size);
380
35
            return (vec_res);
381
35
        }
382
35
    };
383
384
35
    auto col_res = column_create(column_size);
385
35
    auto& vec_res = get_vector(col_res, column_size);
386
387
35
    using Chars = typename StringColumnType::Chars;
388
35
    const Chars& vec_src = string_column.get_chars();
389
390
35
    size_t src_offset = 0;
391
392
    /// ColumnString contains not null terminated strings. But functions parseIPv6, parseIPv4 expect null terminated string.
393
    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be parsed in-place
394
35
    std::string string_buffer;
395
396
35
    int offset_inc = 1;
397
35
    ColumnString* column_string = nullptr;
398
35
    if constexpr (std::is_same_v<ToColumn, ColumnString>) {
399
35
        offset_inc = IPV6_BINARY_LENGTH;
400
35
        column_string = col_res.get();
401
35
    }
402
403
170
    for (size_t out_offset = 0, i = 0; i < column_size; out_offset += offset_inc, ++i) {
404
147
        char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = "::ffff:";
405
147
        size_t src_next_offset = src_offset;
406
407
147
        const char* src_value = nullptr;
408
147
        auto* res_value = reinterpret_cast<unsigned char*>(&vec_res[out_offset]);
409
410
147
        if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
411
147
            src_value = reinterpret_cast<const char*>(&vec_src[src_offset]);
412
147
            src_next_offset = string_column.get_offsets()[i];
413
414
147
            string_buffer.assign(src_value, src_next_offset - src_offset);
415
147
            src_value = string_buffer.c_str();
416
147
        }
417
418
147
        if (null_map && (*null_map)[i]) {
419
4
            if (exception_mode == IPConvertExceptionMode::Throw) {
420
4
                throw Exception(
421
4
                        ErrorCode::INVALID_ARGUMENT,
422
4
                        "Null Input, you may consider convert it to a valid default IPv6 value "
423
4
                        "like '::' first");
424
4
            } else if (exception_mode == IPConvertExceptionMode::Default) {
425
0
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
426
0
            } else {
427
0
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
428
0
                (*vec_null_map_to)[i] = true;
429
0
            }
430
0
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
431
0
                DCHECK(column_string != nullptr);
432
0
                column_string->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
433
0
            }
434
0
            src_offset = src_next_offset;
435
0
            continue;
436
4
        }
437
438
143
        bool parse_result = false;
439
143
        Int64 dummy_result = 0;
440
441
        /// For both cases below: In case of failure, the function parseIPv6 fills vec_res with zero bytes.
442
443
        /// If the source IP address is parsable as an IPv4 address, then transform it into a valid IPv6 address.
444
        /// Keeping it simple by just prefixing `::ffff:` to the IPv4 address to represent it as a valid IPv6 address.
445
143
        size_t string_length = src_next_offset - src_offset;
446
143
        if (string_length != 0) {
447
143
            if (try_parse_ipv4(src_value, dummy_result)) {
448
3
                strncat(src_ipv4_buf, src_value, sizeof(src_ipv4_buf) - strlen(src_ipv4_buf) - 1);
449
3
                parse_result = parse_ipv6_whole(src_ipv4_buf, res_value);
450
140
            } else {
451
140
                parse_result = parse_ipv6_whole(src_value, res_value);
452
140
            }
453
143
        }
454
455
143
        if (parse_result && string_length != 0) {
456
135
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
457
                // handling 16 bytes ipv6 string in the big-endian byte order
458
                // is aimed at conforming to human reading habits
459
135
                std::reverse(res_value, res_value + IPV6_BINARY_LENGTH);
460
135
            }
461
135
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
462
135
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
463
135
                std::copy(res_value, res_value + IPV6_BINARY_LENGTH,
464
135
                          column_string_res->get_chars().begin() + i * IPV6_BINARY_LENGTH);
465
135
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
466
            } else {
467
                col_res->insert_data(reinterpret_cast<const char*>(res_value), IPV6_BINARY_LENGTH);
468
            }
469
135
        } else {
470
8
            if (exception_mode == IPConvertExceptionMode::Throw) {
471
8
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value");
472
8
            }
473
0
            std::fill_n(&vec_res[out_offset], offset_inc, 0);
474
0
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
475
0
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
476
0
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
477
0
            }
478
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
479
                (*vec_null_map_to)[i] = true;
480
            }
481
0
        }
482
135
        src_offset = src_next_offset;
483
135
    }
484
485
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
486
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
487
    }
488
23
    return col_res;
489
35
}
_ZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE1ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
350
42
                          const PaddedPODArray<UInt8>* null_map = nullptr) {
351
42
    const size_t column_size = string_column.size();
352
353
42
    ColumnUInt8::MutablePtr col_null_map_to;
354
42
    ColumnUInt8::Container* vec_null_map_to = nullptr;
355
356
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
357
        col_null_map_to = ColumnUInt8::create(column_size, false);
358
        vec_null_map_to = &col_null_map_to->get_data();
359
    }
360
361
42
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
42
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
42
            auto column_string = ColumnString::create();
364
42
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
42
            column_string->get_offsets().reserve(column_size);
366
42
            return column_string;
367
42
        } else {
368
42
            return ColumnIPv6::create();
369
42
        }
370
42
    };
371
372
42
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
42
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
42
            auto& vec_res = col_res->get_chars();
375
42
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
42
            return (vec_res);
377
42
        } else {
378
42
            auto& vec_res = col_res->get_data();
379
42
            vec_res.resize(col_size);
380
42
            return (vec_res);
381
42
        }
382
42
    };
383
384
42
    auto col_res = column_create(column_size);
385
42
    auto& vec_res = get_vector(col_res, column_size);
386
387
42
    using Chars = typename StringColumnType::Chars;
388
42
    const Chars& vec_src = string_column.get_chars();
389
390
42
    size_t src_offset = 0;
391
392
    /// ColumnString contains not null terminated strings. But functions parseIPv6, parseIPv4 expect null terminated string.
393
    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be parsed in-place
394
42
    std::string string_buffer;
395
396
42
    int offset_inc = 1;
397
42
    ColumnString* column_string = nullptr;
398
42
    if constexpr (std::is_same_v<ToColumn, ColumnString>) {
399
42
        offset_inc = IPV6_BINARY_LENGTH;
400
42
        column_string = col_res.get();
401
42
    }
402
403
848
    for (size_t out_offset = 0, i = 0; i < column_size; out_offset += offset_inc, ++i) {
404
806
        char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = "::ffff:";
405
806
        size_t src_next_offset = src_offset;
406
407
806
        const char* src_value = nullptr;
408
806
        auto* res_value = reinterpret_cast<unsigned char*>(&vec_res[out_offset]);
409
410
806
        if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
411
806
            src_value = reinterpret_cast<const char*>(&vec_src[src_offset]);
412
806
            src_next_offset = string_column.get_offsets()[i];
413
414
806
            string_buffer.assign(src_value, src_next_offset - src_offset);
415
806
            src_value = string_buffer.c_str();
416
806
        }
417
418
806
        if (null_map && (*null_map)[i]) {
419
24
            if (exception_mode == IPConvertExceptionMode::Throw) {
420
0
                throw Exception(
421
0
                        ErrorCode::INVALID_ARGUMENT,
422
0
                        "Null Input, you may consider convert it to a valid default IPv6 value "
423
0
                        "like '::' first");
424
24
            } else if (exception_mode == IPConvertExceptionMode::Default) {
425
24
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
426
24
            } else {
427
0
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
428
0
                (*vec_null_map_to)[i] = true;
429
0
            }
430
24
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
431
24
                DCHECK(column_string != nullptr);
432
24
                column_string->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
433
24
            }
434
24
            src_offset = src_next_offset;
435
24
            continue;
436
24
        }
437
438
782
        bool parse_result = false;
439
782
        Int64 dummy_result = 0;
440
441
        /// For both cases below: In case of failure, the function parseIPv6 fills vec_res with zero bytes.
442
443
        /// If the source IP address is parsable as an IPv4 address, then transform it into a valid IPv6 address.
444
        /// Keeping it simple by just prefixing `::ffff:` to the IPv4 address to represent it as a valid IPv6 address.
445
782
        size_t string_length = src_next_offset - src_offset;
446
782
        if (string_length != 0) {
447
782
            if (try_parse_ipv4(src_value, dummy_result)) {
448
2
                strncat(src_ipv4_buf, src_value, sizeof(src_ipv4_buf) - strlen(src_ipv4_buf) - 1);
449
2
                parse_result = parse_ipv6_whole(src_ipv4_buf, res_value);
450
780
            } else {
451
780
                parse_result = parse_ipv6_whole(src_value, res_value);
452
780
            }
453
782
        }
454
455
782
        if (parse_result && string_length != 0) {
456
579
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
457
                // handling 16 bytes ipv6 string in the big-endian byte order
458
                // is aimed at conforming to human reading habits
459
579
                std::reverse(res_value, res_value + IPV6_BINARY_LENGTH);
460
579
            }
461
579
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
462
579
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
463
579
                std::copy(res_value, res_value + IPV6_BINARY_LENGTH,
464
579
                          column_string_res->get_chars().begin() + i * IPV6_BINARY_LENGTH);
465
579
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
466
            } else {
467
                col_res->insert_data(reinterpret_cast<const char*>(res_value), IPV6_BINARY_LENGTH);
468
            }
469
579
        } else {
470
203
            if (exception_mode == IPConvertExceptionMode::Throw) {
471
0
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value");
472
0
            }
473
203
            std::fill_n(&vec_res[out_offset], offset_inc, 0);
474
203
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
475
203
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
476
203
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
477
203
            }
478
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
479
                (*vec_null_map_to)[i] = true;
480
            }
481
203
        }
482
782
        src_offset = src_next_offset;
483
782
    }
484
485
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
486
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
487
    }
488
42
    return col_res;
489
42
}
_ZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE2ENS_9ColumnStrIjEES4_EENS_3COWINS_7IColumnEE13immutable_ptrIS6_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
350
112
                          const PaddedPODArray<UInt8>* null_map = nullptr) {
351
112
    const size_t column_size = string_column.size();
352
353
112
    ColumnUInt8::MutablePtr col_null_map_to;
354
112
    ColumnUInt8::Container* vec_null_map_to = nullptr;
355
356
112
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
357
112
        col_null_map_to = ColumnUInt8::create(column_size, false);
358
112
        vec_null_map_to = &col_null_map_to->get_data();
359
112
    }
360
361
112
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
112
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
112
            auto column_string = ColumnString::create();
364
112
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
112
            column_string->get_offsets().reserve(column_size);
366
112
            return column_string;
367
112
        } else {
368
112
            return ColumnIPv6::create();
369
112
        }
370
112
    };
371
372
112
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
112
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
112
            auto& vec_res = col_res->get_chars();
375
112
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
112
            return (vec_res);
377
112
        } else {
378
112
            auto& vec_res = col_res->get_data();
379
112
            vec_res.resize(col_size);
380
112
            return (vec_res);
381
112
        }
382
112
    };
383
384
112
    auto col_res = column_create(column_size);
385
112
    auto& vec_res = get_vector(col_res, column_size);
386
387
112
    using Chars = typename StringColumnType::Chars;
388
112
    const Chars& vec_src = string_column.get_chars();
389
390
112
    size_t src_offset = 0;
391
392
    /// ColumnString contains not null terminated strings. But functions parseIPv6, parseIPv4 expect null terminated string.
393
    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be parsed in-place
394
112
    std::string string_buffer;
395
396
112
    int offset_inc = 1;
397
112
    ColumnString* column_string = nullptr;
398
112
    if constexpr (std::is_same_v<ToColumn, ColumnString>) {
399
112
        offset_inc = IPV6_BINARY_LENGTH;
400
112
        column_string = col_res.get();
401
112
    }
402
403
1.08k
    for (size_t out_offset = 0, i = 0; i < column_size; out_offset += offset_inc, ++i) {
404
977
        char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = "::ffff:";
405
977
        size_t src_next_offset = src_offset;
406
407
977
        const char* src_value = nullptr;
408
977
        auto* res_value = reinterpret_cast<unsigned char*>(&vec_res[out_offset]);
409
410
977
        if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
411
977
            src_value = reinterpret_cast<const char*>(&vec_src[src_offset]);
412
977
            src_next_offset = string_column.get_offsets()[i];
413
414
977
            string_buffer.assign(src_value, src_next_offset - src_offset);
415
977
            src_value = string_buffer.c_str();
416
977
        }
417
418
977
        if (null_map && (*null_map)[i]) {
419
18
            if (exception_mode == IPConvertExceptionMode::Throw) {
420
0
                throw Exception(
421
0
                        ErrorCode::INVALID_ARGUMENT,
422
0
                        "Null Input, you may consider convert it to a valid default IPv6 value "
423
0
                        "like '::' first");
424
18
            } else if (exception_mode == IPConvertExceptionMode::Default) {
425
0
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
426
18
            } else {
427
18
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
428
18
                (*vec_null_map_to)[i] = true;
429
18
            }
430
18
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
431
18
                DCHECK(column_string != nullptr);
432
18
                column_string->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
433
18
            }
434
18
            src_offset = src_next_offset;
435
18
            continue;
436
18
        }
437
438
959
        bool parse_result = false;
439
959
        Int64 dummy_result = 0;
440
441
        /// For both cases below: In case of failure, the function parseIPv6 fills vec_res with zero bytes.
442
443
        /// If the source IP address is parsable as an IPv4 address, then transform it into a valid IPv6 address.
444
        /// Keeping it simple by just prefixing `::ffff:` to the IPv4 address to represent it as a valid IPv6 address.
445
959
        size_t string_length = src_next_offset - src_offset;
446
959
        if (string_length != 0) {
447
951
            if (try_parse_ipv4(src_value, dummy_result)) {
448
50
                strncat(src_ipv4_buf, src_value, sizeof(src_ipv4_buf) - strlen(src_ipv4_buf) - 1);
449
50
                parse_result = parse_ipv6_whole(src_ipv4_buf, res_value);
450
901
            } else {
451
901
                parse_result = parse_ipv6_whole(src_value, res_value);
452
901
            }
453
951
        }
454
455
959
        if (parse_result && string_length != 0) {
456
338
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
457
                // handling 16 bytes ipv6 string in the big-endian byte order
458
                // is aimed at conforming to human reading habits
459
338
                std::reverse(res_value, res_value + IPV6_BINARY_LENGTH);
460
338
            }
461
338
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
462
338
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
463
338
                std::copy(res_value, res_value + IPV6_BINARY_LENGTH,
464
338
                          column_string_res->get_chars().begin() + i * IPV6_BINARY_LENGTH);
465
338
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
466
            } else {
467
                col_res->insert_data(reinterpret_cast<const char*>(res_value), IPV6_BINARY_LENGTH);
468
            }
469
621
        } else {
470
621
            if (exception_mode == IPConvertExceptionMode::Throw) {
471
0
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value");
472
0
            }
473
621
            std::fill_n(&vec_res[out_offset], offset_inc, 0);
474
621
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
475
621
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
476
621
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
477
621
            }
478
621
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
479
621
                (*vec_null_map_to)[i] = true;
480
621
            }
481
621
        }
482
959
        src_offset = src_next_offset;
483
959
    }
484
485
112
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
486
112
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
487
112
    }
488
0
    return col_res;
489
112
}
_ZN5doris6detail15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_12ColumnVectorILNS_13PrimitiveTypeE37EEENS_9ColumnStrIjEEEENS_3COWINS_7IColumnEE13immutable_ptrIS9_EERKT1_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
350
13
                          const PaddedPODArray<UInt8>* null_map = nullptr) {
351
13
    const size_t column_size = string_column.size();
352
353
13
    ColumnUInt8::MutablePtr col_null_map_to;
354
13
    ColumnUInt8::Container* vec_null_map_to = nullptr;
355
356
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
357
        col_null_map_to = ColumnUInt8::create(column_size, false);
358
        vec_null_map_to = &col_null_map_to->get_data();
359
    }
360
361
13
    auto column_create = [](size_t column_size) -> typename ToColumn::MutablePtr {
362
13
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
363
13
            auto column_string = ColumnString::create();
364
13
            column_string->get_chars().reserve(column_size * IPV6_BINARY_LENGTH);
365
13
            column_string->get_offsets().reserve(column_size);
366
13
            return column_string;
367
13
        } else {
368
13
            return ColumnIPv6::create();
369
13
        }
370
13
    };
371
372
13
    auto get_vector = [](auto& col_res, size_t col_size) -> decltype(auto) {
373
13
        if constexpr (std::is_same_v<ToColumn, ColumnString>) {
374
13
            auto& vec_res = col_res->get_chars();
375
13
            vec_res.resize(col_size * IPV6_BINARY_LENGTH);
376
13
            return (vec_res);
377
13
        } else {
378
13
            auto& vec_res = col_res->get_data();
379
13
            vec_res.resize(col_size);
380
13
            return (vec_res);
381
13
        }
382
13
    };
383
384
13
    auto col_res = column_create(column_size);
385
13
    auto& vec_res = get_vector(col_res, column_size);
386
387
13
    using Chars = typename StringColumnType::Chars;
388
13
    const Chars& vec_src = string_column.get_chars();
389
390
13
    size_t src_offset = 0;
391
392
    /// ColumnString contains not null terminated strings. But functions parseIPv6, parseIPv4 expect null terminated string.
393
    /// TODO fix this - now parseIPv6/parseIPv4 accept end iterator, so can be parsed in-place
394
13
    std::string string_buffer;
395
396
13
    int offset_inc = 1;
397
13
    ColumnString* column_string = nullptr;
398
    if constexpr (std::is_same_v<ToColumn, ColumnString>) {
399
        offset_inc = IPV6_BINARY_LENGTH;
400
        column_string = col_res.get();
401
    }
402
403
27
    for (size_t out_offset = 0, i = 0; i < column_size; out_offset += offset_inc, ++i) {
404
18
        char src_ipv4_buf[sizeof("::ffff:") + IPV4_MAX_TEXT_LENGTH + 1] = "::ffff:";
405
18
        size_t src_next_offset = src_offset;
406
407
18
        const char* src_value = nullptr;
408
18
        auto* res_value = reinterpret_cast<unsigned char*>(&vec_res[out_offset]);
409
410
18
        if constexpr (std::is_same_v<StringColumnType, ColumnString>) {
411
18
            src_value = reinterpret_cast<const char*>(&vec_src[src_offset]);
412
18
            src_next_offset = string_column.get_offsets()[i];
413
414
18
            string_buffer.assign(src_value, src_next_offset - src_offset);
415
18
            src_value = string_buffer.c_str();
416
18
        }
417
418
18
        if (null_map && (*null_map)[i]) {
419
0
            if (exception_mode == IPConvertExceptionMode::Throw) {
420
0
                throw Exception(
421
0
                        ErrorCode::INVALID_ARGUMENT,
422
0
                        "Null Input, you may consider convert it to a valid default IPv6 value "
423
0
                        "like '::' first");
424
0
            } else if (exception_mode == IPConvertExceptionMode::Default) {
425
0
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
426
0
            } else {
427
0
                std::fill_n(&vec_res[out_offset], offset_inc, 0);
428
0
                (*vec_null_map_to)[i] = true;
429
0
            }
430
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
431
                DCHECK(column_string != nullptr);
432
                column_string->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
433
            }
434
0
            src_offset = src_next_offset;
435
0
            continue;
436
0
        }
437
438
18
        bool parse_result = false;
439
18
        Int64 dummy_result = 0;
440
441
        /// For both cases below: In case of failure, the function parseIPv6 fills vec_res with zero bytes.
442
443
        /// If the source IP address is parsable as an IPv4 address, then transform it into a valid IPv6 address.
444
        /// Keeping it simple by just prefixing `::ffff:` to the IPv4 address to represent it as a valid IPv6 address.
445
18
        size_t string_length = src_next_offset - src_offset;
446
18
        if (string_length != 0) {
447
16
            if (try_parse_ipv4(src_value, dummy_result)) {
448
0
                strncat(src_ipv4_buf, src_value, sizeof(src_ipv4_buf) - strlen(src_ipv4_buf) - 1);
449
0
                parse_result = parse_ipv6_whole(src_ipv4_buf, res_value);
450
16
            } else {
451
16
                parse_result = parse_ipv6_whole(src_value, res_value);
452
16
            }
453
16
        }
454
455
18
        if (parse_result && string_length != 0) {
456
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
457
                // handling 16 bytes ipv6 string in the big-endian byte order
458
                // is aimed at conforming to human reading habits
459
                std::reverse(res_value, res_value + IPV6_BINARY_LENGTH);
460
            }
461
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
462
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
463
                std::copy(res_value, res_value + IPV6_BINARY_LENGTH,
464
                          column_string_res->get_chars().begin() + i * IPV6_BINARY_LENGTH);
465
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
466
14
            } else {
467
14
                col_res->insert_data(reinterpret_cast<const char*>(res_value), IPV6_BINARY_LENGTH);
468
14
            }
469
14
        } else {
470
4
            if (exception_mode == IPConvertExceptionMode::Throw) {
471
4
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value");
472
4
            }
473
0
            std::fill_n(&vec_res[out_offset], offset_inc, 0);
474
            if constexpr (std::is_same_v<ToColumn, ColumnString>) {
475
                auto* column_string_res = static_cast<ColumnString*>(col_res.get());
476
                column_string_res->get_offsets().push_back((i + 1) * IPV6_BINARY_LENGTH);
477
            }
478
            if constexpr (exception_mode == IPConvertExceptionMode::Null) {
479
                (*vec_null_map_to)[i] = true;
480
            }
481
0
        }
482
14
        src_offset = src_next_offset;
483
14
    }
484
485
    if constexpr (exception_mode == IPConvertExceptionMode::Null) {
486
        return ColumnNullable::create(std::move(col_res), std::move(col_null_map_to));
487
    }
488
9
    return col_res;
489
13
}
490
} // namespace detail
491
492
template <IPConvertExceptionMode exception_mode, typename ToColumn = ColumnIPv6>
493
202
ColumnPtr convert_to_ipv6(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
494
202
    const auto* column_input_string = assert_cast<const ColumnString*>(column.get());
495
202
    auto result = detail::convert_to_ipv6<exception_mode, ToColumn>(*column_input_string, null_map);
496
202
    return result;
497
202
}
_ZN5doris15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_9ColumnStrIjEEEENS_3COWINS_7IColumnEE13immutable_ptrIS5_EES8_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
493
35
ColumnPtr convert_to_ipv6(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
494
35
    const auto* column_input_string = assert_cast<const ColumnString*>(column.get());
495
35
    auto result = detail::convert_to_ipv6<exception_mode, ToColumn>(*column_input_string, null_map);
496
35
    return result;
497
35
}
_ZN5doris15convert_to_ipv6ILNS_22IPConvertExceptionModeE1ENS_9ColumnStrIjEEEENS_3COWINS_7IColumnEE13immutable_ptrIS5_EES8_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
493
42
ColumnPtr convert_to_ipv6(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
494
42
    const auto* column_input_string = assert_cast<const ColumnString*>(column.get());
495
42
    auto result = detail::convert_to_ipv6<exception_mode, ToColumn>(*column_input_string, null_map);
496
42
    return result;
497
42
}
_ZN5doris15convert_to_ipv6ILNS_22IPConvertExceptionModeE2ENS_9ColumnStrIjEEEENS_3COWINS_7IColumnEE13immutable_ptrIS5_EES8_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
493
112
ColumnPtr convert_to_ipv6(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
494
112
    const auto* column_input_string = assert_cast<const ColumnString*>(column.get());
495
112
    auto result = detail::convert_to_ipv6<exception_mode, ToColumn>(*column_input_string, null_map);
496
112
    return result;
497
112
}
_ZN5doris15convert_to_ipv6ILNS_22IPConvertExceptionModeE0ENS_12ColumnVectorILNS_13PrimitiveTypeE37EEEEENS_3COWINS_7IColumnEE13immutable_ptrIS6_EES9_PKNS_8PODArrayIhLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEE
Line
Count
Source
493
13
ColumnPtr convert_to_ipv6(ColumnPtr column, const PaddedPODArray<UInt8>* null_map = nullptr) {
494
13
    const auto* column_input_string = assert_cast<const ColumnString*>(column.get());
495
13
    auto result = detail::convert_to_ipv6<exception_mode, ToColumn>(*column_input_string, null_map);
496
13
    return result;
497
13
}
498
499
template <IPConvertExceptionMode exception_mode>
500
class FunctionIPv6StringToNum : public IFunction {
501
public:
502
    static constexpr auto name = exception_mode == IPConvertExceptionMode::Throw
503
                                         ? "ipv6_string_to_num"
504
                                         : (exception_mode == IPConvertExceptionMode::Default
505
                                                    ? "ipv6_string_to_num_or_default"
506
                                                    : "ipv6_string_to_num_or_null");
507
508
141
    static FunctionPtr create() {
509
141
        return std::make_shared<FunctionIPv6StringToNum<exception_mode>>();
510
141
    }
_ZN5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE0EE6createEv
Line
Count
Source
508
32
    static FunctionPtr create() {
509
32
        return std::make_shared<FunctionIPv6StringToNum<exception_mode>>();
510
32
    }
_ZN5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE1EE6createEv
Line
Count
Source
508
27
    static FunctionPtr create() {
509
27
        return std::make_shared<FunctionIPv6StringToNum<exception_mode>>();
510
27
    }
_ZN5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE2EE6createEv
Line
Count
Source
508
82
    static FunctionPtr create() {
509
82
        return std::make_shared<FunctionIPv6StringToNum<exception_mode>>();
510
82
    }
511
512
3
    String get_name() const override { return name; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE0EE8get_nameB5cxx11Ev
Line
Count
Source
512
1
    String get_name() const override { return name; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE1EE8get_nameB5cxx11Ev
Line
Count
Source
512
1
    String get_name() const override { return name; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE2EE8get_nameB5cxx11Ev
Line
Count
Source
512
1
    String get_name() const override { return name; }
513
514
114
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE0EE23get_number_of_argumentsEv
Line
Count
Source
514
23
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE1EE23get_number_of_argumentsEv
Line
Count
Source
514
18
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE2EE23get_number_of_argumentsEv
Line
Count
Source
514
73
    size_t get_number_of_arguments() const override { return 1; }
515
516
303
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE0EE36use_default_implementation_for_nullsEv
Line
Count
Source
516
58
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE1EE36use_default_implementation_for_nullsEv
Line
Count
Source
516
60
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE2EE36use_default_implementation_for_nullsEv
Line
Count
Source
516
185
    bool use_default_implementation_for_nulls() const override { return false; }
517
518
114
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
519
114
        auto result_type = std::make_shared<DataTypeString>();
520
521
114
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
522
73
            return make_nullable(result_type);
523
73
        }
524
525
0
        return result_type;
526
114
    }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE0EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
518
23
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
519
23
        auto result_type = std::make_shared<DataTypeString>();
520
521
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
522
            return make_nullable(result_type);
523
        }
524
525
23
        return result_type;
526
23
    }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE1EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
518
18
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
519
18
        auto result_type = std::make_shared<DataTypeString>();
520
521
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
522
            return make_nullable(result_type);
523
        }
524
525
18
        return result_type;
526
18
    }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE2EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
518
73
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
519
73
        auto result_type = std::make_shared<DataTypeString>();
520
521
73
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
522
73
            return make_nullable(result_type);
523
73
        }
524
525
0
        return result_type;
526
73
    }
527
528
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
529
189
                        uint32_t result, size_t input_rows_count) const override {
530
189
        ColumnPtr column =
531
189
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
532
189
        ColumnPtr null_map_column;
533
189
        const NullMap* null_map = nullptr;
534
535
189
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
536
87
            column = column_nullable->get_nested_column_ptr();
537
87
            null_map_column = column_nullable->get_null_map_column_ptr();
538
87
            null_map = &column_nullable->get_null_map_data();
539
87
        }
540
541
189
        auto col_res = convert_to_ipv6<exception_mode, ColumnString>(column, null_map);
542
543
189
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
544
54
            block.replace_by_position(
545
54
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
546
135
        } else {
547
135
            block.replace_by_position(result, std::move(col_res));
548
135
        }
549
189
        return Status::OK();
550
189
    }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE0EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
529
35
                        uint32_t result, size_t input_rows_count) const override {
530
35
        ColumnPtr column =
531
35
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
532
35
        ColumnPtr null_map_column;
533
35
        const NullMap* null_map = nullptr;
534
535
35
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
536
13
            column = column_nullable->get_nested_column_ptr();
537
13
            null_map_column = column_nullable->get_null_map_column_ptr();
538
13
            null_map = &column_nullable->get_null_map_data();
539
13
        }
540
541
35
        auto col_res = convert_to_ipv6<exception_mode, ColumnString>(column, null_map);
542
543
35
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
544
0
            block.replace_by_position(
545
0
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
546
35
        } else {
547
35
            block.replace_by_position(result, std::move(col_res));
548
35
        }
549
35
        return Status::OK();
550
35
    }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE1EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
529
42
                        uint32_t result, size_t input_rows_count) const override {
530
42
        ColumnPtr column =
531
42
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
532
42
        ColumnPtr null_map_column;
533
42
        const NullMap* null_map = nullptr;
534
535
42
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
536
20
            column = column_nullable->get_nested_column_ptr();
537
20
            null_map_column = column_nullable->get_null_map_column_ptr();
538
20
            null_map = &column_nullable->get_null_map_data();
539
20
        }
540
541
42
        auto col_res = convert_to_ipv6<exception_mode, ColumnString>(column, null_map);
542
543
42
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
544
0
            block.replace_by_position(
545
0
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
546
42
        } else {
547
42
            block.replace_by_position(result, std::move(col_res));
548
42
        }
549
42
        return Status::OK();
550
42
    }
_ZNK5doris23FunctionIPv6StringToNumILNS_22IPConvertExceptionModeE2EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
529
112
                        uint32_t result, size_t input_rows_count) const override {
530
112
        ColumnPtr column =
531
112
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
532
112
        ColumnPtr null_map_column;
533
112
        const NullMap* null_map = nullptr;
534
535
112
        if (const auto* column_nullable = check_and_get_column<ColumnNullable>(column.get())) {
536
54
            column = column_nullable->get_nested_column_ptr();
537
54
            null_map_column = column_nullable->get_null_map_column_ptr();
538
54
            null_map = &column_nullable->get_null_map_data();
539
54
        }
540
541
112
        auto col_res = convert_to_ipv6<exception_mode, ColumnString>(column, null_map);
542
543
112
        if (null_map && exception_mode == IPConvertExceptionMode::Null) {
544
54
            block.replace_by_position(
545
54
                    result, ColumnNullable::create(std::move(col_res), std::move(null_map_column)));
546
58
        } else {
547
58
            block.replace_by_position(result, std::move(col_res));
548
58
        }
549
112
        return Status::OK();
550
112
    }
551
};
552
553
template <typename Type>
554
class FunctionIsIPString : public IFunction {
555
    static_assert(std::is_same_v<Type, IPv4> || std::is_same_v<Type, IPv6>);
556
557
public:
558
    static constexpr auto name = std::is_same_v<Type, IPv4> ? "is_ipv4_string" : "is_ipv6_string";
559
58
    static FunctionPtr create() { return std::make_shared<FunctionIsIPString<Type>>(); }
_ZN5doris18FunctionIsIPStringIjE6createEv
Line
Count
Source
559
28
    static FunctionPtr create() { return std::make_shared<FunctionIsIPString<Type>>(); }
_ZN5doris18FunctionIsIPStringIoE6createEv
Line
Count
Source
559
30
    static FunctionPtr create() { return std::make_shared<FunctionIsIPString<Type>>(); }
560
561
2
    String get_name() const override { return name; }
_ZNK5doris18FunctionIsIPStringIjE8get_nameB5cxx11Ev
Line
Count
Source
561
1
    String get_name() const override { return name; }
_ZNK5doris18FunctionIsIPStringIoE8get_nameB5cxx11Ev
Line
Count
Source
561
1
    String get_name() const override { return name; }
562
563
40
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris18FunctionIsIPStringIjE23get_number_of_argumentsEv
Line
Count
Source
563
19
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris18FunctionIsIPStringIoE23get_number_of_argumentsEv
Line
Count
Source
563
21
    size_t get_number_of_arguments() const override { return 1; }
564
565
40
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
566
40
        return std::make_shared<DataTypeUInt8>();
567
40
    }
_ZNK5doris18FunctionIsIPStringIjE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS6_EE
Line
Count
Source
565
19
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
566
19
        return std::make_shared<DataTypeUInt8>();
567
19
    }
_ZNK5doris18FunctionIsIPStringIoE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS6_EE
Line
Count
Source
565
21
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
566
21
        return std::make_shared<DataTypeUInt8>();
567
21
    }
568
569
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
570
96
                        uint32_t result, size_t input_rows_count) const override {
571
96
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
572
96
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
573
96
        const auto* str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
574
96
        auto col_res = ColumnUInt8::create(input_rows_count, 0);
575
96
        auto& col_res_data = col_res->get_data();
576
577
1.73k
        for (size_t i = 0; i < input_rows_count; ++i) {
578
1.63k
            if constexpr (std::is_same_v<Type, IPv4>) {
579
816
                StringRef ipv4_str = str_addr_column->get_data_at(i);
580
816
                if (IPv4Value::is_valid_string(ipv4_str.data, ipv4_str.size)) {
581
206
                    col_res_data[i] = 1;
582
206
                }
583
818
            } else {
584
818
                StringRef ipv6_str = str_addr_column->get_data_at(i);
585
818
                if (IPv6Value::is_valid_string(ipv6_str.data, ipv6_str.size)) {
586
210
                    col_res_data[i] = 1;
587
210
                }
588
818
            }
589
1.63k
        }
590
591
96
        block.replace_by_position(result, std::move(col_res));
592
96
        return Status::OK();
593
96
    }
_ZNK5doris18FunctionIsIPStringIjE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
570
47
                        uint32_t result, size_t input_rows_count) const override {
571
47
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
572
47
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
573
47
        const auto* str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
574
47
        auto col_res = ColumnUInt8::create(input_rows_count, 0);
575
47
        auto& col_res_data = col_res->get_data();
576
577
863
        for (size_t i = 0; i < input_rows_count; ++i) {
578
816
            if constexpr (std::is_same_v<Type, IPv4>) {
579
816
                StringRef ipv4_str = str_addr_column->get_data_at(i);
580
816
                if (IPv4Value::is_valid_string(ipv4_str.data, ipv4_str.size)) {
581
206
                    col_res_data[i] = 1;
582
206
                }
583
            } else {
584
                StringRef ipv6_str = str_addr_column->get_data_at(i);
585
                if (IPv6Value::is_valid_string(ipv6_str.data, ipv6_str.size)) {
586
                    col_res_data[i] = 1;
587
                }
588
            }
589
816
        }
590
591
47
        block.replace_by_position(result, std::move(col_res));
592
47
        return Status::OK();
593
47
    }
_ZNK5doris18FunctionIsIPStringIoE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
570
49
                        uint32_t result, size_t input_rows_count) const override {
571
49
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
572
49
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
573
49
        const auto* str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
574
49
        auto col_res = ColumnUInt8::create(input_rows_count, 0);
575
49
        auto& col_res_data = col_res->get_data();
576
577
867
        for (size_t i = 0; i < input_rows_count; ++i) {
578
            if constexpr (std::is_same_v<Type, IPv4>) {
579
                StringRef ipv4_str = str_addr_column->get_data_at(i);
580
                if (IPv4Value::is_valid_string(ipv4_str.data, ipv4_str.size)) {
581
                    col_res_data[i] = 1;
582
                }
583
818
            } else {
584
818
                StringRef ipv6_str = str_addr_column->get_data_at(i);
585
818
                if (IPv6Value::is_valid_string(ipv6_str.data, ipv6_str.size)) {
586
210
                    col_res_data[i] = 1;
587
210
                }
588
818
            }
589
818
        }
590
591
49
        block.replace_by_position(result, std::move(col_res));
592
49
        return Status::OK();
593
49
    }
594
};
595
596
class FunctionIsIPAddressInRange : public IFunction {
597
public:
598
    static constexpr auto name = "is_ip_address_in_range";
599
74
    static FunctionPtr create() { return std::make_shared<FunctionIsIPAddressInRange>(); }
600
601
1
    String get_name() const override { return name; }
602
603
65
    size_t get_number_of_arguments() const override { return 2; }
604
605
65
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
606
65
        return std::make_shared<DataTypeUInt8>();
607
65
    }
608
609
    template <PrimitiveType PT, typename ColumnType>
610
    void execute_impl_with_ip(size_t input_rows_count, bool addr_const, bool cidr_const,
611
                              const ColumnString* str_cidr_column, const ColumnPtr addr_column,
612
16
                              ColumnUInt8* col_res) const {
613
16
        auto& col_res_data = col_res->get_data();
614
16
        const auto& ip_data = assert_cast<const ColumnType*>(addr_column.get())->get_data();
615
411
        for (size_t i = 0; i < input_rows_count; ++i) {
616
395
            auto addr_idx = index_check_const(i, addr_const);
617
395
            auto cidr_idx = index_check_const(i, cidr_const);
618
395
            auto cidr_data = str_cidr_column->get_data_at(cidr_idx);
619
            // cidr_data maybe NULL, But the input column is nested column, so check here avoid throw exception
620
395
            if (cidr_data.data == nullptr || cidr_data.size == 0) {
621
0
                col_res_data[i] = 0;
622
0
                continue;
623
0
            }
624
395
            const auto cidr = parse_ip_with_cidr(cidr_data.to_string_view());
625
395
            if constexpr (PT == PrimitiveType::TYPE_IPV4) {
626
196
                if (cidr._address.as_v4()) {
627
0
                    col_res_data[i] = match_ipv4_subnet(ip_data[addr_idx], cidr._address.as_v4(),
628
0
                                                        cidr._prefix)
629
0
                                              ? 1
630
0
                                              : 0;
631
196
                } else {
632
196
                    col_res_data[i] = 0;
633
196
                }
634
199
            } else if constexpr (PT == PrimitiveType::TYPE_IPV6) {
635
199
                if (cidr._address.as_v6()) {
636
199
                    col_res_data[i] = match_ipv6_subnet((uint8_t*)(&ip_data[addr_idx]),
637
199
                                                        cidr._address.as_v6(), cidr._prefix)
638
199
                                              ? 1
639
199
                                              : 0;
640
199
                } else {
641
0
                    col_res_data[i] = 0;
642
0
                }
643
199
            }
644
395
        }
645
16
    }
_ZNK5doris26FunctionIsIPAddressInRange20execute_impl_with_ipILNS_13PrimitiveTypeE36ENS_12ColumnVectorILS2_36EEEEEvmbbPKNS_9ColumnStrIjEENS_3COWINS_7IColumnEE13immutable_ptrISA_EEPNS3_ILS2_2EEE
Line
Count
Source
612
8
                              ColumnUInt8* col_res) const {
613
8
        auto& col_res_data = col_res->get_data();
614
8
        const auto& ip_data = assert_cast<const ColumnType*>(addr_column.get())->get_data();
615
204
        for (size_t i = 0; i < input_rows_count; ++i) {
616
196
            auto addr_idx = index_check_const(i, addr_const);
617
196
            auto cidr_idx = index_check_const(i, cidr_const);
618
196
            auto cidr_data = str_cidr_column->get_data_at(cidr_idx);
619
            // cidr_data maybe NULL, But the input column is nested column, so check here avoid throw exception
620
196
            if (cidr_data.data == nullptr || cidr_data.size == 0) {
621
0
                col_res_data[i] = 0;
622
0
                continue;
623
0
            }
624
196
            const auto cidr = parse_ip_with_cidr(cidr_data.to_string_view());
625
196
            if constexpr (PT == PrimitiveType::TYPE_IPV4) {
626
196
                if (cidr._address.as_v4()) {
627
0
                    col_res_data[i] = match_ipv4_subnet(ip_data[addr_idx], cidr._address.as_v4(),
628
0
                                                        cidr._prefix)
629
0
                                              ? 1
630
0
                                              : 0;
631
196
                } else {
632
196
                    col_res_data[i] = 0;
633
196
                }
634
            } else if constexpr (PT == PrimitiveType::TYPE_IPV6) {
635
                if (cidr._address.as_v6()) {
636
                    col_res_data[i] = match_ipv6_subnet((uint8_t*)(&ip_data[addr_idx]),
637
                                                        cidr._address.as_v6(), cidr._prefix)
638
                                              ? 1
639
                                              : 0;
640
                } else {
641
                    col_res_data[i] = 0;
642
                }
643
            }
644
196
        }
645
8
    }
_ZNK5doris26FunctionIsIPAddressInRange20execute_impl_with_ipILNS_13PrimitiveTypeE37ENS_12ColumnVectorILS2_37EEEEEvmbbPKNS_9ColumnStrIjEENS_3COWINS_7IColumnEE13immutable_ptrISA_EEPNS3_ILS2_2EEE
Line
Count
Source
612
8
                              ColumnUInt8* col_res) const {
613
8
        auto& col_res_data = col_res->get_data();
614
8
        const auto& ip_data = assert_cast<const ColumnType*>(addr_column.get())->get_data();
615
207
        for (size_t i = 0; i < input_rows_count; ++i) {
616
199
            auto addr_idx = index_check_const(i, addr_const);
617
199
            auto cidr_idx = index_check_const(i, cidr_const);
618
199
            auto cidr_data = str_cidr_column->get_data_at(cidr_idx);
619
            // cidr_data maybe NULL, But the input column is nested column, so check here avoid throw exception
620
199
            if (cidr_data.data == nullptr || cidr_data.size == 0) {
621
0
                col_res_data[i] = 0;
622
0
                continue;
623
0
            }
624
199
            const auto cidr = parse_ip_with_cidr(cidr_data.to_string_view());
625
            if constexpr (PT == PrimitiveType::TYPE_IPV4) {
626
                if (cidr._address.as_v4()) {
627
                    col_res_data[i] = match_ipv4_subnet(ip_data[addr_idx], cidr._address.as_v4(),
628
                                                        cidr._prefix)
629
                                              ? 1
630
                                              : 0;
631
                } else {
632
                    col_res_data[i] = 0;
633
                }
634
199
            } else if constexpr (PT == PrimitiveType::TYPE_IPV6) {
635
199
                if (cidr._address.as_v6()) {
636
199
                    col_res_data[i] = match_ipv6_subnet((uint8_t*)(&ip_data[addr_idx]),
637
199
                                                        cidr._address.as_v6(), cidr._prefix)
638
199
                                              ? 1
639
199
                                              : 0;
640
199
                } else {
641
0
                    col_res_data[i] = 0;
642
0
                }
643
199
            }
644
199
        }
645
8
    }
646
647
    Status evaluate_inverted_index(
648
            const ColumnsWithTypeAndName& arguments,
649
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
650
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
651
            const InvertedIndexAnalyzerCtx* /*analyzer_ctx*/,
652
2
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override {
653
2
        DCHECK(arguments.size() == 1);
654
2
        DCHECK(data_type_with_names.size() == 1);
655
2
        DCHECK(iterators.size() == 1);
656
2
        auto* iter = iterators[0];
657
2
        auto data_type_with_name = data_type_with_names[0];
658
2
        if (iter == nullptr) {
659
0
            return Status::OK();
660
0
        }
661
662
2
        if (!segment_v2::IndexReaderHelper::has_bkd_index(iter)) {
663
            // Not support only bkd index
664
0
            return Status::Error<ErrorCode::INVERTED_INDEX_EVALUATE_SKIPPED>(
665
0
                    "Inverted index evaluate skipped, ip range reader can only support by bkd "
666
0
                    "reader");
667
0
        }
668
        // Get the is_ip_address_in_range from the arguments: cidr
669
2
        const auto& cidr_column_with_type_and_name = arguments[0];
670
        // in is_ip_address_in_range param is const Field
671
2
        ColumnPtr arg_column = cidr_column_with_type_and_name.column;
672
2
        DataTypePtr arg_type = cidr_column_with_type_and_name.type;
673
2
        if ((is_column_nullable(*arg_column) && !is_column_const(*remove_nullable(arg_column))) ||
674
2
            (!is_column_nullable(*arg_column) && !is_column_const(*arg_column))) {
675
            // if not we should skip inverted index and evaluate in expression
676
0
            return Status::Error<ErrorCode::INVERTED_INDEX_EVALUATE_SKIPPED>(
677
0
                    "Inverted index evaluate skipped, is_ip_address_in_range only support const "
678
0
                    "value");
679
0
        }
680
        // check param type is string
681
2
        if (!is_string_type(arg_type->get_primitive_type())) {
682
0
            return Status::Error<ErrorCode::INVERTED_INDEX_EVALUATE_SKIPPED>(
683
0
                    "Inverted index evaluate skipped, is_ip_address_in_range only support string "
684
0
                    "type");
685
0
        }
686
        // min && max ip address
687
2
        Field min_ip, max_ip;
688
2
        IPAddressCIDR cidr = parse_ip_with_cidr(arg_column->get_data_at(0));
689
2
        if (data_type_with_name.second->get_primitive_type() == TYPE_IPV4 &&
690
2
            cidr._address.as_v4()) {
691
1
            auto range = apply_cidr_mask(cidr._address.as_v4(), cidr._prefix);
692
1
            min_ip = Field::create_field<TYPE_IPV4>(range.first);
693
1
            max_ip = Field::create_field<TYPE_IPV4>(range.second);
694
1
        } else if (data_type_with_name.second->get_primitive_type() == TYPE_IPV6 &&
695
1
                   cidr._address.as_v6()) {
696
1
            auto cidr_range_ipv6_col = ColumnIPv6::create(2, 0);
697
1
            auto& cidr_range_ipv6_data = cidr_range_ipv6_col->get_data();
698
1
            apply_cidr_mask(reinterpret_cast<const char*>(cidr._address.as_v6()),
699
1
                            reinterpret_cast<char*>(&cidr_range_ipv6_data[0]),
700
1
                            reinterpret_cast<char*>(&cidr_range_ipv6_data[1]), cidr._prefix);
701
1
            min_ip = Field::create_field<TYPE_IPV6>(cidr_range_ipv6_data[0]);
702
1
            max_ip = Field::create_field<TYPE_IPV6>(cidr_range_ipv6_data[1]);
703
1
        } else {
704
            // if here param is invalid for current column to calcute min_ip|max_ip we just return
705
0
            return Status::Error<ErrorCode::INVERTED_INDEX_EVALUATE_SKIPPED>(
706
0
                    "Inverted index evaluate skipped, data type " + arg_type->get_name() +
707
0
                    " can not support this cidr " + arg_column->get_data_at(0).to_string());
708
0
        }
709
        // apply for inverted index
710
2
        std::shared_ptr<roaring::Roaring> null_bitmap = std::make_shared<roaring::Roaring>();
711
712
        // >= min ip
713
2
        segment_v2::InvertedIndexParam min_param;
714
2
        min_param.column_name = data_type_with_name.first;
715
2
        min_param.column_type = data_type_with_name.second;
716
2
        min_param.query_type = segment_v2::InvertedIndexQueryType::GREATER_EQUAL_QUERY;
717
2
        min_param.query_value = min_ip;
718
2
        min_param.num_rows = num_rows;
719
2
        min_param.roaring = std::make_shared<roaring::Roaring>();
720
2
        RETURN_IF_ERROR(iter->read_from_index(&min_param));
721
722
        // <= max ip
723
2
        segment_v2::InvertedIndexParam max_param;
724
2
        max_param.column_name = data_type_with_name.first;
725
2
        max_param.column_type = data_type_with_name.second;
726
2
        max_param.query_type = segment_v2::InvertedIndexQueryType::LESS_EQUAL_QUERY;
727
2
        max_param.query_value = max_ip;
728
2
        max_param.num_rows = num_rows;
729
2
        max_param.roaring = std::make_shared<roaring::Roaring>();
730
2
        RETURN_IF_ERROR(iter->read_from_index(&max_param));
731
732
2
        auto result_roaring = std::make_shared<roaring::Roaring>();
733
2
        *result_roaring = *min_param.roaring & *max_param.roaring;
734
735
2
        DBUG_EXECUTE_IF("ip.inverted_index_filtered", {
736
2
            auto req_id = DebugPoints::instance()->get_debug_param_or_default<int32_t>(
737
2
                    "ip.inverted_index_filtered", "req_id", 0);
738
2
            LOG(INFO) << "execute inverted index req_id: " << req_id
739
2
                      << " min: " << min_param.roaring->cardinality()
740
2
                      << " max: " << max_param.roaring->cardinality()
741
2
                      << " result: " << result_roaring->cardinality();
742
2
        });
743
2
        segment_v2::InvertedIndexResultBitmap result(result_roaring, null_bitmap);
744
2
        bitmap_result = result;
745
2
        bitmap_result.mask_out_null();
746
2
        return Status::OK();
747
2
    }
748
749
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
750
95
                        uint32_t result, size_t input_rows_count) const override {
751
95
        DBUG_EXECUTE_IF("ip.inverted_index_filtered", {
752
95
            auto req_id = DebugPoints::instance()->get_debug_param_or_default<int32_t>(
753
95
                    "ip.inverted_index_filtered", "req_id", 0);
754
95
            return Status::Error<ErrorCode::INTERNAL_ERROR>(
755
95
                    "{} has already execute inverted index req_id {} , should not execute expr "
756
95
                    "with rows: {}",
757
95
                    get_name(), req_id, input_rows_count);
758
95
        });
759
95
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
760
95
        const auto& cidr_column_with_type_and_name = block.get_by_position(arguments[1]);
761
95
        const auto& [addr_column, addr_const] =
762
95
                unpack_if_const(addr_column_with_type_and_name.column);
763
95
        const auto& [cidr_column, cidr_const] =
764
95
                unpack_if_const(cidr_column_with_type_and_name.column);
765
766
95
        auto col_res = ColumnUInt8::create(input_rows_count, 0);
767
95
        auto& col_res_data = col_res->get_data();
768
769
95
        if (addr_column_with_type_and_name.type->get_primitive_type() == TYPE_IPV4) {
770
8
            execute_impl_with_ip<PrimitiveType::TYPE_IPV4, ColumnIPv4>(
771
8
                    input_rows_count, addr_const, cidr_const,
772
8
                    assert_cast<const ColumnString*>(cidr_column.get()), addr_column,
773
8
                    col_res.get());
774
87
        } else if (addr_column_with_type_and_name.type->get_primitive_type() == TYPE_IPV6) {
775
8
            execute_impl_with_ip<PrimitiveType::TYPE_IPV6, ColumnIPv6>(
776
8
                    input_rows_count, addr_const, cidr_const,
777
8
                    assert_cast<const ColumnString*>(cidr_column.get()), addr_column,
778
8
                    col_res.get());
779
79
        } else {
780
79
            const auto* str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
781
79
            const auto* str_cidr_column = assert_cast<const ColumnString*>(cidr_column.get());
782
783
250
            for (size_t i = 0; i < input_rows_count; ++i) {
784
171
                auto addr_idx = index_check_const(i, addr_const);
785
171
                auto cidr_idx = index_check_const(i, cidr_const);
786
171
                auto addr_data = str_addr_column->get_data_at(addr_idx);
787
171
                auto cidr_data = str_cidr_column->get_data_at(cidr_idx);
788
                // cidr_data maybe NULL, But the input column is nested column, so check here avoid throw exception
789
171
                if (cidr_data.data == nullptr || cidr_data.size == 0) {
790
1
                    col_res_data[i] = 0;
791
1
                    continue;
792
1
                }
793
170
                const auto addr = IPAddressVariant(addr_data.to_string_view());
794
170
                const auto cidr = parse_ip_with_cidr(cidr_data.to_string_view());
795
170
                col_res_data[i] = is_address_in_range(addr, cidr) ? 1 : 0;
796
170
            }
797
79
        }
798
799
95
        block.replace_by_position(result, std::move(col_res));
800
95
        return Status::OK();
801
95
    }
802
};
803
804
class FunctionIPv4CIDRToRange : public IFunction {
805
public:
806
    static constexpr auto name = "ipv4_cidr_to_range";
807
21
    static FunctionPtr create() { return std::make_shared<FunctionIPv4CIDRToRange>(); }
808
809
1
    String get_name() const override { return name; }
810
811
12
    size_t get_number_of_arguments() const override { return 2; }
812
813
12
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
814
12
        DataTypePtr element = std::make_shared<DataTypeIPv4>();
815
12
        return std::make_shared<DataTypeStruct>(DataTypes {element, element},
816
12
                                                Strings {"min", "max"});
817
12
    }
818
819
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
820
33
                        uint32_t result, size_t input_rows_count) const override {
821
33
        ColumnWithTypeAndName& ip_column = block.get_by_position(arguments[0]);
822
33
        ColumnWithTypeAndName& cidr_column = block.get_by_position(arguments[1]);
823
824
33
        const auto& [ip_column_ptr, ip_col_const] = unpack_if_const(ip_column.column);
825
33
        const auto& [cidr_column_ptr, cidr_col_const] = unpack_if_const(cidr_column.column);
826
827
33
        const auto* col_ip_column = assert_cast<const ColumnIPv4*>(ip_column_ptr.get());
828
33
        const auto* col_cidr_column = assert_cast<const ColumnInt16*>(cidr_column_ptr.get());
829
830
33
        const typename ColumnIPv4::Container& vec_ip_input = col_ip_column->get_data();
831
33
        const ColumnInt16::Container& vec_cidr_input = col_cidr_column->get_data();
832
33
        auto col_lower_range_output = ColumnIPv4::create(input_rows_count, 0);
833
33
        auto col_upper_range_output = ColumnIPv4::create(input_rows_count, 0);
834
835
33
        ColumnIPv4::Container& vec_lower_range_output = col_lower_range_output->get_data();
836
33
        ColumnIPv4::Container& vec_upper_range_output = col_upper_range_output->get_data();
837
838
33
        static constexpr UInt8 max_cidr_mask = IPV4_BINARY_LENGTH * 8;
839
840
33
        if (ip_col_const) {
841
4
            auto ip = vec_ip_input[0];
842
11
            for (size_t i = 0; i < input_rows_count; ++i) {
843
7
                auto cidr = vec_cidr_input[i];
844
7
                if (cidr < 0 || cidr > max_cidr_mask) {
845
0
                    throw Exception(ErrorCode::INVALID_ARGUMENT, "Illegal cidr value '{}'",
846
0
                                    std::to_string(cidr));
847
0
                }
848
7
                auto range = apply_cidr_mask(ip, cast_set<UInt8>(cidr));
849
7
                vec_lower_range_output[i] = range.first;
850
7
                vec_upper_range_output[i] = range.second;
851
7
            }
852
29
        } else if (cidr_col_const) {
853
20
            auto cidr = vec_cidr_input[0];
854
20
            if (cidr < 0 || cidr > max_cidr_mask) {
855
0
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Illegal cidr value '{}'",
856
0
                                std::to_string(cidr));
857
0
            }
858
425
            for (size_t i = 0; i < input_rows_count; ++i) {
859
405
                auto ip = vec_ip_input[i];
860
405
                auto range = apply_cidr_mask(ip, cast_set<UInt8>(cidr));
861
405
                vec_lower_range_output[i] = range.first;
862
405
                vec_upper_range_output[i] = range.second;
863
405
            }
864
20
        } else {
865
21
            for (size_t i = 0; i < input_rows_count; ++i) {
866
12
                auto ip = vec_ip_input[i];
867
12
                auto cidr = vec_cidr_input[i];
868
12
                if (cidr < 0 || cidr > max_cidr_mask) {
869
0
                    throw Exception(ErrorCode::INVALID_ARGUMENT, "Illegal cidr value '{}'",
870
0
                                    std::to_string(cidr));
871
0
                }
872
12
                auto range = apply_cidr_mask(ip, cast_set<UInt8>(cidr));
873
12
                vec_lower_range_output[i] = range.first;
874
12
                vec_upper_range_output[i] = range.second;
875
12
            }
876
9
        }
877
878
33
        block.replace_by_position(
879
33
                result, ColumnStruct::create(Columns {std::move(col_lower_range_output),
880
33
                                                      std::move(col_upper_range_output)}));
881
33
        return Status::OK();
882
33
    }
883
};
884
885
/**
886
 * this function accepts two arguments: an IPv6 address and a CIDR mask
887
 *  IPv6 address can be either ipv6 type or string type as ipv6 string address
888
 *  FE: PropagateNullable is used to handle nullable columns
889
 */
890
class FunctionIPv6CIDRToRange : public IFunction {
891
public:
892
    static constexpr auto name = "ipv6_cidr_to_range";
893
30
    static FunctionPtr create() { return std::make_shared<FunctionIPv6CIDRToRange>(); }
894
895
1
    String get_name() const override { return name; }
896
897
21
    size_t get_number_of_arguments() const override { return 2; }
898
899
21
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
900
21
        DataTypePtr element = std::make_shared<DataTypeIPv6>();
901
21
        return std::make_shared<DataTypeStruct>(DataTypes {element, element},
902
21
                                                Strings {"min", "max"});
903
21
    }
904
905
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
906
42
                        uint32_t result, size_t input_rows_count) const override {
907
42
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
908
42
        const auto& cidr_column_with_type_and_name = block.get_by_position(arguments[1]);
909
42
        const auto& [addr_column, add_col_const] =
910
42
                unpack_if_const(addr_column_with_type_and_name.column);
911
42
        const auto& [cidr_column, col_const] =
912
42
                unpack_if_const(cidr_column_with_type_and_name.column);
913
914
42
        const auto* cidr_col = assert_cast<const ColumnInt16*>(cidr_column.get());
915
42
        ColumnPtr col_res = nullptr;
916
917
42
        if (addr_column_with_type_and_name.type->get_primitive_type() == TYPE_IPV6) {
918
29
            const auto* ipv6_addr_column = assert_cast<const ColumnIPv6*>(addr_column.get());
919
29
            col_res = execute_impl(*ipv6_addr_column, *cidr_col, input_rows_count, add_col_const,
920
29
                                   col_const);
921
29
        } else if (is_string_type(addr_column_with_type_and_name.type->get_primitive_type())) {
922
13
            ColumnPtr col_ipv6 =
923
13
                    convert_to_ipv6<IPConvertExceptionMode::Throw>(addr_column, nullptr);
924
13
            const auto* ipv6_addr_column = assert_cast<const ColumnIPv6*>(col_ipv6.get());
925
13
            col_res = execute_impl(*ipv6_addr_column, *cidr_col, input_rows_count, add_col_const,
926
13
                                   col_const);
927
13
        } else {
928
0
            return Status::RuntimeError(
929
0
                    "Illegal column {} of argument of function {}, Expected IPv6 or String",
930
0
                    addr_column->get_name(), get_name());
931
0
        }
932
933
42
        block.replace_by_position(result, std::move(col_res));
934
42
        return Status::OK();
935
42
    }
936
937
    static ColumnPtr execute_impl(const ColumnIPv6& from_column, const ColumnInt16& cidr_column,
938
                                  size_t input_rows_count, bool is_addr_const = false,
939
38
                                  bool is_cidr_const = false) {
940
38
        auto col_res_lower_range = ColumnIPv6::create(input_rows_count, 0);
941
38
        auto col_res_upper_range = ColumnIPv6::create(input_rows_count, 0);
942
38
        auto& vec_res_lower_range = col_res_lower_range->get_data();
943
38
        auto& vec_res_upper_range = col_res_upper_range->get_data();
944
945
38
        static constexpr UInt8 max_cidr_mask = IPV6_BINARY_LENGTH * 8;
946
947
38
        if (is_addr_const) {
948
9
            for (size_t i = 0; i < input_rows_count; ++i) {
949
6
                auto cidr = cidr_column.get_int(i);
950
6
                if (cidr < 0 || cidr > max_cidr_mask) {
951
0
                    throw Exception(ErrorCode::INVALID_ARGUMENT, "Illegal cidr value '{}'",
952
0
                                    std::to_string(cidr));
953
0
                }
954
6
                apply_cidr_mask(from_column.get_data_at(0).data,
955
6
                                reinterpret_cast<char*>(&vec_res_lower_range[i]),
956
6
                                reinterpret_cast<char*>(&vec_res_upper_range[i]),
957
6
                                cast_set<UInt8>(cidr));
958
6
            }
959
35
        } else if (is_cidr_const) {
960
20
            auto cidr = cidr_column.get_int(0);
961
20
            if (cidr < 0 || cidr > max_cidr_mask) {
962
0
                throw Exception(ErrorCode::INVALID_ARGUMENT, "Illegal cidr value '{}'",
963
0
                                std::to_string(cidr));
964
0
            }
965
427
            for (size_t i = 0; i < input_rows_count; ++i) {
966
407
                apply_cidr_mask(from_column.get_data_at(i).data,
967
407
                                reinterpret_cast<char*>(&vec_res_lower_range[i]),
968
407
                                reinterpret_cast<char*>(&vec_res_upper_range[i]),
969
407
                                cast_set<UInt8>(cidr));
970
407
            }
971
20
        } else {
972
40
            for (size_t i = 0; i < input_rows_count; ++i) {
973
25
                auto cidr = cidr_column.get_int(i);
974
25
                if (cidr < 0 || cidr > max_cidr_mask) {
975
0
                    throw Exception(ErrorCode::INVALID_ARGUMENT, "Illegal cidr value '{}'",
976
0
                                    std::to_string(cidr));
977
0
                }
978
25
                apply_cidr_mask(from_column.get_data_at(i).data,
979
25
                                reinterpret_cast<char*>(&vec_res_lower_range[i]),
980
25
                                reinterpret_cast<char*>(&vec_res_upper_range[i]),
981
25
                                cast_set<UInt8>(cidr));
982
25
            }
983
15
        }
984
38
        return ColumnStruct::create(
985
38
                Columns {std::move(col_res_lower_range), std::move(col_res_upper_range)});
986
38
    }
987
};
988
989
class FunctionIsIPv4Compat : public IFunction {
990
public:
991
    static constexpr auto name = "is_ipv4_compat";
992
29
    static FunctionPtr create() { return std::make_shared<FunctionIsIPv4Compat>(); }
993
994
1
    String get_name() const override { return name; }
995
996
20
    size_t get_number_of_arguments() const override { return 1; }
997
998
20
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
999
20
        return std::make_shared<DataTypeUInt8>();
1000
20
    }
1001
1002
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
1003
20
                        uint32_t result, size_t input_rows_count) const override {
1004
20
        const ColumnPtr& column = block.get_by_position(arguments[0]).column;
1005
20
        const auto* col_in = assert_cast<const ColumnString*>(column.get());
1006
1007
20
        size_t col_size = col_in->size();
1008
20
        auto col_res = ColumnUInt8::create(col_size, 0);
1009
20
        auto& col_res_data = col_res->get_data();
1010
1011
231
        for (size_t i = 0; i < col_size; ++i) {
1012
211
            auto ipv4_in = col_in->get_data_at(i);
1013
211
            if (is_ipv4_compat(reinterpret_cast<const UInt8*>(ipv4_in.data))) {
1014
6
                col_res_data[i] = 1;
1015
6
            }
1016
211
        }
1017
1018
20
        block.replace_by_position(result, std::move(col_res));
1019
20
        return Status::OK();
1020
20
    }
1021
1022
private:
1023
211
    static bool is_ipv4_compat(const UInt8* address) {
1024
211
        return (LittleEndian::Load64(address) == 0) && (LittleEndian::Load32(address + 8) == 0) &&
1025
211
               (LittleEndian::Load32(address + 12) != 0);
1026
211
    }
1027
};
1028
1029
class FunctionIsIPv4Mapped : public IFunction {
1030
public:
1031
    static constexpr auto name = "is_ipv4_mapped";
1032
29
    static FunctionPtr create() { return std::make_shared<FunctionIsIPv4Mapped>(); }
1033
1034
1
    String get_name() const override { return name; }
1035
1036
20
    size_t get_number_of_arguments() const override { return 1; }
1037
1038
20
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1039
20
        return std::make_shared<DataTypeUInt8>();
1040
20
    }
1041
1042
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
1043
16
                        uint32_t result, size_t input_rows_count) const override {
1044
16
        const ColumnPtr& column = block.get_by_position(arguments[0]).column;
1045
16
        const auto* col_in = assert_cast<const ColumnString*>(column.get());
1046
1047
16
        size_t col_size = col_in->size();
1048
16
        auto col_res = ColumnUInt8::create(col_size, 0);
1049
16
        auto& col_res_data = col_res->get_data();
1050
1051
223
        for (size_t i = 0; i < col_size; ++i) {
1052
207
            auto ipv4_in = col_in->get_data_at(i);
1053
207
            if (is_ipv4_mapped(reinterpret_cast<const UInt8*>(ipv4_in.data))) {
1054
4
                col_res_data[i] = 1;
1055
4
            }
1056
207
        }
1057
1058
16
        block.replace_by_position(result, std::move(col_res));
1059
16
        return Status::OK();
1060
16
    }
1061
1062
private:
1063
207
    static bool is_ipv4_mapped(const UInt8* address) {
1064
207
        return (LittleEndian::Load64(address) == 0) &&
1065
207
               ((LittleEndian::Load64(address + 8) & 0x00000000FFFFFFFFULL) ==
1066
8
                0x00000000FFFF0000ULL);
1067
207
    }
1068
};
1069
1070
template <IPConvertExceptionMode exception_mode, PrimitiveType PType>
1071
0
inline constexpr auto to_ip_func_name() {
1072
0
    if constexpr (PType == TYPE_IPV4) {
1073
0
        return exception_mode == IPConvertExceptionMode::Throw
1074
0
                       ? "to_ipv4"
1075
0
                       : (exception_mode == IPConvertExceptionMode::Default ? "to_ipv4_or_default"
1076
0
                                                                            : "to_ipv4_or_null");
1077
0
    } else {
1078
0
        return exception_mode == IPConvertExceptionMode::Throw
1079
0
                       ? "to_ipv6"
1080
0
                       : (exception_mode == IPConvertExceptionMode::Default ? "to_ipv6_or_default"
1081
0
                                                                            : "to_ipv6_or_null");
1082
0
    }
1083
0
}
Unexecuted instantiation: _ZN5doris15to_ip_func_nameILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE36EEEDav
Unexecuted instantiation: _ZN5doris15to_ip_func_nameILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE36EEEDav
Unexecuted instantiation: _ZN5doris15to_ip_func_nameILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE36EEEDav
Unexecuted instantiation: _ZN5doris15to_ip_func_nameILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE37EEEDav
Unexecuted instantiation: _ZN5doris15to_ip_func_nameILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE37EEEDav
Unexecuted instantiation: _ZN5doris15to_ip_func_nameILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE37EEEDav
1084
1085
template <IPConvertExceptionMode exception_mode, PrimitiveType PType>
1086
class FunctionToIP : public IFunction {
1087
    static_assert(is_ip(PType));
1088
1089
public:
1090
    static constexpr auto name = to_ip_func_name<exception_mode, PType>();
1091
1092
119
    static FunctionPtr create() { return std::make_shared<FunctionToIP<exception_mode, PType>>(); }
_ZN5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE36EE6createEv
Line
Count
Source
1092
22
    static FunctionPtr create() { return std::make_shared<FunctionToIP<exception_mode, PType>>(); }
_ZN5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE36EE6createEv
Line
Count
Source
1092
18
    static FunctionPtr create() { return std::make_shared<FunctionToIP<exception_mode, PType>>(); }
_ZN5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE36EE6createEv
Line
Count
Source
1092
20
    static FunctionPtr create() { return std::make_shared<FunctionToIP<exception_mode, PType>>(); }
_ZN5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE37EE6createEv
Line
Count
Source
1092
22
    static FunctionPtr create() { return std::make_shared<FunctionToIP<exception_mode, PType>>(); }
_ZN5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE37EE6createEv
Line
Count
Source
1092
18
    static FunctionPtr create() { return std::make_shared<FunctionToIP<exception_mode, PType>>(); }
_ZN5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE37EE6createEv
Line
Count
Source
1092
19
    static FunctionPtr create() { return std::make_shared<FunctionToIP<exception_mode, PType>>(); }
1093
1094
12
    String get_name() const override { return name; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE36EE8get_nameB5cxx11Ev
Line
Count
Source
1094
3
    String get_name() const override { return name; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE36EE8get_nameB5cxx11Ev
Line
Count
Source
1094
1
    String get_name() const override { return name; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE36EE8get_nameB5cxx11Ev
Line
Count
Source
1094
1
    String get_name() const override { return name; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE37EE8get_nameB5cxx11Ev
Line
Count
Source
1094
5
    String get_name() const override { return name; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE37EE8get_nameB5cxx11Ev
Line
Count
Source
1094
1
    String get_name() const override { return name; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE37EE8get_nameB5cxx11Ev
Line
Count
Source
1094
1
    String get_name() const override { return name; }
1095
1096
65
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE36EE23get_number_of_argumentsEv
Line
Count
Source
1096
13
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE36EE23get_number_of_argumentsEv
Line
Count
Source
1096
9
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE36EE23get_number_of_argumentsEv
Line
Count
Source
1096
11
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE37EE23get_number_of_argumentsEv
Line
Count
Source
1096
13
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE37EE23get_number_of_argumentsEv
Line
Count
Source
1096
9
    size_t get_number_of_arguments() const override { return 1; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE37EE23get_number_of_argumentsEv
Line
Count
Source
1096
10
    size_t get_number_of_arguments() const override { return 1; }
1097
1098
65
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1099
65
        DataTypePtr result_type;
1100
1101
65
        if constexpr (PType == TYPE_IPV4) {
1102
33
            result_type = std::make_shared<DataTypeIPv4>();
1103
33
        } else {
1104
32
            result_type = std::make_shared<DataTypeIPv6>();
1105
32
        }
1106
1107
65
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1108
21
            return make_nullable(result_type);
1109
44
        } else {
1110
44
            return result_type;
1111
44
        }
1112
65
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE36EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS8_EE
Line
Count
Source
1098
13
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1099
13
        DataTypePtr result_type;
1100
1101
13
        if constexpr (PType == TYPE_IPV4) {
1102
13
            result_type = std::make_shared<DataTypeIPv4>();
1103
        } else {
1104
            result_type = std::make_shared<DataTypeIPv6>();
1105
        }
1106
1107
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1108
            return make_nullable(result_type);
1109
13
        } else {
1110
13
            return result_type;
1111
13
        }
1112
13
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE36EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS8_EE
Line
Count
Source
1098
9
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1099
9
        DataTypePtr result_type;
1100
1101
9
        if constexpr (PType == TYPE_IPV4) {
1102
9
            result_type = std::make_shared<DataTypeIPv4>();
1103
        } else {
1104
            result_type = std::make_shared<DataTypeIPv6>();
1105
        }
1106
1107
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1108
            return make_nullable(result_type);
1109
9
        } else {
1110
9
            return result_type;
1111
9
        }
1112
9
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE36EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS8_EE
Line
Count
Source
1098
11
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1099
11
        DataTypePtr result_type;
1100
1101
11
        if constexpr (PType == TYPE_IPV4) {
1102
11
            result_type = std::make_shared<DataTypeIPv4>();
1103
        } else {
1104
            result_type = std::make_shared<DataTypeIPv6>();
1105
        }
1106
1107
11
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1108
11
            return make_nullable(result_type);
1109
        } else {
1110
            return result_type;
1111
        }
1112
11
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE37EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS8_EE
Line
Count
Source
1098
13
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1099
13
        DataTypePtr result_type;
1100
1101
        if constexpr (PType == TYPE_IPV4) {
1102
            result_type = std::make_shared<DataTypeIPv4>();
1103
13
        } else {
1104
13
            result_type = std::make_shared<DataTypeIPv6>();
1105
13
        }
1106
1107
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1108
            return make_nullable(result_type);
1109
13
        } else {
1110
13
            return result_type;
1111
13
        }
1112
13
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE37EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS8_EE
Line
Count
Source
1098
9
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1099
9
        DataTypePtr result_type;
1100
1101
        if constexpr (PType == TYPE_IPV4) {
1102
            result_type = std::make_shared<DataTypeIPv4>();
1103
9
        } else {
1104
9
            result_type = std::make_shared<DataTypeIPv6>();
1105
9
        }
1106
1107
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1108
            return make_nullable(result_type);
1109
9
        } else {
1110
9
            return result_type;
1111
9
        }
1112
9
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE37EE20get_return_type_implERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS8_EE
Line
Count
Source
1098
10
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1099
10
        DataTypePtr result_type;
1100
1101
        if constexpr (PType == TYPE_IPV4) {
1102
            result_type = std::make_shared<DataTypeIPv4>();
1103
10
        } else {
1104
10
            result_type = std::make_shared<DataTypeIPv6>();
1105
10
        }
1106
1107
10
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1108
10
            return make_nullable(result_type);
1109
        } else {
1110
            return result_type;
1111
        }
1112
10
    }
1113
1114
262
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE36EE36use_default_implementation_for_nullsEv
Line
Count
Source
1114
36
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE36EE36use_default_implementation_for_nullsEv
Line
Count
Source
1114
44
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE36EE36use_default_implementation_for_nullsEv
Line
Count
Source
1114
55
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE37EE36use_default_implementation_for_nullsEv
Line
Count
Source
1114
34
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE37EE36use_default_implementation_for_nullsEv
Line
Count
Source
1114
44
    bool use_default_implementation_for_nulls() const override { return false; }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE37EE36use_default_implementation_for_nullsEv
Line
Count
Source
1114
49
    bool use_default_implementation_for_nulls() const override { return false; }
1115
1116
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
1117
196
                        uint32_t result, size_t input_rows_count) const override {
1118
196
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
1119
196
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
1120
196
        const ColumnString* str_addr_column = nullptr;
1121
196
        const NullMap* addr_null_map = nullptr;
1122
1123
196
        if (addr_column_with_type_and_name.type->is_nullable()) {
1124
143
            const auto* addr_column_nullable =
1125
143
                    assert_cast<const ColumnNullable*>(addr_column.get());
1126
143
            str_addr_column = assert_cast<const ColumnString*>(
1127
143
                    addr_column_nullable->get_nested_column_ptr().get());
1128
143
            addr_null_map = &addr_column_nullable->get_null_map_data();
1129
143
        } else {
1130
53
            str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
1131
53
        }
1132
1133
196
        auto col_res = ColumnVector<PType>::create(input_rows_count, 0);
1134
196
        auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
1135
196
        auto& col_res_data = col_res->get_data();
1136
196
        auto& res_null_map_data = res_null_map->get_data();
1137
1138
3.78k
        for (size_t i = 0; i < input_rows_count; ++i) {
1139
3.54k
            if (addr_null_map && (*addr_null_map)[i]) {
1140
57
                if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1141
6
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
1142
6
                                    "The arguments of function {} must be String, not NULL",
1143
6
                                    get_name());
1144
24
                } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1145
24
                    col_res_data[i] = 0; // '0.0.0.0' or '::'
1146
24
                    continue;
1147
27
                } else {
1148
27
                    res_null_map_data[i] = 1;
1149
27
                    continue;
1150
27
                }
1151
57
            }
1152
1153
1.79k
            if constexpr (PType == TYPE_IPV4) {
1154
1.79k
                StringRef ipv4_str = str_addr_column->get_data_at(i);
1155
1.79k
                IPv4 ipv4_val = 0;
1156
1.79k
                if (IPv4Value::from_string(ipv4_val, ipv4_str.data, ipv4_str.size)) {
1157
583
                    col_res_data[i] = ipv4_val;
1158
1.21k
                } else {
1159
1.21k
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1160
5
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value '{}'",
1161
5
                                        ipv4_str.to_string_view());
1162
602
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1163
602
                        col_res_data[i] = 0; // '0.0.0.0'
1164
604
                    } else {
1165
604
                        res_null_map_data[i] = 1;
1166
604
                    }
1167
1.21k
                }
1168
1.79k
            } else {
1169
1.74k
                StringRef ipv6_str = str_addr_column->get_data_at(i);
1170
1.74k
                IPv6 ipv6_val = 0;
1171
1.74k
                if (IPv6Value::from_string(ipv6_val, ipv6_str.data, ipv6_str.size)) {
1172
525
                    col_res_data[i] = ipv6_val;
1173
1.22k
                } else {
1174
1.22k
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1175
7
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value '{}'",
1176
7
                                        ipv6_str.to_string_view());
1177
607
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1178
607
                        col_res_data[i] = 0; // '::'
1179
608
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1180
608
                        res_null_map_data[i] = 1;
1181
608
                    }
1182
1.22k
                }
1183
1.74k
            }
1184
3.54k
        }
1185
1186
203
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1187
110
            block.replace_by_position(
1188
110
                    result, ColumnNullable::create(std::move(col_res), std::move(res_null_map)));
1189
137
        } else {
1190
137
            block.replace_by_position(result, std::move(col_res));
1191
137
        }
1192
1193
203
        return Status::OK();
1194
196
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE36EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
1117
23
                        uint32_t result, size_t input_rows_count) const override {
1118
23
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
1119
23
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
1120
23
        const ColumnString* str_addr_column = nullptr;
1121
23
        const NullMap* addr_null_map = nullptr;
1122
1123
23
        if (addr_column_with_type_and_name.type->is_nullable()) {
1124
14
            const auto* addr_column_nullable =
1125
14
                    assert_cast<const ColumnNullable*>(addr_column.get());
1126
14
            str_addr_column = assert_cast<const ColumnString*>(
1127
14
                    addr_column_nullable->get_nested_column_ptr().get());
1128
14
            addr_null_map = &addr_column_nullable->get_null_map_data();
1129
14
        } else {
1130
9
            str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
1131
9
        }
1132
1133
23
        auto col_res = ColumnVector<PType>::create(input_rows_count, 0);
1134
23
        auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
1135
23
        auto& col_res_data = col_res->get_data();
1136
23
        auto& res_null_map_data = res_null_map->get_data();
1137
1138
198
        for (size_t i = 0; i < input_rows_count; ++i) {
1139
175
            if (addr_null_map && (*addr_null_map)[i]) {
1140
2
                if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1141
2
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
1142
2
                                    "The arguments of function {} must be String, not NULL",
1143
2
                                    get_name());
1144
                } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1145
                    col_res_data[i] = 0; // '0.0.0.0' or '::'
1146
                    continue;
1147
                } else {
1148
                    res_null_map_data[i] = 1;
1149
                    continue;
1150
                }
1151
2
            }
1152
1153
175
            if constexpr (PType == TYPE_IPV4) {
1154
175
                StringRef ipv4_str = str_addr_column->get_data_at(i);
1155
175
                IPv4 ipv4_val = 0;
1156
175
                if (IPv4Value::from_string(ipv4_val, ipv4_str.data, ipv4_str.size)) {
1157
170
                    col_res_data[i] = ipv4_val;
1158
170
                } else {
1159
5
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1160
5
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value '{}'",
1161
5
                                        ipv4_str.to_string_view());
1162
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1163
                        col_res_data[i] = 0; // '0.0.0.0'
1164
                    } else {
1165
                        res_null_map_data[i] = 1;
1166
                    }
1167
5
                }
1168
            } else {
1169
                StringRef ipv6_str = str_addr_column->get_data_at(i);
1170
                IPv6 ipv6_val = 0;
1171
                if (IPv6Value::from_string(ipv6_val, ipv6_str.data, ipv6_str.size)) {
1172
                    col_res_data[i] = ipv6_val;
1173
                } else {
1174
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1175
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value '{}'",
1176
                                        ipv6_str.to_string_view());
1177
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1178
                        col_res_data[i] = 0; // '::'
1179
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1180
                        res_null_map_data[i] = 1;
1181
                    }
1182
                }
1183
            }
1184
175
        }
1185
1186
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1187
            block.replace_by_position(
1188
                    result, ColumnNullable::create(std::move(col_res), std::move(res_null_map)));
1189
23
        } else {
1190
23
            block.replace_by_position(result, std::move(col_res));
1191
23
        }
1192
1193
23
        return Status::OK();
1194
23
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE36EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
1117
34
                        uint32_t result, size_t input_rows_count) const override {
1118
34
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
1119
34
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
1120
34
        const ColumnString* str_addr_column = nullptr;
1121
34
        const NullMap* addr_null_map = nullptr;
1122
1123
34
        if (addr_column_with_type_and_name.type->is_nullable()) {
1124
26
            const auto* addr_column_nullable =
1125
26
                    assert_cast<const ColumnNullable*>(addr_column.get());
1126
26
            str_addr_column = assert_cast<const ColumnString*>(
1127
26
                    addr_column_nullable->get_nested_column_ptr().get());
1128
26
            addr_null_map = &addr_column_nullable->get_null_map_data();
1129
26
        } else {
1130
8
            str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
1131
8
        }
1132
1133
34
        auto col_res = ColumnVector<PType>::create(input_rows_count, 0);
1134
34
        auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
1135
34
        auto& col_res_data = col_res->get_data();
1136
34
        auto& res_null_map_data = res_null_map->get_data();
1137
1138
848
        for (size_t i = 0; i < input_rows_count; ++i) {
1139
802
            if (addr_null_map && (*addr_null_map)[i]) {
1140
                if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1141
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
1142
                                    "The arguments of function {} must be String, not NULL",
1143
                                    get_name());
1144
12
                } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1145
12
                    col_res_data[i] = 0; // '0.0.0.0' or '::'
1146
12
                    continue;
1147
                } else {
1148
                    res_null_map_data[i] = 1;
1149
                    continue;
1150
                }
1151
12
            }
1152
1153
802
            if constexpr (PType == TYPE_IPV4) {
1154
802
                StringRef ipv4_str = str_addr_column->get_data_at(i);
1155
802
                IPv4 ipv4_val = 0;
1156
802
                if (IPv4Value::from_string(ipv4_val, ipv4_str.data, ipv4_str.size)) {
1157
200
                    col_res_data[i] = ipv4_val;
1158
602
                } else {
1159
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1160
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value '{}'",
1161
                                        ipv4_str.to_string_view());
1162
602
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1163
602
                        col_res_data[i] = 0; // '0.0.0.0'
1164
                    } else {
1165
                        res_null_map_data[i] = 1;
1166
                    }
1167
602
                }
1168
            } else {
1169
                StringRef ipv6_str = str_addr_column->get_data_at(i);
1170
                IPv6 ipv6_val = 0;
1171
                if (IPv6Value::from_string(ipv6_val, ipv6_str.data, ipv6_str.size)) {
1172
                    col_res_data[i] = ipv6_val;
1173
                } else {
1174
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1175
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value '{}'",
1176
                                        ipv6_str.to_string_view());
1177
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1178
                        col_res_data[i] = 0; // '::'
1179
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1180
                        res_null_map_data[i] = 1;
1181
                    }
1182
                }
1183
            }
1184
802
        }
1185
1186
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1187
            block.replace_by_position(
1188
                    result, ColumnNullable::create(std::move(col_res), std::move(res_null_map)));
1189
46
        } else {
1190
46
            block.replace_by_position(result, std::move(col_res));
1191
46
        }
1192
1193
46
        return Status::OK();
1194
34
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE36EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
1117
44
                        uint32_t result, size_t input_rows_count) const override {
1118
44
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
1119
44
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
1120
44
        const ColumnString* str_addr_column = nullptr;
1121
44
        const NullMap* addr_null_map = nullptr;
1122
1123
44
        if (addr_column_with_type_and_name.type->is_nullable()) {
1124
36
            const auto* addr_column_nullable =
1125
36
                    assert_cast<const ColumnNullable*>(addr_column.get());
1126
36
            str_addr_column = assert_cast<const ColumnString*>(
1127
36
                    addr_column_nullable->get_nested_column_ptr().get());
1128
36
            addr_null_map = &addr_column_nullable->get_null_map_data();
1129
36
        } else {
1130
8
            str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
1131
8
        }
1132
1133
44
        auto col_res = ColumnVector<PType>::create(input_rows_count, 0);
1134
44
        auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
1135
44
        auto& col_res_data = col_res->get_data();
1136
44
        auto& res_null_map_data = res_null_map->get_data();
1137
1138
875
        for (size_t i = 0; i < input_rows_count; ++i) {
1139
817
            if (addr_null_map && (*addr_null_map)[i]) {
1140
                if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1141
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
1142
                                    "The arguments of function {} must be String, not NULL",
1143
                                    get_name());
1144
                } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1145
                    col_res_data[i] = 0; // '0.0.0.0' or '::'
1146
                    continue;
1147
14
                } else {
1148
14
                    res_null_map_data[i] = 1;
1149
14
                    continue;
1150
14
                }
1151
14
            }
1152
1153
817
            if constexpr (PType == TYPE_IPV4) {
1154
817
                StringRef ipv4_str = str_addr_column->get_data_at(i);
1155
817
                IPv4 ipv4_val = 0;
1156
817
                if (IPv4Value::from_string(ipv4_val, ipv4_str.data, ipv4_str.size)) {
1157
213
                    col_res_data[i] = ipv4_val;
1158
604
                } else {
1159
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1160
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value '{}'",
1161
                                        ipv4_str.to_string_view());
1162
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1163
                        col_res_data[i] = 0; // '0.0.0.0'
1164
604
                    } else {
1165
604
                        res_null_map_data[i] = 1;
1166
604
                    }
1167
604
                }
1168
            } else {
1169
                StringRef ipv6_str = str_addr_column->get_data_at(i);
1170
                IPv6 ipv6_val = 0;
1171
                if (IPv6Value::from_string(ipv6_val, ipv6_str.data, ipv6_str.size)) {
1172
                    col_res_data[i] = ipv6_val;
1173
                } else {
1174
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1175
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value '{}'",
1176
                                        ipv6_str.to_string_view());
1177
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1178
                        col_res_data[i] = 0; // '::'
1179
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1180
                        res_null_map_data[i] = 1;
1181
                    }
1182
                }
1183
            }
1184
817
        }
1185
1186
58
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1187
58
            block.replace_by_position(
1188
58
                    result, ColumnNullable::create(std::move(col_res), std::move(res_null_map)));
1189
        } else {
1190
            block.replace_by_position(result, std::move(col_res));
1191
        }
1192
1193
58
        return Status::OK();
1194
44
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE0ELNS_13PrimitiveTypeE37EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
1117
21
                        uint32_t result, size_t input_rows_count) const override {
1118
21
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
1119
21
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
1120
21
        const ColumnString* str_addr_column = nullptr;
1121
21
        const NullMap* addr_null_map = nullptr;
1122
1123
21
        if (addr_column_with_type_and_name.type->is_nullable()) {
1124
8
            const auto* addr_column_nullable =
1125
8
                    assert_cast<const ColumnNullable*>(addr_column.get());
1126
8
            str_addr_column = assert_cast<const ColumnString*>(
1127
8
                    addr_column_nullable->get_nested_column_ptr().get());
1128
8
            addr_null_map = &addr_column_nullable->get_null_map_data();
1129
13
        } else {
1130
13
            str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
1131
13
        }
1132
1133
21
        auto col_res = ColumnVector<PType>::create(input_rows_count, 0);
1134
21
        auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
1135
21
        auto& col_res_data = col_res->get_data();
1136
21
        auto& res_null_map_data = res_null_map->get_data();
1137
1138
155
        for (size_t i = 0; i < input_rows_count; ++i) {
1139
134
            if (addr_null_map && (*addr_null_map)[i]) {
1140
4
                if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1141
4
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
1142
4
                                    "The arguments of function {} must be String, not NULL",
1143
4
                                    get_name());
1144
                } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1145
                    col_res_data[i] = 0; // '0.0.0.0' or '::'
1146
                    continue;
1147
                } else {
1148
                    res_null_map_data[i] = 1;
1149
                    continue;
1150
                }
1151
4
            }
1152
1153
            if constexpr (PType == TYPE_IPV4) {
1154
                StringRef ipv4_str = str_addr_column->get_data_at(i);
1155
                IPv4 ipv4_val = 0;
1156
                if (IPv4Value::from_string(ipv4_val, ipv4_str.data, ipv4_str.size)) {
1157
                    col_res_data[i] = ipv4_val;
1158
                } else {
1159
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1160
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value '{}'",
1161
                                        ipv4_str.to_string_view());
1162
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1163
                        col_res_data[i] = 0; // '0.0.0.0'
1164
                    } else {
1165
                        res_null_map_data[i] = 1;
1166
                    }
1167
                }
1168
134
            } else {
1169
134
                StringRef ipv6_str = str_addr_column->get_data_at(i);
1170
134
                IPv6 ipv6_val = 0;
1171
134
                if (IPv6Value::from_string(ipv6_val, ipv6_str.data, ipv6_str.size)) {
1172
127
                    col_res_data[i] = ipv6_val;
1173
127
                } else {
1174
7
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1175
7
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value '{}'",
1176
7
                                        ipv6_str.to_string_view());
1177
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1178
                        col_res_data[i] = 0; // '::'
1179
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1180
                        res_null_map_data[i] = 1;
1181
                    }
1182
7
                }
1183
134
            }
1184
134
        }
1185
1186
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1187
            block.replace_by_position(
1188
                    result, ColumnNullable::create(std::move(col_res), std::move(res_null_map)));
1189
21
        } else {
1190
21
            block.replace_by_position(result, std::move(col_res));
1191
21
        }
1192
1193
21
        return Status::OK();
1194
21
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE1ELNS_13PrimitiveTypeE37EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
1117
35
                        uint32_t result, size_t input_rows_count) const override {
1118
35
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
1119
35
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
1120
35
        const ColumnString* str_addr_column = nullptr;
1121
35
        const NullMap* addr_null_map = nullptr;
1122
1123
35
        if (addr_column_with_type_and_name.type->is_nullable()) {
1124
28
            const auto* addr_column_nullable =
1125
28
                    assert_cast<const ColumnNullable*>(addr_column.get());
1126
28
            str_addr_column = assert_cast<const ColumnString*>(
1127
28
                    addr_column_nullable->get_nested_column_ptr().get());
1128
28
            addr_null_map = &addr_column_nullable->get_null_map_data();
1129
28
        } else {
1130
7
            str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
1131
7
        }
1132
1133
35
        auto col_res = ColumnVector<PType>::create(input_rows_count, 0);
1134
35
        auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
1135
35
        auto& col_res_data = col_res->get_data();
1136
35
        auto& res_null_map_data = res_null_map->get_data();
1137
1138
849
        for (size_t i = 0; i < input_rows_count; ++i) {
1139
802
            if (addr_null_map && (*addr_null_map)[i]) {
1140
                if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1141
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
1142
                                    "The arguments of function {} must be String, not NULL",
1143
                                    get_name());
1144
12
                } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1145
12
                    col_res_data[i] = 0; // '0.0.0.0' or '::'
1146
12
                    continue;
1147
                } else {
1148
                    res_null_map_data[i] = 1;
1149
                    continue;
1150
                }
1151
12
            }
1152
1153
            if constexpr (PType == TYPE_IPV4) {
1154
                StringRef ipv4_str = str_addr_column->get_data_at(i);
1155
                IPv4 ipv4_val = 0;
1156
                if (IPv4Value::from_string(ipv4_val, ipv4_str.data, ipv4_str.size)) {
1157
                    col_res_data[i] = ipv4_val;
1158
                } else {
1159
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1160
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value '{}'",
1161
                                        ipv4_str.to_string_view());
1162
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1163
                        col_res_data[i] = 0; // '0.0.0.0'
1164
                    } else {
1165
                        res_null_map_data[i] = 1;
1166
                    }
1167
                }
1168
802
            } else {
1169
802
                StringRef ipv6_str = str_addr_column->get_data_at(i);
1170
802
                IPv6 ipv6_val = 0;
1171
802
                if (IPv6Value::from_string(ipv6_val, ipv6_str.data, ipv6_str.size)) {
1172
195
                    col_res_data[i] = ipv6_val;
1173
607
                } else {
1174
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1175
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value '{}'",
1176
                                        ipv6_str.to_string_view());
1177
607
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1178
607
                        col_res_data[i] = 0; // '::'
1179
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1180
                        res_null_map_data[i] = 1;
1181
                    }
1182
607
                }
1183
802
            }
1184
802
        }
1185
1186
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1187
            block.replace_by_position(
1188
                    result, ColumnNullable::create(std::move(col_res), std::move(res_null_map)));
1189
47
        } else {
1190
47
            block.replace_by_position(result, std::move(col_res));
1191
47
        }
1192
1193
47
        return Status::OK();
1194
35
    }
_ZNK5doris12FunctionToIPILNS_22IPConvertExceptionModeE2ELNS_13PrimitiveTypeE37EE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjm
Line
Count
Source
1117
39
                        uint32_t result, size_t input_rows_count) const override {
1118
39
        const auto& addr_column_with_type_and_name = block.get_by_position(arguments[0]);
1119
39
        const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
1120
39
        const ColumnString* str_addr_column = nullptr;
1121
39
        const NullMap* addr_null_map = nullptr;
1122
1123
39
        if (addr_column_with_type_and_name.type->is_nullable()) {
1124
31
            const auto* addr_column_nullable =
1125
31
                    assert_cast<const ColumnNullable*>(addr_column.get());
1126
31
            str_addr_column = assert_cast<const ColumnString*>(
1127
31
                    addr_column_nullable->get_nested_column_ptr().get());
1128
31
            addr_null_map = &addr_column_nullable->get_null_map_data();
1129
31
        } else {
1130
8
            str_addr_column = assert_cast<const ColumnString*>(addr_column.get());
1131
8
        }
1132
1133
39
        auto col_res = ColumnVector<PType>::create(input_rows_count, 0);
1134
39
        auto res_null_map = ColumnUInt8::create(input_rows_count, 0);
1135
39
        auto& col_res_data = col_res->get_data();
1136
39
        auto& res_null_map_data = res_null_map->get_data();
1137
1138
863
        for (size_t i = 0; i < input_rows_count; ++i) {
1139
811
            if (addr_null_map && (*addr_null_map)[i]) {
1140
                if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1141
                    throw Exception(ErrorCode::INVALID_ARGUMENT,
1142
                                    "The arguments of function {} must be String, not NULL",
1143
                                    get_name());
1144
                } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1145
                    col_res_data[i] = 0; // '0.0.0.0' or '::'
1146
                    continue;
1147
13
                } else {
1148
13
                    res_null_map_data[i] = 1;
1149
13
                    continue;
1150
13
                }
1151
13
            }
1152
1153
            if constexpr (PType == TYPE_IPV4) {
1154
                StringRef ipv4_str = str_addr_column->get_data_at(i);
1155
                IPv4 ipv4_val = 0;
1156
                if (IPv4Value::from_string(ipv4_val, ipv4_str.data, ipv4_str.size)) {
1157
                    col_res_data[i] = ipv4_val;
1158
                } else {
1159
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1160
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv4 value '{}'",
1161
                                        ipv4_str.to_string_view());
1162
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1163
                        col_res_data[i] = 0; // '0.0.0.0'
1164
                    } else {
1165
                        res_null_map_data[i] = 1;
1166
                    }
1167
                }
1168
811
            } else {
1169
811
                StringRef ipv6_str = str_addr_column->get_data_at(i);
1170
811
                IPv6 ipv6_val = 0;
1171
811
                if (IPv6Value::from_string(ipv6_val, ipv6_str.data, ipv6_str.size)) {
1172
203
                    col_res_data[i] = ipv6_val;
1173
608
                } else {
1174
                    if constexpr (exception_mode == IPConvertExceptionMode::Throw) {
1175
                        throw Exception(ErrorCode::INVALID_ARGUMENT, "Invalid IPv6 value '{}'",
1176
                                        ipv6_str.to_string_view());
1177
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Default) {
1178
                        col_res_data[i] = 0; // '::'
1179
608
                    } else if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1180
608
                        res_null_map_data[i] = 1;
1181
608
                    }
1182
608
                }
1183
811
            }
1184
811
        }
1185
1186
52
        if constexpr (exception_mode == IPConvertExceptionMode::Null) {
1187
52
            block.replace_by_position(
1188
52
                    result, ColumnNullable::create(std::move(col_res), std::move(res_null_map)));
1189
        } else {
1190
            block.replace_by_position(result, std::move(col_res));
1191
        }
1192
1193
52
        return Status::OK();
1194
39
    }
1195
};
1196
1197
class FunctionIPv4ToIPv6 : public IFunction {
1198
public:
1199
    static constexpr auto name = "ipv4_to_ipv6";
1200
16
    static FunctionPtr create() { return std::make_shared<FunctionIPv4ToIPv6>(); }
1201
1202
1
    String get_name() const override { return name; }
1203
1204
7
    size_t get_number_of_arguments() const override { return 1; }
1205
1206
7
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1207
7
        return std::make_shared<DataTypeIPv6>();
1208
7
    }
1209
1210
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
1211
14
                        uint32_t result, size_t input_rows_count) const override {
1212
14
        const auto& ipv4_column_with_type_and_name = block.get_by_position(arguments[0]);
1213
14
        const auto& [ipv4_column, ipv4_const] =
1214
14
                unpack_if_const(ipv4_column_with_type_and_name.column);
1215
14
        const auto* ipv4_addr_column = assert_cast<const ColumnIPv4*>(ipv4_column.get());
1216
14
        const auto& ipv4_column_data = ipv4_addr_column->get_data();
1217
14
        auto col_res = ColumnIPv6::create(input_rows_count, 0);
1218
14
        auto& col_res_data = col_res->get_data();
1219
1220
48
        for (size_t i = 0; i < input_rows_count; ++i) {
1221
34
            auto ipv4_idx = index_check_const(i, ipv4_const);
1222
34
            map_ipv4_to_ipv6(ipv4_column_data[ipv4_idx],
1223
34
                             reinterpret_cast<UInt8*>(&col_res_data[i]));
1224
34
        }
1225
1226
14
        block.replace_by_position(result, std::move(col_res));
1227
14
        return Status::OK();
1228
14
    }
1229
};
1230
1231
class FunctionCutIPv6 : public IFunction {
1232
public:
1233
    static constexpr auto name = "cut_ipv6";
1234
13
    static FunctionPtr create() { return std::make_shared<FunctionCutIPv6>(); }
1235
1236
1
    String get_name() const override { return name; }
1237
1238
4
    size_t get_number_of_arguments() const override { return 3; }
1239
1240
4
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1241
4
        return std::make_shared<DataTypeString>();
1242
4
    }
1243
1244
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
1245
9
                        uint32_t result, size_t input_rows_count) const override {
1246
9
        const auto& ipv6_column_with_type_and_name = block.get_by_position(arguments[0]);
1247
9
        const auto& bytes_to_cut_for_ipv6_column_with_type_and_name =
1248
9
                block.get_by_position(arguments[1]);
1249
9
        const auto& bytes_to_cut_for_ipv4_column_with_type_and_name =
1250
9
                block.get_by_position(arguments[2]);
1251
1252
9
        const auto& [ipv6_column, ipv6_const] =
1253
9
                unpack_if_const(ipv6_column_with_type_and_name.column);
1254
9
        const auto& [bytes_to_cut_for_ipv6_column, bytes_to_cut_for_ipv6_const] =
1255
9
                unpack_if_const(bytes_to_cut_for_ipv6_column_with_type_and_name.column);
1256
9
        const auto& [bytes_to_cut_for_ipv4_column, bytes_to_cut_for_ipv4_const] =
1257
9
                unpack_if_const(bytes_to_cut_for_ipv4_column_with_type_and_name.column);
1258
1259
9
        const auto* ipv6_addr_column = assert_cast<const ColumnIPv6*>(ipv6_column.get());
1260
9
        const auto* to_cut_for_ipv6_bytes_column =
1261
9
                assert_cast<const ColumnInt8*>(bytes_to_cut_for_ipv6_column.get());
1262
9
        const auto* to_cut_for_ipv4_bytes_column =
1263
9
                assert_cast<const ColumnInt8*>(bytes_to_cut_for_ipv4_column.get());
1264
1265
9
        const auto& ipv6_addr_column_data = ipv6_addr_column->get_data();
1266
9
        const auto& to_cut_for_ipv6_bytes_column_data = to_cut_for_ipv6_bytes_column->get_data();
1267
9
        const auto& to_cut_for_ipv4_bytes_column_data = to_cut_for_ipv4_bytes_column->get_data();
1268
1269
9
        auto col_res = ColumnString::create();
1270
9
        ColumnString::Chars& chars_res = col_res->get_chars();
1271
9
        ColumnString::Offsets& offsets_res = col_res->get_offsets();
1272
9
        chars_res.resize(input_rows_count * (IPV6_MAX_TEXT_LENGTH + 1)); // + 1 for ending '\0'
1273
9
        offsets_res.resize(input_rows_count);
1274
9
        auto* begin = reinterpret_cast<char*>(chars_res.data());
1275
9
        auto* pos = begin;
1276
1277
36
        for (size_t i = 0; i < input_rows_count; ++i) {
1278
27
            auto ipv6_idx = index_check_const(i, ipv6_const);
1279
27
            auto bytes_to_cut_for_ipv6_idx = index_check_const(i, bytes_to_cut_for_ipv6_const);
1280
27
            auto bytes_to_cut_for_ipv4_idx = index_check_const(i, bytes_to_cut_for_ipv4_const);
1281
            // the current function logic is processed in big endian manner
1282
            // But ipv6 in doris is stored in little-endian byte order
1283
            // need transfer to big-endian byte order first, so we can't deal this process in column
1284
27
            auto val_128 = ipv6_addr_column_data[ipv6_idx];
1285
27
            auto* address = reinterpret_cast<unsigned char*>(&val_128);
1286
1287
27
            Int8 bytes_to_cut_for_ipv6_count =
1288
27
                    to_cut_for_ipv6_bytes_column_data[bytes_to_cut_for_ipv6_idx];
1289
27
            Int8 bytes_to_cut_for_ipv4_count =
1290
27
                    to_cut_for_ipv4_bytes_column_data[bytes_to_cut_for_ipv4_idx];
1291
1292
27
            if (bytes_to_cut_for_ipv6_count > IPV6_BINARY_LENGTH) [[unlikely]] {
1293
0
                throw Exception(ErrorCode::INVALID_ARGUMENT,
1294
0
                                "Illegal value for argument 2 {} of function {}",
1295
0
                                bytes_to_cut_for_ipv6_column_with_type_and_name.type->get_name(),
1296
0
                                get_name());
1297
0
            }
1298
1299
27
            if (bytes_to_cut_for_ipv4_count > IPV6_BINARY_LENGTH) [[unlikely]] {
1300
0
                throw Exception(ErrorCode::INVALID_ARGUMENT,
1301
0
                                "Illegal value for argument 3 {} of function {}",
1302
0
                                bytes_to_cut_for_ipv4_column_with_type_and_name.type->get_name(),
1303
0
                                get_name());
1304
0
            }
1305
1306
27
            UInt8 bytes_to_cut_count = is_ipv4_mapped(address) ? bytes_to_cut_for_ipv4_count
1307
27
                                                               : bytes_to_cut_for_ipv6_count;
1308
27
            cut_address(address, pos, bytes_to_cut_count);
1309
27
            offsets_res[i] = cast_set<uint32_t>(pos - begin);
1310
27
        }
1311
1312
9
        chars_res.resize(offsets_res[offsets_res.size() - 1]);
1313
1314
9
        block.replace_by_position(result, std::move(col_res));
1315
9
        return Status::OK();
1316
9
    }
1317
1318
private:
1319
27
    static bool is_ipv4_mapped(const UInt8* address) {
1320
27
        return (LittleEndian::Load64(address + 8) == 0) &&
1321
27
               ((LittleEndian::Load64(address) & 0xFFFFFFFF00000000ULL) == 0x0000FFFF00000000ULL);
1322
27
    }
1323
1324
27
    static void cut_address(unsigned char* address, char*& dst, UInt8 zeroed_tail_bytes_count) {
1325
27
        format_ipv6(address, dst, zeroed_tail_bytes_count);
1326
27
    }
1327
};
1328
1329
class FunctionIPv6FromUInt128StringOrNull : public IFunction {
1330
public:
1331
    static constexpr auto name = "ipv6_from_uint128_string_or_null";
1332
11
    static FunctionPtr create() { return std::make_shared<FunctionIPv6FromUInt128StringOrNull>(); }
1333
1334
1
    String get_name() const override { return name; }
1335
1336
2
    size_t get_number_of_arguments() const override { return 1; }
1337
1338
2
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
1339
2
        return std::make_shared<DataTypeNullable>(std::make_shared<DataTypeIPv6>());
1340
2
    }
1341
1342
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
1343
6
                        uint32_t result, size_t input_rows_count) const override {
1344
6
        const auto& ipv6_column_with_type_and_name = block.get_by_position(arguments[0]);
1345
6
        const auto& [ipv6_column, ipv6_const] =
1346
6
                unpack_if_const(ipv6_column_with_type_and_name.column);
1347
6
        const auto* ipv6_addr_column = assert_cast<const ColumnString*>(ipv6_column.get());
1348
        // result is nullable column
1349
6
        auto col_res_nested = ColumnIPv6::create(input_rows_count, 0);
1350
6
        auto col_res_null_map = ColumnUInt8::create(input_rows_count, 1);
1351
6
        auto& col_res_data = col_res_nested->get_data();
1352
6
        auto& res_null_map_data = col_res_null_map->get_data();
1353
1354
216
        for (size_t i = 0; i < input_rows_count; ++i) {
1355
210
            IPv6 ipv6 = 0;
1356
210
            auto ipv6_idx = index_check_const(i, ipv6_const);
1357
210
            StringRef uint128_string = ipv6_addr_column->get_data_at(ipv6_idx);
1358
210
            if (!IPv6Value::from_uint128_string(ipv6, uint128_string.data, uint128_string.size)) {
1359
0
                VLOG_DEBUG << "Invalid uin128 IPv6 value '" << uint128_string.to_string_view()
1360
0
                           << "'";
1361
                // we should set null to the result not throw exception for load senior
1362
210
            } else {
1363
210
                col_res_data[i] = ipv6;
1364
210
                res_null_map_data[i] = 0;
1365
210
            }
1366
210
        }
1367
1368
6
        block.replace_by_position(result, ColumnNullable::create(std::move(col_res_nested),
1369
6
                                                                 std::move(col_res_null_map)));
1370
6
        return Status::OK();
1371
6
    }
1372
};
1373
1374
} // namespace doris