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
|
|
|
|
|
2015-11-15 06:19:34 +00:00
|
|
|
|
#include <DB/AggregateFunctions/UniquesHashSet.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>
|
2015-10-29 02:13:37 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeTuple.h>
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
2014-02-02 09:08:06 +00:00
|
|
|
|
#include <DB/Interpreters/AggregationCommon.h>
|
2014-04-28 01:48:24 +00:00
|
|
|
|
#include <DB/Common/HashTable/HashSet.h>
|
2015-02-22 07:23:37 +00:00
|
|
|
|
#include <DB/Common/HyperLogLogWithSmallSetOptimization.h>
|
2015-07-20 14:22:08 +00:00
|
|
|
|
#include <DB/Common/CombinedCardinalityEstimator.h>
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2013-06-30 11:38:46 +00:00
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-10-29 02:13:37 +00:00
|
|
|
|
#include <DB/Columns/ColumnTuple.h>
|
2013-06-30 11:38:46 +00:00
|
|
|
|
|
2011-09-26 04:00:46 +00:00
|
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
2015-08-31 13:52:17 +00:00
|
|
|
|
#include <DB/AggregateFunctions/UniqCombinedBiasData.h>
|
2015-10-29 04:04:43 +00:00
|
|
|
|
#include <DB/AggregateFunctions/UniqVariadicHash.h>
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
/// uniq
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
2013-08-21 13:26:42 +00:00
|
|
|
|
struct AggregateFunctionUniqUniquesHashSetData
|
2013-02-08 19:34:44 +00:00
|
|
|
|
{
|
2015-10-29 04:02:22 +00:00
|
|
|
|
typedef UniquesHashSet<DefaultHash<UInt64>> Set;
|
2013-08-21 13:26:42 +00:00
|
|
|
|
Set set;
|
2015-02-22 07:23:37 +00:00
|
|
|
|
|
2013-08-21 13:26:42 +00:00
|
|
|
|
static String getName() { return "uniq"; }
|
2013-02-08 19:34:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-10-29 04:13:18 +00:00
|
|
|
|
/// Для функции, принимающей несколько аргументов. Такая функция сама заранее их хэширует, поэтому здесь используется TrivialHash.
|
2015-10-29 04:02:22 +00:00
|
|
|
|
struct AggregateFunctionUniqUniquesHashSetDataForVariadic
|
|
|
|
|
{
|
|
|
|
|
typedef UniquesHashSet<TrivialHash> Set;
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniq"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
/// uniqHLL12
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2015-02-22 07:23:37 +00:00
|
|
|
|
template <typename T>
|
2013-08-21 13:26:42 +00:00
|
|
|
|
struct AggregateFunctionUniqHLL12Data
|
|
|
|
|
{
|
2015-02-22 07:23:37 +00:00
|
|
|
|
typedef HyperLogLogWithSmallSetOptimization<T, 16, 12> Set;
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniqHLL12"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
struct AggregateFunctionUniqHLL12Data<String>
|
|
|
|
|
{
|
|
|
|
|
typedef HyperLogLogWithSmallSetOptimization<UInt64, 16, 12> Set;
|
2013-08-21 13:26:42 +00:00
|
|
|
|
Set set;
|
2015-02-22 07:23:37 +00:00
|
|
|
|
|
2013-08-21 13:26:42 +00:00
|
|
|
|
static String getName() { return "uniqHLL12"; }
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-29 04:13:18 +00:00
|
|
|
|
struct AggregateFunctionUniqHLL12DataForVariadic
|
|
|
|
|
{
|
|
|
|
|
typedef HyperLogLogWithSmallSetOptimization<UInt64, 16, 12, TrivialHash> Set;
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniqHLL12"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
/// uniqExact
|
2013-02-08 19:34:44 +00:00
|
|
|
|
|
2014-02-02 09:08:06 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct AggregateFunctionUniqExactData
|
|
|
|
|
{
|
|
|
|
|
typedef T Key;
|
|
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
|
/// При создании, хэш-таблица должна быть небольшой.
|
2014-02-02 09:08:06 +00:00
|
|
|
|
typedef HashSet<
|
|
|
|
|
Key,
|
2015-10-29 02:19:56 +00:00
|
|
|
|
HashCRC32<Key>,
|
2014-05-03 16:03:49 +00:00
|
|
|
|
HashTableGrower<4>,
|
|
|
|
|
HashTableAllocatorWithStackMemory<sizeof(Key) * (1 << 4)>
|
2014-02-02 09:08:06 +00:00
|
|
|
|
> Set;
|
|
|
|
|
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniqExact"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Для строк будем класть в хэш-таблицу значения SipHash-а (128 бит).
|
|
|
|
|
template <>
|
|
|
|
|
struct AggregateFunctionUniqExactData<String>
|
|
|
|
|
{
|
|
|
|
|
typedef UInt128 Key;
|
|
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
|
/// При создании, хэш-таблица должна быть небольшой.
|
2014-02-02 09:08:06 +00:00
|
|
|
|
typedef HashSet<
|
|
|
|
|
Key,
|
|
|
|
|
UInt128TrivialHash,
|
2014-05-03 16:03:49 +00:00
|
|
|
|
HashTableGrower<3>,
|
|
|
|
|
HashTableAllocatorWithStackMemory<sizeof(Key) * (1 << 3)>
|
2014-02-02 09:08:06 +00:00
|
|
|
|
> Set;
|
|
|
|
|
|
|
|
|
|
Set set;
|
|
|
|
|
|
|
|
|
|
static String getName() { return "uniqExact"; }
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T, HyperLogLogMode mode>
|
|
|
|
|
struct BaseUniqCombinedData
|
2015-07-20 14:22:08 +00:00
|
|
|
|
{
|
2015-10-08 14:23:23 +00:00
|
|
|
|
using Key = UInt32;
|
2015-08-31 13:52:17 +00:00
|
|
|
|
using Set = CombinedCardinalityEstimator<
|
|
|
|
|
Key,
|
2015-10-08 14:23:23 +00:00
|
|
|
|
HashSet<Key, TrivialHash, HashTableGrower<> >,
|
2015-08-31 13:52:17 +00:00
|
|
|
|
16,
|
|
|
|
|
14,
|
|
|
|
|
17,
|
2015-10-08 14:23:23 +00:00
|
|
|
|
TrivialHash,
|
|
|
|
|
UInt32,
|
2015-08-31 13:52:17 +00:00
|
|
|
|
HyperLogLogBiasEstimator<UniqCombinedBiasData>,
|
|
|
|
|
mode
|
|
|
|
|
>;
|
2015-07-20 14:22:08 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
Set set;
|
2015-07-20 14:22:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <HyperLogLogMode mode>
|
|
|
|
|
struct BaseUniqCombinedData<String, mode>
|
2015-07-20 14:22:08 +00:00
|
|
|
|
{
|
|
|
|
|
using Key = UInt64;
|
2015-08-31 13:52:17 +00:00
|
|
|
|
using Set = CombinedCardinalityEstimator<
|
|
|
|
|
Key,
|
|
|
|
|
HashSet<Key, TrivialHash, HashTableGrower<> >,
|
|
|
|
|
16,
|
|
|
|
|
14,
|
|
|
|
|
17,
|
|
|
|
|
TrivialHash,
|
|
|
|
|
UInt64,
|
|
|
|
|
HyperLogLogBiasEstimator<UniqCombinedBiasData>,
|
|
|
|
|
mode
|
|
|
|
|
>;
|
2015-07-20 14:22:08 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
Set set;
|
2015-07-20 14:22:08 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
/// Агрегатные функции uniqCombinedRaw, uniqCombinedLinearCounting, и uniqCombinedBiasCorrected
|
|
|
|
|
/// предназначены для разработки новых версий функции uniqCombined.
|
|
|
|
|
/// Пользователи должны использовать только uniqCombined.
|
2015-07-29 17:06:39 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct AggregateFunctionUniqCombinedRawData
|
|
|
|
|
: public BaseUniqCombinedData<T, HyperLogLogMode::Raw>
|
2015-08-04 13:37:56 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
static String getName() { return "uniqCombinedRaw"; }
|
2015-08-04 13:37:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct AggregateFunctionUniqCombinedLinearCountingData
|
|
|
|
|
: public BaseUniqCombinedData<T, HyperLogLogMode::LinearCounting>
|
2015-08-04 13:37:56 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
static String getName() { return "uniqCombinedLinearCounting"; }
|
2015-08-04 13:37:56 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct AggregateFunctionUniqCombinedBiasCorrectedData
|
|
|
|
|
: public BaseUniqCombinedData<T, HyperLogLogMode::BiasCorrected>
|
2015-08-04 13:37:56 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
static String getName() { return "uniqCombinedBiasCorrected"; }
|
2015-08-12 16:26:53 +00:00
|
|
|
|
};
|
2015-07-29 17:06:39 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
struct AggregateFunctionUniqCombinedData
|
|
|
|
|
: public BaseUniqCombinedData<T, HyperLogLogMode::FullFeatured>
|
2015-08-12 16:26:53 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
static String getName() { return "uniqCombined"; }
|
2015-08-04 13:37:56 +00:00
|
|
|
|
};
|
2015-07-29 17:06:39 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
namespace detail
|
2015-08-04 13:37:56 +00:00
|
|
|
|
{
|
2015-07-29 17:06:39 +00:00
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
/** Хэш-функция для uniq.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> struct AggregateFunctionUniqTraits
|
|
|
|
|
{
|
|
|
|
|
static UInt64 hash(T x) { return x; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <> struct AggregateFunctionUniqTraits<Float32>
|
|
|
|
|
{
|
|
|
|
|
static UInt64 hash(Float32 x)
|
2015-07-29 17:06:39 +00:00
|
|
|
|
{
|
2015-08-04 13:37:56 +00:00
|
|
|
|
UInt64 res = 0;
|
|
|
|
|
memcpy(reinterpret_cast<char *>(&res), reinterpret_cast<char *>(&x), sizeof(x));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-07-29 17:06:39 +00:00
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
template <> struct AggregateFunctionUniqTraits<Float64>
|
|
|
|
|
{
|
|
|
|
|
static UInt64 hash(Float64 x)
|
2015-08-04 12:33:08 +00:00
|
|
|
|
{
|
2015-08-04 13:37:56 +00:00
|
|
|
|
UInt64 res = 0;
|
|
|
|
|
memcpy(reinterpret_cast<char *>(&res), reinterpret_cast<char *>(&x), sizeof(x));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-08-04 12:33:08 +00:00
|
|
|
|
|
2015-10-08 14:23:23 +00:00
|
|
|
|
/** Хэш-функция для uniqCombined.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T> struct AggregateFunctionUniqCombinedTraits
|
|
|
|
|
{
|
|
|
|
|
static UInt32 hash(T x) { return static_cast<UInt32>(intHash64(x)); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <> struct AggregateFunctionUniqCombinedTraits<Float32>
|
|
|
|
|
{
|
|
|
|
|
static UInt32 hash(Float32 x)
|
|
|
|
|
{
|
|
|
|
|
UInt64 res = 0;
|
|
|
|
|
memcpy(reinterpret_cast<char *>(&res), reinterpret_cast<char *>(&x), sizeof(x));
|
|
|
|
|
return static_cast<UInt32>(intHash64(res));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <> struct AggregateFunctionUniqCombinedTraits<Float64>
|
|
|
|
|
{
|
|
|
|
|
static UInt32 hash(Float64 x)
|
|
|
|
|
{
|
|
|
|
|
UInt64 res = 0;
|
|
|
|
|
memcpy(reinterpret_cast<char *>(&res), reinterpret_cast<char *>(&x), sizeof(x));
|
|
|
|
|
return static_cast<UInt32>(intHash64(res));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
/** Структура для делегации работы по добавлению одного элемента в агрегатные функции uniq.
|
2015-08-31 13:52:17 +00:00
|
|
|
|
* Используется для частичной специализации для добавления строк.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T, typename Data, typename Enable = void>
|
|
|
|
|
struct OneAdder;
|
|
|
|
|
|
|
|
|
|
template <typename T, typename Data>
|
|
|
|
|
struct OneAdder<T, Data, typename std::enable_if<
|
|
|
|
|
std::is_same<Data, AggregateFunctionUniqUniquesHashSetData>::value ||
|
2015-10-08 14:23:23 +00:00
|
|
|
|
std::is_same<Data, AggregateFunctionUniqHLL12Data<T> >::value>::type>
|
|
|
|
|
{
|
|
|
|
|
template <typename T2 = T>
|
2015-11-15 06:23:44 +00:00
|
|
|
|
static void addImpl(Data & data, const IColumn & column, size_t row_num,
|
2015-10-08 14:23:23 +00:00
|
|
|
|
typename std::enable_if<!std::is_same<T2, String>::value>::type * = nullptr)
|
|
|
|
|
{
|
|
|
|
|
const auto & value = static_cast<const ColumnVector<T2> &>(column).getData()[row_num];
|
|
|
|
|
data.set.insert(AggregateFunctionUniqTraits<T2>::hash(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T2 = T>
|
2015-11-15 06:23:44 +00:00
|
|
|
|
static void addImpl(Data & data, const IColumn & column, size_t row_num,
|
2015-10-08 14:23:23 +00:00
|
|
|
|
typename std::enable_if<std::is_same<T2, String>::value>::type * = nullptr)
|
|
|
|
|
{
|
|
|
|
|
StringRef value = column.getDataAt(row_num);
|
|
|
|
|
data.set.insert(CityHash64(value.data, value.size));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename T, typename Data>
|
|
|
|
|
struct OneAdder<T, Data, typename std::enable_if<
|
2015-08-31 13:52:17 +00:00
|
|
|
|
std::is_same<Data, AggregateFunctionUniqCombinedRawData<T> >::value ||
|
|
|
|
|
std::is_same<Data, AggregateFunctionUniqCombinedLinearCountingData<T> >::value ||
|
|
|
|
|
std::is_same<Data, AggregateFunctionUniqCombinedBiasCorrectedData<T> >::value ||
|
|
|
|
|
std::is_same<Data, AggregateFunctionUniqCombinedData<T> >::value>::type>
|
2015-08-04 13:37:56 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T2 = T>
|
2015-11-15 06:23:44 +00:00
|
|
|
|
static void addImpl(Data & data, const IColumn & column, size_t row_num,
|
2015-08-31 13:52:17 +00:00
|
|
|
|
typename std::enable_if<!std::is_same<T2, String>::value>::type * = nullptr)
|
2014-02-02 09:08:06 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
const auto & value = static_cast<const ColumnVector<T2> &>(column).getData()[row_num];
|
2015-10-08 14:23:23 +00:00
|
|
|
|
data.set.insert(AggregateFunctionUniqCombinedTraits<T2>::hash(value));
|
2015-08-04 13:37:56 +00:00
|
|
|
|
}
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T2 = T>
|
2015-11-15 06:23:44 +00:00
|
|
|
|
static void addImpl(Data & data, const IColumn & column, size_t row_num,
|
2015-08-31 13:52:17 +00:00
|
|
|
|
typename std::enable_if<std::is_same<T2, String>::value>::type * = nullptr)
|
2014-02-02 09:08:06 +00:00
|
|
|
|
{
|
2015-08-04 13:37:56 +00:00
|
|
|
|
StringRef value = column.getDataAt(row_num);
|
|
|
|
|
data.set.insert(CityHash64(value.data, value.size));
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T, typename Data>
|
|
|
|
|
struct OneAdder<T, Data, typename std::enable_if<
|
2015-08-31 15:32:01 +00:00
|
|
|
|
std::is_same<Data, AggregateFunctionUniqExactData<T> >::value>::type>
|
2015-08-04 13:37:56 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T2 = T>
|
2015-11-15 06:23:44 +00:00
|
|
|
|
static void addImpl(Data & data, const IColumn & column, size_t row_num,
|
2015-08-31 13:52:17 +00:00
|
|
|
|
typename std::enable_if<!std::is_same<T2, String>::value>::type * = nullptr)
|
2014-02-02 09:08:06 +00:00
|
|
|
|
{
|
2015-08-31 13:52:17 +00:00
|
|
|
|
data.set.insert(static_cast<const ColumnVector<T2> &>(column).getData()[row_num]);
|
2015-08-04 13:37:56 +00:00
|
|
|
|
}
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2015-08-31 13:52:17 +00:00
|
|
|
|
template <typename T2 = T>
|
2015-11-15 06:23:44 +00:00
|
|
|
|
static void addImpl(Data & data, const IColumn & column, size_t row_num,
|
2015-08-31 13:52:17 +00:00
|
|
|
|
typename std::enable_if<std::is_same<T2, String>::value>::type * = nullptr)
|
2014-02-02 09:08:06 +00:00
|
|
|
|
{
|
2015-08-04 13:37:56 +00:00
|
|
|
|
StringRef value = column.getDataAt(row_num);
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
UInt128 key;
|
|
|
|
|
SipHash hash;
|
|
|
|
|
hash.update(value.data, value.size);
|
|
|
|
|
hash.get128(key.first, key.second);
|
2014-02-02 09:08:06 +00:00
|
|
|
|
|
2015-08-04 13:37:56 +00:00
|
|
|
|
data.set.insert(key);
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-07-20 14:22:08 +00:00
|
|
|
|
|
2014-02-02 09:08:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-10-29 02:13:37 +00:00
|
|
|
|
/// Приближённо или точно вычисляет количество различных значений.
|
2013-08-21 13:26:42 +00:00
|
|
|
|
template <typename T, typename Data>
|
2014-06-04 01:00:09 +00:00
|
|
|
|
class AggregateFunctionUniq final : public IUnaryAggregateFunction<Data, AggregateFunctionUniq<T, Data> >
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
|
String getName() const override { return Data::getName(); }
|
2011-09-26 04:00:46 +00:00
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
DataTypePtr getReturnType() const override
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2012-12-14 20:24:26 +00:00
|
|
|
|
return new DataTypeUInt64;
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 08:31:08 +00:00
|
|
|
|
void setArgument(const DataTypePtr & argument)
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 06:23:44 +00:00
|
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2015-11-15 06:23:44 +00:00
|
|
|
|
detail::OneAdder<T, Data>::addImpl(this->data(place), column, row_num);
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
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
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
|
2011-09-26 04:00:46 +00:00
|
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
|
this->data(place).set.read(buf);
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
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
|
|
|
|
|
2015-10-29 03:41:09 +00:00
|
|
|
|
/** Для нескольких аргументов. Для вычисления, хэширует их.
|
2015-10-29 02:13:37 +00:00
|
|
|
|
* Можно передать несколько аргументов как есть; также можно передать один аргумент - кортеж.
|
2015-10-29 02:41:13 +00:00
|
|
|
|
* Но (для возможности эффективной реализации), нельзя передать несколько аргументов, среди которых есть кортежи.
|
2015-10-29 02:13:37 +00:00
|
|
|
|
*/
|
|
|
|
|
template <typename Data, bool argument_is_tuple>
|
|
|
|
|
class AggregateFunctionUniqVariadic final : public IAggregateFunctionHelper<Data>
|
|
|
|
|
{
|
|
|
|
|
private:
|
2015-10-29 04:02:22 +00:00
|
|
|
|
static constexpr bool is_exact = std::is_same<Data, AggregateFunctionUniqExactData<String>>::value;
|
|
|
|
|
|
2015-10-29 02:13:37 +00:00
|
|
|
|
size_t num_args = 0;
|
|
|
|
|
|
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
|
String getName() const override { return Data::getName(); }
|
2015-10-29 02:13:37 +00:00
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
DataTypePtr getReturnType() const override
|
2015-10-29 02:13:37 +00:00
|
|
|
|
{
|
|
|
|
|
return new DataTypeUInt64;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setArguments(const DataTypes & arguments) override
|
2015-10-29 02:13:37 +00:00
|
|
|
|
{
|
|
|
|
|
if (argument_is_tuple)
|
|
|
|
|
num_args = typeid_cast<const DataTypeTuple &>(*arguments[0]).getElements().size();
|
|
|
|
|
else
|
|
|
|
|
num_args = arguments.size();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num) const override
|
2015-10-29 02:13:37 +00:00
|
|
|
|
{
|
2015-10-29 04:02:22 +00:00
|
|
|
|
this->data(place).set.insert(UniqVariadicHash<is_exact, argument_is_tuple>::apply(num_args, columns, row_num));
|
2015-10-29 02:13:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2015-10-29 02:13:37 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).set.merge(this->data(rhs).set);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2015-10-29 02:13:37 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).set.write(buf);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
|
2015-10-29 02:13:37 +00:00
|
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
|
this->data(place).set.read(buf);
|
2015-10-29 02:13:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2015-10-29 02:13:37 +00:00
|
|
|
|
{
|
|
|
|
|
static_cast<ColumnUInt64 &>(to).getData().push_back(this->data(place).set.size());
|
|
|
|
|
}
|
2015-11-21 19:46:27 +00:00
|
|
|
|
|
|
|
|
|
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
return static_cast<const AggregateFunctionUniqVariadic &>(*that).add(place, columns, row_num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
|
2015-10-29 02:13:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2011-09-26 04:00:46 +00:00
|
|
|
|
}
|