Skip to content

Commit

Permalink
Merge pull request ClickHouse#54212 from bharatnc/ncb/fix-ipv4-select
Browse files Browse the repository at this point in the history
fix possible type mismatch with IPv4
  • Loading branch information
CurtizJ authored Sep 4, 2023
2 parents 2ca2bdd + 2c8fd5d commit 15cb333
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Core/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ NearestFieldType<std::decay_t<T>> & Field::get()
// Disregard signedness when converting between int64 types.
constexpr Field::Types::Which target = TypeToEnum<StoredType>::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
Expand Down
5 changes: 5 additions & 0 deletions src/Interpreters/convertFieldToType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt32>(src, type);
}
}
else if (which_type.isUUID() && src.getType() == Field::Types::UUID)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1.1.1.1
8.8.8.8
1
0
1
0
0
1
14 changes: 14 additions & 0 deletions tests/queries/0_stateless/02864_test_ipv4_type_mismatch.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 15cb333

Please sign in to comment.