From 6b2618cd9c048071d1fe89340397d2bdf536986c Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Wed, 17 Jul 2024 23:31:19 +0000 Subject: [PATCH 1/5] docs: add short mode query sample & test --- samples/client_query_shortmode.py | 52 ++++++++++++++++++++ samples/tests/test_client_query_shortmode.py | 26 ++++++++++ 2 files changed, 78 insertions(+) create mode 100644 samples/client_query_shortmode.py create mode 100644 samples/tests/test_client_query_shortmode.py diff --git a/samples/client_query_shortmode.py b/samples/client_query_shortmode.py new file mode 100644 index 000000000..0282283c6 --- /dev/null +++ b/samples/client_query_shortmode.py @@ -0,0 +1,52 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +def client_query_shortmode() -> None: + # [START bigquery_query_shortquery] + # This example demonstrates issuing a query that may be run in short query mode. + # + # To enable the short query mode preview feature, the QUERY_PREVIEW_ENABLED + # environmental variable should be set to `TRUE`. + from google.cloud import bigquery + + # Construct a BigQuery client object. + client = bigquery.Client() + + query = """ + SELECT + name, + gender, + SUM(number) AS total + FROM + bigquery-public-data.usa_names.usa_1910_2013 + GROUP BY + name, gender + ORDER BY + total DESC + LIMIT 10 + """ + # Run the query and process the returned row iterator. + rows = client.query_and_wait(query) + + if rows.job_id is not None: + print("Query was run with job state. Job ID: {}".format(rows.job_id)) + else: + print("Query was run in short mode. Query ID: {}".format(rows.query_id)) + + print("The query data:") + for row in rows: + # Row values can be accessed by field name or index. + print("name={}, gender={}, total={}".format(row[0], row[1], row["total"])) + # [END bigquery_query_shortquery] diff --git a/samples/tests/test_client_query_shortmode.py b/samples/tests/test_client_query_shortmode.py new file mode 100644 index 000000000..629b8d69d --- /dev/null +++ b/samples/tests/test_client_query_shortmode.py @@ -0,0 +1,26 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import typing + +from .. import client_query_shortmode + +if typing.TYPE_CHECKING: + import pytest + + +def test_client_query_batch(capsys: "pytest.CaptureFixture[str]") -> None: + job = client_query_shortmode.client_query_shortmode() + out, err = capsys.readouterr() + assert "Query was run" in out From 2408e0d2e08134b309994b53331b35da1aa05c5d Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Wed, 17 Jul 2024 23:34:15 +0000 Subject: [PATCH 2/5] copy/paste is hard --- samples/tests/test_client_query_shortmode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/tests/test_client_query_shortmode.py b/samples/tests/test_client_query_shortmode.py index 629b8d69d..7dd92364f 100644 --- a/samples/tests/test_client_query_shortmode.py +++ b/samples/tests/test_client_query_shortmode.py @@ -20,7 +20,7 @@ import pytest -def test_client_query_batch(capsys: "pytest.CaptureFixture[str]") -> None: +def test_client_query_shortmode(capsys: "pytest.CaptureFixture[str]") -> None: job = client_query_shortmode.client_query_shortmode() out, err = capsys.readouterr() assert "Query was run" in out From f205f8b02b33afdf616c4c282ebc1a0e2863a8a6 Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 19 Jul 2024 19:30:10 +0000 Subject: [PATCH 3/5] comments/formatting --- samples/client_query_shortmode.py | 7 ++++--- samples/tests/test_client_query_shortmode.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/samples/client_query_shortmode.py b/samples/client_query_shortmode.py index 0282283c6..dd07a94e1 100644 --- a/samples/client_query_shortmode.py +++ b/samples/client_query_shortmode.py @@ -26,9 +26,9 @@ def client_query_shortmode() -> None: query = """ SELECT - name, + name, gender, - SUM(number) AS total + SUM(number) AS total FROM bigquery-public-data.usa_names.usa_1910_2013 GROUP BY @@ -37,7 +37,8 @@ def client_query_shortmode() -> None: total DESC LIMIT 10 """ - # Run the query and process the returned row iterator. + # Run the query. The returned `rows` iterator can return information about + # how the query was executed as well as the result data. rows = client.query_and_wait(query) if rows.job_id is not None: diff --git a/samples/tests/test_client_query_shortmode.py b/samples/tests/test_client_query_shortmode.py index 7dd92364f..41132f24c 100644 --- a/samples/tests/test_client_query_shortmode.py +++ b/samples/tests/test_client_query_shortmode.py @@ -21,6 +21,6 @@ def test_client_query_shortmode(capsys: "pytest.CaptureFixture[str]") -> None: - job = client_query_shortmode.client_query_shortmode() + client_query_shortmode.client_query_shortmode() out, err = capsys.readouterr() assert "Query was run" in out From 0635dc9a1a007cafe172bf308396579cca8f5bef Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 19 Jul 2024 19:31:31 +0000 Subject: [PATCH 4/5] more formatting --- samples/client_query_shortmode.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/samples/client_query_shortmode.py b/samples/client_query_shortmode.py index dd07a94e1..b652939fb 100644 --- a/samples/client_query_shortmode.py +++ b/samples/client_query_shortmode.py @@ -29,13 +29,13 @@ def client_query_shortmode() -> None: name, gender, SUM(number) AS total - FROM - bigquery-public-data.usa_names.usa_1910_2013 - GROUP BY - name, gender - ORDER BY - total DESC - LIMIT 10 + FROM + bigquery-public-data.usa_names.usa_1910_2013 + GROUP BY + name, gender + ORDER BY + total DESC + LIMIT 10 """ # Run the query. The returned `rows` iterator can return information about # how the query was executed as well as the result data. From 8aad4c7137fa388bd91a937513db3886bb67b29e Mon Sep 17 00:00:00 2001 From: Seth Hollyman Date: Fri, 19 Jul 2024 19:58:31 +0000 Subject: [PATCH 5/5] whitespace --- samples/client_query_shortmode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/client_query_shortmode.py b/samples/client_query_shortmode.py index b652939fb..50446dc48 100644 --- a/samples/client_query_shortmode.py +++ b/samples/client_query_shortmode.py @@ -16,7 +16,7 @@ def client_query_shortmode() -> None: # [START bigquery_query_shortquery] # This example demonstrates issuing a query that may be run in short query mode. - # + # # To enable the short query mode preview feature, the QUERY_PREVIEW_ENABLED # environmental variable should be set to `TRUE`. from google.cloud import bigquery @@ -31,7 +31,7 @@ def client_query_shortmode() -> None: SUM(number) AS total FROM bigquery-public-data.usa_names.usa_1910_2013 - GROUP BY + GROUP BY name, gender ORDER BY total DESC