Skip to content

Commit b8cde46

Browse files
alixhamitswast
authored andcommitted
BigQuery: removes Client.query_rows() (#4429)
1 parent 58b642e commit b8cde46

File tree

5 files changed

+46
-370
lines changed

5 files changed

+46
-370
lines changed

bigquery/google/cloud/bigquery/client.py

+2-66
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from __future__ import absolute_import
1818

1919
import collections
20-
import concurrent.futures
2120
import functools
2221
import os
2322
import uuid
@@ -29,8 +28,6 @@
2928
from google.resumable_media.requests import ResumableUpload
3029

3130
from google.api_core import page_iterator
32-
from google.api_core.exceptions import GoogleAPICallError
33-
from google.api_core.exceptions import NotFound
3431
from google.cloud import exceptions
3532
from google.cloud.client import ClientWithProject
3633

@@ -1144,67 +1141,6 @@ def create_rows_json(self, table, json_rows, row_ids=None,
11441141

11451142
return errors
11461143

1147-
def query_rows(
1148-
self, query, job_config=None, job_id=None, job_id_prefix=None,
1149-
timeout=None, retry=DEFAULT_RETRY):
1150-
"""Start a query job and wait for the results.
1151-
1152-
See
1153-
https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query
1154-
1155-
:type query: str
1156-
:param query:
1157-
SQL query to be executed. Defaults to the standard SQL dialect.
1158-
Use the ``job_config`` parameter to change dialects.
1159-
1160-
:type job_config: :class:`google.cloud.bigquery.job.QueryJobConfig`
1161-
:param job_config: (Optional) Extra configuration options for the job.
1162-
1163-
:type job_id: str
1164-
:param job_id: (Optional) ID to use for the query job.
1165-
1166-
:type job_id_prefix: str or ``NoneType``
1167-
:param job_id_prefix: (Optional) the user-provided prefix for a
1168-
randomly generated job ID. This parameter will be
1169-
ignored if a ``job_id`` is also given.
1170-
1171-
:type timeout: float
1172-
:param timeout:
1173-
(Optional) How long (in seconds) to wait for job to complete
1174-
before raising a :class:`concurrent.futures.TimeoutError`.
1175-
1176-
:rtype: :class:`~google.api_core.page_iterator.Iterator`
1177-
:returns:
1178-
Iterator of row data :class:`~google.cloud.bigquery.table.Row`-s.
1179-
During each page, the iterator will have the ``total_rows``
1180-
attribute set, which counts the total number of rows **in the
1181-
result set** (this is distinct from the total number of rows in
1182-
the current page: ``iterator.page.num_items``).
1183-
1184-
:raises:
1185-
:class:`~google.api_core.exceptions.GoogleAPICallError` if the
1186-
job failed or :class:`concurrent.futures.TimeoutError` if the job
1187-
did not complete in the given timeout.
1188-
1189-
When an exception happens, the query job will be cancelled on a
1190-
best-effort basis.
1191-
"""
1192-
job_id = _make_job_id(job_id, job_id_prefix)
1193-
1194-
try:
1195-
job = self.query(
1196-
query, job_config=job_config, job_id=job_id, retry=retry)
1197-
rows_iterator = job.result(timeout=timeout)
1198-
except (GoogleAPICallError, concurrent.futures.TimeoutError):
1199-
try:
1200-
self.cancel_job(job_id)
1201-
except NotFound:
1202-
# It's OK if couldn't cancel because job never got created.
1203-
pass
1204-
raise
1205-
1206-
return rows_iterator
1207-
12081144
def list_rows(self, table, selected_fields=None, max_results=None,
12091145
page_token=None, start_index=None, retry=DEFAULT_RETRY):
12101146
"""List the rows of the table.
@@ -1303,12 +1239,12 @@ def list_partitions(self, table, retry=DEFAULT_RETRY):
13031239
"""
13041240
config = QueryJobConfig()
13051241
config.use_legacy_sql = True # required for '$' syntax
1306-
rows = self.query_rows(
1242+
query_job = self.query(
13071243
'SELECT partition_id from [%s:%s.%s$__PARTITIONS_SUMMARY__]' %
13081244
(table.project, table.dataset_id, table.table_id),
13091245
job_config=config,
13101246
retry=retry)
1311-
return [row[0] for row in rows]
1247+
return [row[0] for row in query_job]
13121248

13131249

13141250
# pylint: disable=unused-argument

bigquery/tests/system.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def test_job_cancel(self):
673673
# raise an error, and that the job completed (in the `retry()`
674674
# above).
675675

676-
def test_query_rows_w_legacy_sql_types(self):
676+
def test_query_w_legacy_sql_types(self):
677677
naive = datetime.datetime(2016, 12, 5, 12, 41, 9)
678678
stamp = '%s %s' % (naive.date().isoformat(), naive.time().isoformat())
679679
zoned = naive.replace(tzinfo=UTC)
@@ -706,7 +706,7 @@ def test_query_rows_w_legacy_sql_types(self):
706706
for example in examples:
707707
job_config = bigquery.QueryJobConfig()
708708
job_config.use_legacy_sql = True
709-
rows = list(Config.CLIENT.query_rows(
709+
rows = list(Config.CLIENT.query(
710710
example['sql'], job_config=job_config))
711711
self.assertEqual(len(rows), 1)
712712
self.assertEqual(len(rows[0]), 1)
@@ -806,26 +806,28 @@ def _generate_standard_sql_types_examples(self):
806806
},
807807
]
808808

809-
def test_query_rows_w_standard_sql_types(self):
809+
def test_query_w_standard_sql_types(self):
810810
examples = self._generate_standard_sql_types_examples()
811811
for example in examples:
812-
rows = list(Config.CLIENT.query_rows(example['sql']))
812+
rows = list(Config.CLIENT.query(example['sql']))
813813
self.assertEqual(len(rows), 1)
814814
self.assertEqual(len(rows[0]), 1)
815815
self.assertEqual(rows[0][0], example['expected'])
816816

817-
def test_query_rows_w_failed_query(self):
817+
def test_query_w_failed_query(self):
818818
from google.api_core.exceptions import BadRequest
819819

820820
with self.assertRaises(BadRequest):
821-
Config.CLIENT.query_rows('invalid syntax;')
821+
Config.CLIENT.query('invalid syntax;').result()
822+
823+
def test_query_w_timeout(self):
824+
query_job = Config.CLIENT.query(
825+
'SELECT * FROM `bigquery-public-data.github_repos.commits`;',
826+
job_id_prefix='test_query_w_timeout_')
822827

823-
def test_query_rows_w_timeout(self):
824828
with self.assertRaises(concurrent.futures.TimeoutError):
825-
Config.CLIENT.query_rows(
826-
'SELECT * FROM `bigquery-public-data.github_repos.commits`;',
827-
job_id_prefix='test_query_rows_w_timeout_',
828-
timeout=1) # 1 second is much too short for this query.
829+
# 1 second is much too short for this query.
830+
query_job.result(timeout=1)
829831

830832
def test_dbapi_w_standard_sql_types(self):
831833
examples = self._generate_standard_sql_types_examples()
@@ -1224,9 +1226,9 @@ def test_large_query_w_public_data(self):
12241226
SQL = 'SELECT * from `{}.{}.{}` LIMIT {}'.format(
12251227
PUBLIC, DATASET_ID, TABLE_NAME, LIMIT)
12261228

1227-
iterator = Config.CLIENT.query_rows(SQL)
1229+
query_job = Config.CLIENT.query(SQL)
12281230

1229-
rows = list(iterator)
1231+
rows = list(query_job)
12301232
self.assertEqual(len(rows), LIMIT)
12311233

12321234
def test_query_future(self):
@@ -1256,7 +1258,7 @@ def test_query_table_def(self):
12561258
job_config.table_definitions = {table_id: ec}
12571259
sql = 'SELECT * FROM %s' % table_id
12581260

1259-
got_rows = Config.CLIENT.query_rows(sql, job_config=job_config)
1261+
got_rows = Config.CLIENT.query(sql, job_config=job_config)
12601262

12611263
row_tuples = [r.values() for r in got_rows]
12621264
by_age = operator.itemgetter(1)
@@ -1280,7 +1282,7 @@ def test_query_external_table(self):
12801282

12811283
sql = 'SELECT * FROM %s.%s' % (dataset_id, table_id)
12821284

1283-
got_rows = Config.CLIENT.query_rows(sql)
1285+
got_rows = Config.CLIENT.query(sql)
12841286

12851287
row_tuples = [r.values() for r in got_rows]
12861288
by_age = operator.itemgetter(1)

0 commit comments

Comments
 (0)