|
6 | 6 | import re |
7 | 7 | import time |
8 | 8 | import threading |
| 9 | +import weakref |
9 | 10 | from typing import Dict, List, Optional, Union, Any, TYPE_CHECKING |
10 | 11 | from uuid import UUID |
11 | 12 |
|
@@ -267,6 +268,7 @@ def __init__( |
267 | 268 | raise |
268 | 269 |
|
269 | 270 | self._request_lock = threading.RLock() |
| 271 | + self._terminal_operation_statuses = weakref.WeakKeyDictionary() |
270 | 272 | self._session_id_hex = None |
271 | 273 |
|
272 | 274 | @property |
@@ -929,17 +931,21 @@ def get_execution_result( |
929 | 931 | ) -> "ResultSet": |
930 | 932 | """Fetch a completed async result and its latest operation metadata. |
931 | 933 |
|
932 | | - FetchResults does not carry ``numModifiedRows``, so Thrift refreshes |
933 | | - operation status before constructing the result set. |
| 934 | + FetchResults does not carry ``numModifiedRows``, so reuse a terminal |
| 935 | + status observed by ``get_query_state`` or refresh it when the caller |
| 936 | + resumes directly by command ID. |
934 | 937 | """ |
935 | 938 | thrift_handle = command_id.to_thrift_handle() |
936 | 939 | if not thrift_handle: |
937 | 940 | raise ValueError("Not a valid Thrift command ID") |
938 | 941 |
|
939 | | - operation_status = self._poll_for_status(thrift_handle) |
940 | | - self._check_command_not_in_error_or_closed_state( |
941 | | - thrift_handle, operation_status |
942 | | - ) |
| 942 | + with self._request_lock: |
| 943 | + operation_status = self._terminal_operation_statuses.pop(command_id, None) |
| 944 | + if operation_status is None: |
| 945 | + operation_status = self._poll_for_status(thrift_handle) |
| 946 | + self._check_command_not_in_error_or_closed_state( |
| 947 | + thrift_handle, operation_status |
| 948 | + ) |
943 | 949 |
|
944 | 950 | req = ttypes.TFetchResultsReq( |
945 | 951 | operationHandle=ttypes.TOperationHandle( |
@@ -1040,6 +1046,9 @@ def get_query_state(self, command_id: CommandId) -> CommandState: |
1040 | 1046 | state = CommandState.from_thrift_state(operation_state) |
1041 | 1047 | if state is None: |
1042 | 1048 | raise ValueError(f"Unknown command state: {operation_state}") |
| 1049 | + if state == CommandState.SUCCEEDED: |
| 1050 | + with self._request_lock: |
| 1051 | + self._terminal_operation_statuses[command_id] = poll_resp |
1043 | 1052 | return state |
1044 | 1053 |
|
1045 | 1054 | @staticmethod |
|
0 commit comments