#pragma once #include #include #include #include #define AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE 0xFFFFFF namespace DB { struct AggregateFunctionGroupArrayData { Array value; /// TODO Добавить MemoryTracker }; /// Складывает все значения в массив. Реализовано неэффективно. class AggregateFunctionGroupArray final : public IUnaryAggregateFunction { private: DataTypePtr type; public: String getName() const override { return "groupArray"; } DataTypePtr getReturnType() const override { return new DataTypeArray(type); } void setArgument(const DataTypePtr & argument) override { type = argument; } void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const { data(place).value.push_back(Array::value_type()); column.get(row_num, data(place).value.back()); } void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override { data(place).value.insert(data(place).value.end(), data(rhs).value.begin(), data(rhs).value.end()); } void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override { const Array & value = data(place).value; 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 override { 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); for (size_t i = 0; i < size; ++i) type->deserializeBinary(value[old_size + i], buf); } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override { to.insert(data(place).value); } }; #undef AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE }