andialbrecht / sqlparse

A non-validating SQL parser module for Python
BSD 3-Clause "New" or "Revised" License
3.71k stars 693 forks source link

Select not recognized if followed by a parenthesis #775

Open stenci opened 4 months ago

stenci commented 4 months ago

The following two sql commands should be equivalent, but the missing space in the second case causes it not to recognize the select.

I am starting now to use sqlparse, so please let me know if this is not a bug and I am setting my expectations wrong.

tokens = sqlparse.parse('select (select 1)')[0]
for n, t in enumerate(tokens):
    print(n, t.ttype, t)

# Output:
# 0 Token.Keyword.DML select
# 1 Token.Text.Whitespace  
# 2 None (select 1)
tokens = sqlparse.parse('select(select 1)')[0]
for n, t in enumerate(tokens):
    print(n, t.ttype, t)

# Output:
# 0 None select(select 1)
andialbrecht commented 2 months ago

This is exactly the side effect mentioned here: https://github.com/andialbrecht/sqlparse/blob/master/sqlparse/keywords.py#L56

Fixing this isn't straight forward since it needs some refactoring in the lexer.