Merge pull request #6944 from malkfilipp/add-limit-clause-to-show-queries

Add LIMIT clause to SHOW queries
This commit is contained in:
alexey-milovidov 2019-09-18 02:07:45 +03:00 committed by GitHub
commit 03014fe920
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 3 deletions

View File

@ -1,5 +1,6 @@
#include <IO/ReadBufferFromString.h>
#include <Parsers/ASTShowTablesQuery.h>
#include <Parsers/formatAST.h>
#include <Interpreters/Context.h>
#include <Interpreters/executeQuery.h>
#include <Interpreters/InterpreterShowTablesQuery.h>
@ -53,6 +54,9 @@ String InterpreterShowTablesQuery::getRewrittenQuery()
if (!query.like.empty())
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();
}

View File

@ -13,7 +13,7 @@ ASTPtr ASTShowTablesQuery::clone() const
return res;
}
void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const
void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const
{
if (databases)
{
@ -30,6 +30,12 @@ void ASTShowTablesQuery::formatQueryImpl(const FormatSettings & settings, Format
if (!like.empty())
settings.ostr << (settings.hilite ? hilite_keyword : "") << " 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);
}
}
}

View File

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

View File

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

View File

@ -7,14 +7,14 @@ namespace DB
{
/** Query like this:
* SHOW TABLES [FROM db] [[NOT] LIKE 'str']
* SHOW TABLES [FROM db] [[NOT] LIKE 'str'] [LIMIT expr]
* or
* SHOW DATABASES.
*/
class ParserShowTablesQuery : public IParserBase
{
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);
};

View File

@ -0,0 +1,15 @@
*** Should show 6: ***
test1
test2
test3
test4
test5
test6
*** Should show 2: ***
test1
test2
*** Should show 4: ***
test1
test2
test3
test4

View File

@ -0,0 +1,20 @@
DROP DATABASE IF EXISTS test_show_limit;
CREATE DATABASE test_show_limit;
CREATE TABLE test_show_limit.test1 (test UInt8) ENGINE = TinyLog;
CREATE TABLE test_show_limit.test2 (test UInt8) ENGINE = TinyLog;
CREATE TABLE test_show_limit.test3 (test UInt8) ENGINE = TinyLog;
CREATE TABLE test_show_limit.test4 (test UInt8) ENGINE = TinyLog;
CREATE TABLE test_show_limit.test5 (test UInt8) ENGINE = TinyLog;
CREATE TABLE test_show_limit.test6 (test UInt8) ENGINE = TinyLog;
SELECT '*** Should show 6: ***';
SHOW TABLES FROM test_show_limit;
SELECT '*** Should show 2: ***';
SHOW TABLES FROM test_show_limit LIMIT 2;
SELECT '*** Should show 4: ***';
SHOW TABLES FROM test_show_limit LIMIT 2 * 2;
DROP DATABASE test_show_limit;