2017-05-02 21:08:37 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionTopK.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-20 20:25:22 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-02 21:08:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Substitute return type for Date and DateTime
|
|
|
|
class AggregateFunctionTopKDate : public AggregateFunctionTopK<DataTypeDate::FieldType>
|
|
|
|
{
|
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDate>()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class AggregateFunctionTopKDateTime : public AggregateFunctionTopK<DataTypeDateTime::FieldType>
|
|
|
|
{
|
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDateTime>()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static IAggregateFunction * createWithExtraTypes(const IDataType & argument_type)
|
|
|
|
{
|
|
|
|
if (typeid_cast<const DataTypeDate *>(&argument_type)) return new AggregateFunctionTopKDate;
|
|
|
|
else if (typeid_cast<const DataTypeDateTime *>(&argument_type)) return new AggregateFunctionTopKDateTime;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Check that we can use plain version of AggregateFunctionTopKGeneric
|
2017-12-09 06:32:22 +00:00
|
|
|
if (argument_type.isValueUnambiguouslyRepresentedInContiguousMemoryRegion())
|
2017-05-02 21:08:37 +00:00
|
|
|
return new AggregateFunctionTopKGeneric<true>;
|
2017-12-09 06:32:22 +00:00
|
|
|
else
|
|
|
|
return new AggregateFunctionTopKGeneric<false>;
|
2017-05-02 21:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionTopK(const std::string & name, const DataTypes & argument_types, const Array & /*parameters*/)
|
2017-05-02 21:08:37 +00:00
|
|
|
{
|
|
|
|
if (argument_types.size() != 1)
|
|
|
|
throw Exception("Incorrect number of arguments for aggregate function " + name,
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionTopK>(*argument_types[0]));
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
res = AggregateFunctionPtr(createWithExtraTypes(*argument_types[0]));
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
factory.registerFunction("topK", createAggregateFunctionTopK);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|