2012-10-29 02:58:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <type_traits>
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2012-10-29 02:58:52 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
2012-10-29 02:58:52 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <AggregateFunctions/QuantilesCommon.h>
|
2012-10-29 02:58:52 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
2013-06-30 11:38:46 +00:00
|
|
|
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
template <
|
|
|
|
typename Value,
|
|
|
|
typename SecondArg,
|
|
|
|
typename Data,
|
|
|
|
typename Name,
|
|
|
|
bool returns_float,
|
|
|
|
bool returns_many
|
|
|
|
>
|
|
|
|
class AggregateFunctionQuantile final : public IAggregateFunctionDataHelper<Data,
|
|
|
|
AggregateFunctionQuantile<Value, SecondArg, Data, Name, returns_float, returns_many>>
|
2012-10-29 02:58:52 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-12-20 07:36:30 +00:00
|
|
|
bool have_second_arg = !std::is_same_v<SecondArg, void>;
|
2012-10-29 02:58:52 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
Float64 level = 0.5;
|
|
|
|
QuantileLevels levels;
|
|
|
|
DataTypePtr argument_type;
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
public:
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionQuantile(const DataTypePtr & argument_type, double level)
|
|
|
|
: level(level), argument_type(argument_type) {}
|
|
|
|
|
|
|
|
AggregateFunctionQuantile(const DataTypePtr & argument_type, const Array & params)
|
|
|
|
: levels(params), argument_type(argument_type) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
String getName() const override { return Name::name; }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
DataTypePtr res;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
if constexpr (returns_float)
|
|
|
|
res = std::make_shared<DataTypeFloat32>();
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2017-12-20 07:36:30 +00:00
|
|
|
res = argument_type;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
if constexpr (returns_many)
|
|
|
|
return std::make_shared<DataTypeArray>(res);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2017-12-20 07:36:30 +00:00
|
|
|
return res;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-09-17 20:22:39 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
if constexpr (have_second_arg)
|
|
|
|
this->data(place).insert(
|
|
|
|
static_cast<const ColumnVector<Value> &>(*columns[0]).getData()[row_num],
|
|
|
|
static_cast<const ColumnVector<SecondArg> &>(*columns[1]).getData()[row_num]);
|
|
|
|
else if constexpr (have_determinator)
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
else
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).insert(
|
|
|
|
static_cast<const ColumnVector<Value> &>(*columns[0]).getData()[row_num]);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:51:50 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).merge(this->data(rhs));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).serialize(buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).deserialize(buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
if constexpr (returns_many)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
|
|
|
ColumnArray::Offsets & offsets_to = arr_to.getOffsets();
|
|
|
|
|
|
|
|
size_t size = levels.size();
|
|
|
|
offsets_to.push_back((offsets_to.size() == 0 ? 0 : offsets_to.back()) + size);
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if constexpr (returns_float)
|
|
|
|
{
|
|
|
|
typename ColumnFloat32::Container & data_to = static_cast<ColumnFloat32 &>(arr_to.getData()).getData();
|
|
|
|
size_t old_size = data_to.size();
|
|
|
|
data_to.resize(data_to.size() + size);
|
|
|
|
|
|
|
|
this->data(place).getManyFloat(&levels.levels[0], &levels.permutation[0], size, &data_to[old_size]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
typename ColumnVector<Value>::Container & data_to = static_cast<ColumnVector<Value> &>(arr_to.getData()).getData();
|
|
|
|
size_t old_size = data_to.size();
|
|
|
|
data_to.resize(data_to.size() + size);
|
|
|
|
|
|
|
|
this->data(place).getMany(&levels.levels[0], &levels.permutation[0], size, &data_to[old_size]);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
if constexpr (returns_float)
|
|
|
|
static_cast<ColumnFloat32 &>(to).getData().push_back(this->data(place).getFloat(level));
|
|
|
|
else
|
|
|
|
static_cast<ColumnVector<Value> &>(to).getData().push_back(this->data(place).get(level));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-17 20:22:39 +00:00
|
|
|
|
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2013-06-29 23:49:34 +00:00
|
|
|
};
|
|
|
|
|
2012-10-29 02:58:52 +00:00
|
|
|
}
|