From 0e29e81c5c94ef3945ae874f1e71d82ecda61dde Mon Sep 17 00:00:00 2001 From: Atharva Chavhan Date: Tue, 1 Dec 2026 18:15:33 +0530 Subject: [PATCH] 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 --- src/databricks/sql/backend/thrift_backend.py | 10 ++- tests/unit/test_thrift_backend.py | 79 ++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/databricks/sql/backend/thrift_backend.py b/src/databricks/sql/backend/thrift_backend.py index e6ca68d6d..894d93890 100644 --- a/src/databricks/sql/backend/thrift_backend.py +++ b/src/databricks/sql/backend/thrift_backend.py @@ -685,7 +685,7 @@ def _check_command_not_in_error_or_closed_state( ): if get_operations_resp.operationState == ttypes.TOperationState.ERROR_STATE: if get_operations_resp.displayMessage: - raise ServerOperationError( + err = ServerOperationError( get_operations_resp.displayMessage, { "operation-id": op_handle @@ -694,8 +694,12 @@ def _check_command_not_in_error_or_closed_state( }, host_url=self._host, ) + # Forward the operation-level SQLSTATE, mirroring the kernel + # backend; None when the payload carries none (never invent one). + err.sql_state = get_operations_resp.sqlState + raise err else: - raise ServerOperationError( + err = ServerOperationError( get_operations_resp.errorMessage, { "operation-id": op_handle @@ -704,6 +708,8 @@ def _check_command_not_in_error_or_closed_state( }, host_url=self._host, ) + err.sql_state = get_operations_resp.sqlState + raise err elif get_operations_resp.operationState == ttypes.TOperationState.CLOSED_STATE: raise DatabaseError( "Command {} unexpectedly closed server side".format( diff --git a/tests/unit/test_thrift_backend.py b/tests/unit/test_thrift_backend.py index c9ffc12b9..e3af1b408 100644 --- a/tests/unit/test_thrift_backend.py +++ b/tests/unit/test_thrift_backend.py @@ -1467,6 +1467,85 @@ def test_get_status_uses_display_message_if_available(self, tcli_service_class): self.assertEqual(display_message, str(cm.exception)) self.assertIn(diagnostic_info, str(cm.exception.message_with_context())) + @patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True) + def test_get_status_forwards_server_sql_state(self, tcli_service_class): + tcli_service_instance = tcli_service_class.return_value + + t_get_operation_status_resp = ttypes.TGetOperationStatusResp( + status=self.okay_status, + operationState=ttypes.TOperationState.ERROR_STATE, + errorMessage="boom", + displayMessage="boom", + diagnosticInfo="trace", + sqlState="42000", + ) + + t_execute_resp = ttypes.TExecuteStatementResp( + status=self.okay_status, + directResults=None, + operationHandle=self.operation_handle, + ) + tcli_service_instance.GetOperationStatus.return_value = ( + t_get_operation_status_resp + ) + tcli_service_instance.ExecuteStatement.return_value = t_execute_resp + + thrift_backend = ThriftDatabricksClient( + "foobar", + 443, + "path", + [], + auth_provider=AuthProvider(), + ssl_options=SSLOptions(), + http_client=MagicMock(), + ) + with self.assertRaises(DatabaseError) as cm: + thrift_backend.execute_command( + Mock(), Mock(), 100, 100, Mock(), Mock(), Mock() + ) + + self.assertEqual("42000", cm.exception.sql_state) + + @patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True) + def test_get_status_sql_state_is_none_when_server_sends_none( + self, tcli_service_class + ): + tcli_service_instance = tcli_service_class.return_value + + t_get_operation_status_resp = ttypes.TGetOperationStatusResp( + status=self.okay_status, + operationState=ttypes.TOperationState.ERROR_STATE, + errorMessage="boom", + displayMessage="boom", + diagnosticInfo="trace", + ) + + t_execute_resp = ttypes.TExecuteStatementResp( + status=self.okay_status, + directResults=None, + operationHandle=self.operation_handle, + ) + tcli_service_instance.GetOperationStatus.return_value = ( + t_get_operation_status_resp + ) + tcli_service_instance.ExecuteStatement.return_value = t_execute_resp + + thrift_backend = ThriftDatabricksClient( + "foobar", + 443, + "path", + [], + auth_provider=AuthProvider(), + ssl_options=SSLOptions(), + http_client=MagicMock(), + ) + with self.assertRaises(DatabaseError) as cm: + thrift_backend.execute_command( + Mock(), Mock(), 100, 100, Mock(), Mock(), Mock() + ) + + self.assertIsNone(getattr(cm.exception, "sql_state", None)) + @patch("databricks.sql.backend.thrift_backend.TCLIService.Client", autospec=True) def test_direct_results_uses_display_message_if_available(self, tcli_service_class): tcli_service_instance = tcli_service_class.return_value