Skip to content

Commit 13b10d4

Browse files
tswastJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Document usage of useLegacySQL parameter in BigQuery samples. (#445)
The useLegacySQL parameter must be explicitly set in order to use standard SQL syntax. This adds usage of that parameter to the query samples. I plan to also include this code in the table at https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
1 parent 72f726b commit 13b10d4

File tree

4 files changed

+76
-19
lines changed

4 files changed

+76
-19
lines changed

bigquery/api/async_query.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
"""Command-line application to perform an asynchronous query in BigQuery.
1717
18-
This sample is used on this page:
19-
20-
https://cloud.google.com/bigquery/querying-data#asyncqueries
21-
2218
For more information, see the README.md under /bigquery.
2319
"""
2420

@@ -32,7 +28,9 @@
3228

3329

3430
# [START async_query]
35-
def async_query(bigquery, project_id, query, batch=False, num_retries=5):
31+
def async_query(
32+
bigquery, project_id, query,
33+
batch=False, num_retries=5, use_legacy_sql=False):
3634
# Generate a unique job ID so retries
3735
# don't accidentally duplicate query
3836
job_data = {
@@ -43,7 +41,10 @@ def async_query(bigquery, project_id, query, batch=False, num_retries=5):
4341
'configuration': {
4442
'query': {
4543
'query': query,
46-
'priority': 'BATCH' if batch else 'INTERACTIVE'
44+
'priority': 'BATCH' if batch else 'INTERACTIVE',
45+
# Set to False to use standard SQL syntax. See:
46+
# https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
47+
'useLegacySQL': use_legacy_sql
4748
}
4849
}
4950
}
@@ -77,7 +78,9 @@ def poll_job(bigquery, job):
7778

7879

7980
# [START run]
80-
def main(project_id, query_string, batch, num_retries, interval):
81+
def main(
82+
project_id, query_string, batch, num_retries, interval,
83+
use_legacy_sql):
8184
# [START build_service]
8285
# Grab the application's default credentials from the environment.
8386
credentials = GoogleCredentials.get_application_default()
@@ -92,7 +95,8 @@ def main(project_id, query_string, batch, num_retries, interval):
9295
project_id,
9396
query_string,
9497
batch,
95-
num_retries)
98+
num_retries,
99+
use_legacy_sql)
96100

97101
poll_job(bigquery, query_job)
98102

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

134143
args = parser.parse_args()
135144

@@ -138,5 +147,6 @@ def main(project_id, query_string, batch, num_retries, interval):
138147
args.query,
139148
args.batch,
140149
args.num_retries,
141-
args.poll_interval)
150+
args.poll_interval,
151+
args.use_legacy_sql)
142152
# [END main]

bigquery/api/async_query_test.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,27 @@ def test_async_query(cloud_config, capsys):
2626
query_string=query,
2727
batch=False,
2828
num_retries=5,
29-
interval=1)
29+
interval=1,
30+
use_legacy_sql=True)
31+
32+
out, _ = capsys.readouterr()
33+
value = out.strip().split('\n').pop()
34+
35+
assert json.loads(value) is not None
36+
37+
38+
def test_async_query_standard_sql(cloud_config, capsys):
39+
query = (
40+
'SELECT corpus FROM publicdata.samples.shakespeare '
41+
'GROUP BY corpus;')
42+
43+
main(
44+
project_id=cloud_config.project,
45+
query_string=query,
46+
batch=False,
47+
num_retries=5,
48+
interval=1,
49+
use_legacy_sql=False)
3050

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

bigquery/api/sync_query.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
"""Command-line application to perform an synchronous query in BigQuery.
1717
18-
This sample is used on this page:
19-
20-
https://cloud.google.com/bigquery/querying-data#syncqueries
21-
2218
For more information, see the README.md under /bigquery.
2319
"""
2420

@@ -30,10 +26,15 @@
3026

3127

3228
# [START sync_query]
33-
def sync_query(bigquery, project_id, query, timeout=10000, num_retries=5):
29+
def sync_query(
30+
bigquery, project_id, query,
31+
timeout=10000, num_retries=5, use_legacy_sql=False):
3432
query_data = {
3533
'query': query,
3634
'timeoutMs': timeout,
35+
# Set to False to use standard SQL syntax. See:
36+
# https://cloud.google.com/bigquery/sql-reference/enabling-standard-sql
37+
'useLegacySQL': use_legacy_sql
3738
}
3839
return bigquery.jobs().query(
3940
projectId=project_id,
@@ -42,7 +43,7 @@ def sync_query(bigquery, project_id, query, timeout=10000, num_retries=5):
4243

4344

4445
# [START run]
45-
def main(project_id, query, timeout, num_retries):
46+
def main(project_id, query, timeout, num_retries, use_legacy_sql):
4647
# [START build_service]
4748
# Grab the application's default credentials from the environment.
4849
credentials = GoogleCredentials.get_application_default()
@@ -56,7 +57,8 @@ def main(project_id, query, timeout, num_retries):
5657
project_id,
5758
query,
5859
timeout,
59-
num_retries)
60+
num_retries,
61+
use_legacy_sql)
6062

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

100107
args = parser.parse_args()
101108

102109
main(
103110
args.project_id,
104111
args.query,
105112
args.timeout,
106-
args.num_retries)
113+
args.num_retries,
114+
args.use_legacy_sql)
107115

108116
# [END main]

bigquery/api/sync_query_test.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,26 @@ def test_sync_query(cloud_config, capsys):
2525
project_id=cloud_config.project,
2626
query=query,
2727
timeout=30,
28-
num_retries=5)
28+
num_retries=5,
29+
use_legacy_sql=True)
30+
31+
out, _ = capsys.readouterr()
32+
result = out.split('\n')[0]
33+
34+
assert json.loads(result) is not None
35+
36+
37+
def test_sync_query_standard_sql(cloud_config, capsys):
38+
query = (
39+
'SELECT corpus FROM publicdata.samples.shakespeare '
40+
'GROUP BY corpus;')
41+
42+
main(
43+
project_id=cloud_config.project,
44+
query=query,
45+
timeout=30,
46+
num_retries=5,
47+
use_legacy_sql=False)
2948

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

0 commit comments

Comments
 (0)