-
Notifications
You must be signed in to change notification settings - Fork 1.6k
docs(samples): Update BigQuery Storage Arrow samples batching logic #14961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexZMLyu
wants to merge
15
commits into
googleapis:main
Choose a base branch
from
AlexZMLyu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−20
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a975186
Fix: Update BigQuery Storage Arrow samples batching logic
AlexZMLyu 186d6a6
Chore: Apply manual formatting to Arrow samples
AlexZMLyu e9a7007
Chore: Apply manual Black-style formatting
AlexZMLyu 5a91e4e
Chore: Manual formatting adjustments
AlexZMLyu 84d0fc1
Refactor: Extract AppendRowsRequest creation to helper
AlexZMLyu eb97b8f
Fix AttributeError in append_rows_with_arrow.py
AlexZMLyu 593ac97
Update append_rows_with_arrow.py
AlexZMLyu 1eb8266
Update append_rows_with_arrow.py
AlexZMLyu 2239db8
Fix: Update verify_result in pyarrow sample
AlexZMLyu b510cec
Fix: Update PyArrow serialization in append_rows_with_arrow.py
AlexZMLyu f581b33
feat: Measure request generation time in pyarrow sample
AlexZMLyu 610f38e
Fix: Improve PyArrow batching and serialization in BigQuery Storage s…
AlexZMLyu 501e993
Refactor: Use generator for request creation and block on send
AlexZMLyu ec49ec5
samples: reformat append_rows_with_arrow.py
AlexZMLyu 2464fd3
enhance unit test for write request generation
AlexZMLyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/google-cloud-bigquery-storage/samples/pyarrow/test_generate_write_requests.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Copyright 2025 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 | ||
| # | ||
| # http://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 time | ||
|
|
||
| import pyarrow as pa | ||
| import pytest | ||
|
|
||
| from . import append_rows_with_arrow | ||
|
|
||
|
|
||
| def create_table_with_batches(num_batches, rows_per_batch): | ||
| # Generate a small table to get a valid batch | ||
| small_table = append_rows_with_arrow.generate_pyarrow_table(rows_per_batch) | ||
| # Ensure we get exactly one batch for the small table | ||
| batches = small_table.to_batches() | ||
| assert len(batches) == 1 | ||
| batch = batches[0] | ||
|
|
||
| # Replicate the batch | ||
| all_batches = [batch] * num_batches | ||
| return pa.Table.from_batches(all_batches) | ||
|
|
||
|
|
||
| # Test generate_write_requests with different numbers of batches in the input table. | ||
| # The total rows in the generated table is constantly 1000000. | ||
| @pytest.mark.parametrize( | ||
| "num_batches, rows_per_batch", | ||
| [ | ||
| (1, 1000000), | ||
| (10, 100000), | ||
| (100, 10000), | ||
| (1000, 1000), | ||
| (10000, 100), | ||
| (100000, 10), | ||
| (1000000, 1), | ||
| ], | ||
| ) | ||
| def test_generate_write_requests_varying_batches(num_batches, rows_per_batch): | ||
| """Test generate_write_requests with different numbers of batches in the input table.""" | ||
| # Create a table that returns `num_batches` when to_batches() is called. | ||
| table = create_table_with_batches(num_batches, rows_per_batch) | ||
|
|
||
| # Verify our setup is correct | ||
| assert len(table.to_batches()) == num_batches | ||
|
|
||
| # Generate requests | ||
| start_time = time.perf_counter() | ||
| requests = list(append_rows_with_arrow.generate_write_requests(table)) | ||
| end_time = time.perf_counter() | ||
| print( | ||
| f"\nTime used to generate requests for {num_batches} batches: {end_time - start_time:.4f} seconds" | ||
| ) | ||
|
|
||
| # We expect the requests to be aggregated until 7MB. | ||
| # Since the row number is constant, the number of requests should be deterministic. | ||
| assert len(requests) == 26 | ||
|
|
||
| # Verify total rows in requests matches total rows in table | ||
| total_rows_processed = 0 | ||
| for request in requests: | ||
| # Deserialize the batch from the request to count rows | ||
| serialized_batch = request.arrow_rows.rows.serialized_record_batch | ||
| # We need a schema to read the batch. The schema is PYARROW_SCHEMA. | ||
| batch = pa.ipc.read_record_batch( | ||
| serialized_batch, append_rows_with_arrow.PYARROW_SCHEMA | ||
| ) | ||
| total_rows_processed += batch.num_rows | ||
|
|
||
| expected_rows = num_batches * rows_per_batch | ||
| assert total_rows_processed == expected_rows |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.