ClickHouse/dbms/src/Dictionaries/ExecutableDictionarySource.cpp

144 lines
4.0 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/IO/WriteBufferFromOStream.h>
#include <DB/DataStreams/IBlockOutputStream.h>
#include <DB/DataTypes/DataTypesNumberFixed.h>
2016-11-15 19:51:06 +00:00
namespace DB
{
2016-11-18 01:48:13 +00:00
ExecutableDictionarySource::ExecutableDictionarySource(const DictionaryStructure & dict_struct_, const std::string & name, const std::string & format, Block & sample_block, const Context & context) :
dict_struct{dict_struct_},
name{name},
format{format},
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},
name{other.name},
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 01:48:13 +00:00
//std::cerr << "ExecutableDictionarySource::loadAll " <<std::endl;
2016-11-17 01:09:46 +00:00
LOG_TRACE(log, "execute " + name);
auto process = ShellCommand::execute(name);
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-18 01:48:13 +00:00
//std::cerr << "ExecutableDictionarySource::loadIds s=" << ids.size() <<std::endl;
2016-11-17 01:09:46 +00:00
auto process = ShellCommand::execute(name);
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) {
column.column->insert(id); //maybe faster?
}
Block block;
block.insert(std::move(column));
auto stream_out = context.getOutputFormat(format, process->in, sample_block);
stream_out->write(block);
}
/*
2016-11-17 01:09:46 +00:00
for (auto & id : ids) {
writeString(std::to_string(id), process->in);
2016-11-18 01:48:13 +00:00
writeString("\n", process->in);
2016-11-15 19:51:06 +00:00
}
2016-11-18 01:48:13 +00:00
*/
2016-11-15 19:51:06 +00:00
2016-11-17 01:09:46 +00:00
process->in.close();
2016-11-16 21:02:07 +00:00
2016-11-18 01:48:13 +00:00
/*
2016-11-17 01:09:46 +00:00
std::string process_err;
readStringUntilEOF(process_err, process->err);
std::cerr << "readed ERR [" << process_err << "] " << std::endl;
2016-11-18 01:48:13 +00:00
*/
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-18 01:48:13 +00:00
//std::cerr << " ExecutableDictionarySource::loadKeys cols=" << key_columns.size() << " rows=" <<requested_rows.size() << std::endl;
auto process = ShellCommand::execute(name);
{
Block block;
for(auto & key : key_columns) {
ColumnWithTypeAndName column;
column.type = std::make_shared<DataTypeUInt64>(); // TODO TYPE
//column.column = column.type->createColumn();
//column.column.reset(const_cast<DB::IColumn*>(key));
column.column = key->clone(); // wrong!
//column.column = key->convertToFullColumnIfConst(); // check!
block.insert(std::move(column));
}
auto stream_out = context.getOutputFormat(format, process->in, sample_block);
stream_out->write(block);
}
/*
for (const auto row : requested_rows)
{
writeString(std::to_string(row), process->in);
writeString("\n", process->in); // TODO: format?
}
*/
process->in.close();
/*
std::string process_err;
readStringUntilEOF(process_err, process->err);
std::cerr << "readed ERR [" << process_err << "] " << std::endl;
*/
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-17 01:09:46 +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
{
return "Executable: " + name;
}
}