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

Fixes #701: Add the OVER clause and include it in the Function tokens. #768

Merged
merged 2 commits into from
Apr 13, 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
18 changes: 17 additions & 1 deletion sqlparse/engine/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ def group_identifier(tlist):
tidx, token = tlist.token_next_by(t=ttypes, idx=tidx)


@recurse(sql.Over)
def group_over(tlist):
tidx, token = tlist.token_next_by(m=sql.Over.M_OPEN)
while token:
nidx, next_ = tlist.token_next(tidx)
if imt(next_, i=sql.Parenthesis, t=T.Name):
tlist.group_tokens(sql.Over, tidx, nidx)
tidx, token = tlist.token_next_by(m=sql.Over.M_OPEN, idx=tidx)


def group_arrays(tlist):
sqlcls = sql.SquareBrackets, sql.Identifier, sql.Function
ttypes = T.Name, T.String.Symbol
Expand Down Expand Up @@ -361,7 +371,12 @@ def group_functions(tlist):
while token:
nidx, next_ = tlist.token_next(tidx)
if isinstance(next_, sql.Parenthesis):
tlist.group_tokens(sql.Function, tidx, nidx)
over_idx, over = tlist.token_next(nidx)
if over and isinstance(over, sql.Over):
eidx = over_idx
else:
eidx = nidx
tlist.group_tokens(sql.Function, tidx, eidx)
tidx, token = tlist.token_next_by(t=T.Name, idx=tidx)


Expand Down Expand Up @@ -412,6 +427,7 @@ def group(stmt):
group_for,
group_begin,

group_over,
group_functions,
group_where,
group_period,
Expand Down
14 changes: 13 additions & 1 deletion sqlparse/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ class Where(TokenList):
'HAVING', 'RETURNING', 'INTO')


class Over(TokenList):
"""An OVER clause."""
M_OPEN = T.Keyword, 'OVER'


class Having(TokenList):
"""A HAVING clause."""
M_OPEN = T.Keyword, 'HAVING'
Expand Down Expand Up @@ -618,7 +623,7 @@ class Function(NameAliasMixin, TokenList):

def get_parameters(self):
"""Return a list of parameters."""
parenthesis = self.tokens[-1]
parenthesis = self.token_next_by(i=Parenthesis)[1]
result = []
for token in parenthesis.tokens:
if isinstance(token, IdentifierList):
Expand All @@ -628,6 +633,13 @@ def get_parameters(self):
result.append(token)
return result

def get_window(self):
"""Return the window if it exists."""
over_clause = self.token_next_by(i=Over)
if not over_clause:
return None
return over_clause[1].tokens[-1]


class Begin(TokenList):
"""A BEGIN/END block."""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ def test_grouping_identifier_function():
assert isinstance(p.tokens[0], sql.Identifier)
assert isinstance(p.tokens[0].tokens[0], sql.Operation)
assert isinstance(p.tokens[0].tokens[0].tokens[0], sql.Function)
p = sqlparse.parse('foo(c1) over win1 as bar')[0]
assert isinstance(p.tokens[0], sql.Identifier)
assert isinstance(p.tokens[0].tokens[0], sql.Function)
assert len(p.tokens[0].tokens[0].tokens) == 4
assert isinstance(p.tokens[0].tokens[0].tokens[3], sql.Over)
assert isinstance(p.tokens[0].tokens[0].tokens[3].tokens[2],
sql.Identifier)
p = sqlparse.parse('foo(c1) over (partition by c2 order by c3) as bar')[0]
assert isinstance(p.tokens[0], sql.Identifier)
assert isinstance(p.tokens[0].tokens[0], sql.Function)
assert len(p.tokens[0].tokens[0].tokens) == 4
assert isinstance(p.tokens[0].tokens[0].tokens[3], sql.Over)
assert isinstance(p.tokens[0].tokens[0].tokens[3].tokens[2],
sql.Parenthesis)


@pytest.mark.parametrize('s', ['foo+100', 'foo + 100', 'foo*100'])
Expand Down Expand Up @@ -378,6 +392,14 @@ def test_grouping_function():
p = sqlparse.parse('foo(null, bar)')[0]
assert isinstance(p.tokens[0], sql.Function)
assert len(list(p.tokens[0].get_parameters())) == 2
p = sqlparse.parse('foo(5) over win1')[0]
assert isinstance(p.tokens[0], sql.Function)
assert len(list(p.tokens[0].get_parameters())) == 1
assert isinstance(p.tokens[0].get_window(), sql.Identifier)
p = sqlparse.parse('foo(5) over (PARTITION BY c1)')[0]
assert isinstance(p.tokens[0], sql.Function)
assert len(list(p.tokens[0].get_parameters())) == 1
assert isinstance(p.tokens[0].get_window(), sql.Parenthesis)


def test_grouping_function_not_in():
Expand Down
Loading