This is an automated email from the ASF dual-hosted git repository.
Mryange pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 3c489e297d9 [fix](function) Handle zero-address IPv4 CIDR correctly
(#68081)
3c489e297d9 is described below
commit 3c489e297d95191c26e7259a9939c2fabb3510cb
Author: Mryange <[email protected]>
AuthorDate: Thu Sep 17 09:55:54 2026 +0800
[fix](function) Handle zero-address IPv4 CIDR correctly (#68081)
`is_ip_address_in_range` used the numeric IPv4 value returned by
`as_v4()` as an address-family check. Because `0.0.0.0` is represented
as zero, valid CIDRs such as `0.0.0.0/0` and `0.0.0.0/32` were treated
as non-IPv4 values and returned incorrect results. This change adds
explicit IPv4 and IPv6 type predicates to `IPAddressVariant` and uses
them in both regular function execution and inverted-index evaluation.
### Release note
None
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [ ] Regression test
- [ ] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [ ] Yes. <!-- Explain the behavior change -->
- Does this need documentation?
- [ ] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR
should merge into -->
---
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 b7b9e0a5ac5..f559def8201 100644
--- a/be/src/core/value/ip_address_cidr.h
+++ b/be/src/core/value/ip_address_cidr.h
@@ -85,6 +85,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();
@@ -92,6 +94,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>;
@@ -175,7 +179,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 f09825a0033..a734f55881e 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 4ac9671dfbd..bfb367c9da8 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) {
@@ -224,7 +246,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]