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>
|
2019-09-26 10:23:14 +00:00
|
|
|
#include <Interpreters/ExternalModelsLoader.h>
|
2017-10-17 10:44:46 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2021-06-15 19:55:21 +00:00
|
|
|
#include <common/range.h>
|
2018-04-24 07:16:39 +00:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2018-12-26 16:44:57 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
|
|
|
|
2017-10-17 10:43:42 +00:00
|
|
|
|
2017-10-17 10:44:46 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
FunctionPtr FunctionModelEvaluate::create(ContextPtr context)
|
2017-10-17 10:44:46 +00:00
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
return std::make_shared<FunctionModelEvaluate>(context->getExternalModelsLoader());
|
2017-10-17 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-12-07 03:20:27 +00:00
|
|
|
extern const int TOO_FEW_ARGUMENTS_FOR_FUNCTION;
|
2017-10-17 10:44:46 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
2018-12-26 16:44:57 +00:00
|
|
|
DataTypePtr FunctionModelEvaluate::getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const
|
2017-10-17 10:44:46 +00:00
|
|
|
{
|
|
|
|
if (arguments.size() < 2)
|
|
|
|
throw Exception("Function " + getName() + " expects at least 2 arguments",
|
2018-12-07 03:20:27 +00:00
|
|
|
ErrorCodes::TOO_FEW_ARGUMENTS_FOR_FUNCTION);
|
2017-10-17 10:44:46 +00:00
|
|
|
|
2018-12-26 16:44:57 +00:00
|
|
|
if (!isString(arguments[0].type))
|
|
|
|
throw Exception("Illegal type " + arguments[0].type->getName() + " of first argument of function " + getName()
|
2017-10-17 10:44:46 +00:00
|
|
|
+ ", expected a string.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
2020-04-22 07:03:43 +00:00
|
|
|
const auto * name_col = checkAndGetColumnConst<ColumnString>(arguments[0].column.get());
|
2018-12-26 16:44:57 +00:00
|
|
|
if (!name_col)
|
|
|
|
throw Exception("First argument of function " + getName() + " must be a constant string",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
bool has_nullable = false;
|
|
|
|
for (size_t i = 1; i < arguments.size(); ++i)
|
|
|
|
has_nullable = has_nullable || arguments[i].type->isNullable();
|
|
|
|
|
2019-09-26 10:23:14 +00:00
|
|
|
auto model = models_loader.getModel(name_col->getValue<String>());
|
2018-12-26 16:44:57 +00:00
|
|
|
auto type = model->getReturnType();
|
|
|
|
|
|
|
|
if (has_nullable)
|
|
|
|
{
|
2020-04-22 07:03:43 +00:00
|
|
|
if (const auto * tuple = typeid_cast<const DataTypeTuple *>(type.get()))
|
2018-12-26 16:44:57 +00:00
|
|
|
{
|
|
|
|
auto elements = tuple->getElements();
|
|
|
|
for (auto & element : elements)
|
|
|
|
element = makeNullable(element);
|
|
|
|
|
|
|
|
type = std::make_shared<DataTypeTuple>(elements);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
type = makeNullable(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return type;
|
2017-10-17 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 13:24:45 +00:00
|
|
|
ColumnPtr FunctionModelEvaluate::executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t /*input_rows_count*/) const
|
2017-10-17 10:44:46 +00:00
|
|
|
{
|
2020-10-18 19:00:13 +00:00
|
|
|
const auto * name_col = checkAndGetColumnConst<ColumnString>(arguments[0].column.get());
|
2017-10-17 10:44:46 +00:00
|
|
|
if (!name_col)
|
|
|
|
throw Exception("First argument of function " + getName() + " must be a constant string",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2019-09-26 10:23:14 +00:00
|
|
|
auto model = models_loader.getModel(name_col->getValue<String>());
|
2017-10-17 10:44:46 +00:00
|
|
|
|
2020-10-14 14:15:57 +00:00
|
|
|
ColumnRawPtrs column_ptrs;
|
2018-12-26 16:44:57 +00:00
|
|
|
Columns materialized_columns;
|
|
|
|
ColumnPtr null_map;
|
|
|
|
|
2020-10-14 14:15:57 +00:00
|
|
|
column_ptrs.reserve(arguments.size());
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto arg : collections::range(1, arguments.size()))
|
2018-12-26 16:44:57 +00:00
|
|
|
{
|
2020-11-17 13:24:45 +00:00
|
|
|
const auto & column = arguments[arg].column;
|
2020-10-14 14:15:57 +00:00
|
|
|
column_ptrs.push_back(column.get());
|
2018-12-26 16:44:57 +00:00
|
|
|
if (auto full_column = column->convertToFullColumnIfConst())
|
|
|
|
{
|
|
|
|
materialized_columns.push_back(full_column);
|
2020-10-14 14:15:57 +00:00
|
|
|
column_ptrs.back() = full_column.get();
|
2018-12-26 16:44:57 +00:00
|
|
|
}
|
2020-10-14 14:15:57 +00:00
|
|
|
if (const auto * col_nullable = checkAndGetColumn<ColumnNullable>(*column_ptrs.back()))
|
2018-12-26 16:44:57 +00:00
|
|
|
{
|
|
|
|
if (!null_map)
|
|
|
|
null_map = col_nullable->getNullMapColumnPtr();
|
|
|
|
else
|
|
|
|
{
|
2020-05-14 08:30:18 +00:00
|
|
|
auto mut_null_map = IColumn::mutate(std::move(null_map));
|
2018-12-26 16:44:57 +00:00
|
|
|
|
2019-08-21 02:28:04 +00:00
|
|
|
NullMap & result_null_map = assert_cast<ColumnUInt8 &>(*mut_null_map).getData();
|
2018-12-26 16:44:57 +00:00
|
|
|
const NullMap & src_null_map = col_nullable->getNullMapColumn().getData();
|
|
|
|
|
|
|
|
for (size_t i = 0, size = result_null_map.size(); i < size; ++i)
|
|
|
|
if (src_null_map[i])
|
|
|
|
result_null_map[i] = 1;
|
|
|
|
|
|
|
|
null_map = std::move(mut_null_map);
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:15:57 +00:00
|
|
|
column_ptrs.back() = &col_nullable->getNestedColumn();
|
2018-12-26 16:44:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-14 14:15:57 +00:00
|
|
|
auto res = model->evaluate(column_ptrs);
|
2018-12-26 16:44:57 +00:00
|
|
|
|
|
|
|
if (null_map)
|
|
|
|
{
|
2020-04-22 07:03:43 +00:00
|
|
|
if (const auto * tuple = typeid_cast<const ColumnTuple *>(res.get()))
|
2018-12-26 16:44:57 +00:00
|
|
|
{
|
|
|
|
auto nested = tuple->getColumns();
|
|
|
|
for (auto & col : nested)
|
|
|
|
col = ColumnNullable::create(col, null_map);
|
|
|
|
|
|
|
|
res = ColumnTuple::create(nested);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = ColumnNullable::create(res, null_map);
|
|
|
|
}
|
2017-10-17 10:44:46 +00:00
|
|
|
|
2020-10-18 19:00:13 +00:00
|
|
|
return res;
|
2017-10-17 10:44:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 12:02:30 +00:00
|
|
|
void registerFunctionsExternalModels(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionModelEvaluate>();
|
|
|
|
}
|
|
|
|
|
2017-10-17 10:44:46 +00:00
|
|
|
}
|