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>
|
2017-12-20 07:36:30 +00:00
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-05-26 11:32:14 +00:00
|
|
|
struct Settings;
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2019-04-15 06:36:24 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2020-02-25 18:02:41 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2019-04-15 06:36:24 +00:00
|
|
|
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
|
2020-03-23 02:12:31 +00:00
|
|
|
template <typename HasLimit>
|
|
|
|
class AggregateFunctionGroupUniqArrayDate : public AggregateFunctionGroupUniqArray<DataTypeDate::FieldType, HasLimit>
|
2016-12-16 10:57:44 +00:00
|
|
|
{
|
2019-02-11 19:26:32 +00:00
|
|
|
public:
|
2021-07-26 14:57:49 +00:00
|
|
|
explicit AggregateFunctionGroupUniqArrayDate(const DataTypePtr & argument_type, const Array & parameters_, UInt64 max_elems_ = std::numeric_limits<UInt64>::max())
|
|
|
|
: AggregateFunctionGroupUniqArray<DataTypeDate::FieldType, HasLimit>(argument_type, parameters_, 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
|
|
|
};
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
template <typename HasLimit>
|
|
|
|
class AggregateFunctionGroupUniqArrayDateTime : public AggregateFunctionGroupUniqArray<DataTypeDateTime::FieldType, HasLimit>
|
2016-12-16 10:57:44 +00:00
|
|
|
{
|
2019-02-11 19:26:32 +00:00
|
|
|
public:
|
2021-07-26 14:57:49 +00:00
|
|
|
explicit AggregateFunctionGroupUniqArrayDateTime(const DataTypePtr & argument_type, const Array & parameters_, UInt64 max_elems_ = std::numeric_limits<UInt64>::max())
|
|
|
|
: AggregateFunctionGroupUniqArray<DataTypeDateTime::FieldType, HasLimit>(argument_type, parameters_, 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
|
|
|
};
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
template <typename HasLimit, typename ... TArgs>
|
2019-04-15 06:36:24 +00:00
|
|
|
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);
|
2020-03-23 02:12:31 +00:00
|
|
|
if (which.idx == TypeIndex::Date) return new AggregateFunctionGroupUniqArrayDate<HasLimit>(argument_type, std::forward<TArgs>(args)...);
|
|
|
|
else if (which.idx == TypeIndex::DateTime) return new AggregateFunctionGroupUniqArrayDateTime<HasLimit>(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())
|
2020-03-23 02:12:31 +00:00
|
|
|
return new AggregateFunctionGroupUniqArrayGeneric<true, HasLimit>(argument_type, std::forward<TArgs>(args)...);
|
2017-12-09 06:32:22 +00:00
|
|
|
else
|
2020-03-23 02:12:31 +00:00
|
|
|
return new AggregateFunctionGroupUniqArrayGeneric<false, HasLimit>(argument_type, std::forward<TArgs>(args)...);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-09-13 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
template <typename HasLimit, typename ... TArgs>
|
2019-04-15 06:36:24 +00:00
|
|
|
inline AggregateFunctionPtr createAggregateFunctionGroupUniqArrayImpl(const std::string & name, const DataTypePtr & argument_type, TArgs ... args)
|
|
|
|
{
|
|
|
|
|
2020-03-23 02:12:31 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionGroupUniqArray, HasLimit, 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)
|
2020-03-23 02:12:31 +00:00
|
|
|
res = AggregateFunctionPtr(createWithExtraTypes<HasLimit>(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
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-07 00:15:11 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionGroupUniqArray(
|
|
|
|
const std::string & name, const DataTypes & argument_types, const Array & parameters, const Settings *)
|
2019-04-15 06:36:24 +00:00
|
|
|
{
|
|
|
|
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)
|
2021-07-26 14:57:49 +00:00
|
|
|
return createAggregateFunctionGroupUniqArrayImpl<std::false_type>(name, argument_types[0], parameters);
|
2019-04-15 06:36:24 +00:00
|
|
|
else
|
2021-07-26 14:57:49 +00:00
|
|
|
return createAggregateFunctionGroupUniqArrayImpl<std::true_type>(name, argument_types[0], parameters, max_elems);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionGroupUniqArray(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2020-07-05 23:50:20 +00:00
|
|
|
AggregateFunctionProperties properties = { .returns_default_when_only_null = false, .is_order_dependent = true };
|
|
|
|
|
|
|
|
factory.registerFunction("groupUniqArray", { createAggregateFunctionGroupUniqArray, properties });
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|