2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-09-08 22:04:39 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <DataTypes/DataTypeAggregateFunction.h>
|
|
|
|
#include <Columns/ColumnAggregateFunction.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
/** finalizeAggregation(agg_state) - get the result from the aggregation state.
|
|
|
|
* Takes state of aggregate function. Returns result of aggregation (finalized state).
|
|
|
|
*/
|
|
|
|
class FunctionFinalizeAggregation : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "finalizeAggregation";
|
2021-04-10 23:33:54 +00:00
|
|
|
static FunctionPtr create(ContextPtr)
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
|
|
|
return std::make_shared<FunctionFinalizeAggregation>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-03-23 02:33:11 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2018-09-08 22:04:39 +00:00
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
const DataTypeAggregateFunction * type = checkAndGetDataType<DataTypeAggregateFunction>(arguments[0].get());
|
|
|
|
if (!type)
|
2020-06-25 20:23:56 +00:00
|
|
|
{
|
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Argument for function '{}' must have type AggregateFunction - state of aggregate function."
|
|
|
|
" Got '{}' instead", getName(), arguments[0]->getName());
|
|
|
|
}
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
return type->getReturnType();
|
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
|
2018-09-08 22:04:39 +00:00
|
|
|
{
|
2020-10-17 21:41:50 +00:00
|
|
|
auto column = arguments.at(0).column;
|
2020-05-14 07:59:14 +00:00
|
|
|
if (!typeid_cast<const ColumnAggregateFunction *>(column.get()))
|
2020-10-17 21:41:50 +00:00
|
|
|
throw Exception("Illegal column " + arguments.at(0).column->getName()
|
2018-09-08 22:04:39 +00:00
|
|
|
+ " of first argument of function "
|
|
|
|
+ getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2020-05-14 07:59:14 +00:00
|
|
|
/// Column is copied here, because there is no guarantee that we own it.
|
2020-05-14 08:30:18 +00:00
|
|
|
auto mut_column = IColumn::mutate(std::move(column));
|
2020-10-17 21:41:50 +00:00
|
|
|
return ColumnAggregateFunction::convertToValues(std::move(mut_column));
|
2018-09-08 22:04:39 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2018-09-08 22:04:39 +00:00
|
|
|
|
|
|
|
void registerFunctionFinalizeAggregation(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionFinalizeAggregation>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|