Skip to content

Commit 207dd7c

Browse files
author
Jesse
authored
Use hex string version of operation ID instead of bytes (#170)
--------- Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent d7f76e4 commit 207dd7c

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- Fix: Revised SQLAlchemy dialect and examples for compatibility with SQLAlchemy==1.3.x (#173)
77
- Fix: oauth would fail if expired credentials appeared in ~/.netrc (#122)
88
- Fix: Python HTTP proxies were broken after switch to urllib3 (#158)
9-
- Other: Relax pandas dependency constraint to allow ^2.0.0 (#164)
9+
- Other: Connector now logs operation handle guids as hexadecimal instead of bytes (#170)
1010

1111
## 2.7.0 (2023-06-26)
1212

src/databricks/sql/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def __del__(self):
206206
if self.open:
207207
logger.debug(
208208
"Closing unclosed connection for session "
209-
"{}".format(self.get_session_id())
209+
"{}".format(self.get_session_id_hex())
210210
)
211211
try:
212212
self._close(close_cursors=False)

src/databricks/sql/thrift_backend.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,24 +532,29 @@ def _check_command_not_in_error_or_closed_state(
532532
raise ServerOperationError(
533533
get_operations_resp.displayMessage,
534534
{
535-
"operation-id": op_handle and op_handle.operationId.guid,
535+
"operation-id": op_handle
536+
and self.guid_to_hex_id(op_handle.operationId.guid),
536537
"diagnostic-info": get_operations_resp.diagnosticInfo,
537538
},
538539
)
539540
else:
540541
raise ServerOperationError(
541542
get_operations_resp.errorMessage,
542543
{
543-
"operation-id": op_handle and op_handle.operationId.guid,
544+
"operation-id": op_handle
545+
and self.guid_to_hex_id(op_handle.operationId.guid),
544546
"diagnostic-info": None,
545547
},
546548
)
547549
elif get_operations_resp.operationState == ttypes.TOperationState.CLOSED_STATE:
548550
raise DatabaseError(
549551
"Command {} unexpectedly closed server side".format(
550-
op_handle and op_handle.operationId.guid
552+
op_handle and self.guid_to_hex_id(op_handle.operationId.guid)
551553
),
552-
{"operation-id": op_handle and op_handle.operationId.guid},
554+
{
555+
"operation-id": op_handle
556+
and self.guid_to_hex_id(op_handle.operationId.guid)
557+
},
553558
)
554559

555560
def _poll_for_status(self, op_handle):
@@ -942,7 +947,11 @@ def close_command(self, op_handle):
942947
return resp.status
943948

944949
def cancel_command(self, active_op_handle):
945-
logger.debug("Cancelling command {}".format(active_op_handle.operationId.guid))
950+
logger.debug(
951+
"Cancelling command {}".format(
952+
self.guid_to_hex_id(active_op_handle.operationId.guid)
953+
)
954+
)
946955
req = ttypes.TCancelOperationReq(active_op_handle)
947956
self.make_request(self._client.CancelOperation, req)
948957

@@ -954,3 +963,23 @@ def handle_to_id(session_handle):
954963
def handle_to_hex_id(session_handle: TCLIService.TSessionHandle):
955964
this_uuid = uuid.UUID(bytes=session_handle.sessionId.guid)
956965
return str(this_uuid)
966+
967+
@staticmethod
968+
def guid_to_hex_id(guid: bytes) -> str:
969+
"""Return a hexadecimal string instead of bytes
970+
971+
Example:
972+
IN b'\x01\xee\x1d)\xa4\x19\x1d\xb6\xa9\xc0\x8d\xf1\xfe\xbaB\xdd'
973+
OUT '01ee1d29-a419-1db6-a9c0-8df1feba42dd'
974+
975+
If conversion to hexadecimal fails, the original bytes are returned
976+
"""
977+
978+
this_uuid: Union[bytes, uuid.UUID]
979+
980+
try:
981+
this_uuid = uuid.UUID(bytes=guid)
982+
except Exception as e:
983+
logger.debug(f"Unable to convert bytes to UUID: {bytes} -- {str(e)}")
984+
this_uuid = guid
985+
return str(this_uuid)

0 commit comments

Comments
 (0)