Skip to content

Commit 0e29e81

Browse files
committed
fix(thrift): forward server SQLSTATE on failed operations
The Thrift backend dropped the failed operation's server-reported SQLSTATE: _check_command_not_in_error_or_closed_state raised ServerOperationError without the payload's sqlState, while the kernel backend sets sql_state on the same exception class. Attach the operation-level sqlState to the raised exception in both the displayMessage and errorMessage paths; None when the payload carries none (never invent one). Fixes #1012 Signed-off-by: Atharva Chavhan <atharvachavhan18@gmail.com>
1 parent 543777a commit 0e29e81

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

‎src/databricks/sql/backend/thrift_backend.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ def _check_command_not_in_error_or_closed_state(
685685
):
686686
if get_operations_resp.operationState == ttypes.TOperationState.ERROR_STATE:
687687
if get_operations_resp.displayMessage:
688-
raise ServerOperationError(
688+
err = ServerOperationError(
689689
get_operations_resp.displayMessage,
690690
{
691691
"operation-id": op_handle
@@ -694,8 +694,12 @@ def _check_command_not_in_error_or_closed_state(
694694
},
695695
host_url=self._host,
696696
)
697+
# Forward the operation-level SQLSTATE, mirroring the kernel
698+
# backend; None when the payload carries none (never invent one).
699+
err.sql_state = get_operations_resp.sqlState
700+
raise err
697701
else:
698-
raise ServerOperationError(
702+
err = ServerOperationError(
699703
get_operations_resp.errorMessage,
700704
{
701705
"operation-id": op_handle
@@ -704,6 +708,8 @@ def _check_command_not_in_error_or_closed_state(
704708
},
705709
host_url=self._host,
706710
)
711+
err.sql_state = get_operations_resp.sqlState
712+
raise err
707713
elif get_operations_resp.operationState == ttypes.TOperationState.CLOSED_STATE:
708714
raise DatabaseError(
709715
"Command {} unexpectedly closed server side".format(

‎tests/unit/test_thrift_backend.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,85 @@ def test_get_status_uses_display_message_if_available(self, tcli_service_class):
14671467
self.assertEqual(display_message, str(cm.exception))
14681468
self.assertIn(diagnostic_info, str(cm.exception.message_with_context()))
14691469

1470+
@patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True)
1471+
def test_get_status_forwards_server_sql_state(self, tcli_service_class):
1472+
tcli_service_instance = tcli_service_class.return_value
1473+
1474+
t_get_operation_status_resp = ttypes.TGetOperationStatusResp(
1475+
status=self.okay_status,
1476+
operationState=ttypes.TOperationState.ERROR_STATE,
1477+
errorMessage="boom",
1478+
displayMessage="boom",
1479+
diagnosticInfo="trace",
1480+
sqlState="42000",
1481+
)
1482+
1483+
t_execute_resp = ttypes.TExecuteStatementResp(
1484+
status=self.okay_status,
1485+
directResults=None,
1486+
operationHandle=self.operation_handle,
1487+
)
1488+
tcli_service_instance.GetOperationStatus.return_value = (
1489+
t_get_operation_status_resp
1490+
)
1491+
tcli_service_instance.ExecuteStatement.return_value = t_execute_resp
1492+
1493+
thrift_backend = ThriftDatabricksClient(
1494+
"foobar",
1495+
443,
1496+
"path",
1497+
[],
1498+
auth_provider=AuthProvider(),
1499+
ssl_options=SSLOptions(),
1500+
http_client=MagicMock(),
1501+
)
1502+
with self.assertRaises(DatabaseError) as cm:
1503+
thrift_backend.execute_command(
1504+
Mock(), Mock(), 100, 100, Mock(), Mock(), Mock()
1505+
)
1506+
1507+
self.assertEqual("42000", cm.exception.sql_state)
1508+
1509+
@patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True)
1510+
def test_get_status_sql_state_is_none_when_server_sends_none(
1511+
self, tcli_service_class
1512+
):
1513+
tcli_service_instance = tcli_service_class.return_value
1514+
1515+
t_get_operation_status_resp = ttypes.TGetOperationStatusResp(
1516+
status=self.okay_status,
1517+
operationState=ttypes.TOperationState.ERROR_STATE,
1518+
errorMessage="boom",
1519+
displayMessage="boom",
1520+
diagnosticInfo="trace",
1521+
)
1522+
1523+
t_execute_resp = ttypes.TExecuteStatementResp(
1524+
status=self.okay_status,
1525+
directResults=None,
1526+
operationHandle=self.operation_handle,
1527+
)
1528+
tcli_service_instance.GetOperationStatus.return_value = (
1529+
t_get_operation_status_resp
1530+
)
1531+
tcli_service_instance.ExecuteStatement.return_value = t_execute_resp
1532+
1533+
thrift_backend = ThriftDatabricksClient(
1534+
"foobar",
1535+
443,
1536+
"path",
1537+
[],
1538+
auth_provider=AuthProvider(),
1539+
ssl_options=SSLOptions(),
1540+
http_client=MagicMock(),
1541+
)
1542+
with self.assertRaises(DatabaseError) as cm:
1543+
thrift_backend.execute_command(
1544+
Mock(), Mock(), 100, 100, Mock(), Mock(), Mock()
1545+
)
1546+
1547+
self.assertIsNone(getattr(cm.exception, "sql_state", None))
1548+
14701549
@patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True)
14711550
def test_direct_results_uses_display_message_if_available(self, tcli_service_class):
14721551
tcli_service_instance = tcli_service_class.return_value

0 commit comments

Comments
 (0)