From b53fec52fff7ae3dd5b230bfff2b4e8f66c0573c Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 29 Mar 2016 13:59:03 -0400 Subject: [PATCH] Cache job constructed for a synchronous query. Fixes #1673. --- gcloud/bigquery/query.py | 10 +++++++--- gcloud/bigquery/test_query.py | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/gcloud/bigquery/query.py b/gcloud/bigquery/query.py index 0e580cb4826f..f9158703ce68 100644 --- a/gcloud/bigquery/query.py +++ b/gcloud/bigquery/query.py @@ -50,6 +50,7 @@ def __init__(self, query, client): self._properties = {} self.query = query self._configuration = _SyncQueryConfiguration() + self._job = None @property def project(self): @@ -134,9 +135,12 @@ def job(self): :returns: Job instance used to run the query (None until ``jobReference`` property is set by the server). """ - job_ref = self._properties.get('jobReference') - if job_ref is not None: - return QueryJob(job_ref['jobId'], self.query, self._client) + if self._job is None: + job_ref = self._properties.get('jobReference') + if job_ref is not None: + self._job = QueryJob(job_ref['jobId'], self.query, + self._client) + return self._job @property def page_token(self): diff --git a/gcloud/bigquery/test_query.py b/gcloud/bigquery/test_query.py index b1d323e80bed..bed46d9e85e3 100644 --- a/gcloud/bigquery/test_query.py +++ b/gcloud/bigquery/test_query.py @@ -156,6 +156,8 @@ def test_job_w_jobid(self): self.assertEqual(job.query, self.QUERY) self.assertTrue(job._client is client) self.assertEqual(job.name, SERVER_GENERATED) + fetched_later = query.job + self.assertTrue(fetched_later is job) def test_schema(self): client = _Client(self.PROJECT)