diff --git a/bigquery/docs/snippets.py b/bigquery/docs/snippets.py index 2d950936a5a6..bb584fa0494a 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 " @@ -1327,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] @@ -1420,251 +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""" - - # [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""" - - # [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] - # 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] - # 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 - # 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 - # 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/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] 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_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( diff --git a/bigquery/samples/client_query_partitioned_table.py b/bigquery/samples/client_query_partitioned_table.py new file mode 100644 index 000000000000..71ec3a0e7086 --- /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] + import datetime + + 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 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/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( 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/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/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/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/client_query_w_timestamp_params.py b/bigquery/samples/client_query_w_timestamp_params.py new file mode 100644 index 000000000000..cc334f7e9625 --- /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] + import datetime + + import pytz + from google.cloud import bigquery + + # TODO(developer): Construct a BigQuery client object. + # client = bigquery.Client() + + 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/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/query_no_cache.py b/bigquery/samples/query_no_cache.py new file mode 100644 index 000000000000..3d542a96b7be --- /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_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 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..8603e9b8fe3d --- /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): + + 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 new file mode 100644 index 000000000000..ae4a2fc27db3 --- /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): + + 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 new file mode 100644 index 000000000000..37c15b67b120 --- /dev/null +++ b/bigquery/samples/tests/test_client_query_w_positional_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_positional_params + + +def test_client_query_w_positional_params(capsys, 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 new file mode 100644 index 000000000000..9d0c4282946b --- /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): + + 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 new file mode 100644 index 000000000000..45f7b7518454 --- /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): + + 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_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 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..ea5b5d4dfcda --- /dev/null +++ b/bigquery/samples/tests/test_query_external_gcs_temporary_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 query_external_gcs_temporary_table + + +def test_query_external_gcs_temporary_table(capsys, 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 new file mode 100644 index 000000000000..68f0774d935f --- /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_no_cache.query_no_cache(client) + out, err = capsys.readouterr() + assert re.search(r"(Row[\w(){}:', ]+)$", out) 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)