Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Contributors:
* Devadathan M B (devadathanmb)
* Charalampos Stratakis
* Laszlo Bimba (bimlas)
* Anjanna

Creator:
--------
Expand Down
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Bug fixes:
----------
* Add `VERSION` to built-in function completion so `SELECT VERSION();` is suggested.
* Hide timezone notice at startup when local and server timezones are the same.
* Respect user-specified `LIMIT` clauses when the limit value starts on a new line.

4.4.0 (2025-12-24)
==================
Expand Down
4 changes: 3 additions & 1 deletion pgcli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from cli_helpers.utils import strip_ansi
from .explain_output_formatter import ExplainOutputFormatter
import click
import sqlparse
from sqlparse import tokens as sqlparse_tokens
import tzlocal

try:
Expand Down Expand Up @@ -1112,7 +1114,7 @@ def _should_limit_output(self, sql, cur):
def _has_limit(self, sql):
if not sql:
return False
return "limit " in sql.lower()
return any(token.match(sqlparse_tokens.Keyword, "LIMIT") for statement in sqlparse.parse(sql) for token in statement.flatten())

def _limit_output(self, cur):
limit = min(self.row_limit, cur.rowcount)
Expand Down
28 changes: 26 additions & 2 deletions tests/test_rowlimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ def low_count():
return low_count_cursor


def test_row_limit_with_LIMIT_clause(LIMIT, over_limit):
@pytest.mark.parametrize(
"stmt",
[
"SELECT * FROM students LIMIT 1000",
"SELECT * FROM students LIMIT\n 1000",
"SELECT * FROM students LIMIT\t1000",
"SELECT * FROM students limit 1000",
"SELECT * FROM students LiMiT 1000",
],
)
def test_row_limit_with_LIMIT_clause(LIMIT, over_limit, stmt):
cli = PGCli(row_limit=LIMIT)
stmt = "SELECT * FROM students LIMIT 1000"

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


@pytest.mark.parametrize(
"stmt",
[
"SELECT 'LIMIT 1000' FROM students",
"SELECT * FROM students -- LIMIT 1000",
"SELECT * FROM students /* LIMIT 1000 */",
],
)
def test_row_limit_ignores_LIMIT_in_comments_or_strings(LIMIT, over_limit, stmt):
cli = PGCli(row_limit=LIMIT)

result = cli._should_limit_output(stmt, over_limit)
assert result is True


def test_row_limit_without_LIMIT_clause(LIMIT, over_limit):
cli = PGCli(row_limit=LIMIT)
stmt = "SELECT * FROM students"
Expand Down
Loading