2013-03-05 09:32:12 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-06-01 13:41:58 +00:00
|
|
|
#include <Common/hex.h>
|
2017-06-13 05:14:24 +00:00
|
|
|
#include <Common/formatIPv6.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2018-12-13 13:41:47 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2017-07-25 17:00:38 +00:00
|
|
|
#include <DataTypes/DataTypeUUID.h>
|
2019-04-22 11:21:59 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
2019-04-22 11:21:59 +00:00
|
|
|
#include <Columns/ColumnTuple.h>
|
2019-10-16 15:28:10 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2014-10-30 14:43:21 +00:00
|
|
|
|
2014-10-29 12:25:33 +00:00
|
|
|
#include <arpa/inet.h>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.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;
|
2017-08-01 13:26:19 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2019-03-14 23:10:51 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
2017-06-13 02:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
/** TODO This file contains ridiculous amount of copy-paste.
|
|
|
|
*/
|
|
|
|
|
2017-06-01 13:41:58 +00:00
|
|
|
/** Encoding functions:
|
2014-06-26 00:58:14 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* IPv4NumToString (num) - See below.
|
|
|
|
* IPv4StringToNum(string) - Convert, for example, '192.168.0.1' to 3232235521 and vice versa.
|
2014-06-26 00:58:14 +00:00
|
|
|
*
|
2017-06-01 13:41:58 +00:00
|
|
|
* hex(x) - Returns hex; capital letters; there are no prefixes 0x or suffixes h.
|
|
|
|
* For numbers, returns a variable-length string - hex in the "human" (big endian) format, with the leading zeros being cut,
|
|
|
|
* but only by whole bytes. For dates and datetimes - the same as for numbers.
|
|
|
|
* For example, hex(257) = '0101'.
|
|
|
|
* unhex(string) - Returns a string, hex of which is equal to `string` with regard of case and discarding one leading zero.
|
2018-10-13 14:33:43 +00:00
|
|
|
* If such a string does not exist, could return arbitrary implementation specific value.
|
2014-06-26 00:58:14 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* bitmaskToArray(x) - Returns an array of powers of two in the binary form of x. For example, bitmaskToArray(50) = [2, 16, 32].
|
2013-03-07 12:43:54 +00:00
|
|
|
*/
|
2013-03-05 09:32:12 +00:00
|
|
|
|
|
|
|
|
2017-07-23 06:13:57 +00:00
|
|
|
constexpr size_t uuid_bytes_length = 16;
|
|
|
|
constexpr size_t uuid_text_length = 36;
|
2015-09-02 13:05:17 +00:00
|
|
|
|
2016-12-11 10:57:34 +00:00
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
class FunctionIPv6NumToString : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "IPv6NumToString";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIPv6NumToString>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-07-21 06:35:58 +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)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() +
|
|
|
|
" of argument of function " + getName() +
|
2018-12-13 13:41:47 +00:00
|
|
|
", expected FixedString(" + toString(IPV6_BINARY_LENGTH) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto & col_type_name = block.getByPosition(arguments[0]);
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnPtr & column = col_type_name.column;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto col_in = checkAndGetColumn<ColumnFixedString>(column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
if (col_in->getN() != IPV6_BINARY_LENGTH)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + col_type_name.type->getName() +
|
|
|
|
" of column " + col_in->getName() +
|
|
|
|
" argument of function " + getName() +
|
2018-12-13 13:41:47 +00:00
|
|
|
", expected FixedString(" + toString(IPV6_BINARY_LENGTH) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
const auto size = col_in->size();
|
|
|
|
const auto & vec_in = col_in->getChars();
|
|
|
|
|
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));
|
2017-04-01 07:20:54 +00:00
|
|
|
offsets_res.resize(size);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
auto begin = reinterpret_cast<char *>(vec_res.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
auto pos = begin;
|
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
for (size_t offset = 0, i = 0; offset < vec_in.size(); offset += IPV6_BINARY_LENGTH, ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-01-03 14:52:37 +00:00
|
|
|
formatIPv6(reinterpret_cast<const unsigned char *>(&vec_in[offset]), pos);
|
2017-04-01 07:20:54 +00:00
|
|
|
offsets_res[i] = pos - begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec_res.resize(pos - begin);
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2019-03-14 23:10:51 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
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:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "cutIPv6";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionCutIPv6>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 3; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-07-21 06:35:58 +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)
|
2017-04-01 07:20:54 +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) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!WhichDataType(arguments[1]).isUInt8())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[1]->getName() +
|
|
|
|
" of argument 2 of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!WhichDataType(arguments[2]).isUInt8())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[2]->getName() +
|
|
|
|
" of argument 3 of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1, 2}; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto & col_type_name = block.getByPosition(arguments[0]);
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnPtr & column = col_type_name.column;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto & col_ipv6_zeroed_tail_bytes_type = block.getByPosition(arguments[1]);
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & col_ipv6_zeroed_tail_bytes = col_ipv6_zeroed_tail_bytes_type.column;
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto & col_ipv4_zeroed_tail_bytes_type = block.getByPosition(arguments[2]);
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & col_ipv4_zeroed_tail_bytes = col_ipv4_zeroed_tail_bytes_type.column;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto col_in = checkAndGetColumn<ColumnFixedString>(column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
if (col_in->getN() != IPV6_BINARY_LENGTH)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + col_type_name.type->getName() +
|
|
|
|
" of column " + col_in->getName() +
|
|
|
|
" argument of function " + getName() +
|
2018-12-13 13:41:47 +00:00
|
|
|
", expected FixedString(" + toString(IPV6_BINARY_LENGTH) + ")",
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto ipv6_zeroed_tail_bytes = checkAndGetColumnConst<ColumnVector<UInt8>>(col_ipv6_zeroed_tail_bytes.get());
|
2017-04-01 07:20:54 +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-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)
|
2017-04-01 07:20:54 +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-07-21 06:35:58 +00:00
|
|
|
const auto ipv4_zeroed_tail_bytes = checkAndGetColumnConst<ColumnVector<UInt8>>(col_ipv4_zeroed_tail_bytes.get());
|
2017-04-01 07:20:54 +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-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)
|
2017-04-01 07:20:54 +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);
|
|
|
|
|
|
|
|
const auto size = col_in->size();
|
|
|
|
const auto & vec_in = col_in->getChars();
|
|
|
|
|
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));
|
2017-04-01 07:20:54 +00:00
|
|
|
offsets_res.resize(size);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
auto begin = reinterpret_cast<char *>(vec_res.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
auto pos = begin;
|
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
for (size_t offset = 0, i = 0; offset < vec_in.size(); offset += IPV6_BINARY_LENGTH, ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const auto address = &vec_in[offset];
|
|
|
|
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);
|
2017-04-01 07:20:54 +00:00
|
|
|
offsets_res[i] = pos - begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec_res.resize(pos - begin);
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2015-09-02 13:05:17 +00:00
|
|
|
|
|
|
|
private:
|
2020-01-03 14:52:37 +00:00
|
|
|
bool isIPv4Mapped(const UInt8 * address) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-01-03 14:52:37 +00:00
|
|
|
return (unalignedLoad<UInt64>(address) == 0) &&
|
|
|
|
((unalignedLoad<UInt64>(address + 8) & 0x00000000FFFFFFFFull) == 0x00000000FFFF0000ull);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void cutAddress(const unsigned char * address, char *& dst, UInt8 zeroed_tail_bytes_count) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-06-13 05:14:24 +00:00
|
|
|
formatIPv6(address, dst, zeroed_tail_bytes_count);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-09-02 13:05:17 +00:00
|
|
|
};
|
|
|
|
|
2016-12-11 10:57:34 +00:00
|
|
|
|
2014-10-29 12:49:19 +00:00
|
|
|
class FunctionIPv6StringToNum : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "IPv6StringToNum";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIPv6StringToNum>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
return std::make_shared<DataTypeFixedString>(IPV6_BINARY_LENGTH);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & column = block.getByPosition(arguments[0]).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto col_in = checkAndGetColumn<ColumnString>(column.get()))
|
2017-04-01 07:20:54 +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
|
|
|
|
|
|
|
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
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & vec_src = col_in->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnString::Offsets & offsets_src = col_in->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t src_offset = 0;
|
|
|
|
|
|
|
|
for (size_t out_offset = 0, i = 0;
|
|
|
|
out_offset < vec_res.size();
|
2018-12-13 13:41:47 +00:00
|
|
|
out_offset += IPV6_BINARY_LENGTH, ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-01-03 14:52:37 +00:00
|
|
|
/// In case of failure, the function fills vec_res with zero bytes.
|
|
|
|
parseIPv6(reinterpret_cast<const char *>(&vec_src[src_offset]), reinterpret_cast<unsigned char *>(&vec_res[out_offset]));
|
2017-04-01 07:20:54 +00:00
|
|
|
src_offset = offsets_src[i];
|
|
|
|
}
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-12-16 02:34:02 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-10-29 12:49:19 +00:00
|
|
|
};
|
2016-12-11 10:57:34 +00:00
|
|
|
|
|
|
|
|
2017-09-06 03:17:06 +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>
|
2013-03-05 09:32:12 +00:00
|
|
|
class FunctionIPv4NumToString : public IFunction
|
|
|
|
{
|
2017-09-06 03:17:06 +00:00
|
|
|
public:
|
|
|
|
static constexpr auto name = Name::name;
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIPv4NumToString<mask_tail_octets, Name>>(); }
|
2017-09-06 03:17:06 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return mask_tail_octets == 0; }
|
2017-09-06 03:17:06 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!WhichDataType(arguments[0]).isUInt32())
|
2017-09-06 03:17:06 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName() + ", expected UInt32",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & column = block.getByPosition(arguments[0]).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (const ColumnUInt32 * col = typeid_cast<const ColumnUInt32 *>(column.get()))
|
|
|
|
{
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnUInt32::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
|
2017-04-01 07:20:54 +00:00
|
|
|
offsets_res.resize(vec_in.size());
|
2018-09-02 03:00:04 +00:00
|
|
|
char * begin = reinterpret_cast<char *>(vec_res.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
char * pos = begin;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec_in.size(); ++i)
|
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
DB::formatIPv4(reinterpret_cast<const unsigned char*>(&vec_in[i]), pos, mask_tail_octets, "xxx");
|
2017-04-01 07:20:54 +00:00
|
|
|
offsets_res[i] = pos - begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec_res.resize(pos - begin);
|
2017-12-14 03:56:56 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2013-03-05 09:32:12 +00:00
|
|
|
};
|
2013-03-05 10:29:26 +00:00
|
|
|
|
2016-12-11 10:57:34 +00:00
|
|
|
|
2013-03-05 10:29:26 +00:00
|
|
|
class FunctionIPv4StringToNum : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "IPv4StringToNum";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIPv4StringToNum>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt32>();
|
|
|
|
}
|
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
static inline UInt32 parseIPv4(const char * pos)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
UInt32 result = 0;
|
|
|
|
DB::parseIPv4(pos, reinterpret_cast<unsigned char*>(&result));
|
|
|
|
|
|
|
|
return result;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & column = block.getByPosition(arguments[0]).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnUInt32::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnUInt32::Container & vec_res = col_res->getData();
|
2017-04-01 07:20:54 +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-04-01 07:20:54 +00:00
|
|
|
size_t prev_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < vec_res.size(); ++i)
|
|
|
|
{
|
|
|
|
vec_res[i] = parseIPv4(reinterpret_cast<const char *>(&vec_src[prev_offset]));
|
|
|
|
prev_offset = offsets_src[i];
|
|
|
|
}
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-12-16 02:34:02 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2013-03-05 10:29:26 +00:00
|
|
|
};
|
2013-03-07 12:01:41 +00:00
|
|
|
|
|
|
|
|
2015-09-02 13:05:17 +00:00
|
|
|
class FunctionIPv4ToIPv6 : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "IPv4ToIPv6";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIPv4ToIPv6>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (!checkAndGetDataType<DataTypeUInt32>(arguments[0].get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() +
|
|
|
|
" of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeFixedString>(16);
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto & col_type_name = block.getByPosition(arguments[0]);
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnPtr & column = col_type_name.column;
|
|
|
|
|
|
|
|
if (const auto col_in = typeid_cast<const ColumnUInt32 *>(column.get()))
|
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
auto col_res = ColumnFixedString::create(IPV6_BINARY_LENGTH);
|
2017-04-01 07:20:54 +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
|
|
|
|
|
|
|
const auto & vec_in = col_in->getData();
|
|
|
|
|
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)
|
2017-04-01 07:20:54 +00:00
|
|
|
mapIPv4ToIPv6(vec_in[i], &vec_res[out_offset]);
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-12-16 02:34:02 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-09-02 13:05:17 +00:00
|
|
|
|
|
|
|
private:
|
2020-01-03 14:52:37 +00:00
|
|
|
void mapIPv4ToIPv6(UInt32 in, UInt8 * buf) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2020-01-03 14:52:37 +00:00
|
|
|
unalignedStore<UInt64>(buf, 0);
|
|
|
|
unalignedStore<UInt64>(buf + 8, 0x00000000FFFF0000ull | (static_cast<UInt64>(ntohl(in)) << 32));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-09-02 13:05:17 +00:00
|
|
|
};
|
2014-12-24 14:45:44 +00:00
|
|
|
|
2018-12-13 13:41:47 +00:00
|
|
|
class FunctionToIPv4 : public FunctionIPv4StringToNum
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "toIPv4";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionToIPv4>(); }
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return DataTypeFactory::instance().get("IPv4");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FunctionToIPv6 : public FunctionIPv6StringToNum
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "toIPv6";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionToIPv6>(); }
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!isString(arguments[0]))
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return DataTypeFactory::instance().get("IPv6");
|
|
|
|
}
|
|
|
|
};
|
2016-12-11 10:57:34 +00:00
|
|
|
|
2017-05-08 10:02:10 +00:00
|
|
|
class FunctionMACNumToString : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "MACNumToString";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { 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-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
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",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
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-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & column = block.getByPosition(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
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-05-08 10:02:10 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-07-23 06:13:57 +00:00
|
|
|
return (UInt64(unhex(pos[0])) << 44)
|
|
|
|
| (UInt64(unhex(pos[1])) << 40)
|
|
|
|
| (UInt64(unhex(pos[3])) << 36)
|
|
|
|
| (UInt64(unhex(pos[4])) << 32)
|
|
|
|
| (UInt64(unhex(pos[6])) << 28)
|
|
|
|
| (UInt64(unhex(pos[7])) << 24)
|
|
|
|
| (UInt64(unhex(pos[9])) << 20)
|
|
|
|
| (UInt64(unhex(pos[10])) << 16)
|
|
|
|
| (UInt64(unhex(pos[12])) << 12)
|
|
|
|
| (UInt64(unhex(pos[13])) << 8)
|
|
|
|
| (UInt64(unhex(pos[15])) << 4)
|
|
|
|
| (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
|
|
|
{
|
2017-07-23 06:13:57 +00:00
|
|
|
return (UInt64(unhex(pos[0])) << 20)
|
|
|
|
| (UInt64(unhex(pos[1])) << 16)
|
|
|
|
| (UInt64(unhex(pos[3])) << 12)
|
|
|
|
| (UInt64(unhex(pos[4])) << 8)
|
|
|
|
| (UInt64(unhex(pos[6])) << 4)
|
|
|
|
| (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;
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { 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; }
|
|
|
|
|
|
|
|
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(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-05-08 10:02:10 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & column = block.getByPosition(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
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-07-23 06:13:57 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-05-08 10:02:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-12-11 10:57:34 +00:00
|
|
|
class FunctionUUIDNumToString : public IFunction
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "UUIDNumToString";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionUUIDNumToString>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto ptr = checkAndGetDataType<DataTypeFixedString>(arguments[0].get());
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!ptr || ptr->getN() != uuid_bytes_length)
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() +
|
|
|
|
" of argument of function " + getName() +
|
|
|
|
", expected FixedString(" + toString(uuid_bytes_length) + ")",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnWithTypeAndName & col_type_name = block.getByPosition(arguments[0]);
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnPtr & column = col_type_name.column;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto col_in = checkAndGetColumn<ColumnFixedString>(column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (col_in->getN() != uuid_bytes_length)
|
|
|
|
throw Exception("Illegal type " + col_type_name.type->getName() +
|
|
|
|
" of column " + col_in->getName() +
|
|
|
|
" argument of function " + getName() +
|
|
|
|
", expected FixedString(" + toString(uuid_bytes_length) + ")",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
const auto size = col_in->size();
|
|
|
|
const auto & vec_in = col_in->getChars();
|
|
|
|
|
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-04-01 07:20:54 +00:00
|
|
|
vec_res.resize(size * (uuid_text_length + 1));
|
|
|
|
offsets_res.resize(size);
|
|
|
|
|
|
|
|
size_t src_offset = 0;
|
|
|
|
size_t dst_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
formatUUID(&vec_in[src_offset], &vec_res[dst_offset]);
|
|
|
|
src_offset += uuid_bytes_length;
|
|
|
|
dst_offset += uuid_text_length;
|
|
|
|
vec_res[dst_offset] = 0;
|
|
|
|
++dst_offset;
|
|
|
|
offsets_res[i] = dst_offset;
|
|
|
|
}
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-12-16 02:34:02 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-11 10:57:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionUUIDStringToNum : public IFunction
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
static void parseHex(const UInt8 * __restrict src, UInt8 * __restrict dst, const size_t num_bytes)
|
|
|
|
{
|
|
|
|
size_t src_pos = 0;
|
|
|
|
size_t dst_pos = 0;
|
|
|
|
for (; dst_pos < num_bytes; ++dst_pos)
|
|
|
|
{
|
2017-08-13 23:58:04 +00:00
|
|
|
dst[dst_pos] = unhex2(reinterpret_cast<const char *>(&src[src_pos]));
|
2017-04-01 07:20:54 +00:00
|
|
|
src_pos += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parseUUID(const UInt8 * src36, UInt8 * dst16)
|
|
|
|
{
|
|
|
|
/// If string is not like UUID - implementation specific behaviour.
|
|
|
|
|
|
|
|
parseHex(&src36[0], &dst16[0], 4);
|
|
|
|
parseHex(&src36[9], &dst16[4], 2);
|
|
|
|
parseHex(&src36[14], &dst16[6], 2);
|
|
|
|
parseHex(&src36[19], &dst16[8], 2);
|
|
|
|
parseHex(&src36[24], &dst16[10], 6);
|
|
|
|
}
|
2016-12-11 10:57:34 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "UUIDStringToNum";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionUUIDStringToNum>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
/// String or FixedString(36)
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto ptr = checkAndGetDataType<DataTypeFixedString>(arguments[0].get());
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!ptr || ptr->getN() != uuid_text_length)
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() +
|
|
|
|
" of argument of function " + getName() +
|
|
|
|
", expected FixedString(" + toString(uuid_text_length) + ")",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeFixedString>(uuid_bytes_length);
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnWithTypeAndName & col_type_name = block.getByPosition(arguments[0]);
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnPtr & column = col_type_name.column;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto col_in = checkAndGetColumn<ColumnString>(column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const auto & vec_in = col_in->getChars();
|
|
|
|
const auto & offsets_in = col_in->getOffsets();
|
|
|
|
const size_t size = offsets_in.size();
|
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnFixedString::create(uuid_bytes_length);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & vec_res = col_res->getChars();
|
2017-04-01 07:20:54 +00:00
|
|
|
vec_res.resize(size * uuid_bytes_length);
|
|
|
|
|
|
|
|
size_t src_offset = 0;
|
|
|
|
size_t dst_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
/// If string has incorrect length - then return zero UUID.
|
|
|
|
/// If string has correct length but contains something not like UUID - implementation specific behaviour.
|
|
|
|
|
|
|
|
size_t string_size = offsets_in[i] - src_offset;
|
|
|
|
if (string_size == uuid_text_length + 1)
|
|
|
|
parseUUID(&vec_in[src_offset], &vec_res[dst_offset]);
|
|
|
|
else
|
|
|
|
memset(&vec_res[dst_offset], 0, uuid_bytes_length);
|
|
|
|
|
|
|
|
dst_offset += uuid_bytes_length;
|
|
|
|
src_offset += string_size;
|
|
|
|
}
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2019-01-04 12:10:00 +00:00
|
|
|
else if (const auto col_in_fixed = checkAndGetColumn<ColumnFixedString>(column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-01-04 12:10:00 +00:00
|
|
|
if (col_in_fixed->getN() != uuid_text_length)
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + col_type_name.type->getName() +
|
2019-01-04 12:10:00 +00:00
|
|
|
" of column " + col_in_fixed->getName() +
|
2017-04-01 07:20:54 +00:00
|
|
|
" argument of function " + getName() +
|
|
|
|
", expected FixedString(" + toString(uuid_text_length) + ")",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
const auto size = col_in_fixed->size();
|
|
|
|
const auto & vec_in = col_in_fixed->getChars();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_res = ColumnFixedString::create(uuid_bytes_length);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & vec_res = col_res->getChars();
|
2017-04-01 07:20:54 +00:00
|
|
|
vec_res.resize(size * uuid_bytes_length);
|
|
|
|
|
|
|
|
size_t src_offset = 0;
|
|
|
|
size_t dst_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
parseUUID(&vec_in[src_offset], &vec_res[dst_offset]);
|
|
|
|
src_offset += uuid_text_length;
|
|
|
|
dst_offset += uuid_bytes_length;
|
|
|
|
}
|
2017-12-16 02:34:02 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2019-01-04 12:10:00 +00:00
|
|
|
+ " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-12-11 10:57:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-07 12:01:41 +00:00
|
|
|
class FunctionHex : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "hex";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionHex>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
WhichDataType which(arguments[0]);
|
|
|
|
|
2019-09-23 15:47:34 +00:00
|
|
|
if (!which.isStringOrFixedString() &&
|
|
|
|
!which.isDateOrDateTime() &&
|
|
|
|
!which.isUInt() &&
|
2019-10-16 15:28:10 +00:00
|
|
|
!which.isFloat() &&
|
|
|
|
!which.isDecimal())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
2017-07-21 06:35:58 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeOneUInt(T x, char *& out) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
bool was_nonzero = false;
|
|
|
|
for (int offset = (sizeof(T) - 1) * 8; offset >= 0; offset -= 8)
|
|
|
|
{
|
2017-07-23 06:53:28 +00:00
|
|
|
UInt8 byte = x >> offset;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Leading zeros.
|
2017-04-01 07:20:54 +00:00
|
|
|
if (byte == 0 && !was_nonzero && offset)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
was_nonzero = true;
|
|
|
|
|
2017-07-23 06:53:28 +00:00
|
|
|
writeHexByteUppercase(byte, out);
|
|
|
|
out += 2;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-23 06:53:28 +00:00
|
|
|
*out = '\0';
|
|
|
|
++out;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecuteUInt(const IColumn * col, ColumnPtr & col_res) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnVector<T> * col_vec = checkAndGetColumn<ColumnVector<T>>(col);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-01 13:41:58 +00:00
|
|
|
static constexpr size_t MAX_UINT_HEX_LENGTH = sizeof(T) * 2 + 1; /// Including trailing zero byte.
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (col_vec)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_str = ColumnString::create();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & in_vec = col_vec->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t size = in_vec.size();
|
|
|
|
out_offsets.resize(size);
|
2017-06-01 13:41:58 +00:00
|
|
|
out_vec.resize(size * 3 + MAX_UINT_HEX_LENGTH); /// 3 is length of one byte in hex plus zero byte.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Manual exponential growth, so as not to rely on the linear amortized work time of `resize` (no one guarantees it).
|
2017-04-01 07:20:54 +00:00
|
|
|
if (pos + MAX_UINT_HEX_LENGTH > out_vec.size())
|
|
|
|
out_vec.resize(out_vec.size() * 2 + MAX_UINT_HEX_LENGTH);
|
|
|
|
|
|
|
|
char * begin = reinterpret_cast<char *>(&out_vec[pos]);
|
|
|
|
char * end = begin;
|
|
|
|
executeOneUInt<T>(in_vec[i], end);
|
|
|
|
|
|
|
|
pos += end - begin;
|
|
|
|
out_offsets[i] = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_vec.resize(pos);
|
|
|
|
|
2017-12-16 02:34:02 +00:00
|
|
|
col_res = std::move(col_str);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 16:21:05 +00:00
|
|
|
template <typename T>
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeFloatAndDecimal(const T & in_vec, ColumnPtr & col_res, const size_t type_size_in_bytes) const
|
2019-09-20 16:21:05 +00:00
|
|
|
{
|
2019-11-06 15:06:15 +00:00
|
|
|
const size_t hex_length = type_size_in_bytes * 2 + 1; /// Including trailing zero byte.
|
2019-11-06 15:15:24 +00:00
|
|
|
auto col_str = ColumnString::create();
|
2019-11-06 15:06:15 +00:00
|
|
|
|
2019-11-06 07:42:10 +00:00
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
2019-11-06 15:06:15 +00:00
|
|
|
|
2019-11-06 07:42:10 +00:00
|
|
|
size_t size = in_vec.size();
|
|
|
|
out_offsets.resize(size);
|
2019-11-06 15:06:15 +00:00
|
|
|
out_vec.resize(size * hex_length);
|
|
|
|
|
2019-11-06 07:42:10 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
char * out = reinterpret_cast<char *>(&out_vec[0]);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
const UInt8 * in_pos = reinterpret_cast<const UInt8 *>(&in_vec[i]);
|
2019-11-06 15:06:15 +00:00
|
|
|
executeOneString(in_pos, in_pos + type_size_in_bytes, out);
|
|
|
|
|
|
|
|
pos += hex_length;
|
2019-11-06 07:42:10 +00:00
|
|
|
out_offsets[i] = pos;
|
|
|
|
}
|
|
|
|
col_res = std::move(col_str);
|
|
|
|
}
|
2019-09-20 16:21:05 +00:00
|
|
|
|
2019-11-06 07:42:10 +00:00
|
|
|
template <typename T>
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecuteFloat(const IColumn * col, ColumnPtr & col_res) const
|
2019-11-06 07:42:10 +00:00
|
|
|
{
|
|
|
|
const ColumnVector<T> * col_vec = checkAndGetColumn<ColumnVector<T>>(col);
|
|
|
|
if (col_vec)
|
2019-09-20 16:21:05 +00:00
|
|
|
{
|
2019-11-06 07:42:10 +00:00
|
|
|
const typename ColumnVector<T>::Container & in_vec = col_vec->getData();
|
2019-11-06 15:06:15 +00:00
|
|
|
executeFloatAndDecimal<typename ColumnVector<T>::Container>(in_vec, col_res, sizeof(T));
|
2019-11-06 07:42:10 +00:00
|
|
|
return true;
|
2019-09-20 16:21:05 +00:00
|
|
|
}
|
2019-11-06 08:29:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-20 16:21:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 15:28:10 +00:00
|
|
|
template <typename T>
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecuteDecimal(const IColumn * col, ColumnPtr & col_res) const
|
2019-11-06 08:17:31 +00:00
|
|
|
{
|
2019-11-06 11:48:17 +00:00
|
|
|
const ColumnDecimal<T> * col_dec = checkAndGetColumn<ColumnDecimal<T>>(col);
|
2019-10-16 15:28:10 +00:00
|
|
|
if (col_dec)
|
|
|
|
{
|
|
|
|
const typename ColumnDecimal<T>::Container & in_vec = col_dec->getData();
|
2019-11-06 15:06:15 +00:00
|
|
|
executeFloatAndDecimal<typename ColumnDecimal<T>::Container>(in_vec, col_res, sizeof(T));
|
2019-10-16 15:28:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-06 08:29:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-16 15:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeOneString(const UInt8 * pos, const UInt8 * end, char *& out) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
while (pos < end)
|
|
|
|
{
|
2017-07-23 06:53:28 +00:00
|
|
|
writeHexByteUppercase(*pos, out);
|
|
|
|
++pos;
|
|
|
|
out += 2;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-07-23 06:53:28 +00:00
|
|
|
*out = '\0';
|
|
|
|
++out;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecuteString(const IColumn * col, ColumnPtr & col_res) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnString * col_str_in = checkAndGetColumn<ColumnString>(col);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_str_in)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_str = ColumnString::create();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & in_vec = col_str_in->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnString::Offsets & in_offsets = col_str_in->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t size = in_offsets.size();
|
|
|
|
out_offsets.resize(size);
|
|
|
|
out_vec.resize(in_vec.size() * 2 - size);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
char * begin = reinterpret_cast<char *>(out_vec.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
char * pos = begin;
|
|
|
|
size_t prev_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
size_t new_offset = in_offsets[i];
|
|
|
|
|
|
|
|
executeOneString(&in_vec[prev_offset], &in_vec[new_offset - 1], pos);
|
|
|
|
|
|
|
|
out_offsets[i] = pos - begin;
|
|
|
|
|
|
|
|
prev_offset = new_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!out_offsets.empty() && out_offsets.back() != out_vec.size())
|
|
|
|
throw Exception("Column size mismatch (internal logical error)", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2017-12-16 02:34:02 +00:00
|
|
|
col_res = std::move(col_str);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecuteFixedString(const IColumn * col, ColumnPtr & col_res) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnFixedString * col_fstr_in = checkAndGetColumn<ColumnFixedString>(col);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_fstr_in)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_str = ColumnString::create();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & in_vec = col_fstr_in->getChars();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t size = col_fstr_in->size();
|
|
|
|
|
|
|
|
out_offsets.resize(size);
|
|
|
|
out_vec.resize(in_vec.size() * 2 + size);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
char * begin = reinterpret_cast<char *>(out_vec.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
char * pos = begin;
|
|
|
|
|
|
|
|
size_t n = col_fstr_in->getN();
|
|
|
|
|
|
|
|
size_t prev_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
size_t new_offset = prev_offset + n;
|
|
|
|
|
|
|
|
executeOneString(&in_vec[prev_offset], &in_vec[new_offset], pos);
|
|
|
|
|
|
|
|
out_offsets[i] = pos - begin;
|
|
|
|
prev_offset = new_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!out_offsets.empty() && out_offsets.back() != out_vec.size())
|
|
|
|
throw Exception("Column size mismatch (internal logical error)", ErrorCodes::LOGICAL_ERROR);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-12-16 02:34:02 +00:00
|
|
|
col_res = std::move(col_str);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const IColumn * column = block.getByPosition(arguments[0]).column.get();
|
|
|
|
ColumnPtr & res_column = block.getByPosition(result).column;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (tryExecuteUInt<UInt8>(column, res_column) ||
|
|
|
|
tryExecuteUInt<UInt16>(column, res_column) ||
|
|
|
|
tryExecuteUInt<UInt32>(column, res_column) ||
|
|
|
|
tryExecuteUInt<UInt64>(column, res_column) ||
|
|
|
|
tryExecuteString(column, res_column) ||
|
2019-09-20 16:21:05 +00:00
|
|
|
tryExecuteFixedString(column, res_column) ||
|
|
|
|
tryExecuteFloat<Float32>(column, res_column) ||
|
2019-10-16 15:28:10 +00:00
|
|
|
tryExecuteFloat<Float64>(column, res_column) ||
|
|
|
|
tryExecuteDecimal<Decimal32>(column, res_column) ||
|
|
|
|
tryExecuteDecimal<Decimal64>(column, res_column) ||
|
|
|
|
tryExecuteDecimal<Decimal128>(column, res_column))
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2013-03-07 12:01:41 +00:00
|
|
|
};
|
2013-03-07 12:43:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FunctionUnhex : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "unhex";
|
2017-12-01 21:51:50 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionUnhex>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isString(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void unhexOne(const char * pos, const char * end, char *& out) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if ((end - pos) & 1)
|
|
|
|
{
|
2017-06-01 13:41:58 +00:00
|
|
|
*out = unhex(*pos);
|
|
|
|
++out;
|
|
|
|
++pos;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
while (pos < end)
|
|
|
|
{
|
2017-08-09 01:34:01 +00:00
|
|
|
*out = unhex2(pos);
|
|
|
|
pos += 2;
|
2017-06-01 13:41:58 +00:00
|
|
|
++out;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-06-01 13:41:58 +00:00
|
|
|
*out = '\0';
|
|
|
|
++out;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnPtr & column = block.getByPosition(arguments[0]).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnString * col = checkAndGetColumn<ColumnString>(column.get()))
|
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 & out_vec = col_res->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & out_offsets = col_res->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & in_vec = col->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnString::Offsets & in_offsets = col->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t size = in_offsets.size();
|
|
|
|
out_offsets.resize(size);
|
|
|
|
out_vec.resize(in_vec.size() / 2 + size);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
char * begin = reinterpret_cast<char *>(out_vec.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
char * pos = begin;
|
|
|
|
size_t prev_offset = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
size_t new_offset = in_offsets[i];
|
|
|
|
|
|
|
|
unhexOne(reinterpret_cast<const char *>(&in_vec[prev_offset]), reinterpret_cast<const char *>(&in_vec[new_offset - 1]), pos);
|
|
|
|
|
|
|
|
out_offsets[i] = pos - begin;
|
|
|
|
|
|
|
|
prev_offset = new_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_vec.resize(pos - begin);
|
2017-12-14 03:56:56 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_res);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
}
|
2013-03-07 12:47:12 +00:00
|
|
|
};
|
2013-03-07 13:00:40 +00:00
|
|
|
|
2019-10-25 10:43:52 +00:00
|
|
|
class FunctionChar : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "char";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionChar>(); }
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2019-10-25 10:43:52 +00:00
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.empty())
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " can't be " + toString(arguments.size())
|
|
|
|
+ ", should be at least 1", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
|
|
|
WhichDataType which(arg);
|
|
|
|
if (!(which.isInt() || which.isUInt() || which.isFloat()))
|
|
|
|
throw Exception("Illegal type " + arg->getName() + " of argument of function " + getName()
|
|
|
|
+ ", must be Int, UInt or Float number",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
|
2019-10-25 10:43:52 +00:00
|
|
|
{
|
|
|
|
auto col_str = ColumnString::create();
|
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
|
|
|
|
|
|
|
const auto size_per_row = arguments.size() + 1;
|
2019-10-31 05:19:28 +00:00
|
|
|
out_vec.resize(size_per_row * input_rows_count);
|
|
|
|
out_offsets.resize(input_rows_count);
|
2019-10-25 10:43:52 +00:00
|
|
|
|
2019-10-31 05:19:28 +00:00
|
|
|
for (size_t row = 0; row < input_rows_count; ++row)
|
2019-10-25 10:43:52 +00:00
|
|
|
{
|
|
|
|
out_offsets[row] = size_per_row + out_offsets[row - 1];
|
|
|
|
out_vec[row * size_per_row + size_per_row - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
2019-10-29 05:20:18 +00:00
|
|
|
Columns columns_holder(arguments.size());
|
|
|
|
for (size_t idx = 0; idx < arguments.size(); ++idx)
|
|
|
|
{
|
2020-01-03 14:52:37 +00:00
|
|
|
//partial const column
|
2019-10-29 11:22:00 +00:00
|
|
|
columns_holder[idx] = block.getByPosition(arguments[idx]).column->convertToFullColumnIfConst();
|
2019-10-29 05:20:18 +00:00
|
|
|
const IColumn * column = columns_holder[idx].get();
|
|
|
|
|
2019-10-31 05:19:28 +00:00
|
|
|
if (!(executeNumber<UInt8>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<UInt16>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<UInt32>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<UInt64>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<Int8>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<Int16>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<Int32>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<Int64>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<Float32>(*column, out_vec, idx, input_rows_count, size_per_row)
|
|
|
|
|| executeNumber<Float64>(*column, out_vec, idx, input_rows_count, size_per_row)))
|
2019-10-25 10:43:52 +00:00
|
|
|
{
|
2019-10-29 05:20:18 +00:00
|
|
|
throw Exception{"Illegal column " + block.getByPosition(arguments[idx]).column->getName()
|
2019-10-25 10:43:52 +00:00
|
|
|
+ " of first argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <typename T>
|
2020-07-21 13:58:07 +00:00
|
|
|
bool executeNumber(const IColumn & src_data, ColumnString::Chars & out_vec, const size_t & column_idx, const size_t & rows, const size_t & size_per_row) const
|
2019-10-25 10:43:52 +00:00
|
|
|
{
|
|
|
|
const ColumnVector<T> * src_data_concrete = checkAndGetColumn<ColumnVector<T>>(&src_data);
|
|
|
|
|
|
|
|
if (!src_data_concrete)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t row = 0; row < rows; ++row)
|
|
|
|
{
|
|
|
|
out_vec[row * size_per_row + column_idx] = static_cast<char>(src_data_concrete->getInt(row));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2013-03-07 13:15:22 +00:00
|
|
|
|
|
|
|
class FunctionBitmaskToArray : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "bitmaskToArray";
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionBitmaskToArray>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2020-04-16 22:23:31 +00:00
|
|
|
bool isInjective(const Block &) const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isInteger(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeArray>(arguments[0]);
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecute(const IColumn * column, ColumnPtr & out_column) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-02-09 19:44:27 +00:00
|
|
|
using UnsignedT = std::make_unsigned_t<T>;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnVector<T> * col_from = checkAndGetColumn<ColumnVector<T>>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_values = ColumnVector<T>::create();
|
2017-12-16 02:34:02 +00:00
|
|
|
auto col_offsets = ColumnArray::ColumnOffsets::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnVector<T>::Container & res_values = col_values->getData();
|
2017-12-16 02:34:02 +00:00
|
|
|
ColumnArray::Offsets & res_offsets = col_offsets->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from = col_from->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from.size();
|
|
|
|
res_offsets.resize(size);
|
|
|
|
res_values.reserve(size * 2);
|
|
|
|
|
|
|
|
for (size_t row = 0; row < size; ++row)
|
|
|
|
{
|
2019-02-09 19:44:27 +00:00
|
|
|
UnsignedT x = vec_from[row];
|
2017-04-01 07:20:54 +00:00
|
|
|
while (x)
|
|
|
|
{
|
2019-02-09 19:44:27 +00:00
|
|
|
UnsignedT y = x & (x - 1);
|
|
|
|
UnsignedT bit = x ^ y;
|
2017-04-01 07:20:54 +00:00
|
|
|
x = y;
|
|
|
|
res_values.push_back(bit);
|
|
|
|
}
|
|
|
|
res_offsets[row] = res_values.size();
|
|
|
|
}
|
|
|
|
|
2017-12-16 02:34:02 +00:00
|
|
|
out_column = ColumnArray::create(std::move(col_values), std::move(col_offsets));
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const IColumn * in_column = block.getByPosition(arguments[0]).column.get();
|
|
|
|
ColumnPtr & out_column = block.getByPosition(result).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (tryExecute<UInt8>(in_column, out_column) ||
|
|
|
|
tryExecute<UInt16>(in_column, out_column) ||
|
|
|
|
tryExecute<UInt32>(in_column, out_column) ||
|
|
|
|
tryExecute<UInt64>(in_column, out_column) ||
|
|
|
|
tryExecute<Int8>(in_column, out_column) ||
|
|
|
|
tryExecute<Int16>(in_column, out_column) ||
|
|
|
|
tryExecute<Int32>(in_column, out_column) ||
|
|
|
|
tryExecute<Int64>(in_column, out_column))
|
|
|
|
return;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of first argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2013-03-07 13:15:22 +00:00
|
|
|
};
|
|
|
|
|
2013-08-01 20:22:22 +00:00
|
|
|
class FunctionToStringCutToZero : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "toStringCutToZero";
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionToStringCutToZero>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isStringOrFixedString(arguments[0]))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecuteString(const IColumn * col, ColumnPtr & col_res) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnString * col_str_in = checkAndGetColumn<ColumnString>(col);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_str_in)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_str = ColumnString::create();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & in_vec = col_str_in->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnString::Offsets & in_offsets = col_str_in->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t size = in_offsets.size();
|
|
|
|
out_offsets.resize(size);
|
|
|
|
out_vec.resize(in_vec.size());
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
char * begin = reinterpret_cast<char *>(out_vec.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
char * pos = begin;
|
2017-04-17 17:49:11 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offset current_in_offset = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2017-04-17 17:49:11 +00:00
|
|
|
const char * pos_in = reinterpret_cast<const char *>(&in_vec[current_in_offset]);
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t current_size = strlen(pos_in);
|
|
|
|
memcpySmallAllowReadWriteOverflow15(pos, pos_in, current_size);
|
|
|
|
pos += current_size;
|
|
|
|
*pos = '\0';
|
2017-04-17 17:49:11 +00:00
|
|
|
++pos;
|
|
|
|
out_offsets[i] = pos - begin;
|
|
|
|
current_in_offset = in_offsets[i];
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
out_vec.resize(pos - begin);
|
|
|
|
|
|
|
|
if (!out_offsets.empty() && out_offsets.back() != out_vec.size())
|
|
|
|
throw Exception("Column size mismatch (internal logical error)", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2017-12-16 02:34:02 +00:00
|
|
|
col_res = std::move(col_str);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
bool tryExecuteFixedString(const IColumn * col, ColumnPtr & col_res) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnFixedString * col_fstr_in = checkAndGetColumn<ColumnFixedString>(col);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (col_fstr_in)
|
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_str = ColumnString::create();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & out_vec = col_str->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & out_offsets = col_str->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
const ColumnString::Chars & in_vec = col_fstr_in->getChars();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t size = col_fstr_in->size();
|
|
|
|
|
|
|
|
out_offsets.resize(size);
|
|
|
|
out_vec.resize(in_vec.size() + size);
|
|
|
|
|
2018-09-02 03:00:04 +00:00
|
|
|
char * begin = reinterpret_cast<char *>(out_vec.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
char * pos = begin;
|
2018-09-02 03:00:04 +00:00
|
|
|
const char * pos_in = reinterpret_cast<const char *>(in_vec.data());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t n = col_fstr_in->getN();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
size_t current_size = strnlen(pos_in, n);
|
|
|
|
memcpySmallAllowReadWriteOverflow15(pos, pos_in, current_size);
|
|
|
|
pos += current_size;
|
|
|
|
*pos = '\0';
|
|
|
|
out_offsets[i] = ++pos - begin;
|
|
|
|
pos_in += n;
|
|
|
|
}
|
|
|
|
out_vec.resize(pos - begin);
|
|
|
|
|
|
|
|
if (!out_offsets.empty() && out_offsets.back() != out_vec.size())
|
|
|
|
throw Exception("Column size mismatch (internal logical error)", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
2017-12-16 02:34:02 +00:00
|
|
|
col_res = std::move(col_str);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const IColumn * column = block.getByPosition(arguments[0]).column.get();
|
|
|
|
ColumnPtr & res_column = block.getByPosition(result).column;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (tryExecuteFixedString(column, res_column) || tryExecuteString(column, res_column))
|
|
|
|
return;
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2013-08-01 20:22:22 +00:00
|
|
|
};
|
|
|
|
|
2019-04-22 11:21:59 +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:
|
|
|
|
/// TODO Inefficient.
|
|
|
|
/// NOTE IPv6 is stored in memory in big endian format that makes some difficulties.
|
|
|
|
static void applyCIDRMask(const UInt8 * __restrict src, UInt8 * __restrict dst_lower, UInt8 * __restrict dst_upper, UInt8 bits_to_keep)
|
2019-04-22 11:21:59 +00:00
|
|
|
{
|
2019-05-02 13:04:24 +00:00
|
|
|
UInt8 mask[16]{};
|
2019-04-23 12:28:25 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
UInt8 bytes_to_keep = bits_to_keep / 8;
|
|
|
|
UInt8 bits_to_keep_in_last_byte = bits_to_keep % 8;
|
2019-04-30 06:53:09 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
for (size_t i = 0; i < bits_to_keep / 8; ++i)
|
|
|
|
mask[i] = 0xFFU;
|
2019-04-23 12:28:25 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
if (bits_to_keep_in_last_byte)
|
|
|
|
mask[bytes_to_keep] = 0xFFU << (8 - bits_to_keep_in_last_byte);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
public:
|
2019-05-01 22:09:24 +00:00
|
|
|
static constexpr auto name = "IPv6CIDRToRange";
|
|
|
|
static FunctionPtr create(const Context &) { 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; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
const auto first_argument = checkAndGetDataType<DataTypeFixedString>(arguments[0].get());
|
|
|
|
if (!first_argument || first_argument->getN() != IPV6_BINARY_LENGTH)
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() +
|
|
|
|
" of first argument of function " + getName() +
|
|
|
|
", expected FixedString(" + toString(IPV6_BINARY_LENGTH) + ")",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
const DataTypePtr & second_argument = arguments[1];
|
2019-05-02 13:04:24 +00:00
|
|
|
if (!isUInt8(second_argument))
|
2019-04-22 11:21:59 +00:00
|
|
|
throw Exception{"Illegal type " + second_argument->getName()
|
|
|
|
+ " of second argument of function " + getName()
|
|
|
|
+ ", expected numeric type.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
DataTypePtr element = DataTypeFactory::instance().get("IPv6");
|
|
|
|
return std::make_shared<DataTypeTuple>(DataTypes{element, element});
|
2019-04-22 11:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
|
2019-04-22 11:21:59 +00:00
|
|
|
{
|
|
|
|
const auto & col_type_name_ip = block.getByPosition(arguments[0]);
|
|
|
|
const ColumnPtr & column_ip = col_type_name_ip.column;
|
|
|
|
|
|
|
|
const auto col_ip_in = checkAndGetColumn<ColumnFixedString>(column_ip.get());
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
if (!col_ip_in)
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
if (col_ip_in->getN() != IPV6_BINARY_LENGTH)
|
2019-04-22 11:21:59 +00:00
|
|
|
throw Exception("Illegal type " + col_type_name_ip.type->getName() +
|
|
|
|
" of column " + col_ip_in->getName() +
|
|
|
|
" argument of function " + getName() +
|
|
|
|
", expected FixedString(" + toString(IPV6_BINARY_LENGTH) + ")",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
const auto & col_type_name_cidr = block.getByPosition(arguments[1]);
|
|
|
|
const ColumnPtr & column_cidr = col_type_name_cidr.column;
|
2019-04-24 05:19:36 +00:00
|
|
|
|
2019-05-02 13:04:24 +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-22 11:21:59 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
if (!col_const_cidr_in && !col_cidr_in)
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[1]).column->getName()
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
const auto & vec_in = col_ip_in->getChars();
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
auto col_res_lower_range = ColumnFixedString::create(IPV6_BINARY_LENGTH);
|
|
|
|
auto col_res_upper_range = ColumnFixedString::create(IPV6_BINARY_LENGTH);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
ColumnString::Chars & vec_res_lower_range = col_res_lower_range->getChars();
|
|
|
|
vec_res_lower_range.resize(input_rows_count * IPV6_BINARY_LENGTH);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
ColumnString::Chars & vec_res_upper_range = col_res_upper_range->getChars();
|
|
|
|
vec_res_upper_range.resize(input_rows_count * IPV6_BINARY_LENGTH);
|
2019-04-22 11:21:59 +00:00
|
|
|
|
2019-07-19 20:58:28 +00:00
|
|
|
static constexpr UInt8 max_cidr_mask = IPV6_BINARY_LENGTH * 8;
|
2019-07-19 09:39:25 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
for (size_t offset = 0; offset < input_rows_count; ++offset)
|
|
|
|
{
|
|
|
|
const size_t offset_ipv6 = offset * IPV6_BINARY_LENGTH;
|
|
|
|
UInt8 cidr = col_const_cidr_in
|
|
|
|
? col_const_cidr_in->getValue<UInt8>()
|
|
|
|
: col_cidr_in->getData()[offset];
|
2019-07-19 09:39:25 +00:00
|
|
|
cidr = std::min(cidr, max_cidr_mask);
|
2019-05-02 13:04:24 +00:00
|
|
|
applyCIDRMask(&vec_in[offset_ipv6], &vec_res_lower_range[offset_ipv6], &vec_res_upper_range[offset_ipv6], cidr);
|
2019-04-22 11:21:59 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
block.getByPosition(result).column = 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)
|
|
|
|
return { UInt32(0), UInt32(-1) };
|
2019-04-26 06:30:07 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
UInt32 mask = UInt32(-1) << (8 * sizeof(UInt32) - bits_to_keep);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
public:
|
2019-05-01 22:09:24 +00:00
|
|
|
static constexpr auto name = "IPv4CIDRToRange";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIPv4CIDRToRange>(); }
|
2019-04-23 07:02:23 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (!WhichDataType(arguments[0]).isUInt32())
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() +
|
|
|
|
" of first argument of function " + getName() +
|
|
|
|
", expected UInt32",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
|
|
|
|
const DataTypePtr & second_argument = arguments[1];
|
2019-05-02 13:04:24 +00:00
|
|
|
if (!isUInt8(second_argument))
|
2019-04-23 07:02:23 +00:00
|
|
|
throw Exception{"Illegal type " + second_argument->getName()
|
|
|
|
+ " of second argument of function " + getName()
|
|
|
|
+ ", expected numeric type.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
DataTypePtr element = DataTypeFactory::instance().get("IPv4");
|
|
|
|
return std::make_shared<DataTypeTuple>(DataTypes{element, element});
|
2019-04-23 07:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
|
2020-07-21 13:58:07 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) const override
|
2019-04-23 07:02:23 +00:00
|
|
|
{
|
|
|
|
const auto & col_type_name_ip = block.getByPosition(arguments[0]);
|
|
|
|
const ColumnPtr & column_ip = col_type_name_ip.column;
|
|
|
|
|
|
|
|
const auto col_ip_in = checkAndGetColumn<ColumnUInt32>(column_ip.get());
|
2019-05-02 13:04:24 +00:00
|
|
|
if (!col_ip_in)
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2019-04-23 07:02:23 +00:00
|
|
|
|
|
|
|
const auto & col_type_name_cidr = block.getByPosition(arguments[1]);
|
|
|
|
const ColumnPtr & column_cidr = col_type_name_cidr.column;
|
2019-04-26 06:30:07 +00:00
|
|
|
|
2019-04-23 07:02:23 +00:00
|
|
|
const auto col_const_cidr_in = checkAndGetColumnConst<ColumnUInt8>(column_cidr.get());
|
|
|
|
const auto col_cidr_in = checkAndGetColumn<ColumnUInt8>(column_cidr.get());
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
if (!col_const_cidr_in && !col_cidr_in)
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[1]).column->getName()
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
const auto & vec_in = col_ip_in->getData();
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
auto col_res_lower_range = ColumnUInt32::create();
|
|
|
|
auto col_res_upper_range = ColumnUInt32::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)
|
|
|
|
{
|
|
|
|
UInt8 cidr = col_const_cidr_in
|
|
|
|
? col_const_cidr_in->getValue<UInt8>()
|
|
|
|
: col_cidr_in->getData()[i];
|
2019-04-23 07:02:23 +00:00
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
std::tie(vec_res_lower_range[i], vec_res_upper_range[i]) = applyCIDRMask(vec_in[i], cidr);
|
2019-04-23 07:02:23 +00:00
|
|
|
}
|
|
|
|
|
2019-05-02 13:04:24 +00:00
|
|
|
block.getByPosition(result).column = ColumnTuple::create(Columns{std::move(col_res_lower_range), std::move(col_res_upper_range)});
|
2019-04-23 07:02:23 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-05 09:32:12 +00:00
|
|
|
}
|