Skip to content

Commit

Permalink
Feat(bigquery): add support for CREATE TABLE .. COPY DDL syntax (#2305)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Sep 23, 2023
1 parent e1cbff4 commit 8ed0a81
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ class Generator(generator.Generator):
RENAME_TABLE_WITH_DB = False
NVL2_SUPPORTED = False
UNNEST_WITH_ORDINALITY = False
CLONE_KEYWORD = "COPY"

TRANSFORMS = {
**generator.Generator.TRANSFORMS,
Expand Down
5 changes: 4 additions & 1 deletion sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ class Generator:
# Whether or not session variables / parameters are supported, e.g. @x in T-SQL
SUPPORTS_PARAMETERS = True

# The keyword used to clone a table in a DDL statement
CLONE_KEYWORD = "CLONE"

TYPE_MAPPING = {
exp.DataType.Type.NCHAR: "CHAR",
exp.DataType.Type.NVARCHAR: "VARCHAR",
Expand Down Expand Up @@ -817,7 +820,7 @@ 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}"
this = f"{shallow}{self.CLONE_KEYWORD} {this}"
when = self.sql(expression, "when")

if when:
Expand Down
3 changes: 2 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ class Parser(metaclass=_Parser):

INSERT_ALTERNATIVES = {"ABORT", "FAIL", "IGNORE", "REPLACE", "ROLLBACK"}

CLONE_KEYWORDS = {"CLONE", "COPY"}
CLONE_KINDS = {"TIMESTAMP", "OFFSET", "STATEMENT"}

TABLE_INDEX_HINT_TOKENS = {TokenType.FORCE, TokenType.IGNORE, TokenType.USE}
Expand Down Expand Up @@ -1344,7 +1345,7 @@ def extend_props(temp_props: t.Optional[exp.Properties]) -> None:

shallow = self._match_text_seq("SHALLOW")

if self._match_text_seq("CLONE"):
if self._match_texts(self.CLONE_KEYWORDS):
clone = self._parse_table(schema=True)
when = self._match_texts({"AT", "BEFORE"}) and self._prev.text.upper()
clone_kind = (
Expand Down
4 changes: 4 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def test_bigquery(self):
"""SELECT JSON '"foo"' AS json_data""",
"""SELECT PARSE_JSON('"foo"') AS json_data""",
)
self.validate_identity(
"CREATE OR REPLACE TABLE `a.b.c` copy `a.b.d`",
"CREATE OR REPLACE TABLE a.b.c COPY a.b.d",
)

self.validate_all("SELECT SPLIT(foo)", write={"bigquery": "SELECT SPLIT(foo, ',')"})
self.validate_all("SELECT 1 AS hash", write={"bigquery": "SELECT 1 AS `hash`"})
Expand Down

0 comments on commit 8ed0a81

Please sign in to comment.