2022-12-12 23:42:17 +00:00
|
|
|
#include <functional>
|
2021-10-03 13:42:36 +00:00
|
|
|
#ifdef HAS_RESERVED_IDENTIFIER
|
2021-08-25 20:16:35 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wreserved-identifier"
|
|
|
|
#endif
|
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
#include <Functions/FunctionsCodingIP.h>
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2022-03-12 17:12:05 +00:00
|
|
|
#include <Columns/ColumnNullable.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
2019-04-22 11:21:59 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2022-11-18 20:10:27 +00:00
|
|
|
#include <DataTypes/DataTypeIPv4andIPv6.h>
|
2021-07-16 14:03:25 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2022-03-12 17:12:05 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Common/IPv6ToBinary.h>
|
|
|
|
#include <Common/formatIPv6.h>
|
|
|
|
#include <Common/hex.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2014-10-30 14:43:21 +00:00
|
|
|
|
2014-10-29 12:25:33 +00:00
|
|
|
#include <arpa/inet.h>
|
2019-02-09 19:44:27 +00:00
|
|
|
#include <type_traits>
|
2014-10-30 14:43:21 +00:00
|
|
|
#include <array>
|
2013-03-05 09:32:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2019-03-14 23:10:51 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2017-06-13 02:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-16 22:18:41 +00:00
|
|
|
/** Encoding functions for network addresses:
|
2014-06-26 00:58:14 +00:00
|
|
|
*
|
2022-11-21 07:45:42 +00:00
|
|
|
* IPv6NumToString (num) - See below.
|
|
|
|
* IPv6StringToNum(string) - Convert, for example, '::1' to 1 and vice versa.
|
2013-03-07 12:43:54 +00:00
|
|
|
*/
|
2015-09-02 13:05:17 +00:00
|
|
|
class FunctionIPv6NumToString : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "IPv6NumToString";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIPv6NumToString>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-10-11 23:36:45 +00:00
|
|
|
String getName() const override { return name; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-10-09 07:41:28 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2022-11-21 07:45:42 +00:00
|
|
|
const auto * arg_string = checkAndGetDataType<DataTypeFixedString>(arguments[0].get());
|
|
|
|
const auto * arg_ipv6 = checkAndGetDataType<DataTypeIPv6>(arguments[0].get());
|
|
|
|
if (!arg_ipv6 && !(arg_string && arg_string->getN() == IPV6_BINARY_LENGTH))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of argument of function {}, expected IPv6 or FixedString({})",
|
|
|
|
arguments[0]->getName(), getName(), IPV6_BINARY_LENGTH
|
|
|
|
);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
return std::make_shared<DataTypeString>();
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2022-11-19 06:55:05 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2014-10-29 12:25:33 +00:00
|
|
|
{
|
2022-11-21 07:45:42 +00:00
|
|
|
const ColumnPtr & column = arguments[0].column;
|
|
|
|
const auto * col_ipv6 = checkAndGetColumn<ColumnIPv6>(column.get());
|
|
|
|
const auto * col_string = checkAndGetColumn<ColumnFixedString>(column.get());
|
|
|
|
if (!col_ipv6 && !(col_string && col_string->getN() == IPV6_BINARY_LENGTH))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal column {} of argument of function {}, expected IPv6 or FixedString({})",
|
|
|
|
arguments[0].name, getName(), IPV6_BINARY_LENGTH
|
|
|
|
);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-21 07:45:42 +00:00
|
|
|
auto col_res = ColumnString::create();
|
|
|
|
ColumnString::Chars & vec_res = col_res->getChars();
|
|
|
|
ColumnString::Offsets & offsets_res = col_res->getOffsets();
|
|
|
|
vec_res.resize(input_rows_count * (IPV6_MAX_TEXT_LENGTH + 1));
|
|
|
|
offsets_res.resize(input_rows_count);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-21 07:45:42 +00:00
|
|
|
auto * begin = reinterpret_cast<char *>(vec_res.data());
|
|
|
|
auto * pos = begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-21 07:45:42 +00:00
|
|
|
if (col_ipv6)
|
|
|
|
{
|
|
|
|
const auto & vec_in = col_ipv6->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-19 06:55:05 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2014-10-29 12:25:33 +00:00
|
|
|
{
|
2022-11-19 06:55:05 +00:00
|
|
|
formatIPv6(reinterpret_cast<const unsigned char *>(&vec_in[i]), pos);
|
2014-10-29 12:25:33 +00:00
|
|
|
offsets_res[i] = pos - begin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-11-21 07:45:42 +00:00
|
|
|
{
|
|
|
|
const auto & vec_in = col_string->getChars();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
formatIPv6(reinterpret_cast<const unsigned char *>(&vec_in[i * IPV6_BINARY_LENGTH]), pos);
|
|
|
|
offsets_res[i] = pos - begin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vec_res.resize(pos - begin);
|
|
|
|
return col_res;
|
2014-10-29 12:25:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-11 10:57:34 +00:00
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
class FunctionCutIPv6 : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "cutIPv6";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionCutIPv6>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-10-11 23:36:45 +00:00
|
|
|
String getName() const override { return name; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 3; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto * ptr = checkAndGetDataType<DataTypeFixedString>(arguments[0].get());
|
2018-12-13 13:41:47 +00:00
|
|
|
if (!ptr || ptr->getN() != IPV6_BINARY_LENGTH)
|
2015-09-02 13:05:17 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() +
|
|
|
|
" of argument 1 of function " + getName() +
|
2018-12-13 13:41:47 +00:00
|
|
|
", expected FixedString(" + toString(IPV6_BINARY_LENGTH) + ")",
|
2015-09-02 13:05:17 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!WhichDataType(arguments[1]).isUInt8())
|
2015-09-02 13:05:17 +00:00
|
|
|
throw Exception("Illegal type " + arguments[1]->getName() +
|
|
|
|
" of argument 2 of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!WhichDataType(arguments[2]).isUInt8())
|
2015-09-02 13:05:17 +00:00
|
|
|
throw Exception("Illegal type " + arguments[2]->getName() +
|
|
|
|
" of argument 3 of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
return std::make_shared<DataTypeString>();
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto & col_type_name = arguments[0];
|
2016-12-11 10:57:34 +00:00
|
|
|
const ColumnPtr & column = col_type_name.column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto & col_ipv6_zeroed_tail_bytes_type = arguments[1];
|
2015-09-02 13:05:17 +00:00
|
|
|
const auto & col_ipv6_zeroed_tail_bytes = col_ipv6_zeroed_tail_bytes_type.column;
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto & col_ipv4_zeroed_tail_bytes_type = arguments[2];
|
2015-09-02 13:05:17 +00:00
|
|
|
const auto & col_ipv4_zeroed_tail_bytes = col_ipv4_zeroed_tail_bytes_type.column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
if (const auto * col_in = checkAndGetColumn<ColumnFixedString>(column.get()))
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
if (col_in->getN() != IPV6_BINARY_LENGTH)
|
2016-12-11 10:57:34 +00:00
|
|
|
throw Exception("Illegal type " + col_type_name.type->getName() +
|
2015-09-02 13:05:17 +00:00
|
|
|
" of column " + col_in->getName() +
|
|
|
|
" argument of function " + getName() +
|
2018-12-13 13:41:47 +00:00
|
|
|
", expected FixedString(" + toString(IPV6_BINARY_LENGTH) + ")",
|
2015-09-02 13:05:17 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto * ipv6_zeroed_tail_bytes = checkAndGetColumnConst<ColumnVector<UInt8>>(col_ipv6_zeroed_tail_bytes.get());
|
2015-09-02 13:05:17 +00:00
|
|
|
if (!ipv6_zeroed_tail_bytes)
|
|
|
|
throw Exception("Illegal type " + col_ipv6_zeroed_tail_bytes_type.type->getName() +
|
|
|
|
" of argument 2 of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
UInt8 ipv6_zeroed_tail_bytes_count = ipv6_zeroed_tail_bytes->getValue<UInt8>();
|
2018-12-13 13:41:47 +00:00
|
|
|
if (ipv6_zeroed_tail_bytes_count > IPV6_BINARY_LENGTH)
|
2015-09-02 13:05:17 +00:00
|
|
|
throw Exception("Illegal value for argument 2 " + col_ipv6_zeroed_tail_bytes_type.type->getName() +
|
|
|
|
" of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto * ipv4_zeroed_tail_bytes = checkAndGetColumnConst<ColumnVector<UInt8>>(col_ipv4_zeroed_tail_bytes.get());
|
2015-09-02 13:05:17 +00:00
|
|
|
if (!ipv4_zeroed_tail_bytes)
|
|
|
|
throw Exception("Illegal type " + col_ipv4_zeroed_tail_bytes_type.type->getName() +
|
|
|
|
" of argument 3 of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
UInt8 ipv4_zeroed_tail_bytes_count = ipv4_zeroed_tail_bytes->getValue<UInt8>();
|
2018-12-13 13:41:47 +00:00
|
|
|
if (ipv4_zeroed_tail_bytes_count > IPV6_BINARY_LENGTH)
|
2015-09-02 13:05:17 +00:00
|
|
|
throw Exception("Illegal value for argument 3 " + col_ipv4_zeroed_tail_bytes_type.type->getName() +
|
|
|
|
" of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
const auto size = col_in->size();
|
|
|
|
const auto & vec_in = col_in->getChars();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & vec_res = col_res->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & offsets_res = col_res->getOffsets();
|
2017-06-13 05:14:24 +00:00
|
|
|
vec_res.resize(size * (IPV6_MAX_TEXT_LENGTH + 1));
|
2015-09-02 13:05:17 +00:00
|
|
|
offsets_res.resize(size);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
auto * begin = reinterpret_cast<char *>(vec_res.data());
|
|
|
|
auto * pos = begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
for (size_t offset = 0, i = 0; offset < vec_in.size(); offset += IPV6_BINARY_LENGTH, ++i)
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto * address = &vec_in[offset];
|
2015-09-02 13:05:17 +00:00
|
|
|
UInt8 zeroed_tail_bytes_count = isIPv4Mapped(address) ? ipv4_zeroed_tail_bytes_count : ipv6_zeroed_tail_bytes_count;
|
2020-01-03 14:52:37 +00:00
|
|
|
cutAddress(reinterpret_cast<const unsigned char *>(address), pos, zeroed_tail_bytes_count);
|
2015-09-02 13:05:17 +00:00
|
|
|
offsets_res[i] = pos - begin;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
vec_res.resize(pos - begin);
|
2017-12-16 02:34:02 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_res;
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2021-07-16 14:03:25 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-18 14:49:19 +00:00
|
|
|
static bool isIPv4Mapped(const UInt8 * address)
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2022-11-09 21:54:32 +00:00
|
|
|
return (unalignedLoadLE<UInt64>(address) == 0) &&
|
|
|
|
((unalignedLoadLE<UInt64>(address + 8) & 0x00000000FFFFFFFFull) == 0x00000000FFFF0000ull);
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
static void cutAddress(const unsigned char * address, char *& dst, UInt8 zeroed_tail_bytes_count)
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2017-06-13 05:14:24 +00:00
|
|
|
formatIPv6(address, dst, zeroed_tail_bytes_count);
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
template <IPStringToNumExceptionMode exception_mode>
|
2014-10-29 12:49:19 +00:00
|
|
|
class FunctionIPv6StringToNum : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2022-03-12 17:12:05 +00:00
|
|
|
static constexpr auto name = exception_mode == IPStringToNumExceptionMode::Throw
|
|
|
|
? "IPv6StringToNum"
|
|
|
|
: (exception_mode == IPStringToNumExceptionMode::Default ? "IPv6StringToNumOrDefault" : "IPv6StringToNumOrNull");
|
|
|
|
|
|
|
|
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionIPv6StringToNum>(context); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
explicit FunctionIPv6StringToNum(ContextPtr context)
|
|
|
|
: cast_ipv4_ipv6_default_on_conversion_error(context->getSettingsRef().cast_ipv4_ipv6_default_on_conversion_error)
|
2021-01-25 23:55:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-11 23:36:45 +00:00
|
|
|
String getName() const override { return name; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2022-05-16 19:15:18 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2014-10-29 12:49:19 +00:00
|
|
|
{
|
2022-05-16 19:15:18 +00:00
|
|
|
if (!isStringOrFixedString(removeNullable(arguments[0])))
|
2022-03-12 17:12:05 +00:00
|
|
|
{
|
2021-01-25 23:55:56 +00:00
|
|
|
throw Exception(
|
2022-03-12 17:12:05 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}", arguments[0]->getName(), getName());
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-21 07:45:42 +00:00
|
|
|
auto result_type = std::make_shared<DataTypeFixedString>(IPV6_BINARY_LENGTH);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
|
|
|
|
{
|
|
|
|
return makeNullable(result_type);
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:15:18 +00:00
|
|
|
return arguments[0]->isNullable() ? makeNullable(result_type) : result_type;
|
2022-03-12 17:12:05 +00:00
|
|
|
}
|
2017-07-23 08:40:43 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2014-10-29 12:49:19 +00:00
|
|
|
{
|
2022-05-16 19:15:18 +00:00
|
|
|
ColumnPtr column = arguments[0].column;
|
|
|
|
ColumnPtr null_map_column;
|
|
|
|
const NullMap * null_map = nullptr;
|
|
|
|
if (column->isNullable())
|
|
|
|
{
|
|
|
|
const auto * column_nullable = assert_cast<const ColumnNullable *>(column.get());
|
|
|
|
column = column_nullable->getNestedColumnPtr();
|
|
|
|
null_map_column = column_nullable->getNullMapColumnPtr();
|
|
|
|
null_map = &column_nullable->getNullMapData();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
if constexpr (exception_mode == IPStringToNumExceptionMode::Throw)
|
2014-10-29 12:49:19 +00:00
|
|
|
{
|
2022-03-12 17:12:05 +00:00
|
|
|
if (cast_ipv4_ipv6_default_on_conversion_error)
|
2014-10-29 12:49:19 +00:00
|
|
|
{
|
2022-11-21 07:45:42 +00:00
|
|
|
auto result = convertToIPv6<IPStringToNumExceptionMode::Default, ColumnFixedString>(column, null_map);
|
2022-05-16 19:15:18 +00:00
|
|
|
if (null_map && !result->isNullable())
|
|
|
|
return ColumnNullable::create(result, null_map_column);
|
|
|
|
return result;
|
2014-10-29 12:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-12 17:12:05 +00:00
|
|
|
|
2022-11-21 07:45:42 +00:00
|
|
|
auto result = convertToIPv6<exception_mode, ColumnFixedString>(column, null_map);
|
2022-05-16 19:15:18 +00:00
|
|
|
if (null_map && !result->isNullable())
|
|
|
|
return ColumnNullable::create(IColumn::mutate(result), IColumn::mutate(null_map_column));
|
|
|
|
return result;
|
2014-10-29 12:49:19 +00:00
|
|
|
}
|
2022-03-12 17:12:05 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool cast_ipv4_ipv6_default_on_conversion_error = false;
|
2014-10-29 12:49:19 +00:00
|
|
|
};
|
2016-12-11 10:57:34 +00:00
|
|
|
|
2022-11-18 20:10:27 +00:00
|
|
|
|
|
|
|
/** If mask_tail_octets > 0, the last specified number of octets will be filled with "xxx".
|
|
|
|
*/
|
|
|
|
template <size_t mask_tail_octets, typename Name>
|
|
|
|
class FunctionIPv4NumToString : public IFunction
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template <typename ArgType>
|
|
|
|
ColumnPtr executeTyped(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const
|
|
|
|
{
|
|
|
|
using ColumnType = ColumnVector<ArgType>;
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnPtr & column = arguments[0].column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-18 20:10:27 +00:00
|
|
|
if (const ColumnType * col = typeid_cast<const ColumnType *>(column.get()))
|
2013-03-05 09:32:12 +00:00
|
|
|
{
|
2022-11-18 20:10:27 +00:00
|
|
|
const typename ColumnType::Container & vec_in = col->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
auto col_res = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & vec_res = col_res->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & offsets_res = col_res->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-13 05:14:24 +00:00
|
|
|
vec_res.resize(vec_in.size() * (IPV4_MAX_TEXT_LENGTH + 1)); /// the longest value is: 255.255.255.255\0
|
2013-03-05 09:32:12 +00:00
|
|
|
offsets_res.resize(vec_in.size());
|
2018-09-02 03:00:04 +00:00
|
|
|
char * begin = reinterpret_cast<char *>(vec_res.data());
|
2013-03-05 09:32:12 +00:00
|
|
|
char * pos = begin;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2013-03-05 09:32:12 +00:00
|
|
|
for (size_t i = 0; i < vec_in.size(); ++i)
|
|
|
|
{
|
2022-11-21 07:45:42 +00:00
|
|
|
DB::formatIPv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), sizeof(ArgType), pos, mask_tail_octets, "xxx");
|
2013-03-05 09:32:12 +00:00
|
|
|
offsets_res[i] = pos - begin;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2013-03-05 09:32:12 +00:00
|
|
|
vec_res.resize(pos - begin);
|
2017-12-14 03:56:56 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_res;
|
2013-03-05 09:32:12 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2021-07-16 14:03:25 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2013-03-05 09:32:12 +00:00
|
|
|
}
|
2022-11-18 20:10:27 +00:00
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIPv4NumToString<mask_tail_octets, Name>>(); }
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return mask_tail_octets == 0; }
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
WhichDataType arg_type(arguments[0]);
|
|
|
|
if (!(arg_type.isIPv4() || arg_type.isUInt8() || arg_type.isUInt16() || arg_type.isUInt32()))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of first argument of function {}, expected IPv4 or UInt8 or UInt16 or UInt32",
|
|
|
|
arguments[0]->getName(), getName()
|
|
|
|
);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & ret_type, size_t input_rows_count) const override
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (arguments[0].type->getTypeId())
|
|
|
|
{
|
|
|
|
case TypeIndex::IPv4: return executeTyped<IPv4>(arguments, ret_type, input_rows_count);
|
|
|
|
case TypeIndex::UInt8: return executeTyped<UInt8>(arguments, ret_type, input_rows_count);
|
|
|
|
case TypeIndex::UInt16: return executeTyped<UInt16>(arguments, ret_type, input_rows_count);
|
|
|
|
case TypeIndex::UInt32: return executeTyped<UInt32>(arguments, ret_type, input_rows_count);
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of argument of function {}, expected IPv4 or UInt8 or UInt16 or UInt32",
|
|
|
|
arguments[0].column->getName(), getName()
|
|
|
|
);
|
|
|
|
}
|
2013-03-05 09:32:12 +00:00
|
|
|
};
|
2013-03-05 10:29:26 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
template <IPStringToNumExceptionMode exception_mode>
|
2013-03-05 10:29:26 +00:00
|
|
|
class FunctionIPv4StringToNum : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2022-03-12 17:12:05 +00:00
|
|
|
static constexpr auto name = exception_mode == IPStringToNumExceptionMode::Throw
|
|
|
|
? "IPv4StringToNum"
|
|
|
|
: (exception_mode == IPStringToNumExceptionMode::Default ? "IPv4StringToNumOrDefault" : "IPv4StringToNumOrNull");
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
static FunctionPtr create(ContextPtr context) { return std::make_shared<FunctionIPv4StringToNum>(context); }
|
|
|
|
|
|
|
|
explicit FunctionIPv4StringToNum(ContextPtr context)
|
|
|
|
: cast_ipv4_ipv6_default_on_conversion_error(context->getSettingsRef().cast_ipv4_ipv6_default_on_conversion_error)
|
2013-03-05 10:29:26 +00:00
|
|
|
{
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
String getName() const override { return name; }
|
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2022-05-16 19:15:18 +00:00
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
|
2016-07-06 09:47:55 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2013-03-05 10:29:26 +00:00
|
|
|
{
|
2022-05-16 19:15:18 +00:00
|
|
|
if (!isString(removeNullable(arguments[0])))
|
2022-03-12 17:12:05 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}", arguments[0]->getName(), getName());
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
auto result_type = std::make_shared<DataTypeUInt32>();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
if constexpr (exception_mode == IPStringToNumExceptionMode::Null)
|
|
|
|
{
|
|
|
|
return makeNullable(result_type);
|
|
|
|
}
|
2018-12-13 13:41:47 +00:00
|
|
|
|
2022-05-16 19:15:18 +00:00
|
|
|
return arguments[0]->isNullable() ? makeNullable(result_type) : result_type;
|
2013-03-05 10:29:26 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2013-03-05 10:29:26 +00:00
|
|
|
{
|
2022-05-16 19:15:18 +00:00
|
|
|
ColumnPtr column = arguments[0].column;
|
|
|
|
ColumnPtr null_map_column;
|
|
|
|
const NullMap * null_map = nullptr;
|
|
|
|
if (column->isNullable())
|
|
|
|
{
|
|
|
|
const auto * column_nullable = assert_cast<const ColumnNullable *>(column.get());
|
|
|
|
column = column_nullable->getNestedColumnPtr();
|
|
|
|
null_map_column = column_nullable->getNullMapColumnPtr();
|
|
|
|
null_map = &column_nullable->getNullMapData();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
if constexpr (exception_mode == IPStringToNumExceptionMode::Throw)
|
2013-03-05 10:29:26 +00:00
|
|
|
{
|
2022-03-12 17:12:05 +00:00
|
|
|
if (cast_ipv4_ipv6_default_on_conversion_error)
|
2013-03-05 10:29:26 +00:00
|
|
|
{
|
2022-11-18 20:10:27 +00:00
|
|
|
auto result = convertToIPv4<IPStringToNumExceptionMode::Default, ColumnUInt32>(column, null_map);
|
2022-05-16 19:15:18 +00:00
|
|
|
if (null_map && !result->isNullable())
|
|
|
|
return ColumnNullable::create(result, null_map_column);
|
|
|
|
return result;
|
2013-03-05 10:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-12 17:12:05 +00:00
|
|
|
|
2022-11-18 20:10:27 +00:00
|
|
|
auto result = convertToIPv4<exception_mode, ColumnUInt32>(column, null_map);
|
|
|
|
if (null_map && !result->isNullable())
|
|
|
|
return ColumnNullable::create(IColumn::mutate(result), IColumn::mutate(null_map_column));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool cast_ipv4_ipv6_default_on_conversion_error = false;
|
|
|
|
};
|
|
|
|
|
2013-03-07 12:01:41 +00:00
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
class FunctionIPv4ToIPv6 : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2021-07-16 14:03:25 +00:00
|
|
|
static constexpr auto name = "IPv4ToIPv6";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIPv4ToIPv6>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-10-11 23:36:45 +00:00
|
|
|
String getName() const override { return name; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-10-09 07:41:28 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-12-07 21:29:17 +00:00
|
|
|
/// for backward compatibility IPv4ToIPv6 is overloaded, and result type depends on type of argument -
|
|
|
|
/// if it is UInt32 (presenting IPv4) then result is FixedString(16), if IPv4 - result is IPv6
|
2016-07-06 09:47:55 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2022-11-22 12:57:19 +00:00
|
|
|
const auto * dt_uint32 = checkAndGetDataType<DataTypeUInt32>(arguments[0].get());
|
|
|
|
const auto * dt_ipv4 = checkAndGetDataType<DataTypeIPv4>(arguments[0].get());
|
|
|
|
if (!dt_uint32 && !dt_ipv4)
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of argument of function {}", arguments[0]->getName(), getName()
|
|
|
|
);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-22 12:57:19 +00:00
|
|
|
if (dt_uint32)
|
|
|
|
return std::make_shared<DataTypeFixedString>(16);
|
|
|
|
return std::make_shared<DataTypeIPv6>();
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto & col_type_name = arguments[0];
|
2016-12-11 10:57:34 +00:00
|
|
|
const ColumnPtr & column = col_type_name.column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-11-22 12:57:19 +00:00
|
|
|
if (const auto * col_in = checkAndGetColumn<ColumnIPv4>(*column))
|
|
|
|
{
|
|
|
|
auto col_res = ColumnIPv6::create();
|
|
|
|
|
|
|
|
auto & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(col_in->size());
|
|
|
|
|
|
|
|
const auto & vec_in = col_in->getData();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec_res.size(); ++i)
|
|
|
|
mapIPv4ToIPv6(vec_in[i], reinterpret_cast<UInt8 *>(&vec_res[i].toUnderType()));
|
|
|
|
|
|
|
|
return col_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto * col_in = checkAndGetColumn<ColumnUInt32>(*column))
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
auto col_res = ColumnFixedString::create(IPV6_BINARY_LENGTH);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
auto & vec_res = col_res->getChars();
|
2018-12-13 13:41:47 +00:00
|
|
|
vec_res.resize(col_in->size() * IPV6_BINARY_LENGTH);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
const auto & vec_in = col_in->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
for (size_t out_offset = 0, i = 0; out_offset < vec_res.size(); out_offset += IPV6_BINARY_LENGTH, ++i)
|
2015-09-02 13:05:17 +00:00
|
|
|
mapIPv4ToIPv6(vec_in[i], &vec_res[out_offset]);
|
2017-12-16 02:34:02 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_res;
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
2022-11-22 12:57:19 +00:00
|
|
|
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of argument of function {}", arguments[0].column->getName(), getName()
|
|
|
|
);
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-18 14:49:19 +00:00
|
|
|
static void mapIPv4ToIPv6(UInt32 in, UInt8 * buf)
|
2015-09-02 13:05:17 +00:00
|
|
|
{
|
2020-01-03 14:52:37 +00:00
|
|
|
unalignedStore<UInt64>(buf, 0);
|
2022-11-15 16:53:39 +00:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
2022-11-09 05:12:47 +00:00
|
|
|
unalignedStoreLE<UInt64>(buf + 8, 0x00000000FFFF0000ull | (static_cast<UInt64>(ntohl(in)) << 32));
|
2022-11-15 16:53:39 +00:00
|
|
|
#else
|
2022-11-09 05:12:47 +00:00
|
|
|
unalignedStoreLE<UInt64>(buf + 8, 0x00000000FFFF0000ull | (static_cast<UInt64>(__builtin_bswap32(ntohl(in))) << 32));
|
2022-11-15 16:53:39 +00:00
|
|
|
#endif
|
2015-09-02 13:05:17 +00:00
|
|
|
}
|
|
|
|
};
|
2014-12-24 14:45:44 +00:00
|
|
|
|
2017-05-08 10:02:10 +00:00
|
|
|
class FunctionMACNumToString : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "MACNumToString";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionMACNumToString>(); }
|
2017-05-08 10:02:10 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-10-09 07:41:28 +00:00
|
|
|
bool isInjective(const ColumnsWithTypeAndName &) const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2017-05-08 10:02:10 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!WhichDataType(arguments[0]).isUInt64())
|
2017-05-08 10:02:10 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName() + ", expected UInt64",
|
2021-07-16 14:03:25 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-05-08 10:02:10 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2020-01-03 14:52:37 +00:00
|
|
|
static void formatMAC(UInt64 mac, UInt8 * out)
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2017-07-23 06:13:57 +00:00
|
|
|
/// MAC address is represented in UInt64 in natural order (so, MAC addresses are compared in same order as UInt64).
|
|
|
|
/// Higher two bytes in UInt64 are just ignored.
|
|
|
|
|
2018-09-02 03:10:21 +00:00
|
|
|
writeHexByteUppercase(mac >> 40, &out[0]);
|
2017-07-23 06:13:57 +00:00
|
|
|
out[2] = ':';
|
2017-07-23 06:53:28 +00:00
|
|
|
writeHexByteUppercase(mac >> 32, &out[3]);
|
2017-07-23 06:13:57 +00:00
|
|
|
out[5] = ':';
|
2017-07-23 06:53:28 +00:00
|
|
|
writeHexByteUppercase(mac >> 24, &out[6]);
|
2017-07-23 06:13:57 +00:00
|
|
|
out[8] = ':';
|
2017-07-23 06:53:28 +00:00
|
|
|
writeHexByteUppercase(mac >> 16, &out[9]);
|
2017-07-23 06:13:57 +00:00
|
|
|
out[11] = ':';
|
2017-07-23 06:53:28 +00:00
|
|
|
writeHexByteUppercase(mac >> 8, &out[12]);
|
2017-07-23 06:13:57 +00:00
|
|
|
out[14] = ':';
|
2017-07-23 06:53:28 +00:00
|
|
|
writeHexByteUppercase(mac, &out[15]);
|
2017-07-23 06:13:57 +00:00
|
|
|
out[17] = '\0';
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnPtr & column = arguments[0].column;
|
2017-05-08 10:02:10 +00:00
|
|
|
|
|
|
|
if (const ColumnUInt64 * col = typeid_cast<const ColumnUInt64 *>(column.get()))
|
|
|
|
{
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnUInt64::Container & vec_in = col->getData();
|
2017-05-08 10:02:10 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
auto col_res = ColumnString::create();
|
2017-05-08 10:02:10 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & vec_res = col_res->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & offsets_res = col_res->getOffsets();
|
2017-05-08 10:02:10 +00:00
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
vec_res.resize(vec_in.size() * 18); /// the value is: xx:xx:xx:xx:xx:xx\0
|
2017-05-08 10:02:10 +00:00
|
|
|
offsets_res.resize(vec_in.size());
|
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
size_t current_offset = 0;
|
2017-05-08 10:02:10 +00:00
|
|
|
for (size_t i = 0; i < vec_in.size(); ++i)
|
|
|
|
{
|
2017-07-23 06:13:57 +00:00
|
|
|
formatMAC(vec_in[i], &vec_res[current_offset]);
|
|
|
|
current_offset += 18;
|
|
|
|
offsets_res[i] = current_offset;
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
2017-12-14 03:56:56 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_res;
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2021-07-16 14:03:25 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
struct ParseMACImpl
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2017-07-23 06:13:57 +00:00
|
|
|
static constexpr size_t min_string_size = 17;
|
|
|
|
static constexpr size_t max_string_size = 17;
|
|
|
|
|
|
|
|
/** Example: 01:02:03:04:05:06.
|
|
|
|
* There could be any separators instead of : and them are just ignored.
|
|
|
|
* The order of resulting integers are correspond to the order of MAC address.
|
|
|
|
* If there are any chars other than valid hex digits for bytes, the behaviour is implementation specific.
|
|
|
|
*/
|
|
|
|
static UInt64 parse(const char * pos)
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2022-04-18 08:18:31 +00:00
|
|
|
return (static_cast<UInt64>(unhex(pos[0])) << 44)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[1])) << 40)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[3])) << 36)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[4])) << 32)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[6])) << 28)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[7])) << 24)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[9])) << 20)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[10])) << 16)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[12])) << 12)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[13])) << 8)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[15])) << 4)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[16])));
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
static constexpr auto name = "MACStringToNum";
|
|
|
|
};
|
2017-05-08 10:02:10 +00:00
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
struct ParseOUIImpl
|
|
|
|
{
|
|
|
|
static constexpr size_t min_string_size = 8;
|
|
|
|
static constexpr size_t max_string_size = 17;
|
2017-05-08 10:02:10 +00:00
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
/** OUI is the first three bytes of MAC address.
|
|
|
|
* Example: 01:02:03.
|
|
|
|
*/
|
|
|
|
static UInt64 parse(const char * pos)
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2022-04-18 08:18:31 +00:00
|
|
|
return (static_cast<UInt64>(unhex(pos[0])) << 20)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[1])) << 16)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[3])) << 12)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[4])) << 8)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[6])) << 4)
|
|
|
|
| (static_cast<UInt64>(unhex(pos[7])));
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
static constexpr auto name = "MACStringToOUI";
|
2017-05-15 14:52:47 +00:00
|
|
|
};
|
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
class FunctionMACStringTo : public IFunction
|
2017-05-15 14:52:47 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-07-23 06:13:57 +00:00
|
|
|
static constexpr auto name = Impl::name;
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionMACStringTo<Impl>>(); }
|
2017-05-15 14:52:47 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2017-05-15 14:52:47 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[0]))
|
2017-05-15 14:52:47 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
2021-07-16 14:03:25 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-05-15 14:52:47 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2020-10-18 14:49:19 +00:00
|
|
|
const ColumnPtr & column = arguments[0].column;
|
2017-05-08 10:02:10 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column.get()))
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnUInt64::create();
|
2017-05-08 10:02:10 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnUInt64::Container & vec_res = col_res->getData();
|
2017-05-08 10:02:10 +00:00
|
|
|
vec_res.resize(col->size());
|
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & vec_src = col->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnString::Offsets & offsets_src = col->getOffsets();
|
2017-05-08 10:02:10 +00:00
|
|
|
size_t prev_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec_res.size(); ++i)
|
|
|
|
{
|
2017-07-23 06:13:57 +00:00
|
|
|
size_t current_offset = offsets_src[i];
|
|
|
|
size_t string_size = current_offset - prev_offset - 1; /// mind the terminating zero byte
|
|
|
|
|
|
|
|
if (string_size >= Impl::min_string_size && string_size <= Impl::max_string_size)
|
|
|
|
vec_res[i] = Impl::parse(reinterpret_cast<const char *>(&vec_src[prev_offset]));
|
|
|
|
else
|
|
|
|
vec_res[i] = 0;
|
|
|
|
|
|
|
|
prev_offset = current_offset;
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
2017-12-16 02:34:02 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return col_res;
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
else
|
2020-10-18 14:49:19 +00:00
|
|
|
throw Exception("Illegal column " + arguments[0].column->getName()
|
2021-07-16 14:03:25 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-01 22:09:24 +00:00
|
|
|
class FunctionIPv6CIDRToRange : public IFunction
|
2019-04-22 11:21:59 +00:00
|
|
|
{
|
2019-05-02 13:04:24 +00:00
|
|
|
private:
|
2020-11-29 17:54:46 +00:00
|
|
|
|
|
|
|
#if defined(__SSE2__)
|
|
|
|
|
2021-07-16 14:03:25 +00:00
|
|
|
#include <emmintrin.h>
|
2020-11-29 17:54:46 +00:00
|
|
|
|
2022-11-22 21:34:32 +00:00
|
|
|
static inline void applyCIDRMask(const char * __restrict src, char * __restrict dst_lower, char * __restrict dst_upper, UInt8 bits_to_keep)
|
2019-04-22 11:21:59 +00:00
|
|
|
{
|
2021-03-19 07:24:38 +00:00
|
|
|
__m128i mask = _mm_loadu_si128(reinterpret_cast<const __m128i *>(getCIDRMaskIPv6(bits_to_keep).data()));
|
2020-11-29 17:54:46 +00:00
|
|
|
__m128i lower = _mm_and_si128(_mm_loadu_si128(reinterpret_cast<const __m128i *>(src)), mask);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(dst_lower), lower);
|
2019-04-23 12:28:25 +00:00
|
|
|
|
2020-11-29 17:54:46 +00:00
|
|
|
__m128i inv_mask = _mm_xor_si128(mask, _mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()));
|
|
|
|
__m128i upper = _mm_or_si128(lower, inv_mask);
|
|
|
|
_mm_storeu_si128(reinterpret_cast<__m128i *>(dst_upper), upper);
|
|
|
|
}
|
2019-04-30 06:53:09 +00:00
|
|
|
|
2020-11-29 17:54:46 +00:00
|
|
|
#else
|
2019-04-23 12:28:25 +00:00
|
|
|
|
2020-11-29 17:54:46 +00:00
|
|
|
/// NOTE IPv6 is stored in memory in big endian format that makes some difficulties.
|
2022-11-22 21:34:32 +00:00
|
|
|
static void applyCIDRMask(const char * __restrict src, char * __restrict dst_lower, char * __restrict dst_upper, UInt8 bits_to_keep)
|
2020-11-29 17:54:46 +00:00
|
|
|
{
|
2021-03-19 07:24:38 +00:00
|
|
|
const auto & mask = getCIDRMaskIPv6(bits_to_keep);
|
2019-05-02 13:04:24 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 16; ++i)
|
|
|
|
{
|
|
|
|
dst_lower[i] = src[i] & mask[i];
|
|
|
|
dst_upper[i] = dst_lower[i] | ~mask[i];
|
2019-04-22 11:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 17:54:46 +00:00
|
|
|
#endif
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
public:
|
2019-05-01 22:09:24 +00:00
|
|
|
static constexpr auto name = "IPv6CIDRToRange";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIPv6CIDRToRange>(); }
|
2019-04-22 11:21:59 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
2019-04-22 11:21:59 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2022-11-22 21:34:32 +00:00
|
|
|
const auto * ipv6 = checkAndGetDataType<DataTypeIPv6>(arguments[0].get());
|
|
|
|
const auto * str = checkAndGetDataType<DataTypeFixedString>(arguments[0].get());
|
|
|
|
if (!ipv6 && !(str && str->getN() == IPV6_BINARY_LENGTH))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of first argument of function {}, expected IPv6 or FixedString({})",
|
|
|
|
arguments[0]->getName(), getName(), IPV6_BINARY_LENGTH
|
|
|
|
);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
|
|
|
const DataTypePtr & second_argument = arguments[1];
|
2019-05-02 13:04:24 +00:00
|
|
|
if (!isUInt8(second_argument))
|
2022-11-22 21:34:32 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of second argument of function {}, expected UInt8",
|
|
|
|
second_argument->getName(), getName()
|
|
|
|
);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
DataTypePtr element = std::make_shared<DataTypeIPv6>();
|
2019-05-02 13:04:24 +00:00
|
|
|
return std::make_shared<DataTypeTuple>(DataTypes{element, element});
|
2019-04-22 11:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
2019-04-22 11:21:59 +00:00
|
|
|
{
|
2022-11-22 21:34:32 +00:00
|
|
|
const auto & col_type_name_cidr = arguments[1];
|
|
|
|
const ColumnPtr & column_cidr = col_type_name_cidr.column;
|
|
|
|
|
|
|
|
const auto * col_const_cidr_in = checkAndGetColumnConst<ColumnUInt8>(column_cidr.get());
|
|
|
|
const auto * col_cidr_in = checkAndGetColumn<ColumnUInt8>(column_cidr.get());
|
|
|
|
|
|
|
|
if (!col_const_cidr_in && !col_cidr_in)
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of argument of function {}",
|
|
|
|
arguments[1].column->getName(), getName()
|
|
|
|
);
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto & col_type_name_ip = arguments[0];
|
2019-04-22 11:21:59 +00:00
|
|
|
const ColumnPtr & column_ip = col_type_name_ip.column;
|
|
|
|
|
2022-11-19 06:55:05 +00:00
|
|
|
const auto * col_const_ip_in = checkAndGetColumnConst<ColumnIPv6>(column_ip.get());
|
|
|
|
const auto * col_ip_in = checkAndGetColumn<ColumnIPv6>(column_ip.get());
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2022-11-22 21:34:32 +00:00
|
|
|
const auto * col_const_str_in = checkAndGetColumnConst<ColumnFixedString>(column_ip.get());
|
|
|
|
const auto * col_str_in = checkAndGetColumn<ColumnFixedString>(column_ip.get());
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
std::function<const char *(size_t)> get_ip_data;
|
|
|
|
if (col_const_ip_in)
|
|
|
|
get_ip_data = [col_const_ip_in](size_t) { return col_const_ip_in->getDataAt(0).data; };
|
|
|
|
else if (col_const_str_in)
|
|
|
|
get_ip_data = [col_const_str_in](size_t) { return col_const_str_in->getDataAt(0).data; };
|
|
|
|
else if (col_ip_in)
|
|
|
|
get_ip_data = [col_ip_in](size_t i) { return reinterpret_cast<const char *>(&col_ip_in->getData()[i]); };
|
|
|
|
else if (col_str_in)
|
|
|
|
get_ip_data = [col_str_in](size_t i) { return reinterpret_cast<const char *>(&col_str_in->getChars().data()[i * IPV6_BINARY_LENGTH]); };
|
|
|
|
else
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of argument of function {}",
|
|
|
|
arguments[0].column->getName(), getName()
|
|
|
|
);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
auto col_res_lower_range = ColumnIPv6::create();
|
|
|
|
auto col_res_upper_range = ColumnIPv6::create();
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
auto & vec_res_lower_range = col_res_lower_range->getData();
|
|
|
|
vec_res_lower_range.resize(input_rows_count);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
auto & vec_res_upper_range = col_res_upper_range->getData();
|
|
|
|
vec_res_upper_range.resize(input_rows_count);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
static constexpr UInt8 max_cidr_mask = IPV6_BINARY_LENGTH * 8;
|
2019-07-19 09:39:25 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
2019-05-02 13:04:24 +00:00
|
|
|
{
|
2022-12-12 23:42:17 +00:00
|
|
|
UInt8 cidr = col_const_cidr_in
|
|
|
|
? col_const_cidr_in->getValue<UInt8>()
|
|
|
|
: col_cidr_in->getData()[i];
|
2020-10-05 04:39:36 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
cidr = std::min(cidr, max_cidr_mask);
|
2020-10-05 04:39:36 +00:00
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
applyCIDRMask(get_ip_data(i), reinterpret_cast<char *>(&vec_res_lower_range[i]), reinterpret_cast<char *>(&vec_res_upper_range[i]), cidr);
|
2019-04-22 11:21:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 23:42:17 +00:00
|
|
|
return ColumnTuple::create(Columns{std::move(col_res_lower_range), std::move(col_res_upper_range)});
|
2019-04-22 11:21:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-01 22:09:24 +00:00
|
|
|
|
|
|
|
class FunctionIPv4CIDRToRange : public IFunction
|
2019-04-23 07:02:23 +00:00
|
|
|
{
|
2019-05-02 13:04:24 +00:00
|
|
|
private:
|
|
|
|
static inline std::pair<UInt32, UInt32> applyCIDRMask(UInt32 src, UInt8 bits_to_keep)
|
2019-04-23 07:02:23 +00:00
|
|
|
{
|
2019-05-02 13:04:24 +00:00
|
|
|
if (bits_to_keep >= 8 * sizeof(UInt32))
|
|
|
|
return { src, src };
|
|
|
|
if (bits_to_keep == 0)
|
2022-04-18 08:18:31 +00:00
|
|
|
return { static_cast<UInt32>(0), static_cast<UInt32>(-1) };
|
2019-04-26 06:30:07 +00:00
|
|
|
|
2022-04-18 08:18:31 +00:00
|
|
|
UInt32 mask = static_cast<UInt32>(-1) << (8 * sizeof(UInt32) - bits_to_keep);
|
2019-05-02 13:04:24 +00:00
|
|
|
UInt32 lower = src & mask;
|
|
|
|
UInt32 upper = lower | ~mask;
|
2019-04-23 12:28:25 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
return { lower, upper };
|
2019-04-23 07:02:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 20:10:27 +00:00
|
|
|
template <typename ArgType>
|
|
|
|
ColumnPtr executeTyped(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const
|
2019-04-23 07:02:23 +00:00
|
|
|
{
|
2022-11-18 20:10:27 +00:00
|
|
|
using ColumnType = ColumnVector<ArgType>;
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto & col_type_name_ip = arguments[0];
|
2019-04-23 07:02:23 +00:00
|
|
|
const ColumnPtr & column_ip = col_type_name_ip.column;
|
|
|
|
|
2022-11-18 20:10:27 +00:00
|
|
|
const auto * col_const_ip_in = checkAndGetColumnConst<ColumnType>(column_ip.get());
|
|
|
|
const auto * col_ip_in = checkAndGetColumn<ColumnType>(column_ip.get());
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto & col_type_name_cidr = arguments[1];
|
2019-04-23 07:02:23 +00:00
|
|
|
const ColumnPtr & column_cidr = col_type_name_cidr.column;
|
2019-04-26 06:30:07 +00:00
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
const auto * col_const_cidr_in = checkAndGetColumnConst<ColumnUInt8>(column_cidr.get());
|
|
|
|
const auto * col_cidr_in = checkAndGetColumn<ColumnUInt8>(column_cidr.get());
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2022-11-18 20:10:27 +00:00
|
|
|
auto col_res_lower_range = ColumnIPv4::create();
|
|
|
|
auto col_res_upper_range = ColumnIPv4::create();
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
auto & vec_res_lower_range = col_res_lower_range->getData();
|
|
|
|
vec_res_lower_range.resize(input_rows_count);
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
auto & vec_res_upper_range = col_res_upper_range->getData();
|
|
|
|
vec_res_upper_range.resize(input_rows_count);
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
2022-11-18 20:10:27 +00:00
|
|
|
ArgType ip = col_const_ip_in
|
|
|
|
? col_const_ip_in->template getValue<ArgType>()
|
2021-07-16 14:03:25 +00:00
|
|
|
: col_ip_in->getData()[i];
|
2020-10-05 04:39:36 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
UInt8 cidr = col_const_cidr_in
|
2021-07-16 14:03:25 +00:00
|
|
|
? col_const_cidr_in->getValue<UInt8>()
|
|
|
|
: col_cidr_in->getData()[i];
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2020-10-05 04:39:36 +00:00
|
|
|
std::tie(vec_res_lower_range[i], vec_res_upper_range[i]) = applyCIDRMask(ip, cidr);
|
2019-04-23 07:02:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 14:49:19 +00:00
|
|
|
return ColumnTuple::create(Columns{std::move(col_res_lower_range), std::move(col_res_upper_range)});
|
2019-04-23 07:02:23 +00:00
|
|
|
}
|
2022-11-18 20:10:27 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "IPv4CIDRToRange";
|
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIPv4CIDRToRange>(); }
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
WhichDataType arg_type(arguments[0]);
|
|
|
|
if (!(arg_type.isIPv4() || arg_type.isUInt8() || arg_type.isUInt16() || arg_type.isUInt32()))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of first argument of function {}, expected IPv4 or UInt8 or UInt16 or UInt32",
|
|
|
|
arguments[0]->getName(), getName()
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const DataTypePtr & second_argument = arguments[1];
|
|
|
|
if (!isUInt8(second_argument))
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of second argument of function {}, expected UInt8",
|
|
|
|
second_argument->getName(), getName()
|
|
|
|
);
|
|
|
|
|
|
|
|
DataTypePtr element = DataTypeFactory::instance().get("IPv4");
|
|
|
|
return std::make_shared<DataTypeTuple>(DataTypes{element, element});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & ret_type, size_t input_rows_count) const override
|
|
|
|
{
|
|
|
|
if (arguments[1].type->getTypeId() != TypeIndex::UInt8)
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of second argument of function {}, expected UInt8", arguments[1].type->getName(), getName()
|
|
|
|
);
|
|
|
|
|
|
|
|
switch (arguments[0].type->getTypeId())
|
|
|
|
{
|
|
|
|
case TypeIndex::IPv4: return executeTyped<IPv4>(arguments, ret_type, input_rows_count);
|
|
|
|
case TypeIndex::UInt8: return executeTyped<UInt8>(arguments, ret_type, input_rows_count);
|
|
|
|
case TypeIndex::UInt16: return executeTyped<UInt16>(arguments, ret_type, input_rows_count);
|
|
|
|
case TypeIndex::UInt32: return executeTyped<UInt32>(arguments, ret_type, input_rows_count);
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of argument of function {}, expected IPv4 or UInt8 or UInt16 or UInt32",
|
|
|
|
arguments[0].column->getName(), getName()
|
|
|
|
);
|
|
|
|
}
|
2019-04-23 07:02:23 +00:00
|
|
|
};
|
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
class FunctionIsIPv4String : public IFunction
|
2020-12-22 08:06:33 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "isIPv4String";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIsIPv4String>(); }
|
2020-12-22 08:06:33 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2020-12-22 08:06:33 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
2022-03-12 17:12:05 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[0]->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2020-12-22 08:06:33 +00:00
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
|
|
|
{
|
2022-03-12 17:12:05 +00:00
|
|
|
const ColumnString * input_column = checkAndGetColumn<ColumnString>(arguments[0].column.get());
|
|
|
|
|
|
|
|
if (!input_column)
|
2020-12-22 08:06:33 +00:00
|
|
|
{
|
2022-03-12 17:12:05 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}", arguments[0].column->getName(), getName());
|
|
|
|
}
|
2020-12-22 08:06:33 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
auto col_res = ColumnUInt8::create();
|
2020-12-22 08:06:33 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
ColumnUInt8::Container & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(input_column->size());
|
2020-12-22 08:06:33 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
const ColumnString::Chars & vec_src = input_column->getChars();
|
|
|
|
const ColumnString::Offsets & offsets_src = input_column->getOffsets();
|
|
|
|
size_t prev_offset = 0;
|
|
|
|
UInt32 result = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec_res.size(); ++i)
|
|
|
|
{
|
2022-12-10 00:48:44 +00:00
|
|
|
vec_res[i] = DB::parseIPv4whole(reinterpret_cast<const char *>(&vec_src[prev_offset]), reinterpret_cast<unsigned char *>(&result));
|
2022-03-12 17:12:05 +00:00
|
|
|
prev_offset = offsets_src[i];
|
2020-12-22 08:06:33 +00:00
|
|
|
}
|
2022-03-12 17:12:05 +00:00
|
|
|
|
|
|
|
return col_res;
|
2020-12-22 08:06:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
class FunctionIsIPv6String : public IFunction
|
2020-12-22 08:06:33 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "isIPv6String";
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionIsIPv6String>(); }
|
2020-12-22 08:06:33 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2021-04-29 14:48:26 +00:00
|
|
|
|
2020-12-22 08:06:33 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
2022-03-12 17:12:05 +00:00
|
|
|
{
|
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}", arguments[0]->getName(), getName());
|
|
|
|
}
|
2020-12-22 08:06:33 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
}
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
|
|
|
{
|
2022-03-12 17:12:05 +00:00
|
|
|
const ColumnString * input_column = checkAndGetColumn<ColumnString>(arguments[0].column.get());
|
|
|
|
if (!input_column)
|
2020-12-22 08:06:33 +00:00
|
|
|
{
|
2022-03-12 17:12:05 +00:00
|
|
|
throw Exception(
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of argument of function {}", arguments[0].column->getName(), getName());
|
|
|
|
}
|
2020-12-22 08:06:33 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
auto col_res = ColumnUInt8::create();
|
2020-12-22 08:06:33 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
ColumnUInt8::Container & vec_res = col_res->getData();
|
|
|
|
vec_res.resize(input_column->size());
|
2020-12-22 08:06:33 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
const ColumnString::Chars & vec_src = input_column->getChars();
|
|
|
|
const ColumnString::Offsets & offsets_src = input_column->getOffsets();
|
|
|
|
size_t prev_offset = 0;
|
|
|
|
char buffer[IPV6_BINARY_LENGTH];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec_res.size(); ++i)
|
|
|
|
{
|
2022-12-10 00:48:44 +00:00
|
|
|
vec_res[i] = DB::parseIPv6whole(reinterpret_cast<const char *>(&vec_src[prev_offset]), reinterpret_cast<unsigned char *>(buffer));
|
2022-03-12 17:12:05 +00:00
|
|
|
prev_offset = offsets_src[i];
|
2020-12-22 08:06:33 +00:00
|
|
|
}
|
2022-03-12 17:12:05 +00:00
|
|
|
|
|
|
|
return col_res;
|
2020-12-22 08:06:33 +00:00
|
|
|
}
|
|
|
|
};
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2021-07-16 14:03:25 +00:00
|
|
|
struct NameFunctionIPv4NumToString { static constexpr auto name = "IPv4NumToString"; };
|
|
|
|
struct NameFunctionIPv4NumToStringClassC { static constexpr auto name = "IPv4NumToStringClassC"; };
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(Coding)
|
2021-07-16 14:03:25 +00:00
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionCutIPv6>();
|
|
|
|
factory.registerFunction<FunctionIPv4ToIPv6>();
|
|
|
|
factory.registerFunction<FunctionMACNumToString>();
|
|
|
|
factory.registerFunction<FunctionMACStringTo<ParseMACImpl>>();
|
|
|
|
factory.registerFunction<FunctionMACStringTo<ParseOUIImpl>>();
|
|
|
|
factory.registerFunction<FunctionIPv6CIDRToRange>();
|
|
|
|
factory.registerFunction<FunctionIPv4CIDRToRange>();
|
|
|
|
factory.registerFunction<FunctionIsIPv4String>();
|
|
|
|
factory.registerFunction<FunctionIsIPv6String>();
|
|
|
|
|
|
|
|
factory.registerFunction<FunctionIPv4NumToString<0, NameFunctionIPv4NumToString>>();
|
|
|
|
factory.registerFunction<FunctionIPv4NumToString<1, NameFunctionIPv4NumToStringClassC>>();
|
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
factory.registerFunction<FunctionIPv4StringToNum<IPStringToNumExceptionMode::Throw>>();
|
|
|
|
factory.registerFunction<FunctionIPv4StringToNum<IPStringToNumExceptionMode::Default>>();
|
|
|
|
factory.registerFunction<FunctionIPv4StringToNum<IPStringToNumExceptionMode::Null>>();
|
|
|
|
|
2021-07-16 14:03:25 +00:00
|
|
|
factory.registerFunction<FunctionIPv6NumToString>();
|
2022-03-12 17:12:05 +00:00
|
|
|
factory.registerFunction<FunctionIPv6StringToNum<IPStringToNumExceptionMode::Throw>>();
|
|
|
|
factory.registerFunction<FunctionIPv6StringToNum<IPStringToNumExceptionMode::Default>>();
|
|
|
|
factory.registerFunction<FunctionIPv6StringToNum<IPStringToNumExceptionMode::Null>>();
|
2021-07-16 14:03:25 +00:00
|
|
|
|
2022-03-12 17:12:05 +00:00
|
|
|
/// MySQL compatibility aliases:
|
|
|
|
factory.registerAlias("INET_ATON", FunctionIPv4StringToNum<IPStringToNumExceptionMode::Throw>::name, FunctionFactory::CaseInsensitive);
|
2021-07-16 14:03:25 +00:00
|
|
|
factory.registerAlias("INET6_NTOA", FunctionIPv6NumToString::name, FunctionFactory::CaseInsensitive);
|
2022-03-12 17:12:05 +00:00
|
|
|
factory.registerAlias("INET6_ATON", FunctionIPv6StringToNum<IPStringToNumExceptionMode::Throw>::name, FunctionFactory::CaseInsensitive);
|
2021-07-16 14:03:25 +00:00
|
|
|
factory.registerAlias("INET_NTOA", NameFunctionIPv4NumToString::name, FunctionFactory::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
2013-03-05 09:32:12 +00:00
|
|
|
}
|