mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-03 13:02:00 +00:00
51 lines
1.4 KiB
C++
51 lines
1.4 KiB
C++
#include <memory>
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
#include <AggregateFunctions/AggregateFunctionAvg.h>
|
|
#include <AggregateFunctions/Helpers.h>
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
|
#include "registerAggregateFunctions.h"
|
|
|
|
namespace DB
|
|
{
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
bool allowType(const DataTypePtr& type) noexcept
|
|
{
|
|
const WhichDataType t(type);
|
|
return t.isInt() || t.isUInt() || t.isFloat() || t.isDecimal();
|
|
}
|
|
|
|
AggregateFunctionPtr createAggregateFunctionAvg(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
|
{
|
|
assertNoParameters(name, parameters);
|
|
assertUnary(name, argument_types);
|
|
|
|
const DataTypePtr& data_type = argument_types[0];
|
|
|
|
if (!allowType(data_type))
|
|
throw Exception("Illegal type " + data_type->getName() + " of argument for aggregate function " + name,
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
AggregateFunctionPtr res;
|
|
|
|
if (isDecimal(data_type))
|
|
res.reset(createWithDecimalType<AggregateFunctionAvg>(
|
|
*data_type, argument_types, getDecimalScale(*data_type)));
|
|
else
|
|
res.reset(createWithNumericType<AggregateFunctionAvg>(*data_type, argument_types));
|
|
|
|
return res;
|
|
}
|
|
}
|
|
|
|
void registerAggregateFunctionAvg(AggregateFunctionFactory & factory)
|
|
{
|
|
factory.registerFunction("avg", createAggregateFunctionAvg, AggregateFunctionFactory::CaseInsensitive);
|
|
}
|
|
}
|