#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { /// uniq struct AggregateFunctionUniqUniquesHashSetData { typedef UniquesHashSet> Set; Set set; static String getName() { return "uniq"; } }; /// uniqHLL12 template struct AggregateFunctionUniqHLL12Data { typedef HyperLogLogWithSmallSetOptimization Set; Set set; static String getName() { return "uniqHLL12"; } }; template <> struct AggregateFunctionUniqHLL12Data { typedef HyperLogLogWithSmallSetOptimization Set; Set set; static String getName() { return "uniqHLL12"; } }; /// uniqExact 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"; } }; template struct AggregateFunctionUniqCombinedData { using Key = UInt32; using Set = CombinedCardinalityEstimator >, 16, 14, 17, TrivialHash>; Set set; static String getName() { return "uniqCombined"; } }; template <> struct AggregateFunctionUniqCombinedData { using Key = UInt64; using Set = CombinedCardinalityEstimator >, 16, 14, 17, TrivialHash>; Set set; static String getName() { return "uniqCombined"; } }; namespace detail { /** Хэш-функция для uniqCombined. */ template struct CombinedCardinalityTraits { static UInt32 hash(T key) { return key; } }; template struct CombinedCardinalityTraits::value>::type> { using U = typename std::make_unsigned::type; static UInt32 hash(T key) { return intHash32<0>(static_cast(key)); }; }; template struct CombinedCardinalityTraits::value>::type> { static UInt32 hash(T key) { return intHash32<0>(key); }; }; template struct CombinedCardinalityTraits::value>::type> { static UInt32 hash(T key) { UInt64 res = 0; memcpy(reinterpret_cast(&res), reinterpret_cast(&key), sizeof(key)); return intHash32<0>(res); } }; template struct CombinedCardinalityTraits::value>::type> { static UInt32 hash(T key) { UInt32 res = 0; memcpy(reinterpret_cast(&res), reinterpret_cast(&key), sizeof(key)); return res; } }; /** Хэш-функция для uniq. */ 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; } }; /** Структура для делегации работы по добавлению одного элемента в агрегатные функции 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 struct OneAdder > { static void addOne(AggregateFunctionUniqCombinedData & data, const IColumn & column, size_t row_num) { const auto & value = static_cast &>(column).getData()[row_num]; data.set.insert(CombinedCardinalityTraits::hash(value)); } }; template<> struct OneAdder > { static void addOne(AggregateFunctionUniqCombinedData & data, const IColumn & column, size_t row_num) { StringRef value = column.getDataAt(row_num); data.set.insert(CityHash64(value.data, value.size)); } }; } /// Приближённо вычисляет количество различных значений. 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()); } }; }