Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure nested ordered identifiers are grouped (fixes #745) #746

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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