2017-04-01 09:19:00 +00:00
|
|
|
#include <Dictionaries/ExecutableDictionarySource.h>
|
2016-11-15 19:51:06 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ShellCommand.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2017-05-25 19:26:17 +00:00
|
|
|
#include <DataStreams/OwningBlockInputStream.h>
|
2017-05-25 19:21:57 +00:00
|
|
|
#include <Dictionaries/DictionarySourceHelpers.h>
|
2016-11-15 19:51:06 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/IBlockOutputStream.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2016-11-18 01:48:13 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
#include <common/logger_useful.h>
|
|
|
|
|
|
|
|
|
2016-11-15 19:51:06 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-12-08 02:49:04 +00:00
|
|
|
static const size_t max_block_size = 8192;
|
|
|
|
|
|
|
|
|
2016-11-22 15:03:54 +00:00
|
|
|
ExecutableDictionarySource::ExecutableDictionarySource(const DictionaryStructure & dict_struct_,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
|
|
|
|
Block & sample_block, const Context & context)
|
|
|
|
: log(&Logger::get("ExecutableDictionarySource")),
|
|
|
|
dict_struct{dict_struct_},
|
|
|
|
command{config.getString(config_prefix + ".command")},
|
|
|
|
format{config.getString(config_prefix + ".format")},
|
|
|
|
sample_block{sample_block},
|
|
|
|
context(context)
|
2016-11-15 19:51:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
ExecutableDictionarySource::ExecutableDictionarySource(const ExecutableDictionarySource & other)
|
2017-04-01 07:20:54 +00:00
|
|
|
: log(&Logger::get("ExecutableDictionarySource")),
|
|
|
|
dict_struct{other.dict_struct},
|
|
|
|
command{other.command},
|
|
|
|
format{other.format},
|
|
|
|
sample_block{other.sample_block},
|
|
|
|
context(other.context)
|
2016-11-15 19:51:06 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreamPtr ExecutableDictionarySource::loadAll()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
LOG_TRACE(log, "loadAll " + toString());
|
|
|
|
auto process = ShellCommand::execute(command);
|
2017-05-25 19:21:57 +00:00
|
|
|
auto input_stream = context.getInputFormat(format, process->out, sample_block, max_block_size);
|
|
|
|
return std::make_shared<OwningBlockInputStream<ShellCommand>>(input_stream, std::move(process));
|
2016-11-27 13:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreamPtr ExecutableDictionarySource::loadIds(const std::vector<UInt64> & ids)
|
|
|
|
{
|
2017-05-25 18:24:47 +00:00
|
|
|
LOG_TRACE(log, "loadIds " << toString() << " size = " << ids.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
auto process = ShellCommand::execute(command);
|
2017-05-25 18:24:47 +00:00
|
|
|
|
2017-05-25 19:21:57 +00:00
|
|
|
auto output_stream = context.getOutputFormat(format, process->in, sample_block);
|
|
|
|
formatIDs(output_stream, ids);
|
2017-04-01 07:20:54 +00:00
|
|
|
process->in.close();
|
2016-11-16 21:02:07 +00:00
|
|
|
|
2017-05-25 19:21:57 +00:00
|
|
|
auto input_stream = context.getInputFormat(format, process->out, sample_block, max_block_size);
|
|
|
|
return std::make_shared<OwningBlockInputStream<ShellCommand>>(input_stream, std::move(process));
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlockInputStreamPtr ExecutableDictionarySource::loadKeys(
|
2017-05-25 19:52:05 +00:00
|
|
|
const Columns & key_columns, const std::vector<std::size_t> & requested_rows)
|
2016-11-15 19:51:06 +00:00
|
|
|
{
|
2017-05-25 18:24:47 +00:00
|
|
|
LOG_TRACE(log, "loadKeys " << toString() << " size = " << requested_rows.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
auto process = ShellCommand::execute(command);
|
2016-11-18 01:48:13 +00:00
|
|
|
|
2017-05-25 19:21:57 +00:00
|
|
|
auto output_stream = context.getOutputFormat(format, process->in, sample_block);
|
|
|
|
formatKeys(dict_struct, output_stream, key_columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
process->in.close();
|
2016-11-18 01:48:13 +00:00
|
|
|
|
2017-05-25 19:21:57 +00:00
|
|
|
auto input_stream = context.getInputFormat(format, process->out, sample_block, max_block_size);
|
|
|
|
return std::make_shared<OwningBlockInputStream<ShellCommand>>(input_stream, std::move(process));
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExecutableDictionarySource::isModified() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ExecutableDictionarySource::supportsSelectiveLoad() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DictionarySourcePtr ExecutableDictionarySource::clone() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_unique<ExecutableDictionarySource>(*this);
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string ExecutableDictionarySource::toString() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return "Executable: " + command;
|
2016-11-15 19:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|