99
1010from databricks .sql .backend .databricks_client import DatabricksClient
1111from databricks .sql .backend .types import SessionId , CommandId , CommandState , BackendType
12- from databricks .sql .exc import Error , NotSupportedError
12+ from databricks .sql .exc import Error , NotSupportedError , ServerOperationError
1313from databricks .sql .backend .utils .http_client import CustomHttpClient
1414from databricks .sql .thrift_api .TCLIService import ttypes
1515from databricks .sql .types import SSLOptions
@@ -192,7 +192,13 @@ def open_session(
192192 session_id = session_response .session_id
193193
194194 if not session_id :
195- raise Error ("Failed to create session: No session ID returned" )
195+ raise ServerOperationError (
196+ "Failed to create session: No session ID returned" ,
197+ {
198+ "operation-id" : None ,
199+ "diagnostic-info" : None ,
200+ },
201+ )
196202
197203 return SessionId .from_sea_session_id (session_id )
198204
@@ -302,7 +308,13 @@ def execute_command(
302308 # Create a command ID from the statement ID
303309 statement_id = response .statement_id
304310 if not statement_id :
305- raise Error ("Failed to execute command: No statement ID returned" )
311+ raise ServerOperationError (
312+ "Failed to execute command: No statement ID returned" ,
313+ {
314+ "operation-id" : None ,
315+ "diagnostic-info" : None ,
316+ },
317+ )
306318
307319 command_id = CommandId .from_sea_statement_id (statement_id )
308320
@@ -320,33 +332,18 @@ def execute_command(
320332
321333 # Keep polling until we reach a terminal state
322334 while state in [CommandState .PENDING , CommandState .RUNNING ]:
323- # Add a small delay to avoid excessive API calls
324- time .sleep (0.5 )
325-
326- # Create the request model
327- get_request = GetStatementRequest (statement_id = statement_id )
328-
329- # Get the statement status
330- poll_response_data = self .http_client ._make_request (
331- method = "GET" ,
332- path = self .STATEMENT_PATH_WITH_ID .format (statement_id ),
333- data = get_request .to_dict (),
335+ time .sleep (0.5 ) # add a small delay to avoid excessive API calls
336+ state = self .get_query_state (command_id )
337+
338+ if state != CommandState .SUCCEEDED :
339+ raise ServerOperationError (
340+ f"Statement execution did not succeed: { status .error .message } " ,
341+ {
342+ "operation-id" : command_id .to_sea_statement_id (),
343+ "diagnostic-info" : None ,
344+ },
334345 )
335346
336- # Parse the response
337- poll_response = GetStatementResponse .from_dict (poll_response_data )
338- status = poll_response .status
339- state = status .state
340-
341- # Check for errors
342- if state == CommandState .FAILED and status .error :
343- error_message = status .error .message
344- raise Error (f"Statement execution failed: { error_message } " )
345-
346- # Check for cancellation
347- if state == CommandState .CANCELLED :
348- raise Error ("Statement execution was canceled" )
349-
350347 # Get the final result
351348 return self .get_execution_result (command_id , cursor )
352349
0 commit comments