Skip to content

Commit 4c7b934

Browse files
committed
(WIP) Add support for ARRAY standard SQL type
1 parent b87a4d0 commit 4c7b934

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

bigquery/google/cloud/bigquery/schema.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"DATE": types.StandardSqlDataType.DATE,
3838
"TIME": types.StandardSqlDataType.TIME,
3939
"DATETIME": types.StandardSqlDataType.DATETIME,
40+
# no direct conversion from ARRAY, the latter is represented by mode="REPEATED"
4041
}
4142
"""String names of the legacy SQL types to integer codes of Standard SQL types."""
4243

@@ -179,13 +180,25 @@ def to_standard_sql(self):
179180
An instance of :class:`~google.cloud.bigquery_v2.types.StandardSqlField`.
180181
"""
181182
sql_type = types.StandardSqlDataType()
182-
sql_type.type_kind = LEGACY_TO_STANDARD_TYPES.get(
183-
self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED
184-
)
185183

186-
# NOTE: No need to also handle the "ARRAY" composed type, the latter
187-
# does not exist in legacy SQL types.
188-
if sql_type.type_kind == types.StandardSqlDataType.STRUCT: # noqa: E721
184+
if self.mode == "REPEATED":
185+
sql_type.type_kind = types.StandardSqlDataType.ARRAY
186+
else:
187+
sql_type.type_kind = LEGACY_TO_STANDARD_TYPES.get(
188+
self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED
189+
)
190+
191+
if sql_type.type_kind == types.StandardSqlDataType.ARRAY: # noqa: E721
192+
array_element_type = LEGACY_TO_STANDARD_TYPES.get(
193+
self.field_type, types.StandardSqlDataType.TYPE_KIND_UNSPECIFIED
194+
)
195+
# ARRAY cannot directly contain other arrays, only scalar types and STRUCTs
196+
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#array-type
197+
if array_element_type != types.StandardSqlDataType.STRUCT: # noqa: E721
198+
sql_type.array_element_type.type_kind = array_element_type
199+
else:
200+
raise NotImplementedError("no support yet for array of STRUCT")
201+
elif sql_type.type_kind == types.StandardSqlDataType.STRUCT: # noqa: E721
189202
sql_type.struct_type.fields.extend(
190203
field.to_standard_sql() for field in self.fields
191204
)

bigquery/tests/unit/test_schema.py

+19
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ def test_to_standard_sql_struct_type(self):
256256

257257
self.assertEqual(standard_field, expected_result)
258258

259+
def test_to_standard_sql_array_type_simple(self):
260+
from google.cloud.bigquery_v2 import types
261+
262+
# construct expected result object
263+
sql_type = types.StandardSqlDataType(type_kind=types.StandardSqlDataType.ARRAY)
264+
sql_type.array_element_type.type_kind = types.StandardSqlDataType.INT64
265+
expected_result = types.StandardSqlField(name="valid_numbers", type=sql_type)
266+
267+
# construct "repeated" SchemaField object and convert to standard SQL
268+
schema_field = self._make_one("valid_numbers", "INT64", mode="REPEATED")
269+
standard_field = schema_field.to_standard_sql()
270+
271+
self.assertEqual(standard_field, expected_result)
272+
273+
def test_to_standard_sql_array_type_struct(self):
274+
# from google.cloud.bigquery_v2 import types
275+
pass
276+
# TODO: implement test
277+
259278
def test_to_standard_sql_unknown_type(self):
260279
sql_type = self._get_standard_sql_data_type_class()
261280
field = self._make_one("weird_field", "TROOLEAN")

0 commit comments

Comments
 (0)