|
49 | 49 | from google.api_core.exceptions import DeadlineExceeded |
50 | 50 | from google.api_core.exceptions import ServiceUnavailable |
51 | 51 | from google.api_core.exceptions import Aborted |
| 52 | +from google.protobuf.message import Message |
| 53 | +from google.protobuf.internal.enum_type_wrapper import EnumTypeWrapper |
52 | 54 | import google.auth.credentials |
53 | 55 | import google.auth._default |
54 | 56 | from google.api_core import client_options as client_options_lib |
@@ -485,7 +487,7 @@ def execute_query( |
485 | 487 | DeadlineExceeded, |
486 | 488 | ServiceUnavailable, |
487 | 489 | ), |
488 | | - column_info: dict[str, Any] | None = None, |
| 490 | + column_info: dict[str, Message | EnumTypeWrapper] | None = None, |
489 | 491 | ) -> "ExecuteQueryIterator": |
490 | 492 | """Executes an SQL query on an instance. |
491 | 493 | Returns an iterator to asynchronously stream back columns from selected rows. |
@@ -533,13 +535,58 @@ def execute_query( |
533 | 535 | If None, defaults to prepare_operation_timeout. |
534 | 536 | prepare_retryable_errors: a list of errors that will be retried if encountered during prepareQuery. |
535 | 537 | Defaults to 4 (DeadlineExceeded) and 14 (ServiceUnavailable) |
536 | | - column_info: Dictionary with mappings between column names and additional column information. |
537 | | - An object where column names as keys and custom objects as corresponding |
538 | | - values for deserialization. It's specifically useful for data types like |
539 | | - protobuf where deserialization logic is on user-specific code. When provided, |
540 | | - the custom object enables deserialization of backend-received column data. |
541 | | - If not provided, data remains serialized as bytes for Proto Messages and |
542 | | - integer for Proto Enums. |
| 538 | + column_info: (Optional) A dictionary mapping column names to Protobuf message classes or EnumTypeWrapper objects. |
| 539 | + This dictionary provides the necessary type information for deserializing PROTO and |
| 540 | + ENUM column values from the query results. When an entry is provided |
| 541 | + for a PROTO or ENUM column, the client library will attempt to deserialize the raw data. |
| 542 | +
|
| 543 | + - For PROTO columns: The value in the dictionary should be the |
| 544 | + Protobuf Message class (e.g., ``my_pb2.MyMessage``). |
| 545 | + - For ENUM columns: The value should be the Protobuf EnumTypeWrapper |
| 546 | + object (e.g., ``my_pb2.MyEnum``). |
| 547 | +
|
| 548 | + Example:: |
| 549 | +
|
| 550 | + import my_pb2 |
| 551 | +
|
| 552 | + column_info = { |
| 553 | + "my_proto_column": my_pb2.MyMessage, |
| 554 | + "my_enum_column": my_pb2.MyEnum |
| 555 | + } |
| 556 | +
|
| 557 | + If ``column_info`` is not provided, or if a specific column name is not found |
| 558 | + in the dictionary, or if deserialization fails: |
| 559 | +
|
| 560 | + - PROTO columns will be returned as raw bytes. |
| 561 | + - ENUM columns will be returned as integers. |
| 562 | +
|
| 563 | + Note for Nested PROTO or ENUM Fields: |
| 564 | +
|
| 565 | + To specify types for PROTO or ENUM fields within STRUCTs or MAPs, use a dot-separated |
| 566 | + path from the top-level column name. |
| 567 | +
|
| 568 | + - For STRUCTs: ``struct_column_name.field_name`` |
| 569 | + - For MAPs: ``map_column_name.key`` or ``map_column_name.value`` to specify types |
| 570 | + for the map keys or values, respectively. |
| 571 | +
|
| 572 | + Example:: |
| 573 | +
|
| 574 | + import my_pb2 |
| 575 | +
|
| 576 | + column_info = { |
| 577 | + # Top-level column |
| 578 | + "my_proto_column": my_pb2.MyMessage, |
| 579 | + "my_enum_column": my_pb2.MyEnum, |
| 580 | +
|
| 581 | + # Nested field in a STRUCT column named 'my_struct' |
| 582 | + "my_struct.nested_proto_field": my_pb2.OtherMessage, |
| 583 | + "my_struct.nested_enum_field": my_pb2.AnotherEnum, |
| 584 | +
|
| 585 | + # Nested field in a MAP column named 'my_map' |
| 586 | + "my_map.key": my_pb2.MapKeyEnum, # If map keys were enums |
| 587 | + "my_map.value": my_pb2.MapValueMessage, |
| 588 | + } |
| 589 | +
|
543 | 590 | Returns: |
544 | 591 | ExecuteQueryIterator: an asynchronous iterator that yields rows returned by the query |
545 | 592 | Raises: |
|
0 commit comments