Skip to content

Commit

Permalink
Fix: bigquery conversion without table alias
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 5, 2023
1 parent d5db35f commit 34b6038
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ def _derived_table_values_to_unnest(self: generator.Generator, expression: exp.V
if not isinstance(expression.unnest().parent, exp.From):
return self.values_sql(expression)

alias = expression.args.get("alias")

structs = [
exp.Struct(
expressions=[
exp.alias_(value, column_name)
for value, column_name in zip(
t.expressions, expression.args["alias"].args["columns"]
t.expressions,
alias.columns
if alias and alias.columns
else (f"_c{i}" for i in range(len(t.expressions))),
)
]
)
Expand Down
12 changes: 12 additions & 0 deletions tests/dialects/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ def test_bigquery(self):
"snowflake": "SELECT cola, colb FROM (VALUES (1, 'test')) AS tab(cola, colb)",
},
)
self.validate_all(
"SELECT cola, colb FROM (VALUES (1, 'test')) AS tab",
write={
"bigquery": "SELECT cola, colb FROM UNNEST([STRUCT(1 AS _c0, 'test' AS _c1)])",
},
)
self.validate_all(
"SELECT cola, colb FROM (VALUES (1, 'test'))",
write={
"bigquery": "SELECT cola, colb FROM UNNEST([STRUCT(1 AS _c0, 'test' AS _c1)])",
},
)
self.validate_all(
"SELECT cola, colb, colc FROM (VALUES (1, 'test', NULL)) AS tab(cola, colb, colc)",
write={
Expand Down

0 comments on commit 34b6038

Please sign in to comment.