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: fallback to pandas sql (#4271)

Shahmir Varqha0e0b38dedb20
diff --git a/marimo/_sql/engines/duckdb.py b/marimo/_sql/engines/duckdb.py
index 2950bbd454..fc438fdf10 100644
--- a/marimo/_sql/engines/duckdb.py
+++ b/marimo/_sql/engines/duckdb.py
@@ -52,11 +52,23 @@ class DuckDBEngine(SQLEngine):
return None
if DependencyManager.polars.has():
- return relation.pl()
- elif DependencyManager.pandas.has():
- return relation.df()
- else:
- raise_df_import_error("polars[pyarrow]")
+ import polars as pl
+
+ try:
+ return relation.pl()
+ except (pl.exceptions.PanicException, pl.exceptions.ComputeError):
+ LOGGER.info(
+ "Failed to convert to polars, falling back to pandas"
+ )
+
+ if DependencyManager.pandas.has():
+ try:
+ return relation.df()
+ except Exception as e:
+ LOGGER.warning("Failed to convert dataframe", exc_info=e)
+ return None
+
+ raise_df_import_error("polars[pyarrow]")
@staticmethod
def is_compatible(var: Any) -> bool:
diff --git a/marimo/_sql/engines/sqlalchemy.py b/marimo/_sql/engines/sqlalchemy.py
index 4fc0340d08..6890782b66 100644
--- a/marimo/_sql/engines/sqlalchemy.py
+++ b/marimo/_sql/engines/sqlalchemy.py
@@ -83,11 +83,24 @@ class SQLAlchemyEngine(SQLEngine):
if DependencyManager.polars.has():
import polars as pl
- return pl.DataFrame(rows) # type: ignore
- else:
+ try:
+ return pl.DataFrame(rows) # type: ignore
+ except (
+ pl.exceptions.PanicException,
+ pl.exceptions.ComputeError,
+ ):
+ LOGGER.info(
+ "Failed to convert to polars, falling back to pandas"
+ )
+
+ if DependencyManager.pandas.has():
import pandas as pd
- return pd.DataFrame(rows)
+ try:
+ return pd.DataFrame(rows)
+ except Exception as e:
+ LOGGER.warning("Failed to convert dataframe", exc_info=e)
+ return None
@staticmethod
def is_compatible(var: Any) -> bool:
diff --git a/tests/_sql/test_duckdb.py b/tests/_sql/test_duckdb.py
index 3c45e9705b..a563575e16 100644
--- a/tests/_sql/test_duckdb.py
+++ b/tests/_sql/test_duckdb.py
@@ -14,6 +14,7 @@ from marimo._sql.sql import sql
HAS_DUCKDB = DependencyManager.duckdb.has()
HAS_PANDAS = DependencyManager.pandas.has()
+HAS_POLARS = DependencyManager.polars.has()
if TYPE_CHECKING:
from collections.abc import Generator
@@ -185,3 +186,18 @@ def test_get_current_database_schema() -> None:
sql("DROP TABLE test_schema.test_table;", engine=engine)
sql("DROP SCHEMA test_schema;", engine=engine)
+
+
+ not HAS_DUCKDB or not HAS_POLARS or not HAS_PANDAS,
+ reason="duckdb, polars and pandas not installed",
+)
+def test_duckdb_engine_execute_polars_fallback() -> None:
+ import pandas as pd
+
+ engine = DuckDBEngine(None)
+ # This dtype is currently not supported by polars
+ result = engine.execute(
+ "select to_days(cast((current_date - DATE '2025-01-01') as INTEGER));"
+ )
+ assert isinstance(result, pd.DataFrame)