Repository: macbre/sql-metadata

Issue:
limit_and_offset method bug?
example query:
sqltext = Parser("SELECT count(*) FROM aa WHERE grad = 88038 AND userid in(222,333) AND type = 1 AND checkable = 'sea'  LIMIT 50 OFFSET 1000").limit_and_offset

actually result(wrong):
(333, 1000)

Expected result
(50, 1000)



Discussion:

Patch:
diff --git a/sql_metadata/parser.py b/sql_metadata/parser.py
index ab0a8fc..349f044 100644
--- a/sql_metadata/parser.py
+++ b/sql_metadata/parser.py
@@ -382,8 +382,12 @@ class Parser:  # pylint: disable=R0902
                 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)
 


Test patch:
diff --git a/test/test_limit_and_offset.py b/test/test_limit_and_offset.py
index 024e935..1fd6aae 100644
--- a/test/test_limit_and_offset.py
+++ b/test/test_limit_and_offset.py
@@ -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)