2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
2017-12-20 20:25:22 +00:00
|
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
2017-07-12 01:16:01 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionStatistics.h>
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionVarPop(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertUnary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionVarPop>(*argument_types[0]));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionVarSamp(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertUnary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionVarSamp>(*argument_types[0]));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionStdDevPop(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertUnary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionStdDevPop>(*argument_types[0]));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionStdDevSamp(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertUnary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionStdDevSamp>(*argument_types[0]));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type " + argument_types[0]->getName() + " of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionCovarPop(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertBinary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithTwoNumericTypes<AggregateFunctionCovarPop>(*argument_types[0], *argument_types[1]));
|
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal types " + argument_types[0]->getName() + " and " + argument_types[1]->getName()
|
|
|
|
+ " of arguments for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionCovarSamp(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertBinary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithTwoNumericTypes<AggregateFunctionCovarSamp>(*argument_types[0], *argument_types[1]));
|
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal types " + argument_types[0]->getName() + " and " + argument_types[1]->getName()
|
|
|
|
+ " of arguments for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionCorr(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:25:22 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertBinary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithTwoNumericTypes<AggregateFunctionCorr>(*argument_types[0], *argument_types[1]));
|
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal types " + argument_types[0]->getName() + " and " + argument_types[1]->getName()
|
|
|
|
+ " of arguments for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionsStatistics(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
factory.registerFunction("varSamp", createAggregateFunctionVarSamp);
|
|
|
|
factory.registerFunction("varPop", createAggregateFunctionVarPop);
|
|
|
|
factory.registerFunction("stddevSamp", createAggregateFunctionStdDevSamp);
|
|
|
|
factory.registerFunction("stddevPop", createAggregateFunctionStdDevPop);
|
|
|
|
factory.registerFunction("covarSamp", createAggregateFunctionCovarSamp);
|
|
|
|
factory.registerFunction("covarPop", createAggregateFunctionCovarPop);
|
|
|
|
factory.registerFunction("corr", createAggregateFunctionCorr, AggregateFunctionFactory::CaseInsensitive);
|
|
|
|
|
|
|
|
/// Synonims for compatibility.
|
|
|
|
factory.registerFunction("VAR_SAMP", createAggregateFunctionVarSamp, AggregateFunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerFunction("VAR_POP", createAggregateFunctionVarPop, AggregateFunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerFunction("STDDEV_SAMP", createAggregateFunctionStdDevSamp, AggregateFunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerFunction("STDDEV_POP", createAggregateFunctionStdDevPop, AggregateFunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerFunction("COVAR_SAMP", createAggregateFunctionCovarSamp, AggregateFunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerFunction("COVAR_POP", createAggregateFunctionCovarPop, AggregateFunctionFactory::CaseInsensitive);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|