Skip to content
Open
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
3 changes: 2 additions & 1 deletion sqlglot/expressions/dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ class LoadData(Expression):
"this": True,
"local": False,
"overwrite": False,
"inpath": True,
"inpath": False,
"files": False,
"partition": False,
"input_format": False,
"serde": False,
Expand Down
11 changes: 11 additions & 0 deletions sqlglot/generators/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,17 @@ def mod_sql(self, expression: exp.Mod) -> str:
expr.unnest() if isinstance(expr, exp.Paren) else expr,
)

def loaddata_sql(self, expression: exp.LoadData) -> str:
files = expression.args.get("files")
if files:
overwrite = " OVERWRITE" if expression.args.get("overwrite") else ""
this = self.sql(expression, "this")
table = f" {this}" if expression.args.get("overwrite") else f" INTO TABLE {this}"
files_sql = self.func("FILES", *files.expressions)
return f"LOAD DATA{overwrite}{table} FROM {files_sql}"

return super().loaddata_sql(expression)

def column_parts(self, expression: exp.Column) -> str:
if expression.meta.get("quoted_column"):
# If a column reference is of the form `dataset.table`.name, we need
Expand Down
21 changes: 21 additions & 0 deletions sqlglot/parsers/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,27 @@ def _parse_export_data(self) -> exp.Export:
)
)

def _parse_load(self) -> exp.LoadData | exp.Command:
index = self._index

if self._match_text_seq("DATA"):
overwrite = self._match(TokenType.OVERWRITE)
if not overwrite:
self._match_pair(TokenType.INTO, TokenType.TABLE)

this = self._parse_table(schema=True)
if this and self._match_text_seq("FROM", "FILES"):
return self.expression(
exp.LoadData(
this=this,
overwrite=overwrite,
files=exp.Properties(expressions=self._parse_wrapped_properties()),
)
)

self._retreat(index)
return super()._parse_load()

def _parse_column_ops(self, this: exp.Expr | None) -> exp.Expr | None:
func_index = self._index + 1
this = super()._parse_column_ops(this)
Expand Down
8 changes: 8 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ def test_bigquery(self):
self.validate_identity("BEGIN TRANSACTION")
self.validate_identity("COMMIT TRANSACTION")
self.validate_identity("ROLLBACK TRANSACTION")
self.validate_identity(
"LOAD DATA OVERWRITE mydataset.table1 FROM FILES(format='AVRO', uris=['gs://bucket/path/file.avro'])",
"LOAD DATA OVERWRITE mydataset.table1 FROM FILES(FORMAT='AVRO', uris=['gs://bucket/path/file.avro'])",
).assert_is(exp.LoadData)
self.validate_identity(
"LOAD DATA INTO TABLE mydataset.table1 FROM FILES(format='AVRO', uris=['gs://bucket/path/file.avro'])",
"LOAD DATA INTO TABLE mydataset.table1 FROM FILES(FORMAT='AVRO', uris=['gs://bucket/path/file.avro'])",
)
self.validate_identity("CAST(x AS BIGNUMERIC)")
self.validate_identity("SELECT y + 1 FROM x GROUP BY y + 1 ORDER BY 1")
self.validate_identity("SELECT TIMESTAMP_SECONDS(2) AS t")
Expand Down
Loading