1#include <AggregateFunctions/AggregateFunctionHistogram.h>
2#include <AggregateFunctions/AggregateFunctionFactory.h>
3#include <AggregateFunctions/FactoryHelpers.h>
4#include <AggregateFunctions/Helpers.h>
5
6#include <Common/FieldVisitors.h>
7#include "registerAggregateFunctions.h"
8
9namespace DB
10{
11
12namespace ErrorCodes
13{
14 extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
15 extern const int ILLEGAL_TYPE_OF_ARGUMENT;
16 extern const int BAD_ARGUMENTS;
17 extern const int UNSUPPORTED_PARAMETER;
18 extern const int PARAMETER_OUT_OF_BOUND;
19}
20
21
22namespace
23{
24
25AggregateFunctionPtr createAggregateFunctionHistogram(const std::string & name, const DataTypes & arguments, const Array & params)
26{
27 if (params.size() != 1)
28 throw Exception("Function " + name + " requires single parameter: bins count", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
29
30 if (params[0].getType() != Field::Types::UInt64)
31 throw Exception("Invalid type for bins count", ErrorCodes::UNSUPPORTED_PARAMETER);
32
33 UInt32 bins_count = applyVisitor(FieldVisitorConvertToNumber<UInt32>(), params[0]);
34
35 auto limit = AggregateFunctionHistogramData::bins_count_limit;
36 if (bins_count > limit)
37 throw Exception("Unsupported bins count. Should not be greater than " + std::to_string(limit), ErrorCodes::PARAMETER_OUT_OF_BOUND);
38
39 if (bins_count == 0)
40 throw Exception("Bin count should be positive", ErrorCodes::BAD_ARGUMENTS);
41
42 assertUnary(name, arguments);
43 AggregateFunctionPtr res(createWithNumericType<AggregateFunctionHistogram>(*arguments[0], bins_count, arguments, params));
44
45 if (!res)
46 throw Exception("Illegal type " + arguments[0]->getName() + " of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
47
48 return res;
49}
50
51}
52
53void registerAggregateFunctionHistogram(AggregateFunctionFactory & factory)
54{
55 factory.registerFunction("histogram", createAggregateFunctionHistogram);
56}
57
58}
59