ClickHouse/src/Interpreters/UserDefinedExecutableFunctionFactory.cpp

186 lines
6.8 KiB
C++
Raw Normal View History

2021-09-09 13:47:48 +00:00
#include "UserDefinedExecutableFunctionFactory.h"
#include <IO/WriteHelpers.h>
2021-10-15 20:18:20 +00:00
#include <Processors/Sources/ShellCommandSource.h>
2021-11-01 11:22:21 +00:00
#include <Processors/Sources/SourceFromSingleChunk.h>
2021-10-15 20:18:20 +00:00
#include <Formats/formatBlock.h>
2021-09-09 13:47:48 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
2021-09-09 13:47:48 +00:00
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <Interpreters/ExternalUserDefinedExecutableFunctionsLoader.h>
#include <Interpreters/Context.h>
#include <Interpreters/castColumn.h>
2021-09-09 13:47:48 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int UNSUPPORTED_METHOD;
extern const int TIMEOUT_EXCEEDED;
2021-09-09 13:47:48 +00:00
}
class UserDefinedFunction final : public IFunction
{
public:
explicit UserDefinedFunction(
ExternalUserDefinedExecutableFunctionsLoader::UserDefinedExecutableFunctionPtr executable_function_,
ContextPtr context_)
: executable_function(std::move(executable_function_))
, context(context_)
{
}
String getName() const override { return executable_function->getConfiguration().name; }
bool isVariadic() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
size_t getNumberOfArguments() const override { return executable_function->getConfiguration().argument_types.size(); }
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForNulls() const override { return true; }
bool isDeterministic() const override { return false; }
DataTypePtr getReturnTypeImpl(const DataTypes &) const override
{
const auto & configuration = executable_function->getConfiguration();
return configuration.result_type;
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override
{
const auto & configuration = executable_function->getConfiguration();
auto arguments_copy = arguments;
for (size_t i = 0; i < arguments.size(); ++i)
{
auto & column_with_type = arguments_copy[i];
column_with_type.column = column_with_type.column->convertToFullColumnIfConst();
const auto & argument_type = configuration.argument_types[i];
if (areTypesEqual(arguments_copy[i].type, argument_type))
continue;
ColumnWithTypeAndName column_to_cast = {column_with_type.column, column_with_type.type, column_with_type.name};
column_with_type.column = castColumnAccurate(column_to_cast, argument_type);
column_with_type.type = argument_type;
column_with_type = column_to_cast;
}
ColumnWithTypeAndName result(result_type, "result");
Block result_block({result});
Block arguments_block(arguments_copy);
2021-11-01 11:22:21 +00:00
auto source = std::make_shared<SourceFromSingleChunk>(std::move(arguments_block));
auto shell_input_pipe = Pipe(std::move(source));
2021-11-01 11:22:21 +00:00
auto coordinator = executable_function->getCoordinator();
ShellCommandSourceConfiguration shell_command_source_configuration;
2021-11-01 11:22:21 +00:00
if (coordinator->getConfiguration().is_executable_pool)
{
shell_command_source_configuration.read_fixed_number_of_rows = true;
shell_command_source_configuration.number_of_rows_to_read = input_rows_count;
}
2021-11-01 11:22:21 +00:00
Pipes shell_input_pipes;
shell_input_pipes.emplace_back(std::move(shell_input_pipe));
Pipe pipe = coordinator->createPipe(
configuration.script_path,
configuration.script_arguments,
std::move(shell_input_pipes),
result_block,
context,
2021-11-01 11:22:21 +00:00
shell_command_source_configuration);
2021-09-20 17:54:01 +00:00
QueryPipeline pipeline(std::move(pipe));
PullingPipelineExecutor executor(pipeline);
auto result_column = result_type->createColumn();
result_column->reserve(input_rows_count);
Block block;
while (executor.pull(block))
{
const auto & result_column_to_add = *block.safeGetByPosition(0).column;
result_column->insertRangeFrom(result_column_to_add, 0, result_column_to_add.size());
}
size_t result_column_size = result_column->size();
if (result_column_size != input_rows_count)
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
"Function {} wrong result rows count expected {} actual {}",
getName(),
input_rows_count,
result_column_size);
return result_column;
}
private:
ExternalUserDefinedExecutableFunctionsLoader::UserDefinedExecutableFunctionPtr executable_function;
ContextPtr context;
};
2021-09-09 13:47:48 +00:00
UserDefinedExecutableFunctionFactory & UserDefinedExecutableFunctionFactory::instance()
{
static UserDefinedExecutableFunctionFactory result;
return result;
}
FunctionOverloadResolverPtr UserDefinedExecutableFunctionFactory::get(const String & function_name, ContextPtr context)
2021-09-09 13:47:48 +00:00
{
const auto & loader = context->getExternalUserDefinedExecutableFunctionsLoader();
auto executable_function = std::static_pointer_cast<const UserDefinedExecutableFunction>(loader.load(function_name));
auto function = std::make_shared<UserDefinedFunction>(std::move(executable_function), std::move(context));
return std::make_unique<FunctionToOverloadResolverAdaptor>(std::move(function));
2021-09-09 13:47:48 +00:00
}
FunctionOverloadResolverPtr UserDefinedExecutableFunctionFactory::tryGet(const String & function_name, ContextPtr context)
2021-09-09 13:47:48 +00:00
{
const auto & loader = context->getExternalUserDefinedExecutableFunctionsLoader();
auto load_result = loader.getLoadResult(function_name);
2021-09-09 13:47:48 +00:00
if (load_result.object)
{
auto executable_function = std::static_pointer_cast<const UserDefinedExecutableFunction>(load_result.object);
auto function = std::make_shared<UserDefinedFunction>(std::move(executable_function), std::move(context));
return std::make_unique<FunctionToOverloadResolverAdaptor>(std::move(function));
}
2021-09-09 13:47:48 +00:00
return nullptr;
2021-09-09 13:47:48 +00:00
}
2021-10-27 15:49:18 +00:00
bool UserDefinedExecutableFunctionFactory::has(const String & function_name, ContextPtr context)
{
const auto & loader = context->getExternalUserDefinedExecutableFunctionsLoader();
auto load_result = loader.getLoadResult(function_name);
bool result = load_result.object != nullptr;
return result;
}
std::vector<String> UserDefinedExecutableFunctionFactory::getRegisteredNames(ContextPtr context)
2021-09-09 13:47:48 +00:00
{
const auto & loader = context->getExternalUserDefinedExecutableFunctionsLoader();
auto loaded_objects = loader.getLoadedObjects();
2021-09-09 13:47:48 +00:00
std::vector<std::string> registered_names;
registered_names.reserve(loaded_objects.size());
2021-09-09 13:47:48 +00:00
for (auto & loaded_object : loaded_objects)
registered_names.emplace_back(loaded_object->getLoadableName());
2021-09-09 13:47:48 +00:00
return registered_names;
}
}