2011-09-19 01:42:16 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
#include <DB/Common/Arena.h>
|
|
|
|
|
|
2011-09-19 01:42:16 +00:00
|
|
|
|
#include <DB/AggregateFunctions/IAggregateFunction.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Columns/ColumnVector.h>
|
|
|
|
|
|
2014-05-22 14:09:10 +00:00
|
|
|
|
#include <DB/Core/Field.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/ReadBufferFromString.h>
|
2011-09-19 01:42:16 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2013-02-08 23:41:05 +00:00
|
|
|
|
|
2011-09-19 01:42:16 +00:00
|
|
|
|
/** Столбец, хранящий состояния агрегатных функций.
|
2013-02-08 20:34:30 +00:00
|
|
|
|
* Состояния агрегатных функций хранятся в пуле (arena),
|
|
|
|
|
* (возможно, в нескольких)
|
|
|
|
|
* а в массиве (ColumnVector) хранятся указатели на них.
|
2013-02-08 19:34:44 +00:00
|
|
|
|
* Столбец захватывает владение пулом и всеми агрегатными функциями,
|
|
|
|
|
* которые в него переданы (уничтожает их в дестркуторе).
|
2011-09-19 01:42:16 +00:00
|
|
|
|
*/
|
2013-02-08 23:41:05 +00:00
|
|
|
|
class ColumnAggregateFunction : public ColumnVectorBase<AggregateDataPtr>
|
2011-09-19 01:42:16 +00:00
|
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
|
private:
|
2014-02-26 11:44:54 +00:00
|
|
|
|
AggregateFunctionPtr func; /// Используется для уничтожения состояний и для финализации значений.
|
2013-02-08 20:34:30 +00:00
|
|
|
|
Arenas arenas;
|
2011-09-19 01:42:16 +00:00
|
|
|
|
public:
|
2013-02-08 23:41:05 +00:00
|
|
|
|
ColumnAggregateFunction(const AggregateFunctionPtr & func_)
|
|
|
|
|
: func(func_)
|
2013-02-08 19:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 23:41:05 +00:00
|
|
|
|
ColumnAggregateFunction(const AggregateFunctionPtr & func_, const Arenas & arenas_)
|
|
|
|
|
: func(func_), arenas(arenas_)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void set(const AggregateFunctionPtr & func_)
|
2013-02-08 19:34:44 +00:00
|
|
|
|
{
|
|
|
|
|
func = func_;
|
2013-02-08 20:34:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-21 16:09:04 +00:00
|
|
|
|
AggregateFunctionPtr getAggregateFunction() { return func; }
|
|
|
|
|
AggregateFunctionPtr getAggregateFunction() const { return func; }
|
2014-02-27 12:49:21 +00:00
|
|
|
|
|
2013-02-08 20:34:30 +00:00
|
|
|
|
/// Захватить владение ареной.
|
2013-02-08 23:41:05 +00:00
|
|
|
|
void addArena(ArenaPtr arena_)
|
2013-02-08 20:34:30 +00:00
|
|
|
|
{
|
|
|
|
|
arenas.push_back(arena_);
|
2013-02-08 19:34:44 +00:00
|
|
|
|
}
|
2013-02-08 23:41:05 +00:00
|
|
|
|
|
2014-02-27 12:49:21 +00:00
|
|
|
|
ColumnPtr convertToValues()
|
|
|
|
|
{
|
|
|
|
|
IAggregateFunction * function = func;
|
|
|
|
|
ColumnPtr res = function->getReturnType()->createColumn();
|
|
|
|
|
IColumn & column = *res;
|
|
|
|
|
res->reserve(data.size());
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < data.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
function->insertResultInto(data[i], column);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-03 23:54:12 +00:00
|
|
|
|
~ColumnAggregateFunction()
|
2013-02-03 23:11:21 +00:00
|
|
|
|
{
|
2013-11-03 23:54:12 +00:00
|
|
|
|
if (!func->hasTrivialDestructor())
|
|
|
|
|
for (size_t i = 0, s = data.size(); i < s; ++i)
|
|
|
|
|
func->destroy(data[i]);
|
2013-02-03 23:11:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 01:42:16 +00:00
|
|
|
|
std::string getName() const { return "ColumnAggregateFunction"; }
|
|
|
|
|
|
2013-02-08 23:41:05 +00:00
|
|
|
|
ColumnPtr cloneEmpty() const { return new ColumnAggregateFunction(func, arenas); };
|
2012-05-30 03:30:29 +00:00
|
|
|
|
|
2011-09-19 01:42:16 +00:00
|
|
|
|
bool isNumeric() const { return false; }
|
|
|
|
|
|
|
|
|
|
Field operator[](size_t n) const
|
|
|
|
|
{
|
2014-05-22 14:09:10 +00:00
|
|
|
|
Field field = String();
|
|
|
|
|
{
|
|
|
|
|
WriteBufferFromString buffer(field.get<String &>());
|
|
|
|
|
func->serialize(data[n], buffer);
|
|
|
|
|
}
|
|
|
|
|
return field;
|
2011-09-19 01:42:16 +00:00
|
|
|
|
}
|
2012-10-07 06:30:10 +00:00
|
|
|
|
|
2013-01-07 06:47:15 +00:00
|
|
|
|
void get(size_t n, Field & res) const
|
|
|
|
|
{
|
2014-05-22 14:09:10 +00:00
|
|
|
|
res.assignString("", 0);
|
|
|
|
|
{
|
|
|
|
|
WriteBufferFromString buffer(res.get<String &>());
|
|
|
|
|
func->serialize(data[n], buffer);
|
|
|
|
|
}
|
2013-01-07 06:47:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
|
StringRef getDataAt(size_t n) const
|
|
|
|
|
{
|
2014-05-22 14:09:10 +00:00
|
|
|
|
return StringRef(reinterpret_cast<const char *>(&data[n]), sizeof(data[n]));
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 16:11:20 +00:00
|
|
|
|
/// Объединить состояние в последней строке с заданным
|
|
|
|
|
void insertMerge(const Field & x)
|
2014-05-22 14:09:10 +00:00
|
|
|
|
{
|
|
|
|
|
ReadBufferFromString read_buffer(x.safeGet<const String &>());
|
2014-05-26 16:11:20 +00:00
|
|
|
|
func->deserializeMerge(data.back(), read_buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insert(const Field & x)
|
|
|
|
|
{
|
|
|
|
|
data.push_back(AggregateDataPtr());
|
|
|
|
|
func->create(data.back());
|
|
|
|
|
insertMerge(x);
|
2012-10-07 06:30:10 +00:00
|
|
|
|
}
|
2013-02-16 20:15:45 +00:00
|
|
|
|
|
|
|
|
|
void insertData(const char * pos, size_t length)
|
|
|
|
|
{
|
2014-05-26 16:11:20 +00:00
|
|
|
|
data.push_back(AggregateDataPtr());
|
|
|
|
|
func->create(data.back());
|
2014-05-22 14:09:10 +00:00
|
|
|
|
ReadBuffer read_buffer(const_cast<char *>(pos), length);
|
2014-05-26 16:11:20 +00:00
|
|
|
|
func->deserializeMerge(data.back(), read_buffer);
|
2013-02-16 20:15:45 +00:00
|
|
|
|
}
|
2011-09-19 01:42:16 +00:00
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnPtr cut(size_t start, size_t length) const
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Method cut is not supported for ColumnAggregateFunction.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ColumnPtr filter(const Filter & filter) const
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Method filter is not supported for ColumnAggregateFunction.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
ColumnPtr permute(const Permutation & perm, size_t limit) const
|
2013-05-03 05:23:14 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception("Method permute is not supported for ColumnAggregateFunction.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ColumnPtr replicate(const Offsets_t & offsets) const
|
2013-02-08 23:41:05 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception("Method replicate is not supported for ColumnAggregateFunction.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-06 20:28:22 +00:00
|
|
|
|
void getExtremes(Field & min, Field & max) const
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Method getExtremes is not supported for ColumnAggregateFunction.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-01 20:10:43 +00:00
|
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const
|
2011-09-19 01:42:16 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-09-26 11:05:38 +00:00
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
void getPermutation(bool reverse, size_t limit, Permutation & res) const
|
2011-09-26 11:05:38 +00:00
|
|
|
|
{
|
|
|
|
|
size_t s = data.size();
|
2013-12-08 02:29:40 +00:00
|
|
|
|
res.resize(s);
|
2011-09-26 11:05:38 +00:00
|
|
|
|
for (size_t i = 0; i < s; ++i)
|
|
|
|
|
res[i] = i;
|
|
|
|
|
}
|
2011-09-19 01:42:16 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|