docs: add bigframes_queries sample snippets#14429
Conversation
|
Here is the summary of changes. You are about to add 5 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces code examples and tests for BigQuery DataFrames, covering standard SQL queries, legacy SQL queries, storage API usage, parameterized queries, and uploading from a pandas DataFrame. The feedback recommends parameterizing placeholder values like project_id and table_id to make the examples flexible and enable their tests to run instead of being skipped. Additionally, it is recommended to remove the legacy SQL example and its test entirely, as legacy SQL is not supported by BigQuery DataFrames.
| # limitations under the License. | ||
|
|
||
|
|
||
| def query_standard_sql(): |
There was a problem hiding this comment.
To make this snippet testable and avoid global side effects, parameterize the project_id in the function signature with a default value of "your-project-id". This allows tests to pass a valid project ID while keeping the placeholder for documentation.
| def query_standard_sql(): | |
| def query_standard_sql(project_id: str = "your-project-id"): |
There was a problem hiding this comment.
This test case is for adding samples in welcome page. No past in variables should be allowed.
There was a problem hiding this comment.
Gemini is correct in this case.
| def query_legacy_sql(): | ||
| # [START bigquery_bigframes_query_legacy] | ||
| import bigframes.pandas as bpd | ||
|
|
||
| # Set partial ordering mode as the default configuration for BigQuery DataFrames. | ||
| bpd.options.bigquery.ordering_mode = "partial" | ||
|
|
||
| sql = """ | ||
| SELECT name FROM [bigquery-public-data:usa_names.usa_1910_current] | ||
| WHERE state = 'TX' | ||
| LIMIT 100 | ||
| """ | ||
|
|
||
| # Run a query using legacy SQL syntax. | ||
| query_config = {"query": {"useLegacySql": True}} | ||
| df = bpd.read_gbq(sql, configuration=query_config) | ||
| # [END bigquery_bigframes_query_legacy] | ||
| return df |
There was a problem hiding this comment.
There was a problem hiding this comment.
I update the error message to avoid confusion
| return df | ||
|
|
||
|
|
||
| def upload_from_dataframe(): |
There was a problem hiding this comment.
Parameterize the table_id in the function signature with a default value of "your-project.your_dataset.your_table_name". This makes the snippet more flexible and easier to test.
| def upload_from_dataframe(): | |
| def upload_from_dataframe(table_id: str = "your-project.your_dataset.your_table_name"): |
There was a problem hiding this comment.
This test case is for adding samples in welcome page. No past in variables should be allowed.
| bq_df = bpd.read_pandas(df) | ||
|
|
||
| # Write the DataFrame to a BigQuery table. | ||
| table_id = "your-project.your_dataset.your_table_name" |
There was a problem hiding this comment.
I prefer the original line better
| @pytest.mark.skip(reason="Placeholder project ID 'your-project-id' (b/522845525)") | ||
| def test_query_standard_sql(): | ||
| df = bigframes_queries.query_standard_sql() | ||
| assert df is not None |
There was a problem hiding this comment.
Now that query_standard_sql is parameterized, we can remove the @pytest.mark.skip decorator and run the test using the project_id fixture.
| @pytest.mark.skip(reason="Placeholder project ID 'your-project-id' (b/522845525)") | |
| def test_query_standard_sql(): | |
| df = bigframes_queries.query_standard_sql() | |
| assert df is not None | |
| def test_query_standard_sql(project_id): | |
| df = bigframes_queries.query_standard_sql(project_id=project_id) | |
| assert df is not None |
There was a problem hiding this comment.
This test case is for adding samples in welcome page. No past in variables should be allowed.
| @pytest.mark.skip(reason="Legacy SQL syntax not supported by BigQuery DataFrames (b/522845525)") | ||
| def test_query_legacy_sql(): | ||
| df = bigframes_queries.query_legacy_sql() | ||
| assert df is not None |
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
|
cc @sycai for review |
| # limitations under the License. | ||
|
|
||
|
|
||
| def query_standard_sql(): |
There was a problem hiding this comment.
Gemini is correct in this case.
|
|
||
|
|
||
| def query_standard_sql(): | ||
| # [START bigquery_bigframes_query] |
There was a problem hiding this comment.
The Cloud samples rubric has the region tags outside of the function and move the imports to outside of the function. Refer to https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md
| return df | ||
|
|
||
|
|
||
| def upload_from_dataframe(): |
| @pytest.mark.skip( | ||
| reason="Requires a writable destination table so pytest skips execution, but snippet is required for welcome page documentation." | ||
| ) |
There was a problem hiding this comment.
Don't skip this. Instead, parametrize the sample and pass in a writable destination.
| @pytest.mark.skip( | ||
| reason="Legacy SQL syntax is not supported for execution by BigQuery DataFrames, but snippet is required for welcome page documentation." | ||
| ) | ||
| def test_query_legacy_sql(): | ||
| df = bigframes_queries.query_legacy_sql() | ||
| assert df is not None |
There was a problem hiding this comment.
Agreed with Gemini. It's better not to have a sample than to have one that doesn't work.
| @pytest.mark.skip( | ||
| reason="Placeholder project ID 'your-project-id' cannot be executed by pytest, but snippet is required for welcome page documentation." | ||
| ) |
Adds sample code snippets with region tags (# [START ...] / # [END ...]) for BigQuery DataFrames documentation welcome page (python-libraries.md).
Note here, we need to place all imports inside testcase functions for the following reasons:
Fixes #<522845525> 🦕