2018-07-04 23:24:36 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionHistogram.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
2021-06-14 04:13:35 +00:00
|
|
|
#include <Common/FieldVisitorConvertToNumber.h>
|
2021-06-07 00:15:11 +00:00
|
|
|
|
2018-06-22 18:30:09 +00:00
|
|
|
|
2018-07-04 23:24:36 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2021-05-26 11:32:14 +00:00
|
|
|
struct Settings;
|
2018-06-22 18:30:09 +00:00
|
|
|
|
2018-06-16 18:40:43 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
2018-07-07 12:38:47 +00:00
|
|
|
extern const int UNSUPPORTED_PARAMETER;
|
2018-07-08 08:13:09 +00:00
|
|
|
extern const int PARAMETER_OUT_OF_BOUND;
|
2018-06-16 18:40:43 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 17:55:11 +00:00
|
|
|
|
2018-07-04 23:24:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-06-16 18:40:43 +00:00
|
|
|
|
2021-05-26 11:32:14 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionHistogram(const std::string & name, const DataTypes & arguments, const Array & params, const Settings *)
|
2018-06-22 18:30:09 +00:00
|
|
|
{
|
2018-12-20 17:46:31 +00:00
|
|
|
if (params.size() != 1)
|
2018-07-04 22:28:15 +00:00
|
|
|
throw Exception("Function " + name + " requires single parameter: bins count", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2018-06-22 18:30:09 +00:00
|
|
|
|
2018-07-07 12:38:47 +00:00
|
|
|
if (params[0].getType() != Field::Types::UInt64)
|
|
|
|
throw Exception("Invalid type for bins count", ErrorCodes::UNSUPPORTED_PARAMETER);
|
|
|
|
|
2018-06-16 18:40:43 +00:00
|
|
|
UInt32 bins_count = applyVisitor(FieldVisitorConvertToNumber<UInt32>(), params[0]);
|
2018-06-22 18:30:09 +00:00
|
|
|
|
2018-07-08 08:13:09 +00:00
|
|
|
auto limit = AggregateFunctionHistogramData::bins_count_limit;
|
|
|
|
if (bins_count > limit)
|
|
|
|
throw Exception("Unsupported bins count. Should not be greater than " + std::to_string(limit), ErrorCodes::PARAMETER_OUT_OF_BOUND);
|
|
|
|
|
2018-06-16 18:40:43 +00:00
|
|
|
if (bins_count == 0)
|
|
|
|
throw Exception("Bin count should be positive", ErrorCodes::BAD_ARGUMENTS);
|
2018-06-22 18:30:09 +00:00
|
|
|
|
2018-06-16 18:40:43 +00:00
|
|
|
assertUnary(name, arguments);
|
2019-02-11 19:26:32 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionHistogram>(*arguments[0], bins_count, arguments, params));
|
2018-06-22 18:30:09 +00:00
|
|
|
|
2018-06-16 18:40:43 +00:00
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2018-12-20 17:46:31 +00:00
|
|
|
return res;
|
2018-06-22 18:30:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-16 18:40:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 18:30:09 +00:00
|
|
|
void registerAggregateFunctionHistogram(AggregateFunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction("histogram", createAggregateFunctionHistogram);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|