Skip to content

Commit 7dec67d

Browse files
authored
fix: Fix row limit detection for multiline LIMIT clauses (#1594)
* fix for multiline LIMIT clause detection * added commit details to AUTHORS and changelog files
1 parent 5900a08 commit 7dec67d

4 files changed

Lines changed: 31 additions & 3 deletions

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ Contributors:
147147
* Devadathan M B (devadathanmb)
148148
* Charalampos Stratakis
149149
* Laszlo Bimba (bimlas)
150+
* Anjanna
150151

151152
Creator:
152153
--------

changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Bug fixes:
1616
* Add `VERSION` to built-in function completion so `SELECT VERSION();` is suggested.
1717
* Hide timezone notice at startup when local and server timezones are the same.
1818
* Let `sqlparse` accept arbitrarily-large queries.
19+
* Respect user-specified `LIMIT` clauses when the limit value starts on a new line.
1920

2021
4.4.0 (2025-12-24)
2122
==================

pgcli/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from cli_helpers.utils import strip_ansi
2828
from .explain_output_formatter import ExplainOutputFormatter
2929
import click
30+
import sqlparse
31+
from sqlparse import tokens as sqlparse_tokens
3032
import tzlocal
3133

3234
try:
@@ -1114,7 +1116,7 @@ def _should_limit_output(self, sql, cur):
11141116
def _has_limit(self, sql):
11151117
if not sql:
11161118
return False
1117-
return "limit " in sql.lower()
1119+
return any(token.match(sqlparse_tokens.Keyword, "LIMIT") for statement in sqlparse.parse(sql) for token in statement.flatten())
11181120

11191121
def _limit_output(self, cur):
11201122
limit = min(self.row_limit, cur.rowcount)

tests/test_rowlimit.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,18 @@ def low_count():
4444
return low_count_cursor
4545

4646

47-
def test_row_limit_with_LIMIT_clause(LIMIT, over_limit):
47+
@pytest.mark.parametrize(
48+
"stmt",
49+
[
50+
"SELECT * FROM students LIMIT 1000",
51+
"SELECT * FROM students LIMIT\n 1000",
52+
"SELECT * FROM students LIMIT\t1000",
53+
"SELECT * FROM students limit 1000",
54+
"SELECT * FROM students LiMiT 1000",
55+
],
56+
)
57+
def test_row_limit_with_LIMIT_clause(LIMIT, over_limit, stmt):
4858
cli = PGCli(row_limit=LIMIT)
49-
stmt = "SELECT * FROM students LIMIT 1000"
5059

5160
result = cli._should_limit_output(stmt, over_limit)
5261
assert result is False
@@ -56,6 +65,21 @@ def test_row_limit_with_LIMIT_clause(LIMIT, over_limit):
5665
assert result is False
5766

5867

68+
@pytest.mark.parametrize(
69+
"stmt",
70+
[
71+
"SELECT 'LIMIT 1000' FROM students",
72+
"SELECT * FROM students -- LIMIT 1000",
73+
"SELECT * FROM students /* LIMIT 1000 */",
74+
],
75+
)
76+
def test_row_limit_ignores_LIMIT_in_comments_or_strings(LIMIT, over_limit, stmt):
77+
cli = PGCli(row_limit=LIMIT)
78+
79+
result = cli._should_limit_output(stmt, over_limit)
80+
assert result is True
81+
82+
5983
def test_row_limit_without_LIMIT_clause(LIMIT, over_limit):
6084
cli = PGCli(row_limit=LIMIT)
6185
stmt = "SELECT * FROM students"

0 commit comments

Comments
 (0)