2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Parsers/TablePropertiesQueriesASTs.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
2021-09-15 19:35:48 +00:00
|
|
|
#include <Processors/Sources/SourceFromSingleChunk.h>
|
2021-10-15 20:18:20 +00:00
|
|
|
#include <QueryPipeline/BlockIO.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2021-10-31 08:51:20 +00:00
|
|
|
#include <Access/Common/AccessFlags.h>
|
2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterShowCreateQuery.h>
|
2020-05-07 11:29:58 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
2016-12-12 07:24:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-02-14 07:15:38 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2018-03-07 19:52:22 +00:00
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
extern const int THERE_IS_NO_QUERY;
|
2020-12-17 04:27:04 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2018-02-14 07:15:38 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 07:24:56 +00:00
|
|
|
BlockIO InterpreterShowCreateQuery::execute()
|
|
|
|
{
|
|
|
|
BlockIO res;
|
2021-09-15 19:35:48 +00:00
|
|
|
res.pipeline = executeImpl();
|
2016-12-12 07:24:56 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Block InterpreterShowCreateQuery::getSampleBlock()
|
|
|
|
{
|
2018-02-15 18:54:12 +00:00
|
|
|
return Block{{
|
|
|
|
ColumnString::create(),
|
|
|
|
std::make_shared<DataTypeString>(),
|
|
|
|
"statement"}};
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-15 19:35:48 +00:00
|
|
|
QueryPipeline InterpreterShowCreateQuery::executeImpl()
|
2016-12-12 07:24:56 +00:00
|
|
|
{
|
2018-03-13 13:28:32 +00:00
|
|
|
ASTPtr create_query;
|
2019-10-11 13:21:52 +00:00
|
|
|
ASTQueryWithTableAndOutput * show_query;
|
2020-12-14 14:37:25 +00:00
|
|
|
if ((show_query = query_ptr->as<ASTShowCreateTableQuery>()) ||
|
2021-04-21 13:45:13 +00:00
|
|
|
(show_query = query_ptr->as<ASTShowCreateViewQuery>()) ||
|
|
|
|
(show_query = query_ptr->as<ASTShowCreateDictionaryQuery>()))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
2020-02-21 15:22:28 +00:00
|
|
|
auto resolve_table_type = show_query->temporary ? Context::ResolveExternal : Context::ResolveOrdinary;
|
2021-04-10 23:33:54 +00:00
|
|
|
auto table_id = getContext()->resolveStorageID(*show_query, resolve_table_type);
|
2021-04-21 13:45:13 +00:00
|
|
|
|
|
|
|
bool is_dictionary = static_cast<bool>(query_ptr->as<ASTShowCreateDictionaryQuery>());
|
|
|
|
|
|
|
|
if (is_dictionary)
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_DICTIONARIES, table_id);
|
|
|
|
else
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_COLUMNS, table_id);
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
create_query = DatabaseCatalog::instance().getDatabase(table_id.database_name)->getCreateTableQuery(table_id.table_name, getContext());
|
2021-04-21 13:45:13 +00:00
|
|
|
|
|
|
|
auto & ast_create_query = create_query->as<ASTCreateQuery &>();
|
2020-12-14 14:37:25 +00:00
|
|
|
if (query_ptr->as<ASTShowCreateViewQuery>())
|
|
|
|
{
|
|
|
|
if (!ast_create_query.isView())
|
2021-04-21 13:45:13 +00:00
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "{}.{} is not a VIEW",
|
2021-09-06 22:13:54 +00:00
|
|
|
backQuote(ast_create_query.getDatabase()), backQuote(ast_create_query.getTable()));
|
2021-04-21 13:45:13 +00:00
|
|
|
}
|
|
|
|
else if (is_dictionary)
|
|
|
|
{
|
|
|
|
if (!ast_create_query.is_dictionary)
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "{}.{} is not a DICTIONARY",
|
2021-09-06 22:13:54 +00:00
|
|
|
backQuote(ast_create_query.getDatabase()), backQuote(ast_create_query.getTable()));
|
2020-12-14 14:37:25 +00:00
|
|
|
}
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
2019-12-15 06:34:43 +00:00
|
|
|
else if ((show_query = query_ptr->as<ASTShowCreateDatabaseQuery>()))
|
2019-10-11 13:21:52 +00:00
|
|
|
{
|
|
|
|
if (show_query->temporary)
|
|
|
|
throw Exception("Temporary databases are not possible.", ErrorCodes::SYNTAX_ERROR);
|
2021-09-06 22:13:54 +00:00
|
|
|
show_query->setDatabase(getContext()->resolveDatabase(show_query->getDatabase()));
|
|
|
|
getContext()->checkAccess(AccessType::SHOW_DATABASES, show_query->getDatabase());
|
|
|
|
create_query = DatabaseCatalog::instance().getDatabase(show_query->getDatabase())->getCreateDatabaseQuery();
|
2019-10-11 13:21:52 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 12:53:12 +00:00
|
|
|
if (!create_query)
|
2021-09-06 22:13:54 +00:00
|
|
|
throw Exception("Unable to show the create query of " + show_query->getTable() + ". Maybe it was created by the system.", ErrorCodes::THERE_IS_NO_QUERY);
|
2018-02-14 04:00:37 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
if (!getContext()->getSettingsRef().show_table_uuid_in_table_create_query_if_not_nil)
|
2020-05-07 11:29:58 +00:00
|
|
|
{
|
|
|
|
auto & create = create_query->as<ASTCreateQuery &>();
|
|
|
|
create.uuid = UUIDHelpers::Nil;
|
2021-03-08 17:26:38 +00:00
|
|
|
create.to_inner_uuid = UUIDHelpers::Nil;
|
2020-05-07 11:29:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 16:05:40 +00:00
|
|
|
WriteBufferFromOwnString buf;
|
|
|
|
formatAST(*create_query, buf, false, false);
|
|
|
|
String res = buf.str();
|
2016-12-12 07:24:56 +00:00
|
|
|
|
2017-12-15 18:23:05 +00:00
|
|
|
MutableColumnPtr column = ColumnString::create();
|
2017-07-21 06:35:58 +00:00
|
|
|
column->insert(res);
|
|
|
|
|
2021-09-15 19:35:48 +00:00
|
|
|
return QueryPipeline(std::make_shared<SourceFromSingleChunk>(Block{{
|
2017-12-15 18:23:05 +00:00
|
|
|
std::move(column),
|
2016-12-12 07:24:56 +00:00
|
|
|
std::make_shared<DataTypeString>(),
|
2021-09-15 19:35:48 +00:00
|
|
|
"statement"}}));
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|