2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Cluster.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <DataStreams/RemoteBlockInputStream.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Parsers/IAST.h>
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <TableFunctions/getStructureOfRemoteTable.h>
|
2016-05-13 03:22:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int NO_REMOTE_SHARD_FOUND;
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList getStructureOfRemoteTable(
|
2017-04-01 07:20:54 +00:00
|
|
|
const Cluster & cluster,
|
|
|
|
const std::string & database,
|
|
|
|
const std::string & table,
|
|
|
|
const Context & context)
|
2016-05-13 03:22:16 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Request for a table description
|
|
|
|
String query = "DESC TABLE " + backQuoteIfNeed(database) + "." + backQuoteIfNeed(table);
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList res;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-08-12 21:02:45 +00:00
|
|
|
/// Send to the first any remote shard.
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto & shard_info = cluster.getAnyShardInfo();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (shard_info.isLocal())
|
|
|
|
return context.getTable(database, table)->getColumnsList();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2018-02-07 14:55:23 +00:00
|
|
|
auto input = std::make_shared<RemoteBlockInputStream>(shard_info.pool, query, context);
|
|
|
|
input->setPoolMode(PoolMode::GET_ONE);
|
|
|
|
input->setMainTable(QualifiedTableName{database, table});
|
2017-04-01 07:20:54 +00:00
|
|
|
input->readPrefix();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const DataTypeFactory & data_type_factory = DataTypeFactory::instance();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (Block current = input->read())
|
|
|
|
{
|
|
|
|
ColumnPtr name = current.getByName("name").column;
|
|
|
|
ColumnPtr type = current.getByName("type").column;
|
|
|
|
size_t size = name->size();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
String column_name = (*name)[i].get<const String &>();
|
|
|
|
String data_type_name = (*type)[i].get<const String &>();
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res.emplace_back(column_name, data_type_factory.get(data_type_name));
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2016-05-13 03:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|