Skip to content

Commit 35ec0f0

Browse files
committed
Merge pull request tylertreat#101 from Jonovono/use-legacy-sql
Add support for UseLegacySQL boolean in order to use standard SQL
2 parents 5fa9442 + a9510cf commit 35ec0f0

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

bigquery/client.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def _insert_job(self, body_object):
262262
body=body_object
263263
).execute()
264264

265-
def query(self, query, max_results=None, timeout=0, dry_run=False):
265+
def query(self, query, max_results=None, timeout=0, dry_run=False, use_legacy_sql=None):
266266
"""Submit a query to BigQuery.
267267
268268
Parameters
@@ -278,6 +278,9 @@ def query(self, query, max_results=None, timeout=0, dry_run=False):
278278
If True, the query isn't actually run. A valid query will return an
279279
empty response, while an invalid one will return the same error
280280
message it would if it wasn't a dry run.
281+
use_legacy_sql : bool, optional. Default True.
282+
If False, the query will use BigQuery's standard SQL (https://cloud.google.com/bigquery/sql-reference/)
283+
281284
282285
Returns
283286
-------
@@ -298,8 +301,12 @@ def query(self, query, max_results=None, timeout=0, dry_run=False):
298301
'query': query,
299302
'timeoutMs': timeout * 1000,
300303
'dryRun': dry_run,
301-
'maxResults': max_results,
304+
'maxResults': max_results
302305
}
306+
307+
if use_legacy_sql is not None:
308+
query_data['useLegacySql'] = use_legacy_sql
309+
303310
return self._submit_query_job(query_data)
304311

305312
def get_query_schema(self, job_id):
@@ -1027,6 +1034,7 @@ def write_to_table(
10271034
priority=None,
10281035
create_disposition=None,
10291036
write_disposition=None,
1037+
use_legacy_sql=None
10301038
):
10311039
"""
10321040
Write query result to table. If dataset or table is not provided,
@@ -1055,6 +1063,9 @@ def write_to_table(
10551063
One of the JOB_CREATE_* constants
10561064
write_disposition : str, optional
10571065
One of the JOB_WRITE_* constants
1066+
use_legacy_sql:
1067+
If False, the query will use BigQuery's standard SQL (https://cloud.google.com/bigquery/sql-reference/)
1068+
10581069
10591070
Returns
10601071
-------
@@ -1084,6 +1095,9 @@ def write_to_table(
10841095
if use_query_cache is not None:
10851096
configuration['useQueryCache'] = use_query_cache
10861097

1098+
if use_legacy_sql is not None:
1099+
configuration['useLegacySql'] = use_legacy_sql
1100+
10871101
if priority:
10881102
configuration['priority'] = priority
10891103

bigquery/tests/test_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ def test_query(self):
253253
self.assertEquals(job_id, 'spiderman')
254254
self.assertEquals(results, [])
255255

256+
256257
def test_query_max_results_set(self):
257258
"""Ensure that we retrieve the job id from the query and the maxResults
258259
parameter is set.
@@ -418,6 +419,30 @@ def test_query_with_results(self):
418419
self.assertEquals(job_id, 'spiderman')
419420
self.assertEquals(results, [{'foo': 10}])
420421

422+
def test_query_with_using_legacy_sql(self):
423+
"""Ensure that use_legacy_sql bool gets used"""
424+
425+
mock_query_job = mock.Mock()
426+
expected_job_id = 'spiderman'
427+
expected_job_ref = {'jobId': expected_job_id}
428+
429+
mock_query_job.execute.return_value = {
430+
'jobReference': expected_job_ref,
431+
'jobComplete': True
432+
}
433+
434+
self.mock_job_collection.query.return_value = mock_query_job
435+
436+
job_id, results = self.client.query(self.query, use_legacy_sql=False)
437+
438+
self.mock_job_collection.query.assert_called_once_with(
439+
projectId=self.project_id,
440+
body={'query': self.query, 'timeoutMs': 0, 'dryRun': False,
441+
'maxResults': None, 'useLegacySql': False}
442+
)
443+
self.assertEquals(job_id, 'spiderman')
444+
self.assertEquals(results, [])
445+
421446

422447
class TestGetQueryResults(unittest.TestCase):
423448

0 commit comments

Comments
 (0)