2012-10-29 02:58:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-11-19 14:25:42 +00:00
|
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
/// These must be exposed in header for the purpose of dynamic compilation.
|
|
|
|
#include <AggregateFunctions/QuantileReservoirSampler.h>
|
|
|
|
#include <AggregateFunctions/QuantileReservoirSamplerDeterministic.h>
|
|
|
|
#include <AggregateFunctions/QuantileExact.h>
|
|
|
|
#include <AggregateFunctions/QuantileExactWeighted.h>
|
|
|
|
#include <AggregateFunctions/QuantileTiming.h>
|
|
|
|
#include <AggregateFunctions/QuantileTDigest.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>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
2018-09-12 16:36:13 +00:00
|
|
|
#include <Columns/ColumnDecimal.h>
|
2018-11-14 15:04:23 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
#include <type_traits>
|
2013-06-30 11:38:46 +00:00
|
|
|
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-20 08:14:33 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2019-02-25 18:03:52 +00:00
|
|
|
template <typename> class QuantileTiming;
|
|
|
|
|
2017-12-20 08:14:33 +00:00
|
|
|
|
|
|
|
/** Generic aggregate function for calculation of quantiles.
|
2017-12-20 20:25:22 +00:00
|
|
|
* It depends on quantile calculation data structure. Look at Quantile*.h for various implementations.
|
2017-12-20 08:14:33 +00:00
|
|
|
*/
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
template <
|
2017-12-20 08:14:33 +00:00
|
|
|
/// Type of first argument.
|
2017-12-20 07:36:30 +00:00
|
|
|
typename Value,
|
2017-12-20 08:14:33 +00:00
|
|
|
/// Data structure and implementation of calculation. Look at QuantileExact.h for example.
|
2017-12-20 07:36:30 +00:00
|
|
|
typename Data,
|
2017-12-20 20:25:22 +00:00
|
|
|
/// Structure with static member "name", containing the name of the aggregate function.
|
2017-12-20 07:36:30 +00:00
|
|
|
typename Name,
|
2018-11-14 15:04:23 +00:00
|
|
|
/// If true, the function accepts the second argument
|
2017-12-20 20:25:22 +00:00
|
|
|
/// (in can be "weight" to calculate quantiles or "determinator" that is used instead of PRNG).
|
|
|
|
/// Second argument is always obtained through 'getUInt' method.
|
2018-11-14 15:04:23 +00:00
|
|
|
bool has_second_arg,
|
2017-12-21 01:19:25 +00:00
|
|
|
/// If non-void, the function will return float of specified type with possibly interpolated results and NaN if there was no values.
|
2017-12-20 08:14:33 +00:00
|
|
|
/// Otherwise it will return Value type and default value if there was no values.
|
|
|
|
/// As an example, the function cannot return floats, if the SQL type of argument is Date or DateTime.
|
2017-12-21 01:19:25 +00:00
|
|
|
typename FloatReturnType,
|
2017-12-20 08:14:33 +00:00
|
|
|
/// If true, the function will accept multiple parameters with quantile levels
|
|
|
|
/// and return an Array filled with many values of that quantiles.
|
2017-12-20 07:36:30 +00:00
|
|
|
bool returns_many
|
|
|
|
>
|
|
|
|
class AggregateFunctionQuantile final : public IAggregateFunctionDataHelper<Data,
|
2018-11-14 15:04:23 +00:00
|
|
|
AggregateFunctionQuantile<Value, Data, Name, has_second_arg, FloatReturnType, returns_many>>
|
2012-10-29 02:58:52 +00:00
|
|
|
{
|
|
|
|
private:
|
2018-09-12 16:36:13 +00:00
|
|
|
using ColVecType = std::conditional_t<IsDecimalNumber<Value>, ColumnDecimal<Value>, ColumnVector<Value>>;
|
|
|
|
|
2019-03-15 05:51:11 +00:00
|
|
|
static constexpr bool returns_float = !(std::is_same_v<FloatReturnType, void>);
|
2018-09-12 16:36:13 +00:00
|
|
|
static_assert(!IsDecimalNumber<Value> || !returns_float);
|
2017-12-21 01:19:25 +00:00
|
|
|
|
2017-12-20 08:39:21 +00:00
|
|
|
QuantileLevels<Float64> levels;
|
2017-12-20 20:25:22 +00:00
|
|
|
|
|
|
|
/// Used when there are single level to get.
|
2017-12-20 08:49:56 +00:00
|
|
|
Float64 level = 0.5;
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2019-02-11 19:26:32 +00:00
|
|
|
DataTypePtr & argument_type;
|
2012-10-29 02:58:52 +00:00
|
|
|
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
AggregateFunctionQuantile(const DataTypePtr & argument_type_, const Array & params)
|
|
|
|
: IAggregateFunctionDataHelper<Data, AggregateFunctionQuantile<Value, Data, Name, has_second_arg, FloatReturnType, returns_many>>({argument_type_}, params)
|
2019-02-12 09:31:20 +00:00
|
|
|
, levels(params, returns_many), level(levels.levels[0]), argument_type(this->argument_types[0])
|
2017-12-20 08:14:33 +00:00
|
|
|
{
|
|
|
|
if (!returns_many && levels.size() > 1)
|
2017-12-20 20:58:43 +00:00
|
|
|
throw Exception("Aggregate function " + getName() + " require one parameter or less", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-12-20 08:14:33 +00:00
|
|
|
}
|
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)
|
2017-12-21 01:19:25 +00:00
|
|
|
res = std::make_shared<DataTypeNumber<FloatReturnType>>();
|
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
|
|
|
|
2019-01-24 18:33:57 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena *) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-07-06 23:15:14 +00:00
|
|
|
auto value = static_cast<const ColVecType &>(*columns[0]).getData()[row_num];
|
2019-01-08 15:57:15 +00:00
|
|
|
|
2019-07-06 23:15:14 +00:00
|
|
|
if constexpr (std::is_same_v<Data, QuantileTiming<Value>>)
|
|
|
|
{
|
|
|
|
/// QuantileTiming only supports integers.
|
|
|
|
if (isNaN(value) || value > std::numeric_limits<Value>::max() || value < std::numeric_limits<Value>::min())
|
|
|
|
return;
|
|
|
|
}
|
2019-01-08 15:57:15 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
if constexpr (has_second_arg)
|
2017-12-20 08:49:56 +00:00
|
|
|
this->data(place).add(
|
2019-07-06 23:15:14 +00:00
|
|
|
value,
|
2017-12-20 20:25:22 +00:00
|
|
|
columns[1]->getUInt(row_num));
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
2019-07-06 23:15:14 +00:00
|
|
|
this->data(place).add(value);
|
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 20:25:22 +00:00
|
|
|
/// const_cast is required because some data structures apply finalizaton (like compactization) before serializing.
|
2017-12-20 08:14:33 +00:00
|
|
|
this->data(const_cast<AggregateDataPtr>(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 20:25:22 +00:00
|
|
|
/// const_cast is required because some data structures apply finalizaton (like sorting) for obtain a result.
|
2017-12-20 08:14:33 +00:00
|
|
|
auto & data = this->data(const_cast<AggregateDataPtr>(place));
|
|
|
|
|
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();
|
2018-12-24 14:26:38 +00:00
|
|
|
offsets_to.push_back(offsets_to.back() + size);
|
2017-12-20 07:36:30 +00:00
|
|
|
|
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if constexpr (returns_float)
|
|
|
|
{
|
2017-12-21 01:19:25 +00:00
|
|
|
auto & data_to = static_cast<ColumnVector<FloatReturnType> &>(arr_to.getData()).getData();
|
2017-12-20 07:36:30 +00:00
|
|
|
size_t old_size = data_to.size();
|
|
|
|
data_to.resize(data_to.size() + size);
|
|
|
|
|
2019-03-07 20:04:59 +00:00
|
|
|
data.getManyFloat(levels.levels.data(), levels.permutation.data(), size, data_to.data() + old_size);
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-12 16:36:13 +00:00
|
|
|
auto & data_to = static_cast<ColVecType &>(arr_to.getData()).getData();
|
2017-12-20 07:36:30 +00:00
|
|
|
size_t old_size = data_to.size();
|
|
|
|
data_to.resize(data_to.size() + size);
|
|
|
|
|
2019-03-07 20:04:59 +00:00
|
|
|
data.getMany(levels.levels.data(), levels.permutation.data(), size, data_to.data() + old_size);
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
if constexpr (returns_float)
|
2017-12-21 01:19:25 +00:00
|
|
|
static_cast<ColumnVector<FloatReturnType> &>(to).getData().push_back(data.getFloat(level));
|
2017-12-20 07:36:30 +00:00
|
|
|
else
|
2018-09-12 16:36:13 +00:00
|
|
|
static_cast<ColVecType &>(to).getData().push_back(data.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
|
|
|
|
2019-02-13 11:50:41 +00:00
|
|
|
static void assertSecondArg(const DataTypes & types)
|
2018-11-19 14:25:42 +00:00
|
|
|
{
|
|
|
|
if constexpr (has_second_arg)
|
2019-01-24 18:33:57 +00:00
|
|
|
{
|
2019-02-13 11:50:41 +00:00
|
|
|
assertBinary(Name::name, types);
|
|
|
|
if (!isUnsignedInteger(types[1]))
|
|
|
|
throw Exception("Second argument (weight) for function " + std::string(Name::name) + " must be unsigned integer, but it has type " + types[1]->getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2019-01-24 18:33:57 +00:00
|
|
|
}
|
2018-11-19 14:25:42 +00:00
|
|
|
else
|
2019-02-13 11:50:41 +00:00
|
|
|
assertUnary(Name::name, types);
|
2018-11-19 14:25:42 +00:00
|
|
|
}
|
2018-11-14 15:04:23 +00:00
|
|
|
};
|
2018-02-22 17:32:06 +00:00
|
|
|
|
|
|
|
struct NameQuantile { static constexpr auto name = "quantile"; };
|
|
|
|
struct NameQuantiles { static constexpr auto name = "quantiles"; };
|
|
|
|
struct NameQuantileDeterministic { static constexpr auto name = "quantileDeterministic"; };
|
|
|
|
struct NameQuantilesDeterministic { static constexpr auto name = "quantilesDeterministic"; };
|
|
|
|
|
|
|
|
struct NameQuantileExact { static constexpr auto name = "quantileExact"; };
|
|
|
|
struct NameQuantilesExact { static constexpr auto name = "quantilesExact"; };
|
2019-08-13 19:12:31 +00:00
|
|
|
|
|
|
|
struct NameQuantileExactExclusive { static constexpr auto name = "quantileExactExclusive"; };
|
|
|
|
struct NameQuantilesExactExclusive { static constexpr auto name = "quantilesExactExclusive"; };
|
|
|
|
|
2019-08-14 11:13:04 +00:00
|
|
|
struct NameQuantileExactInclusive { static constexpr auto name = "quantileExactInclusive"; };
|
|
|
|
struct NameQuantilesExactInclusive { static constexpr auto name = "quantilesExactInclusive"; };
|
|
|
|
|
2019-08-13 19:12:31 +00:00
|
|
|
struct NameQuantileExactWeighted { static constexpr auto name = "quantileExactWeighted"; };
|
2018-02-22 17:32:06 +00:00
|
|
|
struct NameQuantilesExactWeighted { static constexpr auto name = "quantilesExactWeighted"; };
|
|
|
|
|
|
|
|
struct NameQuantileTiming { static constexpr auto name = "quantileTiming"; };
|
|
|
|
struct NameQuantileTimingWeighted { static constexpr auto name = "quantileTimingWeighted"; };
|
|
|
|
struct NameQuantilesTiming { static constexpr auto name = "quantilesTiming"; };
|
|
|
|
struct NameQuantilesTimingWeighted { static constexpr auto name = "quantilesTimingWeighted"; };
|
|
|
|
|
|
|
|
struct NameQuantileTDigest { static constexpr auto name = "quantileTDigest"; };
|
|
|
|
struct NameQuantileTDigestWeighted { static constexpr auto name = "quantileTDigestWeighted"; };
|
|
|
|
struct NameQuantilesTDigest { static constexpr auto name = "quantilesTDigest"; };
|
|
|
|
struct NameQuantilesTDigestWeighted { static constexpr auto name = "quantilesTDigestWeighted"; };
|
|
|
|
|
|
|
|
}
|