Commit d172408
authored
feat(bigquery): support queryResultsFormat and compressionCodec in query_and_wait (#18027)
### Summary of Changes
Adds support for fetching query results in Apache Arrow format directly
via `query_and_wait()` using `queryResultsFormat="ARROW"` and optional
buffer compression (e.g., `compression_codec="LZ4_FRAME"`).
1. **`query_and_wait` & `_job_helpers` Enhancements**:
- Added `query_results_format` and `compression_codec` parameters (with
`[Beta]` docstring annotations) to `client.query_and_wait()`,
`client._query_and_wait_bigframes()`, and
`_job_helpers.query_and_wait()`.
- Included `queryResultsFormat` in `_job_helpers.keys_allowlist` and
populated `formatOptions.arrowSerializationOptions.bufferCompression` in
`jobs.query` REST API request payloads.
- Refactored `_wait_or_cancel()` to accept and preserve
`query_results_format` on returned `RowIterator` instances.
2. **Arrow Serialization & Direct Job Stream Reading**:
- Added `RowIterator._download_arrow_from_job_id()` to stream Arrow
record batches directly from
`projects/{project}/locations/{location}/jobs/{job_id}/streams/_default`
via the BigQuery Storage Read API.
- Added logic to decode base64 inline `arrowSchema` and
`arrowRecordBatch` from the initial `jobs.query` REST response
(`_first_page_response`), calculate the starting row `offset`, and
resume `read_rows(stream_name, offset=offset)`.
- Added an optimization to skip calling `read_rows()` or initializing
`BigQueryReadClient` if `jobComplete = True` and all rows were returned
within the first page response.
3. **Safety & Enforcement**:
- Overrode `pages`, `__iter__`, and `__next__` on `RowIterator` and
`_EmptyRowIterator` to raise a descriptive `ValueError` if non-Arrow
iteration is attempted when `queryResultsFormat="ARROW"`.
4. **Testing**:
- Added comprehensive unit test suite in
`tests/unit/test_query_results_format_arrow.py` (16 passing tests)
covering request body formatting, parameter propagation, base64 payload
decoding, offset calculation, stream URI construction, and Storage
client skipping when all rows are present in the first page.
---
Thank you for opening a Pull Request! Before submitting your PR, there
are a few things you can do to make sure it goes smoothly:
- [x] Make sure to open an issue as a
[bug/issue](https://github.com/googleapis/google-cloud-python/issues)
before writing your code! That way we can discuss the change, evaluate
designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)1 parent 9634907 commit d172408
8 files changed
Lines changed: 855 additions & 7 deletions
File tree
- packages/google-cloud-bigquery
- google/cloud/bigquery
- tests
- system
- unit
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
| 63 | + | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
| |||
221 | 223 | | |
222 | 224 | | |
223 | 225 | | |
| 226 | + | |
| 227 | + | |
224 | 228 | | |
225 | 229 | | |
226 | 230 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
430 | 430 | | |
431 | 431 | | |
432 | 432 | | |
| 433 | + | |
| 434 | + | |
433 | 435 | | |
434 | 436 | | |
435 | 437 | | |
| |||
475 | 477 | | |
476 | 478 | | |
477 | 479 | | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
478 | 484 | | |
479 | 485 | | |
480 | 486 | | |
| |||
499 | 505 | | |
500 | 506 | | |
501 | 507 | | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
502 | 515 | | |
503 | 516 | | |
504 | 517 | | |
| |||
522 | 535 | | |
523 | 536 | | |
524 | 537 | | |
| 538 | + | |
525 | 539 | | |
526 | 540 | | |
527 | 541 | | |
| |||
594 | 608 | | |
595 | 609 | | |
596 | 610 | | |
| 611 | + | |
597 | 612 | | |
598 | 613 | | |
599 | 614 | | |
| |||
633 | 648 | | |
634 | 649 | | |
635 | 650 | | |
| 651 | + | |
636 | 652 | | |
637 | 653 | | |
638 | 654 | | |
| |||
673 | 689 | | |
674 | 690 | | |
675 | 691 | | |
| 692 | + | |
676 | 693 | | |
677 | 694 | | |
678 | 695 | | |
| |||
687 | 704 | | |
688 | 705 | | |
689 | 706 | | |
| 707 | + | |
690 | 708 | | |
691 | 709 | | |
692 | 710 | | |
| |||
731 | 749 | | |
732 | 750 | | |
733 | 751 | | |
| 752 | + | |
734 | 753 | | |
735 | 754 | | |
736 | 755 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3665 | 3665 | | |
3666 | 3666 | | |
3667 | 3667 | | |
| 3668 | + | |
| 3669 | + | |
3668 | 3670 | | |
3669 | 3671 | | |
3670 | 3672 | | |
| |||
3712 | 3714 | | |
3713 | 3715 | | |
3714 | 3716 | | |
| 3717 | + | |
| 3718 | + | |
| 3719 | + | |
| 3720 | + | |
3715 | 3721 | | |
3716 | 3722 | | |
3717 | 3723 | | |
| |||
3742 | 3748 | | |
3743 | 3749 | | |
3744 | 3750 | | |
| 3751 | + | |
| 3752 | + | |
3745 | 3753 | | |
3746 | 3754 | | |
3747 | 3755 | | |
| |||
3757 | 3765 | | |
3758 | 3766 | | |
3759 | 3767 | | |
| 3768 | + | |
| 3769 | + | |
3760 | 3770 | | |
3761 | 3771 | | |
3762 | 3772 | | |
| |||
3789 | 3799 | | |
3790 | 3800 | | |
3791 | 3801 | | |
| 3802 | + | |
| 3803 | + | |
3792 | 3804 | | |
3793 | 3805 | | |
3794 | 3806 | | |
| |||
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
495 | 495 | | |
496 | 496 | | |
497 | 497 | | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
0 commit comments