GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — fix: add timeout parameter to to_dataframe and to_arrow met… by chalmerlowe · Pull Request #2354 · googleapis/python-bigquery
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Conversation
|
Summary of ChangesHello @chalmerlowe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the BigQuery client library by integrating a Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This pull request introduces a timeout parameter to the to_dataframe() and to_arrow() methods, which is a valuable addition to prevent indefinite hangs during data download. The core timeout logic in _download_table_bqstorage is well-implemented, correctly using manual thread pool management to handle timeouts gracefully.
I've identified a critical issue where the method signatures for _EmptyRowIterator in google/cloud/bigquery/table.py have not been updated to include the timeout parameter. This will cause a TypeError for empty result sets and needs to be addressed. I've left a detailed comment on this.
Additionally, I noticed that QueryJob.to_dataframe in google/cloud/bigquery/job/query.py is missing the timeout parameter, which is inconsistent with the other updated methods. Please consider adding it for API consistency.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…ntiate new _EmptyRowIterator for each test in test_methods_w_timeout to prevent ValueError\n- Remove total_rows property from _EmptyRowIterator to fix AssertionError\n- Remove redundant __iter__ method in _EmptyRowIterator to fix lint error
There was an error while loading. Please reload this page.
| # Call result() on any finished threads to raise any | ||
| # exceptions encountered. | ||
| future.result() | ||
| # Manually manage the pool to control shutdown behavior on timeout. |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You may wonder why we switched away from using a contextmanager here.
Manual Pool Creation: Instead of the with statement, the code now creates the
ThreadPoolExecutor directly:
pool = concurrent.futures.ThreadPoolExecutor(max_workers=max(1, total_streams))
Conditional Shutdown: The finally block now uses a wait_on_shutdown boolean flag:
pool.shutdown(wait=wait_on_shutdown)
This flag is set to False only when a TimeoutError is raised.
Timeout Handling: Inside the while not_done: loop, there's new logic to check if the
elapsed time has exceeded the specified timeout. If it has, it raises a
concurrent.futures.TimeoutError and sets wait_on_shutdown to False.
The standard context manager for ThreadPoolExecutor always waits for all futures to complete
upon exiting the block. This behavior is not desirable when a timeout is implemented. If the
download times out, we want to stop waiting for the worker threads immediately and not block
until they all finish.
By manually managing the pool and using the wait_on_shutdown flag, we can tell the
pool.shutdown() method not to wait for the threads to complete if a timeout has occurred.
This allows the TimeoutError to be propagated up quickly, rather than being stuck waiting for
threads that are potentially hanging.
So, the change was necessary to ensure the timeout parameter works effectively and the
function doesn't hang unnecessarily when the timeout duration is exceeded. The core logic of
submitting tasks to the pool and retrieving results from the queue remains very similar, but
the shutdown process is now more nuanced to handle timeouts.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| pages = () | ||
| total_rows = 0 | ||
|
|
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
A few words on why these were deleted/modified.
Summary of Changes to _EmptyRowIterator
In the _EmptyRowIterator class, the class-level attributes pages = () and total_rows = 0 were replaced with new handling.
Reasoning:
These class-level attributes were causing issues with how instances of _EmptyRowIterator were behaving, particularly in test scenarios.
total_rows = 0: While seemingly correct, having this at the class level interfered with the instance-level _total_rows attribute inherited from the RowIterator parent class. The parent class expects to manage _total_rows at the instance level. Removing the class-level attribute allows the instance-level attribute to be a single source of truth, initialized to 0 in the __init__ method, and prevents potential conflicts or unexpected behavior.
pages = (): This class-level attribute was not used and added unnecessary clutter. The _EmptyRowIterator is designed to represent an empty result set, so it never has any pages. The __iter__ method already correctly returns an empty iterator (iter(())). Additionally, to ensure methods like to_arrow_iterable function correctly without making network requests, a pages property was added to return an empty iterator.
Replacement and Improvement:
total_rows: The class-level total_rows = 0 was removed. Instead, self._total_rows is now consistently initialized to 0 within the __init__ method of _EmptyRowIterator. This aligns with the parent class's instance-level attribute management and ensures each instance has its own _total_rows set to 0, reflecting an empty iterator.
pages: The class-level pages = () was removed. To provide the necessary empty iterable for methods like to_arrow_iterable, a pages property was added:
@property def pages(self): return iter(())
This approach is cleaner as it explicitly provides an empty iterator when the pages attribute is accessed, making the class's intent clearer and ensuring compatibility with methods expecting an iterable of pages.
These changes make the _EmptyRowIterator more robust and predictable by adhering to the instance attribute patterns of its parent class and explicitly defining an empty pages iterable.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
We worked up a script to reproduce/simulate the customer's issue. After updating the code, when we run the repro script, we get the following results depending on whether we include an argument for the new |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. I'm not very knowledgeable about bigquery in particular, but the changes from the status quo look like they do what's intended.
I really appreciate your detailed explanations of the various changes. Those made the review easier.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
**Description** This PR fixes a crash when handling `_InactiveRpcError` during retry logic and ensures proper `timeout` propagation in `RowIterator.to_dataframe`. **Fixes** **Retry Logic Crash**: Addressed an issue in `google/cloud/bigquery/retry.py` where `_should_retry` would raise a `TypeError` when inspecting unstructured `gRPC` errors (like `_InactiveRpcError`). The fix adds robust error inspection to fallback gracefully when `exc.errors` is not subscriptable. **Timeout Propagation**: Added the missing `timeout` parameter to `RowIterator.to_dataframe` in `google/cloud/bigquery/table.py`. This ensures that the user-specified `timeout` is correctly passed down to the underlying `to_arrow` call, preventing the client from hanging indefinitely when the Storage API is unresponsive. **Changes** Modified `google/cloud/bigquery/retry.py`: Updated `_should_retry` to handle `TypeError` and `KeyError` when accessing `exc.errors`. Modified `google/cloud/bigquery/table.py`: Updated `RowIterator.to_dataframe` signature and implementation to accept and pass the `timeout` parameter. The first half of this work was completed in PR #2354
PR created by the Librarian CLI to initialize a release. Merging this PR will auto trigger a release. Librarian Version: v0.8.0 Language Image: us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/python-librarian-generator@sha256:c8612d3fffb3f6a32353b2d1abd16b61e87811866f7ec9d65b59b02eb452a620 <details><summary>google-cloud-bigquery: 3.40.1</summary> ## [3.40.1](https://togithub.com/googleapis/python-bigquery/compare/v3.40.0...v3.40.1) (2026-02-12) ### Bug Fixes * updates timeout/retry code to respect hanging server (#2408) ([24d45d0](https://togithub.com/googleapis/python-bigquery/commit/24d45d0d)) * add timeout parameter to to_dataframe and to_arrow met… (#2354) ([4f67ba2](https://togithub.com/googleapis/python-bigquery/commit/4f67ba20)) ### Documentation * clarify that only jobs.query and jobs.getQueryResults are affec… (#2349) ([7322843](https://togithub.com/googleapis/python-bigquery/commit/73228432)) </details>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.