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>
|
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
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return block;
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr InterpreterDescribeQuery::executeImpl()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ASTDescribeQuery & ast = typeid_cast<const ASTDescribeQuery &>(*query_ptr);
|
|
|
|
|
2017-12-25 21:57:29 +00:00
|
|
|
NamesAndTypesList columns;
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnDefaults column_defaults;
|
2017-11-01 14:34:05 +00:00
|
|
|
StoragePtr table;
|
|
|
|
|
|
|
|
auto table_expression = typeid_cast<const ASTTableExpression *>(ast.table_expression.get());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-11-01 14:34:05 +00:00
|
|
|
if (table_expression->subquery)
|
2018-02-28 04:55:43 +00:00
|
|
|
{
|
|
|
|
columns = InterpreterSelectWithUnionQuery::getSampleBlock(table_expression->subquery->children[0], context).getNamesAndTypesList();
|
|
|
|
}
|
2017-11-01 14:34:05 +00:00
|
|
|
else
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-11-01 14:34:05 +00:00
|
|
|
if (table_expression->table_function)
|
|
|
|
{
|
|
|
|
auto table_function = typeid_cast<const ASTFunction *>(table_expression->table_function.get());
|
|
|
|
/// Get the table function
|
|
|
|
TableFunctionPtr table_function_ptr = TableFunctionFactory::instance().get(table_function->name, context);
|
|
|
|
/// Run it and remember the result
|
|
|
|
table = table_function_ptr->execute(table_expression->table_function, context);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
String database_name;
|
|
|
|
String table_name;
|
|
|
|
|
|
|
|
auto identifier = table_expression->database_and_table_name;
|
|
|
|
if (identifier->children.size() > 2)
|
|
|
|
throw Exception("Logical error: more than two components in table expression", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
if (identifier->children.size() > 1)
|
|
|
|
{
|
|
|
|
auto database_ptr = identifier->children[0];
|
|
|
|
auto table_ptr = identifier->children[1];
|
|
|
|
|
|
|
|
if (database_ptr)
|
|
|
|
database_name = typeid_cast<ASTIdentifier &>(*database_ptr).name;
|
|
|
|
if (table_ptr)
|
|
|
|
table_name = typeid_cast<ASTIdentifier &>(*table_ptr).name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
table_name = typeid_cast<ASTIdentifier &>(*identifier).name;
|
|
|
|
|
|
|
|
table = context.getTable(database_name, table_name);
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:05:23 +00:00
|
|
|
auto table_lock = table->lockStructure(false, __PRETTY_FUNCTION__);
|
2018-03-13 14:18:11 +00:00
|
|
|
columns = table->getColumns().getAll();
|
|
|
|
column_defaults = table->getColumns().defaults;
|
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
|
|
|
|
|
|
|
const auto it = column_defaults.find(column.name);
|
|
|
|
if (it == std::end(column_defaults))
|
|
|
|
{
|
2017-12-15 18:23:05 +00:00
|
|
|
res_columns[2]->insertDefault();
|
|
|
|
res_columns[3]->insertDefault();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-12 13:47:01 +00:00
|
|
|
res_columns[2]->insert(toString(it->second.kind));
|
2017-12-15 18:23:05 +00:00
|
|
|
res_columns[3]->insert(queryToString(it->second.expression));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|