Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(sql): preserve hint/sql_line/sql_col from engine-reported MarimoSQLException (#10997)

Quan Kaiyi330ca4113aa5
diff --git a/marimo/_sql/error_utils.py b/marimo/_sql/error_utils.py
index 4b0e96737e..a485a45eac 100644
--- a/marimo/_sql/error_utils.py
+++ b/marimo/_sql/error_utils.py
@@ -237,9 +237,15 @@ def create_sql_error_from_exception(
if hasattr(cell, "sqls") and cell.sqls:
sql_statement = str(cell.sqls[0])
- # Check if this is a MarimoSQLException with structured hint data
- if isinstance(exception, MarimoSQLException) and exception.hint:
- # Use the structured hint data from the exception
+ # Check if this is a MarimoSQLException carrying structured metadata, and
+ # reraise accordingly to preserve information.
+ if isinstance(exception, MarimoSQLException) and (
+ exception.hint is not None
+ or exception.sql_statement
+ or exception.sql_line is not None
+ or exception.sql_col is not None
+ ):
+ # Use the structured data from the exception
from marimo._messaging.errors import MarimoSQLError
return MarimoSQLError(
diff --git a/marimo/_sql/sql.py b/marimo/_sql/sql.py
index 537a425c38..40fa0e84e1 100644
--- a/marimo/_sql/sql.py
+++ b/marimo/_sql/sql.py
@@ -134,6 +134,14 @@ def sql(
if is_sql_parse_error(e):
# NB. raising _from_ creates a noisier stack trace, but preserves
# the original exception context for debugging.
+ if isinstance(e, MarimoSQLException):
+ raise MarimoSQLException(
+ message=str(e),
+ sql_statement=e.sql_statement or query,
+ sql_line=e.sql_line,
+ sql_col=e.sql_col,
+ hint=e.hint,
+ ) from e
raise MarimoSQLException(
message=str(e),
sql_statement=query,
diff --git a/tests/_sql/test_sql.py b/tests/_sql/test_sql.py
index 2733c5bfce..45094b4cee 100644
--- a/tests/_sql/test_sql.py
+++ b/tests/_sql/test_sql.py
@@ -333,6 +333,38 @@ def test_invalid_sql(
assert isinstance(exc_info.value.__cause__, sqlalchemy.exc.StatementError)
[email protected](not HAS_DUCKDB, reason="DuckDB not installed")
+def test_sql_preserves_structured_error_details(
+ duckdb_connection: duckdb.DuckDBPyConnection,
+) -> None:
+ """mo.sql must not drop hint/sql_line/sql_col from engine-reported
+ MarimoSQLException (issue #10987)."""
+ from marimo._sql.engines.duckdb import DuckDBEngine
+ from marimo._sql.error_utils import MarimoSQLException
+
+ original_execute = DuckDBEngine.execute
+
+ def raising_execute(_engine: object, query: str) -> object:
+ raise MarimoSQLException(
+ message="boom",
+ sql_statement=query,
+ sql_line=3,
+ sql_col=12,
+ hint="try CAST",
+ )
+
+ DuckDBEngine.execute = raising_execute # type: ignore[method-assign]
+ try:
+ with pytest.raises(MarimoSQLException) as exc_info:
+ sql("SELECT * FROM t", engine=duckdb_connection)
+ assert exc_info.value.sql_line == 3
+ assert exc_info.value.sql_col == 12
+ assert exc_info.value.hint == "try CAST"
+ assert exc_info.value.sql_statement == "SELECT * FROM t"
+ finally:
+ DuckDBEngine.execute = original_execute # type: ignore[method-assign]
+
+
# TODO
@pytest.mark.skipif(not HAS_PANDAS, reason="Pandas not installed")
def test_sql_with_limit() -> None:
diff --git a/tests/_sql/test_sql_error_handling.py b/tests/_sql/test_sql_error_handling.py
index 6169a48505..5a7a4f6738 100644
--- a/tests/_sql/test_sql_error_handling.py
+++ b/tests/_sql/test_sql_error_handling.py
@@ -235,6 +235,26 @@ class TestErrorUtilityFunctions:
# Hint field should exist (may be None for this error)
assert hasattr(error, "hint")
+ def test_create_sql_error_forwards_position_without_hint(self):
+ """Coordinates (sql_line/sql_col) must be forwarded even when hint is
+ None — engines may report position without a hint (issue #10987)."""
+
+ class MockCell:
+ sqls = ["SELECT * FROM t"]
+
+ exception = MarimoSQLException(
+ message="syntax error",
+ sql_statement="SELECT * FROM t",
+ sql_line=3,
+ sql_col=12,
+ hint=None,
+ )
+ error = create_sql_error_from_exception(exception, MockCell())
+ assert error.sql_line == 3
+ assert error.sql_col == 12
+ assert error.sql_statement == "SELECT * FROM t"
+ assert error.hint is None
+
@pytest.mark.requires("duckdb")
def test_create_sql_error_long_statement(self, duckdb_conn):
"""Test SQL statement truncation in error creation."""