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>
|
2017-03-12 10:13:45 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumber.h>
|
2017-02-10 09:02:10 +00:00
|
|
|
#include <DB/DataTypes/DataTypeNullable.h>
|
2011-09-19 03:40:05 +00:00
|
|
|
#include <DB/AggregateFunctions/INullaryAggregateFunction.h>
|
2017-02-10 09:02:10 +00:00
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
|
|
|
#include <DB/Columns/ColumnNullable.h>
|
2011-09-19 03:40:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
struct AggregateFunctionCountData
|
2011-09-19 03:40:05 +00:00
|
|
|
{
|
2017-02-10 09:02:10 +00:00
|
|
|
UInt64 count = 0;
|
2013-02-08 19:34:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-02-10 09:02:10 +00:00
|
|
|
/// Simply count number of calls.
|
2014-06-04 01:00:09 +00:00
|
|
|
class AggregateFunctionCount final : public INullaryAggregateFunction<AggregateFunctionCountData, AggregateFunctionCount>
|
2013-02-08 19:34:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
String getName() const override { return "count"; }
|
2011-09-19 03:40:05 +00:00
|
|
|
|
2017-02-10 09:02:10 +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
|
|
|
{
|
2016-05-28 07:48:40 +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
|
2013-02-08 19:34:44 +00:00
|
|
|
{
|
|
|
|
++data(place).count;
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:33:17 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
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
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-06-30 11:38:46 +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
|
|
|
|
2017-02-10 09:02:10 +00:00
|
|
|
/// May be used for optimization.
|
2013-02-08 23:41:05 +00:00
|
|
|
void addDelta(AggregateDataPtr place, UInt64 x) const
|
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
|
|
|
};
|
|
|
|
|
2017-02-10 09:02:10 +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.
|
2017-02-10 09:02:10 +00:00
|
|
|
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)
|
2017-03-13 21:50:15 +00:00
|
|
|
throw Exception("Maximum number of arguments for aggregate function with Nullable types is " + toString(size_t(MAX_ARGS)),
|
2017-02-10 09:02:10 +00:00
|
|
|
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
|
|
|
}
|