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)
Comment on lines +563 to +572
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, can we use the base generator and render files there? To avoid duplicating logic


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()
Comment on lines +709 to +728
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's overlapping logic with the base parser function, the main difference being matching FROM FILES.

We can instead just inline this into the base parser e.g:

    # parser.py
    def _parse_load(self) -> exp.LoadData | exp.Command:
        if self._match_text_seq("DATA"):
            ...
            return self.expression(
                exp.LoadData(
                    ...,
                    files = self._match_text_seq("FROM", "FILES") and exp.Properties(expressions=self._parse_wrapped_properties())
                )
            )
        return self._parse_as_command(self._prev)


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'])",
Comment on lines +210 to +211
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace the second line with the first and leave validate_identity as single-line given that the only thing changing is the FORMAT upper case

Suggested change
"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'])",
"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'])",
Comment on lines +214 to +215
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto; You can inline both of these tests into one if you use with self.subTest and a for loop e.g:

def test_load_data(self):
  with self.subTest(f"Testing LOAD DATA FROM FILES"):
   for overwrite in ("", "OVERWRITE "): 
     self.validate_identity(f"LOAD DATA {ovewrite} ...").assert_is(exp.LoadData)
 

)
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