ClickHouse/src/Interpreters/InterpreterShowCreateQuery.cpp

108 lines
3.9 KiB
C++
Raw Normal View History

#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>
#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>
#include <Access/Common/AccessFlags.h>
2017-05-23 18:24:43 +00:00
#include <Interpreters/Context.h>
2023-02-17 10:36:58 +00:00
#include <Interpreters/formatWithPossiblyHidingSecrets.h>
#include <Interpreters/InterpreterShowCreateQuery.h>
#include <Parsers/ASTCreateQuery.h>
2016-12-12 07:24:56 +00:00
namespace DB
{
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;
}
2016-12-12 07:24:56 +00:00
BlockIO InterpreterShowCreateQuery::execute()
{
BlockIO res;
2021-09-15 19:35:48 +00:00
res.pipeline = executeImpl();
return res;
2016-12-12 07:24:56 +00:00
}
Block InterpreterShowCreateQuery::getSampleBlock()
{
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
{
ASTPtr create_query;
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>()))
{
2020-02-21 15:22:28 +00:00
auto resolve_table_type = show_query->temporary ? Context::ResolveExternal : Context::ResolveOrdinary;
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);
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",
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",
backQuote(ast_create_query.getDatabase()), backQuote(ast_create_query.getTable()));
2020-12-14 14:37:25 +00:00
}
}
2019-12-15 06:34:43 +00:00
else if ((show_query = query_ptr->as<ASTShowCreateDatabaseQuery>()))
{
if (show_query->temporary)
throw Exception(ErrorCodes::SYNTAX_ERROR, "Temporary databases are not possible.");
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();
}
2020-06-10 12:53:12 +00:00
if (!create_query)
throw Exception(ErrorCodes::THERE_IS_NO_QUERY,
"Unable to show the create query of {}. Maybe it was created by the system.",
show_query->getTable());
if (!getContext()->getSettingsRef().show_table_uuid_in_table_create_query_if_not_nil)
{
auto & create = create_query->as<ASTCreateQuery &>();
create.uuid = UUIDHelpers::Nil;
2021-03-08 17:26:38 +00:00
create.to_inner_uuid = UUIDHelpers::Nil;
}
MutableColumnPtr column = ColumnString::create();
2023-02-17 10:36:58 +00:00
column->insert(format({.ctx = getContext(), .query = *create_query, .one_line = false}));
ColumnConst unification (#1011) * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * Fixed error in ColumnArray::replicateGeneric [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150].
2017-07-21 06:35:58 +00:00
2021-09-15 19:35:48 +00:00
return QueryPipeline(std::make_shared<SourceFromSingleChunk>(Block{{
std::move(column),
std::make_shared<DataTypeString>(),
2021-09-15 19:35:48 +00:00
"statement"}}));
2016-12-12 07:24:56 +00:00
}
}