Merge pull request #14521 from hexiaoting/dev_like

Add new feature: SHOW DATABASES like 'xxx'
This commit is contained in:
alexey-milovidov 2020-09-19 16:34:37 +03:00 committed by GitHub
commit 2cab30ba8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 29 deletions

View File

@ -31,7 +31,24 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
/// SHOW DATABASES
if (query.databases)
return "SELECT name FROM system.databases";
{
std::stringstream rewritten_query;
rewritten_query << "SELECT name FROM system.databases";
if (!query.like.empty())
{
rewritten_query
<< " WHERE name "
<< (query.not_like ? "NOT " : "")
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
<< std::quoted(query.like, '\'');
}
if (query.limit_length)
rewritten_query << " LIMIT " << query.limit_length;
return rewritten_query.str();
}
/// SHOW CLUSTER/CLUSTERS
if (query.clusters)
@ -41,7 +58,11 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
if (!query.like.empty())
{
rewritten_query << " WHERE cluster " << (query.not_like ? "NOT " : "") << "LIKE " << std::quoted(query.like, '\'');
rewritten_query
<< " WHERE cluster "
<< (query.not_like ? "NOT " : "")
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
<< std::quoted(query.like, '\'');
}
if (query.limit_length)
@ -85,7 +106,11 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
rewritten_query << "database = " << std::quoted(database, '\'');
if (!query.like.empty())
rewritten_query << " AND name " << (query.not_like ? "NOT " : "") << "LIKE " << std::quoted(query.like, '\'');
rewritten_query
<< " AND name "
<< (query.not_like ? "NOT " : "")
<< (query.case_insensitive_like ? "ILIKE " : "LIKE ")
<< std::quoted(query.like, '\'');
else if (query.where_expression)
rewritten_query << " AND (" << query.where_expression << ")";

View File

@ -13,29 +13,41 @@ ASTPtr ASTShowTablesQuery::clone() const
return res;
}
void ASTShowTablesQuery::formatLike(const FormatSettings & settings) const
{
if (!like.empty())
settings.ostr
<< (settings.hilite ? hilite_keyword : "")
<< (not_like ? " NOT" : "")
<< (case_insensitive_like ? " ILIKE " : " LIKE ")
<< (settings.hilite ? hilite_none : "")
<< std::quoted(like, '\'');
}
void ASTShowTablesQuery::formatLimit(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{
if (limit_length)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
limit_length->formatImpl(settings, state, frame);
}
}
void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{
if (databases)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SHOW DATABASES" << (settings.hilite ? hilite_none : "");
formatLike(settings);
formatLimit(settings, state, frame);
}
else if (clusters)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << "SHOW CLUSTERS" << (settings.hilite ? hilite_none : "");
formatLike(settings);
formatLimit(settings, state, frame);
if (!like.empty())
settings.ostr
<< (settings.hilite ? hilite_keyword : "")
<< (not_like ? " NOT" : "")
<< (case_insensitive_like ? " ILIKE " : " 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)
{
@ -51,25 +63,15 @@ void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, Format
settings.ostr << (settings.hilite ? hilite_keyword : "") << " FROM " << (settings.hilite ? hilite_none : "")
<< backQuoteIfNeed(from);
if (!like.empty())
settings.ostr
<< (settings.hilite ? hilite_keyword : "")
<< (not_like ? " NOT" : "")
<< (case_insensitive_like ? " ILIKE " : " LIKE ")
<< (settings.hilite ? hilite_none : "")
<< std::quoted(like, '\'');
formatLike(settings);
else if (where_expression)
if (where_expression)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << " WHERE " << (settings.hilite ? hilite_none : "");
where_expression->formatImpl(settings, state, frame);
}
if (limit_length)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
limit_length->formatImpl(settings, state, frame);
}
formatLimit(settings, state, frame);
}
}

View File

@ -36,6 +36,8 @@ public:
ASTPtr clone() const override;
protected:
void formatLike(const FormatSettings & settings) const;
void formatLimit(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const;
void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override;
};

View File

@ -46,6 +46,25 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
if (s_databases.ignore(pos))
{
query->databases = true;
if (s_not.ignore(pos, expected))
query->not_like = true;
if (bool insensitive = s_ilike.ignore(pos, expected); insensitive || s_like.ignore(pos, expected))
{
if (insensitive)
query->case_insensitive_like = true;
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_clusters.ignore(pos))
{

View File

@ -0,0 +1 @@
test_01470

View File

@ -0,0 +1,3 @@
create database if not exists test_01470;
show databases like '%01470';
drop database test_01470;