Skip to content

Commit b8f9146

Browse files
fix: active op handle -> active command id in Cursor
Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent 8afc5d5 commit b8f9146

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/databricks/sql/backend/thrift_backend.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ def open_session(self, session_configuration, catalog, schema) -> SessionId:
579579
response = self.make_request(self._client.OpenSession, open_session_req)
580580
self._check_initial_namespace(catalog, schema, response)
581581
self._check_protocol_version(response)
582+
if response.sessionHandle is None:
583+
return None
582584
return SessionId.from_thrift_handle(response.sessionHandle)
583585
except:
584586
self._transport.close()
@@ -1132,7 +1134,9 @@ def get_columns(
11321134
)
11331135

11341136
def _handle_execute_response(self, resp, cursor):
1135-
cursor.active_op_handle = resp.operationHandle
1137+
command_id = CommandId.from_thrift_handle(resp.operationHandle)
1138+
1139+
cursor.active_command_id = command_id
11361140
self._check_direct_results_for_error(resp.directResults)
11371141

11381142
final_operation_state = self._wait_until_command_done(
@@ -1143,12 +1147,12 @@ def _handle_execute_response(self, resp, cursor):
11431147
execute_response = self._results_message_to_execute_response(
11441148
resp, final_operation_state
11451149
)
1146-
command_id = CommandId.from_thrift_handle(resp.operationHandle)
11471150
execute_response = execute_response._replace(command_id=command_id)
11481151
return execute_response
11491152

11501153
def _handle_execute_response_async(self, resp, cursor):
1151-
cursor.active_op_handle = resp.operationHandle
1154+
command_id = CommandId.from_thrift_handle(resp.operationHandle)
1155+
cursor.active_command_id = command_id
11521156
self._check_direct_results_for_error(resp.directResults)
11531157

11541158
def fetch_results(

src/databricks/sql/client.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def __init__(
415415
self.open = True
416416
self.executing_command_id = None
417417
self.backend = backend
418-
self.active_op_handle = None
418+
self.active_command_id = None
419419
self.escaper = ParamEscaper()
420420
self.lastrowid = None
421421

@@ -875,7 +875,7 @@ def get_query_state(self) -> "TOperationState":
875875
:return:
876876
"""
877877
self._check_not_closed()
878-
return self.backend.get_query_state(self.active_op_handle)
878+
return self.backend.get_query_state(self.active_command_id)
879879

880880
def is_query_pending(self):
881881
"""
@@ -1111,8 +1111,8 @@ def cancel(self) -> None:
11111111
The command should be closed to free resources from the server.
11121112
This method can be called from another thread.
11131113
"""
1114-
if self.active_op_handle is not None:
1115-
self.backend.cancel_command(self.active_op_handle)
1114+
if self.active_command_id is not None:
1115+
self.backend.cancel_command(self.active_command_id)
11161116
else:
11171117
logger.warning(
11181118
"Attempting to cancel a command, but there is no "
@@ -1124,9 +1124,9 @@ def close(self) -> None:
11241124
self.open = False
11251125

11261126
# Close active operation handle if it exists
1127-
if self.active_op_handle:
1127+
if self.active_command_id:
11281128
try:
1129-
self.backend.close_command(self.active_op_handle)
1129+
self.backend.close_command(self.active_command_id)
11301130
except RequestError as e:
11311131
if isinstance(e.args[1], CursorAlreadyClosedError):
11321132
logger.info("Operation was canceled by a prior request")
@@ -1135,7 +1135,7 @@ def close(self) -> None:
11351135
except Exception as e:
11361136
logging.warning(f"Error closing operation handle: {e}")
11371137
finally:
1138-
self.active_op_handle = None
1138+
self.active_command_id = None
11391139

11401140
if self.active_result_set:
11411141
self._close_and_clear_active_result_set()
@@ -1148,8 +1148,8 @@ def query_id(self) -> Optional[str]:
11481148
This attribute will be ``None`` if the cursor has not had an operation
11491149
invoked via the execute method yet, or if cursor was closed.
11501150
"""
1151-
if self.active_op_handle is not None:
1152-
return self.active_op_handle.to_hex_id()
1151+
if self.active_command_id is not None:
1152+
return self.active_command_id.to_hex_id()
11531153
return None
11541154

11551155
@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)