Skip to content

Commit

Permalink
Fix: limit with select subquery closes #1948
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jul 22, 2023
1 parent 95fd75b commit 327451f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
13 changes: 8 additions & 5 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2778,8 +2778,14 @@ def _parse_limit(
) -> t.Optional[exp.Expression]:
if self._match(TokenType.TOP if top else TokenType.LIMIT):
comments = self._prev_comments
limit_paren = self._match(TokenType.L_PAREN)
expression = self._parse_number() if top else self._parse_term()
if top:
limit_paren = self._match(TokenType.L_PAREN)
expression = self._parse_number()

if limit_paren:
self._match_r_paren()
else:
expression = self._parse_term()

if self._match(TokenType.COMMA):
offset = expression
Expand All @@ -2791,9 +2797,6 @@ def _parse_limit(
exp.Limit, this=this, expression=expression, offset=offset, comments=comments
)

if limit_paren:
self._match_r_paren()

return limit_exp

if self._match(TokenType.FETCH):
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ SELECT x LIKE '%x%' FROM test
SELECT * FROM test LIMIT 100
SELECT * FROM test LIMIT 1 + 1
SELECT * FROM test LIMIT 100 OFFSET 200
SELECT * FROM test LIMIT (SELECT 1)
SELECT * FROM test FETCH FIRST ROWS ONLY
SELECT * FROM test FETCH FIRST 1 ROWS ONLY
SELECT * FROM test ORDER BY id DESC FETCH FIRST 10 ROWS WITH TIES
Expand Down

2 comments on commit 327451f

@cpcloud
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also handle OFFSET?

@tobymao
Copy link
Owner Author

@tobymao tobymao commented on 327451f Jul 22, 2023 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.