Skip to content

Commit

Permalink
Make tzcast grouping function less eager
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Cederstrand authored and andialbrecht committed Aug 16, 2022
1 parent 9d2cb6f commit 3d3df9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions sqlparse/engine/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,20 @@ def group_tzcasts(tlist):
def match(token):
return token.ttype == T.Keyword.TZCast

def valid(token):
def valid_prev(token):
return token is not None

def valid_next(token):
return token is not None and (
token.is_whitespace
or token.match(T.Keyword, 'AS')
or token.match(*sql.TypedLiteral.M_CLOSE)
)

def post(tlist, pidx, tidx, nidx):
return pidx, nidx

_group(tlist, sql.Identifier, match, valid, valid, post)
_group(tlist, sql.Identifier, match, valid_prev, valid_next, post)


def group_typed_literal(tlist):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ def test_issue489_tzcasts():
assert p.tokens[-1].get_alias() == 'foo'


def test_issue562_tzcasts():
# Test that whitespace between 'from' and 'bar' is retained
formatted = sqlparse.format(
'SELECT f(HOUR from bar AT TIME ZONE \'UTC\') from foo', reindent=True
)
assert formatted == \
'SELECT f(HOUR\n from bar AT TIME ZONE \'UTC\')\nfrom foo'


def test_as_in_parentheses_indents():
# did raise NoneType has no attribute is_group in _process_parentheses
formatted = sqlparse.format('(as foo)', reindent=True)
Expand Down

0 comments on commit 3d3df9d

Please sign in to comment.