From ef53e6c0f84a9ff772ea187c9f38cdc7a03825fc Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 10:58:23 +0300 Subject: [PATCH 01/16] load_and_query_partitioned_table --- bigquery/docs/snippets.py | 72 ------------------- .../samples/client_load_partitioned_table.py | 50 +++++++++++++ .../samples/client_query_partitioned_table.py | 51 +++++++++++++ .../test_client_load_partitioned_table.py | 22 ++++++ .../test_client_query_partitioned_table.py | 26 +++++++ 5 files changed, 149 insertions(+), 72 deletions(-) create mode 100644 bigquery/samples/client_load_partitioned_table.py create mode 100644 bigquery/samples/client_query_partitioned_table.py create mode 100644 bigquery/samples/tests/test_client_load_partitioned_table.py create mode 100644 bigquery/samples/tests/test_client_query_partitioned_table.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 2d950936a5a6..6099f777dc83 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -228,78 +228,6 @@ def test_create_partitioned_table(client, to_delete): assert table.time_partitioning.expiration_ms == 7776000000 -def test_load_and_query_partitioned_table(client, to_delete): - dataset_id = "load_partitioned_table_dataset_{}".format(_millis()) - dataset = bigquery.Dataset(client.dataset(dataset_id)) - client.create_dataset(dataset) - to_delete.append(dataset) - - # [START bigquery_load_table_partitioned] - # from google.cloud import bigquery - # client = bigquery.Client() - # dataset_id = 'my_dataset' - table_id = "us_states_by_date" - - dataset_ref = client.dataset(dataset_id) - job_config = bigquery.LoadJobConfig() - job_config.schema = [ - bigquery.SchemaField("name", "STRING"), - bigquery.SchemaField("post_abbr", "STRING"), - bigquery.SchemaField("date", "DATE"), - ] - job_config.skip_leading_rows = 1 - job_config.time_partitioning = bigquery.TimePartitioning( - type_=bigquery.TimePartitioningType.DAY, - field="date", # name of column to use for partitioning - expiration_ms=7776000000, - ) # 90 days - uri = "gs://cloud-samples-data/bigquery/us-states/us-states-by-date.csv" - - load_job = client.load_table_from_uri( - uri, dataset_ref.table(table_id), job_config=job_config - ) # API request - - assert load_job.job_type == "load" - - load_job.result() # Waits for table load to complete. - - table = client.get_table(dataset_ref.table(table_id)) - print("Loaded {} rows to table {}".format(table.num_rows, table_id)) - # [END bigquery_load_table_partitioned] - assert table.num_rows == 50 - - project_id = client.project - - # [START bigquery_query_partitioned_table] - import datetime - - # from google.cloud import bigquery - # client = bigquery.Client() - # project_id = 'my-project' - # dataset_id = 'my_dataset' - table_id = "us_states_by_date" - - sql_template = """ - SELECT * - FROM `{}.{}.{}` - WHERE date BETWEEN @start_date AND @end_date - """ - sql = sql_template.format(project_id, dataset_id, table_id) - job_config = bigquery.QueryJobConfig() - job_config.query_parameters = [ - bigquery.ScalarQueryParameter("start_date", "DATE", datetime.date(1800, 1, 1)), - bigquery.ScalarQueryParameter("end_date", "DATE", datetime.date(1899, 12, 31)), - ] - - # API request - query_job = client.query(sql, job_config=job_config) - - rows = list(query_job) - print("{} states were admitted to the US in the 1800s".format(len(rows))) - # [END bigquery_query_partitioned_table] - assert len(rows) == 29 - - @pytest.mark.skip( reason=( "update_table() is flaky " diff --git a/bigquery/samples/client_load_partitioned_table.py b/bigquery/samples/client_load_partitioned_table.py new file mode 100644 index 000000000000..3f9f86db9a9a --- /dev/null +++ b/bigquery/samples/client_load_partitioned_table.py @@ -0,0 +1,50 @@ +# Copyright 2019 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_load_partitioned_table(client, table_id): + + # [START bigquery_load_table_partitioned] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table to create. + # table_id = "your-project.your_dataset.your_table_name" + + job_config = bigquery.LoadJobConfig( + schema=[ + bigquery.SchemaField("name", "STRING"), + bigquery.SchemaField("post_abbr", "STRING"), + bigquery.SchemaField("date", "DATE"), + ], + skip_leading_rows=1, + time_partitioning=bigquery.TimePartitioning( + type_=bigquery.TimePartitioningType.DAY, + field="date", # Name of the column to use for partitioning. + expiration_ms=7776000000, # 90 days. + ), + ) + uri = "gs://cloud-samples-data/bigquery/us-states/us-states-by-date.csv" + + load_job = client.load_table_from_uri( + uri, table_id, job_config=job_config + ) # Make an API request. + + load_job.result() # Wait for the job to complete. + + table = client.get_table(table_id) + print("Loaded {} rows to table {}".format(table.num_rows, table_id)) + # [END bigquery_load_table_partitioned] diff --git a/bigquery/samples/client_query_partitioned_table.py b/bigquery/samples/client_query_partitioned_table.py new file mode 100644 index 000000000000..b9b978eb901f --- /dev/null +++ b/bigquery/samples/client_query_partitioned_table.py @@ -0,0 +1,51 @@ +# Copyright 2019 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_partitioned_table(client, table_id): + + # [START bigquery_query_partitioned_table] + from google.cloud import bigquery + + import datetime + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + # TODO(developer): Set table_id to the ID of the table to query from. + # table_id = "your-project.your_dataset.your_table_name" + + sql = """ + SELECT * + FROM `{}` + WHERE date BETWEEN @start_date AND @end_date + """.format( + table_id + ) + + job_config = bigquery.QueryJobConfig( + query_parameters=[ + bigquery.ScalarQueryParameter( + "start_date", "DATE", datetime.date(1800, 1, 1) + ), + bigquery.ScalarQueryParameter( + "end_date", "DATE", datetime.date(1899, 12, 31) + ), + ] + ) + query_job = client.query(sql, job_config=job_config) # Make an API request. + + rows = list(query_job) + print("{} states were admitted to the US in the 1800s".format(len(rows))) + # [END bigquery_query_partitioned_table] diff --git a/bigquery/samples/tests/test_client_load_partitioned_table.py b/bigquery/samples/tests/test_client_load_partitioned_table.py new file mode 100644 index 000000000000..4e4c8811181f --- /dev/null +++ b/bigquery/samples/tests/test_client_load_partitioned_table.py @@ -0,0 +1,22 @@ +# Copyright 2019 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. + +from .. import client_load_partitioned_table + + +def test_client_load_partitioned_table(capsys, client, random_table_id): + + client_load_partitioned_table.client_load_partitioned_table(client, random_table_id) + out, err = capsys.readouterr() + assert "Loaded 50 rows to table {}".format(random_table_id) in out diff --git a/bigquery/samples/tests/test_client_query_partitioned_table.py b/bigquery/samples/tests/test_client_query_partitioned_table.py new file mode 100644 index 000000000000..73fd71b54d5d --- /dev/null +++ b/bigquery/samples/tests/test_client_query_partitioned_table.py @@ -0,0 +1,26 @@ +# Copyright 2019 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. + +from .. import client_load_partitioned_table +from .. import client_query_partitioned_table + + +def test_client_query_partitioned_table(capsys, client, random_table_id): + + client_load_partitioned_table.client_load_partitioned_table(client, random_table_id) + client_query_partitioned_table.client_query_partitioned_table( + client, random_table_id + ) + out, err = capsys.readouterr() + assert "29 states were admitted to the US in the 1800s" in out From 6f76ca6676b5f0cb8257b88d70e10930eb59638d Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 10:58:43 +0300 Subject: [PATCH 02/16] remove client_query_legacy_sql from snippets --- bigquery/docs/snippets.py | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 6099f777dc83..50390e9764e5 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1255,35 +1255,6 @@ def test_extract_table_compressed(client, to_delete): to_delete.insert(0, blob) -def test_client_query_legacy_sql(client): - """Run a query with Legacy SQL explicitly set""" - # [START bigquery_query_legacy] - # from google.cloud import bigquery - # client = bigquery.Client() - - query = ( - "SELECT name FROM [bigquery-public-data:usa_names.usa_1910_2013] " - 'WHERE state = "TX" ' - "LIMIT 100" - ) - - # Set use_legacy_sql to True to use legacy SQL syntax. - job_config = bigquery.QueryJobConfig() - job_config.use_legacy_sql = True - - query_job = client.query( - query, - # Location must match that of the dataset(s) referenced in the query. - location="US", - job_config=job_config, - ) # API request - starts the query - - # Print the results. - for row in query_job: # API request - fetches results - print(row) - # [END bigquery_query_legacy] - - def test_client_query_total_rows(client, capsys): """Run a query and just check for how many rows.""" # [START bigquery_query_total_rows] From ba35acd0f95e3df66598e2ff299a9897991ffd4a Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 10:59:15 +0300 Subject: [PATCH 03/16] client_query_w_named_params --- bigquery/docs/snippets.py | 38 ----------------- .../samples/client_query_w_named_params.py | 41 +++++++++++++++++++ .../tests/test_client_query_w_named_params.py | 22 ++++++++++ 3 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 bigquery/samples/client_query_w_named_params.py create mode 100644 bigquery/samples/tests/test_client_query_w_named_params.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 50390e9764e5..4f21b87bacb2 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1319,44 +1319,6 @@ def test_manage_job(client): # [END bigquery_get_job] -def test_client_query_w_named_params(client, capsys): - """Run a query using named query parameters""" - - # [START bigquery_query_params_named] - # from google.cloud import bigquery - # client = bigquery.Client() - - query = """ - SELECT word, word_count - FROM `bigquery-public-data.samples.shakespeare` - WHERE corpus = @corpus - AND word_count >= @min_word_count - ORDER BY word_count DESC; - """ - query_params = [ - bigquery.ScalarQueryParameter("corpus", "STRING", "romeoandjuliet"), - bigquery.ScalarQueryParameter("min_word_count", "INT64", 250), - ] - job_config = bigquery.QueryJobConfig() - job_config.query_parameters = query_params - query_job = client.query( - query, - # Location must match that of the dataset(s) referenced in the query. - location="US", - job_config=job_config, - ) # API request - starts the query - - # Print the results - for row in query_job: - print("{}: \t{}".format(row.word, row.word_count)) - - assert query_job.state == "DONE" - # [END bigquery_query_params_named] - - out, _ = capsys.readouterr() - assert "the" in out - - def test_client_query_w_positional_params(client, capsys): """Run a query using query parameters""" diff --git a/bigquery/samples/client_query_w_named_params.py b/bigquery/samples/client_query_w_named_params.py new file mode 100644 index 000000000000..eba5bc221ff9 --- /dev/null +++ b/bigquery/samples/client_query_w_named_params.py @@ -0,0 +1,41 @@ +# Copyright 2019 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_w_named_params(client): + + # [START bigquery_query_params_named] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + query = """ + SELECT word, word_count + FROM `bigquery-public-data.samples.shakespeare` + WHERE corpus = @corpus + AND word_count >= @min_word_count + ORDER BY word_count DESC; + """ + job_config = bigquery.QueryJobConfig( + query_parameters=[ + bigquery.ScalarQueryParameter("corpus", "STRING", "romeoandjuliet"), + bigquery.ScalarQueryParameter("min_word_count", "INT64", 250), + ] + ) + query_job = client.query(query, job_config=job_config) # Make an API request. + + for row in query_job: + print("{}: \t{}".format(row.word, row.word_count)) + # [END bigquery_query_params_named] diff --git a/bigquery/samples/tests/test_client_query_w_named_params.py b/bigquery/samples/tests/test_client_query_w_named_params.py new file mode 100644 index 000000000000..e58e3103fe43 --- /dev/null +++ b/bigquery/samples/tests/test_client_query_w_named_params.py @@ -0,0 +1,22 @@ +# Copyright 2019 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. + +from .. import client_query_w_named_params + + +def test_client_query_w_named_params(capsys, client): + + query_job = client_query_w_named_params.client_query_w_named_params(client) + out, err = capsys.readouterr() + assert "the" in out From 01b9a3f9a3d845ca4092141d148051e040234dad Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 10:59:35 +0300 Subject: [PATCH 04/16] client_query_w_positional_params --- bigquery/docs/snippets.py | 40 ----------------- .../client_query_w_positional_params.py | 43 +++++++++++++++++++ .../test_client_query_w_positional_params.py | 24 +++++++++++ 3 files changed, 67 insertions(+), 40 deletions(-) create mode 100644 bigquery/samples/client_query_w_positional_params.py create mode 100644 bigquery/samples/tests/test_client_query_w_positional_params.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 4f21b87bacb2..479d7a062b35 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1319,46 +1319,6 @@ def test_manage_job(client): # [END bigquery_get_job] -def test_client_query_w_positional_params(client, capsys): - """Run a query using query parameters""" - - # [START bigquery_query_params_positional] - # from google.cloud import bigquery - # client = bigquery.Client() - - query = """ - SELECT word, word_count - FROM `bigquery-public-data.samples.shakespeare` - WHERE corpus = ? - AND word_count >= ? - ORDER BY word_count DESC; - """ - # Set the name to None to use positional parameters. - # Note that you cannot mix named and positional parameters. - query_params = [ - bigquery.ScalarQueryParameter(None, "STRING", "romeoandjuliet"), - bigquery.ScalarQueryParameter(None, "INT64", 250), - ] - job_config = bigquery.QueryJobConfig() - job_config.query_parameters = query_params - query_job = client.query( - query, - # Location must match that of the dataset(s) referenced in the query. - location="US", - job_config=job_config, - ) # API request - starts the query - - # Print the results - for row in query_job: - print("{}: \t{}".format(row.word, row.word_count)) - - assert query_job.state == "DONE" - # [END bigquery_query_params_positional] - - out, _ = capsys.readouterr() - assert "the" in out - - def test_client_query_w_timestamp_params(client, capsys): """Run a query using query parameters""" diff --git a/bigquery/samples/client_query_w_positional_params.py b/bigquery/samples/client_query_w_positional_params.py new file mode 100644 index 000000000000..3f7ce584bcf9 --- /dev/null +++ b/bigquery/samples/client_query_w_positional_params.py @@ -0,0 +1,43 @@ +# Copyright 2019 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_w_positional_params(client): + + # [START bigquery_query_params_positional] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + query = """ + SELECT word, word_count + FROM `bigquery-public-data.samples.shakespeare` + WHERE corpus = ? + AND word_count >= ? + ORDER BY word_count DESC; + """ + # Set the name to None to use positional parameters. + # Note that you cannot mix named and positional parameters. + job_config = bigquery.QueryJobConfig( + query_parameters=[ + bigquery.ScalarQueryParameter(None, "STRING", "romeoandjuliet"), + bigquery.ScalarQueryParameter(None, "INT64", 250), + ] + ) + query_job = client.query(query, job_config=job_config) # Make an API request. + + for row in query_job: + print("{}: \t{}".format(row.word, row.word_count)) + # [END bigquery_query_params_positional] diff --git a/bigquery/samples/tests/test_client_query_w_positional_params.py b/bigquery/samples/tests/test_client_query_w_positional_params.py new file mode 100644 index 000000000000..99c08cd7f8ce --- /dev/null +++ b/bigquery/samples/tests/test_client_query_w_positional_params.py @@ -0,0 +1,24 @@ +# Copyright 2019 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. + +from .. import client_query_w_positional_params + + +def test_client_query_w_positional_params(capsys, client): + + query_job = client_query_w_positional_params.client_query_w_positional_params( + client + ) + out, err = capsys.readouterr() + assert "the" in out From 20e19348ea877c024ce241554190dd0edd17a68f Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 10:59:55 +0300 Subject: [PATCH 05/16] client_query_w_timestamp_params --- bigquery/docs/snippets.py | 38 ----------------- .../client_query_w_timestamp_params.py | 41 +++++++++++++++++++ .../test_client_query_w_timestamp_params.py | 22 ++++++++++ 3 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 bigquery/samples/client_query_w_timestamp_params.py create mode 100644 bigquery/samples/tests/test_client_query_w_timestamp_params.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 479d7a062b35..61d9a6bbf21e 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1319,44 +1319,6 @@ def test_manage_job(client): # [END bigquery_get_job] -def test_client_query_w_timestamp_params(client, capsys): - """Run a query using query parameters""" - - # [START bigquery_query_params_timestamps] - # from google.cloud import bigquery - # client = bigquery.Client() - - import datetime - import pytz - - query = "SELECT TIMESTAMP_ADD(@ts_value, INTERVAL 1 HOUR);" - query_params = [ - bigquery.ScalarQueryParameter( - "ts_value", - "TIMESTAMP", - datetime.datetime(2016, 12, 7, 8, 0, tzinfo=pytz.UTC), - ) - ] - job_config = bigquery.QueryJobConfig() - job_config.query_parameters = query_params - query_job = client.query( - query, - # Location must match that of the dataset(s) referenced in the query. - location="US", - job_config=job_config, - ) # API request - starts the query - - # Print the results - for row in query_job: - print(row) - - assert query_job.state == "DONE" - # [END bigquery_query_params_timestamps] - - out, _ = capsys.readouterr() - assert "2016, 12, 7, 9, 0" in out - - def test_client_query_w_array_params(client, capsys): """Run a query using array query parameters""" # [START bigquery_query_params_arrays] diff --git a/bigquery/samples/client_query_w_timestamp_params.py b/bigquery/samples/client_query_w_timestamp_params.py new file mode 100644 index 000000000000..bf12fa2a6ccb --- /dev/null +++ b/bigquery/samples/client_query_w_timestamp_params.py @@ -0,0 +1,41 @@ +# Copyright 2019 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_w_timestamp_params(client): + + # [START bigquery_query_params_timestamps] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + import datetime + import pytz + + query = "SELECT TIMESTAMP_ADD(@ts_value, INTERVAL 1 HOUR);" + job_config = bigquery.QueryJobConfig( + query_parameters=[ + bigquery.ScalarQueryParameter( + "ts_value", + "TIMESTAMP", + datetime.datetime(2016, 12, 7, 8, 0, tzinfo=pytz.UTC), + ) + ] + ) + query_job = client.query(query, job_config=job_config) # Make an API request. + + for row in query_job: + print(row) + # [END bigquery_query_params_timestamps] diff --git a/bigquery/samples/tests/test_client_query_w_timestamp_params.py b/bigquery/samples/tests/test_client_query_w_timestamp_params.py new file mode 100644 index 000000000000..2f00ecc1f489 --- /dev/null +++ b/bigquery/samples/tests/test_client_query_w_timestamp_params.py @@ -0,0 +1,22 @@ +# Copyright 2019 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. + +from .. import client_query_w_timestamp_params + + +def test_client_query_w_timestamp_params(capsys, client): + + query_job = client_query_w_timestamp_params.client_query_w_timestamp_params(client) + out, err = capsys.readouterr() + assert "2016, 12, 7, 9, 0" in out From 5fe38e982f650c472dca8323a9f7f19209dbdb8a Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:00:14 +0300 Subject: [PATCH 06/16] client_query_w_array_params --- bigquery/docs/snippets.py | 39 ----------------- .../samples/client_query_w_array_params.py | 43 +++++++++++++++++++ .../tests/test_client_query_w_array_params.py | 22 ++++++++++ 3 files changed, 65 insertions(+), 39 deletions(-) create mode 100644 bigquery/samples/client_query_w_array_params.py create mode 100644 bigquery/samples/tests/test_client_query_w_array_params.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 61d9a6bbf21e..ab52db7c6047 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1319,45 +1319,6 @@ def test_manage_job(client): # [END bigquery_get_job] -def test_client_query_w_array_params(client, capsys): - """Run a query using array query parameters""" - # [START bigquery_query_params_arrays] - # from google.cloud import bigquery - # client = bigquery.Client() - - query = """ - SELECT name, sum(number) as count - FROM `bigquery-public-data.usa_names.usa_1910_2013` - WHERE gender = @gender - AND state IN UNNEST(@states) - GROUP BY name - ORDER BY count DESC - LIMIT 10; - """ - query_params = [ - bigquery.ScalarQueryParameter("gender", "STRING", "M"), - bigquery.ArrayQueryParameter("states", "STRING", ["WA", "WI", "WV", "WY"]), - ] - job_config = bigquery.QueryJobConfig() - job_config.query_parameters = query_params - query_job = client.query( - query, - # Location must match that of the dataset(s) referenced in the query. - location="US", - job_config=job_config, - ) # API request - starts the query - - # Print the results - for row in query_job: - print("{}: \t{}".format(row.name, row.count)) - - assert query_job.state == "DONE" - # [END bigquery_query_params_arrays] - - out, _ = capsys.readouterr() - assert "James" in out - - def test_client_query_w_struct_params(client, capsys): """Run a query using struct query parameters""" # [START bigquery_query_params_structs] diff --git a/bigquery/samples/client_query_w_array_params.py b/bigquery/samples/client_query_w_array_params.py new file mode 100644 index 000000000000..254173d4c540 --- /dev/null +++ b/bigquery/samples/client_query_w_array_params.py @@ -0,0 +1,43 @@ +# Copyright 2019 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_w_array_params(client): + + # [START bigquery_query_params_arrays] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + query = """ + SELECT name, sum(number) as count + FROM `bigquery-public-data.usa_names.usa_1910_2013` + WHERE gender = @gender + AND state IN UNNEST(@states) + GROUP BY name + ORDER BY count DESC + LIMIT 10; + """ + job_config = bigquery.QueryJobConfig( + query_parameters=[ + bigquery.ScalarQueryParameter("gender", "STRING", "M"), + bigquery.ArrayQueryParameter("states", "STRING", ["WA", "WI", "WV", "WY"]), + ] + ) + query_job = client.query(query, job_config=job_config) # Make an API request. + + for row in query_job: + print("{}: \t{}".format(row.name, row.count)) + # [END bigquery_query_params_arrays] diff --git a/bigquery/samples/tests/test_client_query_w_array_params.py b/bigquery/samples/tests/test_client_query_w_array_params.py new file mode 100644 index 000000000000..e3ac53205d14 --- /dev/null +++ b/bigquery/samples/tests/test_client_query_w_array_params.py @@ -0,0 +1,22 @@ +# Copyright 2019 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. + +from .. import client_query_w_array_params + + +def test_client_query_w_array_params(capsys, client): + + query_job = client_query_w_array_params.client_query_w_array_params(client) + out, err = capsys.readouterr() + assert "James" in out From 28f43f3f4dc70b16087823cdd03ec54a2ae65dcc Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:00:30 +0300 Subject: [PATCH 07/16] client_query_w_struct_params --- bigquery/docs/snippets.py | 35 ----------------- .../samples/client_query_w_struct_params.py | 38 +++++++++++++++++++ .../test_client_query_w_struct_params.py | 23 +++++++++++ 3 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 bigquery/samples/client_query_w_struct_params.py create mode 100644 bigquery/samples/tests/test_client_query_w_struct_params.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index ab52db7c6047..3b26a1d52e81 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1319,41 +1319,6 @@ def test_manage_job(client): # [END bigquery_get_job] -def test_client_query_w_struct_params(client, capsys): - """Run a query using struct query parameters""" - # [START bigquery_query_params_structs] - # from google.cloud import bigquery - # client = bigquery.Client() - - query = "SELECT @struct_value AS s;" - query_params = [ - bigquery.StructQueryParameter( - "struct_value", - bigquery.ScalarQueryParameter("x", "INT64", 1), - bigquery.ScalarQueryParameter("y", "STRING", "foo"), - ) - ] - job_config = bigquery.QueryJobConfig() - job_config.query_parameters = query_params - query_job = client.query( - query, - # Location must match that of the dataset(s) referenced in the query. - location="US", - job_config=job_config, - ) # API request - starts the query - - # Print the results - for row in query_job: - print(row.s) - - assert query_job.state == "DONE" - # [END bigquery_query_params_structs] - - out, _ = capsys.readouterr() - assert "1" in out - assert "foo" in out - - def test_query_no_cache(client): # [START bigquery_query_no_cache] # from google.cloud import bigquery diff --git a/bigquery/samples/client_query_w_struct_params.py b/bigquery/samples/client_query_w_struct_params.py new file mode 100644 index 000000000000..7c291447f0cb --- /dev/null +++ b/bigquery/samples/client_query_w_struct_params.py @@ -0,0 +1,38 @@ +# Copyright 2019 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_w_struct_params(client): + + # [START bigquery_query_params_structs] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + query = "SELECT @struct_value AS s;" + job_config = bigquery.QueryJobConfig( + query_parameters=[ + bigquery.StructQueryParameter( + "struct_value", + bigquery.ScalarQueryParameter("x", "INT64", 1), + bigquery.ScalarQueryParameter("y", "STRING", "foo"), + ) + ] + ) + query_job = client.query(query, job_config=job_config) # Make an API request. + + for row in query_job: + print(row.s) + # [END bigquery_query_params_structs] diff --git a/bigquery/samples/tests/test_client_query_w_struct_params.py b/bigquery/samples/tests/test_client_query_w_struct_params.py new file mode 100644 index 000000000000..77060a6d7947 --- /dev/null +++ b/bigquery/samples/tests/test_client_query_w_struct_params.py @@ -0,0 +1,23 @@ +# Copyright 2019 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. + +from .. import client_query_w_struct_params + + +def test_client_query_w_struct_params(capsys, client): + + query_job = client_query_w_struct_params.client_query_w_struct_params(client) + out, err = capsys.readouterr() + assert "1" in out + assert "foo" in out From 6abbc749d7e1953c0795073cc48640c1420d2d13 Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:00:44 +0300 Subject: [PATCH 08/16] query_no_cache --- bigquery/docs/snippets.py | 25 -------------- bigquery/samples/query_no_cache.py | 34 +++++++++++++++++++ bigquery/samples/tests/test_query_no_cache.py | 24 +++++++++++++ 3 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 bigquery/samples/query_no_cache.py create mode 100644 bigquery/samples/tests/test_query_no_cache.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 3b26a1d52e81..6442e7233255 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1319,31 +1319,6 @@ def test_manage_job(client): # [END bigquery_get_job] -def test_query_no_cache(client): - # [START bigquery_query_no_cache] - # from google.cloud import bigquery - # client = bigquery.Client() - - job_config = bigquery.QueryJobConfig() - job_config.use_query_cache = False - sql = """ - SELECT corpus - FROM `bigquery-public-data.samples.shakespeare` - GROUP BY corpus; - """ - query_job = client.query( - sql, - # Location must match that of the dataset(s) referenced in the query. - location="US", - job_config=job_config, - ) # API request - - # Print the results. - for row in query_job: # API request - fetches results - print(row) - # [END bigquery_query_no_cache] - - def test_query_external_gcs_temporary_table(client): # [START bigquery_query_external_gcs_temp] # from google.cloud import bigquery diff --git a/bigquery/samples/query_no_cache.py b/bigquery/samples/query_no_cache.py new file mode 100644 index 000000000000..64f705010fb7 --- /dev/null +++ b/bigquery/samples/query_no_cache.py @@ -0,0 +1,34 @@ +# Copyright 2019 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 query_no_cache(client): + + # [START bigquery_query_no_cache] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + job_config = bigquery.QueryJobConfig(use_query_cache=False) + sql = """ + SELECT corpus + FROM `bigquery-public-data.samples.shakespeare` + GROUP BY corpus; + """ + query_job = client.query(sql, job_config=job_config,) # Make an API request. + + for row in query_job: + print(row) + # [END bigquery_query_no_cache] diff --git a/bigquery/samples/tests/test_query_no_cache.py b/bigquery/samples/tests/test_query_no_cache.py new file mode 100644 index 000000000000..95a7d3d203c7 --- /dev/null +++ b/bigquery/samples/tests/test_query_no_cache.py @@ -0,0 +1,24 @@ +# Copyright 2019 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 re + +from .. import query_no_cache + + +def test_query_no_cache(capsys, client): + + query_job = query_no_cache.query_no_cache(client) + out, err = capsys.readouterr() + assert re.search(r"(Row[\w(){}:', ]+)$", out) From 3bf431af8ab6f774de33946c6ce74779a7bdbef0 Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:01:00 +0300 Subject: [PATCH 09/16] query_external_gcs_temporary_table --- bigquery/docs/snippets.py | 30 ------------- .../query_external_gcs_temporary_table.py | 44 +++++++++++++++++++ ...test_query_external_gcs_temporary_table.py | 24 ++++++++++ 3 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 bigquery/samples/query_external_gcs_temporary_table.py create mode 100644 bigquery/samples/tests/test_query_external_gcs_temporary_table.py diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 6442e7233255..bb584fa0494a 100644 --- a/bigquery/docs/snippets.py +++ b/bigquery/docs/snippets.py @@ -1319,36 +1319,6 @@ def test_manage_job(client): # [END bigquery_get_job] -def test_query_external_gcs_temporary_table(client): - # [START bigquery_query_external_gcs_temp] - # from google.cloud import bigquery - # client = bigquery.Client() - - # Configure the external data source and query job - external_config = bigquery.ExternalConfig("CSV") - external_config.source_uris = [ - "gs://cloud-samples-data/bigquery/us-states/us-states.csv" - ] - external_config.schema = [ - bigquery.SchemaField("name", "STRING"), - bigquery.SchemaField("post_abbr", "STRING"), - ] - external_config.options.skip_leading_rows = 1 # optionally skip header row - table_id = "us_states" - job_config = bigquery.QueryJobConfig() - job_config.table_definitions = {table_id: external_config} - - # Example query to find states starting with 'W' - sql = 'SELECT * FROM `{}` WHERE name LIKE "W%"'.format(table_id) - - query_job = client.query(sql, job_config=job_config) # API request - - w_states = list(query_job) # Waits for query to finish - print("There are {} states with names starting with W.".format(len(w_states))) - # [END bigquery_query_external_gcs_temp] - assert len(w_states) == 4 - - def test_query_external_gcs_permanent_table(client, to_delete): dataset_id = "query_external_gcs_{}".format(_millis()) dataset = bigquery.Dataset(client.dataset(dataset_id)) diff --git a/bigquery/samples/query_external_gcs_temporary_table.py b/bigquery/samples/query_external_gcs_temporary_table.py new file mode 100644 index 000000000000..3ef44bd32db1 --- /dev/null +++ b/bigquery/samples/query_external_gcs_temporary_table.py @@ -0,0 +1,44 @@ +# Copyright 2019 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 query_external_gcs_temporary_table(client): + + # [START bigquery_query_external_gcs_temp] + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + # Configure the external data source and query job. + external_config = bigquery.ExternalConfig("CSV") + external_config.source_uris = [ + "gs://cloud-samples-data/bigquery/us-states/us-states.csv" + ] + external_config.schema = [ + bigquery.SchemaField("name", "STRING"), + bigquery.SchemaField("post_abbr", "STRING"), + ] + external_config.options.skip_leading_rows = 1 + table_id = "us_states" + job_config = bigquery.QueryJobConfig(table_definitions={table_id: external_config}) + + # Example query to find states starting with 'W'. + sql = 'SELECT * FROM `{}` WHERE name LIKE "W%"'.format(table_id) + + query_job = client.query(sql, job_config=job_config) # Make an API request. + + w_states = list(query_job) # Wait for the job to complete. + print("There are {} states with names starting with W.".format(len(w_states))) + # [END bigquery_query_external_gcs_temp] diff --git a/bigquery/samples/tests/test_query_external_gcs_temporary_table.py b/bigquery/samples/tests/test_query_external_gcs_temporary_table.py new file mode 100644 index 000000000000..cbe9954b0dc7 --- /dev/null +++ b/bigquery/samples/tests/test_query_external_gcs_temporary_table.py @@ -0,0 +1,24 @@ +# Copyright 2019 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. + +from .. import query_external_gcs_temporary_table + + +def test_query_external_gcs_temporary_table(capsys, client): + + query_job = query_external_gcs_temporary_table.query_external_gcs_temporary_table( + client + ) + out, err = capsys.readouterr() + assert "There are 4 states with names starting with W." in out From 7602e9235ac1d2126908d65b25d21acf3ae1ae53 Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:02:29 +0300 Subject: [PATCH 10/16] unify test_update_table_require_partition_filter --- .../tests/test_update_table_require_partition_filter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bigquery/samples/tests/test_update_table_require_partition_filter.py b/bigquery/samples/tests/test_update_table_require_partition_filter.py index 1cbd2b2279b2..7ce6d64c780a 100644 --- a/bigquery/samples/tests/test_update_table_require_partition_filter.py +++ b/bigquery/samples/tests/test_update_table_require_partition_filter.py @@ -13,13 +13,15 @@ # limitations under the License. from google.cloud import bigquery + from .. import update_table_require_partition_filter def test_update_table_require_partition_filter(capsys, client, random_table_id): + # Make a partitioned table. schema = [bigquery.SchemaField("transaction_timestamp", "TIMESTAMP")] - table = bigquery.Table(random_table_id, schema) + table = bigquery.Table(random_table_id, schema=schema) table.time_partitioning = bigquery.TimePartitioning(field="transaction_timestamp") table = client.create_table(table) From d892866ac94f28a77d7421fd4e3df6c646d3b0a0 Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:02:59 +0300 Subject: [PATCH 11/16] Update test_copy_table_multiple_source.py --- .../tests/test_copy_table_multiple_source.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bigquery/samples/tests/test_copy_table_multiple_source.py b/bigquery/samples/tests/test_copy_table_multiple_source.py index 755fa2ccb5e9..16c1de89627c 100644 --- a/bigquery/samples/tests/test_copy_table_multiple_source.py +++ b/bigquery/samples/tests/test_copy_table_multiple_source.py @@ -20,19 +20,18 @@ def test_copy_table_multiple_source(capsys, client, random_table_id, random_dataset_id): - schema = [ - bigquery.SchemaField("name", "STRING"), - bigquery.SchemaField("post_abbr", "STRING"), - ] - dataset = bigquery.Dataset(random_dataset_id) dataset.location = "US" dataset = client.create_dataset(dataset) table_data = {"table1": b"Washington,WA", "table2": b"California,CA"} for table_id, data in table_data.items(): table_ref = dataset.table(table_id) - job_config = bigquery.LoadJobConfig() - job_config.schema = schema + job_config = bigquery.LoadJobConfig( + schema=[ + bigquery.SchemaField("name", "STRING"), + bigquery.SchemaField("post_abbr", "STRING"), + ] + ) body = six.BytesIO(data) client.load_table_from_file( body, table_ref, location="US", job_config=job_config From b734bab33eb60a4710a12610506982c5d7b51b9e Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:03:06 +0300 Subject: [PATCH 12/16] Update client_query_add_column.py --- bigquery/samples/client_query_add_column.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bigquery/samples/client_query_add_column.py b/bigquery/samples/client_query_add_column.py index 1cde370a35ed..c35548d2a361 100644 --- a/bigquery/samples/client_query_add_column.py +++ b/bigquery/samples/client_query_add_column.py @@ -30,11 +30,11 @@ def client_query_add_column(client, table_id): # Configures the query to append the results to a destination table, # allowing field addition. - job_config = bigquery.QueryJobConfig(destination=table_id) - job_config.schema_update_options = [ - bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION - ] - job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND + job_config = bigquery.QueryJobConfig( + destination=table_id, + schema_update_options=[bigquery.SchemaUpdateOption.ALLOW_FIELD_ADDITION], + write_disposition=bigquery.WriteDisposition.WRITE_APPEND, + ) # Start the query, passing in the extra configuration. query_job = client.query( From 51702996c97ac7dc1557b4c421467e76d3c65502 Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:03:13 +0300 Subject: [PATCH 13/16] Update client_query_relax_column.py --- bigquery/samples/client_query_relax_column.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bigquery/samples/client_query_relax_column.py b/bigquery/samples/client_query_relax_column.py index d8e5743c1e33..8ec117e186fc 100644 --- a/bigquery/samples/client_query_relax_column.py +++ b/bigquery/samples/client_query_relax_column.py @@ -33,11 +33,11 @@ def client_query_relax_column(client, table_id): # Configures the query to append the results to a destination table, # allowing field relaxation. - job_config = bigquery.QueryJobConfig(destination=table_id) - job_config.schema_update_options = [ - bigquery.SchemaUpdateOption.ALLOW_FIELD_RELAXATION - ] - job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND + job_config = bigquery.QueryJobConfig( + destination=table_id, + schema_update_options=[bigquery.SchemaUpdateOption.ALLOW_FIELD_RELAXATION], + write_disposition=bigquery.WriteDisposition.WRITE_APPEND, + ) # Start the query, passing in the extra configuration. query_job = client.query( From a359f9dab4c13cad63d44a8ba0aee2591bf847cf Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Thu, 5 Dec 2019 11:17:46 +0300 Subject: [PATCH 14/16] flake8 correction --- bigquery/samples/tests/test_client_query_w_array_params.py | 2 +- bigquery/samples/tests/test_client_query_w_named_params.py | 2 +- .../samples/tests/test_client_query_w_positional_params.py | 4 +--- bigquery/samples/tests/test_client_query_w_struct_params.py | 2 +- .../samples/tests/test_client_query_w_timestamp_params.py | 2 +- .../samples/tests/test_query_external_gcs_temporary_table.py | 4 +--- bigquery/samples/tests/test_query_no_cache.py | 2 +- 7 files changed, 7 insertions(+), 11 deletions(-) diff --git a/bigquery/samples/tests/test_client_query_w_array_params.py b/bigquery/samples/tests/test_client_query_w_array_params.py index e3ac53205d14..8603e9b8fe3d 100644 --- a/bigquery/samples/tests/test_client_query_w_array_params.py +++ b/bigquery/samples/tests/test_client_query_w_array_params.py @@ -17,6 +17,6 @@ def test_client_query_w_array_params(capsys, client): - query_job = client_query_w_array_params.client_query_w_array_params(client) + client_query_w_array_params.client_query_w_array_params(client) out, err = capsys.readouterr() assert "James" in out diff --git a/bigquery/samples/tests/test_client_query_w_named_params.py b/bigquery/samples/tests/test_client_query_w_named_params.py index e58e3103fe43..ae4a2fc27db3 100644 --- a/bigquery/samples/tests/test_client_query_w_named_params.py +++ b/bigquery/samples/tests/test_client_query_w_named_params.py @@ -17,6 +17,6 @@ def test_client_query_w_named_params(capsys, client): - query_job = client_query_w_named_params.client_query_w_named_params(client) + client_query_w_named_params.client_query_w_named_params(client) out, err = capsys.readouterr() assert "the" in out diff --git a/bigquery/samples/tests/test_client_query_w_positional_params.py b/bigquery/samples/tests/test_client_query_w_positional_params.py index 99c08cd7f8ce..37c15b67b120 100644 --- a/bigquery/samples/tests/test_client_query_w_positional_params.py +++ b/bigquery/samples/tests/test_client_query_w_positional_params.py @@ -17,8 +17,6 @@ def test_client_query_w_positional_params(capsys, client): - query_job = client_query_w_positional_params.client_query_w_positional_params( - client - ) + client_query_w_positional_params.client_query_w_positional_params(client) out, err = capsys.readouterr() assert "the" in out diff --git a/bigquery/samples/tests/test_client_query_w_struct_params.py b/bigquery/samples/tests/test_client_query_w_struct_params.py index 77060a6d7947..9d0c4282946b 100644 --- a/bigquery/samples/tests/test_client_query_w_struct_params.py +++ b/bigquery/samples/tests/test_client_query_w_struct_params.py @@ -17,7 +17,7 @@ def test_client_query_w_struct_params(capsys, client): - query_job = client_query_w_struct_params.client_query_w_struct_params(client) + client_query_w_struct_params.client_query_w_struct_params(client) out, err = capsys.readouterr() assert "1" in out assert "foo" in out diff --git a/bigquery/samples/tests/test_client_query_w_timestamp_params.py b/bigquery/samples/tests/test_client_query_w_timestamp_params.py index 2f00ecc1f489..45f7b7518454 100644 --- a/bigquery/samples/tests/test_client_query_w_timestamp_params.py +++ b/bigquery/samples/tests/test_client_query_w_timestamp_params.py @@ -17,6 +17,6 @@ def test_client_query_w_timestamp_params(capsys, client): - query_job = client_query_w_timestamp_params.client_query_w_timestamp_params(client) + client_query_w_timestamp_params.client_query_w_timestamp_params(client) out, err = capsys.readouterr() assert "2016, 12, 7, 9, 0" in out diff --git a/bigquery/samples/tests/test_query_external_gcs_temporary_table.py b/bigquery/samples/tests/test_query_external_gcs_temporary_table.py index cbe9954b0dc7..ea5b5d4dfcda 100644 --- a/bigquery/samples/tests/test_query_external_gcs_temporary_table.py +++ b/bigquery/samples/tests/test_query_external_gcs_temporary_table.py @@ -17,8 +17,6 @@ def test_query_external_gcs_temporary_table(capsys, client): - query_job = query_external_gcs_temporary_table.query_external_gcs_temporary_table( - client - ) + query_external_gcs_temporary_table.query_external_gcs_temporary_table(client) out, err = capsys.readouterr() assert "There are 4 states with names starting with W." in out diff --git a/bigquery/samples/tests/test_query_no_cache.py b/bigquery/samples/tests/test_query_no_cache.py index 95a7d3d203c7..68f0774d935f 100644 --- a/bigquery/samples/tests/test_query_no_cache.py +++ b/bigquery/samples/tests/test_query_no_cache.py @@ -19,6 +19,6 @@ def test_query_no_cache(capsys, client): - query_job = query_no_cache.query_no_cache(client) + query_no_cache.query_no_cache(client) out, err = capsys.readouterr() assert re.search(r"(Row[\w(){}:', ]+)$", out) From 9e8fd3581e2a19807a7c04a1e24e09fd67348950 Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Fri, 13 Dec 2019 18:21:19 +0300 Subject: [PATCH 15/16] fix queries.rst file --- bigquery/docs/usage/queries.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigquery/docs/usage/queries.rst b/bigquery/docs/usage/queries.rst index 9210e04bc6a2..fc57e54de9df 100644 --- a/bigquery/docs/usage/queries.rst +++ b/bigquery/docs/usage/queries.rst @@ -43,7 +43,7 @@ Run a query using a named query parameter See BigQuery documentation for more information on `parameterized queries `_. -.. literalinclude:: ../snippets.py +.. literalinclude:: ../samples/client_query_w_named_params.py :language: python :dedent: 4 :start-after: [START bigquery_query_params_named] From 3819ec07441eec86578a77c0caa237a11380a42c Mon Sep 17 00:00:00 2001 From: Leonid Emar-Kar Date: Tue, 24 Dec 2019 13:04:40 +0300 Subject: [PATCH 16/16] import reformat + comma deletion --- bigquery/samples/client_query_partitioned_table.py | 4 ++-- bigquery/samples/client_query_w_timestamp_params.py | 6 +++--- bigquery/samples/query_no_cache.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bigquery/samples/client_query_partitioned_table.py b/bigquery/samples/client_query_partitioned_table.py index b9b978eb901f..71ec3a0e7086 100644 --- a/bigquery/samples/client_query_partitioned_table.py +++ b/bigquery/samples/client_query_partitioned_table.py @@ -16,10 +16,10 @@ def client_query_partitioned_table(client, table_id): # [START bigquery_query_partitioned_table] - from google.cloud import bigquery - import datetime + from google.cloud import bigquery + # TODO(developer): Construct a BigQuery client object. # client = bigquery.Client() diff --git a/bigquery/samples/client_query_w_timestamp_params.py b/bigquery/samples/client_query_w_timestamp_params.py index bf12fa2a6ccb..cc334f7e9625 100644 --- a/bigquery/samples/client_query_w_timestamp_params.py +++ b/bigquery/samples/client_query_w_timestamp_params.py @@ -16,14 +16,14 @@ def client_query_w_timestamp_params(client): # [START bigquery_query_params_timestamps] + import datetime + + import pytz from google.cloud import bigquery # TODO(developer): Construct a BigQuery client object. # client = bigquery.Client() - import datetime - import pytz - query = "SELECT TIMESTAMP_ADD(@ts_value, INTERVAL 1 HOUR);" job_config = bigquery.QueryJobConfig( query_parameters=[ diff --git a/bigquery/samples/query_no_cache.py b/bigquery/samples/query_no_cache.py index 64f705010fb7..3d542a96b7be 100644 --- a/bigquery/samples/query_no_cache.py +++ b/bigquery/samples/query_no_cache.py @@ -27,7 +27,7 @@ def query_no_cache(client): FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus; """ - query_job = client.query(sql, job_config=job_config,) # Make an API request. + query_job = client.query(sql, job_config=job_config) # Make an API request. for row in query_job: print(row)