2017-10-17 12:02:30 +00:00
|
|
|
#include <Functions/FunctionsExternalModels.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
|
2017-10-17 10:44:46 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-10-17 12:02:30 +00:00
|
|
|
#include <Interpreters/ExternalModels.h>
|
2017-10-17 10:44:46 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <ext/range.h>
|
2018-04-24 07:16:39 +00:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2017-10-17 10:43:42 +00:00
|
|
|
|
2017-10-17 10:44:46 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
FunctionPtr FunctionModelEvaluate::create(const Context & context)
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionModelEvaluate>(context.getExternalModels());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int TOO_LESS_ARGUMENTS_FOR_FUNCTION;
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FunctionModelEvaluate::getReturnTypeImpl(const DataTypes & arguments) const
|
|
|
|
{
|
|
|
|
if (arguments.size() < 2)
|
|
|
|
throw Exception("Function " + getName() + " expects at least 2 arguments",
|
|
|
|
ErrorCodes::TOO_LESS_ARGUMENTS_FOR_FUNCTION);
|
|
|
|
|
2017-12-09 14:01:42 +00:00
|
|
|
if (!arguments[0]->isString())
|
2017-10-17 10:44:46 +00:00
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of first argument of function " + getName()
|
|
|
|
+ ", expected a string.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeFloat64>();
|
|
|
|
}
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void FunctionModelEvaluate::executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/)
|
2017-10-17 10:44:46 +00:00
|
|
|
{
|
|
|
|
const auto name_col = checkAndGetColumnConst<ColumnString>(block.getByPosition(arguments[0]).column.get());
|
|
|
|
if (!name_col)
|
|
|
|
throw Exception("First argument of function " + getName() + " must be a constant string",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
auto model = models.getModel(name_col->getValue<String>());
|
|
|
|
|
2017-12-13 01:27:53 +00:00
|
|
|
ColumnRawPtrs columns;
|
2017-10-20 09:52:36 +00:00
|
|
|
columns.reserve(arguments.size());
|
|
|
|
for (auto i : ext::range(1, arguments.size()))
|
2017-10-26 14:10:51 +00:00
|
|
|
columns.push_back(block.getByPosition(arguments[i]).column.get());
|
2017-10-17 10:44:46 +00:00
|
|
|
|
|
|
|
block.getByPosition(result).column = model->evaluate(columns);
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:02:30 +00:00
|
|
|
void registerFunctionsExternalModels(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionModelEvaluate>();
|
|
|
|
}
|
|
|
|
|
2017-10-17 10:44:46 +00:00
|
|
|
}
|