ClickHouse
61 строка · 2.7 Кб
1#include <AggregateFunctions/AggregateFunctionQuantile.h>2#include <AggregateFunctions/QuantileTDigest.h>3#include <AggregateFunctions/AggregateFunctionFactory.h>4#include <AggregateFunctions/Helpers.h>5#include <DataTypes/DataTypeDate.h>6#include <DataTypes/DataTypeDateTime.h>7#include <Core/Field.h>8
9
10namespace DB11{
12struct Settings;13
14namespace ErrorCodes15{
16extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;17extern const int ILLEGAL_TYPE_OF_ARGUMENT;18}
19
20namespace
21{
22
23template <typename Value, bool float_return> using FuncQuantileTDigestWeighted = AggregateFunctionQuantile<Value, QuantileTDigest<Value>, NameQuantileTDigestWeighted, true, std::conditional_t<float_return, Float32, void>, false, false>;24template <typename Value, bool float_return> using FuncQuantilesTDigestWeighted = AggregateFunctionQuantile<Value, QuantileTDigest<Value>, NameQuantilesTDigestWeighted, true, std::conditional_t<float_return, Float32, void>, true, false>;25
26template <template <typename, bool> class Function>27AggregateFunctionPtr createAggregateFunctionQuantile(28const std::string & name, const DataTypes & argument_types, const Array & params, const Settings *)29{
30if (argument_types.empty())31throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Aggregate function {} requires at least one argument", name);32
33const DataTypePtr & argument_type = argument_types[0];34WhichDataType which(argument_type);35
36#define DISPATCH(TYPE) \37if (which.idx == TypeIndex::TYPE) return std::make_shared<Function<TYPE, true>>(argument_types, params);38FOR_BASIC_NUMERIC_TYPES(DISPATCH)39#undef DISPATCH40if (which.idx == TypeIndex::Date) return std::make_shared<Function<DataTypeDate::FieldType, false>>(argument_types, params);41if (which.idx == TypeIndex::DateTime) return std::make_shared<Function<DataTypeDateTime::FieldType, false>>(argument_types, params);42
43throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument for aggregate function {}",44argument_type->getName(), name);45}
46
47}
48
49void registerAggregateFunctionsQuantileTDigestWeighted(AggregateFunctionFactory & factory)50{
51/// For aggregate functions returning array we cannot return NULL on empty set.52AggregateFunctionProperties properties = { .returns_default_when_only_null = true };53
54factory.registerFunction(NameQuantileTDigestWeighted::name, createAggregateFunctionQuantile<FuncQuantileTDigestWeighted>);55factory.registerFunction(NameQuantilesTDigestWeighted::name, { createAggregateFunctionQuantile<FuncQuantilesTDigestWeighted>, properties });56
57/// 'median' is an alias for 'quantile'58factory.registerAlias("medianTDigestWeighted", NameQuantileTDigestWeighted::name);59}
60
61}
62