Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def _insert_job(self, body_object):
body=body_object
).execute()

def query(self, query, max_results=None, timeout=0, dry_run=False):
def query(self, query, max_results=None, timeout=0, dry_run=False, use_legacy_sql=None):
"""Submit a query to BigQuery.

Parameters
Expand All @@ -278,6 +278,9 @@ def query(self, query, max_results=None, timeout=0, dry_run=False):
If True, the query isn't actually run. A valid query will return an
empty response, while an invalid one will return the same error
message it would if it wasn't a dry run.
use_legacy_sql : bool, optional. Default True.
If False, the query will use BigQuery's standard SQL (https://cloud.google.com/bigquery/sql-reference/)


Returns
-------
Expand All @@ -298,8 +301,12 @@ def query(self, query, max_results=None, timeout=0, dry_run=False):
'query': query,
'timeoutMs': timeout * 1000,
'dryRun': dry_run,
'maxResults': max_results,
'maxResults': max_results
}

if use_legacy_sql is not None:
query_data['useLegacySql'] = use_legacy_sql

return self._submit_query_job(query_data)

def get_query_schema(self, job_id):
Expand Down Expand Up @@ -1027,6 +1034,7 @@ def write_to_table(
priority=None,
create_disposition=None,
write_disposition=None,
use_legacy_sql=None
):
"""
Write query result to table. If dataset or table is not provided,
Expand Down Expand Up @@ -1055,6 +1063,9 @@ def write_to_table(
One of the JOB_CREATE_* constants
write_disposition : str, optional
One of the JOB_WRITE_* constants
use_legacy_sql:
If False, the query will use BigQuery's standard SQL (https://cloud.google.com/bigquery/sql-reference/)


Returns
-------
Expand Down Expand Up @@ -1084,6 +1095,9 @@ def write_to_table(
if use_query_cache is not None:
configuration['useQueryCache'] = use_query_cache

if use_legacy_sql is not None:
configuration['useLegacySql'] = use_legacy_sql

if priority:
configuration['priority'] = priority

Expand Down
25 changes: 25 additions & 0 deletions bigquery/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def test_query(self):
self.assertEquals(job_id, 'spiderman')
self.assertEquals(results, [])


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

def test_query_with_using_legacy_sql(self):
"""Ensure that use_legacy_sql bool gets used"""

mock_query_job = mock.Mock()
expected_job_id = 'spiderman'
expected_job_ref = {'jobId': expected_job_id}

mock_query_job.execute.return_value = {
'jobReference': expected_job_ref,
'jobComplete': True
}

self.mock_job_collection.query.return_value = mock_query_job

job_id, results = self.client.query(self.query, use_legacy_sql=False)

self.mock_job_collection.query.assert_called_once_with(
projectId=self.project_id,
body={'query': self.query, 'timeoutMs': 0, 'dryRun': False,
'maxResults': None, 'useLegacySql': False}
)
self.assertEquals(job_id, 'spiderman')
self.assertEquals(results, [])


class TestGetQueryResults(unittest.TestCase):

Expand Down