mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-18 13:42:02 +00:00
d89ee33ce2
commitc567d4e1fe
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 20:35:01 2017 +0300 Style [#METR-2944]. commit26bf3e1228
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 20:33:11 2017 +0300 Miscellaneous [#METR-2944]. commiteb946f4c6f
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 20:30:19 2017 +0300 Miscellaneous [#METR-2944]. commit78c867a147
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 20:11:41 2017 +0300 Miscellaneous [#METR-2944]. commit6604c5c83c
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 19:56:15 2017 +0300 Miscellaneous [#METR-2944]. commit23fbf05c1d
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 19:47:52 2017 +0300 Miscellaneous [#METR-2944]. commit98772faf11
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 19:46:05 2017 +0300 Miscellaneous [#METR-2944]. commit3dc636ab9f
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 19:39:46 2017 +0300 Miscellaneous [#METR-2944]. commit3e16aee954
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 19:38:03 2017 +0300 Miscellaneous [#METR-2944]. commitae7e7e90eb
Author: Alexey Milovidov <milovidov@yandex-team.ru> Date: Fri Jan 6 19:34:15 2017 +0300 Miscellaneous [#METR-2944].
211 lines
7.2 KiB
C++
211 lines
7.2 KiB
C++
#pragma once
|
||
|
||
#include <DB/AggregateFunctions/ReservoirSamplerDeterministic.h>
|
||
|
||
#include <DB/Core/FieldVisitors.h>
|
||
|
||
#include <DB/IO/WriteHelpers.h>
|
||
#include <DB/IO/ReadHelpers.h>
|
||
|
||
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
||
#include <DB/DataTypes/DataTypeArray.h>
|
||
|
||
#include <DB/AggregateFunctions/IBinaryAggregateFunction.h>
|
||
|
||
#include <DB/Columns/ColumnArray.h>
|
||
|
||
|
||
namespace DB
|
||
{
|
||
|
||
template <typename ArgumentFieldType>
|
||
struct AggregateFunctionQuantileDeterministicData
|
||
{
|
||
using Sample = ReservoirSamplerDeterministic<ArgumentFieldType, ReservoirSamplerDeterministicOnEmpty::RETURN_NAN_OR_ZERO>;
|
||
Sample sample; /// TODO Добавить MemoryTracker
|
||
};
|
||
|
||
|
||
/** Приближённо вычисляет квантиль.
|
||
* В качестве типа аргумента может быть только числовой тип (в том числе, дата и дата-с-временем).
|
||
* Если returns_float = true, то типом результата будет Float64, иначе - тип результата совпадает с типом аргумента.
|
||
* Для дат и дат-с-временем returns_float следует задавать равным false.
|
||
*/
|
||
template <typename ArgumentFieldType, bool returns_float = true>
|
||
class AggregateFunctionQuantileDeterministic final
|
||
: public IBinaryAggregateFunction<
|
||
AggregateFunctionQuantileDeterministicData<ArgumentFieldType>,
|
||
AggregateFunctionQuantileDeterministic<ArgumentFieldType, returns_float>>
|
||
{
|
||
private:
|
||
using Sample = typename AggregateFunctionQuantileDeterministicData<ArgumentFieldType>::Sample;
|
||
|
||
double level;
|
||
DataTypePtr type;
|
||
|
||
public:
|
||
AggregateFunctionQuantileDeterministic(double level_ = 0.5) : level(level_) {}
|
||
|
||
String getName() const override { return "quantileDeterministic"; }
|
||
|
||
DataTypePtr getReturnType() const override
|
||
{
|
||
return type;
|
||
}
|
||
|
||
void setArgumentsImpl(const DataTypes & arguments)
|
||
{
|
||
type = returns_float ? std::make_shared<DataTypeFloat64>() : arguments[0];
|
||
|
||
if (!arguments[1]->isNumeric())
|
||
throw Exception{
|
||
"Invalid type of second argument to function " + getName() +
|
||
", got " + arguments[1]->getName() + ", expected numeric",
|
||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
||
};
|
||
}
|
||
|
||
void setParameters(const Array & params) override
|
||
{
|
||
if (params.size() != 1)
|
||
throw Exception("Aggregate function " + getName() + " requires exactly one parameter.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
||
|
||
level = applyVisitor(FieldVisitorConvertToNumber<Float64>(), params[0]);
|
||
}
|
||
|
||
|
||
void addImpl(AggregateDataPtr place, const IColumn & column, const IColumn & determinator, size_t row_num, Arena *) const
|
||
{
|
||
this->data(place).sample.insert(static_cast<const ColumnVector<ArgumentFieldType> &>(column).getData()[row_num],
|
||
determinator.get64(row_num));
|
||
}
|
||
|
||
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
||
{
|
||
this->data(place).sample.merge(this->data(rhs).sample);
|
||
}
|
||
|
||
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
||
{
|
||
this->data(place).sample.write(buf);
|
||
}
|
||
|
||
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
||
{
|
||
this->data(place).sample.read(buf);
|
||
}
|
||
|
||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
||
{
|
||
/// Sample может отсортироваться при получении квантиля, но в этом контексте можно не считать это нарушением константности.
|
||
Sample & sample = const_cast<Sample &>(this->data(place).sample);
|
||
|
||
if (returns_float)
|
||
static_cast<ColumnFloat64 &>(to).getData().push_back(sample.quantileInterpolated(level));
|
||
else
|
||
static_cast<ColumnVector<ArgumentFieldType> &>(to).getData().push_back(sample.quantileInterpolated(level));
|
||
}
|
||
};
|
||
|
||
|
||
/** То же самое, но позволяет вычислить сразу несколько квантилей.
|
||
* Для этого, принимает в качестве параметров несколько уровней. Пример: quantiles(0.5, 0.8, 0.9, 0.95)(ConnectTiming).
|
||
* Возвращает массив результатов.
|
||
*/
|
||
template <typename ArgumentFieldType, bool returns_float = true>
|
||
class AggregateFunctionQuantilesDeterministic final
|
||
: public IBinaryAggregateFunction<
|
||
AggregateFunctionQuantileDeterministicData<ArgumentFieldType>,
|
||
AggregateFunctionQuantilesDeterministic<ArgumentFieldType, returns_float>>
|
||
{
|
||
private:
|
||
using Sample = typename AggregateFunctionQuantileDeterministicData<ArgumentFieldType>::Sample;
|
||
|
||
using Levels = std::vector<double>;
|
||
Levels levels;
|
||
DataTypePtr type;
|
||
|
||
public:
|
||
String getName() const override { return "quantilesDeterministic"; }
|
||
|
||
DataTypePtr getReturnType() const override
|
||
{
|
||
return std::make_shared<DataTypeArray>(type);
|
||
}
|
||
|
||
void setArgumentsImpl(const DataTypes & arguments)
|
||
{
|
||
type = returns_float ? std::make_shared<DataTypeFloat64>() : arguments[0];
|
||
|
||
if (!arguments[1]->isNumeric())
|
||
throw Exception{
|
||
"Invalid type of second argument to function " + getName() +
|
||
", got " + arguments[1]->getName() + ", expected numeric",
|
||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
||
};
|
||
}
|
||
|
||
void setParameters(const Array & params) override
|
||
{
|
||
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] = applyVisitor(FieldVisitorConvertToNumber<Float64>(), params[i]);
|
||
}
|
||
|
||
|
||
void addImpl(AggregateDataPtr place, const IColumn & column, const IColumn & determinator, size_t row_num, Arena *) const
|
||
{
|
||
this->data(place).sample.insert(static_cast<const ColumnVector<ArgumentFieldType> &>(column).getData()[row_num],
|
||
determinator.get64(row_num));
|
||
}
|
||
|
||
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
||
{
|
||
this->data(place).sample.merge(this->data(rhs).sample);
|
||
}
|
||
|
||
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
||
{
|
||
this->data(place).sample.write(buf);
|
||
}
|
||
|
||
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
||
{
|
||
this->data(place).sample.read(buf);
|
||
}
|
||
|
||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
||
{
|
||
/// Sample может отсортироваться при получении квантиля, но в этом контексте можно не считать это нарушением константности.
|
||
Sample & sample = const_cast<Sample &>(this->data(place).sample);
|
||
|
||
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);
|
||
|
||
if (returns_float)
|
||
{
|
||
ColumnFloat64::Container_t & data_to = static_cast<ColumnFloat64 &>(arr_to.getData()).getData();
|
||
|
||
for (size_t i = 0; i < size; ++i)
|
||
data_to.push_back(sample.quantileInterpolated(levels[i]));
|
||
}
|
||
else
|
||
{
|
||
typename ColumnVector<ArgumentFieldType>::Container_t & data_to = static_cast<ColumnVector<ArgumentFieldType> &>(arr_to.getData()).getData();
|
||
|
||
for (size_t i = 0; i < size; ++i)
|
||
data_to.push_back(sample.quantileInterpolated(levels[i]));
|
||
}
|
||
}
|
||
};
|
||
|
||
}
|