ClickHouse/dbms/src/AggregateFunctions/AggregateFunctionSum.cpp

77 lines
2.8 KiB
C++
Raw Normal View History

#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/AggregateFunctionSum.h>
#include <AggregateFunctions/Helpers.h>
#include <AggregateFunctions/FactoryHelpers.h>
2015-09-24 12:40:36 +00:00
namespace DB
{
namespace
{
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
using ResultType = std::conditional_t<IsDecimalNumber<T>, Decimal128, typename NearestFieldType<T>::Type>;
using AggregateDataType = AggregateFunctionSumData<ResultType>;
using Function = std::conditional_t<IsDecimalNumber<T>,
AggregateFunctionDecimalSum<T, ResultType, AggregateDataType>,
AggregateFunctionSum<T, ResultType, AggregateDataType>>;
};
template <typename T>
2018-09-11 18:42:06 +00:00
struct SumSameType
{
using ResultType = T;
using AggregateDataType = AggregateFunctionSumData<ResultType>;
using Function = std::conditional_t<IsDecimalNumber<T>,
AggregateFunctionDecimalSum<T, ResultType, AggregateDataType>,
AggregateFunctionSum<T, ResultType, AggregateDataType>>;
};
template <typename T>
2018-09-11 18:42:06 +00:00
struct SumKahan
{
using ResultType = Float64;
using AggregateDataType = AggregateFunctionSumKahanData<ResultType>;
using Function = AggregateFunctionSum<T, ResultType, AggregateDataType>;
};
template <typename T> using AggregateFunctionSumSimple = typename SumSimple<T>::Function;
template <typename T> using AggregateFunctionSumWithOverflow = typename SumSameType<T>::Function;
template <typename T> using AggregateFunctionSumKahan =
std::conditional_t<IsDecimalNumber<T>, typename SumSimple<T>::Function, typename SumKahan<T>::Function>;
template <template <typename> class Function>
AggregateFunctionPtr createAggregateFunctionSum(const std::string & name, const DataTypes & argument_types, const Array & parameters)
2015-09-24 12:40:36 +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))
res.reset(createWithDecimalType<Function>(*data_type));
else
res.reset(createWithNumericType<Function>(*data_type));
2015-09-24 12:40:36 +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);
return res;
2015-09-24 12:40:36 +00:00
}
}
void registerAggregateFunctionSum(AggregateFunctionFactory & factory)
{
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
}
}