ClickHouse/src/Functions/evalMLMethod.cpp

92 lines
2.8 KiB
C++
Raw Normal View History

#include <Functions/IFunctionImpl.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
{
namespace ErrorCodes
{
2020-02-25 18:02:41 +00:00
extern const int BAD_ARGUMENTS;
2019-01-22 21:07:05 +00:00
extern const int ILLEGAL_COLUMN;
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}
/** 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";
2019-04-21 14:32:42 +00:00
static FunctionPtr create(const Context & 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
}
2020-03-18 03:27:32 +00:00
explicit FunctionEvalMLMethod(const Context & 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
}
2020-07-21 13:58:07 +00:00
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, 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);
const auto * model = block.getByPosition(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)
2019-04-14 23:57:14 +00:00
throw Exception("Illegal column " + block.getByPosition(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
block.getByPosition(result).column = agg_function->predictValues(block, arguments, context);
2019-04-08 22:40:37 +00:00
}
2019-04-21 14:32:42 +00:00
const Context & context;
2019-04-08 22:40:37 +00:00
};
void registerFunctionEvalMLMethod(FunctionFactory & factory)
{
factory.registerFunction<FunctionEvalMLMethod>();
}
2019-04-14 23:57:14 +00:00
}