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

137 lines
3.7 KiB
C++
Raw Normal View History

#pragma once
#include <DB/Columns/ColumnArray.h>
#include <DB/DataTypes/DataTypeArray.h>
#include <DB/AggregateFunctions/IAggregateFunction.h>
#include <DB/IO/WriteHelpers.h>
namespace DB
{
2017-03-09 00:56:38 +00:00
/** Not an aggregate function, but an adapter of aggregate functions,
* which any aggregate function `agg(x)` makes an aggregate function of the form `aggArray(x)`.
* The adapted aggregate function calculates nested aggregate function for each element of the array.
*/
class AggregateFunctionArray final : public IAggregateFunction
{
private:
AggregateFunctionPtr nested_func_owner;
IAggregateFunction * nested_func;
2016-07-10 17:19:35 +00:00
size_t num_agruments;
public:
AggregateFunctionArray(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() + "Array";
}
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();
2016-01-13 00:47:12 +00:00
if (0 == num_agruments)
throw Exception("Array aggregate functions requires at least one argument", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
DataTypes nested_arguments;
2016-07-10 17:19:35 +00:00
for (size_t i = 0; i < num_agruments; ++i)
{
if (const DataTypeArray * array = typeid_cast<const DataTypeArray *>(&*arguments[i]))
nested_arguments.push_back(array->getNestedType());
else
throw Exception("Illegal type " + arguments[i]->getName() + " of argument #" + toString(i + 1) + " for aggregate function " + getName() + ". Must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
}
2016-01-13 00:47:12 +00:00
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();
}
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
{
const IColumn * nested[num_agruments];
2016-07-10 17:19:35 +00:00
for (size_t i = 0; i < num_agruments; ++i)
nested[i] = &static_cast<const ColumnArray &>(*columns[i]).getData();
const ColumnArray & first_array_column = static_cast<const ColumnArray &>(*columns[0]);
const IColumn::Offsets_t & offsets = first_array_column.getOffsets();
size_t begin = row_num == 0 ? 0 : offsets[row_num - 1];
size_t end = offsets[row_num];
for (size_t i = begin; i < end; ++i)
nested_func->add(place, nested, i, arena);
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
{
nested_func->merge(place, rhs, arena);
}
2015-11-11 02:04:23 +00:00
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
{
nested_func->serialize(place, buf);
}
2016-09-22 23:26:08 +00:00
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
{
2016-09-22 23:26:08 +00:00
nested_func->deserialize(place, buf, arena);
}
2015-11-11 02:04:23 +00:00
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
{
nested_func->insertResultInto(place, to);
}
bool allocatesMemoryInArena() const override
{
return nested_func->allocatesMemoryInArena();
}
static void addFree(const IAggregateFunction * that, AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena)
{
static_cast<const AggregateFunctionArray &>(*that).add(place, columns, row_num, arena);
}
IAggregateFunction::AddFunc getAddressOfAddFunction() const override final { return &addFree; }
};
}