2017-05-02 21:08:37 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionTopK.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
2017-12-20 20:58:43 +00:00
|
|
|
#include <AggregateFunctions/FactoryHelpers.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>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2021-05-03 22:46:51 +00:00
|
|
|
|
2017-12-20 20:58:43 +00:00
|
|
|
|
|
|
|
#define TOP_K_MAX_SIZE 0xFFFFFF
|
|
|
|
|
2017-05-02 21:08:37 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-05-26 11:32:14 +00:00
|
|
|
struct Settings;
|
2017-05-02 21:08:37 +00:00
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2017-12-20 20:58:43 +00:00
|
|
|
extern const int ARGUMENT_OUT_OF_BOUND;
|
2019-02-10 21:01:26 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2017-12-20 20:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-02 21:08:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Substitute return type for Date and DateTime
|
2019-02-10 21:01:26 +00:00
|
|
|
template <bool is_weighted>
|
|
|
|
class AggregateFunctionTopKDate : public AggregateFunctionTopK<DataTypeDate::FieldType, is_weighted>
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
2019-02-10 21:01:26 +00:00
|
|
|
using AggregateFunctionTopK<DataTypeDate::FieldType, is_weighted>::AggregateFunctionTopK;
|
2017-05-02 21:08:37 +00:00
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDate>()); }
|
|
|
|
};
|
|
|
|
|
2019-02-10 21:01:26 +00:00
|
|
|
template <bool is_weighted>
|
|
|
|
class AggregateFunctionTopKDateTime : public AggregateFunctionTopK<DataTypeDateTime::FieldType, is_weighted>
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
2019-02-10 21:01:26 +00:00
|
|
|
using AggregateFunctionTopK<DataTypeDateTime::FieldType, is_weighted>::AggregateFunctionTopK;
|
2017-05-02 21:08:37 +00:00
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDateTime>()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-02-10 21:01:26 +00:00
|
|
|
template <bool is_weighted>
|
2019-03-10 21:32:42 +00:00
|
|
|
static IAggregateFunction * createWithExtraTypes(const DataTypePtr & argument_type, UInt64 threshold, UInt64 load_factor, const Array & params)
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
2018-09-10 17:09:07 +00:00
|
|
|
WhichDataType which(argument_type);
|
2019-02-10 21:01:26 +00:00
|
|
|
if (which.idx == TypeIndex::Date)
|
2019-03-10 21:32:42 +00:00
|
|
|
return new AggregateFunctionTopKDate<is_weighted>(threshold, load_factor, {argument_type}, params);
|
2019-02-10 21:01:26 +00:00
|
|
|
if (which.idx == TypeIndex::DateTime)
|
2019-03-10 21:32:42 +00:00
|
|
|
return new AggregateFunctionTopKDateTime<is_weighted>(threshold, load_factor, {argument_type}, params);
|
2017-12-20 20:58:43 +00:00
|
|
|
|
|
|
|
/// Check that we can use plain version of AggregateFunctionTopKGeneric
|
|
|
|
if (argument_type->isValueUnambiguouslyRepresentedInContiguousMemoryRegion())
|
2019-03-10 21:32:42 +00:00
|
|
|
return new AggregateFunctionTopKGeneric<true, is_weighted>(threshold, load_factor, argument_type, params);
|
2017-05-02 21:08:37 +00:00
|
|
|
else
|
2019-03-10 21:32:42 +00:00
|
|
|
return new AggregateFunctionTopKGeneric<false, is_weighted>(threshold, load_factor, argument_type, params);
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 21:01:26 +00:00
|
|
|
|
|
|
|
template <bool is_weighted>
|
2021-05-26 11:32:14 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionTopK(const std::string & name, const DataTypes & argument_types, const Array & params, const Settings *)
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
2019-02-10 21:01:26 +00:00
|
|
|
if (!is_weighted)
|
|
|
|
{
|
2019-02-03 10:59:27 +00:00
|
|
|
assertUnary(name, argument_types);
|
2019-02-10 21:01:26 +00:00
|
|
|
}
|
2019-02-03 10:59:27 +00:00
|
|
|
else
|
2019-02-10 21:01:26 +00:00
|
|
|
{
|
2019-02-03 10:59:27 +00:00
|
|
|
assertBinary(name, argument_types);
|
2019-03-10 21:31:10 +00:00
|
|
|
if (!isInteger(argument_types[1]))
|
|
|
|
throw Exception("The second argument for aggregate function 'topKWeighted' must have integer type", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2019-02-10 21:01:26 +00:00
|
|
|
}
|
2017-12-20 20:58:43 +00:00
|
|
|
|
2019-03-09 00:12:58 +00:00
|
|
|
UInt64 threshold = 10; /// default values
|
2019-03-10 21:32:42 +00:00
|
|
|
UInt64 load_factor = 3;
|
2017-12-20 20:58:43 +00:00
|
|
|
|
|
|
|
if (!params.empty())
|
|
|
|
{
|
2019-03-09 00:12:58 +00:00
|
|
|
if (params.size() > 2)
|
|
|
|
throw Exception("Aggregate function " + name + " requires two parameters or less.",
|
2019-02-03 10:59:27 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2017-12-20 20:58:43 +00:00
|
|
|
|
|
|
|
UInt64 k = applyVisitor(FieldVisitorConvertToNumber<UInt64>(), params[0]);
|
2019-03-10 21:32:42 +00:00
|
|
|
if (params.size() == 2)
|
|
|
|
{
|
|
|
|
load_factor = applyVisitor(FieldVisitorConvertToNumber<UInt64>(), params[1]);
|
2019-03-09 00:12:58 +00:00
|
|
|
|
2019-03-10 21:32:42 +00:00
|
|
|
if (load_factor < 1)
|
2020-09-04 01:05:57 +00:00
|
|
|
throw Exception("Too small parameter 'load_factor' for aggregate function " + name + ". Minimum: 1",
|
2019-03-09 00:12:58 +00:00
|
|
|
ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
}
|
2017-12-20 20:58:43 +00:00
|
|
|
|
2020-09-04 01:05:57 +00:00
|
|
|
if (k > TOP_K_MAX_SIZE || load_factor > TOP_K_MAX_SIZE || k * load_factor > TOP_K_MAX_SIZE)
|
|
|
|
throw Exception("Too large parameter(s) for aggregate function " + name + ". Maximum: " + toString(TOP_K_MAX_SIZE),
|
2017-12-20 20:58:43 +00:00
|
|
|
ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
if (k == 0)
|
|
|
|
throw Exception("Parameter 0 is illegal for aggregate function " + name,
|
|
|
|
ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
threshold = k;
|
|
|
|
}
|
2017-05-02 21:08:37 +00:00
|
|
|
|
2020-06-29 23:17:20 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionTopK, is_weighted>(
|
|
|
|
*argument_types[0], threshold, load_factor, argument_types, params));
|
2017-05-02 21:08:37 +00:00
|
|
|
|
|
|
|
if (!res)
|
2019-03-10 21:32:42 +00:00
|
|
|
res = AggregateFunctionPtr(createWithExtraTypes<is_weighted>(argument_types[0], threshold, load_factor, params));
|
2017-05-02 21:08:37 +00:00
|
|
|
|
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type " + argument_types[0]->getName() +
|
|
|
|
" of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionTopK(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2020-07-05 23:50:20 +00:00
|
|
|
AggregateFunctionProperties properties = { .returns_default_when_only_null = false, .is_order_dependent = true };
|
|
|
|
|
|
|
|
factory.registerFunction("topK", { createAggregateFunctionTopK<false>, properties });
|
|
|
|
factory.registerFunction("topKWeighted", { createAggregateFunctionTopK<true>, properties });
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|