#pragma once #include #include #include #include #include #include #include namespace DB { template struct AggregateFunctionUniqTraits; template <> struct AggregateFunctionUniqTraits { static UInt64 hash(UInt64 x) { return x; } }; template <> struct AggregateFunctionUniqTraits { static UInt64 hash(Int64 x) { return x; } }; template <> struct AggregateFunctionUniqTraits { static UInt64 hash(Float64 x) { UInt64 res = 0; memcpy(reinterpret_cast(&res), reinterpret_cast(&x), sizeof(x)); return res; } }; template <> struct AggregateFunctionUniqTraits { /// Имейте ввиду, что вычисление приближённое. static UInt64 hash(const String & x) { return CityHash64(x.data(), x.size()); } }; /// Приближённо вычисляет количество различных значений. template class AggregateFunctionUniq : public IUnaryAggregateFunction { private: UniquesHashSet set; public: AggregateFunctionUniq() {} String getName() const { return "uniq"; } String getTypeID() const { return "uniq_" + TypeName::get(); } AggregateFunctionPtr cloneEmpty() const { return new AggregateFunctionUniq; } DataTypePtr getReturnType() const { return new DataTypeVarUInt; } void setArgument(const DataTypePtr & argument) { } void addOne(const Field & value) { set.insert(AggregateFunctionUniqTraits::hash(boost::get(value))); } void merge(const IAggregateFunction & rhs) { set.merge(static_cast &>(rhs).set); } void serialize(WriteBuffer & buf) const { set.write(buf); } void deserializeMerge(ReadBuffer & buf) { UniquesHashSet tmp_set; tmp_set.read(buf); set.merge(tmp_set); } Field getResult() const { return set.size(); } }; }