ClickHouse/dbms/include/DB/AggregateFunctions/AggregateFunctionCount.h
2016-05-28 10:48:40 +03:00

67 lines
1.4 KiB
C++

#pragma once
#include <DB/IO/WriteHelpers.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/DataTypes/DataTypesNumberFixed.h>
#include <DB/AggregateFunctions/INullaryAggregateFunction.h>
namespace DB
{
struct AggregateFunctionCountData
{
UInt64 count;
AggregateFunctionCountData() : count(0) {}
};
/// Просто считает, сколько раз её вызвали
class AggregateFunctionCount final : public INullaryAggregateFunction<AggregateFunctionCountData, AggregateFunctionCount>
{
public:
String getName() const override { return "count"; }
DataTypePtr getReturnType() const override
{
return std::make_shared<DataTypeUInt64>();
}
void addImpl(AggregateDataPtr place) const
{
++data(place).count;
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
{
data(place).count += data(rhs).count;
}
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
{
writeVarUInt(data(place).count, buf);
}
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
{
readVarUInt(data(place).count, buf);
}
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
{
static_cast<ColumnUInt64 &>(to).getData().push_back(data(place).count);
}
/// Для оптимизации
void addDelta(AggregateDataPtr place, UInt64 x) const
{
data(place).count += x;
}
};
}