2013-07-28 20:53:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypeArray.h>
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2016-09-13 13:24:24 +00:00
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
2013-07-28 20:53:31 +00:00
|
|
|
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
|
|
|
|
2014-04-28 01:48:24 +00:00
|
|
|
#include <DB/Common/HashTable/HashSet.h>
|
2013-07-28 20:53:31 +00:00
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/AggregateFunctionGroupArray.h>
|
|
|
|
|
|
|
|
#define AGGREGATE_FUNCTION_GROUP_ARRAY_UNIQ_MAX_SIZE 0xFFFFFF
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct AggregateFunctionGroupUniqArrayData
|
|
|
|
{
|
2014-04-28 01:48:24 +00:00
|
|
|
/// При создании, хэш-таблица должна быть небольшой.
|
2016-09-13 13:24:24 +00:00
|
|
|
using Set = HashSet<
|
2014-02-02 09:08:06 +00:00
|
|
|
T,
|
2014-04-28 01:48:24 +00:00
|
|
|
DefaultHash<T>,
|
2014-05-03 16:03:49 +00:00
|
|
|
HashTableGrower<4>,
|
|
|
|
HashTableAllocatorWithStackMemory<sizeof(T) * (1 << 4)>
|
2016-09-13 13:24:24 +00:00
|
|
|
>;
|
2014-02-02 09:08:06 +00:00
|
|
|
|
2013-07-28 20:53:31 +00:00
|
|
|
Set value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Складывает все значения в хэш-множество. Возвращает массив уникальных значений. Реализована для числовых типов.
|
|
|
|
template <typename T>
|
2016-07-10 17:19:35 +00:00
|
|
|
class AggregateFunctionGroupUniqArray
|
|
|
|
: public IUnaryAggregateFunction<AggregateFunctionGroupUniqArrayData<T>, AggregateFunctionGroupUniqArray<T>>
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
|
|
|
private:
|
2016-05-28 10:35:44 +00:00
|
|
|
using State = AggregateFunctionGroupUniqArrayData<T>;
|
2015-11-11 02:04:23 +00:00
|
|
|
|
2013-07-28 20:53:31 +00:00
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
String getName() const override { return "groupUniqArray"; }
|
2013-07-28 20:53:31 +00:00
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
DataTypePtr getReturnType() const override
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
2016-05-28 07:48:40 +00:00
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<typename DataTypeFromFieldType<T>::Type>());
|
2013-07-28 20:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-11-15 08:31:08 +00:00
|
|
|
void setArgument(const DataTypePtr & argument)
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena *) const
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
|
|
|
this->data(place).value.insert(static_cast<const ColumnVector<T> &>(column).getData()[row_num]);
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
2014-02-02 09:08:06 +00:00
|
|
|
this->data(place).value.merge(this->data(rhs).value);
|
2013-07-28 20:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
|
|
|
const typename State::Set & set = this->data(place).value;
|
|
|
|
size_t size = set.size();
|
|
|
|
writeVarUInt(size, buf);
|
2016-09-13 13:24:24 +00:00
|
|
|
for (auto it = set.begin(); it != set.end(); ++it)
|
2013-07-28 20:53:31 +00:00
|
|
|
writeIntBinary(*it, buf);
|
|
|
|
}
|
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
this->data(place).value.read(buf);
|
2013-07-28 20:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2013-07-28 20:53:31 +00:00
|
|
|
{
|
|
|
|
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
|
|
|
ColumnArray::Offsets_t & offsets_to = arr_to.getOffsets();
|
|
|
|
|
|
|
|
const typename State::Set & set = this->data(place).value;
|
|
|
|
size_t size = set.size();
|
|
|
|
|
|
|
|
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();
|
|
|
|
size_t old_size = data_to.size();
|
|
|
|
data_to.resize(old_size + size);
|
|
|
|
|
|
|
|
size_t i = 0;
|
2016-09-13 13:24:24 +00:00
|
|
|
for (auto it = set.begin(); it != set.end(); ++it, ++i)
|
2013-07-28 20:53:31 +00:00
|
|
|
data_to[old_size + i] = *it;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-13 13:24:24 +00:00
|
|
|
|
2013-07-28 20:53:31 +00:00
|
|
|
#undef AGGREGATE_FUNCTION_GROUP_ARRAY_UNIQ_MAX_SIZE
|
|
|
|
|
|
|
|
}
|