ClickHouse/dbms/src/Storages/StorageDistributed.cpp

57 lines
1.7 KiB
C++
Raw Normal View History

2012-05-21 20:38:34 +00:00
#include <DB/Parsers/formatAST.h>
#include <DB/DataStreams/RemoteBlockInputStream.h>
#include <DB/Storages/StorageDistributed.h>
namespace DB
{
StorageDistributed::StorageDistributed(
const std::string & name_,
NamesAndTypesListPtr columns_,
const StorageDistributed::Addresses & addresses_,
const String & remote_database_,
const String & remote_table_,
DataTypeFactory & data_type_factory_)
: name(name_), columns(columns_), addresses(addresses_),
remote_database(remote_database_), remote_table(remote_table_),
data_type_factory(data_type_factory_)
{
for (Addresses::const_iterator it = addresses.begin(); it != addresses.end(); ++it)
2012-05-23 19:51:30 +00:00
connections.push_back(new Connection(it->host().toString(), it->port(), data_type_factory, "server"));
2012-05-21 20:38:34 +00:00
}
BlockInputStreams StorageDistributed::read(
const Names & column_names,
ASTPtr query,
2012-05-22 18:32:45 +00:00
QueryProcessingStage::Enum & processed_stage,
2012-05-21 20:38:34 +00:00
size_t max_block_size,
unsigned max_threads)
{
2012-05-22 20:05:43 +00:00
processed_stage = connections.size() == 1
? QueryProcessingStage::Complete
: QueryProcessingStage::WithMergeableState;
2012-05-22 18:32:45 +00:00
2012-05-21 20:38:34 +00:00
/// Заменим в запросе имена БД и таблицы.
ASTPtr modified_query_ast = query->clone();
ASTSelectQuery & select = dynamic_cast<ASTSelectQuery &>(*modified_query_ast);
select.database = new ASTIdentifier(StringRange(), remote_database, ASTIdentifier::Database);
select.table = new ASTIdentifier(StringRange(), remote_table, ASTIdentifier::Table);
std::stringstream s;
formatAST(select, s, 0, false);
String modified_query = s.str();
BlockInputStreams res;
for (Connections::iterator it = connections.begin(); it != connections.end(); ++it)
2012-05-22 20:05:43 +00:00
res.push_back(new RemoteBlockInputStream(**it, modified_query, processed_stage));
2012-05-21 20:38:34 +00:00
return res;
}
}