From 909cf52f26937a0e9758e87f2635cbc8de52e4bd Mon Sep 17 00:00:00 2001
From: Tim Swast <swast@google.com>
Date: Tue, 13 Dec 2016 13:23:50 -0800
Subject: [PATCH 1/2] bigquery: fix array handling in parameterized queries

---
 bigquery/google/cloud/bigquery/_helpers.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/bigquery/google/cloud/bigquery/_helpers.py b/bigquery/google/cloud/bigquery/_helpers.py
index dcea7b237a07..0db3c9fe9653 100644
--- a/bigquery/google/cloud/bigquery/_helpers.py
+++ b/bigquery/google/cloud/bigquery/_helpers.py
@@ -497,10 +497,13 @@ def to_api_repr(self):
             values = [converter(value) for value in values]
         resource = {
             'parameterType': {
-                'arrayType': self.array_type,
+                'type': 'ARRAY',
+                'arrayType': {
+                    'type': self.array_type,
+                },
             },
             'parameterValue': {
-                'arrayValues': values,
+                'arrayValues': [{'value': value} for value in values],
             },
         }
         if self.name is not None:

From 1c9087d481dc3c33a238180f939f1fa110d84370 Mon Sep 17 00:00:00 2001
From: Tim Swast <swast@google.com>
Date: Tue, 27 Dec 2016 13:03:22 -0800
Subject: [PATCH 2/2] bigquery: Add tests for array query parameters.

---
 bigquery/google/cloud/bigquery/_helpers.py |  7 ++-
 bigquery/unit_tests/test__helpers.py       | 63 ++++++++++++++++++----
 system_tests/bigquery.py                   |  8 +++
 3 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/bigquery/google/cloud/bigquery/_helpers.py b/bigquery/google/cloud/bigquery/_helpers.py
index 4e8004d716db..bbcbae1674c3 100644
--- a/bigquery/google/cloud/bigquery/_helpers.py
+++ b/bigquery/google/cloud/bigquery/_helpers.py
@@ -483,8 +483,11 @@ def from_api_repr(cls, resource):
         :returns: instance
         """
         name = resource.get('name')
-        array_type = resource['parameterType']['arrayType']
-        values = resource['parameterValue']['arrayValues']
+        array_type = resource['parameterType']['arrayType']['type']
+        values = [
+            value['value']
+            for value
+            in resource['parameterValue']['arrayValues']]
         converted = [
             _CELLDATA_FROM_JSON[array_type](value, None) for value in values]
         return cls(name, array_type, converted)
diff --git a/bigquery/unit_tests/test__helpers.py b/bigquery/unit_tests/test__helpers.py
index cc2df7b19006..ec84ccbf6950 100644
--- a/bigquery/unit_tests/test__helpers.py
+++ b/bigquery/unit_tests/test__helpers.py
@@ -1067,10 +1067,20 @@ def test_from_api_repr_w_name(self):
         RESOURCE = {
             'name': 'foo',
             'parameterType': {
-                'arrayType': 'INT64',
+                'type': 'ARRAY',
+                'arrayType': {
+                    'type': 'INT64',
+                },
             },
             'parameterValue': {
-                'arrayValues': ['1', '2'],
+                'arrayValues': [
+                    {
+                        'value': '1',
+                    },
+                    {
+                        'value': '2'
+                    },
+                ],
             },
         }
         klass = self._get_target_class()
@@ -1083,10 +1093,19 @@ def test_from_api_repr_wo_name(self):
         RESOURCE = {
             'parameterType': {
                 'type': 'ARRAY',
-                'arrayType': 'INT64',
+                'arrayType': {
+                    'type': 'INT64',
+                },
             },
             'parameterValue': {
-                'arrayValues': ['1', '2'],
+                'arrayValues': [
+                    {
+                        'value': '1',
+                    },
+                    {
+                        'value': '2'
+                    },
+                ],
             },
         }
         klass = self._get_target_class()
@@ -1100,10 +1119,19 @@ def test_to_api_repr_w_name(self):
             'name': 'foo',
             'parameterType': {
                 'type': 'ARRAY',
-                'arrayType': 'INT64',
+                'arrayType': {
+                    'type': 'INT64',
+                },
             },
             'parameterValue': {
-                'arrayValues': ['1', '2'],
+                'arrayValues': [
+                    {
+                        'value': '1',
+                    },
+                    {
+                        'value': '2'
+                    },
+                ],
             },
         }
         param = self._make_one(name='foo', array_type='INT64', values=[1, 2])
@@ -1113,10 +1141,19 @@ def test_to_api_repr_wo_name(self):
         EXPECTED = {
             'parameterType': {
                 'type': 'ARRAY',
-                'arrayType': 'INT64',
+                'arrayType': {
+                    'type': 'INT64',
+                },
             },
             'parameterValue': {
-                'arrayValues': ['1', '2'],
+                'arrayValues': [
+                    {
+                        'value': '1',
+                    },
+                    {
+                        'value': '2'
+                    },
+                ],
             },
         }
         klass = self._get_target_class()
@@ -1127,10 +1164,16 @@ def test_to_api_repr_w_unknown_type(self):
         EXPECTED = {
             'parameterType': {
                 'type': 'ARRAY',
-                'arrayType': 'UNKNOWN',
+                'arrayType': {
+                    'type': 'UNKNOWN',
+                },
             },
             'parameterValue': {
-                'arrayValues': ['unknown'],
+                'arrayValues': [
+                    {
+                        'value': 'unknown',
+                    }
+                ],
             },
         }
         klass = self._get_target_class()
diff --git a/system_tests/bigquery.py b/system_tests/bigquery.py
index 622d63bc3788..51a0573c1289 100644
--- a/system_tests/bigquery.py
+++ b/system_tests/bigquery.py
@@ -482,6 +482,7 @@ def _job_done(instance):
     def test_sync_query_w_standard_sql_types(self):
         import datetime
         from google.cloud._helpers import UTC
+        from google.cloud.bigquery._helpers import ArrayQueryParameter
         from google.cloud.bigquery._helpers import ScalarQueryParameter
         from google.cloud.bigquery._helpers import StructQueryParameter
         naive = datetime.datetime(2016, 12, 5, 12, 41, 9)
@@ -495,6 +496,8 @@ def test_sync_query_w_standard_sql_types(self):
         answer = 42
         answer_param = ScalarQueryParameter(
             name='answer', type_='INT64', value=answer)
+        array_param = ArrayQueryParameter(
+            name='array_param', array_type='INT64', values=[1, 2])
         struct_param = StructQueryParameter(
             'hitchhiker', question_param, answer_param)
         EXAMPLES = [
@@ -570,6 +573,11 @@ def test_sync_query_w_standard_sql_types(self):
                 'expected': zoned,
                 'query_parameters': [zoned_param],
             },
+            {
+                'sql': 'SELECT @array_param',
+                'expected': [1, 2],
+                'query_parameters': [array_param],
+            },
             {
                 'sql': 'SELECT (@hitchhiker.question, @hitchhiker.answer)',
                 'expected': ({'_field_1': question, '_field_2': answer}),