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

87 lines
1.8 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;
};
2012-08-26 12:18:50 +00:00
/// Складывает все значения в массив.
class AggregateFunctionGroupArray : public IUnaryAggregateFunction<AggregateFunctionGroupArrayData>
2012-08-26 12:18:50 +00:00
{
private:
DataTypePtr type;
public:
String getName() const { return "groupArray"; }
String getTypeID() const { return "groupArray"; }
DataTypePtr getReturnType() const
{
return new DataTypeArray(type);
}
void setArgument(const DataTypePtr & argument)
{
type = argument;
}
void addOne(AggregateDataPtr place, const Field & value_) const
2012-08-26 12:18:50 +00:00
{
data(place).value.push_back(value_);
2012-08-26 12:18:50 +00:00
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
2012-08-26 12:18:50 +00:00
{
data(place).value.insert(data(place).value.begin(), data(rhs).value.begin(), data(rhs).value.end());
2012-08-26 12:18:50 +00:00
}
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
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);
}
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
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;
2012-08-26 12:18:50 +00:00
value.resize(size);
for (size_t i = 0; i < size; ++i)
type->deserializeBinary(value[i], buf);
}
Field getResult(ConstAggregateDataPtr place) const
2012-08-26 12:18:50 +00:00
{
return data(place).value;
2012-08-26 12:18:50 +00:00
}
};
#undef AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE
}