2014-03-27 12:48:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2014-03-27 12:48:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-18 21:44:18 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SIZES_OF_ARRAYS_DOESNT_MATCH;
|
2017-12-20 07:36:30 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2017-12-18 21:44:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 12:48:09 +00:00
|
|
|
|
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.
|
2014-03-27 12:48:09 +00:00
|
|
|
*/
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionArray final : public IAggregateFunctionHelper<AggregateFunctionArray>
|
2014-03-27 12:48:09 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionPtr nested_func;
|
2017-12-23 23:06:59 +00:00
|
|
|
size_t num_arguments;
|
2014-03-27 12:48:09 +00:00
|
|
|
|
|
|
|
public:
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionArray(AggregateFunctionPtr nested_, const DataTypes & arguments)
|
2019-02-11 19:26:32 +00:00
|
|
|
: IAggregateFunctionHelper<AggregateFunctionArray>(arguments, {})
|
|
|
|
, nested_func(nested_), num_arguments(arguments.size())
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
|
|
|
for (const auto & type : arguments)
|
2018-09-10 17:09:07 +00:00
|
|
|
if (!isArray(type))
|
2017-12-20 07:36:30 +00:00
|
|
|
throw Exception("All arguments for aggregate function " + getName() + " must be arrays", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return nested_func->getName() + "Array";
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return nested_func->getReturnType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void create(AggregateDataPtr place) const override
|
|
|
|
{
|
|
|
|
nested_func->create(place);
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy(AggregateDataPtr place) const noexcept override
|
|
|
|
{
|
|
|
|
nested_func->destroy(place);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasTrivialDestructor() const override
|
|
|
|
{
|
|
|
|
return nested_func->hasTrivialDestructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sizeOfData() const override
|
|
|
|
{
|
|
|
|
return nested_func->sizeOfData();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t alignOfData() const override
|
|
|
|
{
|
|
|
|
return nested_func->alignOfData();
|
|
|
|
}
|
|
|
|
|
2018-09-21 09:37:09 +00:00
|
|
|
bool isState() const override
|
|
|
|
{
|
|
|
|
return nested_func->isState();
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
|
|
|
{
|
2017-12-23 23:06:59 +00:00
|
|
|
const IColumn * nested[num_arguments];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-23 23:06:59 +00:00
|
|
|
for (size_t i = 0; i < num_arguments; ++i)
|
2017-04-01 07:20:54 +00:00
|
|
|
nested[i] = &static_cast<const ColumnArray &>(*columns[i]).getData();
|
|
|
|
|
|
|
|
const ColumnArray & first_array_column = static_cast<const ColumnArray &>(*columns[0]);
|
2017-12-15 21:32:25 +00:00
|
|
|
const IColumn::Offsets & offsets = first_array_column.getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-12-24 14:26:38 +00:00
|
|
|
size_t begin = offsets[row_num - 1];
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t end = offsets[row_num];
|
|
|
|
|
2017-12-18 21:44:18 +00:00
|
|
|
/// Sanity check. NOTE We can implement specialization for a case with single argument, if the check will hurt performance.
|
2017-12-23 23:06:59 +00:00
|
|
|
for (size_t i = 1; i < num_arguments; ++i)
|
2017-12-18 21:44:18 +00:00
|
|
|
{
|
|
|
|
const ColumnArray & ith_column = static_cast<const ColumnArray &>(*columns[i]);
|
|
|
|
const IColumn::Offsets & ith_offsets = ith_column.getOffsets();
|
|
|
|
|
|
|
|
if (ith_offsets[row_num] != end || (row_num != 0 && ith_offsets[row_num - 1] != begin))
|
|
|
|
throw Exception("Arrays passed to " + getName() + " aggregate function have different sizes", ErrorCodes::SIZES_OF_ARRAYS_DOESNT_MATCH);
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
nested_func->serialize(place, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
|
|
|
|
{
|
|
|
|
nested_func->deserialize(place, buf, arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
nested_func->insertResultInto(place, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allocatesMemoryInArena() const override
|
|
|
|
{
|
|
|
|
return nested_func->allocatesMemoryInArena();
|
|
|
|
}
|
|
|
|
|
2017-09-17 20:22:39 +00:00
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2014-03-27 12:48:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|