Skip to content

Commit

Permalink
Feat(databricks): shallow clone
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 30, 2023
1 parent 6a0110a commit c3d013b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ class Clone(Expression):
"this": True,
"when": False,
"kind": False,
"shallow": False,
"expression": False,
}

Expand Down
6 changes: 4 additions & 2 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,14 +793,16 @@ def create_sql(self, expression: exp.Create) -> str:

def clone_sql(self, expression: exp.Clone) -> str:
this = self.sql(expression, "this")
shallow = "SHALLOW " if expression.args.get("shallow") else ""
this = f"{shallow}CLONE {this}"
when = self.sql(expression, "when")

if when:
kind = self.sql(expression, "kind")
expr = self.sql(expression, "expression")
return f"CLONE {this} {when} ({kind} => {expr})"
return f"{this} {when} ({kind} => {expr})"

return f"CLONE {this}"
return this

def describe_sql(self, expression: exp.Describe) -> str:
return f"DESCRIBE {self.sql(expression, 'this')}"
Expand Down
9 changes: 8 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,8 @@ def extend_props(temp_props: t.Optional[exp.Properties]) -> None:
if self._match_text_seq("WITH", "NO", "SCHEMA", "BINDING"):
no_schema_binding = True

shallow = self._match_text_seq("SHALLOW")

if self._match_text_seq("CLONE"):
clone = self._parse_table(schema=True)
when = self._match_texts({"AT", "BEFORE"}) and self._prev.text.upper()
Expand All @@ -1314,7 +1316,12 @@ def extend_props(temp_props: t.Optional[exp.Properties]) -> None:
clone_expression = self._match(TokenType.FARROW) and self._parse_bitwise()
self._match(TokenType.R_PAREN)
clone = self.expression(
exp.Clone, this=clone, when=when, kind=clone_kind, expression=clone_expression
exp.Clone,
this=clone,
when=when,
kind=clone_kind,
shallow=shallow,
expression=clone_expression,
)

return self.expression(
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class TestDatabricks(Validator):
dialect = "databricks"

def test_databricks(self):
self.validate_identity("CREATE TABLE target SHALLOW CLONE source")
self.validate_identity("INSERT INTO a REPLACE WHERE cond VALUES (1), (2)")
self.validate_identity("SELECT c1 : price")
self.validate_identity("CREATE FUNCTION a.b(x INT) RETURNS INT RETURN x + 1")
Expand Down

0 comments on commit c3d013b

Please sign in to comment.