#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace DB { /// uniq struct AggregateFunctionUniqUniquesHashSetData { using Set = UniquesHashSet>; Set set; static String getName() { return "uniq"; } }; /// For a function that takes multiple arguments. Such a function pre-hashes them in advance, so TrivialHash is used here. struct AggregateFunctionUniqUniquesHashSetDataForVariadic { using Set = UniquesHashSet; Set set; static String getName() { return "uniq"; } }; /// uniqHLL12 template struct AggregateFunctionUniqHLL12Data { using Set = HyperLogLogWithSmallSetOptimization; Set set; static String getName() { return "uniqHLL12"; } }; template <> struct AggregateFunctionUniqHLL12Data { using Set = HyperLogLogWithSmallSetOptimization; Set set; static String getName() { return "uniqHLL12"; } }; template <> struct AggregateFunctionUniqHLL12Data { using Set = HyperLogLogWithSmallSetOptimization; Set set; static String getName() { return "uniqHLL12"; } }; struct AggregateFunctionUniqHLL12DataForVariadic { using Set = HyperLogLogWithSmallSetOptimization; Set set; static String getName() { return "uniqHLL12"; } }; /// uniqExact template struct AggregateFunctionUniqExactData { using Key = T; /// When creating, the hash table must be small. using Set = HashSet< Key, HashCRC32, HashTableGrower<4>, HashTableAllocatorWithStackMemory>; Set set; static String getName() { return "uniqExact"; } }; /// For rows, we put the SipHash values (128 bits) into the hash table. template <> struct AggregateFunctionUniqExactData { using Key = UInt128; /// When creating, the hash table must be small. using Set = HashSet< Key, UInt128TrivialHash, HashTableGrower<3>, HashTableAllocatorWithStackMemory>; Set set; static String getName() { return "uniqExact"; } }; template struct BaseUniqCombinedData { using Key = UInt32; using Set = CombinedCardinalityEstimator< Key, HashSet>, 16, 14, 17, TrivialHash, UInt32, HyperLogLogBiasEstimator, mode >; Set set; }; template struct BaseUniqCombinedData { using Key = UInt64; using Set = CombinedCardinalityEstimator< Key, HashSet>, 16, 14, 17, TrivialHash, UInt64, HyperLogLogBiasEstimator, mode >; Set set; }; /// Aggregate functions uniqCombinedRaw, uniqCombinedLinearCounting, and uniqCombinedBiasCorrected /// are intended for development of new versions of the uniqCombined function. /// Users should only use uniqCombined. template struct AggregateFunctionUniqCombinedRawData : public BaseUniqCombinedData { static String getName() { return "uniqCombinedRaw"; } }; template struct AggregateFunctionUniqCombinedLinearCountingData : public BaseUniqCombinedData { static String getName() { return "uniqCombinedLinearCounting"; } }; template struct AggregateFunctionUniqCombinedBiasCorrectedData : public BaseUniqCombinedData { static String getName() { return "uniqCombinedBiasCorrected"; } }; template struct AggregateFunctionUniqCombinedData : public BaseUniqCombinedData { static String getName() { return "uniqCombined"; } }; namespace detail { /** Hash function for uniq. */ template struct AggregateFunctionUniqTraits { static UInt64 hash(T x) { return x; } }; template <> struct AggregateFunctionUniqTraits { static UInt64 hash(UInt128 x) { SipHash hash; hash.update(reinterpret_cast(&x), sizeof(x)); return hash.get64(); } }; 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; } }; /** Hash function for uniqCombined. */ template struct AggregateFunctionUniqCombinedTraits { static UInt32 hash(T x) { return static_cast(intHash64(x)); } }; template <> struct AggregateFunctionUniqCombinedTraits { static UInt32 hash(UInt128 x) { SipHash hash; hash.update(reinterpret_cast(&x), sizeof(x)); return static_cast(hash.get64()); } }; template <> struct AggregateFunctionUniqCombinedTraits { static UInt32 hash(Float32 x) { UInt64 res = 0; memcpy(reinterpret_cast(&res), reinterpret_cast(&x), sizeof(x)); return static_cast(intHash64(res)); } }; template <> struct AggregateFunctionUniqCombinedTraits { static UInt32 hash(Float64 x) { UInt64 res = 0; memcpy(reinterpret_cast(&res), reinterpret_cast(&x), sizeof(x)); return static_cast(intHash64(res)); } }; /** The structure for the delegation work to add one element to the `uniq` aggregate functions. * Used for partial specialization to add strings. */ template struct OneAdder; template struct OneAdder::value || std::is_same>::value>::type> { template static void ALWAYS_INLINE addImpl(Data & data, const IColumn & column, size_t row_num, typename std::enable_if::value>::type * = nullptr) { const auto & value = static_cast &>(column).getData()[row_num]; data.set.insert(AggregateFunctionUniqTraits::hash(value)); } template static void ALWAYS_INLINE addImpl(Data & data, const IColumn & column, size_t row_num, typename std::enable_if::value>::type * = nullptr) { StringRef value = column.getDataAt(row_num); data.set.insert(CityHash_v1_0_2::CityHash64(value.data, value.size)); } }; template struct OneAdder>::value || std::is_same>::value || std::is_same>::value || std::is_same>::value>::type> { template static void ALWAYS_INLINE addImpl(Data & data, const IColumn & column, size_t row_num, typename std::enable_if::value>::type * = nullptr) { const auto & value = static_cast &>(column).getData()[row_num]; data.set.insert(AggregateFunctionUniqCombinedTraits::hash(value)); } template static void ALWAYS_INLINE addImpl(Data & data, const IColumn & column, size_t row_num, typename std::enable_if::value>::type * = nullptr) { StringRef value = column.getDataAt(row_num); data.set.insert(CityHash_v1_0_2::CityHash64(value.data, value.size)); } }; template struct OneAdder>::value>::type> { template static void ALWAYS_INLINE addImpl(Data & data, const IColumn & column, size_t row_num, typename std::enable_if::value>::type * = nullptr) { data.set.insert(static_cast &>(column).getData()[row_num]); } template static void ALWAYS_INLINE addImpl(Data & data, const IColumn & column, size_t row_num, typename std::enable_if::value>::type * = nullptr) { StringRef value = column.getDataAt(row_num); UInt128 key; SipHash hash; hash.update(value.data, value.size); hash.get128(key.low, key.high); data.set.insert(key); } }; } /// Calculates the number of different values approximately or exactly. template class AggregateFunctionUniq final : public IUnaryAggregateFunction> { public: String getName() const override { return Data::getName(); } DataTypePtr getReturnType() const override { return std::make_shared(); } void setArgument(const DataTypePtr & argument) { } void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena *) const { detail::OneAdder::addImpl(this->data(place), column, row_num); } void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override { this->data(place).set.merge(this->data(rhs).set); } void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override { this->data(place).set.write(buf); } void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override { this->data(place).set.read(buf); } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override { static_cast(to).getData().push_back(this->data(place).set.size()); } const char * getHeaderFilePath() const override { return __FILE__; } }; /** For multiple arguments. To compute, hashes them. * You can pass multiple arguments as is; You can also pass one argument - a tuple. * But (for the possibility of effective implementation), you can not pass several arguments, among which there are tuples. */ template class AggregateFunctionUniqVariadic final : public IAggregateFunctionHelper { private: static constexpr bool is_exact = std::is_same>::value; size_t num_args = 0; public: String getName() const override { return Data::getName(); } DataTypePtr getReturnType() const override { return std::make_shared(); } void setArguments(const DataTypes & arguments) override { if (argument_is_tuple) num_args = typeid_cast(*arguments[0]).getElements().size(); else num_args = arguments.size(); } void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override { this->data(place).set.insert(UniqVariadicHash::apply(num_args, columns, row_num)); } void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override { this->data(place).set.merge(this->data(rhs).set); } void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override { this->data(place).set.write(buf); } void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override { this->data(place).set.read(buf); } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override { static_cast(to).getData().push_back(this->data(place).set.size()); } static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) { static_cast(*that).add(place, columns, row_num, arena); } IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; } const char * getHeaderFilePath() const override { return __FILE__; } }; }