From f7a52cb15d070f89aca3f9b6fa074eed4b5bf04d Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Mon, 9 Oct 2017 14:20:03 -0700 Subject: [PATCH] BQ: Pass selected_fields as a string to tabledata.list. (#4143) BigQuery was only returning the first column when passing in a list instead of a comma-separated string. --- bigquery/google/cloud/bigquery/client.py | 3 ++- bigquery/tests/system.py | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bigquery/google/cloud/bigquery/client.py b/bigquery/google/cloud/bigquery/client.py index ce41824996bd..756e2bb7d41d 100644 --- a/bigquery/google/cloud/bigquery/client.py +++ b/bigquery/google/cloud/bigquery/client.py @@ -924,7 +924,8 @@ def list_rows(self, table, selected_fields=None, max_results=None, params = {} if selected_fields is not None: - params['selectedFields'] = [f.name for f in selected_fields] + params['selectedFields'] = ','.join( + [f.name for f in selected_fields]) if start_index is not None: params['startIndex'] = start_index diff --git a/bigquery/tests/system.py b/bigquery/tests/system.py index 1bf00a9b57ef..e59747578bb3 100644 --- a/bigquery/tests/system.py +++ b/bigquery/tests/system.py @@ -312,8 +312,9 @@ def test_update_table_schema(self): self.assertEqual(found.mode, expected.mode) @staticmethod - def _fetch_single_page(table): - iterator = Config.CLIENT.list_rows(table) + def _fetch_single_page(table, selected_fields=None): + iterator = Config.CLIENT.list_rows( + table, selected_fields=selected_fields) page = six.next(iterator.pages) return list(page) @@ -1236,6 +1237,23 @@ def test_dump_table_w_public_data(self): table = Config.CLIENT.get_table(table_ref) self._fetch_single_page(table) + def test_dump_table_w_public_data_selected_fields(self): + PUBLIC = 'bigquery-public-data' + DATASET_ID = 'samples' + TABLE_NAME = 'natality' + selected_fields = [ + bigquery.SchemaField('year', 'INTEGER', mode='NULLABLE'), + bigquery.SchemaField('month', 'INTEGER', mode='NULLABLE'), + bigquery.SchemaField('day', 'INTEGER', mode='NULLABLE'), + ] + table_ref = DatasetReference(PUBLIC, DATASET_ID).table(TABLE_NAME) + + rows = self._fetch_single_page( + table_ref, selected_fields=selected_fields) + + self.assertGreater(len(rows), 0) + self.assertEqual(len(rows[0]), 3) + def test_large_query_w_public_data(self): PUBLIC = 'bigquery-public-data' DATASET_ID = 'samples'