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