Skip to content

Commit 04419e8

Browse files
add error info to ServerOperationFailure
Signed-off-by: varun-edachali-dbx <[email protected]>
1 parent 8fbca9d commit 04419e8

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

src/databricks/sql/backend/sea/backend.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import re
66
from typing import Any, Dict, Tuple, List, Optional, Union, TYPE_CHECKING, Set
77

8-
from databricks.sql.backend.sea.models.base import ExternalLink, ResultManifest
8+
from databricks.sql.backend.sea.models.base import (
9+
ExternalLink,
10+
ResultManifest,
11+
StatementStatus,
12+
)
913
from databricks.sql.backend.sea.utils.constants import (
1014
ALLOWED_SESSION_CONF_TO_DEFAULT_VALUES_MAP,
1115
ResultFormat,
@@ -389,8 +393,9 @@ def _response_to_result_set(
389393
)
390394

391395
def _check_command_not_in_failed_or_closed_state(
392-
self, state: CommandState, command_id: CommandId
396+
self, status: StatementStatus, command_id: CommandId
393397
) -> None:
398+
state = status.state
394399
if state == CommandState.CLOSED:
395400
raise DatabaseError(
396401
"Command {} unexpectedly closed server side".format(command_id),
@@ -399,8 +404,9 @@ def _check_command_not_in_failed_or_closed_state(
399404
},
400405
)
401406
if state == CommandState.FAILED:
407+
error = status.error
402408
raise ServerOperationError(
403-
"Command {} failed".format(command_id),
409+
"Command failed - {}: {}".format(error.error_code, error.message),
404410
{
405411
"operation-id": command_id,
406412
},
@@ -414,16 +420,18 @@ def _wait_until_command_done(
414420
"""
415421

416422
final_response: Union[ExecuteStatementResponse, GetStatementResponse] = response
417-
418-
state = final_response.status.state
419423
command_id = CommandId.from_sea_statement_id(final_response.statement_id)
420424

421-
while state in [CommandState.PENDING, CommandState.RUNNING]:
425+
while final_response.status.state in [
426+
CommandState.PENDING,
427+
CommandState.RUNNING,
428+
]:
422429
time.sleep(self.POLL_INTERVAL_SECONDS)
423430
final_response = self._poll_query(command_id)
424-
state = final_response.status.state
425431

426-
self._check_command_not_in_failed_or_closed_state(state, command_id)
432+
self._check_command_not_in_failed_or_closed_state(
433+
final_response.status, command_id
434+
)
427435

428436
return final_response
429437

tests/unit/test_sea_backend.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SeaDatabricksClient,
1313
_filter_session_configuration,
1414
)
15+
from databricks.sql.backend.sea.models.base import ServiceError, StatementStatus
1516
from databricks.sql.backend.types import SessionId, CommandId, CommandState, BackendType
1617
from databricks.sql.parameters.native import IntegerParameter, TDbsqlParameter
1718
from databricks.sql.thrift_api.TCLIService import ttypes
@@ -408,7 +409,7 @@ def test_command_execution_advanced(
408409
async_op=False,
409410
enforce_embedded_schema_correctness=False,
410411
)
411-
assert "Command test-statement-123 failed" in str(excinfo.value)
412+
assert "Command failed" in str(excinfo.value)
412413

413414
# Test missing statement ID
414415
mock_http_client.reset_mock()
@@ -529,30 +530,35 @@ def test_command_management(
529530
def test_check_command_state(self, sea_client, sea_command_id):
530531
"""Test _check_command_not_in_failed_or_closed_state method."""
531532
# Test with RUNNING state (should not raise)
532-
sea_client._check_command_not_in_failed_or_closed_state(
533-
CommandState.RUNNING, sea_command_id
534-
)
533+
status = StatementStatus(state=CommandState.RUNNING)
534+
sea_client._check_command_not_in_failed_or_closed_state(status, sea_command_id)
535535

536536
# Test with SUCCEEDED state (should not raise)
537-
sea_client._check_command_not_in_failed_or_closed_state(
538-
CommandState.SUCCEEDED, sea_command_id
539-
)
537+
status = StatementStatus(state=CommandState.SUCCEEDED)
538+
sea_client._check_command_not_in_failed_or_closed_state(status, sea_command_id)
540539

541540
# Test with CLOSED state (should raise DatabaseError)
541+
status = StatementStatus(state=CommandState.CLOSED)
542542
with pytest.raises(DatabaseError) as excinfo:
543543
sea_client._check_command_not_in_failed_or_closed_state(
544-
CommandState.CLOSED, sea_command_id
544+
status, sea_command_id
545545
)
546546
assert "Command test-statement-123 unexpectedly closed server side" in str(
547547
excinfo.value
548548
)
549549

550550
# Test with FAILED state (should raise ServerOperationError)
551+
status = StatementStatus(
552+
state=CommandState.FAILED,
553+
error=ServiceError(
554+
message="Syntax error in SQL", error_code="SYNTAX_ERROR"
555+
),
556+
)
551557
with pytest.raises(ServerOperationError) as excinfo:
552558
sea_client._check_command_not_in_failed_or_closed_state(
553-
CommandState.FAILED, sea_command_id
559+
status, sea_command_id
554560
)
555-
assert "Command test-statement-123 failed" in str(excinfo.value)
561+
assert "Command failed" in str(excinfo.value)
556562

557563
def test_utility_methods(self, sea_client):
558564
"""Test utility methods."""

0 commit comments

Comments
 (0)