2021-02-28 21:04:05 +00:00
|
|
|
#include "ExecutablePoolDictionarySource.h"
|
|
|
|
|
2021-12-25 13:34:17 +00:00
|
|
|
#include <filesystem>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Common/logger_useful.h>
|
2021-12-25 17:26:54 +00:00
|
|
|
#include <Common/LocalDateTime.h>
|
2021-12-25 13:34:17 +00:00
|
|
|
#include <Common/filesystemHelpers.h>
|
2021-09-02 11:53:20 +00:00
|
|
|
|
2021-11-01 11:22:21 +00:00
|
|
|
#include <Processors/Formats/IOutputFormat.h>
|
|
|
|
#include <Processors/Sources/ShellCommandSource.h>
|
|
|
|
#include <Processors/Sources/SourceFromSingleChunk.h>
|
2021-10-15 20:18:20 +00:00
|
|
|
#include <Formats/formatBlock.h>
|
2021-09-02 11:53:20 +00:00
|
|
|
|
2021-02-28 21:04:05 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-09-02 11:53:20 +00:00
|
|
|
|
|
|
|
#include <Dictionaries/DictionarySourceFactory.h>
|
|
|
|
#include <Dictionaries/DictionarySourceHelpers.h>
|
|
|
|
#include <Dictionaries/DictionaryStructure.h>
|
2021-02-28 21:04:05 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int DICTIONARY_ACCESS_DENIED;
|
|
|
|
extern const int UNSUPPORTED_METHOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecutablePoolDictionarySource::ExecutablePoolDictionarySource(
|
|
|
|
const DictionaryStructure & dict_struct_,
|
2021-03-06 18:45:51 +00:00
|
|
|
const Configuration & configuration_,
|
2021-02-28 21:04:05 +00:00
|
|
|
Block & sample_block_,
|
2021-12-28 09:43:30 +00:00
|
|
|
std::shared_ptr<ShellCommandSourceCoordinator> coordinator_,
|
2021-06-01 12:20:52 +00:00
|
|
|
ContextPtr context_)
|
2021-09-02 11:53:20 +00:00
|
|
|
: dict_struct(dict_struct_)
|
|
|
|
, configuration(configuration_)
|
|
|
|
, sample_block(sample_block_)
|
2021-11-01 11:22:21 +00:00
|
|
|
, coordinator(std::move(coordinator_))
|
2021-09-02 11:53:20 +00:00
|
|
|
, context(context_)
|
|
|
|
, log(&Poco::Logger::get("ExecutablePoolDictionarySource"))
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
|
|
|
/// Remove keys from sample_block for implicit_key dictionary because
|
|
|
|
/// these columns will not be returned from source
|
|
|
|
/// Implicit key means that the source script will return only values,
|
|
|
|
/// and the correspondence to the requested keys is determined implicitly - by the order of rows in the result.
|
2021-03-06 18:45:51 +00:00
|
|
|
if (configuration.implicit_key)
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
|
|
|
auto keys_names = dict_struct.getKeysNames();
|
|
|
|
|
|
|
|
for (auto & key_name : keys_names)
|
|
|
|
{
|
|
|
|
size_t key_column_position_in_block = sample_block.getPositionByName(key_name);
|
|
|
|
sample_block.erase(key_column_position_in_block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ExecutablePoolDictionarySource::ExecutablePoolDictionarySource(const ExecutablePoolDictionarySource & other)
|
2021-09-02 11:53:20 +00:00
|
|
|
: dict_struct(other.dict_struct)
|
|
|
|
, configuration(other.configuration)
|
|
|
|
, sample_block(other.sample_block)
|
2021-11-01 11:22:21 +00:00
|
|
|
, coordinator(other.coordinator)
|
2021-09-02 11:53:20 +00:00
|
|
|
, context(Context::createCopy(other.context))
|
|
|
|
, log(&Poco::Logger::get("ExecutablePoolDictionarySource"))
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
QueryPipeline ExecutablePoolDictionarySource::loadAll()
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "ExecutablePoolDictionarySource does not support loadAll method");
|
2021-02-28 21:04:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
QueryPipeline ExecutablePoolDictionarySource::loadUpdatedAll()
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD, "ExecutablePoolDictionarySource does not support loadUpdatedAll method");
|
2021-02-28 21:04:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
QueryPipeline ExecutablePoolDictionarySource::loadIds(const std::vector<UInt64> & ids)
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
|
|
|
LOG_TRACE(log, "loadIds {} size = {}", toString(), ids.size());
|
|
|
|
|
|
|
|
auto block = blockForIds(dict_struct, ids);
|
|
|
|
return getStreamForBlock(block);
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
QueryPipeline ExecutablePoolDictionarySource::loadKeys(const Columns & key_columns, const std::vector<size_t> & requested_rows)
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
|
|
|
LOG_TRACE(log, "loadKeys {} size = {}", toString(), requested_rows.size());
|
|
|
|
|
|
|
|
auto block = blockForKeys(dict_struct, key_columns, requested_rows);
|
|
|
|
return getStreamForBlock(block);
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
QueryPipeline ExecutablePoolDictionarySource::getStreamForBlock(const Block & block)
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
2021-12-25 13:34:17 +00:00
|
|
|
String command = configuration.command;
|
|
|
|
const auto & coordinator_configuration = coordinator->getConfiguration();
|
|
|
|
|
|
|
|
if (coordinator_configuration.execute_direct)
|
|
|
|
{
|
|
|
|
auto global_context = context->getGlobalContext();
|
|
|
|
auto user_scripts_path = global_context->getUserScriptsPath();
|
|
|
|
auto script_path = user_scripts_path + '/' + command;
|
|
|
|
|
|
|
|
if (!fileOrSymlinkPathStartsWith(script_path, user_scripts_path))
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
|
|
|
|
"Executable file {} must be inside user scripts folder {}",
|
|
|
|
command,
|
|
|
|
user_scripts_path);
|
|
|
|
|
2022-04-04 12:23:34 +00:00
|
|
|
if (!FS::exists(script_path))
|
2021-12-25 13:34:17 +00:00
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
|
|
|
|
"Executable file {} does not exist inside user scripts folder {}",
|
|
|
|
command,
|
|
|
|
user_scripts_path);
|
|
|
|
|
2022-04-04 12:23:34 +00:00
|
|
|
if (!FS::canExecute(script_path))
|
|
|
|
throw Exception(ErrorCodes::UNSUPPORTED_METHOD,
|
|
|
|
"Executable file {} is not executable inside user scripts folder {}",
|
|
|
|
command,
|
|
|
|
user_scripts_path);
|
|
|
|
|
2021-12-25 13:34:17 +00:00
|
|
|
command = std::move(script_path);
|
|
|
|
}
|
|
|
|
|
2021-11-01 11:22:21 +00:00
|
|
|
auto source = std::make_shared<SourceFromSingleChunk>(block);
|
|
|
|
auto shell_input_pipe = Pipe(std::move(source));
|
2021-02-28 21:04:05 +00:00
|
|
|
|
2021-09-03 10:00:40 +00:00
|
|
|
ShellCommandSourceConfiguration command_configuration;
|
|
|
|
command_configuration.read_fixed_number_of_rows = true;
|
2021-11-01 11:22:21 +00:00
|
|
|
command_configuration.number_of_rows_to_read = block.rows();
|
2023-05-03 12:59:19 +00:00
|
|
|
command_configuration.check_exit_code = true;
|
2021-02-28 21:04:05 +00:00
|
|
|
|
2021-11-01 11:22:21 +00:00
|
|
|
Pipes shell_input_pipes;
|
|
|
|
shell_input_pipes.emplace_back(std::move(shell_input_pipe));
|
2021-08-04 17:58:18 +00:00
|
|
|
|
2021-12-25 13:34:17 +00:00
|
|
|
auto pipe = coordinator->createPipe(
|
|
|
|
command,
|
|
|
|
configuration.command_arguments,
|
|
|
|
std::move(shell_input_pipes),
|
|
|
|
sample_block,
|
|
|
|
context,
|
|
|
|
command_configuration);
|
|
|
|
|
|
|
|
if (configuration.implicit_key)
|
|
|
|
pipe.addTransform(std::make_shared<TransformWithAdditionalColumns>(block, pipe.getHeader()));
|
|
|
|
|
2022-05-20 19:49:31 +00:00
|
|
|
return QueryPipeline(std::move(pipe));
|
2021-02-28 21:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExecutablePoolDictionarySource::isModified() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExecutablePoolDictionarySource::supportsSelectiveLoad() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExecutablePoolDictionarySource::hasUpdateField() const
|
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
return false;
|
2021-02-28 21:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DictionarySourcePtr ExecutablePoolDictionarySource::clone() const
|
|
|
|
{
|
2021-12-15 12:55:28 +00:00
|
|
|
return std::make_shared<ExecutablePoolDictionarySource>(*this);
|
2021-02-28 21:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ExecutablePoolDictionarySource::toString() const
|
|
|
|
{
|
2021-11-01 11:22:21 +00:00
|
|
|
size_t pool_size = coordinator->getConfiguration().pool_size;
|
|
|
|
return "ExecutablePool size: " + std::to_string(pool_size) + " command: " + configuration.command;
|
2021-02-28 21:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerDictionarySourceExecutablePool(DictionarySourceFactory & factory)
|
|
|
|
{
|
|
|
|
auto create_table_source = [=](const DictionaryStructure & dict_struct,
|
|
|
|
const Poco::Util::AbstractConfiguration & config,
|
|
|
|
const std::string & config_prefix,
|
|
|
|
Block & sample_block,
|
2021-08-12 15:16:55 +00:00
|
|
|
ContextPtr global_context,
|
2021-02-28 21:04:05 +00:00
|
|
|
const std::string & /* default_database */,
|
2021-05-24 21:27:24 +00:00
|
|
|
bool created_from_ddl) -> DictionarySourcePtr
|
2021-02-28 21:04:05 +00:00
|
|
|
{
|
|
|
|
if (dict_struct.has_expressions)
|
2021-03-06 18:45:51 +00:00
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "Dictionary source of type `executable_pool` does not support attribute expressions");
|
2021-02-28 21:04:05 +00:00
|
|
|
|
|
|
|
/// Executable dictionaries may execute arbitrary commands.
|
|
|
|
/// It's OK for dictionaries created by administrator from xml-file, but
|
|
|
|
/// maybe dangerous for dictionaries created from DDL-queries.
|
2021-08-12 15:16:55 +00:00
|
|
|
if (created_from_ddl && global_context->getApplicationType() != Context::ApplicationType::LOCAL)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::DICTIONARY_ACCESS_DENIED,
|
|
|
|
"Dictionaries with executable pool dictionary source are not allowed "
|
|
|
|
"to be created from DDL query");
|
2021-02-28 21:04:05 +00:00
|
|
|
|
2021-08-12 15:16:55 +00:00
|
|
|
ContextMutablePtr context = copyContextAndApplySettingsFromDictionaryConfig(global_context, config, config_prefix);
|
2021-02-28 21:04:05 +00:00
|
|
|
|
2021-06-10 08:34:57 +00:00
|
|
|
String settings_config_prefix = config_prefix + ".executable_pool";
|
2021-03-06 18:45:51 +00:00
|
|
|
|
2021-06-10 08:34:57 +00:00
|
|
|
size_t max_command_execution_time = config.getUInt64(settings_config_prefix + ".max_command_execution_time", 10);
|
2021-03-06 18:45:51 +00:00
|
|
|
|
2022-02-09 21:13:06 +00:00
|
|
|
size_t max_execution_time_seconds = static_cast<size_t>(context->getSettingsRef().max_execution_time.totalSeconds());
|
2021-03-12 12:37:56 +00:00
|
|
|
if (max_execution_time_seconds != 0 && max_command_execution_time > max_execution_time_seconds)
|
2021-03-06 18:45:51 +00:00
|
|
|
max_command_execution_time = max_execution_time_seconds;
|
|
|
|
|
2021-12-25 13:34:17 +00:00
|
|
|
bool execute_direct = config.getBool(settings_config_prefix + ".execute_direct", false);
|
|
|
|
std::string command_value = config.getString(settings_config_prefix + ".command");
|
|
|
|
std::vector<String> command_arguments;
|
|
|
|
|
|
|
|
if (execute_direct)
|
|
|
|
{
|
|
|
|
boost::split(command_arguments, command_value, [](char c) { return c == ' '; });
|
|
|
|
|
|
|
|
command_value = std::move(command_arguments[0]);
|
|
|
|
command_arguments.erase(command_arguments.begin());
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:45:51 +00:00
|
|
|
ExecutablePoolDictionarySource::Configuration configuration
|
|
|
|
{
|
2021-12-25 13:34:17 +00:00
|
|
|
.command = std::move(command_value),
|
|
|
|
.command_arguments = std::move(command_arguments),
|
2021-11-01 11:22:21 +00:00
|
|
|
.implicit_key = config.getBool(settings_config_prefix + ".implicit_key", false),
|
|
|
|
};
|
|
|
|
|
2021-12-28 09:43:30 +00:00
|
|
|
ShellCommandSourceCoordinator::Configuration shell_command_coordinator_configration
|
2021-11-01 11:22:21 +00:00
|
|
|
{
|
2021-06-10 08:34:57 +00:00
|
|
|
.format = config.getString(settings_config_prefix + ".format"),
|
2021-12-24 10:39:48 +00:00
|
|
|
.command_termination_timeout_seconds = config.getUInt64(settings_config_prefix + ".command_termination_timeout", 10),
|
|
|
|
.command_read_timeout_milliseconds = config.getUInt64(settings_config_prefix + ".command_read_timeout", 10000),
|
|
|
|
.command_write_timeout_milliseconds = config.getUInt64(settings_config_prefix + ".command_write_timeout", 10000),
|
2021-12-28 10:20:50 +00:00
|
|
|
.pool_size = config.getUInt64(settings_config_prefix + ".pool_size", 16),
|
2021-12-24 10:39:48 +00:00
|
|
|
.max_command_execution_time_seconds = max_command_execution_time,
|
2021-11-01 11:22:21 +00:00
|
|
|
.is_executable_pool = true,
|
|
|
|
.send_chunk_header = config.getBool(settings_config_prefix + ".send_chunk_header", false),
|
2021-12-25 13:34:17 +00:00
|
|
|
.execute_direct = execute_direct
|
2021-03-06 18:45:51 +00:00
|
|
|
};
|
|
|
|
|
2021-12-28 09:43:30 +00:00
|
|
|
auto coordinator = std::make_shared<ShellCommandSourceCoordinator>(shell_command_coordinator_configration);
|
2021-11-01 11:22:21 +00:00
|
|
|
return std::make_unique<ExecutablePoolDictionarySource>(dict_struct, configuration, sample_block, std::move(coordinator), context);
|
2021-02-28 21:04:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
factory.registerSource("executable_pool", create_table_source);
|
|
|
|
}
|
|
|
|
|
2021-08-06 08:41:45 +00:00
|
|
|
}
|