andialbrecht / sqlparse

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

IdentifierList excludes single GROUP BY identifier #650

Closed dwoodleych closed 2 years ago

dwoodleych commented 2 years ago

If there is a single column in group by the identifier is not included in IdentifierList


import sqlparse

rawsql = "SELECT Test1, Test2, SUM(Test3) from tblTest GROUP BY Test1, Test2"

querysql = sqlparse.format(rawsql, encoding=None, reindent_aligned=True) parsed = sqlparse.parse(querysql) stmt = parsed[0]

for t in stmt: if type(t) == sqlparse.sql.IdentifierList: print(t)


Yields:

Test1, Test2, SUM(Test3) Test1, Test2

and then the following code with a single column in GROUP BY


import sqlparse

rawsql = "SELECT Test1, Test2, SUM(Test3) from tblTest GROUP BY Test1"

querysql = sqlparse.format(rawsql, encoding=None, reindent_aligned=True) parsed = sqlparse.parse(querysql) stmt = parsed[0]

for t in stmt: if type(t) == sqlparse.sql.IdentifierList: print(t)


yields:

Test1, Test2, SUM(Test3)

dwoodleych commented 2 years ago

I'm an idiot