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: report SQL errors during automatic dataframe conversion (#11247)

Shahmir Varqhae8436c827f90
diff --git a/marimo/_sql/engines/adbc.py b/marimo/_sql/engines/adbc.py
index a4a4ccd55c..78b84a4636 100644
--- a/marimo/_sql/engines/adbc.py
+++ b/marimo/_sql/engines/adbc.py
@@ -599,6 +599,8 @@ class AdbcDBAPIEngine(SQLConnection[AdbcDbApiConnection]):
return None
arrow_table = cursor.fetch_arrow_table()
+ # Conversion errors must not leave completed SQL uncommitted.
+ _try_commit()
def convert_to_polars() -> pl.DataFrame | pl.Series:
import polars as pl
@@ -609,14 +611,12 @@ class AdbcDBAPIEngine(SQLConnection[AdbcDbApiConnection]):
df: pd.DataFrame = arrow_table.to_pandas()
return df
- result = convert_to_output(
+ return convert_to_output(
sql_output_format=sql_output_format,
to_polars=convert_to_polars,
to_pandas=convert_to_pandas,
to_native=lambda: arrow_table,
)
- _try_commit()
- return result
finally:
try:
cursor.close()
diff --git a/marimo/_sql/utils.py b/marimo/_sql/utils.py
index 8a95359a7f..77e50f3d65 100644
--- a/marimo/_sql/utils.py
+++ b/marimo/_sql/utils.py
@@ -296,11 +296,7 @@ def convert_to_output(
DependencyManager.pandas.require("to convert this data")
if DependencyManager.pandas.has():
- try:
- return to_pandas()
- except Exception as e:
- LOGGER.warning("Failed to convert dataframe", exc_info=e)
- return None
+ return to_pandas()
raise_df_import_error("polars[pyarrow]")
diff --git a/tests/_sql/test_adbc.py b/tests/_sql/test_adbc.py
index 502b92faeb..81dd9c0928 100644
--- a/tests/_sql/test_adbc.py
+++ b/tests/_sql/test_adbc.py
@@ -372,7 +372,24 @@ def test_adbc_catalog_parses_adbc_get_objects() -> None:
]
-def test_adbc_execute_prefers_arrow_fetch(monkeypatch) -> None:
+def adbc_execution() -> tuple[FakeAdbcDbApiConnection, FakeAdbcDbApiCursor]:
+ cursor = FakeAdbcDbApiCursor(
+ description=[("id", None)],
+ arrow_table=cast("pa.Table", object()),
+ )
+ conn = FakeAdbcDbApiConnection(
+ cursor=cursor,
+ objects_pylist=[],
+ table_schema=FakeAdbcTableSchema([]),
+ )
+ return conn, cursor
+
+
+def test_adbc_execute_prefers_arrow_fetch(
+ monkeypatch: pytest.MonkeyPatch,
+ adbc_execution: tuple[FakeAdbcDbApiConnection, FakeAdbcDbApiCursor],
+) -> None:
sentinel_result = object()
def fake_convert_to_output(*args: Any, **kwargs: Any) -> Any:
@@ -383,15 +400,7 @@ def test_adbc_execute_prefers_arrow_fetch(monkeypatch) -> None:
"marimo._sql.engines.adbc.convert_to_output", fake_convert_to_output
)
- cursor = FakeAdbcDbApiCursor(
- description=[("col", None)],
- arrow_table=cast("pa.Table", object()),
- )
- conn = FakeAdbcDbApiConnection(
- cursor=cursor,
- objects_pylist=[],
- table_schema=FakeAdbcTableSchema([]),
- )
+ conn, cursor = adbc_execution
engine = AdbcDBAPIEngine(conn)
monkeypatch.setattr(engine, "sql_output_format", lambda: "auto")
@@ -403,22 +412,64 @@ def test_adbc_execute_prefers_arrow_fetch(monkeypatch) -> None:
assert cursor.did_close is True
-def test_adbc_execute_native_returns_arrow_table(monkeypatch) -> None:
- arrow_table = cast("pa.Table", object())
- cursor = FakeAdbcDbApiCursor(
- description=[("col", None)], arrow_table=arrow_table
- )
- conn = FakeAdbcDbApiConnection(
- cursor=cursor,
- objects_pylist=[],
- table_schema=FakeAdbcTableSchema([]),
+def test_adbc_execute_commits_before_conversion_failure(
+ monkeypatch: pytest.MonkeyPatch,
+ adbc_execution: tuple[FakeAdbcDbApiConnection, FakeAdbcDbApiCursor],
+) -> None:
+ conn, cursor = adbc_execution
+ engine = AdbcDBAPIEngine(conn)
+ monkeypatch.setattr(engine, "sql_output_format", lambda: "auto")
+ conversion_error = ValueError("dataframe conversion failed")
+
+ def fail_conversion(**_kwargs: Any) -> Any:
+ raise conversion_error
+
+ monkeypatch.setattr(
+ "marimo._sql.engines.adbc.convert_to_output", fail_conversion
)
+ with pytest.raises(ValueError) as exc_info:
+ engine.execute("INSERT INTO items VALUES (1) RETURNING id")
+
+ assert exc_info.value is conversion_error
+ assert cursor.did_fetch_arrow is True
+ assert conn.did_commit is True
+ assert cursor.did_close is True
+
+
[email protected]("failing_method", ["execute", "fetch_arrow_table"])
+def test_adbc_execute_does_not_commit_failed_query_or_fetch(
+ monkeypatch: pytest.MonkeyPatch,
+ adbc_execution: tuple[FakeAdbcDbApiConnection, FakeAdbcDbApiCursor],
+ failing_method: str,
+) -> None:
+ conn, cursor = adbc_execution
+ query_error = RuntimeError("query or fetch failed")
+
+ def fail(*_args: Any, **_kwargs: Any) -> Any:
+ raise query_error
+
+ monkeypatch.setattr(cursor, failing_method, fail)
+ with pytest.raises(RuntimeError) as exc_info:
+ AdbcDBAPIEngine(conn).execute(
+ "INSERT INTO items VALUES (1) RETURNING id"
+ )
+
+ assert exc_info.value is query_error
+ assert conn.did_commit is False
+ assert cursor.did_close is True
+
+
+def test_adbc_execute_native_returns_arrow_table(
+ monkeypatch: pytest.MonkeyPatch,
+ adbc_execution: tuple[FakeAdbcDbApiConnection, FakeAdbcDbApiCursor],
+) -> None:
+ conn, cursor = adbc_execution
engine = AdbcDBAPIEngine(conn)
monkeypatch.setattr(engine, "sql_output_format", lambda: "native")
result = engine.execute("SELECT 1")
- assert result is arrow_table
+ assert result is cursor._arrow_table
assert cursor.did_execute is True
assert cursor.did_fetch_arrow is True
assert conn.did_commit is True
diff --git a/tests/_sql/test_engine_utils.py b/tests/_sql/test_engine_utils.py
index cd321f4b47..561bde18f5 100644
--- a/tests/_sql/test_engine_utils.py
+++ b/tests/_sql/test_engine_utils.py
@@ -293,32 +293,55 @@ class TestAutoOutputFormat:
assert result == pandas_result
assert_only_one_called(mock_functions, "to_pandas")
+ @pytest.mark.parametrize(
+ "has_polars",
+ [
+ False,
+ pytest.param(
+ True,
+ marks=pytest.mark.skipif(
+ not DependencyManager.polars.has(),
+ reason="Polars is not installed",
+ ),
+ ),
+ ],
+ )
@patch("marimo._sql.utils.DependencyManager")
- @patch("marimo._sql.utils.LOGGER")
- def test_without_polars_with_pandas_failure(
+ def test_pandas_failure_propagates(
self,
- mock_logger: Mock,
mock_dependency_manager: Mock,
mock_functions: MockFnDict,
+ has_polars: bool,
) -> None:
- """Test auto output format when polars is not available and pandas fails."""
- mock_dependency_manager.polars.has.return_value = False
+ """Auto output preserves the final error, including after Polars fallback."""
+ mock_dependency_manager.polars.has.return_value = has_polars
mock_dependency_manager.pandas.has.return_value = True
+ if has_polars:
+ import polars as pl
+
+ mock_functions[
+ "to_polars"
+ ].side_effect = pl.exceptions.ComputeError(
+ "polars conversion failed"
+ )
- pandas_error = Exception("pandas error")
+ pandas_error = ValueError("pandas conversion failed")
mock_functions["to_pandas"].side_effect = pandas_error
- result = convert_to_output(
- sql_output_format="auto",
- to_polars=mock_functions["to_polars"],
- to_pandas=mock_functions["to_pandas"],
- )
+ with pytest.raises(ValueError) as exc_info:
+ convert_to_output(
+ sql_output_format="auto",
+ to_polars=mock_functions["to_polars"],
+ to_pandas=mock_functions["to_pandas"],
+ )
- assert result is None
- assert_only_one_called(mock_functions, "to_pandas")
- mock_logger.warning.assert_called_once_with(
- "Failed to convert dataframe", exc_info=pandas_error
- )
+ assert exc_info.value is pandas_error
+ if has_polars:
+ assert_multiple_called_once(
+ mock_functions, ["to_polars", "to_pandas"]
+ )
+ else:
+ assert_only_one_called(mock_functions, "to_pandas")
@patch("marimo._sql.utils.DependencyManager")
def test_without_polars_without_pandas(