support EXISTS DATABASE syntax

This commit is contained in:
spongedc 2020-12-24 17:16:47 +08:00
parent a58acbae67
commit 71ab8c18fa
6 changed files with 39 additions and 8 deletions

View File

@ -53,6 +53,12 @@ BlockInputStreamPtr InterpreterExistsQuery::executeImpl()
result = DatabaseCatalog::instance().isTableExist({database, exists_query->table}, context); result = DatabaseCatalog::instance().isTableExist({database, exists_query->table}, context);
} }
} }
else if ((exists_query = query_ptr->as<ASTExistsDatabaseQuery>()))
{
String database = context.resolveDatabase(exists_query->database);
context.checkAccess(AccessType::SHOW_DATABASES, database);
result = DatabaseCatalog::instance().isDatabaseExist(database);
}
else if ((exists_query = query_ptr->as<ASTExistsDictionaryQuery>())) else if ((exists_query = query_ptr->as<ASTExistsDictionaryQuery>()))
{ {
if (exists_query->temporary) if (exists_query->temporary)

View File

@ -148,6 +148,10 @@ std::unique_ptr<IInterpreter> InterpreterFactory::get(ASTPtr & query, Context &
{ {
return std::make_unique<InterpreterOptimizeQuery>(query, context); return std::make_unique<InterpreterOptimizeQuery>(query, context);
} }
else if (query->as<ASTExistsDatabaseQuery>())
{
return std::make_unique<InterpreterExistsQuery>(query, context);
}
else if (query->as<ASTExistsTableQuery>()) else if (query->as<ASTExistsTableQuery>())
{ {
return std::make_unique<InterpreterExistsQuery>(query, context); return std::make_unique<InterpreterExistsQuery>(query, context);

View File

@ -34,15 +34,23 @@ bool ParserTablePropertiesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected &
bool temporary = false; bool temporary = false;
if (s_exists.ignore(pos, expected)) if (s_exists.ignore(pos, expected))
{ {
if (s_temporary.ignore(pos, expected)) if (s_database.ignore(pos, expected))
temporary = true; {
query = std::make_shared<ASTExistsDatabaseQuery>();
if (s_table.checkWithoutMoving(pos, expected)) parse_only_database_name = true;
query = std::make_shared<ASTExistsTableQuery>(); }
else if (s_dictionary.checkWithoutMoving(pos, expected))
query = std::make_shared<ASTExistsDictionaryQuery>();
else else
query = std::make_shared<ASTExistsTableQuery>(); {
if (s_temporary.ignore(pos, expected))
temporary = true;
if (s_table.checkWithoutMoving(pos, expected))
query = std::make_shared<ASTExistsTableQuery>();
else if (s_dictionary.checkWithoutMoving(pos, expected))
query = std::make_shared<ASTExistsDictionaryQuery>();
else
query = std::make_shared<ASTExistsTableQuery>();
}
} }
else if (s_show.ignore(pos, expected)) else if (s_show.ignore(pos, expected))
{ {

View File

@ -7,6 +7,14 @@
namespace DB namespace DB
{ {
struct ASTExistsDatabaseQueryIDAndQueryNames
{
static constexpr auto ID = "ExistsDatabaseQuery";
static constexpr auto Query = "EXISTS DATABASE";
/// No temporary databases are supported, just for parsing
static constexpr auto QueryTemporary = "";
};
struct ASTExistsTableQueryIDAndQueryNames struct ASTExistsTableQueryIDAndQueryNames
{ {
static constexpr auto ID = "ExistsTableQuery"; static constexpr auto ID = "ExistsTableQuery";
@ -51,6 +59,7 @@ struct ASTDescribeQueryExistsQueryIDAndQueryNames
static constexpr auto QueryTemporary = "DESCRIBE TEMPORARY TABLE"; static constexpr auto QueryTemporary = "DESCRIBE TEMPORARY TABLE";
}; };
using ASTExistsDatabaseQuery = ASTQueryWithTableAndOutputImpl<ASTExistsDatabaseQueryIDAndQueryNames>;
using ASTExistsTableQuery = ASTQueryWithTableAndOutputImpl<ASTExistsTableQueryIDAndQueryNames>; using ASTExistsTableQuery = ASTQueryWithTableAndOutputImpl<ASTExistsTableQueryIDAndQueryNames>;
using ASTExistsDictionaryQuery = ASTQueryWithTableAndOutputImpl<ASTExistsDictionaryQueryIDAndQueryNames>; using ASTExistsDictionaryQuery = ASTQueryWithTableAndOutputImpl<ASTExistsDictionaryQueryIDAndQueryNames>;
using ASTShowCreateTableQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateTableQueryIDAndQueryNames>; using ASTShowCreateTableQuery = ASTQueryWithTableAndOutputImpl<ASTShowCreateTableQueryIDAndQueryNames>;

View File

@ -2,6 +2,8 @@
0 0
0 0
0 0
1
0
0 0
0 0
1 1

View File

@ -3,7 +3,9 @@ EXISTS TABLE db_01048.t_01048;
EXISTS DICTIONARY db_01048.t_01048; EXISTS DICTIONARY db_01048.t_01048;
DROP DATABASE IF EXISTS db_01048; DROP DATABASE IF EXISTS db_01048;
EXISTS DATABASE db_01048;
CREATE DATABASE db_01048; CREATE DATABASE db_01048;
EXISTS DATABASE db_01048;
DROP TABLE IF EXISTS db_01048.t_01048; DROP TABLE IF EXISTS db_01048.t_01048;
EXISTS db_01048.t_01048; EXISTS db_01048.t_01048;