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

88 lines
2.1 KiB
C
Raw Normal View History

2012-08-26 12:18:50 +00:00
#pragma once
#include <DB/IO/WriteHelpers.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/DataTypes/DataTypeArray.h>
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
#define AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE 0xFFFFFF
namespace DB
{
struct AggregateFunctionGroupArrayData
{
Array value; /// TODO Добавить MemoryTracker
};
/// Складывает все значения в массив. Реализовано неэффективно.
class AggregateFunctionGroupArray final : public IUnaryAggregateFunction<AggregateFunctionGroupArrayData, AggregateFunctionGroupArray>
2012-08-26 12:18:50 +00:00
{
private:
DataTypePtr type;
2012-08-26 12:18:50 +00:00
public:
2015-11-11 02:04:23 +00:00
String getName() const override { return "groupArray"; }
2012-08-26 12:18:50 +00:00
2015-11-11 02:04:23 +00:00
DataTypePtr getReturnType() const override
2012-08-26 12:18:50 +00:00
{
return new DataTypeArray(type);
}
2015-11-11 02:04:23 +00:00
void setArgument(const DataTypePtr & argument) override
2012-08-26 12:18:50 +00:00
{
type = argument;
}
void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const
2012-08-26 12:18:50 +00:00
{
data(place).value.push_back(Array::value_type());
column.get(row_num, data(place).value.back());
2012-08-26 12:18:50 +00:00
}
2015-11-11 02:04:23 +00:00
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
2012-08-26 12:18:50 +00:00
{
data(place).value.insert(data(place).value.end(), data(rhs).value.begin(), data(rhs).value.end());
2012-08-26 12:18:50 +00:00
}
2015-11-11 02:04:23 +00:00
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
2012-08-26 12:18:50 +00:00
{
const Array & value = data(place).value;
2012-08-26 12:18:50 +00:00
size_t size = value.size();
writeVarUInt(size, buf);
for (size_t i = 0; i < size; ++i)
type->serializeBinary(value[i], buf);
}
2015-11-11 02:04:23 +00:00
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const override
2012-08-26 12:18:50 +00:00
{
size_t size = 0;
readVarUInt(size, buf);
if (size > AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE)
throw Exception("Too large array size", ErrorCodes::TOO_LARGE_ARRAY_SIZE);
Array & value = data(place).value;
size_t old_size = value.size();
value.resize(old_size + size);
2012-08-26 12:18:50 +00:00
for (size_t i = 0; i < size; ++i)
type->deserializeBinary(value[old_size + i], buf);
2012-08-26 12:18:50 +00:00
}
2015-11-11 02:04:23 +00:00
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
2012-08-26 12:18:50 +00:00
{
to.insert(data(place).value);
2012-08-26 12:18:50 +00:00
}
};
#undef AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE
}