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
28 changes: 19 additions & 9 deletions bigquery/api/async_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

"""Command-line application to perform an asynchronous query in BigQuery.

This sample is used on this page:

https://cloud.google.com/bigquery/querying-data#asyncqueries

For more information, see the README.md under /bigquery.
"""

Expand All @@ -32,7 +28,9 @@


# [START async_query]
def async_query(bigquery, project_id, query, batch=False, num_retries=5):
def async_query(
bigquery, project_id, query,
batch=False, num_retries=5, use_legacy_sql=False):
# Generate a unique job ID so retries
# don't accidentally duplicate query
job_data = {
Expand All @@ -43,7 +41,10 @@ def async_query(bigquery, project_id, query, batch=False, num_retries=5):
'configuration': {
'query': {
'query': query,
'priority': 'BATCH' if batch else 'INTERACTIVE'
'priority': 'BATCH' if batch else 'INTERACTIVE',
# Set to False to use standard SQL syntax. See:
# https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
'useLegacySQL': use_legacy_sql
}
}
}
Expand Down Expand Up @@ -77,7 +78,9 @@ def poll_job(bigquery, job):


# [START run]
def main(project_id, query_string, batch, num_retries, interval):
def main(
project_id, query_string, batch, num_retries, interval,
use_legacy_sql):
# [START build_service]
# Grab the application's default credentials from the environment.
credentials = GoogleCredentials.get_application_default()
Expand All @@ -92,7 +95,8 @@ def main(project_id, query_string, batch, num_retries, interval):
project_id,
query_string,
batch,
num_retries)
num_retries,
use_legacy_sql)

poll_job(bigquery, query_job)

Expand Down Expand Up @@ -130,6 +134,11 @@ def main(project_id, query_string, batch, num_retries, interval):
help='How often to poll the query for completion (seconds).',
type=int,
default=1)
parser.add_argument(
'-l', '--use_legacy_sql',
help='Use legacy BigQuery SQL syntax instead of standard SQL syntax.',
type=bool,
default=False)

args = parser.parse_args()

Expand All @@ -138,5 +147,6 @@ def main(project_id, query_string, batch, num_retries, interval):
args.query,
args.batch,
args.num_retries,
args.poll_interval)
args.poll_interval,
args.use_legacy_sql)
# [END main]
22 changes: 21 additions & 1 deletion bigquery/api/async_query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,27 @@ def test_async_query(cloud_config, capsys):
query_string=query,
batch=False,
num_retries=5,
interval=1)
interval=1,
use_legacy_sql=True)

out, _ = capsys.readouterr()
value = out.strip().split('\n').pop()

assert json.loads(value) is not None


def test_async_query_standard_sql(cloud_config, capsys):
query = (
'SELECT corpus FROM publicdata.samples.shakespeare '
'GROUP BY corpus;')

main(
project_id=cloud_config.project,
query_string=query,
batch=False,
num_retries=5,
interval=1,
use_legacy_sql=False)

out, _ = capsys.readouterr()
value = out.strip().split('\n').pop()
Expand Down
24 changes: 16 additions & 8 deletions bigquery/api/sync_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@

"""Command-line application to perform an synchronous query in BigQuery.

This sample is used on this page:

https://cloud.google.com/bigquery/querying-data#syncqueries

For more information, see the README.md under /bigquery.
"""

Expand All @@ -30,10 +26,15 @@


# [START sync_query]
def sync_query(bigquery, project_id, query, timeout=10000, num_retries=5):
def sync_query(
bigquery, project_id, query,
timeout=10000, num_retries=5, use_legacy_sql=False):
query_data = {
'query': query,
'timeoutMs': timeout,
# Set to False to use standard SQL syntax. See:
# https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
'useLegacySQL': use_legacy_sql
}
return bigquery.jobs().query(
projectId=project_id,
Expand All @@ -42,7 +43,7 @@ def sync_query(bigquery, project_id, query, timeout=10000, num_retries=5):


# [START run]
def main(project_id, query, timeout, num_retries):
def main(project_id, query, timeout, num_retries, use_legacy_sql):
# [START build_service]
# Grab the application's default credentials from the environment.
credentials = GoogleCredentials.get_application_default()
Expand All @@ -56,7 +57,8 @@ def main(project_id, query, timeout, num_retries):
project_id,
query,
timeout,
num_retries)
num_retries,
use_legacy_sql)

# [START paging]
# Page through the result set and print all results.
Expand Down Expand Up @@ -96,13 +98,19 @@ def main(project_id, query, timeout, num_retries):
help='Number of times to retry in case of 500 error.',
type=int,
default=5)
parser.add_argument(
'-l', '--use_legacy_sql',
help='Use legacy BigQuery SQL syntax instead of standard SQL syntax.',
type=bool,
default=False)

args = parser.parse_args()

main(
args.project_id,
args.query,
args.timeout,
args.num_retries)
args.num_retries,
args.use_legacy_sql)

# [END main]
21 changes: 20 additions & 1 deletion bigquery/api/sync_query_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ def test_sync_query(cloud_config, capsys):
project_id=cloud_config.project,
query=query,
timeout=30,
num_retries=5)
num_retries=5,
use_legacy_sql=True)

out, _ = capsys.readouterr()
result = out.split('\n')[0]

assert json.loads(result) is not None


def test_sync_query_standard_sql(cloud_config, capsys):
query = (
'SELECT corpus FROM publicdata.samples.shakespeare '
'GROUP BY corpus;')

main(
project_id=cloud_config.project,
query=query,
timeout=30,
num_retries=5,
use_legacy_sql=False)

out, _ = capsys.readouterr()
result = out.split('\n')[0]
Expand Down