ClickHouse/dbms/src/Dictionaries/ExecutableDictionarySource.cpp

138 lines
3.8 KiB
C++
Raw Normal View History

2016-11-15 19:51:06 +00:00
#include <DB/Dictionaries/ExecutableDictionarySource.h>
#include <DB/Common/ShellCommand.h>
#include <DB/Interpreters/Context.h>
2016-11-17 01:09:46 +00:00
#include <DB/Dictionaries/OwningBlockInputStream.h>
2016-11-15 19:51:06 +00:00
2016-11-18 01:48:13 +00:00
#include <DB/DataStreams/IBlockOutputStream.h>
#include <DB/DataTypes/DataTypesNumberFixed.h>
2016-11-15 19:51:06 +00:00
namespace DB
{
2016-11-22 15:03:54 +00:00
ExecutableDictionarySource::ExecutableDictionarySource(const DictionaryStructure & dict_struct_,
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
Block & sample_block, const Context & context) :
2016-11-18 01:48:13 +00:00
dict_struct{dict_struct_},
2016-11-23 22:44:53 +00:00
path{config.getString(config_prefix + ".path")},
2016-11-22 15:03:54 +00:00
format{config.getString(config_prefix + ".format")},
2016-11-18 01:48:13 +00:00
sample_block{sample_block},
context(context)
2016-11-15 19:51:06 +00:00
{
}
2016-11-18 01:48:13 +00:00
ExecutableDictionarySource::ExecutableDictionarySource(const ExecutableDictionarySource & other) :
dict_struct{other.dict_struct},
2016-11-23 22:44:53 +00:00
path{other.path},
2016-11-15 19:51:06 +00:00
format{other.format},
2016-11-18 01:48:13 +00:00
sample_block{other.sample_block},
context(other.context)
2016-11-15 19:51:06 +00:00
{
}
BlockInputStreamPtr ExecutableDictionarySource::loadAll()
{
2016-11-18 20:20:08 +00:00
LOG_TRACE(log, "loadAll " + toString());
2016-11-23 22:44:53 +00:00
auto process = ShellCommand::execute(path);
2016-11-17 01:09:46 +00:00
auto stream = context.getInputFormat(format, process->out, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ShellCommand>>(stream, std::move(process));
}
2016-11-15 19:51:06 +00:00
2016-11-17 01:09:46 +00:00
BlockInputStreamPtr ExecutableDictionarySource::loadIds(const std::vector<UInt64> & ids)
{
2016-11-25 20:37:06 +00:00
LOG_TRACE(log, "loadIds " + toString() + " ids=" + std::to_string(ids.size()));
2016-11-23 22:44:53 +00:00
auto process = ShellCommand::execute(path);
2016-11-15 19:51:06 +00:00
2016-11-18 01:48:13 +00:00
{
ColumnWithTypeAndName column;
column.type = std::make_shared<DataTypeUInt64>();
column.column = column.type->createColumn();
for (auto & id : ids) {
2016-11-18 20:20:08 +00:00
column.column->insert(id); //CHECKME maybe faster?
2016-11-18 01:48:13 +00:00
}
Block block;
block.insert(std::move(column));
auto stream_out = context.getOutputFormat(format, process->in, sample_block);
2016-11-25 20:37:06 +00:00
stream_out->writePrefix();
2016-11-18 01:48:13 +00:00
stream_out->write(block);
2016-11-25 20:37:06 +00:00
stream_out->writeSuffix();
stream_out->flush();
2016-11-18 01:48:13 +00:00
}
2016-11-17 01:09:46 +00:00
process->in.close();
2016-11-16 21:02:07 +00:00
2016-11-25 20:37:06 +00:00
/*
2016-11-17 01:09:46 +00:00
std::string process_err;
readStringUntilEOF(process_err, process->err);
2016-11-25 20:37:06 +00:00
std::cerr << "readed STDERR [" << process_err << "] " << std::endl;
*/
2016-11-15 19:51:06 +00:00
2016-11-17 01:09:46 +00:00
auto stream = context.getInputFormat( format, process->out, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ShellCommand>>(stream, std::move(process));
2016-11-15 19:51:06 +00:00
}
BlockInputStreamPtr ExecutableDictionarySource::loadKeys(
const ConstColumnPlainPtrs & key_columns, const std::vector<std::size_t> & requested_rows)
{
2016-11-25 20:37:06 +00:00
LOG_TRACE(log, "loadKeys " + toString() + " rows=" + std::to_string(requested_rows.size()));
2016-11-23 22:44:53 +00:00
auto process = ShellCommand::execute(path);
2016-11-18 01:48:13 +00:00
{
Block block;
2016-11-18 20:20:08 +00:00
const auto keys_size = key_columns.size();
for (const auto i : ext::range(0, keys_size))
{
const auto & key_description = (*dict_struct.key)[i];
const auto & key = key_columns[i];
2016-11-18 01:48:13 +00:00
ColumnWithTypeAndName column;
2016-11-18 20:20:08 +00:00
column.type = key_description.type;
column.column = key->clone(); // CHECKME !!
2016-11-18 01:48:13 +00:00
block.insert(std::move(column));
}
auto stream_out = context.getOutputFormat(format, process->in, sample_block);
2016-11-25 20:37:06 +00:00
stream_out->writePrefix();
2016-11-18 01:48:13 +00:00
stream_out->write(block);
2016-11-25 20:37:06 +00:00
stream_out->writeSuffix();
stream_out->flush();
2016-11-18 01:48:13 +00:00
}
process->in.close();
2016-11-25 20:37:06 +00:00
/*
2016-11-18 01:48:13 +00:00
std::string process_err;
readStringUntilEOF(process_err, process->err);
2016-11-25 20:37:06 +00:00
std::cerr << "readed STDERR [" << process_err << "] " << std::endl;
*/
2016-11-18 01:48:13 +00:00
auto stream = context.getInputFormat( format, process->out, sample_block, max_block_size);
return std::make_shared<OwningBlockInputStream<ShellCommand>>(stream, std::move(process));
2016-11-15 19:51:06 +00:00
}
bool ExecutableDictionarySource::isModified() const
{
2016-11-18 01:48:13 +00:00
return true;
2016-11-15 19:51:06 +00:00
}
bool ExecutableDictionarySource::supportsSelectiveLoad() const
{
2016-11-23 22:44:53 +00:00
return true;
2016-11-15 19:51:06 +00:00
}
DictionarySourcePtr ExecutableDictionarySource::clone() const
{
return std::make_unique<ExecutableDictionarySource>(*this);
}
std::string ExecutableDictionarySource::toString() const
{
2016-11-23 22:44:53 +00:00
return "Executable: " + path;
2016-11-15 19:51:06 +00:00
}
}