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