Skip to content

Commit

Permalink
Merge pull request googleapis#1992 from dhermes/fix-1607
Browse files Browse the repository at this point in the history
Adding BigQuery support for legacy SQL.
  • Loading branch information
dhermes committed Jul 18, 2016
2 parents e653b95 + 21ae0c9 commit d171791
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gcloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ class _AsyncQueryConfiguration(object):
_flatten_results = None
_priority = None
_use_query_cache = None
_use_legacy_sql = None
_write_disposition = None


Expand Down Expand Up @@ -927,6 +928,12 @@ def __init__(self, name, query, client):
https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query.useQueryCache
"""

use_legacy_sql = _TypedProperty('use_legacy_sql', bool)
"""See:
https://cloud.google.com/bigquery/docs/\
reference/v2/jobs#configuration.query.useLegacySql
"""

write_disposition = WriteDisposition('write_disposition')
"""See:
https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.query.writeDisposition
Expand Down Expand Up @@ -965,6 +972,8 @@ def _populate_config_resource(self, configuration):
configuration['priority'] = self.priority
if self.use_query_cache is not None:
configuration['useQueryCache'] = self.use_query_cache
if self.use_legacy_sql is not None:
configuration['useLegacySql'] = self.use_legacy_sql
if self.write_disposition is not None:
configuration['writeDisposition'] = self.write_disposition

Expand Down
10 changes: 10 additions & 0 deletions gcloud/bigquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class _SyncQueryConfiguration(object):
_timeout_ms = None
_preserve_nulls = None
_use_query_cache = None
_use_legacy_sql = None


class QueryResults(object):
Expand Down Expand Up @@ -233,6 +234,12 @@ def schema(self):
https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#useQueryCache
"""

use_legacy_sql = _TypedProperty('use_legacy_sql', bool)
"""See:
https://cloud.google.com/bigquery/docs/\
reference/v2/jobs/query#useLegacySql
"""

def _set_properties(self, api_response):
"""Update properties from resource in body of ``api_response``
Expand Down Expand Up @@ -264,6 +271,9 @@ def _build_resource(self):
if self.use_query_cache is not None:
resource['useQueryCache'] = self.use_query_cache

if self.use_legacy_sql is not None:
resource['useLegacySql'] = self.use_legacy_sql

if self.dry_run is not None:
resource['dryRun'] = self.dry_run

Expand Down
8 changes: 8 additions & 0 deletions gcloud/bigquery/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,11 @@ def _verifyBooleanResourceProperties(self, job, config):
config['useQueryCache'])
else:
self.assertTrue(job.use_query_cache is None)
if 'useLegacySql' in config:
self.assertEqual(job.use_legacy_sql,
config['useLegacySql'])
else:
self.assertTrue(job.use_legacy_sql is None)

def _verifyResourceProperties(self, job, resource):
self._verifyReadonlyResourceProperties(job, resource)
Expand Down Expand Up @@ -1310,6 +1315,7 @@ def test_ctor(self):
self.assertTrue(job.flatten_results is None)
self.assertTrue(job.priority is None)
self.assertTrue(job.use_query_cache is None)
self.assertTrue(job.use_legacy_sql is None)
self.assertTrue(job.write_disposition is None)

def test_from_api_repr_missing_identity(self):
Expand Down Expand Up @@ -1420,6 +1426,7 @@ def test_begin_w_alternate_client(self):
'flattenResults': True,
'priority': 'INTERACTIVE',
'useQueryCache': True,
'useLegacySql': True,
'writeDisposition': 'WRITE_TRUNCATE',
}
RESOURCE['configuration']['query'] = QUERY_CONFIGURATION
Expand All @@ -1439,6 +1446,7 @@ def test_begin_w_alternate_client(self):
job.flatten_results = True
job.priority = 'INTERACTIVE'
job.use_query_cache = True
job.use_legacy_sql = True
job.write_disposition = 'WRITE_TRUNCATE'

job.begin(client=client2)
Expand Down
3 changes: 3 additions & 0 deletions gcloud/bigquery/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_ctor(self):
self.assertTrue(query.max_results is None)
self.assertTrue(query.preserve_nulls is None)
self.assertTrue(query.use_query_cache is None)
self.assertTrue(query.use_legacy_sql is None)

def test_job_wo_jobid(self):
client = _Client(self.PROJECT)
Expand Down Expand Up @@ -206,6 +207,7 @@ def test_run_w_alternate_client(self):
query.preserve_nulls = True
query.timeout_ms = 20000
query.use_query_cache = False
query.use_legacy_sql = True
query.dry_run = True

query.run(client=client2)
Expand All @@ -226,6 +228,7 @@ def test_run_w_alternate_client(self):
'preserveNulls': True,
'timeoutMs': 20000,
'useQueryCache': False,
'useLegacySql': True,
}
self.assertEqual(req['data'], SENT)
self._verifyResourceProperties(query, RESOURCE)
Expand Down

0 comments on commit d171791

Please sign in to comment.