2018-11-19 14:25:42 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionQuantile.h>
|
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
|
|
|
|
2019-10-04 17:46:36 +00:00
|
|
|
#include <Core/Field.h>
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerAggregateFunctions.h"
|
2017-12-20 07:36:30 +00:00
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-10-25 03:25:02 +00:00
|
|
|
template <typename Value, bool float_return> using FuncQuantile = AggregateFunctionQuantile<Value, QuantileReservoirSampler<Value>, NameQuantile, false, std::conditional_t<float_return, Float64, void>, false>;
|
|
|
|
template <typename Value, bool float_return> using FuncQuantiles = AggregateFunctionQuantile<Value, QuantileReservoirSampler<Value>, NameQuantiles, false, std::conditional_t<float_return, Float64, void>, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-10-25 03:25:02 +00:00
|
|
|
template <typename Value, bool float_return> using FuncQuantileDeterministic = AggregateFunctionQuantile<Value, QuantileReservoirSamplerDeterministic<Value>, NameQuantileDeterministic, true, std::conditional_t<float_return, Float64, void>, false>;
|
|
|
|
template <typename Value, bool float_return> using FuncQuantilesDeterministic = AggregateFunctionQuantile<Value, QuantileReservoirSamplerDeterministic<Value>, NameQuantilesDeterministic, true, std::conditional_t<float_return, Float64, void>, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-03-15 05:51:11 +00:00
|
|
|
template <typename Value, bool _> using FuncQuantileExact = AggregateFunctionQuantile<Value, QuantileExact<Value>, NameQuantileExact, false, void, false>;
|
|
|
|
template <typename Value, bool _> using FuncQuantilesExact = AggregateFunctionQuantile<Value, QuantileExact<Value>, NameQuantilesExact, false, void, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-08-13 19:12:31 +00:00
|
|
|
template <typename Value, bool _> using FuncQuantileExactExclusive = AggregateFunctionQuantile<Value, QuantileExactExclusive<Value>, NameQuantileExactExclusive, false, Float64, false>;
|
|
|
|
template <typename Value, bool _> using FuncQuantilesExactExclusive = AggregateFunctionQuantile<Value, QuantileExactExclusive<Value>, NameQuantilesExactExclusive, false, Float64, true>;
|
|
|
|
|
2019-08-14 11:13:04 +00:00
|
|
|
template <typename Value, bool _> using FuncQuantileExactInclusive = AggregateFunctionQuantile<Value, QuantileExactInclusive<Value>, NameQuantileExactInclusive, false, Float64, false>;
|
|
|
|
template <typename Value, bool _> using FuncQuantilesExactInclusive = AggregateFunctionQuantile<Value, QuantileExactInclusive<Value>, NameQuantilesExactInclusive, false, Float64, true>;
|
|
|
|
|
2019-03-15 05:51:11 +00:00
|
|
|
template <typename Value, bool _> using FuncQuantileExactWeighted = AggregateFunctionQuantile<Value, QuantileExactWeighted<Value>, NameQuantileExactWeighted, true, void, false>;
|
|
|
|
template <typename Value, bool _> using FuncQuantilesExactWeighted = AggregateFunctionQuantile<Value, QuantileExactWeighted<Value>, NameQuantilesExactWeighted, true, void, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-03-15 05:51:11 +00:00
|
|
|
template <typename Value, bool _> using FuncQuantileTiming = AggregateFunctionQuantile<Value, QuantileTiming<Value>, NameQuantileTiming, false, Float32, false>;
|
|
|
|
template <typename Value, bool _> using FuncQuantilesTiming = AggregateFunctionQuantile<Value, QuantileTiming<Value>, NameQuantilesTiming, false, Float32, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-03-15 05:51:11 +00:00
|
|
|
template <typename Value, bool _> using FuncQuantileTimingWeighted = AggregateFunctionQuantile<Value, QuantileTiming<Value>, NameQuantileTimingWeighted, true, Float32, false>;
|
|
|
|
template <typename Value, bool _> using FuncQuantilesTimingWeighted = AggregateFunctionQuantile<Value, QuantileTiming<Value>, NameQuantilesTimingWeighted, true, Float32, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-10-25 03:25:02 +00:00
|
|
|
template <typename Value, bool float_return> using FuncQuantileTDigest = AggregateFunctionQuantile<Value, QuantileTDigest<Value>, NameQuantileTDigest, false, std::conditional_t<float_return, Float32, void>, false>;
|
|
|
|
template <typename Value, bool float_return> using FuncQuantilesTDigest = AggregateFunctionQuantile<Value, QuantileTDigest<Value>, NameQuantilesTDigest, false, std::conditional_t<float_return, Float32, void>, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-10-25 03:25:02 +00:00
|
|
|
template <typename Value, bool float_return> using FuncQuantileTDigestWeighted = AggregateFunctionQuantile<Value, QuantileTDigest<Value>, NameQuantileTDigestWeighted, true, std::conditional_t<float_return, Float32, void>, false>;
|
|
|
|
template <typename Value, bool float_return> using FuncQuantilesTDigestWeighted = AggregateFunctionQuantile<Value, QuantileTDigest<Value>, NameQuantilesTDigestWeighted, true, std::conditional_t<float_return, Float32, void>, true>;
|
2018-09-12 16:36:13 +00:00
|
|
|
|
|
|
|
|
2019-03-15 05:51:11 +00:00
|
|
|
template <template <typename, bool> class Function>
|
2019-02-09 20:17:20 +00:00
|
|
|
static constexpr bool supportDecimal()
|
2018-09-12 16:36:13 +00:00
|
|
|
{
|
2019-05-16 12:53:33 +00:00
|
|
|
return std::is_same_v<Function<Float32, false>, FuncQuantile<Float32, false>> ||
|
|
|
|
std::is_same_v<Function<Float32, false>, FuncQuantiles<Float32, false>> ||
|
|
|
|
std::is_same_v<Function<Float32, false>, FuncQuantileExact<Float32, false>> ||
|
2019-05-16 14:33:13 +00:00
|
|
|
std::is_same_v<Function<Float32, false>, FuncQuantilesExact<Float32, false>> ||
|
|
|
|
std::is_same_v<Function<Float32, false>, FuncQuantileExactWeighted<Float32, false>> ||
|
|
|
|
std::is_same_v<Function<Float32, false>, FuncQuantilesExactWeighted<Float32, false>>;
|
2018-09-12 16:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 05:51:11 +00:00
|
|
|
template <template <typename, bool> class Function>
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionQuantile(const std::string & name, const DataTypes & argument_types, const Array & params)
|
2017-07-12 01:16:01 +00:00
|
|
|
{
|
2018-11-19 14:25:42 +00:00
|
|
|
/// Second argument type check doesn't depend on the type of the first one.
|
2019-03-15 05:51:11 +00:00
|
|
|
Function<void, true>::assertSecondArg(argument_types);
|
2017-12-20 20:58:43 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
const DataTypePtr & argument_type = argument_types[0];
|
2018-09-10 17:09:07 +00:00
|
|
|
WhichDataType which(argument_type);
|
2017-12-20 08:14:33 +00:00
|
|
|
|
2018-09-12 16:36:13 +00:00
|
|
|
#define DISPATCH(TYPE) \
|
2019-03-15 05:51:11 +00:00
|
|
|
if (which.idx == TypeIndex::TYPE) return std::make_shared<Function<TYPE, true>>(argument_type, params);
|
2018-09-12 16:36:13 +00:00
|
|
|
FOR_NUMERIC_TYPES(DISPATCH)
|
|
|
|
#undef DISPATCH
|
2019-03-15 05:51:11 +00:00
|
|
|
if (which.idx == TypeIndex::Date) return std::make_shared<Function<DataTypeDate::FieldType, false>>(argument_type, params);
|
|
|
|
if (which.idx == TypeIndex::DateTime) return std::make_shared<Function<DataTypeDateTime::FieldType, false>>(argument_type, params);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-02-09 20:17:20 +00:00
|
|
|
if constexpr (supportDecimal<Function>())
|
2018-09-12 16:36:13 +00:00
|
|
|
{
|
2019-05-16 12:53:33 +00:00
|
|
|
if (which.idx == TypeIndex::Decimal32) return std::make_shared<Function<Decimal32, false>>(argument_type, params);
|
|
|
|
if (which.idx == TypeIndex::Decimal64) return std::make_shared<Function<Decimal64, false>>(argument_type, params);
|
|
|
|
if (which.idx == TypeIndex::Decimal128) return std::make_shared<Function<Decimal128, false>>(argument_type, params);
|
2018-09-12 16:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw Exception("Illegal type " + argument_type->getName() + " of argument for aggregate function " + name,
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
2017-12-20 08:14:33 +00:00
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionsQuantile(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantile::name, createAggregateFunctionQuantile<FuncQuantile>);
|
|
|
|
factory.registerFunction(NameQuantiles::name, createAggregateFunctionQuantile<FuncQuantiles>);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantileDeterministic::name, createAggregateFunctionQuantile<FuncQuantileDeterministic>);
|
|
|
|
factory.registerFunction(NameQuantilesDeterministic::name, createAggregateFunctionQuantile<FuncQuantilesDeterministic>);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantileExact::name, createAggregateFunctionQuantile<FuncQuantileExact>);
|
|
|
|
factory.registerFunction(NameQuantilesExact::name, createAggregateFunctionQuantile<FuncQuantilesExact>);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2019-08-13 19:12:31 +00:00
|
|
|
factory.registerFunction(NameQuantileExactExclusive::name, createAggregateFunctionQuantile<FuncQuantileExactExclusive>);
|
|
|
|
factory.registerFunction(NameQuantilesExactExclusive::name, createAggregateFunctionQuantile<FuncQuantilesExactExclusive>);
|
|
|
|
|
2019-08-14 11:13:04 +00:00
|
|
|
factory.registerFunction(NameQuantileExactInclusive::name, createAggregateFunctionQuantile<FuncQuantileExactInclusive>);
|
|
|
|
factory.registerFunction(NameQuantilesExactInclusive::name, createAggregateFunctionQuantile<FuncQuantilesExactInclusive>);
|
2019-08-13 19:12:31 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantileExactWeighted::name, createAggregateFunctionQuantile<FuncQuantileExactWeighted>);
|
|
|
|
factory.registerFunction(NameQuantilesExactWeighted::name, createAggregateFunctionQuantile<FuncQuantilesExactWeighted>);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantileTiming::name, createAggregateFunctionQuantile<FuncQuantileTiming>);
|
|
|
|
factory.registerFunction(NameQuantilesTiming::name, createAggregateFunctionQuantile<FuncQuantilesTiming>);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantileTimingWeighted::name, createAggregateFunctionQuantile<FuncQuantileTimingWeighted>);
|
|
|
|
factory.registerFunction(NameQuantilesTimingWeighted::name, createAggregateFunctionQuantile<FuncQuantilesTimingWeighted>);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantileTDigest::name, createAggregateFunctionQuantile<FuncQuantileTDigest>);
|
|
|
|
factory.registerFunction(NameQuantilesTDigest::name, createAggregateFunctionQuantile<FuncQuantilesTDigest>);
|
2018-09-12 16:36:13 +00:00
|
|
|
|
2018-11-14 15:04:23 +00:00
|
|
|
factory.registerFunction(NameQuantileTDigestWeighted::name, createAggregateFunctionQuantile<FuncQuantileTDigestWeighted>);
|
|
|
|
factory.registerFunction(NameQuantilesTDigestWeighted::name, createAggregateFunctionQuantile<FuncQuantilesTDigestWeighted>);
|
2017-12-20 20:25:22 +00:00
|
|
|
|
|
|
|
/// 'median' is an alias for 'quantile'
|
2018-07-25 16:08:23 +00:00
|
|
|
factory.registerAlias("median", NameQuantile::name);
|
|
|
|
factory.registerAlias("medianDeterministic", NameQuantileDeterministic::name);
|
|
|
|
factory.registerAlias("medianExact", NameQuantileExact::name);
|
|
|
|
factory.registerAlias("medianExactWeighted", NameQuantileExactWeighted::name);
|
|
|
|
factory.registerAlias("medianTiming", NameQuantileTiming::name);
|
|
|
|
factory.registerAlias("medianTimingWeighted", NameQuantileTimingWeighted::name);
|
|
|
|
factory.registerAlias("medianTDigest", NameQuantileTDigest::name);
|
|
|
|
factory.registerAlias("medianTDigestWeighted", NameQuantileTDigestWeighted::name);
|
2017-07-12 01:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|