Add LIMIT clause to SHOW queries

This commit is contained in:
malkfilipp 2019-09-15 19:07:27 +03:00
parent 79a206b092
commit 98050c108f
5 changed files with 23 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#include <IO/ReadBufferFromString.h> #include <IO/ReadBufferFromString.h>
#include <Parsers/ASTShowTablesQuery.h> #include <Parsers/ASTShowTablesQuery.h>
#include <Parsers/formatAST.h>
#include <Interpreters/Context.h> #include <Interpreters/Context.h>
#include <Interpreters/executeQuery.h> #include <Interpreters/executeQuery.h>
#include <Interpreters/InterpreterShowTablesQuery.h> #include <Interpreters/InterpreterShowTablesQuery.h>
@ -53,6 +54,9 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
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 " : "") << "LIKE " << std::quoted(query.like, '\'');
if (query.limit_length)
rewritten_query << " LIMIT " << query.limit_length;
return rewritten_query.str(); return rewritten_query.str();
} }

View File

@ -13,7 +13,7 @@ ASTPtr ASTShowTablesQuery::clone() const
return res; return res;
} }
void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{ {
if (databases) if (databases)
{ {
@ -30,6 +30,12 @@ void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, Format
if (!like.empty()) if (!like.empty())
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIKE " << (settings.hilite ? hilite_none : "") settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIKE " << (settings.hilite ? hilite_none : "")
<< std::quoted(like, '\''); << std::quoted(like, '\'');
if (limit_length)
{
settings.ostr << (settings.hilite ? hilite_keyword : "") << " LIMIT " << (settings.hilite ? hilite_none : "");
limit_length->formatImpl(settings, state, frame);
}
} }
} }

View File

@ -19,6 +19,7 @@ public:
String from; String from;
String like; String like;
bool not_like{false}; bool not_like{false};
ASTPtr limit_length;
/** Get the text that identifies this element. */ /** Get the text that identifies this element. */
String getID(char) const override { return "ShowTables"; } String getID(char) const override { return "ShowTables"; }

View File

@ -5,6 +5,7 @@
#include <Parsers/CommonParsers.h> #include <Parsers/CommonParsers.h>
#include <Parsers/ParserShowTablesQuery.h> #include <Parsers/ParserShowTablesQuery.h>
#include <Parsers/ExpressionElementParsers.h> #include <Parsers/ExpressionElementParsers.h>
#include <Parsers/ExpressionListParsers.h>
#include <Common/typeid_cast.h> #include <Common/typeid_cast.h>
@ -22,8 +23,10 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
ParserKeyword s_from("FROM"); ParserKeyword s_from("FROM");
ParserKeyword s_not("NOT"); ParserKeyword s_not("NOT");
ParserKeyword s_like("LIKE"); ParserKeyword s_like("LIKE");
ParserKeyword s_limit("LIMIT");
ParserStringLiteral like_p; ParserStringLiteral like_p;
ParserIdentifier name_p; ParserIdentifier name_p;
ParserExpressionWithOptionalAlias limit_p(false);
ASTPtr like; ASTPtr like;
ASTPtr database; ASTPtr database;
@ -60,6 +63,12 @@ bool ParserShowTablesQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expec
} }
else if (query->not_like) else if (query->not_like)
return false; return false;
if (s_limit.ignore(pos, expected))
{
if (!limit_p.parse(pos, query->limit_length, expected))
return false;
}
} }
else else
return false; return false;

View File

@ -7,14 +7,14 @@ namespace DB
{ {
/** Query like this: /** Query like this:
* SHOW TABLES [FROM db] [[NOT] LIKE 'str'] * SHOW TABLES [FROM db] [[NOT] LIKE 'str'] [LIMIT expr]
* or * or
* SHOW DATABASES. * SHOW DATABASES.
*/ */
class ParserShowTablesQuery : public IParserBase class ParserShowTablesQuery : public IParserBase
{ {
protected: protected:
const char * getName() const { return "SHOW [TEMPORARY] TABLES|DATABASES [[NOT] LIKE 'str']"; } const char * getName() const { return "SHOW [TEMPORARY] TABLES|DATABASES [[NOT] LIKE 'str'] [LIMIT expr]"; }
bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected); bool parseImpl(Pos & pos, ASTPtr & node, Expected & expected);
}; };