Skip to content

Commit

Permalink
[Bug](function) fix is_ip_address_in_range function parse error throw…
Browse files Browse the repository at this point in the history
… exception (#45657)

### What problem does this PR solve?
Problem Summary:
before: will be throw exception when parse NULL value, as the input is
empty invalid.
so need check firstly and then parse it.

```
mysql [test]>select * from ip_test;
+------+------------------+
| id   | data             |
+------+------------------+
|   54 | 2001:db8:4::/128 |
|   55 | NULL             |
+------+------------------+
2 rows in set (0.07 sec)

mysql [test]>SELECT  data,   IS_IP_ADDRESS_IN_RANGE(CAST('0.0.0.1' AS STRING), data) FROM ip_test;
ERROR 1105 (HY000): errCode = 2, detailMessage = (10.16.10.8)[INVALID_ARGUMENT][E33] The text does not contain '/':

```
  • Loading branch information
zhangstar333 authored Dec 20, 2024
1 parent 034085a commit 0ab4eae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
23 changes: 16 additions & 7 deletions be/src/vec/functions/function_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,13 @@ class FunctionIsIPAddressInRange : public IFunction {
for (size_t i = 0; i < input_rows_count; ++i) {
auto addr_idx = index_check_const(i, addr_const);
auto cidr_idx = index_check_const(i, cidr_const);
const auto cidr =
parse_ip_with_cidr(str_cidr_column->get_data_at(cidr_idx).to_string_view());
auto cidr_data = str_cidr_column->get_data_at(cidr_idx);
// cidr_data maybe NULL, But the input column is nested column, so check here avoid throw exception
if (cidr_data.data == nullptr || cidr_data.size == 0) {
col_res_data[i] = 0;
continue;
}
const auto cidr = parse_ip_with_cidr(cidr_data.to_string_view());
if constexpr (PT == PrimitiveType::TYPE_IPV4) {
if (cidr._address.as_v4()) {
col_res_data[i] = match_ipv4_subnet(ip_data[addr_idx], cidr._address.as_v4(),
Expand Down Expand Up @@ -775,11 +780,15 @@ class FunctionIsIPAddressInRange : public IFunction {
for (size_t i = 0; i < input_rows_count; ++i) {
auto addr_idx = index_check_const(i, addr_const);
auto cidr_idx = index_check_const(i, cidr_const);

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());
auto addr_data = str_addr_column->get_data_at(addr_idx);
auto cidr_data = str_cidr_column->get_data_at(cidr_idx);
// cidr_data maybe NULL, But the input column is nested column, so check here avoid throw exception
if (cidr_data.data == nullptr || cidr_data.size == 0) {
col_res_data[i] = 0;
continue;
}
const auto addr = IPAddressVariant(addr_data.to_string_view());
const auto cidr = parse_ip_with_cidr(cidr_data.to_string_view());
col_res_data[i] = is_address_in_range(addr, cidr) ? 1 : 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,11 @@
-- !sql --
\N

-- !sql1 --
54 2001:db8:4::/128
55 \N

-- !sql2 --
\N \N
2001:db8:4::/128 false

Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ suite("test_is_ip_address_in_range_function") {
qt_sql "SELECT is_ip_address_in_range(NULL, '::ffff:192.168.0.4/128')"

qt_sql "SELECT is_ip_address_in_range(NULL, NULL)"


sql """ DROP TABLE IF EXISTS ip_test """
sql """ CREATE TABLE IF NOT EXISTS ip_test(id INT, data string) DISTRIBUTED BY HASH(id) BUCKETS 1 PROPERTIES ('replication_num' = '1');"""
sql """ INSERT INTO ip_test values (54, '2001:db8:4::/128'); """
sql """ INSERT INTO ip_test values (55, NULL); """
qt_sql1 """ select * from ip_test order by 1; """
qt_sql2 "SELECT data, IS_IP_ADDRESS_IN_RANGE(CAST('0.0.0.1' AS STRING), data) FROM ip_test order by 1;"

}

0 comments on commit 0ab4eae

Please sign in to comment.