Skip to content

Commit

Permalink
Fix(oracle): improve handling of KEEP (...) OVER (...) window syntax (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jul 24, 2023
1 parent e71bbc4 commit da5a4d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4172,7 +4172,7 @@ def _parse_window(

self._match_r_paren()

return self.expression(
window = self.expression(
exp.Window,
this=this,
partition_by=partition,
Expand All @@ -4183,6 +4183,12 @@ def _parse_window(
first=first,
)

# This covers Oracle's FIRST/LAST syntax: aggregate KEEP (...) OVER (...)
if self._match_set(self.WINDOW_BEFORE_PAREN_TOKENS, advance=False):
return self._parse_window(window, alias=alias)

return window

def _parse_window_spec(self) -> t.Dict[str, t.Optional[str | exp.Expression]]:
self._match(TokenType.BETWEEN)

Expand Down
5 changes: 5 additions & 0 deletions tests/dialects/test_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def test_oracle(self):
self.validate_identity(
"SELECT MIN(column_name) KEEP (DENSE_RANK FIRST ORDER BY column_name DESC) FROM table_name"
)
self.validate_identity(
"SELECT last_name, department_id, salary, MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct) "
'OVER (PARTITION BY department_id) AS "Worst", MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct) '
'OVER (PARTITION BY department_id) AS "Best" FROM employees ORDER BY department_id, salary, last_name'
)

self.validate_all(
"NVL(NULL, 1)",
Expand Down

0 comments on commit da5a4d1

Please sign in to comment.