ClickHouse/src/Functions/evalMLMethod.cpp

95 lines
2.7 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 <iostream>
#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";
static FunctionPtr create(ContextConstPtr 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
}
explicit FunctionEvalMLMethod(ContextConstPtr 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;
}
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())
2019-04-14 23:57:14 +00:00
throw Exception("Function " + getName() + " requires at least one argument", ErrorCodes::BAD_ARGUMENTS);
const auto * type = checkAndGetDataType<DataTypeAggregateFunction>(arguments[0].get());
2019-04-08 22:40:37 +00:00
if (!type)
throw Exception("Argument for function " + getName() + " must have type AggregateFunction - state of aggregate function.",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
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())
2019-04-14 23:57:14 +00:00
throw Exception("Function " + getName() + " requires at least one argument", ErrorCodes::BAD_ARGUMENTS);
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)
2020-10-17 21:41:50 +00:00
throw Exception("Illegal column " + arguments[0].column->getName()
2019-04-21 14:32:42 +00:00
+ " of first argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN);
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
}
ContextConstPtr context;
2019-04-08 22:40:37 +00:00
};
2020-09-07 18:00:37 +00:00
}
2019-04-08 22:40:37 +00:00
void registerFunctionEvalMLMethod(FunctionFactory & factory)
{
factory.registerFunction<FunctionEvalMLMethod>();
}
2019-04-14 23:57:14 +00:00
}