andialbrecht / sqlparse

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

Additional space when formatting with comma_first #644

Open wtn20 opened 2 years ago

wtn20 commented 2 years ago

Small issue when formatting a comma first query, if there are spaces preceding the column identifier.

Example:

import sqlparse

sqlparse_format_options = {
            'keyword_case': 'lower',
            'identifier_case': 'lower',
            'strip_comments': True,
            'reindent_aligned': True,
            'use_space_around_operators': True,
            'comma_first': True,
        }

str_1 = '''
SELECT 
 *, foo.a
FROM foo WHERE bar
'''

formatted_1 = sqlparse.format(str_1, **sqlparse_format_options)

str_2 = '''
SELECT 
 *
 , foo.a
FROM foo WHERE bar
'''

formatted_2 = sqlparse.format(str_2, **sqlparse_format_options)

print(formatted_1 == formatted_2)
print()
print(formatted_1)
print()
print(formatted_2)

Returns

False

select *,
       foo.a
  from foo
 where bar

select * ,
       foo.a
  from foo
 where bar

Expected

True

select *,
       foo.a
  from foo
 where bar

select *,
       foo.a
  from foo
 where bar