Skip to content

Fix Function.get_window() crash when there is no OVER clause#866

Open
uttam12331 wants to merge 1 commit into
andialbrecht:masterfrom
uttam12331:fix-get-window-no-over
Open

Fix Function.get_window() crash when there is no OVER clause#866
uttam12331 wants to merge 1 commit into
andialbrecht:masterfrom
uttam12331:fix-get-window-no-over

Conversation

@uttam12331

Copy link
Copy Markdown

Summary

Function.get_window() raises AttributeError for any function without an OVER clause:

>>> import sqlparse
>>> sqlparse.parse('foo(x)')[0].tokens[0].get_window()
AttributeError: 'NoneType' object has no attribute 'tokens'

token_next_by() returns an (idx, token) tuple — (None, None) when nothing matches. A tuple is always truthy, so the existing if not over_clause: guard is dead code and never returns None; execution falls through to over_clause[1].tokens[-1], and over_clause[1] is None.

Fix

Guard on the token instead of the tuple, matching the sibling get_parameters() in the same class (which uses token_next_by(i=Parenthesis)[1]):

over_clause = self.token_next_by(i=Over)
if over_clause[1] is None:
    return None
return over_clause[1].tokens[-1]

The OVER-present paths are unchanged (window token still returned); only the no-OVER case now correctly returns None.

Tests

Added a negative-path assertion to test_grouping_function (foo(5)get_window() is None). Full suite passes locally (487 passed).

token_next_by() returns a (idx, token) tuple, which is always truthy,
so 'if not over_clause' never triggered; when a function had no OVER
clause, over_clause[1] was None and over_clause[1].tokens[-1] raised
AttributeError instead of returning None. Guard on the token, matching
the sibling get_parameters().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant