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>
|
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>
|
2021-01-21 14:42:50 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.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>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/itoa.h>
|
|
|
|
#include <base/map.h>
|
|
|
|
#include <base/range.h>
|
2021-08-11 12:32:01 +00:00
|
|
|
#include <Dictionaries/DictionarySource.h>
|
2021-03-29 09:04:05 +00:00
|
|
|
#include <Dictionaries/DictionaryFactory.h>
|
2020-12-29 10:09:01 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2017-05-12 21:23:12 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-12-26 19:16:57 +00:00
|
|
|
extern const int CANNOT_PARSE_INPUT_ASSERTION_FAILED;
|
|
|
|
extern const int CANNOT_PARSE_NUMBER;
|
2017-05-12 21:23:12 +00:00
|
|
|
extern const int DICTIONARY_IS_EMPTY;
|
2020-12-26 19:16:57 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int TYPE_MISMATCH;
|
2021-06-09 10:43:40 +00:00
|
|
|
extern const int UNSUPPORTED_METHOD;
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2020-12-27 08:43:27 +00:00
|
|
|
throw DB::Exception("Extra characters at the end of IP address", ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED);
|
2020-11-21 14:56:58 +00:00
|
|
|
if (ec != std::errc())
|
2020-12-27 08:43:27 +00:00
|
|
|
throw DB::Exception("Mask for IP address is not a valid number", ErrorCodes::CANNOT_PARSE_NUMBER);
|
2020-11-21 14:56:58 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-12-27 08:43:27 +00:00
|
|
|
throw DB::Exception("Can't parse address \"" + std::string(addr_str) + "\": " + ex.what(),
|
2020-12-26 19:16:57 +00:00
|
|
|
ErrorCodes::CANNOT_PARSE_INPUT_ASSERTION_FAILED);
|
2020-11-21 14:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 07:31:09 +00:00
|
|
|
static size_t formatIPWithPrefix(const unsigned char * src, UInt8 prefix_len, bool isv4, char * dst)
|
|
|
|
{
|
|
|
|
char * ptr = dst;
|
|
|
|
if (isv4)
|
|
|
|
formatIPv4(src, ptr);
|
|
|
|
else
|
|
|
|
formatIPv6(src, ptr);
|
|
|
|
*(ptr - 1) = '/';
|
|
|
|
ptr = itoa(prefix_len, ptr);
|
|
|
|
return ptr - dst;
|
|
|
|
}
|
|
|
|
|
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)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::TYPE_MISMATCH, "Expected a single IP address or IP with mask");
|
2020-11-11 21:58:30 +00:00
|
|
|
|
2020-11-28 20:11:15 +00:00
|
|
|
const auto * key_ipv4type = typeid_cast<const DataTypeUInt32 *>(key_types[0].get());
|
|
|
|
const auto * key_ipv6type = typeid_cast<const DataTypeFixedString *>(key_types[0].get());
|
|
|
|
|
|
|
|
if (key_ipv4type == nullptr && (key_ipv6type == nullptr || key_ipv6type->getN() != 16))
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::TYPE_MISMATCH,
|
|
|
|
"Key does not match, expected either `IPv4` (`UInt32`) or `IPv6` (`FixedString(16)`)");
|
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)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::TYPE_MISMATCH, "Mask do not match, expected UInt8");
|
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-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_,
|
2021-01-29 23:25:28 +00:00
|
|
|
bool require_nonempty_)
|
2021-03-24 16:31:00 +00:00
|
|
|
: IDictionary(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_)
|
2021-01-29 23:25:28 +00:00
|
|
|
, access_to_key_from_attributes(dict_struct_.access_to_key_from_attributes)
|
2020-11-21 18:38:10 +00:00
|
|
|
, logger(&Poco::Logger::get("IPAddressDictionary"))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
createAttributes();
|
2020-07-08 10:55:39 +00:00
|
|
|
loadData();
|
|
|
|
calculateBytesAllocated();
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
ColumnPtr IPAddressDictionary::getColumn(
|
|
|
|
const std::string & attribute_name,
|
2021-01-21 14:42:50 +00:00
|
|
|
const DataTypePtr & result_type,
|
2020-12-29 10:09:01 +00:00
|
|
|
const Columns & key_columns,
|
|
|
|
const DataTypes & key_types,
|
2021-02-16 21:33:02 +00:00
|
|
|
const ColumnPtr & default_values_column) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
validateKeyTypes(key_types);
|
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
ColumnPtr result;
|
|
|
|
|
2017-05-12 21:23:12 +00:00
|
|
|
const auto & attribute = getAttribute(attribute_name);
|
2021-01-21 14:42:50 +00:00
|
|
|
const auto & dictionary_attribute = dict_struct.getAttribute(attribute_name, result_type);
|
2020-12-29 15:21:49 +00:00
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
auto size = key_columns.front()->size();
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
auto type_call = [&](const auto &dictionary_attribute_type)
|
|
|
|
{
|
|
|
|
using Type = std::decay_t<decltype(dictionary_attribute_type)>;
|
|
|
|
using AttributeType = typename Type::AttributeType;
|
2021-01-23 13:18:24 +00:00
|
|
|
using ValueType = DictionaryValueType<AttributeType>;
|
2021-01-23 16:47:33 +00:00
|
|
|
using ColumnProvider = DictionaryAttributeColumnProvider<AttributeType>;
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2021-01-25 12:58:26 +00:00
|
|
|
const auto & null_value = std::get<AttributeType>(attribute.null_values);
|
2021-01-23 16:47:33 +00:00
|
|
|
DictionaryDefaultValueExtractor<AttributeType> default_value_extractor(null_value, default_values_column);
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2021-01-23 13:18:24 +00:00
|
|
|
auto column = ColumnProvider::getColumn(dictionary_attribute, size);
|
2018-12-10 15:25:45 +00:00
|
|
|
|
2021-06-09 17:21:32 +00:00
|
|
|
if constexpr (std::is_same_v<ValueType, Array>)
|
2021-01-23 13:18:24 +00:00
|
|
|
{
|
|
|
|
auto * out = column.get();
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2021-06-09 17:21:32 +00:00
|
|
|
getItemsImpl<ValueType>(
|
|
|
|
attribute,
|
|
|
|
key_columns,
|
|
|
|
[&](const size_t, const Array & value) { out->insert(value); },
|
|
|
|
default_value_extractor);
|
|
|
|
}
|
|
|
|
else if constexpr (std::is_same_v<ValueType, StringRef>)
|
|
|
|
{
|
|
|
|
auto * out = column.get();
|
|
|
|
|
|
|
|
getItemsImpl<ValueType>(
|
2021-01-23 13:18:24 +00:00
|
|
|
attribute,
|
|
|
|
key_columns,
|
|
|
|
[&](const size_t, const StringRef value) { out->insertData(value.data, value.size); },
|
|
|
|
default_value_extractor);
|
2020-12-29 10:09:01 +00:00
|
|
|
}
|
2020-12-29 10:46:25 +00:00
|
|
|
else
|
2020-12-29 10:09:01 +00:00
|
|
|
{
|
2020-12-29 15:21:49 +00:00
|
|
|
auto & out = column->getData();
|
2020-12-29 10:09:01 +00:00
|
|
|
|
2021-06-09 17:21:32 +00:00
|
|
|
getItemsImpl<ValueType>(
|
2021-01-23 13:18:24 +00:00
|
|
|
attribute,
|
|
|
|
key_columns,
|
|
|
|
[&](const size_t row, const auto value) { return out[row] = value; },
|
|
|
|
default_value_extractor);
|
2020-12-29 10:09:01 +00:00
|
|
|
}
|
2021-01-23 13:18:24 +00:00
|
|
|
|
|
|
|
result = std::move(column);
|
2020-12-29 10:09:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
callOnDictionaryAttributeType(attribute.type, type_call);
|
2020-12-29 15:21:49 +00:00
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
return result;
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
|
2021-01-23 13:18:24 +00:00
|
|
|
ColumnUInt8::Ptr IPAddressDictionary::hasKeys(const Columns & key_columns, const DataTypes & key_types) const
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
validateKeyTypes(key_types);
|
|
|
|
|
2020-12-24 07:38:10 +00:00
|
|
|
const auto first_column = key_columns.front();
|
|
|
|
const auto rows = first_column->size();
|
2020-12-29 10:09:01 +00:00
|
|
|
|
|
|
|
auto result = ColumnUInt8::create(rows);
|
2021-05-06 06:42:05 +00:00
|
|
|
auto & out = result->getData();
|
|
|
|
|
|
|
|
size_t keys_found = 0;
|
2020-12-29 10:09:01 +00:00
|
|
|
|
2020-12-24 07:38:10 +00:00
|
|
|
if (first_column->isNumeric())
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2020-12-24 07:38:10 +00:00
|
|
|
uint8_t addrv6_buf[IPV6_BINARY_LENGTH];
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, rows))
|
2020-12-24 07:38:10 +00:00
|
|
|
{
|
|
|
|
auto addrv4 = UInt32(first_column->get64(i));
|
|
|
|
auto found = tryLookupIPv4(addrv4, addrv6_buf);
|
|
|
|
out[i] = (found != ipNotFound());
|
2021-05-06 06:42:05 +00:00
|
|
|
keys_found += out[i];
|
2020-12-24 07:38:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, rows))
|
2020-12-24 07:38:10 +00:00
|
|
|
{
|
|
|
|
auto addr = first_column->getDataAt(i);
|
|
|
|
if (unlikely(addr.size != IPV6_BINARY_LENGTH))
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected key to be FixedString(16)");
|
2018-12-10 15:25:45 +00:00
|
|
|
|
2020-12-24 07:38:10 +00:00
|
|
|
auto found = tryLookupIPv6(reinterpret_cast<const uint8_t *>(addr.data));
|
|
|
|
out[i] = (found != ipNotFound());
|
2021-05-06 06:42:05 +00:00
|
|
|
keys_found += out[i];
|
2020-12-24 07:38:10 +00:00
|
|
|
}
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
2020-12-24 07:38:10 +00:00
|
|
|
|
|
|
|
query_count.fetch_add(rows, std::memory_order_relaxed);
|
2021-05-06 06:42:05 +00:00
|
|
|
found_count.fetch_add(keys_found, std::memory_order_relaxed);
|
2020-12-29 10:09:01 +00:00
|
|
|
|
|
|
|
return result;
|
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
|
|
|
{
|
2021-01-29 23:25:28 +00:00
|
|
|
auto create_attributes_from_dictionary_attributes = [this](const std::vector<DictionaryAttribute> & dict_attrs)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2021-01-29 23:25:28 +00:00
|
|
|
attributes.reserve(attributes.size() + dict_attrs.size());
|
2021-06-09 10:43:40 +00:00
|
|
|
|
2021-01-29 23:25:28 +00:00
|
|
|
for (const auto & attribute : dict_attrs)
|
|
|
|
{
|
2021-06-09 10:43:40 +00:00
|
|
|
if (attribute.is_nullable)
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
|
|
|
|
"{}: array or nullable attributes not supported for dictionary of type {}",
|
|
|
|
full_name,
|
|
|
|
getTypeName());
|
|
|
|
|
2021-01-29 23:25:28 +00:00
|
|
|
attribute_index_by_name.emplace(attribute.name, attributes.size());
|
|
|
|
attributes.push_back(createAttributeWithType(attribute.underlying_type, attribute.null_value));
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2021-01-29 23:25:28 +00:00
|
|
|
if (attribute.hierarchical)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::TYPE_MISMATCH,
|
|
|
|
"{}: hierarchical attributes not supported for dictionary of type {}",
|
|
|
|
full_name,
|
|
|
|
getTypeName());
|
2021-01-29 23:25:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
create_attributes_from_dictionary_attributes(dict_struct.attributes);
|
|
|
|
if (access_to_key_from_attributes)
|
|
|
|
create_attributes_from_dictionary_attributes(*dict_struct.key);
|
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
|
|
|
{
|
2021-09-16 17:40:42 +00:00
|
|
|
QueryPipeline pipeline(source_ptr->loadAll());
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
std::vector<IPRecord> ip_records;
|
|
|
|
|
|
|
|
bool has_ipv6 = false;
|
|
|
|
|
2021-08-05 18:08:52 +00:00
|
|
|
PullingPipelineExecutor executor(pipeline);
|
|
|
|
Block block;
|
|
|
|
while (executor.pull(block))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
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;
|
2021-06-15 19:55:21 +00:00
|
|
|
const auto attribute_column_ptrs = collections::map<Columns>(
|
|
|
|
collections::range(0, dict_struct.attributes.size()),
|
2020-12-24 07:31:09 +00:00
|
|
|
[&](const size_t attribute_idx) { return block.safeGetByPosition(attribute_idx + 1).column; });
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto row : collections::range(0, rows))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto attribute_idx : collections::range(0, dict_struct.attributes.size()))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
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-12-24 07:31:09 +00:00
|
|
|
if (access_to_key_from_attributes)
|
|
|
|
{
|
|
|
|
/// We format key attribute values here instead of filling with data from key_column
|
|
|
|
/// because string representation can be normalized if bits beyond mask are set.
|
2020-12-29 10:29:54 +00:00
|
|
|
/// Also all IPv4 will be displayed as mapped IPv6 if there are any IPv6.
|
2020-12-24 07:31:09 +00:00
|
|
|
/// It's consistent with representation in table created with `ENGINE = Dictionary` from this dictionary.
|
|
|
|
char str_buffer[48];
|
|
|
|
if (has_ipv6)
|
|
|
|
{
|
|
|
|
uint8_t ip_buffer[IPV6_BINARY_LENGTH];
|
|
|
|
for (const auto & record : ip_records)
|
|
|
|
{
|
|
|
|
size_t str_len = formatIPWithPrefix(record.asIPv6Binary(ip_buffer), record.prefixIPv6(), false, str_buffer);
|
|
|
|
setAttributeValue(attributes.back(), String(str_buffer, str_len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto & record : ip_records)
|
|
|
|
{
|
|
|
|
UInt32 addr = IPv4AsUInt32(record.addr.addr());
|
|
|
|
size_t str_len = formatIPWithPrefix(reinterpret_cast<const unsigned char *>(&addr), record.prefix, true, str_buffer);
|
|
|
|
setAttributeValue(attributes.back(), String(str_buffer, str_len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2021-05-09 19:31:44 +00:00
|
|
|
LOG_TRACE(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)
|
2021-05-09 19:31:44 +00:00
|
|
|
LOG_TRACE(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;
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, ip_records.size()))
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
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)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::DICTIONARY_IS_EMPTY, "{}: dictionary source is empty and 'require_nonempty' property is set.", full_name);
|
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-12-29 10:09:01 +00:00
|
|
|
template <>
|
|
|
|
void IPAddressDictionary::addAttributeSize<String>(const Attribute & attribute)
|
|
|
|
{
|
|
|
|
addAttributeSize<StringRef>(attribute);
|
|
|
|
bytes_allocated += sizeof(Arena) + attribute.string_arena->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)
|
|
|
|
{
|
2020-12-29 10:09:01 +00:00
|
|
|
auto type_call = [&](const auto & dictionary_attribute_type)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2020-12-29 10:09:01 +00:00
|
|
|
using Type = std::decay_t<decltype(dictionary_attribute_type)>;
|
|
|
|
using AttributeType = typename Type::AttributeType;
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
addAttributeSize<AttributeType>(attribute);
|
|
|
|
};
|
|
|
|
|
|
|
|
callOnDictionaryAttributeType(attribute.type, type_call);
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-03-22 20:23:44 +00:00
|
|
|
attribute.null_values = null_value.isNull() ? T{} : T(null_value.get<T>());
|
2018-11-13 19:43:17 +00:00
|
|
|
attribute.maps.emplace<ContainerType<T>>();
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
template <>
|
|
|
|
void IPAddressDictionary::createAttributeImpl<String>(Attribute & attribute, const Field & null_value)
|
|
|
|
{
|
|
|
|
attribute.null_values = null_value.isNull() ? String() : null_value.get<String>();
|
|
|
|
attribute.maps.emplace<ContainerType<StringRef>>();
|
|
|
|
attribute.string_arena = std::make_unique<Arena>();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
auto type_call = [&](const auto & dictionary_attribute_type)
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
2020-12-29 10:09:01 +00:00
|
|
|
using Type = std::decay_t<decltype(dictionary_attribute_type)>;
|
|
|
|
using AttributeType = typename Type::AttributeType;
|
2020-12-24 07:31:09 +00:00
|
|
|
|
2020-12-29 10:09:01 +00:00
|
|
|
createAttributeImpl<AttributeType>(attr, null_value);
|
|
|
|
};
|
|
|
|
|
|
|
|
callOnDictionaryAttributeType(type, type_call);
|
2017-05-12 21:23:12 +00:00
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2021-06-09 17:21:32 +00:00
|
|
|
template <typename AttributeType, typename ValueSetter, typename DefaultValueExtractor>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::getItemsByTwoKeyColumnsImpl(
|
2021-01-23 13:18:24 +00:00
|
|
|
const Attribute & attribute,
|
|
|
|
const Columns & key_columns,
|
|
|
|
ValueSetter && set_value,
|
2021-01-23 16:47:33 +00:00
|
|
|
DefaultValueExtractor & default_value_extractor) 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)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::TYPE_MISMATCH, "Expected a UInt32 IP column");
|
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
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, rows))
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
UInt32 addr = key_ip_column_ptr->getElement(i);
|
|
|
|
UInt8 mask = key_mask_column.getElement(i);
|
2020-11-08 20:50:49 +00:00
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
auto range = collections::range(0, row_idx.size());
|
2020-11-11 21:58:30 +00:00
|
|
|
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
|
|
|
{
|
2021-06-09 17:21:32 +00:00
|
|
|
set_value(i, vec[row_idx[*found_it]]);
|
2020-11-08 20:50:49 +00:00
|
|
|
}
|
|
|
|
else
|
2021-01-23 13:18:24 +00:00
|
|
|
set_value(i, default_value_extractor[i]);
|
2020-11-08 20:50:49 +00:00
|
|
|
}
|
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)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::TYPE_MISMATCH, "Expected a FixedString(16) IP column");
|
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;
|
|
|
|
};
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, rows))
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
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};
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
auto range = collections::range(0, row_idx.size());
|
2020-11-11 21:58:30 +00:00
|
|
|
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))
|
2021-06-09 17:21:32 +00:00
|
|
|
set_value(i, vec[row_idx[*found_it]]);
|
2020-11-11 21:58:30 +00:00
|
|
|
else
|
2021-01-23 13:18:24 +00:00
|
|
|
set_value(i, default_value_extractor[i]);
|
2020-11-11 21:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 17:21:32 +00:00
|
|
|
template <typename AttributeType, typename ValueSetter, typename DefaultValueExtractor>
|
2020-11-21 18:38:10 +00:00
|
|
|
void IPAddressDictionary::getItemsImpl(
|
2021-01-23 13:18:24 +00:00
|
|
|
const Attribute & attribute,
|
|
|
|
const Columns & key_columns,
|
|
|
|
ValueSetter && set_value,
|
2021-01-23 16:47:33 +00:00
|
|
|
DefaultValueExtractor & default_value_extractor) const
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
const auto first_column = key_columns.front();
|
|
|
|
const auto rows = first_column->size();
|
|
|
|
|
|
|
|
// special case for getBlockInputStream
|
|
|
|
if (unlikely(key_columns.size() == 2))
|
|
|
|
{
|
2021-06-09 17:21:32 +00:00
|
|
|
getItemsByTwoKeyColumnsImpl<AttributeType>(
|
2021-01-23 13:18:24 +00:00
|
|
|
attribute, key_columns, std::forward<ValueSetter>(set_value), default_value_extractor);
|
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);
|
|
|
|
|
2021-05-06 06:42:05 +00:00
|
|
|
size_t keys_found = 0;
|
|
|
|
|
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];
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, rows))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
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())
|
2021-05-06 06:42:05 +00:00
|
|
|
{
|
2021-06-09 17:21:32 +00:00
|
|
|
set_value(i, vec[*found]);
|
2021-05-06 06:42:05 +00:00
|
|
|
++keys_found;
|
|
|
|
}
|
2020-11-21 14:56:58 +00:00
|
|
|
else
|
2021-01-23 13:18:24 +00:00
|
|
|
set_value(i, default_value_extractor[i]);
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
for (const auto i : collections::range(0, rows))
|
2017-05-12 21:23:12 +00:00
|
|
|
{
|
|
|
|
auto addr = first_column->getDataAt(i);
|
2020-11-08 16:01:12 +00:00
|
|
|
if (addr.size != IPV6_BINARY_LENGTH)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected key to be FixedString(16)");
|
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())
|
2021-05-06 06:42:05 +00:00
|
|
|
{
|
2021-06-09 17:21:32 +00:00
|
|
|
set_value(i, vec[*found]);
|
2021-05-06 06:42:05 +00:00
|
|
|
++keys_found;
|
|
|
|
}
|
2020-11-21 14:56:58 +00:00
|
|
|
else
|
2021-01-23 13:18:24 +00:00
|
|
|
set_value(i, default_value_extractor[i]);
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
query_count.fetch_add(rows, std::memory_order_relaxed);
|
2021-05-06 06:42:05 +00:00
|
|
|
found_count.fetch_add(keys_found, std::memory_order_relaxed);
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-12-29 15:21:49 +00:00
|
|
|
auto type_call = [&](const auto & dictionary_attribute_type)
|
|
|
|
{
|
2020-12-29 10:09:01 +00:00
|
|
|
using Type = std::decay_t<decltype(dictionary_attribute_type)>;
|
|
|
|
using AttributeType = typename Type::AttributeType;
|
|
|
|
|
|
|
|
if constexpr (std::is_same_v<AttributeType, String>)
|
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-12-29 15:21:49 +00:00
|
|
|
setAttributeValueImpl<StringRef>(attribute, StringRef{string_in_arena, string.size()});
|
2017-05-12 21:23:12 +00:00
|
|
|
}
|
2020-12-29 10:09:01 +00:00
|
|
|
else
|
|
|
|
{
|
2021-03-22 20:23:44 +00:00
|
|
|
setAttributeValueImpl<AttributeType>(attribute, value.get<AttributeType>());
|
2020-12-29 10:09:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
callOnDictionaryAttributeType(attribute.type, type_call);
|
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))
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "{}: no such attribute '{}'", full_name, attribute_name);
|
2017-05-12 21:23:12 +00:00
|
|
|
|
|
|
|
return attributes[it->second];
|
|
|
|
}
|
|
|
|
|
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();
|
2021-06-15 19:55:21 +00:00
|
|
|
for (size_t row : collections::range(0, row_idx.size()))
|
2020-11-11 21:58:30 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (size_t row : collections::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]);
|
|
|
|
}
|
2021-10-21 14:17:53 +00:00
|
|
|
|
2020-11-11 21:58:30 +00:00
|
|
|
return {std::move(key_ip_column), std::move(key_mask_column)};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename KeyColumnType, bool IsIPv4>
|
|
|
|
static auto keyViewGetter()
|
|
|
|
{
|
2021-10-22 11:18:45 +00:00
|
|
|
return [](const Columns & columns, const std::vector<DictionaryAttribute> & dictonary_key_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];
|
2021-06-15 19:55:21 +00:00
|
|
|
for (size_t row : collections::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);
|
2020-12-24 07:31:09 +00:00
|
|
|
size_t str_len;
|
2020-11-11 21:58:30 +00:00
|
|
|
if constexpr (IsIPv4)
|
2020-12-24 07:31:09 +00:00
|
|
|
str_len = formatIPWithPrefix(reinterpret_cast<const unsigned char *>(&key_ip_column.getElement(row)), mask, true, buffer);
|
2020-11-11 21:58:30 +00:00
|
|
|
else
|
2020-12-24 07:31:09 +00:00
|
|
|
str_len = formatIPWithPrefix(reinterpret_cast<const unsigned char *>(key_ip_column.getDataAt(row).data), mask, false, buffer);
|
|
|
|
column->insertData(buffer, str_len);
|
2017-06-05 09:02:05 +00:00
|
|
|
}
|
2018-12-10 15:25:45 +00:00
|
|
|
return ColumnsWithTypeAndName{
|
2021-10-22 11:18:45 +00:00
|
|
|
ColumnWithTypeAndName(std::move(column), std::make_shared<DataTypeString>(), dictonary_key_attributes.front().name)};
|
2017-06-05 09:02:05 +00:00
|
|
|
};
|
|
|
|
}
|
2017-05-12 21:23:12 +00:00
|
|
|
|
2021-10-21 14:17:53 +00:00
|
|
|
Pipe IPAddressDictionary::read(const Names & column_names, size_t max_block_size, size_t num_streams) const
|
2020-11-08 16:01:12 +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
|
|
|
|
2021-10-21 14:17:53 +00:00
|
|
|
auto key_columns = getKeyColumns();
|
|
|
|
|
2021-10-22 11:18:45 +00:00
|
|
|
std::shared_ptr<const IDataType> key_type;
|
|
|
|
if (is_ipv4)
|
|
|
|
key_type = std::make_shared<DataTypeUInt32>();
|
|
|
|
else
|
|
|
|
key_type = std::make_shared<DataTypeFixedString>(IPV6_BINARY_LENGTH);
|
|
|
|
|
|
|
|
ColumnsWithTypeAndName key_columns_with_type = {
|
|
|
|
ColumnWithTypeAndName(key_columns.front(), key_type, ""),
|
|
|
|
ColumnWithTypeAndName(key_columns.back(), std::make_shared<DataTypeUInt8>(), "")
|
|
|
|
};
|
2021-10-21 14:17:53 +00:00
|
|
|
|
|
|
|
ColumnsWithTypeAndName view_columns;
|
|
|
|
|
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>();
|
2021-10-22 11:18:45 +00:00
|
|
|
view_columns = get_view(key_columns, *dict_struct.key);
|
2021-10-21 14:17:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto get_view = keyViewGetter<ColumnFixedString, false>();
|
2021-10-22 11:18:45 +00:00
|
|
|
view_columns = get_view(key_columns, *dict_struct.key);
|
2021-10-21 14:17:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const IDictionary> dictionary = shared_from_this();
|
|
|
|
auto coordinator = std::make_shared<DictionarySourceCoordinator>(dictionary, column_names, std::move(key_columns_with_type), std::move(view_columns), max_block_size);
|
|
|
|
|
|
|
|
Pipes pipes;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_streams; ++i)
|
|
|
|
{
|
|
|
|
auto source = std::make_shared<DictionarySource>(coordinator);
|
|
|
|
pipes.emplace_back(Pipe(std::move(source)));
|
2020-11-11 21:58:30 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 14:17:53 +00:00
|
|
|
return Pipe::unitePipes(std::move(pipes));
|
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
|
|
|
};
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
auto range = collections::range(0, row_idx.size());
|
2020-11-11 21:58:30 +00:00
|
|
|
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,
|
2021-05-24 21:27:24 +00:00
|
|
|
DictionarySourcePtr source_ptr,
|
2021-08-12 15:16:55 +00:00
|
|
|
ContextPtr /* global_context */,
|
2021-05-24 21:27:24 +00:00
|
|
|
bool /*created_from_ddl*/) -> DictionaryPtr
|
2018-12-10 15:50:58 +00:00
|
|
|
{
|
2020-12-23 16:58:54 +00:00
|
|
|
if (!dict_struct.key || dict_struct.key->size() != 1)
|
2021-04-10 18:48:36 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Dictionary of layout 'ip_trie' has to have one 'key'");
|
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-24 07:31:09 +00:00
|
|
|
|
2020-12-23 16:58:54 +00:00
|
|
|
// This is specialised dictionary for storing IPv4 and IPv6 prefixes.
|
2021-01-29 23:25:28 +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
|
|
|
}
|
|
|
|
|
2021-08-06 08:41:45 +00:00
|
|
|
}
|