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>
|
2015-11-15 04:54:56 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
|
|
|
|
#include <DB/Columns/ColumnVector.h>
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
2012-08-26 12:18:50 +00:00
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
|
|
|
|
|
|
|
#define AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE 0xFFFFFF
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-11-15 04:54:56 +00:00
|
|
|
|
|
|
|
/// Частный случай - реализация для числовых типов.
|
|
|
|
template <typename T>
|
|
|
|
struct AggregateFunctionGroupArrayDataNumeric
|
|
|
|
{
|
|
|
|
/// Сразу будет выделена память на несколько элементов так, чтобы состояние занимало 64 байта.
|
|
|
|
static constexpr size_t bytes_in_arena = 64 - sizeof(PODArray<T>);
|
|
|
|
|
|
|
|
using Array = PODArray<T, bytes_in_arena / sizeof(T), AllocatorWithStackMemory<Allocator<false>, bytes_in_arena>>;
|
|
|
|
Array value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Общий случай (неэффективно). NOTE Можно ещё реализовать частный случай для строк.
|
|
|
|
struct AggregateFunctionGroupArrayDataGeneric
|
|
|
|
{
|
|
|
|
Array value; /// TODO Добавить MemoryTracker
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class AggregateFunctionGroupArrayNumeric final
|
|
|
|
: public IUnaryAggregateFunction<AggregateFunctionGroupArrayDataNumeric<T>, AggregateFunctionGroupArrayNumeric<T>>
|
2013-02-08 19:34:44 +00:00
|
|
|
{
|
2015-11-15 04:54:56 +00:00
|
|
|
public:
|
|
|
|
String getName() const override { return "groupArray"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return new DataTypeArray(new typename DataTypeFromFieldType<T>::Type);
|
|
|
|
}
|
|
|
|
|
2015-11-15 08:31:08 +00:00
|
|
|
void setArgument(const DataTypePtr & argument)
|
2015-11-15 04:54:56 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-15 06:23:44 +00:00
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2015-11-15 04:54:56 +00:00
|
|
|
{
|
|
|
|
this->data(place).value.push_back(static_cast<const ColumnVector<T> &>(column).getData()[row_num]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
|
|
|
{
|
|
|
|
this->data(place).value.insert(this->data(rhs).value.begin(), this->data(rhs).value.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
const auto & value = this->data(place).value;
|
|
|
|
size_t size = value.size();
|
|
|
|
writeVarUInt(size, buf);
|
|
|
|
buf.write(reinterpret_cast<const char *>(&value[0]), size * sizeof(value[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
auto & value = this->data(place).value;
|
|
|
|
|
|
|
|
size_t old_size = value.size();
|
|
|
|
value.resize(old_size + size);
|
|
|
|
buf.read(reinterpret_cast<char *>(&value[old_size]), size * sizeof(value[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
const auto & value = this->data(place).value;
|
|
|
|
size_t size = value.size();
|
|
|
|
|
|
|
|
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
|
|
|
ColumnArray::Offsets_t & offsets_to = arr_to.getOffsets();
|
|
|
|
|
|
|
|
offsets_to.push_back((offsets_to.size() == 0 ? 0 : offsets_to.back()) + size);
|
|
|
|
|
|
|
|
typename ColumnVector<T>::Container_t & data_to = static_cast<ColumnVector<T> &>(arr_to.getData()).getData();
|
|
|
|
data_to.insert(this->data(place).value.begin(), this->data(place).value.end());
|
|
|
|
}
|
2013-02-08 19:34:44 +00:00
|
|
|
};
|
2015-02-04 05:07:51 +00:00
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
2015-11-15 04:54:56 +00:00
|
|
|
/// Складывает все значения в массив, общий случай. Реализовано неэффективно.
|
|
|
|
class AggregateFunctionGroupArrayGeneric final
|
|
|
|
: public IUnaryAggregateFunction<AggregateFunctionGroupArrayDataGeneric, AggregateFunctionGroupArrayGeneric>
|
2012-08-26 12:18:50 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
DataTypePtr type;
|
2015-02-04 05:07:51 +00:00
|
|
|
|
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-15 08:31:08 +00:00
|
|
|
void setArgument(const DataTypePtr & argument)
|
2012-08-26 12:18:50 +00:00
|
|
|
{
|
|
|
|
type = argument;
|
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
2015-11-15 06:23:44 +00:00
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2012-08-26 12:18:50 +00:00
|
|
|
{
|
2013-06-25 08:06:40 +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
|
|
|
{
|
2015-01-19 19:51:46 +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
|
|
|
{
|
2013-02-08 19:34:44 +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);
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
Array & value = data(place).value;
|
2013-07-28 19:47:27 +00:00
|
|
|
|
|
|
|
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)
|
2013-07-28 19:47:27 +00:00
|
|
|
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
|
|
|
{
|
2013-06-30 11:38:46 +00:00
|
|
|
to.insert(data(place).value);
|
2012-08-26 12:18:50 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#undef AGGREGATE_FUNCTION_GROUP_ARRAY_MAX_ARRAY_SIZE
|
|
|
|
|
|
|
|
}
|