2011-09-19 03:40:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/VarInt.h>
|
2019-02-10 17:40:52 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2011-09-19 03:40:05 +00:00
|
|
|
|
2017-02-12 11:40:47 +00:00
|
|
|
#include <array>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.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-04-01 07:20:54 +00:00
|
|
|
UInt64 count = 0;
|
2013-02-08 19:34:44 +00:00
|
|
|
};
|
|
|
|
|
2017-07-27 21:27:10 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
2017-12-20 07:36:30 +00:00
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2017-07-27 21:27:10 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
2017-02-10 09:02:10 +00:00
|
|
|
/// Simply count number of calls.
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionCount final : public IAggregateFunctionDataHelper<AggregateFunctionCountData, AggregateFunctionCount>
|
2013-02-08 19:34:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-02-13 11:50:41 +00:00
|
|
|
AggregateFunctionCount(const DataTypes & argument_types_) : IAggregateFunctionDataHelper(argument_types_, {}) {}
|
2019-02-11 19:26:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "count"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn **, size_t, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
++data(place).count;
|
|
|
|
}
|
|
|
|
|
2017-12-01 21:13:25 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt64 &>(to).getData().push_back(data(place).count);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 20:22:39 +00:00
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2019-10-30 19:15:55 +00:00
|
|
|
|
|
|
|
/// Reset the state to specified value. This function is not the part of common interface.
|
|
|
|
void set(AggregateDataPtr place, UInt64 new_count)
|
|
|
|
{
|
|
|
|
data(place).count = new_count;
|
|
|
|
}
|
2011-09-19 03:40:05 +00:00
|
|
|
};
|
|
|
|
|
2017-02-10 09:02:10 +00:00
|
|
|
|
|
|
|
/// Simply count number of not-NULL values.
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionCountNotNullUnary final : public IAggregateFunctionDataHelper<AggregateFunctionCountData, AggregateFunctionCountNotNullUnary>
|
2017-02-10 09:02:10 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-02-11 19:26:32 +00:00
|
|
|
AggregateFunctionCountNotNullUnary(const DataTypePtr & argument, const Array & params)
|
|
|
|
: IAggregateFunctionDataHelper<AggregateFunctionCountData, AggregateFunctionCountNotNullUnary>({argument}, params)
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
|
|
|
if (!argument->isNullable())
|
|
|
|
throw Exception("Logical error: not Nullable data type passed to AggregateFunctionCountNotNullUnary", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "count"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
}
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
data(place).count += !assert_cast<const ColumnNullable &>(*columns[0]).isNullAt(row_num);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:13:25 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
assert_cast<ColumnUInt64 &>(to).getData().push_back(data(place).count);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-09-17 20:22:39 +00:00
|
|
|
|
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2017-02-10 09:02:10 +00:00
|
|
|
};
|
|
|
|
|
2011-09-19 03:40:05 +00:00
|
|
|
}
|