2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionGroupUniqArray.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
|
|
|
|
{
|
|
|
|
|
2019-04-15 06:36:24 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2016-09-13 13:24:24 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-12-16 10:57:44 +00:00
|
|
|
/// Substitute return type for Date and DateTime
|
2019-04-15 06:36:24 +00:00
|
|
|
template <typename has_limit>
|
|
|
|
class AggregateFunctionGroupUniqArrayDate : public AggregateFunctionGroupUniqArray<DataTypeDate::FieldType, has_limit>
|
2016-12-16 10:57:44 +00:00
|
|
|
{
|
2019-02-11 19:26:32 +00:00
|
|
|
public:
|
2019-04-15 06:36:24 +00:00
|
|
|
AggregateFunctionGroupUniqArrayDate(const DataTypePtr & argument_type, UInt64 max_elems_ = std::numeric_limits<UInt64>::max()) : AggregateFunctionGroupUniqArray<DataTypeDate::FieldType, has_limit>(argument_type, max_elems_) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDate>()); }
|
2016-12-16 10:57:44 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 06:36:24 +00:00
|
|
|
template <typename has_limit>
|
|
|
|
class AggregateFunctionGroupUniqArrayDateTime : public AggregateFunctionGroupUniqArray<DataTypeDateTime::FieldType, has_limit>
|
2016-12-16 10:57:44 +00:00
|
|
|
{
|
2019-02-11 19:26:32 +00:00
|
|
|
public:
|
2019-04-15 06:36:24 +00:00
|
|
|
AggregateFunctionGroupUniqArrayDateTime(const DataTypePtr & argument_type, UInt64 max_elems_ = std::numeric_limits<UInt64>::max()) : AggregateFunctionGroupUniqArray<DataTypeDateTime::FieldType, has_limit>(argument_type, max_elems_) {}
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDateTime>()); }
|
2016-12-16 10:57:44 +00:00
|
|
|
};
|
|
|
|
|
2019-04-15 06:36:24 +00:00
|
|
|
template <typename has_limit, typename ... TArgs>
|
|
|
|
static IAggregateFunction * createWithExtraTypes(const DataTypePtr & argument_type, TArgs && ... args)
|
2016-09-13 13:24:24 +00:00
|
|
|
{
|
2018-09-10 17:09:07 +00:00
|
|
|
WhichDataType which(argument_type);
|
2019-04-15 06:36:24 +00:00
|
|
|
if (which.idx == TypeIndex::Date) return new AggregateFunctionGroupUniqArrayDate<has_limit>(argument_type, std::forward<TArgs>(args)...);
|
|
|
|
else if (which.idx == TypeIndex::DateTime) return new AggregateFunctionGroupUniqArrayDateTime<has_limit>(argument_type, std::forward<TArgs>(args)...);
|
2017-04-01 07:20:54 +00:00
|
|
|
else
|
|
|
|
{
|
2019-06-26 12:32:53 +00:00
|
|
|
/// Check that we can use plain version of AggregateFunctionGroupUniqArrayGeneric
|
2017-12-20 07:36:30 +00:00
|
|
|
if (argument_type->isValueUnambiguouslyRepresentedInContiguousMemoryRegion())
|
2019-06-26 12:32:53 +00:00
|
|
|
return new AggregateFunctionGroupUniqArrayGeneric<true, has_limit>(argument_type, std::forward<TArgs>(args)...);
|
2017-12-09 06:32:22 +00:00
|
|
|
else
|
2019-06-26 12:32:53 +00:00
|
|
|
return new AggregateFunctionGroupUniqArrayGeneric<false, has_limit>(argument_type, std::forward<TArgs>(args)...);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-09-13 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 06:36:24 +00:00
|
|
|
template <typename has_limit, typename ... TArgs>
|
|
|
|
inline AggregateFunctionPtr createAggregateFunctionGroupUniqArrayImpl(const std::string & name, const DataTypePtr & argument_type, TArgs ... args)
|
|
|
|
{
|
|
|
|
|
2019-04-15 11:10:56 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionGroupUniqArray, has_limit, const DataTypePtr &, TArgs...>(*argument_type, argument_type, std::forward<TArgs>(args)...));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
2019-04-15 06:36:24 +00:00
|
|
|
res = AggregateFunctionPtr(createWithExtraTypes<has_limit>(argument_type, std::forward<TArgs>(args)...));
|
2016-09-13 13:24:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
2019-04-15 06:36:24 +00:00
|
|
|
throw Exception("Illegal type " + argument_type->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;
|
2019-04-15 06:36:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
AggregateFunctionPtr createAggregateFunctionGroupUniqArray(const std::string & name, const DataTypes & argument_types, const Array & parameters)
|
|
|
|
{
|
|
|
|
assertUnary(name, argument_types);
|
|
|
|
|
|
|
|
bool limit_size = false;
|
|
|
|
UInt64 max_elems = std::numeric_limits<UInt64>::max();
|
|
|
|
|
|
|
|
if (parameters.empty())
|
|
|
|
{
|
|
|
|
// no limit
|
|
|
|
}
|
|
|
|
else if (parameters.size() == 1)
|
|
|
|
{
|
|
|
|
auto type = parameters[0].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[0].get<Int64>() < 0) ||
|
|
|
|
(type == Field::Types::UInt64 && parameters[0].get<UInt64>() == 0))
|
|
|
|
throw Exception("Parameter for aggregate function " + name + " should be positive number", ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
limit_size = true;
|
|
|
|
max_elems = parameters[0].get<UInt64>();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw Exception("Incorrect number of parameters for aggregate function " + name + ", should be 0 or 1",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
if (!limit_size)
|
|
|
|
return createAggregateFunctionGroupUniqArrayImpl<std::false_type>(name, argument_types[0]);
|
|
|
|
else
|
|
|
|
return createAggregateFunctionGroupUniqArrayImpl<std::true_type>(name, argument_types[0], max_elems);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionGroupUniqArray(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
factory.registerFunction("groupUniqArray", createAggregateFunctionGroupUniqArray);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|