Skip to content

Commit 1138972

Browse files
committed
Fix parsing of UNION ALL after WHERE (fixes andialbrecht#349).
1 parent dc788ab commit 1138972

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Bug Fixes
99

1010
* Fix parsing of MySQL table names starting with digits (issue337).
1111
* Fix detection of identifiers using comparisons (issue327).
12+
* Fix parsing of UNION ALL after WHERE (issue349).
13+
1214

1315

1416
Release 0.2.3 (Mar 02, 2017)

sqlparse/sql.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,9 @@ def is_multiline(self):
526526
class Where(TokenList):
527527
"""A WHERE clause."""
528528
M_OPEN = T.Keyword, 'WHERE'
529-
M_CLOSE = T.Keyword, ('ORDER', 'GROUP', 'LIMIT', 'UNION', 'EXCEPT',
530-
'HAVING', 'RETURNING', 'INTO')
529+
M_CLOSE = T.Keyword, (
530+
'ORDER', 'GROUP', 'LIMIT', 'UNION', 'UNION ALL', 'EXCEPT',
531+
'HAVING', 'RETURNING', 'INTO')
531532

532533

533534
class Case(TokenList):

tests/test_grouping.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ def test_grouping_where():
216216
assert isinstance(p.tokens[-1].tokens[0].tokens[-2], sql.Where)
217217

218218

219+
@pytest.mark.parametrize('s', (
220+
'select 1 where 1 = 2 union select 2',
221+
'select 1 where 1 = 2 union all select 2',
222+
))
223+
def test_grouping_where_union(s):
224+
p = sqlparse.parse(s)[0]
225+
assert p.tokens[5].value.startswith('union')
226+
227+
219228
def test_returning_kw_ends_where_clause():
220229
s = 'delete from foo where x > y returning z'
221230
p = sqlparse.parse(s)[0]

0 commit comments

Comments
 (0)