Skip to content

Commit b16db6a

Browse files
authored
feat(bigquery): non-nullable schema support for embedded fields in struct (#10189)
1 parent c62efce commit b16db6a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

ibis/backends/bigquery/tests/unit/test_datatypes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
param(dt.string, "STRING", id="string"),
2525
param(dt.Array(dt.int64), "ARRAY<INT64>", id="array<int64>"),
2626
param(dt.Array(dt.string), "ARRAY<STRING>", id="array<string>"),
27+
param(
28+
dt.Struct({"a": dt.String(nullable=False)}),
29+
"STRUCT<`a` STRING NOT NULL>",
30+
id="struct<a: !string>",
31+
),
2732
param(
2833
dt.Struct.from_tuples(
2934
[("a", dt.int64), ("b", dt.string), ("c", dt.Array(dt.string))]

ibis/backends/sql/datatypes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,24 @@ def _from_ibis_GeoSpatial(cls, dtype: dt.GeoSpatial) -> sge.DataType:
944944
f"Current geotype: {dtype.geotype}, Current srid: {dtype.srid}"
945945
)
946946

947+
@classmethod
948+
def _from_ibis_Struct(cls, dtype: dt.Struct) -> sge.DataType:
949+
fields = [
950+
sge.ColumnDef(
951+
# always quote struct fields to allow reserved words as field names
952+
this=sg.to_identifier(name, quoted=True),
953+
# Bigquery supports embeddable nulls
954+
kind=cls.from_ibis(field),
955+
constraints=(
956+
None
957+
if field.nullable
958+
else [sge.ColumnConstraint(kind=sge.NotNullColumnConstraint())]
959+
),
960+
)
961+
for name, field in dtype.items()
962+
]
963+
return sge.DataType(this=typecode.STRUCT, expressions=fields, nested=True)
964+
947965

948966
class BigQueryUDFType(BigQueryType):
949967
@classmethod

0 commit comments

Comments
 (0)