Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WHERE var IN (..) is not Comparison #566

Closed
hurcy opened this issue Jul 28, 2020 · 1 comment · Fixed by #567
Closed

WHERE var IN (..) is not Comparison #566

hurcy opened this issue Jul 28, 2020 · 1 comment · Fixed by #567
Milestone

Comments

@hurcy
Copy link
Contributor

hurcy commented Jul 28, 2020

Hi,

IN is recognized as Keyword, but WHERE var IN (..) isn't recognized as Comparison.

I would like to add following line in the keyword.py

diff --git a/sqlparse/keywords.py b/sqlparse/keywords.py
index 9c37e50..d554343 100644
--- a/sqlparse/keywords.py
+++ b/sqlparse/keywords.py
@@ -46,6 +46,7 @@ SQL_REGEX = {
 
         (r'\\\w+', tokens.Command),
 
+        (r'(NOT\s+)?(IN)\b', tokens.Operator.Comparison),
         # FIXME(andi): VALUES shouldn't be listed here
         # see https://github.com/andialbrecht/sqlparse/pull/64
         # AS and IN are special, it may be followed by a parenthesis, but
diff --git a/tests/test_grouping.py b/tests/test_grouping.py
index 87dcf11..253d1ef 100644
--- a/tests/test_grouping.py
+++ b/tests/test_grouping.py
@@ -373,10 +373,20 @@ def test_grouping_function_not_in():
     # issue183
     p = sqlparse.parse('in(1, 2)')[0]
     assert len(p.tokens) == 2
-    assert p.tokens[0].ttype == T.Keyword
+    assert p.tokens[0].ttype == T.Comparison
     assert isinstance(p.tokens[1], sql.Parenthesis)
 
 
+def test_in_comparison():
+    # issue566
+    p = sqlparse.parse('a in (1, 2)')[0]
+    assert len(p.tokens) == 1
+    assert isinstance(p.tokens[0], sql.Comparison)
+    assert len(p.tokens[0].tokens) == 5
+    assert p.tokens[0].left.value == 'a'
+    assert p.tokens[0].right.value == '(1, 2)'
+
+
 def test_grouping_varchar():
     p = sqlparse.parse('"text" Varchar(50) NOT NULL')[0]
     assert isinstance(p.tokens[2], sql.Function)
@alexarze
Copy link

alexarze commented Aug 4, 2020

Howdy @hurcy ,

Does this pull request also recognize NOT IN as a comparsion token? It would be really confusing to recognize IN statements as comparisons, but not NOT IN statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants