2015-10-29 03:41:09 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <city.h>
|
|
|
|
|
#include <DB/Core/Defines.h>
|
|
|
|
|
#include <DB/Common/SipHash.h>
|
|
|
|
|
#include <DB/Common/UInt128.h>
|
|
|
|
|
#include <DB/Columns/ColumnTuple.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Хэширует набор аргументов агрегатной функции
|
|
|
|
|
* для вычисления количества уникальных значений
|
|
|
|
|
* и добавляет их в множество.
|
|
|
|
|
*
|
|
|
|
|
* Четыре варианта (2 x 2):
|
|
|
|
|
*
|
|
|
|
|
* - для приближённого вычисления, использует некриптографическую 64-битную хэш-функцию;
|
|
|
|
|
* - для точного вычисления, использует криптографическую 128-битную хэш-функцию;
|
|
|
|
|
*
|
|
|
|
|
* - для нескольких аргументов, переданных обычным способом;
|
|
|
|
|
* - для одного аргумента-кортежа.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
template <bool exact, bool for_tuple>
|
|
|
|
|
struct UniqVariadicHash;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
template <>
|
|
|
|
|
struct UniqVariadicHash<false, false>
|
|
|
|
|
{
|
|
|
|
|
static inline UInt64 apply(size_t num_args, const IColumn ** columns, size_t row_num)
|
2015-10-29 03:41:09 +00:00
|
|
|
|
{
|
2015-10-29 04:02:22 +00:00
|
|
|
|
UInt64 hash;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
const IColumn ** column = columns;
|
|
|
|
|
const IColumn ** columns_end = column + num_args;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
{
|
|
|
|
|
StringRef value = (*column)->getDataAt(row_num);
|
|
|
|
|
hash = CityHash64(value.data, value.size);
|
|
|
|
|
++column;
|
|
|
|
|
}
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
while (column < columns_end)
|
|
|
|
|
{
|
|
|
|
|
StringRef value = (*column)->getDataAt(row_num);
|
|
|
|
|
hash = Hash128to64(uint128(CityHash64(value.data, value.size), hash));
|
|
|
|
|
++column;
|
|
|
|
|
}
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
return hash;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
}
|
2015-10-29 04:02:22 +00:00
|
|
|
|
};
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
template <>
|
|
|
|
|
struct UniqVariadicHash<false, true>
|
|
|
|
|
{
|
|
|
|
|
static inline UInt64 apply(size_t num_args, const IColumn ** columns, size_t row_num)
|
2015-10-29 03:41:09 +00:00
|
|
|
|
{
|
2015-10-29 04:02:22 +00:00
|
|
|
|
UInt64 hash;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
const Columns & tuple_columns = static_cast<const ColumnTuple *>(columns[0])->getColumns();
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
const ColumnPtr * column = tuple_columns.data();
|
|
|
|
|
const ColumnPtr * columns_end = column + num_args;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
{
|
|
|
|
|
StringRef value = column->get()->getDataAt(row_num);
|
|
|
|
|
hash = CityHash64(value.data, value.size);
|
|
|
|
|
++column;
|
|
|
|
|
}
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
while (column < columns_end)
|
|
|
|
|
{
|
|
|
|
|
StringRef value = column->get()->getDataAt(row_num);
|
|
|
|
|
hash = Hash128to64(uint128(CityHash64(value.data, value.size), hash));
|
|
|
|
|
++column;
|
|
|
|
|
}
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
return hash;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
}
|
2015-10-29 04:02:22 +00:00
|
|
|
|
};
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
template <>
|
|
|
|
|
struct UniqVariadicHash<true, false>
|
2015-10-29 03:41:09 +00:00
|
|
|
|
{
|
2015-10-29 04:02:22 +00:00
|
|
|
|
static inline UInt128 apply(size_t num_args, const IColumn ** columns, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
SipHash hash;
|
|
|
|
|
|
|
|
|
|
const IColumn ** column = columns;
|
|
|
|
|
const IColumn ** columns_end = column + num_args;
|
|
|
|
|
|
|
|
|
|
while (column < columns_end)
|
|
|
|
|
{
|
|
|
|
|
StringRef value = (*column)->getDataAt(row_num);
|
|
|
|
|
hash.update(reinterpret_cast<const char *>(&value.size), sizeof(value.size));
|
|
|
|
|
hash.update(value.data, value.size);
|
|
|
|
|
++column;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UInt128 key;
|
|
|
|
|
hash.get128(key.first, key.second);
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
template <>
|
|
|
|
|
struct UniqVariadicHash<true, true>
|
|
|
|
|
{
|
|
|
|
|
static inline UInt128 apply(size_t num_args, const IColumn ** columns, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
SipHash hash;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
const Columns & tuple_columns = static_cast<const ColumnTuple *>(columns[0])->getColumns();
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
const ColumnPtr * column = tuple_columns.data();
|
|
|
|
|
const ColumnPtr * columns_end = column + num_args;
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
while (column < columns_end)
|
|
|
|
|
{
|
|
|
|
|
StringRef value = column->get()->getDataAt(row_num);
|
|
|
|
|
hash.update(reinterpret_cast<const char *>(&value.size), sizeof(value.size));
|
|
|
|
|
hash.update(value.data, value.size);
|
|
|
|
|
++column;
|
|
|
|
|
}
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
2015-10-29 04:02:22 +00:00
|
|
|
|
UInt128 key;
|
|
|
|
|
hash.get128(key.first, key.second);
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
|
|
|
|
}
|