ClickHouse/dbms/include/DB/AggregateFunctions/AggregateFunctionCount.h

188 lines
5.1 KiB
C++
Raw Normal View History

2011-09-19 03:40:05 +00:00
#pragma once
2016-12-29 23:17:05 +00:00
#include <DB/IO/VarInt.h>
2011-09-19 03:40:05 +00:00
2017-02-12 11:40:47 +00:00
#include <array>
#include <DB/DataTypes/DataTypesNumber.h>
#include <DB/DataTypes/DataTypeNullable.h>
2011-09-19 03:40:05 +00:00
#include <DB/AggregateFunctions/INullaryAggregateFunction.h>
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
#include <DB/Columns/ColumnNullable.h>
2011-09-19 03:40:05 +00:00
namespace DB
{
struct AggregateFunctionCountData
2011-09-19 03:40:05 +00:00
{
UInt64 count = 0;
};
/// Simply count number of calls.
class AggregateFunctionCount final : public INullaryAggregateFunction<AggregateFunctionCountData, AggregateFunctionCount>
{
public:
2015-11-11 02:04:23 +00:00
String getName() const override { return "count"; }
2011-09-19 03:40:05 +00:00
void setArguments(const DataTypes & arguments) override
{
/// You may pass some arguments. All of them are ignored.
}
2015-11-11 02:04:23 +00:00
DataTypePtr getReturnType() const override
2011-09-19 03:40:05 +00:00
{
return std::make_shared<DataTypeUInt64>();
2011-09-19 03:40:05 +00:00
}
2015-11-15 06:23:44 +00:00
void addImpl(AggregateDataPtr place) const
{
++data(place).count;
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
2011-09-19 03:40:05 +00:00
{
data(place).count += data(rhs).count;
2011-09-19 03:40:05 +00:00
}
2015-11-11 02:04:23 +00:00
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
2011-09-19 03:40:05 +00:00
{
writeVarUInt(data(place).count, buf);
2011-09-19 03:40:05 +00:00
}
2016-09-22 23:26:08 +00:00
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
2011-09-19 03:40:05 +00:00
{
2016-03-12 04:01:03 +00:00
readVarUInt(data(place).count, buf);
2011-09-19 03:40:05 +00:00
}
2015-11-11 02:04:23 +00:00
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
2011-09-19 03:40:05 +00:00
{
static_cast<ColumnUInt64 &>(to).getData().push_back(data(place).count);
2011-09-19 03:40:05 +00:00
}
2012-07-15 23:13:08 +00:00
/// May be used for optimization.
void addDelta(AggregateDataPtr place, UInt64 x) const
2012-07-15 23:13:08 +00:00
{
data(place).count += x;
2012-07-15 23:13:08 +00:00
}
2011-09-19 03:40:05 +00:00
};
/// Simply count number of not-NULL values.
class AggregateFunctionCountNotNullUnary final : public IUnaryAggregateFunction<AggregateFunctionCountData, AggregateFunctionCountNotNullUnary>
{
public:
String getName() const override { return "count"; }
DataTypePtr getReturnType() const override
{
return std::make_shared<DataTypeUInt64>();
}
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena * arena) const
{
data(place).count += !static_cast<const ColumnNullable &>(column).isNullAt(row_num);
}
void setArgument(const DataTypePtr & argument)
{
if (!argument->isNullable() && !argument->isNull())
throw Exception("Not Nullable argument passed to aggregate function count", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) 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, Arena *) 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);
}
};
2017-02-10 09:07:27 +00:00
/// Count number of calls where all arguments are not NULL.
class AggregateFunctionCountNotNullVariadic final : public IAggregateFunctionHelper<AggregateFunctionCountData>
{
public:
String getName() const override { return "count"; }
DataTypePtr getReturnType() const override
{
return std::make_shared<DataTypeUInt64>();
}
void setArguments(const DataTypes & arguments) override
{
number_of_arguments = arguments.size();
if (number_of_arguments == 1)
throw Exception("Logical error: single argument is passed to AggregateFunctionCountNotNullVariadic", ErrorCodes::LOGICAL_ERROR);
if (number_of_arguments > MAX_ARGS)
throw Exception("Maximum number of arguments for aggregate function with Nullable types is " + toString(size_t(MAX_ARGS)),
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
for (size_t i = 0; i < number_of_arguments; ++i)
is_nullable[i] = arguments[i]->isNullable() || arguments[i]->isNull();
}
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
{
for (size_t i = 0; i < number_of_arguments; ++i)
if (is_nullable[i] && static_cast<const ColumnNullable &>(*columns[i]).isNullAt(row_num))
return;
++data(place).count;
}
static void addFree(const IAggregateFunction * that, AggregateDataPtr place,
const IColumn ** columns, size_t row_num, Arena * arena)
{
return static_cast<const AggregateFunctionCountNotNullVariadic &>(*that).add(place, columns, row_num, arena);
}
AddFunc getAddressOfAddFunction() const override
{
return &addFree;
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) 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, Arena *) 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);
}
private:
enum { MAX_ARGS = 8 };
size_t number_of_arguments = 0;
std::array<char, MAX_ARGS> is_nullable; /// Plain array is better than std::vector due to one indirection less.
};
2011-09-19 03:40:05 +00:00
}