| 1 | #pragma once |
| 2 | |
| 3 | #include <memory> |
| 4 | #include <DataTypes/IDataType.h> |
| 5 | #include <AggregateFunctions/IAggregateFunction.h> |
| 6 | |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | /** Aggregate function combinator allows to take one aggregate function |
| 12 | * and transform it to another aggregate function. |
| 13 | * |
| 14 | * In SQL language they are used as suffixes for existing aggregate functions. |
| 15 | * |
| 16 | * Example: -If combinator takes an aggregate function and transforms it |
| 17 | * to aggregate function with additional argument at end (condition), |
| 18 | * that will pass values to original aggregate function when the condition is true. |
| 19 | * |
| 20 | * More examples: |
| 21 | * |
| 22 | * sum(x) - calculate sum of x |
| 23 | * sumIf(x, cond) - calculate sum of x for rows where condition is true. |
| 24 | * sumArray(arr) - calculate sum of all elements of arrays. |
| 25 | * |
| 26 | * PS. Please don't mess it with so called "combiner" - totally unrelated notion from Hadoop world. |
| 27 | * "combining" - merging the states of aggregate functions - is supported naturally in ClickHouse. |
| 28 | */ |
| 29 | |
| 30 | class IAggregateFunctionCombinator |
| 31 | { |
| 32 | public: |
| 33 | virtual String getName() const = 0; |
| 34 | |
| 35 | virtual bool isForInternalUsageOnly() const { return false; } |
| 36 | |
| 37 | /** From the arguments for combined function (ex: UInt64, UInt8 for sumIf), |
| 38 | * get the arguments for nested function (ex: UInt64 for sum). |
| 39 | * If arguments are not suitable for combined function, throw an exception. |
| 40 | */ |
| 41 | virtual DataTypes transformArguments(const DataTypes & arguments) const |
| 42 | { |
| 43 | return arguments; |
| 44 | } |
| 45 | |
| 46 | /** From the parameters for combined function, |
| 47 | * get the parameters for nested function. |
| 48 | * If arguments are not suitable for combined function, throw an exception. |
| 49 | */ |
| 50 | virtual Array transformParameters(const Array & parameters) const |
| 51 | { |
| 52 | return parameters; |
| 53 | } |
| 54 | |
| 55 | /** Create combined aggregate function (ex: sumIf) |
| 56 | * from nested function (ex: sum) |
| 57 | * and arguments for combined agggregate function (ex: UInt64, UInt8 for sumIf). |
| 58 | * It's assumed that function transformArguments was called before this function and 'arguments' are validated. |
| 59 | */ |
| 60 | virtual AggregateFunctionPtr transformAggregateFunction( |
| 61 | const AggregateFunctionPtr & nested_function, |
| 62 | const DataTypes & arguments, |
| 63 | const Array & params) const = 0; |
| 64 | |
| 65 | virtual ~IAggregateFunctionCombinator() {} |
| 66 | }; |
| 67 | |
| 68 | using AggregateFunctionCombinatorPtr = std::shared_ptr<const IAggregateFunctionCombinator>; |
| 69 | |
| 70 | } |
| 71 | |