Skip to content
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
8 changes: 7 additions & 1 deletion sqlglot/generators/oracle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations


from sqlglot import exp, generator, transforms
from sqlglot.dialects.dialect import (
groupconcat_sql,
Expand Down Expand Up @@ -111,6 +110,13 @@ class OracleGenerator(generator.Generator):
exp.VolatileProperty: exp.Properties.Location.UNSUPPORTED,
}

def nthvalue_sql(self, expression: exp.NthValue) -> str:
result = self.func("NTH_VALUE", expression.this, expression.args.get("offset"))
from_first = expression.args.get("from_first")
if from_first is not None:
result = result + (" FROM FIRST" if from_first else " FROM LAST")
return result

def currenttimestamp_sql(self, expression: exp.CurrentTimestamp) -> str:
if expression.args.get("sysdate"):
return "SYSDATE"
Expand Down
8 changes: 8 additions & 0 deletions sqlglot/parsers/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class OracleParser(parser.Parser):
"TO_NUMBER": lambda self: self._parse_to_number(),
}

def _parse_window(self, this: exp.Expr | None, alias: bool = False) -> exp.Expr | None:
if isinstance(this, exp.NthValue):
if self._match_text_seq("FROM", "FIRST"):
this.set("from_first", True)
elif self._match_text_seq("FROM", "LAST"):
this.set("from_first", False)
return super()._parse_window(this, alias)

PROPERTY_PARSERS = {
**parser.Parser.PROPERTY_PARSERS,
"GLOBAL": lambda self: (
Expand Down
6 changes: 6 additions & 0 deletions tests/dialects/test_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class TestOracle(Validator):

def test_oracle(self):
self.validate_identity("1 /* /* */", "1 /* / * */")
self.validate_identity(
"SELECT NTH_VALUE(1, 1) FROM FIRST OVER (ORDER BY 1) AS FIRST_VALUE FROM DUAL"
)
self.validate_identity(
"SELECT NTH_VALUE(1, 1) FROM LAST OVER (ORDER BY 1) AS LAST_VALUE FROM DUAL"
)
self.validate_all(
"SELECT CONNECT_BY_ROOT x y",
write={
Expand Down
Loading