2014-07-24 20:20:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-10-12 07:05:54 +00:00
|
|
|
#include <DB/Core/FieldVisitors.h>
|
2014-07-25 01:32:59 +00:00
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
2015-10-29 04:04:43 +00:00
|
|
|
#include <DB/AggregateFunctions/UniqVariadicHash.h>
|
2017-03-12 10:13:45 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumber.h>
|
2015-10-29 03:41:09 +00:00
|
|
|
#include <DB/DataTypes/DataTypeTuple.h>
|
2017-03-12 10:13:45 +00:00
|
|
|
#include <DB/Columns/ColumnsNumber.h>
|
2014-07-24 20:20:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2017-03-09 04:18:41 +00:00
|
|
|
/** Counts the number of unique values up to no more than specified in the parameter.
|
2014-07-24 20:20:36 +00:00
|
|
|
*
|
2017-03-09 00:56:38 +00:00
|
|
|
* Example: uniqUpTo(3)(UserID)
|
|
|
|
* - will count the number of unique visitors, return 1, 2, 3 or 4 if visitors > = 4.
|
2014-07-24 20:20:36 +00:00
|
|
|
*
|
2017-03-09 00:56:38 +00:00
|
|
|
* For strings, a non-cryptographic hash function is used, due to which the calculation may be a bit inaccurate.
|
2014-07-24 20:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename T>
|
2014-08-01 05:19:17 +00:00
|
|
|
struct __attribute__((__packed__)) AggregateFunctionUniqUpToData
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2017-03-09 04:18:41 +00:00
|
|
|
/** If count == threshold + 1 - this means that it is "overflowed" (values greater than threshold).
|
2017-03-09 00:56:38 +00:00
|
|
|
* In this case (for example, after calling the merge function), the `data` array does not necessarily contain the initialized values
|
|
|
|
* - example: combine a state in which there are few values, with another state that has overflowed;
|
2017-03-09 04:18:41 +00:00
|
|
|
* then set count to `threshold + 1`, and values from another state are not copied.
|
2017-03-09 00:56:38 +00:00
|
|
|
*/
|
2014-08-01 05:19:17 +00:00
|
|
|
UInt8 count = 0;
|
2014-08-01 19:14:21 +00:00
|
|
|
|
|
|
|
T data[0];
|
2014-07-24 20:20:36 +00:00
|
|
|
|
2014-07-25 01:32:59 +00:00
|
|
|
|
2014-07-24 20:20:36 +00:00
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/// threshold - for how many elements there is room in a `data`.
|
2014-08-01 05:19:17 +00:00
|
|
|
void insert(T x, UInt8 threshold)
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2017-03-09 04:26:17 +00:00
|
|
|
/// The state is already full - nothing needs to be done.
|
2014-07-24 20:20:36 +00:00
|
|
|
if (count > threshold)
|
|
|
|
return;
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/// Linear search for the matching element.
|
2014-08-01 19:14:21 +00:00
|
|
|
for (size_t i = 0; i < count; ++i)
|
2014-07-24 20:20:36 +00:00
|
|
|
if (data[i] == x)
|
|
|
|
return;
|
|
|
|
|
2017-03-09 04:26:17 +00:00
|
|
|
/// Did not find the matching element. If there is room for one more element, insert it.
|
2014-07-24 20:20:36 +00:00
|
|
|
if (count < threshold)
|
|
|
|
data[count] = x;
|
|
|
|
|
2017-03-09 04:26:17 +00:00
|
|
|
/// After increasing count, the state may be overflowed.
|
2014-07-24 20:20:36 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
2014-08-01 05:19:17 +00:00
|
|
|
void merge(const AggregateFunctionUniqUpToData<T> & rhs, UInt8 threshold)
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
|
|
|
if (count > threshold)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (rhs.count > threshold)
|
|
|
|
{
|
2017-03-09 04:26:17 +00:00
|
|
|
/// If `rhs` is overflowed, then set `count` too also overflowed for the current state.
|
2014-07-24 20:20:36 +00:00
|
|
|
count = rhs.count;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-01 19:14:21 +00:00
|
|
|
for (size_t i = 0; i < rhs.count; ++i)
|
2014-07-24 20:20:36 +00:00
|
|
|
insert(rhs.data[i], threshold);
|
|
|
|
}
|
|
|
|
|
2014-08-01 05:19:17 +00:00
|
|
|
void write(WriteBuffer & wb, UInt8 threshold) const
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2014-08-01 19:14:21 +00:00
|
|
|
writeBinary(count, wb);
|
|
|
|
|
2017-03-09 04:26:17 +00:00
|
|
|
/// Write values only if the state is not overflowed. Otherwise, they are not needed, and only the fact that the state is overflowed is important.
|
2014-08-01 19:14:21 +00:00
|
|
|
if (count <= threshold)
|
2014-10-02 19:20:09 +00:00
|
|
|
wb.write(reinterpret_cast<const char *>(&data[0]), count * sizeof(data[0]));
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
void read(ReadBuffer & rb, UInt8 threshold)
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
readBinary(count, rb);
|
2014-07-24 20:20:36 +00:00
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
if (count <= threshold)
|
|
|
|
rb.read(reinterpret_cast<char *>(&data[0]), count * sizeof(data[0]));
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|
2014-07-25 01:32:59 +00:00
|
|
|
|
|
|
|
|
2015-11-15 06:23:44 +00:00
|
|
|
void addImpl(const IColumn & column, size_t row_num, UInt8 threshold)
|
2014-07-25 01:32:59 +00:00
|
|
|
{
|
|
|
|
insert(static_cast<const ColumnVector<T> &>(column).getData()[row_num], threshold);
|
|
|
|
}
|
2014-07-24 20:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/// For strings, their hashes are remembered.
|
2014-07-24 20:20:36 +00:00
|
|
|
template <>
|
2014-07-25 01:32:59 +00:00
|
|
|
struct AggregateFunctionUniqUpToData<String> : AggregateFunctionUniqUpToData<UInt64>
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2015-11-15 06:23:44 +00:00
|
|
|
void addImpl(const IColumn & column, size_t row_num, UInt8 threshold)
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2017-03-09 00:56:38 +00:00
|
|
|
/// Keep in mind that calculations are approximate.
|
2014-07-25 01:32:59 +00:00
|
|
|
StringRef value = column.getDataAt(row_num);
|
|
|
|
insert(CityHash64(value.data, value.size), threshold);
|
|
|
|
}
|
|
|
|
};
|
2014-07-24 20:20:36 +00:00
|
|
|
|
|
|
|
|
2014-08-01 05:19:17 +00:00
|
|
|
constexpr UInt8 uniq_upto_max_threshold = 100;
|
2014-07-24 20:20:36 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class AggregateFunctionUniqUpTo final : public IUnaryAggregateFunction<AggregateFunctionUniqUpToData<T>, AggregateFunctionUniqUpTo<T> >
|
|
|
|
{
|
|
|
|
private:
|
2017-03-09 04:26:17 +00:00
|
|
|
UInt8 threshold = 5; /// Default value if the parameter is not specified.
|
2014-07-24 20:20:36 +00:00
|
|
|
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
size_t sizeOfData() const override
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
|
|
|
return sizeof(AggregateFunctionUniqUpToData<T>) + sizeof(T) * threshold;
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
String getName() const override { return "uniqUpTo"; }
|
2014-07-24 20:20:36 +00:00
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
DataTypePtr getReturnType() const override
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2016-05-28 07:48:40 +00:00
|
|
|
return std::make_shared<DataTypeUInt64>();
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 08:31:08 +00:00
|
|
|
void setArgument(const DataTypePtr & argument)
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void setParameters(const Array & params) override
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
|
|
|
if (params.size() != 1)
|
|
|
|
throw Exception("Aggregate function " + getName() + " requires exactly one parameter.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
UInt64 threshold_param = applyVisitor(FieldVisitorConvertToNumber<UInt64>(), params[0]);
|
2014-07-24 20:20:36 +00:00
|
|
|
|
2014-08-01 19:31:38 +00:00
|
|
|
if (threshold_param > uniq_upto_max_threshold)
|
2014-07-24 20:20:36 +00:00
|
|
|
throw Exception("Too large parameter for aggregate function " + getName() + ". Maximum: " + toString(uniq_upto_max_threshold),
|
|
|
|
ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
2014-08-01 19:31:38 +00:00
|
|
|
|
|
|
|
threshold = threshold_param;
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena *) const
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2015-11-15 06:23:44 +00:00
|
|
|
this->data(place).addImpl(column, row_num, threshold);
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:17 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
|
|
|
this->data(place).merge(this->data(rhs), threshold);
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2014-07-31 20:26:22 +00:00
|
|
|
this->data(place).write(buf, threshold);
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 23:26:08 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
this->data(place).read(buf, threshold);
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2014-07-24 20:20:36 +00:00
|
|
|
{
|
|
|
|
static_cast<ColumnUInt64 &>(to).getData().push_back(this->data(place).size());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/** 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.
|
2015-10-29 03:41:09 +00:00
|
|
|
*/
|
|
|
|
template <bool argument_is_tuple>
|
|
|
|
class AggregateFunctionUniqUpToVariadic final : public IAggregateFunctionHelper<AggregateFunctionUniqUpToData<UInt64>>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
size_t num_args = 0;
|
2017-03-09 04:26:17 +00:00
|
|
|
UInt8 threshold = 5; /// Default value if the parameter is not specified.
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
size_t sizeOfData() const override
|
2015-10-30 02:34:24 +00:00
|
|
|
{
|
|
|
|
return sizeof(AggregateFunctionUniqUpToData<UInt64>) + sizeof(UInt64) * threshold;
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
String getName() const override { return "uniqUpTo"; }
|
2015-10-29 03:41:09 +00:00
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
DataTypePtr getReturnType() const override
|
2015-10-29 03:41:09 +00:00
|
|
|
{
|
2016-05-28 07:48:40 +00:00
|
|
|
return std::make_shared<DataTypeUInt64>();
|
2015-10-29 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void setArguments(const DataTypes & arguments) override
|
2015-10-29 03:41:09 +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 setParameters(const Array & params) override
|
2015-10-29 03:41:09 +00:00
|
|
|
{
|
|
|
|
if (params.size() != 1)
|
|
|
|
throw Exception("Aggregate function " + getName() + " requires exactly one parameter.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2017-01-06 17:41:19 +00:00
|
|
|
UInt64 threshold_param = applyVisitor(FieldVisitorConvertToNumber<UInt64>(), params[0]);
|
2015-10-29 03:41:09 +00:00
|
|
|
|
|
|
|
if (threshold_param > uniq_upto_max_threshold)
|
|
|
|
throw Exception("Too large parameter for aggregate function " + getName() + ". Maximum: " + toString(uniq_upto_max_threshold),
|
|
|
|
ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
threshold = threshold_param;
|
|
|
|
}
|
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
|
2015-10-29 03:41:09 +00:00
|
|
|
{
|
2015-10-29 04:02:22 +00:00
|
|
|
this->data(place).insert(UniqVariadicHash<false, argument_is_tuple>::apply(num_args, columns, row_num), threshold);
|
2015-10-29 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:17 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2015-10-29 03:41:09 +00:00
|
|
|
{
|
|
|
|
this->data(place).merge(this->data(rhs), threshold);
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2015-10-29 03:41:09 +00:00
|
|
|
{
|
|
|
|
this->data(place).write(buf, threshold);
|
|
|
|
}
|
|
|
|
|
2016-09-22 23:26:08 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
2015-10-29 03:41:09 +00:00
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
this->data(place).read(buf, threshold);
|
2015-10-29 03:41:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2015-10-29 03:41:09 +00:00
|
|
|
{
|
|
|
|
static_cast<ColumnUInt64 &>(to).getData().push_back(this->data(place).size());
|
|
|
|
}
|
2015-11-21 19:46:27 +00:00
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *arena)
|
2015-11-21 19:46:27 +00:00
|
|
|
{
|
2016-09-19 22:30:40 +00:00
|
|
|
static_cast<const AggregateFunctionUniqUpToVariadic &>(*that).add(place, columns, row_num, arena);
|
2015-11-21 19:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
|
2015-10-29 03:41:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-24 20:20:36 +00:00
|
|
|
}
|