Skip to content

Commit

Permalink
fix(bigquery): quote struct field names in memtable when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 27, 2023
1 parent 8008888 commit b1fcde8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
6 changes: 2 additions & 4 deletions ibis/backends/bigquery/compiler.py
Expand Up @@ -5,6 +5,7 @@
import re
from functools import partial

import sqlglot as sg
import toolz

import ibis.common.graph as lin
Expand Down Expand Up @@ -66,7 +67,6 @@ def find_bigquery_udf(op):


_NAME_REGEX = re.compile(r'[^!"$()*,./;?@[\\\]^`{}~\n]+')
_EXACT_NAME_REGEX = re.compile(f"^{_NAME_REGEX.pattern}$")


class BigQueryExprTranslator(sql_compiler.ExprTranslator):
Expand Down Expand Up @@ -125,9 +125,7 @@ def _rewrite_notany(op):

class BigQueryTableSetFormatter(sql_compiler.TableSetFormatter):
def _quote_identifier(self, name):
if _EXACT_NAME_REGEX.match(name) is not None:
return name
return f"`{name}`"
return sg.to_identifier(name).sql("bigquery")

def _format_in_memory_table(self, op):
import ibis
Expand Down
6 changes: 5 additions & 1 deletion ibis/backends/bigquery/datatypes.py
@@ -1,6 +1,7 @@
from __future__ import annotations

import google.cloud.bigquery as bq
import sqlglot as sg

import ibis.expr.datatypes as dt
import ibis.expr.schema as sch
Expand Down Expand Up @@ -76,7 +77,10 @@ def from_ibis(cls, dtype: dt.DataType) -> str:
elif dtype.is_array():
return f"ARRAY<{cls.from_ibis(dtype.value_type)}>"
elif dtype.is_struct():
fields = (f"{k} {cls.from_ibis(v)}" for k, v in dtype.fields.items())
fields = (
f"{sg.to_identifier(k).sql('bigquery')} {cls.from_ibis(v)}"
for k, v in dtype.fields.items()
)
return "STRUCT<{}>".format(", ".join(fields))
elif dtype.is_json():
return "JSON"
Expand Down
@@ -0,0 +1,3 @@
SELECT
t0.*
FROM UNNEST(ARRAY<STRUCT<`Column One` INT64>>[STRUCT(1 AS `Column One`), STRUCT(2 AS `Column One`), STRUCT(3 AS `Column One`)]) AS t0

0 comments on commit b1fcde8

Please sign in to comment.