Skip to content

Commit

Permalink
Fix handling of LIMIT / OFFSET in queries with the IN condition (#387)
Browse files Browse the repository at this point in the history
* Add test_with_in_condition() (see #382)

* Parser: fix the handling of LIMIT <offset>,<limit>

Only handle the punctuation when we already have the LIMIT keyword parsed. From now on IN(a,b,c) condition should not mess with it.
  • Loading branch information
macbre committed Apr 20, 2023
1 parent e5cc1c9 commit 1c20ba1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sql_metadata/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,12 @@ def limit_and_offset(self) -> Optional[Tuple[int, int]]:
elif token.last_keyword_normalized == "OFFSET":
# OFFSET <offset>
offset = int(token.value)
elif token.previous_token.is_punctuation:
elif (
token.previous_token.is_punctuation
and token.last_keyword_normalized == "LIMIT"
):
# LIMIT <offset>,<limit>
# enter this condition only when the limit has already been parsed
offset = limit
limit = int(token.value)

Expand Down
7 changes: 7 additions & 0 deletions test/test_limit_and_offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,10 @@ def test_comma_separated():
"WHERE cl_type = 'page' AND cl_to = 'Spotify/Song' "
"ORDER BY cl_sortkey LIMIT 927600,200"
).limit_and_offset == (200, 927600)


def test_with_in_condition():
# https://github.com/macbre/sql-metadata/issues/382
assert Parser(
"SELECT count(*) FROM aa WHERE userid IN (222,333) LIMIT 50 OFFSET 1000"
).limit_and_offset == (50, 1000)

0 comments on commit 1c20ba1

Please sign in to comment.