2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionUniqUpTo.h>
|
2021-06-14 04:13:35 +00:00
|
|
|
#include <Common/FieldVisitorConvertToNumber.h>
|
2017-12-20 21:22:04 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
2021-07-15 11:41:52 +00:00
|
|
|
#include <DataTypes/DataTypeDate32.h>
|
2017-12-20 21:22:04 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeFixedString.h>
|
2021-05-03 22:46:51 +00:00
|
|
|
|
2017-12-20 21:22:04 +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
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2017-12-20 21:22:04 +00:00
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
2017-12-20 20:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-24 12:40:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-03-09 03:38:43 +00:00
|
|
|
constexpr UInt8 uniq_upto_max_threshold = 100;
|
2017-12-20 20:25:22 +00:00
|
|
|
|
|
|
|
|
2021-05-26 11:32:14 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionUniqUpTo(const std::string & name, const DataTypes & argument_types, const Array & params, const Settings *)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-12-20 20:58:43 +00:00
|
|
|
UInt8 threshold = 5; /// default value
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2017-12-20 20:58:43 +00:00
|
|
|
if (!params.empty())
|
|
|
|
{
|
|
|
|
if (params.size() != 1)
|
|
|
|
throw Exception("Aggregate function " + name + " requires one parameter or less.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
UInt64 threshold_param = applyVisitor(FieldVisitorConvertToNumber<UInt64>(), params[0]);
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2017-12-20 20:58:43 +00:00
|
|
|
if (threshold_param > uniq_upto_max_threshold)
|
|
|
|
throw Exception("Too large parameter for aggregate function " + name + ". Maximum: " + toString(uniq_upto_max_threshold),
|
|
|
|
ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2017-12-20 20:58:43 +00:00
|
|
|
threshold = threshold_param;
|
|
|
|
}
|
2017-12-20 20:25:22 +00:00
|
|
|
|
2018-03-17 18:14:05 +00:00
|
|
|
if (argument_types.empty())
|
|
|
|
throw Exception("Incorrect number of arguments for aggregate function " + name,
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2018-07-16 03:12:01 +00:00
|
|
|
bool use_exact_hash_function = !isAllArgumentsContiguousInMemory(argument_types);
|
|
|
|
|
2015-10-29 03:41:09 +00:00
|
|
|
if (argument_types.size() == 1)
|
|
|
|
{
|
|
|
|
const IDataType & argument_type = *argument_types[0];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-02-11 19:26:32 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionUniqUpTo>(*argument_types[0], threshold, argument_types, params));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-09-10 17:09:07 +00:00
|
|
|
WhichDataType which(argument_type);
|
2015-10-29 03:41:09 +00:00
|
|
|
if (res)
|
|
|
|
return res;
|
2018-09-10 17:09:07 +00:00
|
|
|
else if (which.isDate())
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpTo<DataTypeDate::FieldType>>(threshold, argument_types, params);
|
2021-07-15 11:41:52 +00:00
|
|
|
else if (which.isDate32())
|
|
|
|
return std::make_shared<AggregateFunctionUniqUpTo<DataTypeDate32::FieldType>>(threshold, argument_types, params);
|
2018-09-10 17:09:07 +00:00
|
|
|
else if (which.isDateTime())
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpTo<DataTypeDateTime::FieldType>>(threshold, argument_types, params);
|
2018-09-10 17:09:07 +00:00
|
|
|
else if (which.isStringOrFixedString())
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpTo<String>>(threshold, argument_types, params);
|
2018-09-10 17:09:07 +00:00
|
|
|
else if (which.isUUID())
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpTo<DataTypeUUID::FieldType>>(threshold, argument_types, params);
|
2018-09-10 17:09:07 +00:00
|
|
|
else if (which.isTuple())
|
2018-07-16 03:12:01 +00:00
|
|
|
{
|
|
|
|
if (use_exact_hash_function)
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpToVariadic<true, true>>(argument_types, params, threshold);
|
2018-07-16 03:12:01 +00:00
|
|
|
else
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpToVariadic<false, true>>(argument_types, params, threshold);
|
2018-07-16 03:12:01 +00:00
|
|
|
}
|
2015-10-29 03:41:09 +00:00
|
|
|
}
|
2018-03-17 18:14:05 +00:00
|
|
|
|
|
|
|
/// "Variadic" method also works as a fallback generic case for single argument.
|
2018-07-16 03:12:01 +00:00
|
|
|
if (use_exact_hash_function)
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpToVariadic<true, false>>(argument_types, params, threshold);
|
2018-07-16 03:12:01 +00:00
|
|
|
else
|
2019-02-11 19:26:32 +00:00
|
|
|
return std::make_shared<AggregateFunctionUniqUpToVariadic<false, false>>(argument_types, params, threshold);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionUniqUpTo(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2020-06-14 07:44:02 +00:00
|
|
|
factory.registerFunction("uniqUpTo", {createAggregateFunctionUniqUpTo, {true}});
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|