diff --git a/src/Core/Field.h b/src/Core/Field.h index 4edd4da3571c..e77217abc03d 100644 --- a/src/Core/Field.h +++ b/src/Core/Field.h @@ -870,7 +870,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 0e810748ab16..4e38103ac1f2 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 000000000000..9eafc3a58163 --- /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 000000000000..20d0976afd1e --- /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;