ClickHouse/src/Functions/evalMLMethod.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.8 KiB
C++
Raw Normal View History

2021-05-17 07:30:42 +00:00
#include <Functions/IFunction.h>
2019-01-22 21:07:05 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <DataTypes/DataTypeAggregateFunction.h>
#include <Columns/ColumnAggregateFunction.h>
#include <Common/typeid_cast.h>
#include <Common/PODArray.h>
namespace DB
{
2020-09-07 18:00:37 +00:00
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
2019-01-22 21:07:05 +00:00
2020-09-07 18:00:37 +00:00
namespace
{
2019-01-22 21:07:05 +00:00
/** finalizeAggregation(agg_state) - get the result from the aggregation state.
* Takes state of aggregate function. Returns result of aggregation (finalized state).
*/
2019-04-08 22:40:37 +00:00
class FunctionEvalMLMethod : public IFunction
{
public:
static constexpr auto name = "evalMLMethod";
2021-06-01 12:20:52 +00:00
static FunctionPtr create(ContextPtr context)
2019-01-22 21:07:05 +00:00
{
2019-04-21 14:32:42 +00:00
return std::make_shared<FunctionEvalMLMethod>(context);
2019-04-08 22:40:37 +00:00
}
2021-06-01 12:20:52 +00:00
explicit FunctionEvalMLMethod(ContextPtr context_) : context(context_)
2019-04-21 14:32:42 +00:00
{}
2019-04-08 22:40:37 +00:00
String getName() const override
{
return name;
}
2019-01-22 21:07:05 +00:00
2019-04-15 00:16:13 +00:00
bool isVariadic() const override
{
2019-04-08 22:40:37 +00:00
return true;
}
2021-06-22 16:21:23 +00:00
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override
{
return true;
}
2019-04-08 22:40:37 +00:00
size_t getNumberOfArguments() const override
{
return 0;
}
2019-01-22 21:07:05 +00:00
2019-04-08 22:40:37 +00:00
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
2019-01-22 21:07:05 +00:00
{
if (arguments.empty())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Function {} requires at least one argument", getName());
2019-04-14 23:57:14 +00:00
const auto * type = checkAndGetDataType<DataTypeAggregateFunction>(arguments[0].get());
2019-04-08 22:40:37 +00:00
if (!type)
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
"Argument for function {} must have type AggregateFunction - state "
"of aggregate function.", getName());
2019-04-08 22:40:37 +00:00
return type->getReturnTypeToPredict();
2019-01-22 21:07:05 +00:00
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const override
2019-04-08 22:40:37 +00:00
{
if (arguments.empty())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Function {} requires at least one argument", getName());
2019-04-14 23:57:14 +00:00
2020-10-17 21:41:50 +00:00
const auto * model = arguments[0].column.get();
2019-04-08 22:40:37 +00:00
if (const auto * column_with_states = typeid_cast<const ColumnConst *>(model))
model = column_with_states->getDataColumnPtr().get();
2019-04-08 22:40:37 +00:00
const auto * agg_function = typeid_cast<const ColumnAggregateFunction *>(model);
if (!agg_function)
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}",
arguments[0].column->getName(), getName());
2019-04-08 22:40:37 +00:00
2020-10-17 21:41:50 +00:00
return agg_function->predictValues(arguments, context);
2019-04-08 22:40:37 +00:00
}
2021-06-01 12:20:52 +00:00
ContextPtr context;
2019-04-08 22:40:37 +00:00
};
2020-09-07 18:00:37 +00:00
}
REGISTER_FUNCTION(EvalMLMethod)
2019-04-08 22:40:37 +00:00
{
factory.registerFunction<FunctionEvalMLMethod>();
}
2019-04-14 23:57:14 +00:00
}