2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionAvg.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
2015-09-24 12:40:36 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2018-09-12 13:27:32 +00:00
|
|
|
template <typename T>
|
|
|
|
struct Avg
|
|
|
|
{
|
2018-11-20 20:09:20 +00:00
|
|
|
using FieldType = std::conditional_t<IsDecimalNumber<T>, Decimal128, NearestFieldType<T>>;
|
2018-09-12 13:27:32 +00:00
|
|
|
using Function = AggregateFunctionAvg<T, AggregateFunctionAvgData<FieldType>>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using AggregateFuncAvg = typename Avg<T>::Function;
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionAvg(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
assertNoParameters(name, parameters);
|
2017-12-20 08:14:33 +00:00
|
|
|
assertUnary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2018-09-12 13:27:32 +00:00
|
|
|
AggregateFunctionPtr res;
|
|
|
|
DataTypePtr data_type = argument_types[0];
|
|
|
|
if (isDecimal(data_type))
|
2019-02-12 09:31:20 +00:00
|
|
|
res.reset(createWithDecimalType<AggregateFuncAvg>(*data_type, *data_type, argument_types));
|
2018-09-12 13:27:32 +00:00
|
|
|
else
|
2019-02-12 09:31:20 +00:00
|
|
|
res.reset(createWithNumericType<AggregateFuncAvg>(*data_type, argument_types));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
2018-09-12 13:27:32 +00:00
|
|
|
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name,
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionAvg(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
factory.registerFunction("avg", createAggregateFunctionAvg, AggregateFunctionFactory::CaseInsensitive);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|