getStructureOfRemoteTable.cpp 1.6 KB
Newer Older
1 2 3 4 5 6
#include <Interpreters/Cluster.h>
#include <Interpreters/Context.h>
#include <DataStreams/RemoteBlockInputStream.h>
#include <DataTypes/DataTypeFactory.h>
#include <Storages/IStorage.h>
#include <Parsers/IAST.h>
7

8
#include <TableFunctions/getStructureOfRemoteTable.h>
9 10 11 12 13 14 15


namespace DB
{

namespace ErrorCodes
{
16
    extern const int NO_REMOTE_SHARD_FOUND;
17 18 19 20
}


NamesAndTypesList getStructureOfRemoteTable(
21 22 23 24
    const Cluster & cluster,
    const std::string & database,
    const std::string & table,
    const Context & context)
25
{
26 27 28
    /// Request for a table description
    String query = "DESC TABLE " + backQuoteIfNeed(database) + "." + backQuoteIfNeed(table);
    NamesAndTypesList res;
29

30
    /// Send to the first any remote shard.
31
    const auto & shard_info = cluster.getAnyShardInfo();
32

33 34
    if (shard_info.isLocal())
        return context.getTable(database, table)->getColumnsList();
35

36
    BlockInputStreamPtr input = std::make_shared<RemoteBlockInputStream>(shard_info.pool, query, context);
37
    input->readPrefix();
38

39
    const DataTypeFactory & data_type_factory = DataTypeFactory::instance();
40

41 42 43 44 45
    while (Block current = input->read())
    {
        ColumnPtr name = current.getByName("name").column;
        ColumnPtr type = current.getByName("type").column;
        size_t size = name->size();
46

47 48 49 50
        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 &>();
51

52 53 54
            res.emplace_back(column_name, data_type_factory.get(data_type_name));
        }
    }
55

56
    return res;
57 58 59
}

}