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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
update default duckdb mo.sql deps (#9599)
Shahmir Varqhaedbb52649480
diff --git a/marimo/_sql/engines/types.py b/marimo/_sql/engines/types.pyindex a3082839d6..d62f1e61a8 100644--- a/marimo/_sql/engines/types.py+++ b/marimo/_sql/engines/types.py@@ -8,16 +8,12 @@ from typing import Any, Generic, Literal, TypeVarfrom marimo._config.config import SqlOutputTypefrom marimo._data.models import Database, DataTable, Schemafrom marimo._dependencies.dependencies import DependencyManager-from marimo._runtime.context.types import (- ContextNotInitializedError,- get_context,- runtime_context_installed,-)from marimo._sql.parse import (format_query_with_globals,replace_brackets_with_quotes,)from marimo._sql.utils import (+ get_configured_sql_output_format,is_query_empty,strip_explain_from_error_message,wrap_query_with_explain,@@ -134,13 +130,8 @@ class QueryEngine(BaseEngine[CONN], ABC):"""Execute a SQL query and return a dataframe."""def sql_output_format(self) -> SqlOutputType:- if runtime_context_installed():- try:- ctx = get_context()- return _validate_sql_output_format(ctx.app_config.sql_output)- except ContextNotInitializedError:- return "auto"- return "auto"+ configured_output_format = get_configured_sql_output_format()+ return _validate_sql_output_format(configured_output_format)def execute_in_explain_mode(self, query: str, globals_dict: dict[str, Any] | None = Nonediff --git a/marimo/_sql/sql.py b/marimo/_sql/sql.pyindex 5228b0254f..537a425c38 100644--- a/marimo/_sql/sql.py+++ b/marimo/_sql/sql.py@@ -2,9 +2,13 @@from __future__ import annotationsimport os-from typing import Any, Literal, cast+from typing import TYPE_CHECKING, Any, Literal, cast-from marimo._dependencies.dependencies import DependencyManager+if TYPE_CHECKING:+ from collections.abc import Callable++from marimo._config.config import SqlOutputType+from marimo._dependencies.dependencies import Dependency, DependencyManagerfrom marimo._output.rich_help import mddocfrom marimo._runtime.output import replacefrom marimo._sql.engines.dbapi import DBAPIConnection, DBAPIEngine@@ -15,10 +19,12 @@ from marimo._sql.error_utils import MarimoSQLException, is_sql_parse_errorfrom marimo._sql.get_engines import SUPPORTED_ENGINESfrom marimo._sql.utils import (extract_explain_content,+ get_configured_sql_output_format,is_explain_query,raise_df_import_error,)from marimo._types.ids import VariableName+from marimo._utils.assert_never import log_neverfrom marimo._utils.narwhals_utils import can_narwhalify_lazyframe@@ -27,6 +33,49 @@ def get_default_result_limit() -> int | None:return int(limit) if limit is not None else None+def _resolve_default_duckdb_deps(+ sql_output: SqlOutputType,+ *,+ polars_installed: Callable[[], bool],+ pandas_installed: Callable[[], bool],+) -> list[Dependency]:+ deps: list[Dependency] = [+ DependencyManager.duckdb,+ DependencyManager.sqlglot,+ ]+ polars_with_pyarrow = Dependency(+ "polars", pkg_name_to_install="polars[pyarrow]"+ )++ if sql_output == "polars" or sql_output == "lazy-polars":+ deps.append(polars_with_pyarrow)+ elif sql_output == "pandas":+ deps.append(DependencyManager.pandas)+ elif sql_output == "auto":+ if not polars_installed() and not pandas_installed():+ deps.append(polars_with_pyarrow)+ elif sql_output == "native":+ # "native" returns the underlying DuckDB relation and needs no df lib.+ pass+ else:+ log_never(sql_output)++ return deps+++def _default_duckdb_deps() -> list[Dependency]:+ """+ Return all deps required to run `mo.sql(query)` with the default DuckDB+ engine, including the dataframe library used for the configured output+ format.+ """+ return _resolve_default_duckdb_deps(+ get_configured_sql_output_format(),+ polars_installed=DependencyManager.polars.has,+ pandas_installed=DependencyManager.pandas.has,+ )++@mddocdef sql(query: str,@@ -63,8 +112,7 @@ def sql(if engine is None:DependencyManager.require_many("to execute sql",- DependencyManager.duckdb,- DependencyManager.sqlglot,+ *_default_duckdb_deps(),source="kernel",)sql_engine = DuckDBEngine(connection=None)diff --git a/marimo/_sql/utils.py b/marimo/_sql/utils.pyindex 2249fd8d0b..d98802a2ae 100644--- a/marimo/_sql/utils.py+++ b/marimo/_sql/utils.py@@ -14,6 +14,7 @@ from marimo._runtime._wasm._duckdb import (from marimo._runtime.context.types import (ContextNotInitializedError,get_context,+ runtime_context_installed,)if TYPE_CHECKING:@@ -31,6 +32,20 @@ CHEAP_DISCOVERY_DATABASES = ["duckdb", "sqlite", "mysql", "postgresql"]_NO_WASM_DUCKDB_RESULT = object()+def get_configured_sql_output_format() -> SqlOutputType:+ """Read the configured SQL output format from the runtime context.++ Returns "auto" when no runtime context is available (e.g. when `mo.sql(...)`+ is called outside of a marimo app).+ """+ if not runtime_context_installed():+ return "auto"+ try:+ return get_context().app_config.sql_output+ except ContextNotInitializedError:+ return "auto"++def _try_wasm_duckdb(method_name: str,query: str,diff --git a/tests/_sql/test_get_engines.py b/tests/_sql/test_get_engines.pyindex 62c580d0e2..80f9515fe1 100644--- a/tests/_sql/test_get_engines.py+++ b/tests/_sql/test_get_engines.py@@ -346,7 +346,11 @@ def test_get_engines_duckdb_databases() -> None:assert connection.default_database == "memory"assert connection.default_schema == "main"- sql("CREATE TABLE test_table (id INTEGER);")+ with patch(+ "marimo._sql.sql.get_configured_sql_output_format",+ return_value="native",+ ):+ sql("CREATE TABLE test_table (id INTEGER);")# Reload the connection to get the new tableconnection = engine_to_data_source_connection(@@ -370,7 +374,11 @@ def test_get_engines_duckdb_databases() -> None:sample_values=[],)]- sql("DROP TABLE test_table;")+ with patch(+ "marimo._sql.sql.get_configured_sql_output_format",+ return_value="native",+ ):+ sql("DROP TABLE test_table;")@pytest.mark.skipif(not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")diff --git a/tests/_sql/test_sql.py b/tests/_sql/test_sql.pyindex 3eb07aae08..2733c5bfce 100644--- a/tests/_sql/test_sql.py+++ b/tests/_sql/test_sql.py@@ -9,12 +9,18 @@ from unittest.mock import MagicMock, patchimport pytestfrom inline_snapshot import snapshot-from marimo._dependencies.dependencies import DependencyManager+from marimo._config.config import SqlOutputType+from marimo._dependencies.dependencies import Dependency, DependencyManager+from marimo._dependencies.errors import ManyModulesNotFoundErrorfrom marimo._output.formatting import Plainfrom marimo._plugins import uifrom marimo._sql.engines.ibis import IbisEnginefrom marimo._sql.engines.sqlalchemy import SQLAlchemyEngine-from marimo._sql.sql import _query_includes_limit, sql+from marimo._sql.sql import (+ _query_includes_limit,+ _resolve_default_duckdb_deps,+ sql,+)from marimo._sql.utils import (extract_explain_content,is_explain_query,@@ -24,7 +30,7 @@ from marimo._sql.utils import ()if TYPE_CHECKING:- from collections.abc import Generator+ from collections.abc import Callable, Generatorimport duckdbimport sqlalchemy as sa@@ -126,6 +132,178 @@ def test_sql_with_invalid_engine() -> None:sql("SELECT 1", engine="invalid")+def fake_dependency_has(monkeypatch: pytest.MonkeyPatch) -> set[str]:+ """Stub `Dependency.has` so the bundled-deps logic doesn't depend on the+ test environment having (or not having) duckdb/polars/etc. installed.++ The returned set acts as the "installed packages" registry — add a pkg name+ to it to make `Dependency(pkg).has()` return True.+ """+ installed: set[str] = set()++ def fake(self: Dependency, quiet: bool = False) -> bool:+ del quiet+ return self.pkg in installed++ monkeypatch.setattr(Dependency, "has", fake)+ for dep in vars(DependencyManager).values():+ if isinstance(dep, Dependency) and "has" in dep.__dict__:+ monkeypatch.delattr(dep, "has")+ return installed+++def fake_sql_output(monkeypatch: pytest.MonkeyPatch) -> dict[str, str]:+ """Stub the runtime-context lookup so we can drive `sql_output` without+ spinning up a kernel.++ `sql.py` and `engines/types.py` import `get_configured_sql_output_format`+ directly, so each consumer module holds its own binding and must be+ patched independently.+ """+ current = {"value": "auto"}++ def fake_format() -> str:+ return current["value"]++ for target in (+ "marimo._sql.sql.get_configured_sql_output_format",+ "marimo._sql.engines.types.get_configured_sql_output_format",+ ):+ monkeypatch.setattr(target, fake_format)+ return current+++class TestDefaultDuckDBDeps:+ """Tests for the pure `_resolve_default_duckdb_deps` helper. These are+ side-effect free: they don't touch global runtime state or the real+ `DependencyManager`, so they're immune to test-ordering issues.+ """++ @staticmethod+ def _pkg_names(deps: list[Dependency]) -> list[str]:+ return [dep.pkg_name_to_install or dep.pkg for dep in deps]++ @staticmethod+ def _const(value: bool) -> Callable[[], bool]:+ return lambda: value++ def test_auto_bundles_polars_when_no_df_lib_present(self) -> None:+ deps = _resolve_default_duckdb_deps(+ "auto",+ polars_installed=self._const(False),+ pandas_installed=self._const(False),+ )+ assert self._pkg_names(deps) == [+ "duckdb",+ "sqlglot",+ "polars[pyarrow]",+ ]++ def test_auto_skips_df_lib_when_pandas_already_installed(self) -> None:+ deps = _resolve_default_duckdb_deps(+ "auto",+ polars_installed=self._const(False),+ pandas_installed=self._const(True),+ )+ assert self._pkg_names(deps) == ["duckdb", "sqlglot"]++ def test_auto_skips_df_lib_when_polars_already_installed(self) -> None:+ deps = _resolve_default_duckdb_deps(+ "auto",+ polars_installed=self._const(True),+ pandas_installed=self._const(False),+ )+ assert self._pkg_names(deps) == ["duckdb", "sqlglot"]++ def test_auto_short_circuits_pandas_check_when_polars_present(+ self,+ ) -> None:+ """If polars is already installed, pandas.has() must not be invoked."""+ pandas_calls = 0++ def pandas_installed() -> bool:+ nonlocal pandas_calls+ pandas_calls += 1+ return False++ _resolve_default_duckdb_deps(+ "auto",+ polars_installed=self._const(True),+ pandas_installed=pandas_installed,+ )+ assert pandas_calls == 0++ def test_pandas_output_requires_pandas(self) -> None:+ deps = _resolve_default_duckdb_deps(+ "pandas",+ polars_installed=self._const(False),+ pandas_installed=self._const(False),+ )+ assert self._pkg_names(deps) == ["duckdb", "sqlglot", "pandas"]++ @pytest.mark.parametrize("output", ["polars", "lazy-polars"])+ def test_polars_outputs_require_polars_pyarrow(+ self, output: SqlOutputType+ ) -> None:+ deps = _resolve_default_duckdb_deps(+ output,+ polars_installed=self._const(False),+ pandas_installed=self._const(False),+ )+ assert self._pkg_names(deps) == [+ "duckdb",+ "sqlglot",+ "polars[pyarrow]",+ ]++ def test_native_output_skips_df_lib(self) -> None:+ deps = _resolve_default_duckdb_deps(+ "native",+ polars_installed=self._const(False),+ pandas_installed=self._const(False),+ )+ assert self._pkg_names(deps) == ["duckdb", "sqlglot"]++ @pytest.mark.parametrize(+ "output", ["polars", "lazy-polars", "pandas", "native"]+ )+ def test_non_auto_outputs_skip_has_checks(+ self, output: SqlOutputType+ ) -> None:+ """Non-auto branches must not invoke `.has()` callables at all."""++ def boom() -> bool:+ raise AssertionError(+ "has() should not be called for non-auto sql_output"+ )++ _resolve_default_duckdb_deps(+ output, polars_installed=boom, pandas_installed=boom+ )++[email protected]("fake_dependency_has")+class TestDefaultDuckDBDepsEndToEnd:+ """End-to-end: verify `mo.sql(...)` bundles all missing installs into a+ single ManyModulesNotFoundError, so the user only gets prompted once."""++ def test_sql_raises_single_bundled_error_when_all_missing(+ self, fake_sql_output: dict[str, str]+ ) -> None:+ fake_sql_output["value"] = "auto"+ with pytest.raises(ManyModulesNotFoundError) as excinfo:+ sql("SELECT 1")++ assert excinfo.value.package_names == [+ "duckdb",+ "sqlglot",+ "polars[pyarrow]",+ ]+ assert excinfo.value.source == "kernel"++@pytest.mark.skipif(not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")def test_empty_sql(sqlite_engine: sa.Engine, duckdb_connection: duckdb.DuckDBPyConnection