2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionGroupArray.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
2017-12-20 21:22:04 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-12-15 06:34:43 +00:00
|
|
|
#include "registerAggregateFunctions.h"
|
2017-12-20 07:36:30 +00:00
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2019-03-14 23:10:51 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2017-09-15 12:16:12 +00:00
|
|
|
template <template <typename, typename> class AggregateFunctionTemplate, typename Data, typename ... TArgs>
|
2017-08-11 23:25:03 +00:00
|
|
|
static IAggregateFunction * createWithNumericOrTimeType(const IDataType & argument_type, TArgs && ... args)
|
|
|
|
{
|
2018-09-10 17:09:07 +00:00
|
|
|
WhichDataType which(argument_type);
|
|
|
|
if (which.idx == TypeIndex::Date) return new AggregateFunctionTemplate<UInt16, Data>(std::forward<TArgs>(args)...);
|
|
|
|
if (which.idx == TypeIndex::DateTime) return new AggregateFunctionTemplate<UInt32, Data>(std::forward<TArgs>(args)...);
|
2017-12-20 20:58:43 +00:00
|
|
|
return createWithNumericType<AggregateFunctionTemplate, Data, TArgs...>(argument_type, std::forward<TArgs>(args)...);
|
2017-08-11 23:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-19 07:42:46 +00:00
|
|
|
template <typename Trait, typename ... TArgs>
|
2017-08-11 23:25:03 +00:00
|
|
|
inline AggregateFunctionPtr createAggregateFunctionGroupArrayImpl(const DataTypePtr & argument_type, TArgs ... args)
|
|
|
|
{
|
2019-12-19 07:42:46 +00:00
|
|
|
if (auto res = createWithNumericOrTimeType<GroupArrayNumericImpl, Trait>(*argument_type, argument_type, std::forward<TArgs>(args)...))
|
2017-08-11 23:25:03 +00:00
|
|
|
return AggregateFunctionPtr(res);
|
|
|
|
|
2018-09-10 17:09:07 +00:00
|
|
|
WhichDataType which(argument_type);
|
|
|
|
if (which.idx == TypeIndex::String)
|
2019-12-19 07:42:46 +00:00
|
|
|
return std::make_shared<GroupArrayGeneralImpl<GroupArrayNodeString, Trait>>(argument_type, std::forward<TArgs>(args)...);
|
2017-08-11 23:25:03 +00:00
|
|
|
|
2019-12-19 07:42:46 +00:00
|
|
|
return std::make_shared<GroupArrayGeneralImpl<GroupArrayNodeGeneral, Trait>>(argument_type, std::forward<TArgs>(args)...);
|
|
|
|
|
|
|
|
// Link list implementation doesn't show noticeable performance improvement
|
|
|
|
// if (which.idx == TypeIndex::String)
|
|
|
|
// return std::make_shared<GroupArrayGeneralListImpl<GroupArrayListNodeString, Trait>>(argument_type, std::forward<TArgs>(args)...);
|
|
|
|
|
|
|
|
// return std::make_shared<GroupArrayGeneralListImpl<GroupArrayListNodeGeneral, Trait>>(argument_type, std::forward<TArgs>(args)...);
|
2018-08-26 00:51:37 +00:00
|
|
|
}
|
2017-08-11 23:25:03 +00:00
|
|
|
|
|
|
|
|
2020-03-09 03:38:43 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionGroupArray(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2017-06-26 12:16:29 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
assertUnary(name, argument_types);
|
2017-06-26 12:16:29 +00:00
|
|
|
|
2017-07-10 23:30:17 +00:00
|
|
|
bool limit_size = false;
|
2017-08-11 23:25:03 +00:00
|
|
|
UInt64 max_elems = std::numeric_limits<UInt64>::max();
|
2017-07-10 23:30:17 +00:00
|
|
|
|
2017-07-11 17:43:51 +00:00
|
|
|
if (parameters.empty())
|
|
|
|
{
|
|
|
|
// no limit
|
|
|
|
}
|
2017-07-10 23:30:17 +00:00
|
|
|
else if (parameters.size() == 1)
|
|
|
|
{
|
2017-07-14 13:09:52 +00:00
|
|
|
auto type = parameters[0].getType();
|
2018-11-26 00:56:50 +00:00
|
|
|
if (type != Field::Types::Int64 && type != Field::Types::UInt64)
|
2017-07-14 13:09:52 +00:00
|
|
|
throw Exception("Parameter for aggregate function " + name + " should be positive number", ErrorCodes::BAD_ARGUMENTS);
|
2017-07-10 23:30:17 +00:00
|
|
|
|
2018-11-26 00:56:50 +00:00
|
|
|
if ((type == Field::Types::Int64 && parameters[0].get<Int64>() < 0) ||
|
2017-07-14 13:09:52 +00:00
|
|
|
(type == Field::Types::UInt64 && parameters[0].get<UInt64>() == 0))
|
2017-07-10 23:30:17 +00:00
|
|
|
throw Exception("Parameter for aggregate function " + name + " should be positive number", ErrorCodes::BAD_ARGUMENTS);
|
2017-07-14 13:09:52 +00:00
|
|
|
|
|
|
|
limit_size = true;
|
|
|
|
max_elems = parameters[0].get<UInt64>();
|
2017-07-10 23:30:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Incorrect number of parameters for aggregate function " + name + ", should be 0 or 1",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-06-26 12:16:29 +00:00
|
|
|
|
2017-07-10 23:30:17 +00:00
|
|
|
if (!limit_size)
|
2019-12-19 07:42:46 +00:00
|
|
|
return createAggregateFunctionGroupArrayImpl<GroupArrayTrait<false, Sampler::NONE>>(argument_types[0]);
|
2017-06-26 12:16:29 +00:00
|
|
|
else
|
2019-12-19 07:42:46 +00:00
|
|
|
return createAggregateFunctionGroupArrayImpl<GroupArrayTrait<true, Sampler::NONE>>(argument_types[0], max_elems);
|
|
|
|
}
|
|
|
|
|
2020-03-09 03:38:43 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionGroupArraySample(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
2019-12-19 07:42:46 +00:00
|
|
|
{
|
|
|
|
assertUnary(name, argument_types);
|
|
|
|
|
|
|
|
if (parameters.size() != 1 && parameters.size() != 2)
|
|
|
|
throw Exception("Incorrect number of parameters for aggregate function " + name + ", should be 1 or 2",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2020-03-19 23:48:53 +00:00
|
|
|
auto get_parameter = [&](size_t i)
|
2019-12-19 07:42:46 +00:00
|
|
|
{
|
|
|
|
auto type = parameters[i].getType();
|
|
|
|
if (type != Field::Types::Int64 && type != Field::Types::UInt64)
|
|
|
|
throw Exception("Parameter for aggregate function " + name + " should be positive number", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
if ((type == Field::Types::Int64 && parameters[i].get<Int64>() < 0) ||
|
|
|
|
(type == Field::Types::UInt64 && parameters[i].get<UInt64>() == 0))
|
|
|
|
throw Exception("Parameter for aggregate function " + name + " should be positive number", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
2020-03-19 23:48:53 +00:00
|
|
|
return parameters[i].get<UInt64>();
|
|
|
|
};
|
|
|
|
|
|
|
|
UInt64 max_elems = get_parameter(0);
|
|
|
|
|
|
|
|
UInt64 seed;
|
|
|
|
if (parameters.size() >= 2)
|
|
|
|
seed = get_parameter(1);
|
|
|
|
else
|
|
|
|
seed = thread_local_rng();
|
2019-12-19 07:42:46 +00:00
|
|
|
|
|
|
|
return createAggregateFunctionGroupArrayImpl<GroupArrayTrait<true, Sampler::RNG>>(argument_types[0], max_elems, seed);
|
2017-06-26 12:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
|
|
|
|
void registerAggregateFunctionGroupArray(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
factory.registerFunction("groupArray", createAggregateFunctionGroupArray);
|
2019-12-19 07:42:46 +00:00
|
|
|
factory.registerFunction("groupArraySample", createAggregateFunctionGroupArraySample);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|