Skip to content

Commit ba61a8a

Browse files
authored
docs: add short mode query sample & test (#1978)
* docs: add short mode query sample & test
1 parent 57bf873 commit ba61a8a

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

samples/client_query_shortmode.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def client_query_shortmode() -> None:
17+
# [START bigquery_query_shortquery]
18+
# This example demonstrates issuing a query that may be run in short query mode.
19+
#
20+
# To enable the short query mode preview feature, the QUERY_PREVIEW_ENABLED
21+
# environmental variable should be set to `TRUE`.
22+
from google.cloud import bigquery
23+
24+
# Construct a BigQuery client object.
25+
client = bigquery.Client()
26+
27+
query = """
28+
SELECT
29+
name,
30+
gender,
31+
SUM(number) AS total
32+
FROM
33+
bigquery-public-data.usa_names.usa_1910_2013
34+
GROUP BY
35+
name, gender
36+
ORDER BY
37+
total DESC
38+
LIMIT 10
39+
"""
40+
# Run the query. The returned `rows` iterator can return information about
41+
# how the query was executed as well as the result data.
42+
rows = client.query_and_wait(query)
43+
44+
if rows.job_id is not None:
45+
print("Query was run with job state. Job ID: {}".format(rows.job_id))
46+
else:
47+
print("Query was run in short mode. Query ID: {}".format(rows.query_id))
48+
49+
print("The query data:")
50+
for row in rows:
51+
# Row values can be accessed by field name or index.
52+
print("name={}, gender={}, total={}".format(row[0], row[1], row["total"]))
53+
# [END bigquery_query_shortquery]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import typing
16+
17+
from .. import client_query_shortmode
18+
19+
if typing.TYPE_CHECKING:
20+
import pytest
21+
22+
23+
def test_client_query_shortmode(capsys: "pytest.CaptureFixture[str]") -> None:
24+
client_query_shortmode.client_query_shortmode()
25+
out, err = capsys.readouterr()
26+
assert "Query was run" in out

0 commit comments

Comments
 (0)