2020-11-21 18:38:10 +00:00
|
|
|
#include "IPAddressDictionary.h"
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <stack>
|
2020-11-21 14:56:58 +00:00
|
|
|
#include <charconv>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2020-11-08 16:01:12 +00:00
|
|
|
#include <Common/IPv6ToBinary.h>
|
2020-11-11 21:58:30 +00:00
|
|
|
#include <Common/memcmpSmall.h>
|
|
|
|
#include <Common/memcpySmall.h>
|
2020-11-08 20:50:49 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-06-05 09:02:05 +00:00
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
2017-06-13 05:14:24 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2017-06-05 09:02:05 +00:00
|
|
|
#include <IO/WriteIntText.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <Poco/ByteOrder.h>
|
2017-06-13 05:14:24 +00:00
|
|
|
#include <Common/formatIPv6.h>
|
2018-12-29 18:39:55 +00:00
|
|
|
#include <common/itoa.h>
|
2018-12-10 15:25:45 +00:00
|
|
|
#include <ext/map.h>
|
|
|
|
#include <ext/range.h>
|
|
|
|
#include "DictionaryBlockInputStream.h"
|
2018-11-28 11:37:12 +00:00
|
|
|
#include "DictionaryFactory.h"
|
2017-05-12 21:23:12 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2017-05-12 21:23:12 +00:00
|
|
|
extern const int TYPE_MISMATCH;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
extern const int DICTIONARY_IS_EMPTY;
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-11-15 17:09:28 +00:00
|
|
|
/// Intermediate structure that are used in loading procedure
|
2020-11-11 21:58:30 +00:00
|
|
|
struct IPRecord
|
|
|
|
{
|
|
|
|
Poco::Net::IPAddress addr;
|
|
|
|
UInt8 prefix;
|
|
|
|
size_t row;
|
2020-11-15 14:36:05 +00:00
|
|
|
bool isv6;
|
|
|
|
|
|
|
|
IPRecord(const Poco::Net::IPAddress & addr_, UInt8 prefix_, size_t row_)
|
|
|
|
: addr(addr_)
|
|
|
|
, prefix(prefix_)
|
|
|
|
, row(row_)
|
|
|
|
, isv6(addr.family() == Poco::Net::IPAddress::IPv6)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t * asIPv6Binary(uint8_t * buf) const
|
|
|
|
{
|
|
|
|
if (isv6)
|
|
|
|
return reinterpret_cast<const uint8_t *>(addr.addr());
|
|
|
|
memset(buf, 0, 10);
|
|
|
|
buf[10] = '\xFF';
|
|
|
|
buf[11] = '\xFF';
|
|
|
|
memcpy(&buf[12], addr.addr(), 4);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2020-11-15 17:09:28 +00:00
|
|
|
|
|
|
|
inline UInt8 prefixIPv6() const
|
|
|
|
{
|
|
|
|
return isv6 ? prefix : prefix + 96;
|
|
|
|
}
|
2020-11-11 21:58:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct IPv4Subnet
|
|
|
|
{
|
|
|
|
UInt32 addr;
|
|
|
|
UInt8 prefix;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IPv6Subnet
|
|
|
|
{
|
|
|
|
const uint8_t * addr;
|
|
|
|
UInt8 prefix;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-21 14:56:58 +00:00
|
|
|
static std::pair<Poco::Net::IPAddress, UInt8> parseIPFromString(const std::string_view addr_str)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
size_t pos = addr_str.find('/');
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
Poco::Net::IPAddress addr{std::string(addr_str.substr(0, pos))};
|
|
|
|
|
2020-11-21 17:39:16 +00:00
|
|
|
uint8_t prefix = 0;
|
|
|
|
const auto * addr_str_end = addr_str.data() + addr_str.size();
|
2020-11-21 14:56:58 +00:00
|
|
|
auto [p, ec] = std::from_chars(addr_str.data() + pos + 1, addr_str_end, prefix);
|
|
|
|
if (p != addr_str_end)
|
|
|
|
throw DB::Exception("extra characters at the end", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
if (ec != std::errc())
|
|
|
|
throw DB::Exception("mask is not a valid number", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
addr = addr & Poco::Net::IPAddress(prefix, addr.family());
|
|
|
|
return {addr, prefix};
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Net::IPAddress addr{std::string(addr_str)};
|
|
|
|
return {addr, addr.length() * 8};
|
|
|
|
}
|
|
|
|
catch (Poco::Exception & ex)
|
|
|
|
{
|
|
|
|
throw DB::Exception("can't parse address \"" + std::string(addr_str) + "\": " + ex.what(),
|
|
|
|
ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 00:57:00 +00:00
|
|
|
static void validateKeyTypes(const DataTypes & key_types)
|
|
|
|
{
|
2020-11-12 18:47:10 +00:00
|
|
|
if (key_types.empty() || key_types.size() > 2)
|
2020-11-11 21:58:30 +00:00
|
|
|
throw Exception{"Expected a single IP address or IP with mask", ErrorCodes::TYPE_MISMATCH};
|
|
|
|
|
|
|
|
const auto & actual_type = key_types[0]->getName();
|
|
|
|
if (actual_type != "UInt32" && actual_type != "FixedString(16)")
|
|
|
|
throw Exception{"Key does not match, expected either UInt32 or FixedString(16)", ErrorCodes::TYPE_MISMATCH};
|
2020-03-18 00:57:00 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
if (key_types.size() > 1)
|
2020-11-08 20:50:49 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
const auto * mask_col_type = typeid_cast<const DataTypeUInt8 *>(key_types[1].get());
|
|
|
|
if (mask_col_type == nullptr)
|
|
|
|
throw Exception{"Mask do not match, expected UInt8", ErrorCodes::TYPE_MISMATCH};
|
2020-11-08 20:50:49 +00:00
|
|
|
}
|
2020-11-11 21:58:30 +00:00
|
|
|
}
|
2020-03-18 00:57:00 +00:00
|
|
|
|
2020-11-16 07:43:55 +00:00
|
|
|
template <typename T, typename Comp>
|
2020-11-21 14:56:58 +00:00
|
|
|
size_t sortAndUnique(std::vector<T> & vec, Comp comp)
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
2020-11-16 07:43:55 +00:00
|
|
|
std::sort(vec.begin(), vec.end(),
|
|
|
|
[&](const auto & a, const auto & b) { return comp(a, b) < 0; });
|
|
|
|
|
|
|
|
auto new_end = std::unique(vec.begin(), vec.end(),
|
|
|
|
[&](const auto & a, const auto & b) { return comp(a, b) == 0; });
|
2020-11-21 14:56:58 +00:00
|
|
|
size_t deleted_count = std::distance(new_end, vec.end());
|
|
|
|
vec.erase(new_end, vec.end());
|
|
|
|
return deleted_count;
|
2020-11-16 07:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static inline int compareTo(T a, T b)
|
|
|
|
{
|
|
|
|
return a > b ? 1 : (a < b ? -1 : 0);
|
2020-03-18 00:57:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 14:36:05 +00:00
|
|
|
inline static UInt32 IPv4AsUInt32(const void * addr)
|
|
|
|
{
|
|
|
|
return Poco::ByteOrder::fromNetwork(*reinterpret_cast<const UInt32 *>(addr));
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
/// Convert mapped IPv6 to IPv4 if possible
|
|
|
|
inline static UInt32 mappedIPv4ToBinary(const uint8_t * addr, bool & success)
|
2020-11-08 16:01:12 +00:00
|
|
|
{
|
2020-11-12 07:37:27 +00:00
|
|
|
success = addr[0] == 0x0 && addr[1] == 0x0 &&
|
|
|
|
addr[2] == 0x0 && addr[3] == 0x0 &&
|
|
|
|
addr[4] == 0x0 && addr[5] == 0x0 &&
|
|
|
|
addr[6] == 0x0 && addr[7] == 0x0 &&
|
|
|
|
addr[8] == 0x0 && addr[9] == 0x0 &&
|
|
|
|
addr[10] == 0xff && addr[11] == 0xff;
|
2020-11-11 21:58:30 +00:00
|
|
|
if (!success)
|
|
|
|
return 0;
|
2020-11-15 14:36:05 +00:00
|
|
|
return IPv4AsUInt32(&addr[12]);
|
2020-11-11 21:58:30 +00:00
|
|
|
}
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
/// Convert IPv4 to IPv6-mapped and save results to buf
|
|
|
|
inline static void mapIPv4ToIPv6(UInt32 addr, uint8_t * buf)
|
|
|
|
{
|
|
|
|
memset(buf, 0, 10);
|
|
|
|
buf[10] = '\xFF';
|
|
|
|
buf[11] = '\xFF';
|
|
|
|
addr = Poco::ByteOrder::toNetwork(addr);
|
2020-11-21 14:56:58 +00:00
|
|
|
memcpy(&buf[12], &addr, 4);
|
2020-11-11 21:58:30 +00:00
|
|
|
}
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
static bool matchIPv4Subnet(UInt32 target, UInt32 addr, UInt8 prefix)
|
|
|
|
{
|
2020-11-21 14:56:58 +00:00
|
|
|
UInt32 mask = (prefix >= 32) ? 0xffffffffu : ~(0xffffffffu >> prefix);
|
2020-11-11 21:58:30 +00:00
|
|
|
return (target & mask) == addr;
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:32:58 +00:00
|
|
|
#if defined(__SSE2__)
|
|
|
|
#include <emmintrin.h>
|
|
|
|
|
|
|
|
static bool matchIPv6Subnet(const uint8_t * target, const uint8_t * addr, UInt8 prefix)
|
|
|
|
{
|
|
|
|
uint16_t mask = _mm_movemask_epi8(_mm_cmpeq_epi8(
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(target)),
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(addr))));
|
|
|
|
mask = ~mask;
|
|
|
|
|
2020-11-21 14:56:58 +00:00
|
|
|
if (mask)
|
2020-11-14 20:32:58 +00:00
|
|
|
{
|
|
|
|
auto offset = __builtin_ctz(mask);
|
|
|
|
|
2020-11-15 14:36:05 +00:00
|
|
|
if (prefix / 8 != offset)
|
|
|
|
return prefix / 8 < offset;
|
2020-11-14 20:32:58 +00:00
|
|
|
|
|
|
|
auto cmpmask = ~(0xff >> (prefix % 8));
|
|
|
|
return (target[offset] & cmpmask) == addr[offset];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
# else
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
static bool matchIPv6Subnet(const uint8_t * target, const uint8_t * addr, UInt8 prefix)
|
|
|
|
{
|
2020-11-12 18:47:10 +00:00
|
|
|
if (prefix > IPV6_BINARY_LENGTH * 8U)
|
|
|
|
prefix = IPV6_BINARY_LENGTH * 8U;
|
2020-11-11 21:58:30 +00:00
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (; prefix >= 8; ++i, prefix -= 8)
|
|
|
|
{
|
|
|
|
if (target[i] != addr[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (prefix == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto mask = ~(0xff >> prefix);
|
2020-11-14 20:29:58 +00:00
|
|
|
return (target[i] & mask) == addr[i];
|
2020-11-11 21:58:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 20:32:58 +00:00
|
|
|
#endif // __SSE2__
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
IPAddressDictionary::IPAddressDictionary(
|
2020-07-14 18:46:29 +00:00
|
|
|
const StorageID & dict_id_,
|
2019-08-03 11:02:40 +00:00
|
|
|
const DictionaryStructure & dict_struct_,
|
|
|
|
DictionarySourcePtr source_ptr_,
|
|
|
|
const DictionaryLifetime dict_lifetime_,
|
|
|
|
bool require_nonempty_)
|
2020-07-14 18:46:29 +00:00
|
|
|
: IDictionaryBase(dict_id_)
|
2019-08-03 11:02:40 +00:00
|
|
|
, dict_struct(dict_struct_)
|
|
|
|
, source_ptr{std::move(source_ptr_)}
|
|
|
|
, dict_lifetime(dict_lifetime_)
|
|
|
|
, require_nonempty(require_nonempty_)
|
2020-11-21 18:38:10 +00:00
|
|
|
, logger(&Poco::Logger::get("IPAddressDictionary"))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
createAttributes();
|
2020-11-11 21:58:30 +00:00
|
|
|
|
2020-07-08 10:55:39 +00:00
|
|
|
loadData();
|
|
|
|
calculateBytesAllocated();
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:50:58 +00:00
|
|
|
#define DECLARE(TYPE) \
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::get##TYPE( \
|
2018-12-10 15:25:45 +00:00
|
|
|
const std::string & attribute_name, const Columns & key_columns, const DataTypes & key_types, ResultArrayType<TYPE> & out) const \
|
2018-12-10 15:50:58 +00:00
|
|
|
{ \
|
|
|
|
validateKeyTypes(key_types); \
|
|
|
|
\
|
|
|
|
const auto & attribute = getAttribute(attribute_name); \
|
2020-07-14 18:46:54 +00:00
|
|
|
checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::ut##TYPE); \
|
2018-12-10 15:50:58 +00:00
|
|
|
\
|
|
|
|
const auto null_value = std::get<TYPE>(attribute.null_values); \
|
|
|
|
\
|
2019-05-28 19:30:01 +00:00
|
|
|
getItemsImpl<TYPE, TYPE>( \
|
2018-12-10 15:50:58 +00:00
|
|
|
attribute, \
|
|
|
|
key_columns, \
|
|
|
|
[&](const size_t row, const auto value) { out[row] = value; }, \
|
|
|
|
[&](const size_t) { return null_value; }); \
|
2018-12-10 15:25:45 +00:00
|
|
|
}
|
2017-05-12 21:23:12 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
2017-11-14 00:08:54 +00:00
|
|
|
DECLARE(UInt128)
|
2017-05-12 21:23:12 +00:00
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
2018-10-08 19:45:17 +00:00
|
|
|
DECLARE(Decimal32)
|
|
|
|
DECLARE(Decimal64)
|
|
|
|
DECLARE(Decimal128)
|
2017-05-12 21:23:12 +00:00
|
|
|
#undef DECLARE
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::getString(
|
2018-12-10 15:25:45 +00:00
|
|
|
const std::string & attribute_name, const Columns & key_columns, const DataTypes & key_types, ColumnString * out) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
validateKeyTypes(key_types);
|
|
|
|
|
|
|
|
const auto & attribute = getAttribute(attribute_name);
|
2020-07-14 18:46:54 +00:00
|
|
|
checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::utString);
|
2017-05-12 21:23:12 +00:00
|
|
|
|
|
|
|
const auto & null_value = StringRef{std::get<String>(attribute.null_values)};
|
|
|
|
|
2018-12-10 15:25:45 +00:00
|
|
|
getItemsImpl<StringRef, StringRef>(
|
|
|
|
attribute,
|
|
|
|
key_columns,
|
|
|
|
[&](const size_t, const StringRef value) { out->insertData(value.data, value.size); },
|
|
|
|
[&](const size_t) { return null_value; });
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:50:58 +00:00
|
|
|
#define DECLARE(TYPE) \
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::get##TYPE( \
|
2018-12-10 15:50:58 +00:00
|
|
|
const std::string & attribute_name, \
|
|
|
|
const Columns & key_columns, \
|
|
|
|
const DataTypes & key_types, \
|
|
|
|
const PaddedPODArray<TYPE> & def, \
|
|
|
|
ResultArrayType<TYPE> & out) const \
|
|
|
|
{ \
|
|
|
|
validateKeyTypes(key_types); \
|
|
|
|
\
|
|
|
|
const auto & attribute = getAttribute(attribute_name); \
|
2020-07-14 18:46:54 +00:00
|
|
|
checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::ut##TYPE); \
|
2018-12-10 15:50:58 +00:00
|
|
|
\
|
2019-05-28 19:30:01 +00:00
|
|
|
getItemsImpl<TYPE, TYPE>( \
|
2018-12-10 15:50:58 +00:00
|
|
|
attribute, \
|
|
|
|
key_columns, \
|
|
|
|
[&](const size_t row, const auto value) { out[row] = value; }, \
|
|
|
|
[&](const size_t row) { return def[row]; }); \
|
2018-12-10 15:25:45 +00:00
|
|
|
}
|
2017-05-12 21:23:12 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
2017-11-14 00:08:54 +00:00
|
|
|
DECLARE(UInt128)
|
2017-05-12 21:23:12 +00:00
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
2018-10-08 19:45:17 +00:00
|
|
|
DECLARE(Decimal32)
|
|
|
|
DECLARE(Decimal64)
|
|
|
|
DECLARE(Decimal128)
|
2017-05-12 21:23:12 +00:00
|
|
|
#undef DECLARE
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::getString(
|
2018-12-10 15:25:45 +00:00
|
|
|
const std::string & attribute_name,
|
|
|
|
const Columns & key_columns,
|
|
|
|
const DataTypes & key_types,
|
|
|
|
const ColumnString * const def,
|
|
|
|
ColumnString * const out) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
validateKeyTypes(key_types);
|
|
|
|
|
|
|
|
const auto & attribute = getAttribute(attribute_name);
|
2020-07-14 18:46:54 +00:00
|
|
|
checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::utString);
|
2018-12-10 15:25:45 +00:00
|
|
|
|
|
|
|
getItemsImpl<StringRef, StringRef>(
|
|
|
|
attribute,
|
|
|
|
key_columns,
|
|
|
|
[&](const size_t, const StringRef value) { out->insertData(value.data, value.size); },
|
|
|
|
[&](const size_t row) { return def->getDataAt(row); });
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 15:50:58 +00:00
|
|
|
#define DECLARE(TYPE) \
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::get##TYPE( \
|
2018-12-10 15:50:58 +00:00
|
|
|
const std::string & attribute_name, \
|
|
|
|
const Columns & key_columns, \
|
|
|
|
const DataTypes & key_types, \
|
|
|
|
const TYPE def, \
|
|
|
|
ResultArrayType<TYPE> & out) const \
|
|
|
|
{ \
|
|
|
|
validateKeyTypes(key_types); \
|
|
|
|
\
|
|
|
|
const auto & attribute = getAttribute(attribute_name); \
|
2020-07-14 18:46:54 +00:00
|
|
|
checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::ut##TYPE); \
|
2018-12-10 15:50:58 +00:00
|
|
|
\
|
2019-05-28 19:30:01 +00:00
|
|
|
getItemsImpl<TYPE, TYPE>( \
|
2018-12-10 15:25:45 +00:00
|
|
|
attribute, key_columns, [&](const size_t row, const auto value) { out[row] = value; }, [&](const size_t) { return def; }); \
|
|
|
|
}
|
2017-05-12 21:23:12 +00:00
|
|
|
DECLARE(UInt8)
|
|
|
|
DECLARE(UInt16)
|
|
|
|
DECLARE(UInt32)
|
|
|
|
DECLARE(UInt64)
|
2017-11-14 00:08:54 +00:00
|
|
|
DECLARE(UInt128)
|
2017-05-12 21:23:12 +00:00
|
|
|
DECLARE(Int8)
|
|
|
|
DECLARE(Int16)
|
|
|
|
DECLARE(Int32)
|
|
|
|
DECLARE(Int64)
|
|
|
|
DECLARE(Float32)
|
|
|
|
DECLARE(Float64)
|
2018-10-08 19:45:17 +00:00
|
|
|
DECLARE(Decimal32)
|
|
|
|
DECLARE(Decimal64)
|
|
|
|
DECLARE(Decimal128)
|
2017-05-12 21:23:12 +00:00
|
|
|
#undef DECLARE
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::getString(
|
2018-12-10 15:25:45 +00:00
|
|
|
const std::string & attribute_name,
|
|
|
|
const Columns & key_columns,
|
|
|
|
const DataTypes & key_types,
|
|
|
|
const String & def,
|
|
|
|
ColumnString * const out) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
validateKeyTypes(key_types);
|
|
|
|
|
|
|
|
const auto & attribute = getAttribute(attribute_name);
|
2020-07-14 18:46:54 +00:00
|
|
|
checkAttributeType(this, attribute_name, attribute.type, AttributeUnderlyingType::utString);
|
2018-12-10 15:25:45 +00:00
|
|
|
|
|
|
|
getItemsImpl<StringRef, StringRef>(
|
|
|
|
attribute,
|
|
|
|
key_columns,
|
|
|
|
[&](const size_t, const StringRef value) { out->insertData(value.data, value.size); },
|
|
|
|
[&](const size_t) { return StringRef{def}; });
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::has(const Columns & key_columns, const DataTypes & key_types, PaddedPODArray<UInt8> & out) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
validateKeyTypes(key_types);
|
|
|
|
|
|
|
|
const auto & attribute = attributes.front();
|
|
|
|
|
|
|
|
switch (attribute.type)
|
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt8:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<UInt8>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt16:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<UInt16>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt32:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<UInt32>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt64:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<UInt64>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt128:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<UInt128>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt8:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Int8>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt16:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Int16>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt32:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Int32>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt64:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Int64>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat32:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Float32>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat64:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Float64>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utString:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<StringRef>(attribute, key_columns, out);
|
|
|
|
break;
|
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal32:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Decimal32>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal64:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Decimal64>(attribute, key_columns, out);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal128:
|
2018-12-10 15:25:45 +00:00
|
|
|
has<Decimal128>(attribute, key_columns, out);
|
|
|
|
break;
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::createAttributes()
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
const auto size = dict_struct.attributes.size();
|
|
|
|
attributes.reserve(size);
|
|
|
|
|
|
|
|
for (const auto & attribute : dict_struct.attributes)
|
|
|
|
{
|
|
|
|
attribute_index_by_name.emplace(attribute.name, attributes.size());
|
|
|
|
attributes.push_back(createAttributeWithType(attribute.underlying_type, attribute.null_value));
|
|
|
|
|
|
|
|
if (attribute.hierarchical)
|
2019-12-25 23:12:12 +00:00
|
|
|
throw Exception{full_name + ": hierarchical attributes not supported for dictionary of type " + getTypeName(),
|
2018-12-10 15:25:45 +00:00
|
|
|
ErrorCodes::TYPE_MISMATCH};
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::loadData()
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
auto stream = source_ptr->loadAll();
|
|
|
|
stream->readPrefix();
|
|
|
|
|
|
|
|
const auto attributes_size = attributes.size();
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
std::vector<IPRecord> ip_records;
|
|
|
|
|
|
|
|
bool has_ipv6 = false;
|
|
|
|
|
2017-05-12 21:23:12 +00:00
|
|
|
while (const auto block = stream->read())
|
|
|
|
{
|
|
|
|
const auto rows = block.rows();
|
|
|
|
element_count += rows;
|
|
|
|
|
2020-12-23 16:58:54 +00:00
|
|
|
const ColumnPtr key_column_ptr = block.safeGetByPosition(0).column;
|
2018-12-10 15:25:45 +00:00
|
|
|
const auto attribute_column_ptrs = ext::map<Columns>(ext::range(0, attributes_size), [&](const size_t attribute_idx)
|
|
|
|
{
|
2020-12-23 16:58:54 +00:00
|
|
|
return block.safeGetByPosition(attribute_idx + 1).column;
|
2018-12-10 15:25:45 +00:00
|
|
|
});
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
for (const auto row : ext::range(0, rows))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
for (const auto attribute_idx : ext::range(0, attributes_size))
|
|
|
|
{
|
|
|
|
const auto & attribute_column = *attribute_column_ptrs[attribute_idx];
|
|
|
|
auto & attribute = attributes[attribute_idx];
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
setAttributeValue(attribute, attribute_column[row]);
|
2020-11-08 16:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-23 16:58:54 +00:00
|
|
|
const auto [addr, prefix] = parseIPFromString(std::string_view(key_column_ptr->getDataAt(row)));
|
2020-11-21 14:56:58 +00:00
|
|
|
has_ipv6 = has_ipv6 || (addr.family() == Poco::Net::IPAddress::IPv6);
|
2020-11-11 21:58:30 +00:00
|
|
|
|
2020-11-21 14:56:58 +00:00
|
|
|
size_t row_number = ip_records.size();
|
|
|
|
ip_records.emplace_back(addr, prefix, row_number);
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
stream->readSuffix();
|
|
|
|
|
2020-12-23 16:58:54 +00:00
|
|
|
row_idx.reserve(ip_records.size());
|
|
|
|
mask_column.reserve(ip_records.size());
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
if (has_ipv6)
|
|
|
|
{
|
2020-11-21 14:56:58 +00:00
|
|
|
auto deleted_count = sortAndUnique(ip_records,
|
2020-11-11 21:58:30 +00:00
|
|
|
[](const auto & record_a, const auto & record_b)
|
|
|
|
{
|
2020-11-15 14:36:05 +00:00
|
|
|
uint8_t a_buf[IPV6_BINARY_LENGTH];
|
|
|
|
uint8_t b_buf[IPV6_BINARY_LENGTH];
|
|
|
|
|
|
|
|
auto cmpres = memcmp16(record_a.asIPv6Binary(a_buf), record_b.asIPv6Binary(b_buf));
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
if (cmpres == 0)
|
2020-11-16 07:43:55 +00:00
|
|
|
return compareTo(record_a.prefixIPv6(), record_b.prefixIPv6());
|
|
|
|
return cmpres;
|
2020-11-11 21:58:30 +00:00
|
|
|
});
|
2020-11-16 07:43:55 +00:00
|
|
|
if (deleted_count > 0)
|
|
|
|
LOG_WARNING(logger, "removing {} non-unique subnets from input", deleted_count);
|
2020-11-11 21:58:30 +00:00
|
|
|
|
|
|
|
auto & ipv6_col = ip_column.emplace<IPv6Container>();
|
|
|
|
ipv6_col.resize_fill(IPV6_BINARY_LENGTH * ip_records.size());
|
|
|
|
|
|
|
|
for (const auto & record : ip_records)
|
|
|
|
{
|
|
|
|
size_t i = row_idx.size();
|
2020-11-14 20:29:58 +00:00
|
|
|
|
2020-11-21 14:56:58 +00:00
|
|
|
IPv6ToRawBinary(record.addr, reinterpret_cast<char *>(&ipv6_col[i * IPV6_BINARY_LENGTH]));
|
2020-11-15 17:09:28 +00:00
|
|
|
mask_column.push_back(record.prefixIPv6());
|
2020-11-11 21:58:30 +00:00
|
|
|
row_idx.push_back(record.row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-21 14:56:58 +00:00
|
|
|
auto deleted_count = sortAndUnique(ip_records,
|
2020-11-11 21:58:30 +00:00
|
|
|
[](const auto & record_a, const auto & record_b)
|
|
|
|
{
|
2020-11-16 07:43:55 +00:00
|
|
|
UInt32 a = IPv4AsUInt32(record_a.addr.addr());
|
|
|
|
UInt32 b = IPv4AsUInt32(record_b.addr.addr());
|
2020-11-11 21:58:30 +00:00
|
|
|
|
|
|
|
if (a == b)
|
2020-11-16 07:43:55 +00:00
|
|
|
return compareTo(record_a.prefix, record_b.prefix);
|
|
|
|
return compareTo(a, b);
|
2020-11-11 21:58:30 +00:00
|
|
|
});
|
2020-11-16 07:43:55 +00:00
|
|
|
if (deleted_count > 0)
|
|
|
|
LOG_WARNING(logger, "removing {} non-unique subnets from input", deleted_count);
|
2020-11-11 21:58:30 +00:00
|
|
|
|
|
|
|
auto & ipv4_col = ip_column.emplace<IPv4Container>();
|
|
|
|
ipv4_col.reserve(ip_records.size());
|
|
|
|
for (const auto & record : ip_records)
|
|
|
|
{
|
2020-11-15 14:36:05 +00:00
|
|
|
auto addr = IPv4AsUInt32(record.addr.addr());
|
2020-11-11 21:58:30 +00:00
|
|
|
ipv4_col.push_back(addr);
|
|
|
|
mask_column.push_back(record.prefix);
|
|
|
|
row_idx.push_back(record.row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent_subnet.resize(ip_records.size());
|
|
|
|
std::stack<size_t> subnets_stack;
|
|
|
|
for (const auto i : ext::range(0, ip_records.size()))
|
|
|
|
{
|
|
|
|
parent_subnet[i] = i;
|
|
|
|
while (!subnets_stack.empty())
|
|
|
|
{
|
2020-11-15 14:36:05 +00:00
|
|
|
size_t pi = subnets_stack.top();
|
|
|
|
if (has_ipv6)
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
2020-11-15 14:36:05 +00:00
|
|
|
uint8_t a_buf[IPV6_BINARY_LENGTH];
|
|
|
|
uint8_t b_buf[IPV6_BINARY_LENGTH];
|
|
|
|
const auto * cur_address = ip_records[i].asIPv6Binary(a_buf);
|
|
|
|
const auto * cur_subnet = ip_records[pi].asIPv6Binary(b_buf);
|
|
|
|
|
2020-11-15 17:09:28 +00:00
|
|
|
bool is_mask_smaller = ip_records[pi].prefixIPv6() < ip_records[i].prefixIPv6();
|
|
|
|
if (is_mask_smaller && matchIPv6Subnet(cur_address, cur_subnet, ip_records[pi].prefixIPv6()))
|
2020-11-15 14:36:05 +00:00
|
|
|
{
|
|
|
|
parent_subnet[i] = pi;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UInt32 cur_address = IPv4AsUInt32(ip_records[i].addr.addr());
|
|
|
|
UInt32 cur_subnet = IPv4AsUInt32(ip_records[pi].addr.addr());
|
|
|
|
|
|
|
|
bool is_mask_smaller = ip_records[pi].prefix < ip_records[i].prefix;
|
|
|
|
if (is_mask_smaller && matchIPv4Subnet(cur_address, cur_subnet, ip_records[pi].prefix))
|
|
|
|
{
|
|
|
|
parent_subnet[i] = pi;
|
|
|
|
break;
|
|
|
|
}
|
2020-11-11 21:58:30 +00:00
|
|
|
}
|
|
|
|
subnets_stack.pop();
|
|
|
|
}
|
|
|
|
subnets_stack.push(i);
|
|
|
|
}
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
LOG_TRACE(logger, "{} ip records are read", ip_records.size());
|
2020-11-15 14:36:05 +00:00
|
|
|
|
2017-05-12 21:23:12 +00:00
|
|
|
if (require_nonempty && 0 == element_count)
|
2019-12-25 23:12:12 +00:00
|
|
|
throw Exception{full_name + ": dictionary source is empty and 'require_nonempty' property is set.", ErrorCodes::DICTIONARY_IS_EMPTY};
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::addAttributeSize(const Attribute & attribute)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2018-11-13 19:43:17 +00:00
|
|
|
const auto & vec = std::get<ContainerType<T>>(attribute.maps);
|
2017-05-12 21:23:12 +00:00
|
|
|
bytes_allocated += sizeof(ContainerType<T>) + (vec.capacity() * sizeof(T));
|
|
|
|
bucket_count = vec.size();
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::calculateBytesAllocated()
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
if (auto * ipv4_col = std::get_if<IPv4Container>(&ip_column))
|
|
|
|
{
|
|
|
|
bytes_allocated += ipv4_col->size() * sizeof((*ipv4_col)[0]);
|
|
|
|
}
|
|
|
|
else if (auto * ipv6_col = std::get_if<IPv6Container>(&ip_column))
|
|
|
|
{
|
|
|
|
bytes_allocated += ipv6_col->size() * sizeof((*ipv6_col)[0]);
|
|
|
|
}
|
|
|
|
bytes_allocated += mask_column.size() * sizeof(mask_column[0]);
|
2020-11-21 14:56:58 +00:00
|
|
|
bytes_allocated += parent_subnet.size() * sizeof(parent_subnet[0]);
|
2020-11-11 21:58:30 +00:00
|
|
|
bytes_allocated += row_idx.size() * sizeof(row_idx[0]);
|
2017-05-12 21:23:12 +00:00
|
|
|
bytes_allocated += attributes.size() * sizeof(attributes.front());
|
|
|
|
|
|
|
|
for (const auto & attribute : attributes)
|
|
|
|
{
|
|
|
|
switch (attribute.type)
|
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt8:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<UInt8>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt16:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<UInt16>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt32:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<UInt32>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt64:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<UInt64>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt128:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<UInt128>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt8:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Int8>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt16:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Int16>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt32:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Int32>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt64:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Int64>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat32:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Float32>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat64:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Float64>(attribute);
|
|
|
|
break;
|
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal32:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Decimal32>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal64:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Decimal64>(attribute);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal128:
|
2018-12-10 15:25:45 +00:00
|
|
|
addAttributeSize<Decimal128>(attribute);
|
|
|
|
break;
|
2018-10-08 19:45:17 +00:00
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utString:
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
addAttributeSize<StringRef>(attribute);
|
|
|
|
bytes_allocated += sizeof(Arena) + attribute.string_arena->size();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::createAttributeImpl(Attribute & attribute, const Field & null_value)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2018-11-20 20:09:20 +00:00
|
|
|
attribute.null_values = T(null_value.get<NearestFieldType<T>>());
|
2018-11-13 19:43:17 +00:00
|
|
|
attribute.maps.emplace<ContainerType<T>>();
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
IPAddressDictionary::Attribute IPAddressDictionary::createAttributeWithType(const AttributeUnderlyingType type, const Field & null_value)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2017-12-01 20:21:35 +00:00
|
|
|
Attribute attr{type, {}, {}, {}};
|
2017-05-12 21:23:12 +00:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt8:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<UInt8>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt16:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<UInt16>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt32:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<UInt32>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt64:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<UInt64>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt128:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<UInt128>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt8:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Int8>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt16:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Int16>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt32:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Int32>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt64:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Int64>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat32:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Float32>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat64:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Float64>(attr, null_value);
|
|
|
|
break;
|
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal32:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Decimal32>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal64:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Decimal64>(attr, null_value);
|
|
|
|
break;
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal128:
|
2018-12-10 15:25:45 +00:00
|
|
|
createAttributeImpl<Decimal128>(attr, null_value);
|
|
|
|
break;
|
2018-10-08 19:45:17 +00:00
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utString:
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2018-11-13 18:41:07 +00:00
|
|
|
attr.null_values = null_value.get<String>();
|
2018-11-13 19:43:17 +00:00
|
|
|
attr.maps.emplace<ContainerType<StringRef>>();
|
2017-05-12 21:23:12 +00:00
|
|
|
attr.string_arena = std::make_unique<Arena>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
const uint8_t * IPAddressDictionary::getIPv6FromOffset(const IPAddressDictionary::IPv6Container & ipv6_col, size_t i)
|
2020-11-16 07:43:55 +00:00
|
|
|
{
|
|
|
|
return reinterpret_cast<const uint8_t *>(&ipv6_col[i * IPV6_BINARY_LENGTH]);
|
|
|
|
}
|
|
|
|
|
2017-05-12 21:23:12 +00:00
|
|
|
template <typename AttributeType, typename OutputType, typename ValueSetter, typename DefaultGetter>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::getItemsByTwoKeyColumnsImpl(
|
2018-12-10 15:25:45 +00:00
|
|
|
const Attribute & attribute, const Columns & key_columns, ValueSetter && set_value, DefaultGetter && get_default) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
const auto first_column = key_columns.front();
|
|
|
|
const auto rows = first_column->size();
|
2020-11-11 21:58:30 +00:00
|
|
|
auto & vec = std::get<ContainerType<AttributeType>>(attribute.maps);
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
if (const auto * ipv4_col = std::get_if<IPv4Container>(&ip_column))
|
2020-11-08 20:50:49 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
const auto * key_ip_column_ptr = typeid_cast<const ColumnVector<UInt32> *>(&*key_columns.front());
|
|
|
|
if (key_ip_column_ptr == nullptr)
|
|
|
|
throw Exception{"Expected a UInt32 IP column", ErrorCodes::TYPE_MISMATCH};
|
2020-11-08 20:50:49 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
const auto & key_mask_column = assert_cast<const ColumnVector<UInt8> &>(*key_columns.back());
|
2020-11-08 20:50:49 +00:00
|
|
|
|
2020-11-21 14:56:58 +00:00
|
|
|
auto comp_v4 = [&](size_t elem, const IPv4Subnet & target)
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
UInt32 addr = (*ipv4_col)[elem];
|
|
|
|
if (addr == target.addr)
|
2020-11-16 07:43:55 +00:00
|
|
|
return mask_column[elem] < target.prefix;
|
2020-11-11 21:58:30 +00:00
|
|
|
return addr < target.addr;
|
|
|
|
};
|
2020-11-08 20:50:49 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
for (const auto i : ext::range(0, rows))
|
|
|
|
{
|
|
|
|
UInt32 addr = key_ip_column_ptr->getElement(i);
|
|
|
|
UInt8 mask = key_mask_column.getElement(i);
|
2020-11-08 20:50:49 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
auto range = ext::range(0, row_idx.size());
|
|
|
|
auto found_it = std::lower_bound(range.begin(), range.end(), IPv4Subnet{addr, mask}, comp_v4);
|
2020-11-08 20:50:49 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
if (likely(found_it != range.end() &&
|
|
|
|
(*ipv4_col)[*found_it] == addr &&
|
|
|
|
mask_column[*found_it] == mask))
|
2020-11-08 20:50:49 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
set_value(i, static_cast<OutputType>(vec[row_idx[*found_it]]));
|
2020-11-08 20:50:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
set_value(i, get_default(i));
|
|
|
|
}
|
2020-11-11 21:58:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto * key_ip_column_ptr = typeid_cast<const ColumnFixedString *>(&*key_columns.front());
|
2020-11-21 14:56:58 +00:00
|
|
|
if (key_ip_column_ptr == nullptr || key_ip_column_ptr->getN() != IPV6_BINARY_LENGTH)
|
|
|
|
throw Exception{"Expected a FixedString(16) IP column", ErrorCodes::TYPE_MISMATCH};
|
2020-11-11 21:58:30 +00:00
|
|
|
|
|
|
|
const auto & key_mask_column = assert_cast<const ColumnVector<UInt8> &>(*key_columns.back());
|
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
const auto * ipv6_col = std::get_if<IPv6Container>(&ip_column);
|
2020-11-21 14:56:58 +00:00
|
|
|
auto comp_v6 = [&](size_t i, const IPv6Subnet & target)
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
auto cmpres = memcmp16(getIPv6FromOffset(*ipv6_col, i), target.addr);
|
|
|
|
if (cmpres == 0)
|
2020-11-16 07:43:55 +00:00
|
|
|
return mask_column[i] < target.prefix;
|
2020-11-11 21:58:30 +00:00
|
|
|
return cmpres < 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, rows))
|
|
|
|
{
|
|
|
|
auto addr = key_ip_column_ptr->getDataAt(i);
|
|
|
|
UInt8 mask = key_mask_column.getElement(i);
|
|
|
|
|
|
|
|
IPv6Subnet target{reinterpret_cast<const uint8_t *>(addr.data), mask};
|
|
|
|
|
|
|
|
auto range = ext::range(0, row_idx.size());
|
|
|
|
auto found_it = std::lower_bound(range.begin(), range.end(), target, comp_v6);
|
|
|
|
|
|
|
|
if (likely(found_it != range.end() &&
|
2020-11-14 20:32:58 +00:00
|
|
|
memequal16(getIPv6FromOffset(*ipv6_col, *found_it), target.addr) &&
|
2020-11-11 21:58:30 +00:00
|
|
|
mask_column[*found_it] == mask))
|
|
|
|
set_value(i, static_cast<OutputType>(vec[row_idx[*found_it]]));
|
|
|
|
else
|
|
|
|
set_value(i, get_default(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AttributeType, typename OutputType, typename ValueSetter, typename DefaultGetter>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::getItemsImpl(
|
2020-11-11 21:58:30 +00:00
|
|
|
const Attribute & attribute, const Columns & key_columns, ValueSetter && set_value, DefaultGetter && get_default) const
|
|
|
|
{
|
|
|
|
const auto first_column = key_columns.front();
|
|
|
|
const auto rows = first_column->size();
|
|
|
|
|
|
|
|
// special case for getBlockInputStream
|
|
|
|
if (unlikely(key_columns.size() == 2))
|
|
|
|
{
|
|
|
|
getItemsByTwoKeyColumnsImpl<AttributeType, OutputType>(
|
|
|
|
attribute, key_columns, std::forward<ValueSetter>(set_value), std::forward<DefaultGetter>(get_default));
|
2020-11-08 20:50:49 +00:00
|
|
|
query_count.fetch_add(rows, std::memory_order_relaxed);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
auto & vec = std::get<ContainerType<AttributeType>>(attribute.maps);
|
|
|
|
|
2017-05-12 21:23:12 +00:00
|
|
|
if (first_column->isNumeric())
|
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
uint8_t addrv6_buf[IPV6_BINARY_LENGTH];
|
2017-05-12 21:23:12 +00:00
|
|
|
for (const auto i : ext::range(0, rows))
|
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
// addrv4 has native endianness
|
|
|
|
auto addrv4 = UInt32(first_column->get64(i));
|
|
|
|
auto found = tryLookupIPv4(addrv4, addrv6_buf);
|
2020-11-21 14:56:58 +00:00
|
|
|
if (found != ipNotFound())
|
|
|
|
set_value(i, static_cast<OutputType>(vec[*found]));
|
|
|
|
else
|
|
|
|
set_value(i, get_default(i));
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto i : ext::range(0, rows))
|
|
|
|
{
|
|
|
|
auto addr = first_column->getDataAt(i);
|
2020-11-08 16:01:12 +00:00
|
|
|
if (addr.size != IPV6_BINARY_LENGTH)
|
2017-05-12 21:23:12 +00:00
|
|
|
throw Exception("Expected key to be FixedString(16)", ErrorCodes::LOGICAL_ERROR);
|
2017-06-05 09:02:05 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
auto found = tryLookupIPv6(reinterpret_cast<const uint8_t *>(addr.data));
|
2020-11-21 14:56:58 +00:00
|
|
|
if (found != ipNotFound())
|
|
|
|
set_value(i, static_cast<OutputType>(vec[*found]));
|
|
|
|
else
|
|
|
|
set_value(i, get_default(i));
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query_count.fetch_add(rows, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::setAttributeValueImpl(Attribute & attribute, const T value)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2018-11-13 19:43:17 +00:00
|
|
|
auto & vec = std::get<ContainerType<T>>(attribute.maps);
|
2017-05-12 21:23:12 +00:00
|
|
|
vec.push_back(value);
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::setAttributeValue(Attribute & attribute, const Field & value)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
switch (attribute.type)
|
|
|
|
{
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt8:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<UInt8>(attribute, value.get<UInt64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt16:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<UInt16>(attribute, value.get<UInt64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt32:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<UInt32>(attribute, value.get<UInt64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt64:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<UInt64>(attribute, value.get<UInt64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utUInt128:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<UInt128>(attribute, value.get<UInt128>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt8:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Int8>(attribute, value.get<Int64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt16:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Int16>(attribute, value.get<Int64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt32:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Int32>(attribute, value.get<Int64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utInt64:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Int64>(attribute, value.get<Int64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat32:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Float32>(attribute, value.get<Float64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utFloat64:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Float64>(attribute, value.get<Float64>());
|
2018-12-10 15:25:45 +00:00
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal32:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Decimal32>(attribute, value.get<Decimal32>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal64:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Decimal64>(attribute, value.get<Decimal64>());
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utDecimal128:
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<Decimal128>(attribute, value.get<Decimal128>());
|
2018-10-08 19:45:17 +00:00
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
case AttributeUnderlyingType::utString:
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
const auto & string = value.get<String>();
|
2020-04-22 07:03:43 +00:00
|
|
|
const auto * string_in_arena = attribute.string_arena->insert(string.data(), string.size());
|
2020-11-08 16:01:12 +00:00
|
|
|
return setAttributeValueImpl<StringRef>(attribute, StringRef{string_in_arena, string.size()});
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
const IPAddressDictionary::Attribute & IPAddressDictionary::getAttribute(const std::string & attribute_name) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
const auto it = attribute_index_by_name.find(attribute_name);
|
|
|
|
if (it == std::end(attribute_index_by_name))
|
2019-12-25 23:12:12 +00:00
|
|
|
throw Exception{full_name + ": no such attribute '" + attribute_name + "'", ErrorCodes::BAD_ARGUMENTS};
|
2017-05-12 21:23:12 +00:00
|
|
|
|
|
|
|
return attributes[it->second];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::has(const Attribute &, const Columns & key_columns, PaddedPODArray<UInt8> & out) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
const auto first_column = key_columns.front();
|
|
|
|
const auto rows = first_column->size();
|
|
|
|
if (first_column->isNumeric())
|
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
uint8_t addrv6_buf[IPV6_BINARY_LENGTH];
|
2017-05-12 21:23:12 +00:00
|
|
|
for (const auto i : ext::range(0, rows))
|
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
auto addrv4 = UInt32(first_column->get64(i));
|
|
|
|
auto found = tryLookupIPv4(addrv4, addrv6_buf);
|
|
|
|
out[i] = (found != ipNotFound());
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto i : ext::range(0, rows))
|
|
|
|
{
|
|
|
|
auto addr = first_column->getDataAt(i);
|
2020-11-11 21:58:30 +00:00
|
|
|
if (unlikely(addr.size != IPV6_BINARY_LENGTH))
|
2017-05-12 21:23:12 +00:00
|
|
|
throw Exception("Expected key to be FixedString(16)", ErrorCodes::LOGICAL_ERROR);
|
2017-06-05 09:02:05 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
auto found = tryLookupIPv6(reinterpret_cast<const uint8_t *>(addr.data));
|
|
|
|
out[i] = (found != ipNotFound());
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 09:02:05 +00:00
|
|
|
query_count.fetch_add(rows, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
Columns IPAddressDictionary::getKeyColumns() const
|
2017-06-05 09:02:05 +00:00
|
|
|
{
|
2020-11-12 18:47:10 +00:00
|
|
|
const auto * ipv4_col = std::get_if<IPv4Container>(&ip_column);
|
2020-11-11 21:58:30 +00:00
|
|
|
if (ipv4_col)
|
2018-01-10 00:04:08 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
auto key_ip_column = ColumnVector<UInt32>::create();
|
|
|
|
auto key_mask_column = ColumnVector<UInt8>::create();
|
|
|
|
for (size_t row : ext::range(0, row_idx.size()))
|
|
|
|
{
|
|
|
|
key_ip_column->insertValue((*ipv4_col)[row]);
|
|
|
|
key_mask_column->insertValue(mask_column[row]);
|
|
|
|
}
|
|
|
|
return {std::move(key_ip_column), std::move(key_mask_column)};
|
2020-11-08 16:01:12 +00:00
|
|
|
}
|
2017-06-05 09:02:05 +00:00
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
const auto * ipv6_col = std::get_if<IPv6Container>(&ip_column);
|
2017-06-05 09:02:05 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
auto key_ip_column = ColumnFixedString::create(IPV6_BINARY_LENGTH);
|
|
|
|
auto key_mask_column = ColumnVector<UInt8>::create();
|
2017-06-05 09:02:05 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
for (size_t row : ext::range(0, row_idx.size()))
|
2017-06-05 09:02:05 +00:00
|
|
|
{
|
2020-11-12 18:47:10 +00:00
|
|
|
const char * data = reinterpret_cast<const char *>(getIPv6FromOffset(*ipv6_col, row));
|
2020-11-11 21:58:30 +00:00
|
|
|
key_ip_column->insertData(data, IPV6_BINARY_LENGTH);
|
|
|
|
key_mask_column->insertValue(mask_column[row]);
|
|
|
|
}
|
|
|
|
return {std::move(key_ip_column), std::move(key_mask_column)};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename KeyColumnType, bool IsIPv4>
|
|
|
|
static auto keyViewGetter()
|
|
|
|
{
|
|
|
|
return [](const Columns & columns, const std::vector<DictionaryAttribute> & dict_attributes)
|
2017-06-05 09:02:05 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto column = ColumnString::create();
|
2020-11-11 21:58:30 +00:00
|
|
|
const auto & key_ip_column = assert_cast<const KeyColumnType &>(*columns.front());
|
|
|
|
const auto & key_mask_column = assert_cast<const ColumnVector<UInt8> &>(*columns.back());
|
2017-06-05 09:02:05 +00:00
|
|
|
char buffer[48];
|
2020-11-11 21:58:30 +00:00
|
|
|
for (size_t row : ext::range(0, key_ip_column.size()))
|
2017-06-05 09:02:05 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
UInt8 mask = key_mask_column.getElement(row);
|
2017-06-05 09:02:05 +00:00
|
|
|
char * ptr = buffer;
|
2020-11-11 21:58:30 +00:00
|
|
|
if constexpr (IsIPv4)
|
|
|
|
formatIPv4(reinterpret_cast<const unsigned char *>(&key_ip_column.getElement(row)), ptr);
|
|
|
|
else
|
|
|
|
formatIPv6(reinterpret_cast<const unsigned char *>(key_ip_column.getDataAt(row).data), ptr);
|
2017-06-05 09:02:05 +00:00
|
|
|
*(ptr - 1) = '/';
|
2018-12-29 18:39:55 +00:00
|
|
|
ptr = itoa(mask, ptr);
|
|
|
|
column->insertData(buffer, ptr - buffer);
|
2017-06-05 09:02:05 +00:00
|
|
|
}
|
2018-12-10 15:25:45 +00:00
|
|
|
return ColumnsWithTypeAndName{
|
2019-01-04 12:10:00 +00:00
|
|
|
ColumnWithTypeAndName(std::move(column), std::make_shared<DataTypeString>(), dict_attributes.front().name)};
|
2017-06-05 09:02:05 +00:00
|
|
|
};
|
|
|
|
}
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
BlockInputStreamPtr IPAddressDictionary::getBlockInputStream(const Names & column_names, size_t max_block_size) const
|
2020-11-08 16:01:12 +00:00
|
|
|
{
|
2020-11-21 18:38:10 +00:00
|
|
|
using BlockInputStreamType = DictionaryBlockInputStream<IPAddressDictionary, UInt64>;
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
const bool is_ipv4 = std::get_if<IPv4Container>(&ip_column) != nullptr;
|
2020-11-11 21:58:30 +00:00
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
auto get_keys = [is_ipv4](const Columns & columns, const std::vector<DictionaryAttribute> & dict_attributes)
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
const auto & attr = dict_attributes.front();
|
|
|
|
std::shared_ptr<const IDataType> key_typ;
|
2020-11-12 18:47:10 +00:00
|
|
|
if (is_ipv4)
|
2020-11-11 21:58:30 +00:00
|
|
|
key_typ = std::make_shared<DataTypeUInt32>();
|
|
|
|
else
|
|
|
|
key_typ = std::make_shared<DataTypeFixedString>(IPV6_BINARY_LENGTH);
|
|
|
|
|
|
|
|
return ColumnsWithTypeAndName({
|
|
|
|
ColumnWithTypeAndName(columns.front(), key_typ, attr.name),
|
|
|
|
ColumnWithTypeAndName(columns.back(), std::make_shared<DataTypeUInt8>(), attr.name + ".mask")
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
if (is_ipv4)
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
auto get_view = keyViewGetter<ColumnVector<UInt32>, true>();
|
|
|
|
return std::make_shared<BlockInputStreamType>(
|
|
|
|
shared_from_this(), max_block_size, getKeyColumns(), column_names, std::move(get_keys), std::move(get_view));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto get_view = keyViewGetter<ColumnFixedString, false>();
|
|
|
|
return std::make_shared<BlockInputStreamType>(
|
|
|
|
shared_from_this(), max_block_size, getKeyColumns(), column_names, std::move(get_keys), std::move(get_view));
|
2020-11-08 16:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
IPAddressDictionary::RowIdxConstIter IPAddressDictionary::ipNotFound() const
|
2020-11-08 20:50:49 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
return row_idx.end();
|
|
|
|
}
|
2020-11-08 20:50:49 +00:00
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
IPAddressDictionary::RowIdxConstIter IPAddressDictionary::tryLookupIPv4(UInt32 addr, uint8_t * buf) const
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
if (std::get_if<IPv6Container>(&ip_column))
|
|
|
|
{
|
|
|
|
mapIPv4ToIPv6(addr, buf);
|
|
|
|
return lookupIP<IPv6Container>(buf);
|
|
|
|
}
|
|
|
|
return lookupIP<IPv4Container>(addr);
|
2020-11-08 20:50:49 +00:00
|
|
|
}
|
|
|
|
|
2020-11-21 18:38:10 +00:00
|
|
|
IPAddressDictionary::RowIdxConstIter IPAddressDictionary::tryLookupIPv6(const uint8_t * addr) const
|
2020-11-08 16:01:12 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
if (std::get_if<IPv4Container>(&ip_column))
|
|
|
|
{
|
|
|
|
bool is_mapped = false;
|
|
|
|
UInt32 addrv4 = mappedIPv4ToBinary(addr, is_mapped);
|
|
|
|
if (!is_mapped)
|
|
|
|
return ipNotFound();
|
|
|
|
return lookupIP<IPv4Container>(addrv4);
|
|
|
|
}
|
|
|
|
return lookupIP<IPv6Container>(addr);
|
2020-11-08 16:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
template <typename IPContainerType, typename IPValueType>
|
2020-11-21 18:38:10 +00:00
|
|
|
IPAddressDictionary::RowIdxConstIter IPAddressDictionary::lookupIP(IPValueType target) const
|
2020-11-08 16:01:12 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
if (row_idx.empty())
|
|
|
|
return ipNotFound();
|
|
|
|
|
2020-11-12 18:47:10 +00:00
|
|
|
const auto * ipv4or6_col = std::get_if<IPContainerType>(&ip_column);
|
2020-11-11 21:58:30 +00:00
|
|
|
if (ipv4or6_col == nullptr)
|
|
|
|
return ipNotFound();
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
auto comp = [&](auto value, auto idx) -> bool
|
2020-11-08 16:01:12 +00:00
|
|
|
{
|
2020-11-11 21:58:30 +00:00
|
|
|
if constexpr (std::is_same_v<IPContainerType, IPv4Container>)
|
|
|
|
return value < (*ipv4or6_col)[idx];
|
|
|
|
else
|
|
|
|
return memcmp16(value, getIPv6FromOffset(*ipv4or6_col, idx)) < 0;
|
2020-11-08 16:01:12 +00:00
|
|
|
};
|
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
auto range = ext::range(0, row_idx.size());
|
|
|
|
auto found_it = std::upper_bound(range.begin(), range.end(), target, comp);
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
if (found_it == range.begin())
|
|
|
|
return ipNotFound();
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
--found_it;
|
|
|
|
if constexpr (std::is_same_v<IPContainerType, IPv4Container>)
|
|
|
|
{
|
|
|
|
for (auto idx = *found_it;; idx = parent_subnet[idx])
|
|
|
|
{
|
|
|
|
if (matchIPv4Subnet(target, (*ipv4or6_col)[idx], mask_column[idx]))
|
|
|
|
return row_idx.begin() + idx;
|
|
|
|
|
|
|
|
if (idx == parent_subnet[idx])
|
|
|
|
return ipNotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto idx = *found_it;; idx = parent_subnet[idx])
|
|
|
|
{
|
|
|
|
if (matchIPv6Subnet(target, getIPv6FromOffset(*ipv4or6_col, idx), mask_column[idx]))
|
|
|
|
return row_idx.begin() + idx;
|
|
|
|
|
|
|
|
if (idx == parent_subnet[idx])
|
|
|
|
return ipNotFound();
|
|
|
|
}
|
|
|
|
}
|
2020-11-08 16:01:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
return ipNotFound();
|
2020-11-08 16:01:12 +00:00
|
|
|
}
|
2018-11-28 11:37:12 +00:00
|
|
|
|
|
|
|
void registerDictionaryTrie(DictionaryFactory & factory)
|
|
|
|
{
|
2019-12-25 23:12:12 +00:00
|
|
|
auto create_layout = [=](const std::string &,
|
2018-12-10 15:25:45 +00:00
|
|
|
const DictionaryStructure & dict_struct,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
2018-12-10 15:50:58 +00:00
|
|
|
DictionarySourcePtr source_ptr) -> DictionaryPtr
|
|
|
|
{
|
2020-12-23 16:58:54 +00:00
|
|
|
if (!dict_struct.key || dict_struct.key->size() != 1)
|
|
|
|
throw Exception{"Dictionary of layout 'ip_trie' has to have one 'key'", ErrorCodes::BAD_ARGUMENTS};
|
2018-11-28 11:37:12 +00:00
|
|
|
|
2020-07-14 19:19:17 +00:00
|
|
|
const auto dict_id = StorageID::fromDictionaryConfig(config, config_prefix);
|
2018-12-10 15:25:45 +00:00
|
|
|
const DictionaryLifetime dict_lifetime{config, config_prefix + ".lifetime"};
|
2018-11-28 11:37:12 +00:00
|
|
|
const bool require_nonempty = config.getBool(config_prefix + ".require_nonempty", false);
|
2020-12-23 16:58:54 +00:00
|
|
|
// This is specialised dictionary for storing IPv4 and IPv6 prefixes.
|
2020-11-21 18:38:10 +00:00
|
|
|
return std::make_unique<IPAddressDictionary>(dict_id, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
|
2018-11-28 11:37:12 +00:00
|
|
|
};
|
2019-10-21 16:05:45 +00:00
|
|
|
factory.registerLayout("ip_trie", create_layout, true);
|
2018-11-28 11:37:12 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|