2011-09-26 04:00:46 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <city.h>
|
2014-02-02 09:08:06 +00:00
|
|
|
|
#include <type_traits>
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
|
|
|
|
#include <stats/UniquesHashSet.h>
|
2013-08-21 13:26:42 +00:00
|
|
|
|
#include <statdaemons/HyperLogLogCounter.h>
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
2013-04-26 18:57:08 +00:00
|
|
|
|
#include <DB/IO/WriteBufferFromString.h>
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2013-04-26 18:57:08 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
2014-02-02 09:08:06 +00:00
|
|
|
|
#include <DB/Interpreters/AggregationCommon.h>
|
|
|
|
|
#include <DB/Interpreters/HashSet.h>
|
|
|
|
|
|
2013-06-30 11:38:46 +00:00
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
|
|
2011-09-26 04:00:46 +00:00
|
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2013-06-25 14:16:16 +00:00
|
|
|
|
template <typename T> struct AggregateFunctionUniqTraits
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
|
static UInt64 hash(T x) { return x; }
|
2011-09-26 04:00:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-06-25 14:16:16 +00:00
|
|
|
|
template <> struct AggregateFunctionUniqTraits<Float32>
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
|
static UInt64 hash(Float32 x)
|
|
|
|
|
{
|
|
|
|
|
UInt64 res = 0;
|
|
|
|
|
memcpy(reinterpret_cast<char *>(&res), reinterpret_cast<char *>(&x), sizeof(x));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2011-09-26 04:00:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <> struct AggregateFunctionUniqTraits<Float64>
|
|
|
|
|
{
|
|
|
|
|
static UInt64 hash(Float64 x)
|
|
|
|
|
{
|
|
|
|
|
UInt64 res = 0;
|
|
|
|
|
memcpy(reinterpret_cast<char *>(&res), reinterpret_cast<char *>(&x), sizeof(x));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-08-21 13:26:42 +00:00
|
|
|
|
struct AggregateFunctionUniqUniquesHashSetData
|
2013-02-08 19:34:44 +00:00
|
|
|
|
{
|
2013-08-21 13:26:42 +00:00
|
|
|
|
typedef UniquesHashSet Set;
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniq"; }
|
2013-02-08 19:34:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2013-08-21 13:26:42 +00:00
|
|
|
|
struct AggregateFunctionUniqHLL12Data
|
|
|
|
|
{
|
2013-08-22 10:58:52 +00:00
|
|
|
|
typedef HLL12 Set;
|
2013-08-21 13:26:42 +00:00
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniqHLL12"; }
|
|
|
|
|
};
|
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
|
2014-02-02 09:08:06 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct AggregateFunctionUniqExactData
|
|
|
|
|
{
|
|
|
|
|
typedef T Key;
|
|
|
|
|
|
|
|
|
|
struct GrowthTraits : public default_growth_traits
|
|
|
|
|
{
|
|
|
|
|
/// При создании, хэш-таблица должна быть небольшой.
|
|
|
|
|
static const int INITIAL_SIZE_DEGREE = 64 / sizeof(Key);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef HashSet<
|
|
|
|
|
Key,
|
|
|
|
|
default_hash<Key>,
|
|
|
|
|
default_zero_traits<Key>,
|
|
|
|
|
GrowthTraits,
|
|
|
|
|
HashTableAllocatorWithStackMemory<64>
|
|
|
|
|
> Set;
|
|
|
|
|
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniqExact"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Для строк будем класть в хэш-таблицу значения SipHash-а (128 бит).
|
|
|
|
|
template <>
|
|
|
|
|
struct AggregateFunctionUniqExactData<String>
|
|
|
|
|
{
|
|
|
|
|
typedef UInt128 Key;
|
|
|
|
|
|
|
|
|
|
struct GrowthTraits : public default_growth_traits
|
|
|
|
|
{
|
|
|
|
|
/// При создании, хэш-таблица должна быть небольшой.
|
|
|
|
|
static const int INITIAL_SIZE_DEGREE = 64 / sizeof(Key);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef HashSet<
|
|
|
|
|
Key,
|
|
|
|
|
UInt128TrivialHash,
|
|
|
|
|
UInt128ZeroTraits,
|
|
|
|
|
GrowthTraits,
|
|
|
|
|
HashTableAllocatorWithStackMemory<64>
|
|
|
|
|
> Set;
|
|
|
|
|
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniqExact"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
|
{
|
|
|
|
|
/** Структура для делегации работы по добавлению одного элемента в аггрегатные функции uniq.
|
|
|
|
|
* Используется для частичной специализации для добавления строк.
|
|
|
|
|
*/
|
|
|
|
|
template<typename T, typename Data>
|
|
|
|
|
struct OneAdder
|
|
|
|
|
{
|
|
|
|
|
static void addOne(Data & data, const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
data.set.insert(AggregateFunctionUniqTraits<T>::hash(static_cast<const ColumnVector<T> &>(column).getData()[row_num]));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Data>
|
|
|
|
|
struct OneAdder<String, Data>
|
|
|
|
|
{
|
|
|
|
|
static void addOne(Data & data, const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
/// Имейте ввиду, что вычисление приближённое.
|
|
|
|
|
StringRef value = column.getDataAt(row_num);
|
|
|
|
|
data.set.insert(CityHash64(value.data, value.size));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
struct OneAdder<T, AggregateFunctionUniqExactData<T> >
|
|
|
|
|
{
|
|
|
|
|
static void addOne(AggregateFunctionUniqExactData<T> & data, const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
data.set.insert(static_cast<const ColumnVector<T> &>(column).getData()[row_num]);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct OneAdder<String, AggregateFunctionUniqExactData<String> >
|
|
|
|
|
{
|
|
|
|
|
static void addOne(AggregateFunctionUniqExactData<String> & data, const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
StringRef value = column.getDataAt(row_num);
|
|
|
|
|
|
|
|
|
|
UInt128 key;
|
|
|
|
|
SipHash hash;
|
|
|
|
|
hash.update(value.data, value.size);
|
|
|
|
|
hash.get128(key.first, key.second);
|
|
|
|
|
|
|
|
|
|
data.set.insert(key);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-09-26 04:00:46 +00:00
|
|
|
|
/// Приближённо вычисляет количество различных значений.
|
2013-08-21 13:26:42 +00:00
|
|
|
|
template <typename T, typename Data>
|
2013-12-22 08:06:30 +00:00
|
|
|
|
class AggregateFunctionUniq : public IUnaryAggregateFunction<Data, AggregateFunctionUniq<T, Data> >
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
AggregateFunctionUniq() {}
|
|
|
|
|
|
2013-08-21 13:26:42 +00:00
|
|
|
|
String getName() const { return Data::getName(); }
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const
|
|
|
|
|
{
|
2012-12-14 20:24:26 +00:00
|
|
|
|
return new DataTypeUInt64;
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setArgument(const DataTypePtr & argument)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 08:06:40 +00:00
|
|
|
|
void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2014-02-02 09:08:06 +00:00
|
|
|
|
detail::OneAdder<T, Data>::addOne(this->data(place), column, row_num);
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2013-08-21 13:26:42 +00:00
|
|
|
|
this->data(place).set.merge(this->data(rhs).set);
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2013-08-21 13:26:42 +00:00
|
|
|
|
this->data(place).set.write(buf);
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2014-02-02 09:08:06 +00:00
|
|
|
|
this->data(place).set.readAndMerge(buf);
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 11:38:46 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2013-08-21 13:26:42 +00:00
|
|
|
|
static_cast<ColumnUInt64 &>(to).getData().push_back(this->data(place).set.size());
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
2013-08-21 13:26:42 +00:00
|
|
|
|
};
|
2013-06-25 14:16:16 +00:00
|
|
|
|
|
2013-04-26 18:57:08 +00:00
|
|
|
|
|
|
|
|
|
/** То же самое, но выводит состояние вычислений в строке в текстовом виде.
|
|
|
|
|
* Используется, если какой-то внешней программе (сейчас это ███████████)
|
|
|
|
|
* надо получить это состояние и потом использовать по-своему.
|
|
|
|
|
*/
|
2013-08-21 13:26:42 +00:00
|
|
|
|
template <typename T, typename Data>
|
|
|
|
|
class AggregateFunctionUniqState : public AggregateFunctionUniq<T, Data>
|
2013-04-26 18:57:08 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2013-08-21 13:26:42 +00:00
|
|
|
|
String getName() const { return Data::getName() + "State"; }
|
2013-04-26 18:57:08 +00:00
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const
|
|
|
|
|
{
|
|
|
|
|
return new DataTypeString;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 11:38:46 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
2013-04-26 18:57:08 +00:00
|
|
|
|
{
|
2013-06-30 11:38:46 +00:00
|
|
|
|
String res;
|
2013-06-30 11:40:30 +00:00
|
|
|
|
{
|
|
|
|
|
WriteBufferFromString wb(res);
|
|
|
|
|
this->data(place).set.writeText(wb);
|
|
|
|
|
}
|
2013-06-30 11:38:46 +00:00
|
|
|
|
|
|
|
|
|
static_cast<ColumnString &>(to).insertDataWithTerminatingZero(res.data(), res.size() + 1);
|
2013-04-26 18:57:08 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-13 20:30:47 +00:00
|
|
|
|
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|