Support SHOW CREATE VIEW Syntax

This commit is contained in:
spongedc 2020-12-14 22:37:25 +08:00
parent e7ffae89e9
commit 8b2feaeca2
10 changed files with 69 additions and 8 deletions

View File

@ -529,6 +529,7 @@
M(560, ZSTD_ENCODER_FAILED) \
M(561, ZSTD_DECODER_FAILED) \
M(562, TLD_LIST_NOT_FOUND) \
M(563, NOT_VIEW) \
\
M(999, KEEPER_EXCEPTION) \
M(1000, POCO_EXCEPTION) \

View File

@ -30,6 +30,7 @@ namespace ErrorCodes
extern const int NOT_IMPLEMENTED;
extern const int CANNOT_GET_CREATE_TABLE_QUERY;
extern const int CANNOT_GET_CREATE_DICTIONARY_QUERY;
extern const int NOT_VIEW;
}
class IDatabaseTablesIterator

View File

@ -160,6 +160,10 @@ std::unique_ptr<IInterpreter> InterpreterFactory::get(ASTPtr & query, Context &
{
return std::make_unique<InterpreterShowCreateQuery>(query, context);
}
else if (query->as<ASTShowCreateViewQuery>())
{
return std::make_unique<InterpreterShowCreateQuery>(query, context);
}
else if (query->as<ASTShowCreateDatabaseQuery>())
{
return std::make_unique<InterpreterShowCreateQuery>(query, context);

View File

@ -43,12 +43,19 @@ BlockInputStreamPtr InterpreterShowCreateQuery::executeImpl()
{
ASTPtr create_query;
ASTQueryWithTableAndOutput * show_query;
if ((show_query = query_ptr->as<ASTShowCreateTableQuery>()))
if ((show_query = query_ptr->as<ASTShowCreateTableQuery>()) ||
(show_query = query_ptr->as<ASTShowCreateViewQuery>()))
{
auto resolve_table_type = show_query->temporary ? Context::ResolveExternal : Context::ResolveOrdinary;
auto table_id = context.resolveStorageID(*show_query, resolve_table_type);
context.checkAccess(AccessType::SHOW_COLUMNS, table_id);
create_query = DatabaseCatalog::instance().getDatabase(table_id.database_name)->getCreateTableQuery(table_id.table_name, context);
if (query_ptr->as<ASTShowCreateViewQuery>())
{
auto & ast_create_query = create_query->as<ASTCreateQuery &>();
if (!ast_create_query.isView())
throw Exception("'" + ast_create_query.database + "." + ast_create_query.table +"' is not VIEW", ErrorCodes::NOT_VIEW);
}
}
else if ((show_query = query_ptr->as<ASTShowCreateDatabaseQuery>()))
{

View File

@ -91,6 +91,8 @@ public:
return removeOnCluster<ASTCreateQuery>(clone(), new_database);
}
bool isView() { return is_view || is_materialized_view || is_live_view; }
protected:
void formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
};

View File

@ -21,6 +21,7 @@ bool ParserTablePropertiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected &
ParserKeyword s_create("CREATE");
ParserKeyword s_database("DATABASE");
ParserKeyword s_table("TABLE");
ParserKeyword s_view("VIEW");
ParserKeyword s_dictionary("DICTIONARY");
ParserToken s_dot(TokenType::Dot);
ParserIdentifier name_p;
@ -30,6 +31,7 @@ bool ParserTablePropertiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected &
std::shared_ptr<ASTQueryWithTableAndOutput> query;
bool parse_only_database_name = false;
bool parse_show_create_view = false;
bool temporary = false;
if (s_exists.ignore(pos, expected))
@ -56,6 +58,11 @@ bool ParserTablePropertiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected &
}
else if (s_dictionary.checkWithoutMoving(pos, expected))
query = std::make_shared<ASTShowCreateDictionaryQuery>();
else if (s_view.ignore(pos, expected))
{
query = std::make_shared<ASTShowCreateViewQuery>();
parse_show_create_view = true;
}
else
query = std::make_shared<ASTShowCreateTableQuery>();
}
@ -71,15 +78,16 @@ bool ParserTablePropertiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected &
}
else
{
if (temporary || s_temporary.ignore(pos, expected))
query->temporary = true;
if (!s_table.ignore(pos, expected))
s_dictionary.ignore(pos, expected);
if (!parse_show_create_view)
{
if (temporary || s_temporary.ignore(pos, expected))
query->temporary = true;
if (!s_table.ignore(pos, expected))
s_dictionary.ignore(pos, expected);
}
if (!name_p.parse(pos, table, expected))
return false;
if (s_dot.ignore(pos, expected))
{
database = table;

View File

@ -7,7 +7,7 @@
namespace DB
{
/** Query (EXISTS | SHOW CREATE) [TABLE|DICTIONARY] [db.]name [FORMAT format]
/** Query (EXISTS | SHOW CREATE) [DATABASE|TABLE|DICTIONARY] [db.]name [FORMAT format]
*/
class ParserTablePropertiesQuery : public IParserBase
{

View File

@ -29,6 +29,14 @@ struct ASTShowCreateTableQueryIDAndQueryNames
static constexpr auto QueryTemporary = "SHOW CREATE TEMPORARY TABLE";
};
struct ASTShowCreateViewQueryIDAndQueryNames
{
static constexpr auto ID = "ShowCreateViewQuery";
static constexpr auto Query = "SHOW CREATE VIEW";
/// No temporary view are supported, just for parsing
static constexpr auto QueryTemporary = "";
};
struct ASTShowCreateDatabaseQueryIDAndQueryNames
{
static constexpr auto ID = "ShowCreateDatabaseQuery";
@ -54,6 +62,7 @@ struct ASTDescribeQueryExistsQueryIDAndQueryNames
using ASTExistsTableQuery = ASTQueryWithTableAndOutputImpl<ASTExistsTableQueryIDAndQueryNames>;
using ASTExistsDictionaryQuery = ASTQueryWithTableAndOutputImpl<ASTExistsDictionaryQueryIDAndQueryNames>;
using ASTShowCreateTableQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateTableQueryIDAndQueryNames>;
using ASTShowCreateViewQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateViewQueryIDAndQueryNames>;
using ASTShowCreateDictionaryQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateDictionaryQueryIDAndQueryNames>;
class ASTShowCreateDatabaseQuery : public ASTQueryWithTableAndOutputImpl<ASTShowCreateDatabaseQueryIDAndQueryNames>

View File

@ -0,0 +1,3 @@
CREATE VIEW test_1602.v\n(\n `EventDate` DateTime,\n `CounterID` UInt32,\n `UserID` UInt32\n) AS\nSELECT *\nFROM test_1602.tbl
CREATE MATERIALIZED VIEW test_1602.vv\n(\n `EventDate` DateTime,\n `CounterID` UInt32,\n `UserID` UInt32\n)\nENGINE = MergeTree\nPARTITION BY toYYYYMM(EventDate)\nORDER BY (CounterID, EventDate, intHash32(UserID))\nSETTINGS index_granularity = 8192 AS\nSELECT *\nFROM test_1602.tbl
CREATE LIVE VIEW test_1602.vvv\n(\n `EventDate` DateTime,\n `CounterID` UInt32,\n `UserID` UInt32\n) AS\nSELECT *\nFROM test_1602.tbl

View File

@ -0,0 +1,26 @@
DROP DATABASE IF EXISTS test_1602;
CREATE DATABASE test_1602;
CREATE TABLE test_1602.tbl (`EventDate` DateTime, `CounterID` UInt32, `UserID` UInt32) ENGINE = MergeTree() PARTITION BY toYYYYMM(EventDate) ORDER BY (CounterID, EventDate, intHash32(UserID)) SETTINGS index_granularity = 8192;
CREATE VIEW test_1602.v AS SELECT * FROM test_1602.tbl;
CREATE MATERIALIZED VIEW test_1602.vv (`EventDate` DateTime, `CounterID` UInt32, `UserID` UInt32) ENGINE = MergeTree() PARTITION BY toYYYYMM(EventDate) ORDER BY (CounterID, EventDate, intHash32(UserID)) SETTINGS index_granularity = 8192 AS SELECT * FROM test_1602.tbl;
SET allow_experimental_live_view=1;
CREATE LIVE VIEW test_1602.vvv AS SELECT * FROM test_1602.tbl;
SHOW CREATE VIEW test_1602.v;
SHOW CREATE VIEW test_1602.vv;
SHOW CREATE VIEW test_1602.vvv;
SHOW CREATE VIEW test_1602.not_exist_view; -- { serverError 390 }
SHOW CREATE VIEW test_1602.tbl; -- { serverError 563 }
DROP DATABASE IF EXISTS test_1602;