This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new 3c53410236e branch-4.1: [fix](function) Handle zero-address IPv4 CIDR
correctly #68081 (#68420)
3c53410236e is described below
commit 3c53410236ed909aef1a2667ef8b05cb4b8f5195
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Thu Sep 24 09:24:18 2026 +0800
branch-4.1: [fix](function) Handle zero-address IPv4 CIDR correctly #68081
(#68420)
Cherry-picked from #68081
Co-authored-by: Mryange <[email protected]>
---
be/src/core/value/ip_address_cidr.h | 6 +++++-
be/src/exprs/function/function_ip.h | 8 ++++----
be/test/exprs/function/function_ip_test.cpp | 24 +++++++++++++++++++++++-
3 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/be/src/core/value/ip_address_cidr.h
b/be/src/core/value/ip_address_cidr.h
index b096ed55985..0ddb119eca7 100644
--- a/be/src/core/value/ip_address_cidr.h
+++ b/be/src/core/value/ip_address_cidr.h
@@ -86,6 +86,8 @@ public:
return 0;
}
+ bool is_v4() const { return std::holds_alternative<IPv4AddrType>(_addr); }
+
const UInt8* as_v6() const {
if (const auto* val = std::get_if<IPv6AddrType>(&_addr)) {
return val->data();
@@ -93,6 +95,8 @@ public:
return nullptr;
}
+ bool is_v6() const { return std::holds_alternative<IPv6AddrType>(_addr); }
+
private:
using IPv4AddrType = UInt32;
using IPv6AddrType = std::array<UInt8, IPV6_BINARY_LENGTH>;
@@ -176,7 +180,7 @@ inline IPAddressCIDR parse_ip_with_cidr(std::string_view
cidr_str) {
const auto* prefix_str_end = prefix_str.data() + prefix_str.size();
auto [parse_end, parse_error] = std::from_chars(prefix_str.data(),
prefix_str_end, prefix);
- uint8_t max_prefix = (addr.as_v6() ? IPV6_BINARY_LENGTH :
IPV4_BINARY_LENGTH) * 8;
+ uint8_t max_prefix = (addr.is_v6() ? IPV6_BINARY_LENGTH :
IPV4_BINARY_LENGTH) * 8;
if (parse_error != std::errc() || parse_end != prefix_str_end || prefix >
max_prefix) {
throw Exception(ErrorCode::INVALID_ARGUMENT, "The CIDR has a malformed
prefix bits: {}",
diff --git a/be/src/exprs/function/function_ip.h
b/be/src/exprs/function/function_ip.h
index 3278caa5e46..2dba40459e6 100644
--- a/be/src/exprs/function/function_ip.h
+++ b/be/src/exprs/function/function_ip.h
@@ -623,7 +623,7 @@ public:
}
const auto cidr = parse_ip_with_cidr(cidr_data.to_string_view());
if constexpr (PT == PrimitiveType::TYPE_IPV4) {
- if (cidr._address.as_v4()) {
+ if (cidr._address.is_v4()) {
col_res_data[i] = match_ipv4_subnet(ip_data[addr_idx],
cidr._address.as_v4(),
cidr._prefix)
? 1
@@ -632,7 +632,7 @@ public:
col_res_data[i] = 0;
}
} else if constexpr (PT == PrimitiveType::TYPE_IPV6) {
- if (cidr._address.as_v6()) {
+ if (cidr._address.is_v6()) {
col_res_data[i] =
match_ipv6_subnet((uint8_t*)(&ip_data[addr_idx]),
cidr._address.as_v6(),
cidr._prefix)
? 1
@@ -687,12 +687,12 @@ public:
Field min_ip, max_ip;
IPAddressCIDR cidr = parse_ip_with_cidr(arg_column->get_data_at(0));
if (data_type_with_name.second->get_primitive_type() == TYPE_IPV4 &&
- cidr._address.as_v4()) {
+ cidr._address.is_v4()) {
auto range = apply_cidr_mask(cidr._address.as_v4(), cidr._prefix);
min_ip = Field::create_field<TYPE_IPV4>(range.first);
max_ip = Field::create_field<TYPE_IPV4>(range.second);
} else if (data_type_with_name.second->get_primitive_type() ==
TYPE_IPV6 &&
- cidr._address.as_v6()) {
+ cidr._address.is_v6()) {
auto cidr_range_ipv6_col = ColumnIPv6::create(2, 0);
auto& cidr_range_ipv6_data = cidr_range_ipv6_col->get_data();
apply_cidr_mask(reinterpret_cast<const
char*>(cidr._address.as_v6()),
diff --git a/be/test/exprs/function/function_ip_test.cpp
b/be/test/exprs/function/function_ip_test.cpp
index 1ee32398c1c..97733bbe5ef 100644
--- a/be/test/exprs/function/function_ip_test.cpp
+++ b/be/test/exprs/function/function_ip_test.cpp
@@ -28,6 +28,17 @@
namespace doris {
+TEST(FunctionIpTest, IPAddressVariantTypeTest) {
+ IPAddressVariant ipv4_zero("0.0.0.0");
+ EXPECT_TRUE(ipv4_zero.is_v4());
+ EXPECT_FALSE(ipv4_zero.is_v6());
+ EXPECT_EQ(ipv4_zero.as_v4(), 0);
+
+ IPAddressVariant ipv6_zero("::");
+ EXPECT_FALSE(ipv6_zero.is_v4());
+ EXPECT_TRUE(ipv6_zero.is_v6());
+}
+
TEST(FunctionIpTest, FunctionIsIPAddressInRangeTest) {
std::string func_name = "is_ip_address_in_range";
@@ -86,6 +97,17 @@ TEST(FunctionIpTest, FunctionIsIPAddressInRangeTest) {
const_addr_dataset));
}
}
+
+ {
+ DataSet ipv4_data_set = {
+ {{static_cast<IPv4>(0), std::string("0.0.0.0/0")},
static_cast<uint8_t>(1)},
+ {{static_cast<IPv4>(0), std::string("0.0.0.0/32")},
static_cast<uint8_t>(1)},
+ {{static_cast<IPv4>(1), std::string("0.0.0.0/32")},
static_cast<uint8_t>(0)},
+ };
+ InputTypeSet input_types = {PrimitiveType::TYPE_IPV4,
PrimitiveType::TYPE_VARCHAR};
+ static_cast<void>(
+ check_function<DataTypeUInt8, true>(func_name, input_types,
ipv4_data_set));
+ }
}
TEST(FunctionIpTest, FunctionIPv4ToIPv6Test) {
@@ -235,7 +257,7 @@ TEST(FunctionIpTest, evaluate_inverted_index) {
// IPv4 test
{
auto cidr_col = ColumnString::create();
- cidr_col->insert_data("127.0.0.0/8", 11);
+ cidr_col->insert_data("0.0.0.0/0", 9);
auto const_cidr_col = ColumnConst::create(std::move(cidr_col), 1);
ColumnsWithTypeAndName arguments = {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]