Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bigquery)!: Refactor EXPORT DATA statement #4693

Merged
merged 1 commit into from
Feb 3, 2025
Merged
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
55 changes: 4 additions & 51 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,6 @@ class Parser(parser.Parser):
LOG_DEFAULTS_TO_LN = True
SUPPORTS_IMPLICIT_UNNEST = True

ID_VAR_TOKENS = {
*parser.Parser.ID_VAR_TOKENS,
TokenType.EXPORT,
}

FUNCTIONS = {
**parser.Parser.FUNCTIONS,
"CONTAINS_SUBSTR": _build_contains_substring,
Expand Down Expand Up @@ -837,47 +832,13 @@ def _parse_features_at_time(self) -> exp.FeaturesAtTime:
return expr

def _parse_export_data(self) -> exp.Export:
# https://cloud.google.com/bigquery/docs/reference/standard-sql/export-statements
if not self._match_text_seq("DATA"):
self.raise_error("Expected 'DATA' after 'EXPORT'")

with_connection = None
options = None

if self._match_text_seq("WITH", "CONNECTION"):
parts = []
while True:
part = self._parse_var()
if not part:
break
parts.append(part.name)
if not self._match(TokenType.DOT):
break

if not parts:
self.raise_error("Expected connection name after WITH CONNECTION")

with_connection = exp.Identifier(this=".".join(parts))

if self._match_text_seq("OPTIONS"):
self._match(TokenType.L_PAREN)
options = self._parse_properties()
self._match(TokenType.R_PAREN)
else:
self.raise_error("Expected 'OPTIONS' after 'EXPORT DATA'")

self._match_text_seq("AS")

# Parse the full SELECT statement
query = self._parse_statement()
if not isinstance(query, exp.Select):
self.raise_error("Expected SELECT statement in EXPORT DATA")
self._match_text_seq("DATA")

return self.expression(
exp.Export,
this=query,
with_connection=with_connection,
options=options,
connection=self._match_text_seq("WITH", "CONNECTION") and self._parse_table_parts(),
options=self._parse_properties(),
this=self._match_text_seq("AS") and self._parse_select(),
)

class Generator(generator.Generator):
Expand Down Expand Up @@ -1288,11 +1249,3 @@ def cast_sql(self, expression: exp.Cast, safe_prefix: t.Optional[str] = None) ->
return f"{self.sql(expression, 'to')}{self.sql(this)}"

return super().cast_sql(expression, safe_prefix=safe_prefix)

def export_sql(self, expression: exp.Export) -> str:
this = self.sql(expression, "this")
with_connection = self.sql(expression, "with_connection")
with_connection = f"WITH CONNECTION {with_connection} " if with_connection else ""
options = self.sql(expression, "options")
options = f"{options} " if options else ""
return f"EXPORT DATA {with_connection}{options}{this}"
13 changes: 5 additions & 8 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,11 @@ def kind(self) -> t.Optional[str]:
return kind and kind.upper()


# https://cloud.google.com/bigquery/docs/reference/standard-sql/export-statements
class Export(Expression):
arg_types = {"this": True, "connection": False, "options": True}


class Filter(Expression):
arg_types = {"this": True, "expression": True}

Expand Down Expand Up @@ -8650,11 +8655,3 @@ def null() -> Null:
Boolean,
Null,
)


class Export(Expression):
arg_types = {
"this": True,
"with_connection": False,
"options": False,
}
7 changes: 7 additions & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4752,3 +4752,10 @@ def xmltable_sql(self, expression: exp.XMLTable) -> str:
def xmlnamespace_sql(self, expression: exp.XMLNamespace) -> str:
this = self.sql(expression, "this")
return this if isinstance(expression.this, exp.Alias) else f"DEFAULT {this}"

def export_sql(self, expression: exp.Export) -> str:
this = self.sql(expression, "this")
connection = self.sql(expression, "connection")
connection = f"WITH CONNECTION {connection} " if connection else ""
options = self.sql(expression, "options")
return f"EXPORT DATA {connection}{options} AS {this}"
1 change: 1 addition & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ class Parser(metaclass=_Parser):
TokenType.DIV,
TokenType.END,
TokenType.EXECUTE,
TokenType.EXPORT,
TokenType.ESCAPE,
TokenType.FALSE,
TokenType.FIRST,
Expand Down
4 changes: 2 additions & 2 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,10 +1662,10 @@ def test_bigquery(self):
self.validate_identity("SELECT * FROM ML.FEATURES_AT_TIME((SELECT 1), num_rows => 1)")

self.validate_identity(
"EXPORT DATA OPTIONS (URI='gs://path*.csv.gz', FORMAT='CSV') SELECT * FROM all_rows"
"EXPORT DATA OPTIONS (URI='gs://path*.csv.gz', FORMAT='CSV') AS SELECT * FROM all_rows"
)
self.validate_identity(
"EXPORT DATA WITH CONNECTION myproject.us.myconnection OPTIONS (URI='gs://path*.csv.gz', FORMAT='CSV') SELECT * FROM all_rows"
"EXPORT DATA WITH CONNECTION myproject.us.myconnection OPTIONS (URI='gs://path*.csv.gz', FORMAT='CSV') AS SELECT * FROM all_rows"
)

def test_errors(self):
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,4 @@ SELECT 1 LIMIT 1
CAST(x AS INT128)
CAST(x AS UINT128)
CAST(x AS UINT256)
SELECT export