Skip to content

Commit

Permalink
BQ: Pass selected_fields as a string to tabledata.list. (#4143)
Browse files Browse the repository at this point in the history
BigQuery was only returning the first column when passing in a list
instead of a comma-separated string.
  • Loading branch information
tswast committed Oct 16, 2017
1 parent 9876ad6 commit f7a52cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 20 additions & 2 deletions bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit f7a52cb

Please sign in to comment.