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