2013-09-11 00:55:31 +00:00
|
|
|
|
#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 : public IAggregateFunction
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
AggregateFunctionPtr nested_func_owner;
|
|
|
|
|
IAggregateFunction * nested_func;
|
2013-10-28 16:13:19 +00:00
|
|
|
|
int num_agruments;
|
2013-09-11 00:55:31 +00:00
|
|
|
|
public:
|
|
|
|
|
AggregateFunctionIf(AggregateFunctionPtr nested_) : nested_func_owner(nested_), nested_func(nested_func_owner.get()) {}
|
|
|
|
|
|
|
|
|
|
String getName() const
|
|
|
|
|
{
|
|
|
|
|
return nested_func->getName() + "If";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const
|
|
|
|
|
{
|
|
|
|
|
return nested_func->getReturnType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setArguments(const DataTypes & arguments)
|
|
|
|
|
{
|
2013-10-28 16:13:19 +00:00
|
|
|
|
num_agruments = arguments.size();
|
|
|
|
|
|
|
|
|
|
if (!dynamic_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.",
|
2013-09-11 00:55:31 +00:00
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
|
|
DataTypes nested_arguments;
|
2013-10-28 16:13:19 +00:00
|
|
|
|
for (int i = 0; i < num_agruments - 1; i ++)
|
|
|
|
|
nested_arguments.push_back(arguments[i]);
|
2013-09-11 00:55:31 +00:00
|
|
|
|
nested_func->setArguments(nested_arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 18:16:26 +00:00
|
|
|
|
void setParameters(const Array & params)
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->setParameters(params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void create(AggregateDataPtr place) const
|
|
|
|
|
{
|
|
|
|
|
nested_func->create(place);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void destroy(AggregateDataPtr place) const
|
|
|
|
|
{
|
|
|
|
|
nested_func->destroy(place);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:54:12 +00:00
|
|
|
|
bool hasTrivialDestructor() const
|
|
|
|
|
{
|
|
|
|
|
return nested_func->hasTrivialDestructor();
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 00:55:31 +00:00
|
|
|
|
size_t sizeOfData() const
|
|
|
|
|
{
|
|
|
|
|
return nested_func->sizeOfData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t alignOfData() const
|
|
|
|
|
{
|
|
|
|
|
return nested_func->alignOfData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num) const
|
|
|
|
|
{
|
2013-10-28 16:13:19 +00:00
|
|
|
|
if (static_cast<const ColumnUInt8 &>(*columns[num_agruments - 1]).getData()[row_num])
|
2013-09-11 00:55:31 +00:00
|
|
|
|
nested_func->add(place, columns, row_num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
|
|
|
|
|
{
|
|
|
|
|
nested_func->merge(place, rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
|
|
|
|
|
{
|
|
|
|
|
nested_func->serialize(place, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
|
|
|
|
|
{
|
|
|
|
|
nested_func->deserializeMerge(place, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
|
|
|
|
{
|
|
|
|
|
nested_func->insertResultInto(place, to);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|