show clusters

This commit is contained in:
hexiaoting 2020-06-05 17:13:13 +08:00
parent c73aa0cb61
commit d2052dd1d4
6 changed files with 87 additions and 3 deletions

View File

@ -8,6 +8,7 @@
#include <Common/typeid_cast.h>
#include <iomanip>
#include <sstream>
#include <iostream>
namespace DB
@ -33,6 +34,32 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
if (query.databases)
return "SELECT name FROM system.databases";
/// SHOW CLUSTER/CLUSTERS
if (query.clusters)
{
std::stringstream rewritten_query;
rewritten_query << "SELECT cluster FROM system.clusters";
if (!query.like.empty())
{
rewritten_query << " WHERE cluster " << (query.not_like ? "NOT " : "") << "LIKE " << std::quoted(query.like, '\'');
}
if (query.limit_length)
rewritten_query << " LIMIT " << query.limit_length;
return rewritten_query.str();
}
else if (query.cluster)
{
std::stringstream rewritten_query;
rewritten_query << "SELECT * FROM system.clusters";
rewritten_query << " WHERE cluster = " << std::quoted(query.cluster_str, '\'');
return rewritten_query.str();
}
if (query.temporary && !query.from.empty())
throw Exception("The `FROM` and `TEMPORARY` cannot be used together in `SHOW TABLES`", ErrorCodes::SYNTAX_ERROR);

View File

@ -2,7 +2,6 @@
#include <Parsers/ASTShowTablesQuery.h>
#include <Common/quoteString.h>
namespace DB
{
@ -20,6 +19,24 @@ void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, Format
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SHOW DATABASES" << (settings.hilite ? hilite_none : "");
}
else if (clusters)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SHOW CLUSTERS" << (settings.hilite ? hilite_none : "");
if (!like.empty())
settings.ostr << (settings.hilite ? hilite_keyword : "") << (not_like ? " NOT" : "") << " LIKE " << (settings.hilite ? hilite_none : "")
<< std::quoted(like, '\'');
if (limit_length)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
limit_length->formatImpl(settings, state, frame);
}
}
else if (cluster)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SHOW CLUSTER" << (settings.hilite ? hilite_none : "");
settings.ostr << " " << backQuoteIfNeed(cluster_str);
}
else
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SHOW " << (temporary ? "TEMPORARY " : "") <<

View File

@ -9,14 +9,17 @@ namespace DB
{
/** Query SHOW TABLES or SHOW DATABASES
/** Query SHOW TABLES or SHOW DATABASES or SHOW CLUSTERS
*/
class ASTShowTablesQuery : public ASTQueryWithOutput
{
public:
bool databases{false};
bool clusters{false};
bool cluster{false};
bool dictionaries{false};
bool temporary{false};
String cluster_str;
String from;
String like;
bool not_like{false};

View File

@ -6,8 +6,10 @@
#include <Parsers/ParserShowTablesQuery.h>
#include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Common/typeid_cast.h>
#include <common/logger_useful.h>
namespace DB
@ -20,6 +22,8 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
ParserKeyword s_temporary("TEMPORARY");
ParserKeyword s_tables("TABLES");
ParserKeyword s_databases("DATABASES");
ParserKeyword s_clusters("CLUSTERS");
ParserKeyword s_cluster("CLUSTER");
ParserKeyword s_dictionaries("DICTIONARIES");
ParserKeyword s_from("FROM");
ParserKeyword s_in("IN");
@ -43,6 +47,36 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
{
query->databases = true;
}
else if (s_clusters.ignore(pos))
{
query->clusters = true;
if (s_not.ignore(pos, expected))
query->not_like = true;
if (s_like.ignore(pos, expected))
{
if (!like_p.parse(pos, like, expected))
return false;
}
else if (query->not_like)
return false;
if (s_limit.ignore(pos, expected))
{
if (!exp_elem.parse(pos, query->limit_length, expected))
return false;
}
}
else if (s_cluster.ignore(pos))
{
query->cluster = true;
String cluster_str;
if (!parseIdentifierOrStringLiteral(pos, expected, cluster_str))
return false;
query->cluster_str = std::move(cluster_str);
}
else
{
if (s_temporary.ignore(pos))

View File

@ -14,7 +14,7 @@ namespace DB
class ParserShowTablesQuery : public IParserBase
{
protected:
const char * getName() const override { return "SHOW [TEMPORARY] TABLES|DATABASES [[NOT] LIKE 'str'] [LIMIT expr]"; }
const char * getName() const override { return "SHOW [TEMPORARY] TABLES|DATABASES|CLUSTERS|CLUSTER 'name' [[NOT] LIKE 'str'] [LIMIT expr]"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected) override;
};

View File

@ -0,0 +1,3 @@
show clusters;
show clusters like 'test%' limit 1;
show cluster 'test_shard_localhost';