Skip to content

Commit 02505e8

Browse files
fix: active op handle -> active command id in Cursor
1 parent 42f49b2 commit 02505e8

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/databricks/sql/backend/thrift_backend.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ def open_session(self, session_configuration, catalog, schema) -> SessionId:
578578
response = self.make_request(self._client.OpenSession, open_session_req)
579579
self._check_initial_namespace(catalog, schema, response)
580580
self._check_protocol_version(response)
581+
if response.sessionHandle is None:
582+
return None
581583
return SessionId.from_thrift_handle(response.sessionHandle)
582584
except:
583585
self._transport.close()
@@ -1072,7 +1074,9 @@ def get_columns(
10721074
return self._handle_execute_response(resp, cursor)
10731075

10741076
def _handle_execute_response(self, resp, cursor):
1075-
cursor.active_op_handle = resp.operationHandle
1077+
command_id = CommandId.from_thrift_handle(resp.operationHandle)
1078+
1079+
cursor.active_command_id = command_id
10761080
self._check_direct_results_for_error(resp.directResults)
10771081

10781082
final_operation_state = self._wait_until_command_done(
@@ -1083,12 +1087,12 @@ def _handle_execute_response(self, resp, cursor):
10831087
execute_response = self._results_message_to_execute_response(
10841088
resp, final_operation_state
10851089
)
1086-
command_id = CommandId.from_thrift_handle(resp.operationHandle)
10871090
execute_response = execute_response._replace(command_id=command_id)
10881091
return execute_response
10891092

10901093
def _handle_execute_response_async(self, resp, cursor):
1091-
cursor.active_op_handle = resp.operationHandle
1094+
command_id = CommandId.from_thrift_handle(resp.operationHandle)
1095+
cursor.active_command_id = command_id
10921096
self._check_direct_results_for_error(resp.directResults)
10931097

10941098
def fetch_results(

src/databricks/sql/client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def __init__(
403403
self.open = True
404404
self.executing_command_id = None
405405
self.backend = backend
406-
self.active_op_handle = None
406+
self.active_command_id = None
407407
self.escaper = ParamEscaper()
408408
self.lastrowid = None
409409

@@ -862,7 +862,7 @@ def get_query_state(self) -> "TOperationState":
862862
:return:
863863
"""
864864
self._check_not_closed()
865-
return self.backend.get_query_state(self.active_op_handle)
865+
return self.backend.get_query_state(self.active_command_id)
866866

867867
def is_query_pending(self):
868868
"""
@@ -893,7 +893,7 @@ def get_async_execution_result(self):
893893
operation_state = self.get_query_state()
894894
if operation_state == ttypes.TOperationState.FINISHED_STATE:
895895
execute_response = self.backend.get_execution_result(
896-
self.active_op_handle, self
896+
self.active_command_id, self
897897
)
898898
self.active_result_set = ResultSet(
899899
self.connection,
@@ -1124,8 +1124,8 @@ def cancel(self) -> None:
11241124
The command should be closed to free resources from the server.
11251125
This method can be called from another thread.
11261126
"""
1127-
if self.active_op_handle is not None:
1128-
self.backend.cancel_command(self.active_op_handle)
1127+
if self.active_command_id is not None:
1128+
self.backend.cancel_command(self.active_command_id)
11291129
else:
11301130
logger.warning(
11311131
"Attempting to cancel a command, but there is no "
@@ -1137,9 +1137,9 @@ def close(self) -> None:
11371137
self.open = False
11381138

11391139
# Close active operation handle if it exists
1140-
if self.active_op_handle:
1140+
if self.active_command_id:
11411141
try:
1142-
self.backend.close_command(self.active_op_handle)
1142+
self.backend.close_command(self.active_command_id)
11431143
except RequestError as e:
11441144
if isinstance(e.args[1], CursorAlreadyClosedError):
11451145
logger.info("Operation was canceled by a prior request")
@@ -1148,7 +1148,7 @@ def close(self) -> None:
11481148
except Exception as e:
11491149
logging.warning(f"Error closing operation handle: {e}")
11501150
finally:
1151-
self.active_op_handle = None
1151+
self.active_command_id = None
11521152

11531153
if self.active_result_set:
11541154
self._close_and_clear_active_result_set()
@@ -1161,8 +1161,8 @@ def query_id(self) -> Optional[str]:
11611161
This attribute will be ``None`` if the cursor has not had an operation
11621162
invoked via the execute method yet, or if cursor was closed.
11631163
"""
1164-
if self.active_op_handle is not None:
1165-
return self.active_op_handle.to_hex_id()
1164+
if self.active_command_id is not None:
1165+
return self.active_command_id.to_hex_id()
11661166
return None
11671167

11681168
@property

src/databricks/sql/ids.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def from_thrift_handle(cls, session_handle):
6767
Returns:
6868
A SessionId instance
6969
"""
70+
if session_handle is None or session_handle.sessionId is None:
71+
return None
72+
7073
guid_bytes = session_handle.sessionId.guid
7174
secret_bytes = session_handle.sessionId.secret
7275

@@ -171,6 +174,9 @@ def from_thrift_handle(cls, operation_handle):
171174
Returns:
172175
A CommandId instance
173176
"""
177+
if operation_handle is None or operation_handle.operationId is None:
178+
return None
179+
174180
guid_bytes = operation_handle.operationId.guid
175181
secret_bytes = operation_handle.operationId.secret
176182

0 commit comments

Comments
 (0)