#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { template struct AggregateFunctionUniqTraits { static UInt64 hash(T x) { return x; } }; template <> struct AggregateFunctionUniqTraits { static UInt64 hash(Float32 x) { UInt64 res = 0; memcpy(reinterpret_cast(&res), reinterpret_cast(&x), sizeof(x)); return res; } }; template <> struct AggregateFunctionUniqTraits { static UInt64 hash(Float64 x) { UInt64 res = 0; memcpy(reinterpret_cast(&res), reinterpret_cast(&x), sizeof(x)); return res; } }; struct AggregateFunctionUniqUniquesHashSetData { typedef UniquesHashSet> Set; Set set; static String getName() { return "uniq"; } }; struct AggregateFunctionUniqHLL12Data { typedef HLL12 Set; Set set; static String getName() { return "uniqHLL12"; } }; template struct AggregateFunctionUniqExactData { typedef T Key; /// При создании, хэш-таблица должна быть небольшой. typedef HashSet< Key, DefaultHash, HashTableGrower<4>, HashTableAllocatorWithStackMemory > Set; Set set; static String getName() { return "uniqExact"; } }; /// Для строк будем класть в хэш-таблицу значения SipHash-а (128 бит). template <> struct AggregateFunctionUniqExactData { typedef UInt128 Key; /// При создании, хэш-таблица должна быть небольшой. typedef HashSet< Key, UInt128TrivialHash, HashTableGrower<3>, HashTableAllocatorWithStackMemory > Set; Set set; static String getName() { return "uniqExact"; } }; namespace detail { /** Структура для делегации работы по добавлению одного элемента в агрегатные функции uniq. * Используется для частичной специализации для добавления строк. */ template struct OneAdder { static void addOne(Data & data, const IColumn & column, size_t row_num) { data.set.insert(AggregateFunctionUniqTraits::hash(static_cast &>(column).getData()[row_num])); } }; template struct OneAdder { 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 struct OneAdder > { static void addOne(AggregateFunctionUniqExactData & data, const IColumn & column, size_t row_num) { data.set.insert(static_cast &>(column).getData()[row_num]); } }; template<> struct OneAdder > { static void addOne(AggregateFunctionUniqExactData & 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); } }; } /// Приближённо вычисляет количество различных значений. template class AggregateFunctionUniq final : public IUnaryAggregateFunction > { public: String getName() const { return Data::getName(); } DataTypePtr getReturnType() const { return new DataTypeUInt64; } void setArgument(const DataTypePtr & argument) { } void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const { detail::OneAdder::addOne(this->data(place), column, row_num); } void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const { this->data(place).set.merge(this->data(rhs).set); } void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const { this->data(place).set.write(buf); } void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const { this->data(place).set.readAndMerge(buf); } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const { static_cast(to).getData().push_back(this->data(place).set.size()); } }; }