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.
|
|
|
|
|
*/
|
2014-06-04 01:00:09 +00:00
|
|
|
|
class AggregateFunctionIf final : public IAggregateFunction
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
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()) {}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-09-11 00:55:31 +00:00
|
|
|
|
String getName() const
|
|
|
|
|
{
|
|
|
|
|
return nested_func->getName() + "If";
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-09-11 00:55:31 +00:00
|
|
|
|
DataTypePtr getReturnType() const
|
|
|
|
|
{
|
|
|
|
|
return nested_func->getReturnType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setArguments(const DataTypes & arguments)
|
|
|
|
|
{
|
2013-10-28 16:13:19 +00:00
|
|
|
|
num_agruments = arguments.size();
|
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
if (!typeid_cast<const DataTypeUInt8 *>(&*arguments[num_agruments - 1]))
|
2013-10-28 16:13:19 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-23 01:09:18 +00:00
|
|
|
|
void destroy(AggregateDataPtr place) const noexcept
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|