2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.h>
|
|
|
|
#include <DataStreams/BlockIO.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Parsers/queryToString.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-11-01 14:34:05 +00:00
|
|
|
#include <TableFunctions/ITableFunction.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
2018-02-28 04:55:43 +00:00
|
|
|
#include <Interpreters/InterpreterSelectWithUnionQuery.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/InterpreterDescribeQuery.h>
|
2019-01-25 15:42:24 +00:00
|
|
|
#include <Interpreters/IdentifierSemantic.h>
|
2020-01-26 09:49:53 +00:00
|
|
|
#include <Access/AccessFlags.h>
|
2017-11-01 14:34:05 +00:00
|
|
|
#include <Parsers/ASTIdentifier.h>
|
2018-02-28 04:55:43 +00:00
|
|
|
#include <Parsers/ASTFunction.h>
|
2017-11-01 14:34:05 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2018-02-28 04:55:43 +00:00
|
|
|
#include <Parsers/TablePropertiesQueriesASTs.h>
|
2016-12-12 07:24:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
BlockIO InterpreterDescribeQuery::execute()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
BlockIO res;
|
|
|
|
res.in = executeImpl();
|
|
|
|
return res;
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block InterpreterDescribeQuery::getSampleBlock()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Block block;
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnWithTypeAndName col;
|
|
|
|
col.name = "name";
|
|
|
|
col.type = std::make_shared<DataTypeString>();
|
|
|
|
col.column = col.type->createColumn();
|
|
|
|
block.insert(col);
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
col.name = "type";
|
|
|
|
block.insert(col);
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
col.name = "default_type";
|
|
|
|
block.insert(col);
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
col.name = "default_expression";
|
|
|
|
block.insert(col);
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2019-02-19 17:02:51 +00:00
|
|
|
col.name = "comment";
|
2018-11-06 13:26:43 +00:00
|
|
|
block.insert(col);
|
|
|
|
|
2018-12-25 10:04:38 +00:00
|
|
|
col.name = "codec_expression";
|
|
|
|
block.insert(col);
|
|
|
|
|
2019-04-15 09:30:45 +00:00
|
|
|
col.name = "ttl_expression";
|
|
|
|
block.insert(col);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return block;
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr InterpreterDescribeQuery::executeImpl()
|
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
ColumnsDescription columns;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-16 21:46:53 +00:00
|
|
|
const auto & ast = query_ptr->as<ASTDescribeQuery &>();
|
|
|
|
const auto & table_expression = ast.table_expression->as<ASTTableExpression &>();
|
2019-03-14 15:20:51 +00:00
|
|
|
if (table_expression.subquery)
|
2018-02-28 04:55:43 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
auto names_and_types = InterpreterSelectWithUnionQuery::getSampleBlock(
|
|
|
|
table_expression.subquery->children.at(0), context).getNamesAndTypesList();
|
|
|
|
columns = ColumnsDescription(std::move(names_and_types));
|
2018-02-28 04:55:43 +00:00
|
|
|
}
|
2017-11-01 14:34:05 +00:00
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
StoragePtr table;
|
|
|
|
if (table_expression.table_function)
|
2017-11-01 14:34:05 +00:00
|
|
|
{
|
2019-03-16 21:46:53 +00:00
|
|
|
const auto & table_function = table_expression.table_function->as<ASTFunction &>();
|
2019-03-14 15:20:51 +00:00
|
|
|
TableFunctionPtr table_function_ptr = TableFunctionFactory::instance().get(table_function.name, context);
|
|
|
|
/// Run the table function and remember the result
|
2019-07-18 18:29:49 +00:00
|
|
|
table = table_function_ptr->execute(table_expression.table_function, context, table_function_ptr->getName());
|
2017-11-01 14:34:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-13 15:41:36 +00:00
|
|
|
auto table_id = context.resolveStorageID(table_expression.database_and_table_name);
|
2020-03-06 14:36:01 +00:00
|
|
|
context.checkAccess(AccessType::SHOW_COLUMNS, table_id);
|
2020-05-28 23:01:18 +00:00
|
|
|
table = DatabaseCatalog::instance().getTable(table_id, context);
|
2017-11-01 14:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 18:10:27 +00:00
|
|
|
auto table_lock = table->lockStructureForShare(
|
|
|
|
false, context.getInitialQueryId(), context.getSettingsRef().lock_acquire_timeout);
|
2019-03-14 15:20:51 +00:00
|
|
|
columns = table->getColumns();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
Block sample_block = getSampleBlock();
|
|
|
|
MutableColumns res_columns = sample_block.cloneEmptyColumns();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-11-01 14:34:05 +00:00
|
|
|
for (const auto & column : columns)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-15 18:23:05 +00:00
|
|
|
res_columns[0]->insert(column.name);
|
|
|
|
res_columns[1]->insert(column.type->getName());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
if (column.default_desc.expression)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[2]->insert(toString(column.default_desc.kind));
|
|
|
|
res_columns[3]->insert(queryToString(column.default_desc.expression));
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[2]->insertDefault();
|
|
|
|
res_columns[3]->insertDefault();
|
2018-11-06 13:26:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[4]->insert(column.comment);
|
2018-12-25 10:04:38 +00:00
|
|
|
|
2019-03-14 15:20:51 +00:00
|
|
|
if (column.codec)
|
|
|
|
res_columns[5]->insert(column.codec->getCodecDesc());
|
2018-12-25 10:04:38 +00:00
|
|
|
else
|
2019-03-14 15:20:51 +00:00
|
|
|
res_columns[5]->insertDefault();
|
2019-04-15 09:30:45 +00:00
|
|
|
|
|
|
|
if (column.ttl)
|
|
|
|
res_columns[6]->insert(queryToString(column.ttl));
|
|
|
|
else
|
|
|
|
res_columns[6]->insertDefault();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
return std::make_shared<OneBlockInputStream>(sample_block.cloneWithColumns(std::move(res_columns)));
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|