Skip to content

Commit f6fd7a7

Browse files
[PECO-1440] Expose current query id on cursor object (#364)
* [PECO-1440] Expose current query id on cursor object Signed-off-by: Levko Kravets <[email protected]> * Clear `active_op_handle` when closing the cursor Signed-off-by: Levko Kravets <[email protected]> --------- Signed-off-by: Levko Kravets <[email protected]>
1 parent 5048934 commit f6fd7a7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/databricks/sql/client.py

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import os
88
import decimal
9+
from uuid import UUID
910

1011
from databricks.sql import __version__
1112
from databricks.sql import *
@@ -1004,9 +1005,22 @@ def cancel(self) -> None:
10041005
def close(self) -> None:
10051006
"""Close cursor"""
10061007
self.open = False
1008+
self.active_op_handle = None
10071009
if self.active_result_set:
10081010
self._close_and_clear_active_result_set()
10091011

1012+
@property
1013+
def query_id(self) -> Optional[str]:
1014+
"""
1015+
This attribute is an identifier of last executed query.
1016+
1017+
This attribute will be ``None`` if the cursor has not had an operation
1018+
invoked via the execute method yet, or if cursor was closed.
1019+
"""
1020+
if self.active_op_handle is not None:
1021+
return str(UUID(bytes=self.active_op_handle.operationId.guid))
1022+
return None
1023+
10101024
@property
10111025
def description(self) -> Optional[List[Tuple]]:
10121026
"""

tests/unit/test_client.py

+21
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
import itertools
77
from decimal import Decimal
88
from datetime import datetime, date
9+
from uuid import UUID
910

1011
from databricks.sql.thrift_api.TCLIService.ttypes import (
1112
TOpenSessionResp,
1213
TExecuteStatementResp,
14+
TOperationHandle,
15+
THandleIdentifier,
16+
TOperationType
1317
)
1418
from databricks.sql.thrift_backend import ThriftBackend
1519

@@ -610,6 +614,23 @@ def test_staging_operation_response_is_handled(self, mock_client_class, mock_han
610614

611615
mock_handle_staging_operation.call_count == 1
612616

617+
@patch("%s.client.ThriftBackend" % PACKAGE_NAME, ThriftBackendMockFactory.new())
618+
def test_access_current_query_id(self):
619+
operation_id = 'EE6A8778-21FC-438B-92D8-96AC51EE3821'
620+
621+
connection = databricks.sql.connect(**self.DUMMY_CONNECTION_ARGS)
622+
cursor = connection.cursor()
623+
624+
self.assertIsNone(cursor.query_id)
625+
626+
cursor.active_op_handle = TOperationHandle(
627+
operationId=THandleIdentifier(guid=UUID(operation_id).bytes, secret=0x00),
628+
operationType=TOperationType.EXECUTE_STATEMENT)
629+
self.assertEqual(cursor.query_id.upper(), operation_id.upper())
630+
631+
cursor.close()
632+
self.assertIsNone(cursor.query_id)
633+
613634

614635
if __name__ == '__main__':
615636
suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

0 commit comments

Comments
 (0)