2018-04-09 13:52:39 +00:00
|
|
|
#include "getStructureOfRemoteTable.h"
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Cluster.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2019-01-09 12:21:04 +00:00
|
|
|
#include <Interpreters/ClusterProxy/executeQuery.h>
|
2018-02-15 18:54:12 +00:00
|
|
|
#include <Interpreters/InterpreterDescribeQuery.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/RemoteBlockInputStream.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2019-02-19 17:02:51 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
2018-03-12 13:47:01 +00:00
|
|
|
#include <Parsers/ExpressionListParsers.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2018-07-24 14:05:37 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2018-07-24 13:10:34 +00:00
|
|
|
#include <TableFunctions/TableFunctionFactory.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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-12 13:47:01 +00:00
|
|
|
ColumnsDescription getStructureOfRemoteTable(
|
2017-04-01 07:20:54 +00:00
|
|
|
const Cluster & cluster,
|
|
|
|
const std::string & database,
|
|
|
|
const std::string & table,
|
2018-07-24 13:10:34 +00:00
|
|
|
const Context & context,
|
|
|
|
const ASTPtr & table_func_ptr)
|
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();
|
2018-07-27 21:33:30 +00:00
|
|
|
|
2018-07-24 13:10:34 +00:00
|
|
|
String query;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2018-07-24 13:10:34 +00:00
|
|
|
if (table_func_ptr)
|
|
|
|
{
|
|
|
|
if (shard_info.isLocal())
|
|
|
|
{
|
2019-03-11 13:22:51 +00:00
|
|
|
const auto * table_function = table_func_ptr->as<ASTFunction>();
|
2019-07-18 18:29:49 +00:00
|
|
|
TableFunctionPtr table_function_ptr = TableFunctionFactory::instance().get(table_function->name, context);
|
|
|
|
return table_function_ptr->execute(table_func_ptr, context, table_function_ptr->getName())->getColumns();
|
2018-07-24 13:10:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 12:31:47 +00:00
|
|
|
auto table_func_name = queryToString(table_func_ptr);
|
2018-07-24 13:10:34 +00:00
|
|
|
query = "DESC TABLE " + table_func_name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (shard_info.isLocal())
|
|
|
|
return context.getTable(database, table)->getColumns();
|
|
|
|
|
|
|
|
/// Request for a table description
|
|
|
|
query = "DESC TABLE " + backQuoteIfNeed(database) + "." + backQuoteIfNeed(table);
|
|
|
|
}
|
2018-03-12 13:47:01 +00:00
|
|
|
|
|
|
|
ColumnsDescription res;
|
2016-05-13 03:22:16 +00:00
|
|
|
|
2019-01-09 12:21:04 +00:00
|
|
|
auto new_context = ClusterProxy::removeUserRestrictionsFromSettings(context, context.getSettingsRef());
|
2019-02-19 17:02:51 +00:00
|
|
|
|
|
|
|
/// Expect only needed columns from the result of DESC TABLE. NOTE 'comment' column is ignored for compatibility reasons.
|
|
|
|
Block sample_block
|
|
|
|
{
|
|
|
|
{ ColumnString::create(), std::make_shared<DataTypeString>(), "name" },
|
|
|
|
{ ColumnString::create(), std::make_shared<DataTypeString>(), "type" },
|
|
|
|
{ ColumnString::create(), std::make_shared<DataTypeString>(), "default_type" },
|
|
|
|
{ ColumnString::create(), std::make_shared<DataTypeString>(), "default_expression" },
|
|
|
|
};
|
|
|
|
|
2019-01-09 12:21:04 +00:00
|
|
|
/// Execute remote query without restrictions (because it's not real user query, but part of implementation)
|
2019-02-19 17:02:51 +00:00
|
|
|
auto input = std::make_shared<RemoteBlockInputStream>(shard_info.pool, query, sample_block, new_context);
|
2018-02-07 14:55:23 +00:00
|
|
|
input->setPoolMode(PoolMode::GET_ONE);
|
2018-07-24 13:10:34 +00:00
|
|
|
if (!table_func_ptr)
|
|
|
|
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
|
|
|
|
2018-03-12 13:47:01 +00:00
|
|
|
ParserExpression expr_parser;
|
2018-07-25 12:31:47 +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;
|
2018-03-12 13:47:01 +00:00
|
|
|
ColumnPtr default_kind = current.getByName("default_type").column;
|
|
|
|
ColumnPtr default_expr = current.getByName("default_expression").column;
|
2017-04-01 07:20:54 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
ColumnDescription column;
|
2018-03-12 13:47:01 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
column.name = (*name)[i].get<const String &>();
|
2018-03-12 13:47:01 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
String data_type_name = (*type)[i].get<const String &>();
|
|
|
|
column.type = data_type_factory.get(data_type_name);
|
2018-03-12 13:47:01 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
String kind_name = (*default_kind)[i].get<const String &>();
|
|
|
|
if (!kind_name.empty())
|
|
|
|
{
|
|
|
|
column.default_desc.kind = columnDefaultKindFromString(kind_name);
|
2018-03-12 13:47:01 +00:00
|
|
|
String expr_str = (*default_expr)[i].get<const String &>();
|
2019-03-14 15:20:51 +00:00
|
|
|
column.default_desc.expression = parseQuery(
|
|
|
|
expr_parser, expr_str.data(), expr_str.data() + expr_str.size(), "default expression", 0);
|
2018-03-12 13:47:01 +00:00
|
|
|
}
|
2019-03-14 15:20:51 +00:00
|
|
|
|
|
|
|
res.add(column);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|