2012-07-15 21:43:04 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <openssl/md5.h>
|
2014-10-29 12:25:33 +00:00
|
|
|
#include <openssl/sha.h>
|
2012-07-15 21:43:04 +00:00
|
|
|
#include <city.h>
|
2015-07-17 15:56:08 +00:00
|
|
|
#include <farmhash.h>
|
2015-07-20 14:59:20 +00:00
|
|
|
#include <metrohash.h>
|
2012-07-15 21:43:04 +00:00
|
|
|
|
|
|
|
#include <Poco/ByteOrder.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/SipHash.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
|
|
|
#include <DataTypes/DataTypeEnum.h>
|
2017-12-08 00:50:25 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnFixedString.h>
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <Common/HashTable/Hash.h>
|
|
|
|
#include <Functions/IFunction.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2012-07-15 21:43:04 +00:00
|
|
|
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/range.h>
|
2015-04-15 15:00:28 +00:00
|
|
|
|
2012-07-15 21:43:04 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/** Hashing functions.
|
2012-07-15 21:43:04 +00:00
|
|
|
*
|
2017-10-26 18:36:23 +00:00
|
|
|
* halfMD5: String -> UInt64
|
2012-07-15 21:43:04 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* A faster cryptographic hash function:
|
2017-10-26 18:36:23 +00:00
|
|
|
* sipHash64: String -> UInt64
|
2013-10-21 14:35:12 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* Fast non-cryptographic hash function for strings:
|
2012-07-15 21:43:04 +00:00
|
|
|
* cityHash64: String -> UInt64
|
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* A non-cryptographic hash from a tuple of values of any types (uses cityHash64 for strings and intHash64 for numbers):
|
2017-10-26 18:36:23 +00:00
|
|
|
* cityHash64: any* -> UInt64
|
2014-07-04 09:42:56 +00:00
|
|
|
*
|
2017-05-27 15:45:25 +00:00
|
|
|
* Fast non-cryptographic hash function from any integer:
|
2017-10-26 18:36:23 +00:00
|
|
|
* intHash32: number -> UInt32
|
|
|
|
* intHash64: number -> UInt64
|
2014-07-03 11:24:01 +00:00
|
|
|
*
|
2012-07-15 21:43:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct HalfMD5Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt64 apply(const char * begin, size_t size)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned char char_data[16];
|
|
|
|
Poco::UInt64 uint64_data;
|
|
|
|
} buf;
|
|
|
|
|
|
|
|
MD5_CTX ctx;
|
|
|
|
MD5_Init(&ctx);
|
|
|
|
MD5_Update(&ctx, reinterpret_cast<const unsigned char *>(begin), size);
|
|
|
|
MD5_Final(buf.char_data, &ctx);
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
return Poco::ByteOrder::flipBytes(buf.uint64_data); /// Compatibility with existing code.
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-07-15 21:43:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-29 12:25:33 +00:00
|
|
|
struct MD5Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "MD5";
|
|
|
|
enum { length = 16 };
|
|
|
|
|
|
|
|
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
|
|
|
|
{
|
|
|
|
MD5_CTX ctx;
|
|
|
|
MD5_Init(&ctx);
|
|
|
|
MD5_Update(&ctx, reinterpret_cast<const unsigned char *>(begin), size);
|
|
|
|
MD5_Final(out_char_data, &ctx);
|
|
|
|
}
|
2014-10-29 12:25:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SHA1Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "SHA1";
|
|
|
|
enum { length = 20 };
|
|
|
|
|
|
|
|
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
|
|
|
|
{
|
|
|
|
SHA_CTX ctx;
|
|
|
|
SHA1_Init(&ctx);
|
|
|
|
SHA1_Update(&ctx, reinterpret_cast<const unsigned char *>(begin), size);
|
|
|
|
SHA1_Final(out_char_data, &ctx);
|
|
|
|
}
|
2014-10-29 12:25:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SHA224Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "SHA224";
|
|
|
|
enum { length = 28 };
|
|
|
|
|
|
|
|
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
|
|
|
|
{
|
|
|
|
SHA256_CTX ctx;
|
|
|
|
SHA224_Init(&ctx);
|
|
|
|
SHA224_Update(&ctx, reinterpret_cast<const unsigned char *>(begin), size);
|
|
|
|
SHA224_Final(out_char_data, &ctx);
|
|
|
|
}
|
2014-10-29 12:25:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SHA256Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "SHA256";
|
|
|
|
enum { length = 32 };
|
|
|
|
|
|
|
|
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
|
|
|
|
{
|
|
|
|
SHA256_CTX ctx;
|
|
|
|
SHA256_Init(&ctx);
|
|
|
|
SHA256_Update(&ctx, reinterpret_cast<const unsigned char *>(begin), size);
|
|
|
|
SHA256_Final(out_char_data, &ctx);
|
|
|
|
}
|
2014-10-29 12:25:33 +00:00
|
|
|
};
|
|
|
|
|
2013-10-21 14:35:12 +00:00
|
|
|
struct SipHash64Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt64 apply(const char * begin, size_t size)
|
|
|
|
{
|
|
|
|
return sipHash64(begin, size);
|
|
|
|
}
|
2013-10-21 14:35:12 +00:00
|
|
|
};
|
|
|
|
|
2014-10-29 12:25:33 +00:00
|
|
|
struct SipHash128Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "sipHash128";
|
|
|
|
enum { length = 16 };
|
2014-10-29 12:25:33 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static void apply(const char * begin, const size_t size, unsigned char * out_char_data)
|
|
|
|
{
|
|
|
|
sipHash128(begin, size, reinterpret_cast<char*>(out_char_data));
|
|
|
|
}
|
2014-10-29 12:25:33 +00:00
|
|
|
};
|
|
|
|
|
2012-07-15 21:43:04 +00:00
|
|
|
struct IntHash32Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ReturnType = UInt32;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt32 apply(UInt64 x)
|
|
|
|
{
|
2017-05-27 15:45:25 +00:00
|
|
|
/// seed is taken from /dev/urandom. It allows you to avoid undesirable dependencies with hashes in different data structures.
|
2017-04-01 07:20:54 +00:00
|
|
|
return intHash32<0x75D9543DE018BF45ULL>(x);
|
|
|
|
}
|
2012-07-15 21:43:04 +00:00
|
|
|
};
|
|
|
|
|
2014-05-10 05:17:08 +00:00
|
|
|
struct IntHash64Impl
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ReturnType = UInt64;
|
2014-05-10 05:17:08 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static UInt64 apply(UInt64 x)
|
|
|
|
{
|
|
|
|
return intHash64(x ^ 0x4CF2D2BAAE6DA887ULL);
|
|
|
|
}
|
2014-05-10 05:17:08 +00:00
|
|
|
};
|
|
|
|
|
2012-07-15 21:43:04 +00:00
|
|
|
|
|
|
|
template <typename Impl, typename Name>
|
|
|
|
class FunctionStringHash64 : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionStringHash64>(); };
|
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
|
|
|
|
{
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!arguments[0]->isString())
|
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<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnString * col_from = checkAndGetColumn<ColumnString>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnUInt64::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const typename ColumnString::Chars_t & data = col_from->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnString::Offsets & offsets = col_from->getOffsets();
|
|
|
|
typename ColumnUInt64::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = offsets.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
vec_to[i] = Impl::apply(
|
|
|
|
reinterpret_cast<const char *>(&data[i == 0 ? 0 : offsets[i - 1]]),
|
|
|
|
i == 0 ? offsets[i] - 1 : (offsets[i] - 1 - offsets[i - 1]));
|
2017-12-16 05:21:04 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
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 first argument of function " + Name::name,
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2012-07-15 21:43:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-29 12:25:33 +00:00
|
|
|
template <typename Impl>
|
|
|
|
class FunctionStringHashFixedString : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Impl::name;
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionStringHashFixedString>(); };
|
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
|
|
|
|
{
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!arguments[0]->isString())
|
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>(Impl::length);
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnString * col_from = checkAndGetColumn<ColumnString>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnFixedString::create(Impl::length);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const typename ColumnString::Chars_t & data = col_from->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnString::Offsets & offsets = col_from->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
auto & chars_to = col_to->getChars();
|
|
|
|
const auto size = offsets.size();
|
|
|
|
chars_to.resize(size * Impl::length);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
Impl::apply(
|
|
|
|
reinterpret_cast<const char *>(&data[i == 0 ? 0 : offsets[i - 1]]),
|
|
|
|
i == 0 ? offsets[i] - 1 : (offsets[i] - 1 - offsets[i - 1]),
|
|
|
|
&chars_to[i * Impl::length]);
|
2017-12-16 05:21:04 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
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 first argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2014-10-29 12:25:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-07-15 21:43:04 +00:00
|
|
|
template <typename Impl, typename Name>
|
|
|
|
class FunctionIntHash : public IFunction
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Name::name;
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionIntHash>(); };
|
2014-11-12 17:23:26 +00:00
|
|
|
|
2012-07-15 21:43:04 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
using ToType = typename Impl::ReturnType;
|
|
|
|
|
|
|
|
template <typename FromType>
|
|
|
|
void executeType(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (auto col_from = checkAndGetColumn<ColumnVector<FromType>>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnVector<ToType>::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<FromType>::Container & vec_from = col_from->getData();
|
|
|
|
typename ColumnVector<ToType>::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size_t size = vec_from.size();
|
|
|
|
vec_to.resize(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
vec_to[i] = Impl::apply(vec_from[i]);
|
2017-12-16 05:21:04 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
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 first argument of function " + Name::name,
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2012-07-15 21:43:04 +00:00
|
|
|
|
|
|
|
public:
|
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
|
|
|
|
{
|
2017-12-09 06:32:22 +00:00
|
|
|
if (!arguments[0]->isValueRepresentedByNumber())
|
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<DataTypeNumber<typename Impl::ReturnType>>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-18 01:11:48 +00:00
|
|
|
const IDataType * from_type = block.getByPosition(arguments[0]).type.get();
|
2017-07-21 06:35:58 +00:00
|
|
|
|
|
|
|
if (checkDataType<DataTypeUInt8>(from_type)) executeType<UInt8>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeUInt16>(from_type)) executeType<UInt16>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeUInt32>(from_type)) executeType<UInt32>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeUInt64>(from_type)) executeType<UInt64>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeInt8>(from_type)) executeType<Int8>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeInt16>(from_type)) executeType<Int16>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeInt32>(from_type)) executeType<Int32>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeInt64>(from_type)) executeType<Int64>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeDate>(from_type)) executeType<UInt16>(block, arguments, result);
|
|
|
|
else if (checkDataType<DataTypeDateTime>(from_type)) executeType<UInt32>(block, arguments, result);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal type " + block.getByPosition(arguments[0]).type->getName() + " of argument of function " + getName(),
|
2017-04-01 07:20:54 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
2012-07-15 21:43:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-03 11:24:01 +00:00
|
|
|
template <typename T>
|
|
|
|
static UInt64 toInteger(T x)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return x;
|
2014-07-03 11:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2016-05-24 19:01:17 +00:00
|
|
|
UInt64 toInteger<Float32>(Float32 x);
|
2014-07-03 11:24:01 +00:00
|
|
|
|
|
|
|
template <>
|
2016-05-24 19:01:17 +00:00
|
|
|
UInt64 toInteger<Float64>(Float64 x);
|
2014-07-03 11:24:01 +00:00
|
|
|
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/** We use hash functions called CityHash, FarmHash, MetroHash.
|
|
|
|
* In this regard, this template is named with the words `NeighborhoodHash`.
|
2015-10-29 00:12:04 +00:00
|
|
|
*/
|
2015-07-17 15:56:08 +00:00
|
|
|
template <typename Impl>
|
|
|
|
class FunctionNeighbourhoodHash64 : public IFunction
|
2014-07-03 11:24:01 +00:00
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = Impl::name;
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionNeighbourhoodHash64>(); };
|
2014-11-12 17:23:26 +00:00
|
|
|
|
2014-07-03 11:24:01 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename FromType, bool first>
|
2017-12-15 21:32:25 +00:00
|
|
|
void executeIntType(const IColumn * column, ColumnUInt64::Container & vec_to)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnVector<FromType> * col_from = checkAndGetColumn<ColumnVector<FromType>>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<FromType>::Container & vec_from = col_from->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
UInt64 h = IntHash64Impl::apply(toInteger(vec_from[i]));
|
|
|
|
if (first)
|
|
|
|
vec_to[i] = h;
|
|
|
|
else
|
|
|
|
vec_to[i] = Impl::Hash128to64(typename Impl::uint128_t(vec_to[i], h));
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (auto col_from = checkAndGetColumnConst<ColumnVector<FromType>>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const UInt64 hash = IntHash64Impl::apply(toInteger(col_from->template getValue<FromType>()));
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_to.size();
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
vec_to.assign(size, hash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
vec_to[i] = Impl::Hash128to64(typename Impl::uint128_t(vec_to[i], hash));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Illegal column " + column->getName()
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool first>
|
2017-12-15 21:32:25 +00:00
|
|
|
void executeString(const IColumn * column, ColumnUInt64::Container & vec_to)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnString * col_from = checkAndGetColumn<ColumnString>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const typename ColumnString::Chars_t & data = col_from->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnString::Offsets & offsets = col_from->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = offsets.size();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
const UInt64 h = Impl::Hash64(
|
|
|
|
reinterpret_cast<const char *>(&data[i == 0 ? 0 : offsets[i - 1]]),
|
|
|
|
i == 0 ? offsets[i] - 1 : (offsets[i] - 1 - offsets[i - 1]));
|
|
|
|
if (first)
|
|
|
|
vec_to[i] = h;
|
|
|
|
else
|
|
|
|
vec_to[i] = Impl::Hash128to64(typename Impl::uint128_t(vec_to[i], h));
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (const ColumnFixedString * col_from = checkAndGetColumn<ColumnFixedString>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const typename ColumnString::Chars_t & data = col_from->getChars();
|
|
|
|
size_t n = col_from->getN();
|
|
|
|
size_t size = data.size() / n;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
const UInt64 h = Impl::Hash64(reinterpret_cast<const char *>(&data[i * n]), n);
|
|
|
|
if (first)
|
|
|
|
vec_to[i] = h;
|
|
|
|
else
|
|
|
|
vec_to[i] = Impl::Hash128to64(typename Impl::uint128_t(vec_to[i], h));
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (const ColumnConst * col_from = checkAndGetColumnConstStringOrFixedString(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
String value = col_from->getValue<String>().data();
|
|
|
|
const UInt64 hash = Impl::Hash64(value.data(), value.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
const size_t size = vec_to.size();
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
vec_to.assign(size, hash);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
vec_to[i] = Impl::Hash128to64(typename Impl::uint128_t(vec_to[i], hash));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Illegal column " + column->getName()
|
|
|
|
+ " of first argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool first>
|
2017-12-15 21:32:25 +00:00
|
|
|
void executeArray(const IDataType * type, const IColumn * column, ColumnUInt64::Container & vec_to)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const IDataType * nested_type = typeid_cast<const DataTypeArray *>(type)->getNestedType().get();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnArray * col_from = checkAndGetColumn<ColumnArray>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const IColumn * nested_column = &col_from->getData();
|
2017-12-15 21:32:25 +00:00
|
|
|
const ColumnArray::Offsets & offsets = col_from->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
const size_t nested_size = nested_column->size();
|
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnUInt64::Container vec_temp(nested_size);
|
2017-04-01 07:20:54 +00:00
|
|
|
executeAny<true>(nested_type, nested_column, vec_temp);
|
|
|
|
|
|
|
|
const size_t size = offsets.size();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
const size_t begin = i == 0 ? 0 : offsets[i - 1];
|
|
|
|
const size_t end = offsets[i];
|
|
|
|
|
|
|
|
UInt64 h = IntHash64Impl::apply(end - begin);
|
|
|
|
if (first)
|
|
|
|
vec_to[i] = h;
|
|
|
|
else
|
|
|
|
vec_to[i] = Impl::Hash128to64(typename Impl::uint128_t(vec_to[i], h));
|
|
|
|
|
|
|
|
for (size_t j = begin; j < end; ++j)
|
|
|
|
vec_to[i] = Impl::Hash128to64(typename Impl::uint128_t(vec_to[i], vec_temp[j]));
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 06:35:58 +00:00
|
|
|
else if (const ColumnConst * col_from = checkAndGetColumnConst<ColumnArray>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-05-27 15:45:25 +00:00
|
|
|
/// NOTE: here, of course, you can do without the materialization of the column.
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnPtr full_column = col_from->convertToFullColumn();
|
|
|
|
executeArray<first>(type, &*full_column, vec_to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Illegal column " + column->getName()
|
|
|
|
+ " of first argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool first>
|
2017-12-15 21:32:25 +00:00
|
|
|
void executeAny(const IDataType * from_type, const IColumn * icolumn, ColumnUInt64::Container & vec_to)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (checkDataType<DataTypeUInt8>(from_type)) executeIntType<UInt8, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeUInt16>(from_type)) executeIntType<UInt16, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeUInt32>(from_type)) executeIntType<UInt32, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeUInt64>(from_type)) executeIntType<UInt64, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeInt8>(from_type)) executeIntType<Int8, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeInt16>(from_type)) executeIntType<Int16, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeInt32>(from_type)) executeIntType<Int32, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeInt64>(from_type)) executeIntType<Int64, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeEnum8>(from_type)) executeIntType<Int8, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeEnum16>(from_type)) executeIntType<Int16, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeDate>(from_type)) executeIntType<UInt16, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeDateTime>(from_type)) executeIntType<UInt32, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeFloat32>(from_type)) executeIntType<Float32, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeFloat64>(from_type)) executeIntType<Float64, first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeString>(from_type)) executeString<first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeFixedString>(from_type)) executeString<first>(icolumn, vec_to);
|
|
|
|
else if (checkDataType<DataTypeArray>(from_type)) executeArray<first>(from_type, icolumn, vec_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
throw Exception("Unexpected type " + from_type->getName() + " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
void executeForArgument(const IDataType * type, const IColumn * column, ColumnUInt64::Container & vec_to, bool & is_first)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
/// Flattening of tuples.
|
|
|
|
if (const ColumnTuple * tuple = typeid_cast<const ColumnTuple *>(column))
|
|
|
|
{
|
2017-12-08 00:50:25 +00:00
|
|
|
const Columns & tuple_columns = tuple->getColumns();
|
|
|
|
const DataTypes & tuple_types = typeid_cast<const DataTypeTuple &>(*type).getElements();
|
|
|
|
size_t tuple_size = tuple_columns.size();
|
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
|
|
|
executeForArgument(tuple_types[i].get(), tuple_columns[i].get(), vec_to, is_first);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-12-09 12:23:09 +00:00
|
|
|
else if (const ColumnTuple * tuple = checkAndGetColumnConstData<ColumnTuple>(column))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-09 12:23:09 +00:00
|
|
|
const Columns & tuple_columns = tuple->getColumns();
|
|
|
|
const DataTypes & tuple_types = typeid_cast<const DataTypeTuple &>(*type).getElements();
|
|
|
|
size_t tuple_size = tuple_columns.size();
|
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
|
|
|
{
|
2017-12-16 05:46:46 +00:00
|
|
|
auto tmp = ColumnConst::create(tuple_columns[i], column->size());
|
|
|
|
executeForArgument(tuple_types[i].get(), tmp.get(), vec_to, is_first);
|
2017-12-09 12:23:09 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (is_first)
|
|
|
|
executeAny<true>(type, column, vec_to);
|
|
|
|
else
|
|
|
|
executeAny<false>(type, column, vec_to);
|
|
|
|
}
|
|
|
|
|
|
|
|
is_first = false;
|
|
|
|
}
|
2015-10-29 00:12:04 +00:00
|
|
|
|
2014-07-03 11:24:01 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
2017-12-17 10:51:19 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-04-24 07:16:39 +00:00
|
|
|
size_t rows = input_rows_count;
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnUInt64::create(rows);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnUInt64::Container & vec_to = col_to->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (arguments.empty())
|
|
|
|
{
|
|
|
|
/// Constant random number from /dev/urandom is used as a hash value of empty list of arguments.
|
2017-10-14 01:03:35 +00:00
|
|
|
vec_to.assign(rows, static_cast<UInt64>(0xe28dbde7fe22e41c));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The function supports arbitary number of arguments of arbitary types.
|
|
|
|
|
|
|
|
bool is_first_argument = true;
|
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const ColumnWithTypeAndName & col = block.getByPosition(arguments[i]);
|
2017-04-01 07:20:54 +00:00
|
|
|
executeForArgument(col.type.get(), col.column.get(), vec_to, is_first_argument);
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:51:19 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2014-07-03 11:24:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-04-15 15:00:28 +00:00
|
|
|
struct URLHashImpl
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
static UInt64 apply(const char * data, const size_t size)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
/// do not take last slash, '?' or '#' character into account
|
|
|
|
if (size > 0 && (data[size - 1] == '/' || data[size - 1] == '?' || data[size - 1] == '#'))
|
2017-06-21 08:35:38 +00:00
|
|
|
return CityHash_v1_0_2::CityHash64(data, size - 1);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-06-21 08:35:38 +00:00
|
|
|
return CityHash_v1_0_2::CityHash64(data, size);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-04-15 15:00:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct URLHierarchyHashImpl
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
static size_t findLevelLength(const UInt64 level, const char * begin, const char * end)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
auto pos = begin;
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Let's parse everything that goes before the path
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// Suppose that the protocol has already been changed to lowercase.
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos < end && ((*pos > 'a' && *pos < 'z') || (*pos > '0' && *pos < '9')))
|
|
|
|
++pos;
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/** We will calculate the hierarchy only for URLs in which there is a protocol, and after it there are two slashes.
|
|
|
|
* (http, file - fit, mailto, magnet - do not fit), and after two slashes there is still something
|
|
|
|
* For the rest, simply return the full URL as the only element of the hierarchy.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
if (pos == begin || pos == end || !(*pos++ == ':' && pos < end && *pos++ == '/' && pos < end && *pos++ == '/' && pos < end))
|
|
|
|
{
|
|
|
|
pos = end;
|
|
|
|
return 0 == level ? pos - begin : 0;
|
|
|
|
}
|
|
|
|
|
2017-05-27 15:45:25 +00:00
|
|
|
/// The domain for simplicity is everything that after the protocol and the two slashes, until the next slash or before `?` or `#`
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos < end && !(*pos == '/' || *pos == '?' || *pos == '#'))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos != end)
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (0 == level)
|
|
|
|
return pos - begin;
|
|
|
|
|
|
|
|
UInt64 current_level = 0;
|
|
|
|
|
|
|
|
while (current_level != level && pos < end)
|
|
|
|
{
|
2017-05-27 15:45:25 +00:00
|
|
|
/// We go to the next `/` or `?` or `#`, skipping all at the beginning.
|
2017-04-01 07:20:54 +00:00
|
|
|
while (pos < end && (*pos == '/' || *pos == '?' || *pos == '#'))
|
|
|
|
++pos;
|
|
|
|
if (pos == end)
|
|
|
|
break;
|
|
|
|
while (pos < end && !(*pos == '/' || *pos == '?' || *pos == '#'))
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
if (pos != end)
|
|
|
|
++pos;
|
|
|
|
|
|
|
|
++current_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current_level == level ? pos - begin : 0;
|
|
|
|
}
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
static UInt64 apply(const UInt64 level, const char * data, const size_t size)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return URLHashImpl::apply(data, findLevelLength(level, data, data + size));
|
|
|
|
}
|
2015-04-15 15:00:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionURLHash : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "URLHash";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionURLHash>(); }
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
const auto arg_count = arguments.size();
|
|
|
|
if (arg_count != 1 && arg_count != 2)
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Number of arguments for function " + getName() + " doesn't match: passed " +
|
|
|
|
toString(arg_count) + ", should be 1 or 2.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const auto first_arg = arguments.front().get();
|
2017-07-21 06:35:58 +00:00
|
|
|
if (!checkDataType<DataTypeString>(first_arg))
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Illegal type " + first_arg->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (arg_count == 2)
|
|
|
|
{
|
|
|
|
const auto second_arg = arguments.back().get();
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!second_arg->isInteger())
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Illegal type " + second_arg->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; }
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const auto arg_count = arguments.size();
|
|
|
|
|
|
|
|
if (arg_count == 1)
|
|
|
|
executeSingleArg(block, arguments, result);
|
|
|
|
else if (arg_count == 2)
|
|
|
|
executeTwoArgs(block, arguments, result);
|
|
|
|
else
|
2017-06-13 02:06:53 +00:00
|
|
|
throw Exception{"got into IFunction::execute with unexpected number of arguments", ErrorCodes::LOGICAL_ERROR};
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-04-15 15:00:28 +00:00
|
|
|
|
|
|
|
private:
|
2017-07-21 06:35:58 +00:00
|
|
|
void executeSingleArg(Block & block, const ColumnNumbers & arguments, const size_t result) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto col_untyped = block.getByPosition(arguments.front()).column.get();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const auto col_from = checkAndGetColumn<ColumnString>(col_untyped))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const auto size = col_from->size();
|
2017-12-16 05:46:46 +00:00
|
|
|
auto col_to = ColumnUInt64::create(size);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const auto & chars = col_from->getChars();
|
|
|
|
const auto & offsets = col_from->getOffsets();
|
|
|
|
auto & out = col_to->getData();
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = URLHashImpl::apply(
|
|
|
|
reinterpret_cast<const char *>(&chars[i == 0 ? 0 : offsets[i - 1]]),
|
|
|
|
i == 0 ? offsets[i] - 1 : (offsets[i] - 1 - offsets[i - 1]));
|
2017-12-16 05:21:04 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Illegal column " + block.getByPosition(arguments[0]).column->getName() +
|
|
|
|
" of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN};
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
void executeTwoArgs(Block & block, const ColumnNumbers & arguments, const size_t result) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto level_col = block.getByPosition(arguments.back()).column.get();
|
2017-12-09 10:14:45 +00:00
|
|
|
if (!level_col->isColumnConst())
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Second argument of function " + getName() + " must be an integral constant", ErrorCodes::ILLEGAL_COLUMN};
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const auto level = level_col->get64(0);
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
const auto col_untyped = block.getByPosition(arguments.front()).column.get();
|
|
|
|
if (const auto col_from = checkAndGetColumn<ColumnString>(col_untyped))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
const auto size = col_from->size();
|
2017-12-16 05:46:46 +00:00
|
|
|
auto col_to = ColumnUInt64::create(size);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
const auto & chars = col_from->getChars();
|
|
|
|
const auto & offsets = col_from->getOffsets();
|
|
|
|
auto & out = col_to->getData();
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
out[i] = URLHierarchyHashImpl::apply(level,
|
|
|
|
reinterpret_cast<const char *>(&chars[i == 0 ? 0 : offsets[i - 1]]),
|
|
|
|
i == 0 ? offsets[i] - 1 : (offsets[i] - 1 - offsets[i - 1]));
|
2017-12-16 05:21:04 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
2018-05-07 02:01:11 +00:00
|
|
|
throw Exception{"Illegal column " + block.getByPosition(arguments[0]).column->getName() +
|
|
|
|
" of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN};
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-04-15 15:00:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
struct NameHalfMD5 { static constexpr auto name = "halfMD5"; };
|
|
|
|
struct NameSipHash64 { static constexpr auto name = "sipHash64"; };
|
|
|
|
struct NameIntHash32 { static constexpr auto name = "intHash32"; };
|
|
|
|
struct NameIntHash64 { static constexpr auto name = "intHash64"; };
|
2012-07-15 21:43:04 +00:00
|
|
|
|
2015-07-17 15:56:08 +00:00
|
|
|
struct ImplCityHash64
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "cityHash64";
|
2017-06-21 08:35:38 +00:00
|
|
|
using uint128_t = CityHash_v1_0_2::uint128;
|
2015-07-17 15:56:08 +00:00
|
|
|
|
2017-06-21 08:35:38 +00:00
|
|
|
static auto Hash128to64(const uint128_t & x) { return CityHash_v1_0_2::Hash128to64(x); }
|
2017-07-21 06:35:58 +00:00
|
|
|
static auto Hash64(const char * s, const size_t len) { return CityHash_v1_0_2::CityHash64(s, len); }
|
2015-07-17 15:56:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ImplFarmHash64
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "farmHash64";
|
|
|
|
using uint128_t = farmhash::uint128_t;
|
2015-07-17 15:56:08 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static auto Hash128to64(const uint128_t & x) { return farmhash::Hash128to64(x); }
|
2017-07-21 06:35:58 +00:00
|
|
|
static auto Hash64(const char * s, const size_t len) { return farmhash::Hash64(s, len); }
|
2015-07-17 15:56:08 +00:00
|
|
|
};
|
|
|
|
|
2015-07-20 14:59:20 +00:00
|
|
|
struct ImplMetroHash64
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "metroHash64";
|
2017-06-21 08:35:38 +00:00
|
|
|
using uint128_t = CityHash_v1_0_2::uint128;
|
2015-07-20 14:59:20 +00:00
|
|
|
|
2017-06-21 08:35:38 +00:00
|
|
|
static auto Hash128to64(const uint128_t & x) { return CityHash_v1_0_2::Hash128to64(x); }
|
2017-07-21 06:35:58 +00:00
|
|
|
static auto Hash64(const char * s, const size_t len)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-01-10 00:04:08 +00:00
|
|
|
union
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
UInt64 u64;
|
|
|
|
UInt8 u8[sizeof(u64)];
|
|
|
|
};
|
2015-07-20 14:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
metrohash64_1(reinterpret_cast<const UInt8 *>(s), len, 0, u8);
|
2015-07-20 14:59:20 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return u64;
|
|
|
|
}
|
2015-07-20 14:59:20 +00:00
|
|
|
};
|
|
|
|
|
2015-07-17 15:56:08 +00:00
|
|
|
using FunctionHalfMD5 = FunctionStringHash64<HalfMD5Impl, NameHalfMD5>;
|
|
|
|
using FunctionSipHash64 = FunctionStringHash64<SipHash64Impl, NameSipHash64>;
|
|
|
|
using FunctionIntHash32 = FunctionIntHash<IntHash32Impl, NameIntHash32>;
|
|
|
|
using FunctionIntHash64 = FunctionIntHash<IntHash64Impl, NameIntHash64>;
|
|
|
|
using FunctionMD5 = FunctionStringHashFixedString<MD5Impl>;
|
|
|
|
using FunctionSHA1 = FunctionStringHashFixedString<SHA1Impl>;
|
|
|
|
using FunctionSHA224 = FunctionStringHashFixedString<SHA224Impl>;
|
|
|
|
using FunctionSHA256 = FunctionStringHashFixedString<SHA256Impl>;
|
|
|
|
using FunctionSipHash128 = FunctionStringHashFixedString<SipHash128Impl>;
|
|
|
|
using FunctionCityHash64 = FunctionNeighbourhoodHash64<ImplCityHash64>;
|
|
|
|
using FunctionFarmHash64 = FunctionNeighbourhoodHash64<ImplFarmHash64>;
|
2015-07-20 14:59:20 +00:00
|
|
|
using FunctionMetroHash64 = FunctionNeighbourhoodHash64<ImplMetroHash64>;
|
2012-07-15 21:43:04 +00:00
|
|
|
|
|
|
|
}
|