mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-26 01:22:04 +00:00
Merge pull request #14521 from hexiaoting/dev_like
Add new feature: SHOW DATABASES like 'xxx'
This commit is contained in:
commit
2cab30ba8d
@ -31,7 +31,24 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
|
|||||||
|
|
||||||
/// SHOW DATABASES
|
/// SHOW DATABASES
|
||||||
if (query.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
|
/// SHOW CLUSTER/CLUSTERS
|
||||||
if (query.clusters)
|
if (query.clusters)
|
||||||
@ -41,7 +58,11 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
|
|||||||
|
|
||||||
if (!query.like.empty())
|
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)
|
if (query.limit_length)
|
||||||
@ -85,7 +106,11 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
|
|||||||
rewritten_query << "database = " << std::quoted(database, '\'');
|
rewritten_query << "database = " << std::quoted(database, '\'');
|
||||||
|
|
||||||
if (!query.like.empty())
|
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)
|
else if (query.where_expression)
|
||||||
rewritten_query << " AND (" << query.where_expression << ")";
|
rewritten_query << " AND (" << query.where_expression << ")";
|
||||||
|
|
||||||
|
@ -13,16 +13,8 @@ ASTPtr ASTShowTablesQuery::clone() const
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
void ASTShowTablesQuery::formatLike(const FormatSettings & settings) const
|
||||||
{
|
{
|
||||||
if (databases)
|
|
||||||
{
|
|
||||||
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())
|
if (!like.empty())
|
||||||
settings.ostr
|
settings.ostr
|
||||||
<< (settings.hilite ? hilite_keyword : "")
|
<< (settings.hilite ? hilite_keyword : "")
|
||||||
@ -30,12 +22,32 @@ void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, Format
|
|||||||
<< (case_insensitive_like ? " ILIKE " : " LIKE ")
|
<< (case_insensitive_like ? " ILIKE " : " LIKE ")
|
||||||
<< (settings.hilite ? hilite_none : "")
|
<< (settings.hilite ? hilite_none : "")
|
||||||
<< std::quoted(like, '\'');
|
<< std::quoted(like, '\'');
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASTShowTablesQuery::formatLimit(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
|
||||||
|
{
|
||||||
if (limit_length)
|
if (limit_length)
|
||||||
{
|
{
|
||||||
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
|
||||||
limit_length->formatImpl(settings, state, frame);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (cluster)
|
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 : "")
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " FROM " << (settings.hilite ? hilite_none : "")
|
||||||
<< backQuoteIfNeed(from);
|
<< backQuoteIfNeed(from);
|
||||||
|
|
||||||
if (!like.empty())
|
formatLike(settings);
|
||||||
settings.ostr
|
|
||||||
<< (settings.hilite ? hilite_keyword : "")
|
|
||||||
<< (not_like ? " NOT" : "")
|
|
||||||
<< (case_insensitive_like ? " ILIKE " : " LIKE ")
|
|
||||||
<< (settings.hilite ? hilite_none : "")
|
|
||||||
<< std::quoted(like, '\'');
|
|
||||||
|
|
||||||
else if (where_expression)
|
if (where_expression)
|
||||||
{
|
{
|
||||||
settings.ostr << (settings.hilite ? hilite_keyword : "") << " WHERE " << (settings.hilite ? hilite_none : "");
|
settings.ostr << (settings.hilite ? hilite_keyword : "") << " WHERE " << (settings.hilite ? hilite_none : "");
|
||||||
where_expression->formatImpl(settings, state, frame);
|
where_expression->formatImpl(settings, state, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (limit_length)
|
formatLimit(settings, state, frame);
|
||||||
{
|
|
||||||
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
|
|
||||||
limit_length->formatImpl(settings, state, frame);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +36,8 @@ public:
|
|||||||
ASTPtr clone() const override;
|
ASTPtr clone() const override;
|
||||||
|
|
||||||
protected:
|
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;
|
void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -46,6 +46,25 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
|
|||||||
if (s_databases.ignore(pos))
|
if (s_databases.ignore(pos))
|
||||||
{
|
{
|
||||||
query->databases = true;
|
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))
|
else if (s_clusters.ignore(pos))
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
test_01470
|
3
tests/queries/0_stateless/01470_show_databases_like.sql
Normal file
3
tests/queries/0_stateless/01470_show_databases_like.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
create database if not exists test_01470;
|
||||||
|
show databases like '%01470';
|
||||||
|
drop database test_01470;
|
Loading…
Reference in New Issue
Block a user