Skip to content

Commit

Permalink
Merge pull request ClickHouse#34917 from spongedu/support_exits_view_…
Browse files Browse the repository at this point in the history
…detail

support 'EXISTS [MATERIALIZED|LIVE|WINDOW] VIEW' syntax
  • Loading branch information
kitaisreal authored Mar 23, 2022
2 parents 33938ce + 1618f5a commit 355f49f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/Interpreters/InterpreterExistsQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ QueryPipeline InterpreterExistsQuery::executeImpl()
String database = getContext()->resolveDatabase(exists_query->getDatabase());
getContext()->checkAccess(AccessType::SHOW_TABLES, database, exists_query->getTable());
auto table = DatabaseCatalog::instance().tryGetTable({database, exists_query->getTable()}, getContext());
result = table && table->isView();
if (exists_query->materialized) {
result = table && table->isView() && table->getName() == "MaterializedView";
}
else if (exists_query->live) {
result = table && table->isView() && table->getName() == "LiveView";
}
else if (exists_query->window) {
result = table && table->isView() && table->getName() == "";
}
else
{
result = table && table->isView();
}
}
else if ((exists_query = query_ptr->as<ASTExistsDatabaseQuery>()))
{
Expand Down
4 changes: 4 additions & 0 deletions src/Parsers/ASTQueryWithTableAndOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class ASTQueryWithTableAndOutput : public ASTQueryWithOutput
UUID uuid = UUIDHelpers::Nil;
bool temporary{false};

bool materialized;
bool live;
bool window;

String getDatabase() const;
String getTable() const;

Expand Down
42 changes: 42 additions & 0 deletions src/Parsers/ParserTablePropertiesQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ bool ParserTablePropertiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected &
ParserKeyword s_table("TABLE");
ParserKeyword s_view("VIEW");
ParserKeyword s_dictionary("DICTIONARY");
ParserKeyword s_materialized("MATERIALIZED");
ParserKeyword s_live("LIVE");
ParserKeyword s_window("WINDOW");
ParserToken s_dot(TokenType::Dot);
ParserIdentifier name_p(true);

Expand All @@ -41,6 +44,45 @@ bool ParserTablePropertiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected &
query = std::make_shared<ASTExistsDatabaseQuery>();
parse_only_database_name = true;
}
else if (s_materialized.ignore(pos, expected))
{
if (s_view.ignore(pos, expected))
{
query = std::make_shared<ASTExistsViewQuery>();
query->materialized = true;
exists_view = true;
}
else
{
return false;
}
}
else if (s_live.ignore(pos, expected))
{
if (s_view.ignore(pos, expected))
{
query = std::make_shared<ASTExistsViewQuery>();
query->live = true;
exists_view = true;
}
else
{
return false;
}
}
else if (s_window.ignore(pos, expected))
{
if (s_view.ignore(pos, expected))
{
query = std::make_shared<ASTExistsViewQuery>();
query->window = true;
exists_view = true;
}
else
{
return false;
}
}
else if (s_view.ignore(pos, expected))
{
query = std::make_shared<ASTExistsViewQuery>();
Expand Down
12 changes: 11 additions & 1 deletion src/Parsers/TablePropertiesQueriesASTs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,22 @@ struct ASTDescribeQueryExistsQueryIDAndQueryNames
};

using ASTExistsTableQuery = ASTQueryWithTableAndOutputImpl<ASTExistsTableQueryIDAndQueryNames>;
using ASTExistsViewQuery = ASTQueryWithTableAndOutputImpl<ASTExistsViewQueryIDAndQueryNames>;
using ASTExistsDictionaryQuery = ASTQueryWithTableAndOutputImpl<ASTExistsDictionaryQueryIDAndQueryNames>;
using ASTShowCreateTableQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateTableQueryIDAndQueryNames>;
using ASTShowCreateViewQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateViewQueryIDAndQueryNames>;
using ASTShowCreateDictionaryQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateDictionaryQueryIDAndQueryNames>;

class ASTExistsViewQuery : public ASTQueryWithTableAndOutputImpl<ASTExistsViewQueryIDAndQueryNames>
{
protected:
void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "EXISTS" << " "
<< (materialized ? "MATERIALIZED" : (live ? "LIVE" : (window ? "WINDOW" : "")))
<< " VIEW " << (settings.hilite ? hilite_none : "") << backQuoteIfNeed(getTable());
}
};

class ASTExistsDatabaseQuery : public ASTQueryWithTableAndOutputImpl<ASTExistsDatabaseQueryIDAndQueryNames>
{
protected:
Expand Down
6 changes: 6 additions & 0 deletions tests/queries/0_stateless/01048_exists_query.reference
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
0
0
0
1
1
0
0
0
0
0
0
0
10 changes: 10 additions & 0 deletions tests/queries/0_stateless/01048_exists_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ EXISTS VIEW db_01048.v_not_exist;
EXISTS VIEW db_not_exists.v_not_exist;
DROP TABLE db_01048.t_01048_2;

CREATE TABLE db_01048.t_01048_3 (x UInt8) ENGINE = Memory;
CREATE MATERIALIZED VIEW db_01048.v_01048 ENGINE = Memory AS SELECT * FROM db_01048.t_01048_3;
EXISTS VIEW db_01048.v_01048;
EXISTS MATERIALIZED VIEW db_01048.v_01048;
EXISTS LIVE VIEW db_01048.v_01048;
DROP VIEW db_01048.v_01048;
EXISTS VIEW db_01048.v_01048;
EXISTS MATERIALIZED VIEW db_01048.v_01048;
EXISTS LIVE VIEW db_01048.v_01048;
DROP TABLE db_01048.t_01048_3;

DROP DATABASE db_01048;
EXISTS db_01048.t_01048;
Expand Down

0 comments on commit 355f49f

Please sign in to comment.