From 2c8fd5dbb143729050d7f2b245c51991dced59db Mon Sep 17 00:00:00 2001 From: Bharat Nallan Chakravarthy Date: Sat, 2 Sep 2023 14:12:49 -0700 Subject: [PATCH] fix possible type mismatch with IPv4 --- src/Core/Field.h | 2 +- src/Interpreters/convertFieldToType.cpp | 5 +++++ .../02864_test_ipv4_type_mismatch.reference | 8 ++++++++ .../0_stateless/02864_test_ipv4_type_mismatch.sql | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/queries/0_stateless/02864_test_ipv4_type_mismatch.reference create mode 100644 tests/queries/0_stateless/02864_test_ipv4_type_mismatch.sql diff --git a/src/Core/Field.h b/src/Core/Field.h index 12542ca0bf1..5e4ee0bca27 100644 --- a/src/Core/Field.h +++ b/src/Core/Field.h @@ -871,7 +871,7 @@ NearestFieldType> & Field::get() // Disregard signedness when converting between int64 types. constexpr Field::Types::Which target = TypeToEnum::value; if (target != which - && (!isInt64OrUInt64orBoolFieldType(target) || !isInt64OrUInt64orBoolFieldType(which))) + && (!isInt64OrUInt64orBoolFieldType(target) || !isInt64OrUInt64orBoolFieldType(which)) && target != Field::Types::IPv4) throw Exception(ErrorCodes::LOGICAL_ERROR, "Invalid Field get from type {} to type {}", which, target); #endif diff --git a/src/Interpreters/convertFieldToType.cpp b/src/Interpreters/convertFieldToType.cpp index 0e810748ab1..4e38103ac1f 100644 --- a/src/Interpreters/convertFieldToType.cpp +++ b/src/Interpreters/convertFieldToType.cpp @@ -283,6 +283,11 @@ Field convertFieldToTypeImpl(const Field & src, const IDataType & type, const ID /// Already in needed type. return src; } + if (which_type.isIPv4() && src.getType() == Field::Types::UInt64) + { + /// convert to UInt32 which is the underlying type for native IPv4 + return convertNumericType(src, type); + } } else if (which_type.isUUID() && src.getType() == Field::Types::UUID) { diff --git a/tests/queries/0_stateless/02864_test_ipv4_type_mismatch.reference b/tests/queries/0_stateless/02864_test_ipv4_type_mismatch.reference new file mode 100644 index 00000000000..9eafc3a5816 --- /dev/null +++ b/tests/queries/0_stateless/02864_test_ipv4_type_mismatch.reference @@ -0,0 +1,8 @@ +1.1.1.1 +8.8.8.8 +1 +0 +1 +0 +0 +1 diff --git a/tests/queries/0_stateless/02864_test_ipv4_type_mismatch.sql b/tests/queries/0_stateless/02864_test_ipv4_type_mismatch.sql new file mode 100644 index 00000000000..20d0976afd1 --- /dev/null +++ b/tests/queries/0_stateless/02864_test_ipv4_type_mismatch.sql @@ -0,0 +1,14 @@ +DROP TABLE IF EXISTS test; + +CREATE TABLE test +( + ip IPv4 Codec(ZSTD(6)), +) ENGINE MergeTree() order by ip; + +INSERT INTO test values ('1.1.1.1'); +INSERT INTO test values (toIPv4('8.8.8.8')); + +SELECT * FROM test ORDER BY ip; +SELECT ip IN IPv4StringToNum('1.1.1.1') FROM test order by ip; +SELECT ip IN ('1.1.1.1') FROM test order by ip; +SELECT ip IN IPv4StringToNum('8.8.8.8') FROM test order by ip;