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

115 lines
3.3 KiB
C++
Raw Normal View History

#pragma once
#include <DB/DataTypes/DataTypesNumberFixed.h>
#include <DB/AggregateFunctions/IAggregateFunction.h>
namespace DB
{
/** Не агрегатная функция, а адаптер агрегатных функций,
* который любую агрегатную функцию agg(x) делает агрегатной функцией вида aggIf(x, cond).
* Адаптированная агрегатная функция принимает два аргумента - значение и условие,
* и вычисляет вложенную агрегатную функцию для значений при выполненном условии.
* Например, avgIf(x, cond) вычисляет среднее x при условии cond.
*/
class AggregateFunctionIf final : public IAggregateFunction
{
private:
AggregateFunctionPtr nested_func_owner;
IAggregateFunction * nested_func;
int num_agruments;
public:
AggregateFunctionIf(AggregateFunctionPtr nested_) : nested_func_owner(nested_), nested_func(nested_func_owner.get()) {}
2015-11-11 02:04:23 +00:00
String getName() const override
{
return nested_func->getName() + "If";
}
2015-11-11 02:04:23 +00:00
DataTypePtr getReturnType() const override
{
return nested_func->getReturnType();
}
2015-11-11 02:04:23 +00:00
void setArguments(const DataTypes & arguments) override
{
num_agruments = arguments.size();
if (!typeid_cast<const DataTypeUInt8 *>(&*arguments[num_agruments - 1]))
throw Exception("Illegal type " + arguments[num_agruments - 1]->getName() + " of second argument for aggregate function " + getName() + ". Must be UInt8.",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
DataTypes nested_arguments;
for (int i = 0; i < num_agruments - 1; i ++)
nested_arguments.push_back(arguments[i]);
nested_func->setArguments(nested_arguments);
}
2015-11-11 02:04:23 +00:00
void setParameters(const Array & params) override
{
nested_func->setParameters(params);
}
2015-11-11 02:04:23 +00:00
void create(AggregateDataPtr place) const override
{
nested_func->create(place);
}
2015-11-11 02:04:23 +00:00
void destroy(AggregateDataPtr place) const noexcept override
{
nested_func->destroy(place);
}
2015-11-11 02:04:23 +00:00
bool hasTrivialDestructor() const override
{
return nested_func->hasTrivialDestructor();
}
2015-11-11 02:04:23 +00:00
size_t sizeOfData() const override
{
return nested_func->sizeOfData();
}
2015-11-11 02:04:23 +00:00
size_t alignOfData() const override
{
return nested_func->alignOfData();
}
2015-11-11 02:04:23 +00:00
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num) const override
{
if (static_cast<const ColumnUInt8 &>(*columns[num_agruments - 1]).getData()[row_num])
nested_func->add(place, columns, row_num);
}
2015-11-11 02:04:23 +00:00
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
{
nested_func->merge(place, rhs);
}
2015-11-11 02:04:23 +00:00
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
{
nested_func->serialize(place, buf);
}
2016-03-12 04:01:03 +00:00
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
{
2016-03-12 04:01:03 +00:00
nested_func->deserialize(place, buf);
}
2015-11-11 02:04:23 +00:00
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
{
nested_func->insertResultInto(place, to);
}
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num)
{
return static_cast<const AggregateFunctionIf &>(*that).add(place, columns, row_num);
}
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
};
}