2017-09-15 11:14:19 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionSumMap.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
2017-12-20 20:58:43 +00:00
|
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
2017-09-15 11:14:19 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2017-12-20 20:58:43 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionSumMap(const std::string & name, const DataTypes & arguments, const Array & params)
|
2017-09-15 11:14:19 +00:00
|
|
|
{
|
2017-12-20 20:58:43 +00:00
|
|
|
assertNoParameters(name, params);
|
|
|
|
|
|
|
|
if (arguments.size() < 2)
|
|
|
|
throw Exception("Aggregate function " + name + " requires at least two arguments of Array type.",
|
2017-09-15 11:14:19 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2017-12-20 20:58:43 +00:00
|
|
|
const auto * array_type = checkAndGetDataType<DataTypeArray>(arguments[0].get());
|
2017-10-12 04:13:33 +00:00
|
|
|
if (!array_type)
|
|
|
|
throw Exception("First argument for function " + name + " must be an array.",
|
2017-12-20 20:58:43 +00:00
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
const DataTypePtr & keys_type = array_type->getNestedType();
|
|
|
|
|
|
|
|
DataTypes values_types;
|
|
|
|
for (size_t i = 1; i < arguments.size(); ++i)
|
|
|
|
{
|
|
|
|
array_type = checkAndGetDataType<DataTypeArray>(arguments[i].get());
|
|
|
|
if (!array_type)
|
|
|
|
throw Exception("Argument #" + toString(i) + " for function " + name + " must be an array.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
values_types.push_back(array_type->getNestedType());
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:22:28 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericBasedType<AggregateFunctionSumMap>(*keys_type, keys_type, values_types));
|
|
|
|
if (!res)
|
|
|
|
res.reset(createWithDecimalType<AggregateFunctionSumMap>(*keys_type, keys_type, values_types));
|
2018-03-22 15:37:24 +00:00
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return res;
|
2017-09-15 11:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionSumMap(AggregateFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction("sumMap", createAggregateFunctionSumMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|