| 1 | #include <AggregateFunctions/AggregateFunctionMerge.h> |
|---|---|
| 2 | #include <AggregateFunctions/AggregateFunctionCombinatorFactory.h> |
| 3 | #include <DataTypes/DataTypeAggregateFunction.h> |
| 4 | #include "registerAggregateFunctions.h" |
| 5 | |
| 6 | |
| 7 | namespace DB |
| 8 | { |
| 9 | |
| 10 | namespace ErrorCodes |
| 11 | { |
| 12 | extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; |
| 13 | extern const int BAD_ARGUMENTS; |
| 14 | } |
| 15 | |
| 16 | class AggregateFunctionCombinatorMerge final : public IAggregateFunctionCombinator |
| 17 | { |
| 18 | public: |
| 19 | String getName() const override { return "Merge"; } |
| 20 | |
| 21 | DataTypes transformArguments(const DataTypes & arguments) const override |
| 22 | { |
| 23 | if (arguments.size() != 1) |
| 24 | throw Exception("Incorrect number of arguments for aggregate function with "+ getName() + " suffix", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); |
| 25 | |
| 26 | const DataTypePtr & argument = arguments[0]; |
| 27 | |
| 28 | const DataTypeAggregateFunction * function = typeid_cast<const DataTypeAggregateFunction *>(argument.get()); |
| 29 | if (!function) |
| 30 | throw Exception("Illegal type "+ argument->getName() + " of argument for aggregate function with "+ getName() + " suffix" |
| 31 | + " must be AggregateFunction(...)", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); |
| 32 | |
| 33 | return function->getArgumentsDataTypes(); |
| 34 | } |
| 35 | |
| 36 | AggregateFunctionPtr transformAggregateFunction( |
| 37 | const AggregateFunctionPtr & nested_function, const DataTypes & arguments, const Array &) const override |
| 38 | { |
| 39 | const DataTypePtr & argument = arguments[0]; |
| 40 | |
| 41 | const DataTypeAggregateFunction * function = typeid_cast<const DataTypeAggregateFunction *>(argument.get()); |
| 42 | if (!function) |
| 43 | throw Exception("Illegal type "+ argument->getName() + " of argument for aggregate function with "+ getName() + " suffix" |
| 44 | + " must be AggregateFunction(...)", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); |
| 45 | |
| 46 | if (nested_function->getName() != function->getFunctionName()) |
| 47 | throw Exception("Illegal type "+ argument->getName() + " of argument for aggregate function with "+ getName() + " suffix" |
| 48 | + ", because it corresponds to different aggregate function: "+ function->getFunctionName() + " instead of "+ nested_function->getName(), |
| 49 | ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); |
| 50 | |
| 51 | return std::make_shared<AggregateFunctionMerge>(nested_function, argument); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | void registerAggregateFunctionCombinatorMerge(AggregateFunctionCombinatorFactory & factory) |
| 56 | { |
| 57 | factory.registerCombinator(std::make_shared<AggregateFunctionCombinatorMerge>()); |
| 58 | } |
| 59 | |
| 60 | } |
| 61 |