2017-05-02 21:08:37 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
|
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
|
|
|
|
#include <Common/SpaceSaving.h>
|
|
|
|
|
2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2017-05-02 21:08:37 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2017-05-02 21:08:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
// Allow NxK more space before calculating top K to increase accuracy
|
|
|
|
#define TOP_K_LOAD_FACTOR 3
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct AggregateFunctionTopKData
|
|
|
|
{
|
2017-05-10 00:17:45 +00:00
|
|
|
using Set = SpaceSaving
|
|
|
|
<
|
|
|
|
T,
|
|
|
|
HashCRC32<T>,
|
2017-05-12 20:38:03 +00:00
|
|
|
HashTableGrower<4>,
|
|
|
|
HashTableAllocatorWithStackMemory<sizeof(T) * (1 << 4)>
|
2017-05-10 00:17:45 +00:00
|
|
|
>;
|
2017-05-02 21:08:37 +00:00
|
|
|
Set value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class AggregateFunctionTopK
|
2017-12-20 07:36:30 +00:00
|
|
|
: public IAggregateFunctionDataHelper<AggregateFunctionTopKData<T>, AggregateFunctionTopK<T>>
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
using State = AggregateFunctionTopKData<T>;
|
2017-12-20 20:58:43 +00:00
|
|
|
|
|
|
|
UInt64 threshold;
|
|
|
|
UInt64 reserved;
|
2017-05-02 21:08:37 +00:00
|
|
|
|
|
|
|
public:
|
2017-12-20 20:58:43 +00:00
|
|
|
AggregateFunctionTopK(UInt64 threshold)
|
|
|
|
: threshold(threshold), reserved(TOP_K_LOAD_FACTOR * threshold) {}
|
|
|
|
|
2017-05-02 21:08:37 +00:00
|
|
|
String getName() const override { return "topK"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeNumber<T>>());
|
|
|
|
}
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
|
|
|
auto & set = this->data(place).value;
|
2017-05-05 23:36:02 +00:00
|
|
|
if (set.capacity() != reserved)
|
2017-05-02 21:08:37 +00:00
|
|
|
set.resize(reserved);
|
2017-12-20 07:36:30 +00:00
|
|
|
set.insert(static_cast<const ColumnVector<T> &>(*columns[0]).getData()[row_num]);
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:51:50 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
|
|
|
this->data(place).value.merge(this->data(rhs).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
this->data(place).value.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
|
|
|
{
|
|
|
|
auto & set = this->data(place).value;
|
|
|
|
set.resize(reserved);
|
|
|
|
set.read(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnArray::Offsets & offsets_to = arr_to.getOffsets();
|
2017-05-02 21:08:37 +00:00
|
|
|
|
|
|
|
const typename State::Set & set = this->data(place).value;
|
2017-10-08 22:53:38 +00:00
|
|
|
auto result_vec = set.topK(threshold);
|
|
|
|
size_t size = result_vec.size();
|
2017-05-02 21:08:37 +00:00
|
|
|
|
2018-12-24 14:26:38 +00:00
|
|
|
offsets_to.push_back(offsets_to.back() + size);
|
2017-05-02 21:08:37 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnVector<T>::Container & data_to = static_cast<ColumnVector<T> &>(arr_to.getData()).getData();
|
2017-05-02 21:08:37 +00:00
|
|
|
size_t old_size = data_to.size();
|
|
|
|
data_to.resize(old_size + size);
|
|
|
|
|
|
|
|
size_t i = 0;
|
2017-10-08 22:53:38 +00:00
|
|
|
for (auto it = result_vec.begin(); it != result_vec.end(); ++it, ++i)
|
2017-05-02 21:08:37 +00:00
|
|
|
data_to[old_size + i] = it->key;
|
|
|
|
}
|
2017-09-17 20:22:39 +00:00
|
|
|
|
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2017-05-02 21:08:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Generic implementation, it uses serialized representation as object descriptor.
|
|
|
|
struct AggregateFunctionTopKGenericData
|
|
|
|
{
|
2017-05-10 00:17:45 +00:00
|
|
|
using Set = SpaceSaving
|
|
|
|
<
|
|
|
|
StringRef,
|
|
|
|
StringRefHash,
|
2017-05-12 20:38:03 +00:00
|
|
|
HashTableGrower<4>,
|
|
|
|
HashTableAllocatorWithStackMemory<sizeof(StringRef) * (1 << 4)>
|
2017-05-10 00:17:45 +00:00
|
|
|
>;
|
2017-05-02 21:08:37 +00:00
|
|
|
|
|
|
|
Set value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Template parameter with true value should be used for columns that store their elements in memory continuously.
|
|
|
|
* For such columns topK() can be implemented more efficently (especially for small numeric arrays).
|
|
|
|
*/
|
|
|
|
template <bool is_plain_column = false>
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionTopKGeneric : public IAggregateFunctionDataHelper<AggregateFunctionTopKGenericData, AggregateFunctionTopKGeneric<is_plain_column>>
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
using State = AggregateFunctionTopKGenericData;
|
2017-12-20 20:58:43 +00:00
|
|
|
|
|
|
|
UInt64 threshold;
|
|
|
|
UInt64 reserved;
|
2017-05-02 21:08:37 +00:00
|
|
|
DataTypePtr input_data_type;
|
|
|
|
|
|
|
|
static void deserializeAndInsert(StringRef str, IColumn & data_to);
|
|
|
|
|
|
|
|
public:
|
2017-12-20 20:58:43 +00:00
|
|
|
AggregateFunctionTopKGeneric(UInt64 threshold, const DataTypePtr & input_data_type)
|
|
|
|
: threshold(threshold), reserved(TOP_K_LOAD_FACTOR * threshold), input_data_type(input_data_type) {}
|
|
|
|
|
2017-05-02 21:08:37 +00:00
|
|
|
String getName() const override { return "topK"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
2017-12-18 01:11:48 +00:00
|
|
|
return std::make_shared<DataTypeArray>(input_data_type);
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool allocatesMemoryInArena() const override
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
this->data(place).value.write(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
|
|
|
|
{
|
|
|
|
auto & set = this->data(place).value;
|
2017-09-28 07:01:54 +00:00
|
|
|
set.clear();
|
2017-10-08 22:10:13 +00:00
|
|
|
set.resize(reserved);
|
2017-05-02 21:08:37 +00:00
|
|
|
|
2017-10-08 22:09:00 +00:00
|
|
|
// Specialized here because there's no deserialiser for StringRef
|
2018-06-04 19:22:27 +00:00
|
|
|
size_t size = 0;
|
|
|
|
readVarUInt(size, buf);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2017-10-08 22:09:00 +00:00
|
|
|
{
|
2017-06-26 00:11:32 +00:00
|
|
|
auto ref = readStringBinaryInto(*arena, buf);
|
2018-06-04 19:22:27 +00:00
|
|
|
UInt64 count;
|
|
|
|
UInt64 error;
|
2017-05-02 21:08:37 +00:00
|
|
|
readVarUInt(count, buf);
|
|
|
|
readVarUInt(error, buf);
|
2017-06-26 00:11:32 +00:00
|
|
|
set.insert(ref, count, error);
|
|
|
|
arena->rollback(ref.size);
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
2017-09-28 07:01:54 +00:00
|
|
|
|
|
|
|
set.readAlphaMap(buf);
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
|
|
|
auto & set = this->data(place).value;
|
2017-10-08 22:09:00 +00:00
|
|
|
if (set.capacity() != reserved)
|
2017-05-02 21:08:37 +00:00
|
|
|
set.resize(reserved);
|
|
|
|
|
2017-12-21 02:06:08 +00:00
|
|
|
if constexpr (is_plain_column)
|
2017-12-21 02:07:26 +00:00
|
|
|
{
|
|
|
|
set.insert(columns[0]->getDataAt(row_num));
|
|
|
|
}
|
|
|
|
else
|
2017-12-21 02:06:08 +00:00
|
|
|
{
|
|
|
|
const char * begin = nullptr;
|
|
|
|
StringRef str_serialized = columns[0]->serializeValueIntoArena(row_num, *arena, begin);
|
|
|
|
set.insert(str_serialized);
|
|
|
|
arena->rollback(str_serialized.size);
|
|
|
|
}
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:51:50 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
|
|
|
this->data(place).value.merge(this->data(rhs).value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnArray::Offsets & offsets_to = arr_to.getOffsets();
|
2017-05-02 21:08:37 +00:00
|
|
|
IColumn & data_to = arr_to.getData();
|
|
|
|
|
2017-10-08 22:53:38 +00:00
|
|
|
auto result_vec = this->data(place).value.topK(threshold);
|
2018-12-24 14:26:38 +00:00
|
|
|
offsets_to.push_back(offsets_to.back() + result_vec.size());
|
2017-05-02 21:08:37 +00:00
|
|
|
|
2017-10-08 22:53:38 +00:00
|
|
|
for (auto & elem : result_vec)
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
2017-12-21 02:06:08 +00:00
|
|
|
if constexpr (is_plain_column)
|
|
|
|
data_to.insertData(elem.key.data, elem.key.size);
|
|
|
|
else
|
|
|
|
data_to.deserializeAndInsertFromArena(elem.key.data);
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-17 20:22:39 +00:00
|
|
|
|
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2017-05-02 21:08:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#undef TOP_K_LOAD_FACTOR
|
|
|
|
|
|
|
|
}
|