Skip to content

Commit

Permalink
Merge pull request #2901 from tswast/arrays
Browse files Browse the repository at this point in the history
Fix array handling in parameterized queries
  • Loading branch information
tseaver authored Dec 28, 2016
2 parents d158cc7 + 1c9087d commit 6119779
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
13 changes: 9 additions & 4 deletions bigquery/google/cloud/bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -502,10 +505,12 @@ def to_api_repr(self):
resource = {
'parameterType': {
'type': 'ARRAY',
'arrayType': self.array_type,
'arrayType': {
'type': self.array_type,
},
},
'parameterValue': {
'arrayValues': values,
'arrayValues': [{'value': value} for value in values],
},
}
if self.name is not None:
Expand Down
63 changes: 53 additions & 10 deletions bigquery/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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])
Expand All @@ -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()
Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions system_tests/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = [
Expand Down Expand Up @@ -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}),
Expand Down

0 comments on commit 6119779

Please sign in to comment.