IP dictionary works with aliased types IPv4 and IPv6 (close #6804)

This commit is contained in:
vdimir 2020-11-28 23:11:15 +03:00
parent 8d91bf1713
commit 5f330a9366
No known key found for this signature in database
GPG Key ID: 4F25F52AFAF0C2C0
3 changed files with 12 additions and 3 deletions

View File

@ -112,9 +112,12 @@ static void validateKeyTypes(const DataTypes & key_types)
if (key_types.empty() || key_types.size() > 2) if (key_types.empty() || key_types.size() > 2)
throw Exception{"Expected a single IP address or IP with mask", ErrorCodes::TYPE_MISMATCH}; throw Exception{"Expected a single IP address or IP with mask", ErrorCodes::TYPE_MISMATCH};
const auto & actual_type = key_types[0]->getName(); const auto * key_ipv4type = typeid_cast<const DataTypeUInt32 *>(key_types[0].get());
if (actual_type != "UInt32" && actual_type != "FixedString(16)") const auto * key_ipv6type = typeid_cast<const DataTypeFixedString *>(key_types[0].get());
throw Exception{"Key does not match, expected either UInt32 or FixedString(16)", ErrorCodes::TYPE_MISMATCH};
if (key_ipv4type == nullptr && (key_ipv6type == nullptr || key_ipv6type->getN() != 16))
throw Exception{"Key does not match, expected either `IPv4` (`UInt32`) or `IPv6` (`FixedString(16)`)",
ErrorCodes::TYPE_MISMATCH};
if (key_types.size() > 1) if (key_types.size() > 1)
{ {

View File

@ -50,6 +50,8 @@
1 1
1 1
1 1
1
1
***ipv4 trie dict mask*** ***ipv4 trie dict mask***
1 1
1 1

View File

@ -95,6 +95,10 @@ SELECT 11212 == dictGetUInt32('database_for_dict.dict_ipv4_trie', 'asn', tuple(I
SELECT 11211 == dictGetUInt32('database_for_dict.dict_ipv4_trie', 'asn', tuple(IPv4StringToNum('202.79.32.2'))); SELECT 11211 == dictGetUInt32('database_for_dict.dict_ipv4_trie', 'asn', tuple(IPv4StringToNum('202.79.32.2')));
-- check that dictionary works with aliased types `IPv4` and `IPv6`
SELECT 11211 == dictGetUInt32('database_for_dict.dict_ipv4_trie', 'asn', tuple(toIPv4('202.79.32.2')));
SELECT 11212 == dictGetUInt32('database_for_dict.dict_ipv4_trie', 'asn', tuple(toIPv6('::ffff:101.79.55.22')));
CREATE TABLE database_for_dict.table_from_ipv4_trie_dict CREATE TABLE database_for_dict.table_from_ipv4_trie_dict
( (
prefix String, prefix String,