Skip to content

Commit d74ea26

Browse files
committed
Fix parsing and formatting of statements containing EXCEPT keyword.
1 parent ff6371f commit d74ea26

4 files changed

Lines changed: 18 additions & 2 deletions

File tree

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Bug Fixes
55
* Fix incorrect parsing of string literals containing line breaks (issue118).
66
* Fix typo in keywords, add MERGE, COLLECT keywords (issue122/124, by Cristian Orellana).
77
* Improve parsing of string literals in columns.
8+
* Fix parsing and formatting of statements containing EXCEPT keyword.
89

910
Enhancements
1011
* Classify DML keywords (issue116, by Victor Hahn).

sqlparse/engine/grouping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def group_where(tlist):
297297
if not isinstance(sgroup, sql.Where)]
298298
idx = 0
299299
token = tlist.token_next_match(idx, T.Keyword, 'WHERE')
300-
stopwords = ('ORDER', 'GROUP', 'LIMIT', 'UNION')
300+
stopwords = ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT')
301301
while token:
302302
tidx = tlist.token_index(token)
303303
end = tlist.token_next_match(tidx + 1, T.Keyword, stopwords)

sqlparse/filters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def nl(self):
301301
def _split_kwds(self, tlist):
302302
split_words = ('FROM', 'STRAIGHT_JOIN$', 'JOIN$', 'AND', 'OR',
303303
'GROUP', 'ORDER', 'UNION', 'VALUES',
304-
'SET', 'BETWEEN')
304+
'SET', 'BETWEEN', 'EXCEPT')
305305

306306
def _next_token(i):
307307
t = tlist.token_next_match(i, T.Keyword, split_words,

tests/test_regressions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,18 @@ def test_issue90():
218218
' "price" = 1,',
219219
' "description" = NULL'])
220220
assert formatted == tformatted
221+
222+
223+
def test_except_formatting():
224+
sql = 'SELECT 1 FROM foo WHERE 2 = 3 EXCEPT SELECT 2 FROM bar WHERE 1 = 2'
225+
formatted = sqlparse.format(sql, reindent=True)
226+
tformatted = '\n'.join([
227+
'SELECT 1',
228+
'FROM foo',
229+
'WHERE 2 = 3',
230+
'EXCEPT',
231+
'SELECT 2',
232+
'FROM bar',
233+
'WHERE 1 = 2'
234+
])
235+
assert formatted == tformatted

0 commit comments

Comments
 (0)