Skip to content

Commit

Permalink
Ensure nested ordered identifiers are grouped (fixes #745)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley authored and andialbrecht committed Mar 5, 2024
1 parent b97387c commit 39b5a02
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlparse/engine/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ def group_functions(tlist):
tidx, token = tlist.token_next_by(t=T.Name, idx=tidx)


@recurse(sql.Identifier)
def group_order(tlist):
"""Group together Identifier and Asc/Desc token"""
tidx, token = tlist.token_next_by(t=T.Keyword.Order)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ def test_grouping_identifier_list_with_order():
assert str(p.tokens[0].tokens[3]) == '2 desc'


def test_grouping_nested_identifier_with_order():
# issue745
p = sqlparse.parse('(a desc)')[0]
assert isinstance(p.tokens[0], sql.Parenthesis)
assert isinstance(p.tokens[0].tokens[1], sql.Identifier)
assert str(p.tokens[0].tokens[1]) == 'a desc'


def test_grouping_where():
s = 'select * from foo where bar = 1 order by id desc'
p = sqlparse.parse(s)[0]
Expand Down

0 comments on commit 39b5a02

Please sign in to comment.