[FIX]fix is_ip_address_in_range func with const param (#34266)

This commit is contained in:
yangshijie
2024-05-07 15:03:58 +08:00
committed by yiguolei
parent 520774a24b
commit 9b712b03b4
4 changed files with 175 additions and 6 deletions

View File

@ -672,8 +672,10 @@ public:
const auto& cidr_column_with_type_and_name = block.get_by_position(arguments[1]);
WhichDataType addr_type(addr_column_with_type_and_name.type);
WhichDataType cidr_type(cidr_column_with_type_and_name.type);
const ColumnPtr& addr_column = addr_column_with_type_and_name.column;
const ColumnPtr& cidr_column = cidr_column_with_type_and_name.column;
const auto& [addr_column, addr_const] =
unpack_if_const(addr_column_with_type_and_name.column);
const auto& [cidr_column, cidr_const] =
unpack_if_const(cidr_column_with_type_and_name.column);
const ColumnString* str_addr_column = nullptr;
const ColumnString* str_cidr_column = nullptr;
const NullMap* null_map_addr = nullptr;
@ -715,18 +717,22 @@ public:
auto& col_res_data = col_res->get_data();
for (size_t i = 0; i < input_rows_count; ++i) {
if (null_map_addr && (*null_map_addr)[i]) {
auto addr_idx = index_check_const(i, addr_const);
auto cidr_idx = index_check_const(i, cidr_const);
if (null_map_addr && (*null_map_addr)[addr_idx]) {
throw Exception(ErrorCode::INVALID_ARGUMENT,
"The arguments of function {} must be String, not NULL",
get_name());
}
if (null_map_cidr && (*null_map_cidr)[i]) {
if (null_map_cidr && (*null_map_cidr)[cidr_idx]) {
throw Exception(ErrorCode::INVALID_ARGUMENT,
"The arguments of function {} must be String, not NULL",
get_name());
}
const auto addr = IPAddressVariant(str_addr_column->get_data_at(i).to_string_view());
const auto cidr = parse_ip_with_cidr(str_cidr_column->get_data_at(i).to_string_view());
const auto addr =
IPAddressVariant(str_addr_column->get_data_at(addr_idx).to_string_view());
const auto cidr =
parse_ip_with_cidr(str_cidr_column->get_data_at(cidr_idx).to_string_view());
col_res_data[i] = is_address_in_range(addr, cidr) ? 1 : 0;
}

View File

@ -0,0 +1,83 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "function_test_util.h"
#include "gtest/gtest_pred_impl.h"
#include "vec/core/types.h"
#include "vec/data_types/data_type_number.h"
namespace doris::vectorized {
TEST(FunctionIpTest, FunctionIsIPAddressInRangeTest) {
std::string func_name = "is_ip_address_in_range";
DataSet data_set = {
{{std::string("127.0.0.1"), std::string("127.0.0.0/8")}, (uint8_t)1},
{{std::string("128.0.0.1"), std::string("127.0.0.0/8")}, (uint8_t)0},
{{std::string("ffff::1"), std::string("ffff::/16")}, (uint8_t)1},
{{std::string("fffe::1"), std::string("ffff::/16")}, (uint8_t)0},
{{std::string("192.168.99.255"), std::string("192.168.100.0/22")}, (uint8_t)0},
{{std::string("192.168.100.1"), std::string("192.168.100.0/22")}, (uint8_t)1},
{{std::string("192.168.103.255"), std::string("192.168.100.0/22")}, (uint8_t)1},
{{std::string("192.168.104.0"), std::string("192.168.100.0/22")}, (uint8_t)0},
{{std::string("::192.168.99.255"), std::string("::192.168.100.0/118")}, (uint8_t)0},
{{std::string("::192.168.100.1"), std::string("::192.168.100.0/118")}, (uint8_t)1},
{{std::string("::192.168.103.255"), std::string("::192.168.100.0/118")}, (uint8_t)1},
{{std::string("::192.168.104.0"), std::string("::192.168.100.0/118")}, (uint8_t)0},
{{std::string("192.168.100.1"), std::string("192.168.100.0/22")}, (uint8_t)1},
{{std::string("192.168.100.1"), std::string("192.168.100.0/24")}, (uint8_t)1},
{{std::string("192.168.100.1"), std::string("192.168.100.0/32")}, (uint8_t)0},
{{std::string("::192.168.100.1"), std::string("::192.168.100.0/118")}, (uint8_t)1},
{{std::string("::192.168.100.1"), std::string("::192.168.100.0/120")}, (uint8_t)1},
{{std::string("::192.168.100.1"), std::string("::192.168.100.0/128")}, (uint8_t)0},
{{std::string("192.168.100.1"), std::string("192.168.100.0/22")}, (uint8_t)1},
{{std::string("192.168.103.255"), std::string("192.168.100.0/24")}, (uint8_t)0},
{{std::string("::192.168.100.1"), std::string("::192.168.100.0/118")}, (uint8_t)1},
{{std::string("::192.168.103.255"), std::string("::192.168.100.0/120")}, (uint8_t)0},
{{std::string("127.0.0.1"), std::string("ffff::/16")}, (uint8_t)0},
{{std::string("127.0.0.1"), std::string("::127.0.0.1/128")}, (uint8_t)0},
{{std::string("::1"), std::string("127.0.0.0/8")}, (uint8_t)0},
{{std::string("::127.0.0.1"), std::string("127.0.0.1/32")}, (uint8_t)0}};
{
// vector vs vector
InputTypeSet input_types = {TypeIndex::String, TypeIndex::String};
static_cast<void>(check_function<DataTypeUInt8, false>(func_name, input_types, data_set));
}
{
// vector vs scalar
InputTypeSet input_types = {TypeIndex::String, Consted {TypeIndex::String}};
for (const auto& line : data_set) {
DataSet const_cidr_dataset = {line};
static_cast<void>(check_function<DataTypeUInt8, false>(func_name, input_types,
const_cidr_dataset));
}
}
{
// scalar vs vector
InputTypeSet input_types = {Consted {TypeIndex::String}, TypeIndex::String};
for (const auto& line : data_set) {
DataSet const_addr_dataset = {line};
static_cast<void>(check_function<DataTypeUInt8, false>(func_name, input_types,
const_addr_dataset));
}
}
}
} // namespace doris::vectorized

View File

@ -25,3 +25,59 @@
24 false
25 false
26 false
-- !sql --
1 false
2 false
3 false
4 false
5 false
6 true
7 false
8 false
9 false
10 false
11 false
12 false
13 true
14 true
15 true
16 false
17 false
18 false
19 true
20 false
21 false
22 false
23 false
24 false
25 false
26 false
-- !sql --
1 false
2 false
3 false
4 false
5 true
6 true
7 true
8 true
9 false
10 false
11 false
12 false
13 true
14 true
15 true
16 false
17 false
18 false
19 true
20 true
21 false
22 false
23 false
24 false
25 false
26 false

View File

@ -64,7 +64,31 @@ suite("test_is_ip_address_in_range_function") {
(26, '::127.0.0.1', '127.0.0.1/32')
"""
// vector vs vector
qt_sql "select id, is_ip_address_in_range(addr, cidr) from test_is_ip_address_in_range_function order by id"
// vector vs scalar
qt_sql "select id, is_ip_address_in_range(addr, '192.168.100.0/24') from test_is_ip_address_in_range_function order by id"
// scalar vs vector
qt_sql "select id, is_ip_address_in_range('192.168.100.0', cidr) from test_is_ip_address_in_range_function order by id"
test {
sql "SELECT is_ip_address_in_range('::ffff:192.168.0.1', NULL)"
// check exception message contains
exception "The arguments of function is_ip_address_in_range must be String, not NULL"
}
test {
sql "SELECT is_ip_address_in_range(NULL, '::ffff:192.168.0.4/128')"
// check exception message contains
exception "The arguments of function is_ip_address_in_range must be String, not NULL"
}
test {
sql "SELECT is_ip_address_in_range(NULL, NULL)"
// check exception message contains
exception "The arguments of function is_ip_address_in_range must be String, not NULL"
}
sql """ DROP TABLE IF EXISTS test_is_ip_address_in_range_function """
}