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;
|
2016-07-10 17:19:35 +00:00
|
|
|
|
size_t 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
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
String getName() const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->getName() + "If";
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
DataTypePtr getReturnType() const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->getReturnType();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setArguments(const DataTypes & arguments) override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
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;
|
2016-07-10 17:19:35 +00:00
|
|
|
|
for (size_t i = 0; i < num_agruments - 1; i ++)
|
2013-10-28 16:13:19 +00:00
|
|
|
|
nested_arguments.push_back(arguments[i]);
|
2013-09-11 00:55:31 +00:00
|
|
|
|
nested_func->setArguments(nested_arguments);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setParameters(const Array & params) override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->setParameters(params);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void create(AggregateDataPtr place) const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->create(place);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void destroy(AggregateDataPtr place) const noexcept override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->destroy(place);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
bool hasTrivialDestructor() const override
|
2013-11-03 23:54:12 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->hasTrivialDestructor();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
size_t sizeOfData() const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->sizeOfData();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
size_t alignOfData() const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
return nested_func->alignOfData();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
2013-10-28 16:13:19 +00:00
|
|
|
|
if (static_cast<const ColumnUInt8 &>(*columns[num_agruments - 1]).getData()[row_num])
|
2016-09-19 22:30:40 +00:00
|
|
|
|
nested_func->add(place, columns, row_num, nullptr);
|
2013-09-11 00:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->merge(place, rhs);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->serialize(place, buf);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
|
nested_func->deserialize(place, buf);
|
2013-09-11 00:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2013-09-11 00:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
nested_func->insertResultInto(place, to);
|
|
|
|
|
}
|
2015-11-21 19:46:27 +00:00
|
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
|
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena)
|
2015-11-21 19:46:27 +00:00
|
|
|
|
{
|
2016-09-19 22:30:40 +00:00
|
|
|
|
static_cast<const AggregateFunctionIf &>(*that).add(place, columns, row_num, arena);
|
2015-11-21 19:46:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
|
2013-09-11 00:55:31 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|