Skip to content

Commit 39b5a02

Browse files
john-bodleyandialbrecht
authored andcommitted
Ensure nested ordered identifiers are grouped (fixes #745)
1 parent b97387c commit 39b5a02

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

sqlparse/engine/grouping.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ def group_functions(tlist):
360360
tidx, token = tlist.token_next_by(t=T.Name, idx=tidx)
361361

362362

363+
@recurse(sql.Identifier)
363364
def group_order(tlist):
364365
"""Group together Identifier and Asc/Desc token"""
365366
tidx, token = tlist.token_next_by(t=T.Keyword.Order)

tests/test_grouping.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ def test_grouping_identifier_list_with_order():
247247
assert str(p.tokens[0].tokens[3]) == '2 desc'
248248

249249

250+
def test_grouping_nested_identifier_with_order():
251+
# issue745
252+
p = sqlparse.parse('(a desc)')[0]
253+
assert isinstance(p.tokens[0], sql.Parenthesis)
254+
assert isinstance(p.tokens[0].tokens[1], sql.Identifier)
255+
assert str(p.tokens[0].tokens[1]) == 'a desc'
256+
257+
250258
def test_grouping_where():
251259
s = 'select * from foo where bar = 1 order by id desc'
252260
p = sqlparse.parse(s)[0]

0 commit comments

Comments
 (0)