Skip to content

Commit

Permalink
feat(oracle): Support for CONNECT BY [NOCYCLE] (#3238)
Browse files Browse the repository at this point in the history
* feat(oracle): Support for CONNECT BY [NOCYCLE]

* Update sqlglot/parser.py

Remove unnecessary none

Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>

---------

Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
  • Loading branch information
VaggelisD and georgesittas committed Mar 28, 2024
1 parent 647611e commit 59f1d13
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ class Check(Expression):

# https://docs.snowflake.com/en/sql-reference/constructs/connect-by
class Connect(Expression):
arg_types = {"start": False, "connect": True}
arg_types = {"start": False, "connect": True, "nocycle": False}


class Prior(Expression):
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,8 +1846,9 @@ def having_sql(self, expression: exp.Having) -> str:
def connect_sql(self, expression: exp.Connect) -> str:
start = self.sql(expression, "start")
start = self.seg(f"START WITH {start}") if start else ""
nocycle = " NOCYCLE" if expression.args.get("nocycle") else ""
connect = self.sql(expression, "connect")
connect = self.seg(f"CONNECT BY {connect}")
connect = self.seg(f"CONNECT BY{nocycle} {connect}")
return start + connect

def prior_sql(self, expression: exp.Prior) -> str:
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3505,6 +3505,7 @@ def _parse_connect(self, skip_start_token: bool = False) -> t.Optional[exp.Conne
return None

self._match(TokenType.CONNECT_BY)
nocycle = self._match_text_seq("NOCYCLE")
self.NO_PAREN_FUNCTION_PARSERS["PRIOR"] = lambda self: self.expression(
exp.Prior, this=self._parse_bitwise()
)
Expand All @@ -3514,7 +3515,7 @@ def _parse_connect(self, skip_start_token: bool = False) -> t.Optional[exp.Conne
if not start and self._match(TokenType.START_WITH):
start = self._parse_conjunction()

return self.expression(exp.Connect, start=start, connect=connect)
return self.expression(exp.Connect, start=start, connect=connect, nocycle=nocycle)

def _parse_name_as_expression(self) -> exp.Alias:
return self.expression(
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def test_oracle(self):
"SELECT * FROM t SAMPLE (0.25)",
)
self.validate_identity("SELECT TO_CHAR(-100, 'L99', 'NL_CURRENCY = '' AusDollars '' ')")
self.validate_identity(
"SELECT * FROM t START WITH col CONNECT BY NOCYCLE PRIOR col1 = col2"
)

self.validate_all(
"CURRENT_TIMESTAMP BETWEEN TO_DATE(f.C_SDATE, 'yyyy/mm/dd') AND TO_DATE(f.C_EDATE, 'yyyy/mm/dd')",
Expand Down

0 comments on commit 59f1d13

Please sign in to comment.