ClickHouse
62 строки · 2.5 Кб
1#include <AggregateFunctions/AggregateFunctionQuantile.h>2#include <AggregateFunctions/QuantileDD.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 FuncQuantileDD = AggregateFunctionQuantile<Value, QuantileDD<Value>, NameQuantileDD, false, std::conditional_t<float_return, Float64, void>, false, true>;24template <typename Value, bool float_return> using FuncQuantilesDD = AggregateFunctionQuantile<Value, QuantileDD<Value>, NameQuantilesDD, false, std::conditional_t<float_return, Float64, void>, true, true>;25
26
27template <template <typename, bool> class Function>28AggregateFunctionPtr createAggregateFunctionQuantile(29const std::string & name, const DataTypes & argument_types, const Array & params, const Settings *)30{
31if (argument_types.empty())32throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Aggregate function {} requires at least one argument", name);33
34const DataTypePtr & argument_type = argument_types[0];35WhichDataType which(argument_type);36
37#define DISPATCH(TYPE) \38if (which.idx == TypeIndex::TYPE) return std::make_shared<Function<TYPE, true>>(argument_types, params);39FOR_BASIC_NUMERIC_TYPES(DISPATCH)40#undef DISPATCH41if (which.idx == TypeIndex::Date) return std::make_shared<Function<DataTypeDate::FieldType, false>>(argument_types, params);42if (which.idx == TypeIndex::DateTime) return std::make_shared<Function<DataTypeDateTime::FieldType, false>>(argument_types, params);43
44throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument for aggregate function {}",45argument_type->getName(), name);46}
47
48}
49
50void registerAggregateFunctionsQuantileDD(AggregateFunctionFactory & factory)51{
52/// For aggregate functions returning array we cannot return NULL on empty set.53AggregateFunctionProperties properties = { .returns_default_when_only_null = true };54
55factory.registerFunction(NameQuantileDD::name, createAggregateFunctionQuantile<FuncQuantileDD>);56factory.registerFunction(NameQuantilesDD::name, { createAggregateFunctionQuantile<FuncQuantilesDD>, properties });57
58/// 'median' is an alias for 'quantile'59factory.registerAlias("medianDD", NameQuantileDD::name);60}
61
62}
63