Skip to content

Commit

Permalink
Chore: cleanup bigquery transform
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed May 5, 2023
1 parent 0b46fa6 commit 7e5cede
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
25 changes: 14 additions & 11 deletions sqlglot/dialects/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,21 @@ def func(self, expression):

def _derived_table_values_to_unnest(self: generator.Generator, expression: exp.Values) -> str:
if not isinstance(expression.unnest().parent, exp.From):
expression = t.cast(exp.Values, transforms.remove_precision_parameterized_types(expression))
return self.values_sql(expression)
rows = [tuple_exp.expressions for tuple_exp in expression.find_all(exp.Tuple)]
structs = []
for row in rows:
aliases = [
exp.alias_(value, column_name)
for value, column_name in zip(row, expression.args["alias"].args["columns"])
]
structs.append(exp.Struct(expressions=aliases))
unnest_exp = exp.Unnest(expressions=[exp.Array(expressions=structs)])
return self.unnest_sql(unnest_exp)

structs = [
exp.Struct(
expressions=[
exp.alias_(value, column_name)
for value, column_name in zip(
t.expressions, expression.args["alias"].args["columns"]
)
]
)
for t in expression.find_all(exp.Tuple)
]

return self.unnest_sql(exp.Unnest(expressions=[exp.Array(expressions=structs)]))


def _returnsproperty_sql(self: generator.Generator, expression: exp.ReturnsProperty) -> str:
Expand Down
17 changes: 3 additions & 14 deletions sqlglot/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,9 @@ def remove_precision_parameterized_types(expression: exp.Expression) -> exp.Expr
Some dialects only allow the precision for parameterized types to be defined in the DDL and not in
other expressions. This transforms removes the precision from parameterized types in expressions.
"""
return expression.transform(
lambda node: exp.DataType(
**{
**node.args,
"expressions": [
node_expression
for node_expression in node.expressions
if isinstance(node_expression, exp.DataType)
],
}
)
if isinstance(node, exp.DataType)
else node,
)
for node in expression.find_all(exp.DataType):
node.set("expressions", [e for e in node.expressions if isinstance(e, exp.DataType)])
return expression


def unnest_to_explode(expression: exp.Expression) -> exp.Expression:
Expand Down

0 comments on commit 7e5cede

Please sign in to comment.