2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2020-07-03 12:18:47 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnAggregateFunction.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionState.h>
|
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <AggregateFunctions/parseAggregateFunctionParameters.h>
|
|
|
|
#include <Common/Arena.h>
|
|
|
|
|
2021-04-04 09:23:40 +00:00
|
|
|
#include <ext/scope_guard_safe.h>
|
2020-07-03 12:18:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-07-03 12:18:47 +00:00
|
|
|
|
|
|
|
class FunctionInitializeAggregation : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "initializeAggregation";
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionInitializeAggregation>(); }
|
2020-07-03 12:18:47 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0}; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override;
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override;
|
2020-07-03 12:18:47 +00:00
|
|
|
|
|
|
|
private:
|
2021-06-06 21:49:55 +00:00
|
|
|
/// TODO Rewrite with FunctionBuilder.
|
2020-07-03 12:18:47 +00:00
|
|
|
mutable AggregateFunctionPtr aggregate_function;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
DataTypePtr FunctionInitializeAggregation::getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const
|
|
|
|
{
|
|
|
|
if (arguments.size() < 2)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be at least 2.",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
const ColumnConst * aggregate_function_name_column = checkAndGetColumnConst<ColumnString>(arguments[0].column.get());
|
|
|
|
if (!aggregate_function_name_column)
|
|
|
|
throw Exception("First argument for function " + getName() + " must be constant string: name of aggregate function.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
DataTypes argument_types(arguments.size() - 1);
|
|
|
|
for (size_t i = 1, size = arguments.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
argument_types[i - 1] = arguments[i].type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aggregate_function)
|
|
|
|
{
|
|
|
|
String aggregate_function_name_with_params = aggregate_function_name_column->getValue<String>();
|
|
|
|
|
|
|
|
if (aggregate_function_name_with_params.empty())
|
|
|
|
throw Exception("First argument for function " + getName() + " (name of aggregate function) cannot be empty.",
|
|
|
|
ErrorCodes::BAD_ARGUMENTS);
|
|
|
|
|
|
|
|
String aggregate_function_name;
|
|
|
|
Array params_row;
|
|
|
|
getAggregateFunctionNameAndParametersArray(aggregate_function_name_with_params,
|
|
|
|
aggregate_function_name, params_row, "function " + getName());
|
|
|
|
|
|
|
|
AggregateFunctionProperties properties;
|
|
|
|
aggregate_function = AggregateFunctionFactory::instance().get(aggregate_function_name, argument_types, params_row, properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aggregate_function->getReturnType();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr FunctionInitializeAggregation::executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const
|
2020-07-03 12:18:47 +00:00
|
|
|
{
|
2021-06-06 21:49:55 +00:00
|
|
|
const IAggregateFunction & agg_func = *aggregate_function;
|
2020-07-03 12:18:47 +00:00
|
|
|
std::unique_ptr<Arena> arena = std::make_unique<Arena>();
|
|
|
|
|
|
|
|
const size_t num_arguments_columns = arguments.size() - 1;
|
|
|
|
|
|
|
|
std::vector<ColumnPtr> materialized_columns(num_arguments_columns);
|
|
|
|
std::vector<const IColumn *> aggregate_arguments_vec(num_arguments_columns);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_arguments_columns; ++i)
|
|
|
|
{
|
2020-10-19 13:42:14 +00:00
|
|
|
const IColumn * col = arguments[i + 1].column.get();
|
2020-07-03 12:18:47 +00:00
|
|
|
materialized_columns.emplace_back(col->convertToFullColumnIfConst());
|
|
|
|
aggregate_arguments_vec[i] = &(*materialized_columns.back());
|
|
|
|
}
|
|
|
|
|
|
|
|
const IColumn ** aggregate_arguments = aggregate_arguments_vec.data();
|
|
|
|
|
2020-10-19 13:42:14 +00:00
|
|
|
MutableColumnPtr result_holder = result_type->createColumn();
|
2020-07-03 12:18:47 +00:00
|
|
|
IColumn & res_col = *result_holder;
|
|
|
|
|
|
|
|
/// AggregateFunction's states should be inserted into column using specific way
|
|
|
|
auto * res_col_aggregate_function = typeid_cast<ColumnAggregateFunction *>(&res_col);
|
|
|
|
|
|
|
|
if (!res_col_aggregate_function && agg_func.isState())
|
|
|
|
throw Exception("State function " + agg_func.getName() + " inserts results into non-state column "
|
2020-10-19 13:42:14 +00:00
|
|
|
+ result_type->getName(), ErrorCodes::ILLEGAL_COLUMN);
|
2020-07-03 12:18:47 +00:00
|
|
|
|
|
|
|
PODArray<AggregateDataPtr> places(input_rows_count);
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
{
|
|
|
|
places[i] = arena->alignedAlloc(agg_func.sizeOfData(), agg_func.alignOfData());
|
|
|
|
try
|
|
|
|
{
|
|
|
|
agg_func.create(places[i]);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < i; ++j)
|
|
|
|
agg_func.destroy(places[j]);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 09:23:40 +00:00
|
|
|
SCOPE_EXIT_MEMORY_SAFE({
|
2020-07-03 12:18:47 +00:00
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
agg_func.destroy(places[i]);
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
2021-06-09 11:07:21 +00:00
|
|
|
const auto * that = &agg_func;
|
2020-07-03 12:18:47 +00:00
|
|
|
/// Unnest consecutive trailing -State combinators
|
2021-06-09 11:07:21 +00:00
|
|
|
while (const auto * func = typeid_cast<const AggregateFunctionState *>(that))
|
2020-07-03 12:18:47 +00:00
|
|
|
that = func->getNestedFunction().get();
|
|
|
|
that->addBatch(input_rows_count, places.data(), 0, aggregate_arguments, arena.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < input_rows_count; ++i)
|
|
|
|
if (!res_col_aggregate_function)
|
|
|
|
agg_func.insertResultInto(places[i], res_col, arena.get());
|
|
|
|
else
|
|
|
|
res_col_aggregate_function->insertFrom(places[i]);
|
2020-10-19 13:42:14 +00:00
|
|
|
return result_holder;
|
2020-07-03 12:18:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:00:37 +00:00
|
|
|
}
|
2020-07-03 12:18:47 +00:00
|
|
|
|
|
|
|
void registerFunctionInitializeAggregation(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionInitializeAggregation>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|