2011-09-19 03:40:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
2012-12-14 20:24:26 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2011-09-19 03:40:05 +00:00
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/INullaryAggregateFunction.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
struct AggregateFunctionCountData
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
|
|
|
UInt64 count;
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
AggregateFunctionCountData() : count(0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Просто считает, сколько раз её вызвали
|
|
|
|
class AggregateFunctionCount : public INullaryAggregateFunction<AggregateFunctionCountData>
|
|
|
|
{
|
|
|
|
public:
|
2011-09-19 03:40:05 +00:00
|
|
|
String getName() const { return "count"; }
|
2011-09-25 05:07:47 +00:00
|
|
|
String getTypeID() const { return "count"; }
|
2011-09-19 03:40:05 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnType() const
|
|
|
|
{
|
2012-12-14 20:24:26 +00:00
|
|
|
return new DataTypeUInt64;
|
2011-09-19 03:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void addZero(AggregateDataPtr place)
|
|
|
|
{
|
|
|
|
++data(place).count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
data(place).count += data(rhs).count;
|
2011-09-19 03:40:05 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
writeVarUInt(data(place).count, buf);
|
2011-09-19 03:40:05 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
|
|
|
UInt64 tmp;
|
|
|
|
readVarUInt(tmp, buf);
|
2013-02-08 19:34:44 +00:00
|
|
|
data(place).count += tmp;
|
2011-09-19 03:40:05 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
Field getResult(ConstAggregateDataPtr place) const
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
return data(place).count;
|
2011-09-19 03:40:05 +00:00
|
|
|
}
|
2012-07-15 23:13:08 +00:00
|
|
|
|
|
|
|
/// Для оптимизации
|
2013-02-08 19:34:44 +00:00
|
|
|
void addDelta(AggregateDataPtr place, UInt64 x)
|
2012-07-15 23:13:08 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
data(place).count += x;
|
2012-07-15 23:13:08 +00:00
|
|
|
}
|
2011-09-19 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|