2012-10-29 02:58:52 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stats/ReservoirSampler.h>
|
|
|
|
|
|
2015-10-12 07:05:54 +00:00
|
|
|
|
#include <DB/Core/FieldVisitors.h>
|
|
|
|
|
|
2012-10-29 02:58:52 +00:00
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
2013-06-29 23:49:34 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeArray.h>
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
|
|
|
|
|
2013-06-30 11:38:46 +00:00
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
|
|
|
|
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2013-07-27 18:43:10 +00:00
|
|
|
|
template <typename ArgumentFieldType>
|
2013-02-08 19:34:44 +00:00
|
|
|
|
struct AggregateFunctionQuantileData
|
|
|
|
|
{
|
2013-09-24 19:56:33 +00:00
|
|
|
|
typedef ReservoirSampler<ArgumentFieldType, ReservoirSamplerOnEmpty::RETURN_NAN_OR_ZERO> Sample;
|
2014-05-03 22:57:43 +00:00
|
|
|
|
Sample sample; /// TODO Добавить MemoryTracker
|
2013-02-08 19:34:44 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
|
|
/** Приближённо вычисляет квантиль.
|
|
|
|
|
* В качестве типа аргумента может быть только числовой тип (в том числе, дата и дата-с-временем).
|
|
|
|
|
* Если returns_float = true, то типом результата будет Float64, иначе - тип результата совпадает с типом аргумента.
|
|
|
|
|
* Для дат и дат-с-временем returns_float следует задавать равным false.
|
|
|
|
|
*/
|
|
|
|
|
template <typename ArgumentFieldType, bool returns_float = true>
|
2015-11-11 02:04:23 +00:00
|
|
|
|
class AggregateFunctionQuantile final
|
|
|
|
|
: public IUnaryAggregateFunction<AggregateFunctionQuantileData<ArgumentFieldType>, AggregateFunctionQuantile<ArgumentFieldType, returns_float> >
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
|
|
|
|
private:
|
2014-11-21 12:29:33 +00:00
|
|
|
|
using Sample = typename AggregateFunctionQuantileData<ArgumentFieldType>::Sample;
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
|
|
double level;
|
|
|
|
|
DataTypePtr type;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
AggregateFunctionQuantile(double level_ = 0.5) : level(level_) {}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
String getName() const override { return "quantile"; }
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
DataTypePtr getReturnType() const override
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setArgument(const DataTypePtr & argument) override
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
|
|
|
|
if (returns_float)
|
|
|
|
|
type = new DataTypeFloat64;
|
|
|
|
|
else
|
|
|
|
|
type = argument;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setParameters(const Array & params) override
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
|
|
|
|
if (params.size() != 1)
|
|
|
|
|
throw Exception("Aggregate function " + getName() + " requires exactly one parameter.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2013-01-05 20:03:19 +00:00
|
|
|
|
level = apply_visitor(FieldVisitorConvertToNumber<Float64>(), params[0]);
|
2012-10-29 02:58:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
|
2013-06-25 08:06:40 +00:00
|
|
|
|
void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
2013-06-25 14:16:16 +00:00
|
|
|
|
this->data(place).sample.insert(static_cast<const ColumnVector<ArgumentFieldType> &>(column).getData()[row_num]);
|
2012-10-29 02:58:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
2013-02-08 23:41:05 +00:00
|
|
|
|
this->data(place).sample.merge(this->data(rhs).sample);
|
2012-10-29 02:58:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
2013-02-08 23:41:05 +00:00
|
|
|
|
this->data(place).sample.write(buf);
|
2012-10-29 02:58:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const override
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
|
|
|
|
Sample tmp_sample;
|
|
|
|
|
tmp_sample.read(buf);
|
2013-02-08 23:41:05 +00:00
|
|
|
|
this->data(place).sample.merge(tmp_sample);
|
2012-10-29 02:58:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2012-10-29 02:58:52 +00:00
|
|
|
|
{
|
2012-11-14 09:38:30 +00:00
|
|
|
|
/// Sample может отсортироваться при получении квантиля, но в этом контексте можно не считать это нарушением константности.
|
2013-06-30 11:38:46 +00:00
|
|
|
|
Sample & sample = const_cast<Sample &>(this->data(place).sample);
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
|
|
if (returns_float)
|
2013-06-30 11:38:46 +00:00
|
|
|
|
static_cast<ColumnFloat64 &>(to).getData().push_back(sample.quantileInterpolated(level));
|
2012-10-29 02:58:52 +00:00
|
|
|
|
else
|
2013-06-30 11:38:46 +00:00
|
|
|
|
static_cast<ColumnVector<ArgumentFieldType> &>(to).getData().push_back(sample.quantileInterpolated(level));
|
2012-10-29 02:58:52 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-29 23:49:34 +00:00
|
|
|
|
|
|
|
|
|
/** То же самое, но позволяет вычислить сразу несколько квантилей.
|
|
|
|
|
* Для этого, принимает в качестве параметров несколько уровней. Пример: quantiles(0.5, 0.8, 0.9, 0.95)(ConnectTiming).
|
|
|
|
|
* Возвращает массив результатов.
|
|
|
|
|
*/
|
|
|
|
|
template <typename ArgumentFieldType, bool returns_float = true>
|
2015-11-11 02:04:23 +00:00
|
|
|
|
class AggregateFunctionQuantiles final
|
|
|
|
|
: public IUnaryAggregateFunction<AggregateFunctionQuantileData<ArgumentFieldType>, AggregateFunctionQuantiles<ArgumentFieldType, returns_float> >
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
private:
|
2014-11-21 12:29:33 +00:00
|
|
|
|
using Sample = typename AggregateFunctionQuantileData<ArgumentFieldType>::Sample;
|
2013-06-29 23:49:34 +00:00
|
|
|
|
|
2015-11-15 03:11:24 +00:00
|
|
|
|
using Levels = std::vector<double>;
|
2013-06-29 23:49:34 +00:00
|
|
|
|
Levels levels;
|
|
|
|
|
DataTypePtr type;
|
|
|
|
|
|
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
|
String getName() const override { return "quantiles"; }
|
2013-06-29 23:49:34 +00:00
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
DataTypePtr getReturnType() const override
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
return new DataTypeArray(type);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setArgument(const DataTypePtr & argument) override
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
if (returns_float)
|
|
|
|
|
type = new DataTypeFloat64;
|
|
|
|
|
else
|
|
|
|
|
type = argument;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void setParameters(const Array & params) override
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
if (params.empty())
|
|
|
|
|
throw Exception("Aggregate function " + getName() + " requires at least one parameter.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
size_t size = params.size();
|
|
|
|
|
levels.resize(size);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
levels[i] = apply_visitor(FieldVisitorConvertToNumber<Float64>(), params[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
|
|
|
|
{
|
|
|
|
|
this->data(place).sample.insert(static_cast<const ColumnVector<ArgumentFieldType> &>(column).getData()[row_num]);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).sample.merge(this->data(rhs).sample);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).sample.write(buf);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const override
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
Sample tmp_sample;
|
|
|
|
|
tmp_sample.read(buf);
|
|
|
|
|
this->data(place).sample.merge(tmp_sample);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2013-06-29 23:49:34 +00:00
|
|
|
|
{
|
|
|
|
|
/// Sample может отсортироваться при получении квантиля, но в этом контексте можно не считать это нарушением константности.
|
|
|
|
|
Sample & sample = const_cast<Sample &>(this->data(place).sample);
|
|
|
|
|
|
2013-06-30 11:38:46 +00:00
|
|
|
|
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
|
|
|
|
ColumnArray::Offsets_t & offsets_to = arr_to.getOffsets();
|
|
|
|
|
|
|
|
|
|
size_t size = levels.size();
|
|
|
|
|
offsets_to.push_back((offsets_to.size() == 0 ? 0 : offsets_to.back()) + size);
|
2014-11-21 12:29:33 +00:00
|
|
|
|
|
2013-06-29 23:49:34 +00:00
|
|
|
|
if (returns_float)
|
2013-06-30 11:38:46 +00:00
|
|
|
|
{
|
|
|
|
|
ColumnFloat64::Container_t & data_to = static_cast<ColumnFloat64 &>(arr_to.getData()).getData();
|
|
|
|
|
|
2013-06-29 23:49:34 +00:00
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2015-11-15 03:11:24 +00:00
|
|
|
|
data_to.push_back(sample.quantileInterpolated(levels[i]));
|
2013-06-30 11:38:46 +00:00
|
|
|
|
}
|
2013-06-29 23:49:34 +00:00
|
|
|
|
else
|
2013-06-30 11:38:46 +00:00
|
|
|
|
{
|
|
|
|
|
typename ColumnVector<ArgumentFieldType>::Container_t & data_to = static_cast<ColumnVector<ArgumentFieldType> &>(arr_to.getData()).getData();
|
2014-11-21 12:29:33 +00:00
|
|
|
|
|
2013-06-29 23:49:34 +00:00
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2015-11-15 03:11:24 +00:00
|
|
|
|
data_to.push_back(sample.quantileInterpolated(levels[i]));
|
2013-06-30 11:38:46 +00:00
|
|
|
|
}
|
2013-06-29 23:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-10-29 02:58:52 +00:00
|
|
|
|
}
|