Merge pull request #31062 from Enmk/Governance/view_comment

Views with comment
This commit is contained in:
Maksim Kita 2021-12-16 12:27:12 +03:00 committed by GitHub
commit 33374f8b0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 41 additions and 10 deletions

View File

@ -427,8 +427,11 @@ void ASTCreateQuery::formatQueryImpl(const FormatSettings & settings, FormatStat
if (select) if (select)
{ {
settings.ostr << (settings.hilite ? hilite_keyword : "") << " AS" << settings.nl_or_ws << (settings.hilite ? hilite_none : ""); settings.ostr << (settings.hilite ? hilite_keyword : "") << " AS"
<< (comment ? "(" : "")
<< settings.nl_or_ws << (settings.hilite ? hilite_none : "");
select->formatImpl(settings, state, frame); select->formatImpl(settings, state, frame);
settings.ostr << (comment ? ")" : "");
} }
if (tables) if (tables)

View File

@ -747,6 +747,7 @@ bool ParserCreateLiveViewQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & e
if (!select_p.parse(pos, select, expected)) if (!select_p.parse(pos, select, expected))
return false; return false;
auto comment = parseComment(pos, expected);
auto query = std::make_shared<ASTCreateQuery>(); auto query = std::make_shared<ASTCreateQuery>();
node = query; node = query;
@ -781,6 +782,9 @@ bool ParserCreateLiveViewQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & e
if (live_view_periodic_refresh) if (live_view_periodic_refresh)
query->live_view_periodic_refresh.emplace(live_view_periodic_refresh->as<ASTLiteral &>().value.safeGet<UInt64>()); query->live_view_periodic_refresh.emplace(live_view_periodic_refresh->as<ASTLiteral &>().value.safeGet<UInt64>());
if (comment)
query->set(query->comment, comment);
return true; return true;
} }

View File

@ -280,7 +280,8 @@ StorageLiveView::StorageLiveView(
const StorageID & table_id_, const StorageID & table_id_,
ContextPtr context_, ContextPtr context_,
const ASTCreateQuery & query, const ASTCreateQuery & query,
const ColumnsDescription & columns_) const ColumnsDescription & columns_,
const String & comment)
: IStorage(table_id_) : IStorage(table_id_)
, WithContext(context_->getGlobalContext()) , WithContext(context_->getGlobalContext())
{ {
@ -291,6 +292,9 @@ StorageLiveView::StorageLiveView(
StorageInMemoryMetadata storage_metadata; StorageInMemoryMetadata storage_metadata;
storage_metadata.setColumns(columns_); storage_metadata.setColumns(columns_);
if (!comment.empty())
storage_metadata.setComment(comment);
setInMemoryMetadata(storage_metadata); setInMemoryMetadata(storage_metadata);
if (!query.select) if (!query.select)
@ -621,7 +625,7 @@ void registerStorageLiveView(StorageFactory & factory)
"Experimental LIVE VIEW feature is not enabled (the setting 'allow_experimental_live_view')", "Experimental LIVE VIEW feature is not enabled (the setting 'allow_experimental_live_view')",
ErrorCodes::SUPPORT_IS_DISABLED); ErrorCodes::SUPPORT_IS_DISABLED);
return StorageLiveView::create(args.table_id, args.getLocalContext(), args.query, args.columns); return StorageLiveView::create(args.table_id, args.getLocalContext(), args.query, args.columns, args.comment);
}); });
} }

View File

@ -233,7 +233,7 @@ private:
ContextPtr context_, ContextPtr context_,
const ASTCreateQuery & query, const ASTCreateQuery & query,
const ColumnsDescription & columns const ColumnsDescription & columns
); , const String & comment);
}; };
} }

View File

@ -156,9 +156,6 @@ StoragePtr StorageFactory::get(
throw Exception("Unknown table engine " + name, ErrorCodes::UNKNOWN_STORAGE); throw Exception("Unknown table engine " + name, ErrorCodes::UNKNOWN_STORAGE);
} }
if (query.comment)
comment = query.comment->as<ASTLiteral &>().value.get<String>();
auto check_feature = [&](String feature_description, FeatureMatcherFn feature_matcher_fn) auto check_feature = [&](String feature_description, FeatureMatcherFn feature_matcher_fn)
{ {
if (!feature_matcher_fn(it->second.features)) if (!feature_matcher_fn(it->second.features))
@ -204,6 +201,9 @@ StoragePtr StorageFactory::get(
} }
} }
if (query.comment)
comment = query.comment->as<ASTLiteral &>().value.get<String>();
ASTs empty_engine_args; ASTs empty_engine_args;
Arguments arguments{ Arguments arguments{
.engine_name = name, .engine_name = name,

View File

@ -60,7 +60,8 @@ StorageMaterializedView::StorageMaterializedView(
ContextPtr local_context, ContextPtr local_context,
const ASTCreateQuery & query, const ASTCreateQuery & query,
const ColumnsDescription & columns_, const ColumnsDescription & columns_,
bool attach_) bool attach_,
const String & comment)
: IStorage(table_id_), WithMutableContext(local_context->getGlobalContext()) : IStorage(table_id_), WithMutableContext(local_context->getGlobalContext())
{ {
StorageInMemoryMetadata storage_metadata; StorageInMemoryMetadata storage_metadata;
@ -81,6 +82,9 @@ StorageMaterializedView::StorageMaterializedView(
auto select = SelectQueryDescription::getSelectQueryFromASTForMatView(query.select->clone(), local_context); auto select = SelectQueryDescription::getSelectQueryFromASTForMatView(query.select->clone(), local_context);
storage_metadata.setSelectQuery(select); storage_metadata.setSelectQuery(select);
if (!comment.empty())
storage_metadata.setComment(comment);
setInMemoryMetadata(storage_metadata); setInMemoryMetadata(storage_metadata);
bool point_to_itself_by_uuid = has_inner_table && query.to_inner_uuid != UUIDHelpers::Nil bool point_to_itself_by_uuid = has_inner_table && query.to_inner_uuid != UUIDHelpers::Nil
@ -432,7 +436,7 @@ void registerStorageMaterializedView(StorageFactory & factory)
/// Pass local_context here to convey setting for inner table /// Pass local_context here to convey setting for inner table
return StorageMaterializedView::create( return StorageMaterializedView::create(
args.table_id, args.getLocalContext(), args.query, args.table_id, args.getLocalContext(), args.query,
args.columns, args.attach); args.columns, args.attach, args.comment);
}); });
} }

View File

@ -108,7 +108,8 @@ protected:
ContextPtr local_context, ContextPtr local_context,
const ASTCreateQuery & query, const ASTCreateQuery & query,
const ColumnsDescription & columns_, const ColumnsDescription & columns_,
bool attach_); bool attach_,
const String & comment);
}; };
} }

View File

@ -0,0 +1,3 @@
live_view_comment_test LiveView live view
materialized_view_comment_test MaterializedView materialized view
view_comment_test View simple view

View File

@ -0,0 +1,12 @@
-- Make sure that any kind of `VIEW` can be created with a `COMMENT` clause
-- and value of that clause is visible as `comment` column of `system.tables` table.
CREATE VIEW view_comment_test AS (SELECT 1) COMMENT 'simple view';
CREATE MATERIALIZED VIEW materialized_view_comment_test TO test1 (a UInt64) AS (SELECT 1) COMMENT 'materialized view';
SET allow_experimental_live_view=1;
CREATE LIVE VIEW live_view_comment_test AS (SELECT 1) COMMENT 'live view';
SYSTEM FLUSH LOGS;
SELECT name, engine, comment FROM system.tables WHERE database == currentDatabase() ORDER BY name;