ClickHouse/src/AggregateFunctions/AggregateFunctionHistogram.cpp

60 lines
1.9 KiB
C++
Raw Normal View History

2018-07-04 23:24:36 +00:00
#include <AggregateFunctions/AggregateFunctionHistogram.h>
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/FactoryHelpers.h>
#include <AggregateFunctions/Helpers.h>
2018-06-22 18:30:09 +00:00
#include <Common/FieldVisitors.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
{
struct Settings;
2018-06-22 18:30:09 +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-11-01 17:55:11 +00:00
2018-07-04 23:24:36 +00:00
namespace
{
AggregateFunctionPtr createAggregateFunctionHistogram(const std::string & name, const DataTypes & arguments, const Array & params, const Settings *)
2018-06-22 18:30:09 +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);
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);
if (bins_count == 0)
throw Exception("Bin count should be positive", ErrorCodes::BAD_ARGUMENTS);
2018-06-22 18:30:09 +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
if (!res)
throw Exception("Illegal type " + arguments[0]->getName() + " of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
return res;
2018-06-22 18:30:09 +00:00
}
}
2018-06-22 18:30:09 +00:00
void registerAggregateFunctionHistogram(AggregateFunctionFactory & factory)
{
factory.registerFunction("histogram", createAggregateFunctionHistogram);
}
}