From 78fde4a92e61a89d0b490b93acc90fff9635d1bf Mon Sep 17 00:00:00 2001 From: HemangChothani <50404902+HemangChothani@users.noreply.github.com> Date: Wed, 9 Dec 2020 15:27:19 -0500 Subject: [PATCH] fix: handle null values in array query parameters (#426) --- google/cloud/bigquery/_helpers.py | 2 +- tests/unit/test_query.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/google/cloud/bigquery/_helpers.py b/google/cloud/bigquery/_helpers.py index 716c8a394..100136108 100644 --- a/google/cloud/bigquery/_helpers.py +++ b/google/cloud/bigquery/_helpers.py @@ -40,7 +40,7 @@ def _not_null(value, field): """Check whether 'value' should be coerced to 'field' type.""" - return value is not None or field.mode != "NULLABLE" + return value is not None or (field is not None and field.mode != "NULLABLE") def _int_from_json(value, field): diff --git a/tests/unit/test_query.py b/tests/unit/test_query.py index a7c639ed1..cf268daf1 100644 --- a/tests/unit/test_query.py +++ b/tests/unit/test_query.py @@ -383,6 +383,16 @@ def test_from_api_repr_wo_values(self): self.assertEqual(param.array_type, "INT64") self.assertEqual(param.values, []) + def test_from_api_repr_w_none_values(self): + RESOURCE = { + "parameterType": {"type": "ARRAY", "arrayType": {"type": "INT64"}}, + "parameterValue": {"arrayValues": [{"value": "1"}, {"value": None}]}, + } + klass = self._get_target_class() + param = klass.from_api_repr(RESOURCE) + self.assertEqual(param.array_type, "INT64") + self.assertEqual(param.values, [1, None]) + def test_from_api_repr_w_struct_type(self): from google.cloud.bigquery.query import StructQueryParameter