ClickHouse
100 строк · 3.1 Кб
1#include "AggregateFunctionResample.h"
2#include "AggregateFunctionCombinatorFactory.h"
3
4namespace DB
5{
6
7namespace ErrorCodes
8{
9extern const int ILLEGAL_TYPE_OF_ARGUMENT;
10extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
11}
12
13namespace
14{
15
16class AggregateFunctionCombinatorResample final : public IAggregateFunctionCombinator
17{
18public:
19String getName() const override
20{
21return "Resample";
22}
23
24DataTypes transformArguments(const DataTypes & arguments) const override
25{
26if (arguments.empty())
27throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
28"Incorrect number of arguments for aggregate function with {} suffix", getName());
29
30return DataTypes(arguments.begin(), arguments.end() - 1);
31}
32
33Array transformParameters(const Array & params) const override
34{
35if (params.size() < 3)
36throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
37"Incorrect number of parameters for aggregate function with {} suffix", getName());
38
39return Array(params.begin(), params.end() - 3);
40}
41
42AggregateFunctionPtr transformAggregateFunction(
43const AggregateFunctionPtr & nested_function,
44const AggregateFunctionProperties &,
45const DataTypes & arguments,
46const Array & params) const override
47{
48WhichDataType which{arguments.back()};
49
50if (which.isNativeUInt() || which.isDate() || which.isDateTime() || which.isDateTime64())
51{
52UInt64 begin = params[params.size() - 3].safeGet<UInt64>();
53UInt64 end = params[params.size() - 2].safeGet<UInt64>();
54
55UInt64 step = params[params.size() - 1].safeGet<UInt64>();
56
57return std::make_shared<AggregateFunctionResample<UInt64>>(
58nested_function,
59begin,
60end,
61step,
62arguments,
63params);
64}
65
66if (which.isNativeInt() || which.isEnum() || which.isInterval())
67{
68Int64 begin, end;
69
70// notice: UInt64 -> Int64 may lead to overflow
71if (!params[params.size() - 3].tryGet<Int64>(begin))
72begin = params[params.size() - 3].safeGet<UInt64>();
73if (!params[params.size() - 2].tryGet<Int64>(end))
74end = params[params.size() - 2].safeGet<UInt64>();
75
76UInt64 step = params[params.size() - 1].safeGet<UInt64>();
77
78return std::make_shared<AggregateFunctionResample<Int64>>(
79nested_function,
80begin,
81end,
82step,
83arguments,
84params);
85}
86
87throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
88"Illegal types of argument for aggregate function {}, the type "
89"of the last argument should be native integer or integer-like", getName());
90}
91};
92
93}
94
95void registerAggregateFunctionCombinatorResample(AggregateFunctionCombinatorFactory & factory)
96{
97factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorResample>());
98}
99
100}
101