2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionSum.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
|
|
|
|
{
|
2021-05-26 11:32:14 +00:00
|
|
|
struct Settings;
|
|
|
|
|
2020-02-25 18:10:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
2015-09-24 12:40:36 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2017-12-22 23:26:30 +00:00
|
|
|
template <typename T>
|
2018-09-11 18:42:06 +00:00
|
|
|
struct SumSimple
|
|
|
|
{
|
|
|
|
/// @note It uses slow Decimal128 (cause we need such a variant). sumWithOverflow is faster for Decimal32/64
|
2021-09-10 11:49:22 +00:00
|
|
|
using ResultType = std::conditional_t<is_decimal<T>,
|
2020-08-19 11:52:17 +00:00
|
|
|
std::conditional_t<std::is_same_v<T, Decimal256>, Decimal256, Decimal128>,
|
|
|
|
NearestFieldType<T>>;
|
2018-09-11 18:42:06 +00:00
|
|
|
using AggregateDataType = AggregateFunctionSumData<ResultType>;
|
2020-03-12 19:13:38 +00:00
|
|
|
using Function = AggregateFunctionSum<T, ResultType, AggregateDataType, AggregateFunctionTypeSum>;
|
2018-09-11 18:42:06 +00:00
|
|
|
};
|
2017-10-17 22:57:00 +00:00
|
|
|
|
2017-12-22 23:26:30 +00:00
|
|
|
template <typename T>
|
2018-09-11 18:42:06 +00:00
|
|
|
struct SumSameType
|
|
|
|
{
|
|
|
|
using ResultType = T;
|
|
|
|
using AggregateDataType = AggregateFunctionSumData<ResultType>;
|
2020-03-12 19:13:38 +00:00
|
|
|
using Function = AggregateFunctionSum<T, ResultType, AggregateDataType, AggregateFunctionTypeSumWithOverflow>;
|
2018-09-11 18:42:06 +00:00
|
|
|
};
|
2017-10-17 22:57:00 +00:00
|
|
|
|
2017-12-22 23:26:30 +00:00
|
|
|
template <typename T>
|
2018-09-11 18:42:06 +00:00
|
|
|
struct SumKahan
|
|
|
|
{
|
|
|
|
using ResultType = Float64;
|
|
|
|
using AggregateDataType = AggregateFunctionSumKahanData<ResultType>;
|
2020-03-12 19:13:38 +00:00
|
|
|
using Function = AggregateFunctionSum<T, ResultType, AggregateDataType, AggregateFunctionTypeSumKahan>;
|
2018-09-11 18:42:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> using AggregateFunctionSumSimple = typename SumSimple<T>::Function;
|
|
|
|
template <typename T> using AggregateFunctionSumWithOverflow = typename SumSameType<T>::Function;
|
|
|
|
template <typename T> using AggregateFunctionSumKahan =
|
2021-09-10 11:49:22 +00:00
|
|
|
std::conditional_t<is_decimal<T>, typename SumSimple<T>::Function, typename SumKahan<T>::Function>;
|
2017-10-17 22:57:00 +00:00
|
|
|
|
|
|
|
|
2017-12-22 23:26:30 +00:00
|
|
|
template <template <typename> class Function>
|
2021-05-26 11:32:14 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionSum(const std::string & name, const DataTypes & argument_types, const Array & parameters, const Settings *)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertUnary(name, argument_types);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2018-09-11 18:42:06 +00:00
|
|
|
AggregateFunctionPtr res;
|
|
|
|
DataTypePtr data_type = argument_types[0];
|
|
|
|
if (isDecimal(data_type))
|
2019-02-11 19:26:32 +00:00
|
|
|
res.reset(createWithDecimalType<Function>(*data_type, *data_type, argument_types));
|
2018-09-11 18:42:06 +00:00
|
|
|
else
|
2019-02-11 19:26:32 +00:00
|
|
|
res.reset(createWithNumericType<Function>(*data_type, argument_types));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
2018-09-11 18:42:06 +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 registerAggregateFunctionSum(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2017-12-22 23:26:30 +00:00
|
|
|
factory.registerFunction("sum", createAggregateFunctionSum<AggregateFunctionSumSimple>, AggregateFunctionFactory::CaseInsensitive);
|
|
|
|
factory.registerFunction("sumWithOverflow", createAggregateFunctionSum<AggregateFunctionSumWithOverflow>);
|
|
|
|
factory.registerFunction("sumKahan", createAggregateFunctionSum<AggregateFunctionSumKahan>);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|