|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def test_query_standard_sql(): |
| 17 | + # [START bigquery_bigframes_query] |
| 18 | + import bigframes.pandas as bpd |
| 19 | + |
| 20 | + # Set partial ordering mode as the default configuration for BigQuery DataFrames. |
| 21 | + bpd.options.bigquery.ordering_mode = "partial" |
| 22 | + |
| 23 | + sql = """ |
| 24 | + SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current` |
| 25 | + WHERE state = 'TX' |
| 26 | + LIMIT 100 |
| 27 | + """ |
| 28 | + |
| 29 | + # Run a query alongside existing SQL. The project will be determined from default credentials. |
| 30 | + df = bpd.read_gbq(sql) |
| 31 | + |
| 32 | + # Run a query after explicitly specifying a project. |
| 33 | + bpd.options.bigquery.project = "your-project-id" |
| 34 | + df = bpd.read_gbq(sql) |
| 35 | + # [END bigquery_bigframes_query] |
| 36 | + assert df is not None |
| 37 | + |
| 38 | + |
| 39 | +def test_query_legacy_sql(): |
| 40 | + # [START bigquery_bigframes_query_legacy] |
| 41 | + import bigframes.pandas as bpd |
| 42 | + |
| 43 | + # Set partial ordering mode as the default configuration for BigQuery DataFrames. |
| 44 | + bpd.options.bigquery.ordering_mode = "partial" |
| 45 | + |
| 46 | + sql = """ |
| 47 | + SELECT name FROM [bigquery-public-data:usa_names.usa_1910_current] |
| 48 | + WHERE state = 'TX' |
| 49 | + LIMIT 100 |
| 50 | + """ |
| 51 | + |
| 52 | + # Run a query using legacy SQL syntax. |
| 53 | + query_config = {"query": {"useLegacySql": True}} |
| 54 | + df = bpd.read_gbq(sql, configuration=query_config) |
| 55 | + # [END bigquery_bigframes_query_legacy] |
| 56 | + assert df is not None |
| 57 | + |
| 58 | + |
| 59 | +def test_query_bqstorage(): |
| 60 | + # [START bigquery_bigframes_query_bqstorage] |
| 61 | + import bigframes.pandas as bpd |
| 62 | + |
| 63 | + # Set partial ordering mode as the default configuration for BigQuery DataFrames. |
| 64 | + bpd.options.bigquery.ordering_mode = "partial" |
| 65 | + |
| 66 | + sql = """ |
| 67 | + SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current` |
| 68 | + WHERE state = 'TX' |
| 69 | + LIMIT 100 |
| 70 | + """ |
| 71 | + |
| 72 | + # Read query results into a server-side DataFrame without downloading data. |
| 73 | + df = bpd.read_gbq(sql) |
| 74 | + |
| 75 | + # When downloading results to an in-memory pandas DataFrame, bigquery-dataframes |
| 76 | + # automatically uses the BigQuery Storage API if installed. |
| 77 | + pandas_df = df.to_pandas() |
| 78 | + # [END bigquery_bigframes_query_bqstorage] |
| 79 | + assert pandas_df is not None |
| 80 | + |
| 81 | + |
| 82 | +def test_query_parameters(): |
| 83 | + # [START bigquery_bigframes_query_parameters] |
| 84 | + import bigframes.pandas as bpd |
| 85 | + |
| 86 | + # Set partial ordering mode as the default configuration for BigQuery DataFrames. |
| 87 | + bpd.options.bigquery.ordering_mode = "partial" |
| 88 | + |
| 89 | + sql = """ |
| 90 | + SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current` |
| 91 | + WHERE state = @state |
| 92 | + LIMIT 100 |
| 93 | + """ |
| 94 | + |
| 95 | + query_config = { |
| 96 | + "query": { |
| 97 | + "parameterMode": "NAMED", |
| 98 | + "queryParameters": [ |
| 99 | + { |
| 100 | + "name": "state", |
| 101 | + "parameterType": {"type": "STRING"}, |
| 102 | + "parameterValue": {"value": "TX"}, |
| 103 | + } |
| 104 | + ], |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + df = bpd.read_gbq(sql, configuration=query_config) |
| 109 | + # [END bigquery_bigframes_query_parameters] |
| 110 | + assert df is not None |
| 111 | + |
| 112 | + |
| 113 | +def test_upload_from_dataframe(): |
| 114 | + # [START bigquery_bigframes_upload_from_dataframe] |
| 115 | + import pandas as pd |
| 116 | + |
| 117 | + import bigframes.pandas as bpd |
| 118 | + |
| 119 | + # Set partial ordering mode as the default configuration for BigQuery DataFrames. |
| 120 | + bpd.options.bigquery.ordering_mode = "partial" |
| 121 | + |
| 122 | + # Create a local pandas DataFrame. |
| 123 | + df = pd.DataFrame( |
| 124 | + { |
| 125 | + "my_string": ["a", "b", "c"], |
| 126 | + "my_int64": [1, 2, 3], |
| 127 | + "my_float64": [4.0, 5.0, 6.0], |
| 128 | + } |
| 129 | + ) |
| 130 | + |
| 131 | + # Convert the local pandas DataFrame to a BigQuery DataFrame. |
| 132 | + bq_df = bpd.read_pandas(df) |
| 133 | + |
| 134 | + # Write the DataFrame to a BigQuery table. |
| 135 | + table_id = "your-project.your_dataset.your_table_name" |
| 136 | + bq_df.to_gbq(table_id, if_exists="replace") |
| 137 | + # [END bigquery_bigframes_upload_from_dataframe] |
| 138 | + assert bq_df is not None |
0 commit comments