Fix cast into IPv4, IPv6 address in IN section

This commit is contained in:
Maksim Kita 2022-03-23 15:22:08 +01:00
parent f6d9687174
commit ff2d5dae83
3 changed files with 17 additions and 2 deletions

View File

@ -3506,9 +3506,13 @@ private:
/// 'requested_result_is_nullable' is true if CAST to Nullable type is requested.
WrapperType prepareImpl(const DataTypePtr & from_type, const DataTypePtr & to_type, bool requested_result_is_nullable) const
{
bool convert_to_ipv6 = to_type->getCustomName() && to_type->getCustomName()->getName() == "IPv6";
/// We can cast IPv6 into IPv6, IPv4 into IPv4, but we should not allow to cast FixedString(16) into IPv6 as part of identity cast
bool safe_convert_into_custom_type = true;
if (from_type->equals(*to_type) && !convert_to_ipv6)
if (const auto * to_type_custom_name = to_type->getCustomName())
safe_convert_into_custom_type = from_type->getCustomName() && from_type->getCustomName()->getName() == to_type_custom_name->getName();
if (from_type->equals(*to_type) && safe_convert_into_custom_type)
{
if (isUInt8(from_type))
return createUInt8ToUInt8Wrapper(from_type, to_type);

View File

@ -0,0 +1,2 @@
0
0

View File

@ -0,0 +1,9 @@
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table (id UInt64, value_ipv4 IPv4, value_ipv6 IPv6) ENGINE=MergeTree ORDER BY id;
INSERT INTO test_table VALUES (0, '127.0.0.1', '127.0.0.1');
SELECT id FROM test_table WHERE value_ipv4 IN (SELECT value_ipv4 FROM test_table);
SELECT id FROM test_table WHERE value_ipv6 IN (SELECT value_ipv6 FROM test_table);
DROP TABLE test_table;