9
9
10
10
from databricks .sql .backend .databricks_client import DatabricksClient
11
11
from 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
13
13
from databricks .sql .backend .utils .http_client import CustomHttpClient
14
14
from databricks .sql .thrift_api .TCLIService import ttypes
15
15
from databricks .sql .types import SSLOptions
@@ -192,7 +192,13 @@ def open_session(
192
192
session_id = session_response .session_id
193
193
194
194
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
+ )
196
202
197
203
return SessionId .from_sea_session_id (session_id )
198
204
@@ -302,7 +308,13 @@ def execute_command(
302
308
# Create a command ID from the statement ID
303
309
statement_id = response .statement_id
304
310
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
+ )
306
318
307
319
command_id = CommandId .from_sea_statement_id (statement_id )
308
320
@@ -320,33 +332,18 @@ def execute_command(
320
332
321
333
# Keep polling until we reach a terminal state
322
334
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
+ },
334
345
)
335
346
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
-
350
347
# Get the final result
351
348
return self .get_execution_result (command_id , cursor )
352
349
0 commit comments