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
fix: adapt DuckDB relation and result handling for 2.0 (#11525)
Shahmir Varqha8bf43404b385
diff --git a/marimo/_data/charts.py b/marimo/_data/charts.pyindex fa16f22849..66567bc3bf 100644--- a/marimo/_data/charts.py+++ b/marimo/_data/charts.py@@ -360,6 +360,8 @@ class DateChartBuilder(ChartBuilder):# Get min and max dates using narwhalsmin_date = df[column].min()max_date = df[column].max()+ if min_date is None or max_date is None:+ return self.DEFAULT_DATE_FORMAT, self.DEFAULT_TIME_UNIT# Handle time-only dataif isinstance(min_date, time) and isinstance(max_date, time):diff --git a/marimo/_data/preview_column.py b/marimo/_data/preview_column.pyindex 1786b870d6..10b7b4fbd1 100644--- a/marimo/_data/preview_column.py+++ b/marimo/_data/preview_column.py@@ -187,8 +187,10 @@ def get_column_preview_for_duckdb(f"SELECT {column_name} FROM {fully_qualified_table_name}",connection=None,)+ if relation is None:+ raise ValueError("DuckDB returned no column-preview data")chart_spec = _get_chart_spec(- column_data=relation,+ column_data=relation.to_arrow_table(),column_type=column_type,column_name=column_name,chart_builder=get_chart_builder(diff --git a/marimo/_data/sql_summaries.py b/marimo/_data/sql_summaries.pyindex 92d891a06e..642b6eac3e 100644--- a/marimo/_data/sql_summaries.py+++ b/marimo/_data/sql_summaries.py@@ -72,9 +72,9 @@ def get_sql_stats(FROM {table_name}"""- stats_result: tuple[int, ...] | None = wrapped_sql(- stats_query, connection=None- ).fetchone()+ relation = wrapped_sql(stats_query, connection=None)+ rows = relation.fetchall() if relation is not None else []+ stats_result: tuple[int, ...] | None = rows[0] if rows else Noneif stats_result is None:raise ValueError(f"Column {column_name} not found in table {table_name}"@@ -149,10 +149,10 @@ def get_column_type(# DuckDB 1.0's pragma_table_info does not preserve escaped identifier quotes.if is_duckdb_v2():table_name = fully_qualified_table_name- column_info_result: tuple[str] | None = execute_duckdb_sql(- "SELECT type FROM pragma_table_info(?) WHERE name = ?",+ rows = execute_duckdb_sql(+ "SELECT type FROM pragma_table_info(?) WHERE name = ? LIMIT 1",[fully_qualified_table_name, column_name],- ).fetchone()+ ).fetchall()elif "." in fully_qualified_table_name:# Fully qualified table namedb_name, schema_name, table_name = parse_fully_qualified_table_name(@@ -165,11 +165,12 @@ def get_column_type(AND table_schema = $2AND table_catalog = $3AND column_name = $4+ LIMIT 1"""- column_info_result = execute_duckdb_sql(+ rows = execute_duckdb_sql(column_info_query,[table_name, schema_name, db_name, column_name],- ).fetchone()+ ).fetchall()else:# Simple table nametable_name = fully_qualified_table_name@@ -178,12 +179,14 @@ def get_column_type(FROM information_schema.columnsWHERE table_name = $1AND column_name = $2+ LIMIT 1"""- column_info_result = execute_duckdb_sql(+ rows = execute_duckdb_sql(column_info_query,[table_name, column_name],- ).fetchone()+ ).fetchall()+ column_info_result = rows[0] if rows else Noneif column_info_result is None:raise ValueError(f"Column {column_name} not found in table {table_name}"diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.pyindex c56308b57b..d339e2af1f 100644--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py@@ -239,9 +239,17 @@ class NarwhalsTableManager(indexed = relation.project(f"{columns}, row_number() OVER () - 1 AS {quoted_index}")- selected_columns = ", ".join(- f"source.{quote_sql_identifier(name, dialect='duckdb')}"- for name in relation.columns+ requested_index = index_name + "_requested"+ while requested_index.casefold() in occupied:+ requested_index += "_"+ requested_order = index_name + "_order"+ while requested_order.casefold() in occupied:+ requested_order += "_"+ quoted_requested_index = quote_sql_identifier(+ requested_index, dialect="duckdb"+ )+ quoted_requested_order = quote_sql_identifier(+ requested_order, dialect="duckdb")indexed, row_count = _snapshot_duckdb_relation(indexed)if max(indices) >= row_count:@@ -251,7 +259,7 @@ class NarwhalsTableManager(requested = relation.query(view,f"SELECT * FROM unnest([{', '.join(str(int(i)) for i in indices)}]) "- "WITH ORDINALITY AS selected(row_index, ordinal)",+ f"WITH ORDINALITY AS selected({quoted_requested_index}, {quoted_requested_order})",)finally:relation.query(view, f"DROP VIEW IF EXISTS {view}")@@ -259,10 +267,10 @@ class NarwhalsTableManager(indexed.set_alias("source").join(requested.set_alias("selected"),- f"source.{quoted_index} = selected.row_index",+ f"source.{quoted_index} = selected.{quoted_requested_index}",)- .order("selected.ordinal")- .project(selected_columns)+ .order(quoted_requested_order)+ .project(columns))return self.with_new_data(nw.from_native(selected, pass_through=False)@@ -314,6 +322,8 @@ class NarwhalsTableManager(def drop_columns(self, columns: list[str]) -> TableManager[Any]:if self.data.implementation.is_duckdb() and self._geometry_columns:+ if not set(columns).intersection(self.data.columns):+ return selfreturn self.select_columns([name for name in self.data.columns if name not in columns])diff --git a/marimo/_runtime/_wasm/_duckdb/dataframe.py b/marimo/_runtime/_wasm/_duckdb/dataframe.pyindex ff6c05f3b7..fe7cfcb9b7 100644--- a/marimo/_runtime/_wasm/_duckdb/dataframe.py+++ b/marimo/_runtime/_wasm/_duckdb/dataframe.py@@ -108,16 +108,23 @@ def _read_json_objects_path(path: str, options: Mapping[str, Any], function_name: str) -> pd.DataFrame:import duckdb+ import pandas as pd# DuckDB exposes JSON-object readers as SQL table functions, not Python# module methods, so invoke the table function with bound parameters.option_items = tuple(options.items())query_args = ["?"]query_args.extend(f"{key} := ?" for key, _ in option_items)- return duckdb.sql(+ relation = duckdb.sql(f"SELECT * FROM {function_name}({', '.join(query_args)})",params=[path, *(value for _, value in option_items)],- ).df()+ )+ if relation is None:+ raise ValueError("DuckDB JSON reader returned no relation")+ dataframe: object = relation.df()+ if not isinstance(dataframe, pd.DataFrame):+ raise TypeError("DuckDB JSON reader did not return a pandas DataFrame")+ return dataframedef read_text_dataframe(data: bytes, url: str) -> pd.DataFrame:diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex 57991b0eb7..16fcbd7508 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -483,12 +483,10 @@ def _get_attached_catalogs() -> set[str]:import duckdbtry:- return {- row[0].lower()- for row in duckdb.sql(- "SELECT database_name FROM duckdb_databases()"- ).fetchall()- }+ result = duckdb.sql("SELECT database_name FROM duckdb_databases()")+ if result is None:+ return set()+ return {row[0].lower() for row in result.fetchall()}except Exception:return set()diff --git a/marimo/_sql/bindings.py b/marimo/_sql/bindings.pyindex eb7c544285..473fbe6813 100644--- a/marimo/_sql/bindings.py+++ b/marimo/_sql/bindings.py@@ -60,30 +60,38 @@ def existing_sql_bindings(import duckdbtry:- database, schema = duckdb.sql(- "SELECT current_database(), current_schema()"- ).fetchone() or ("memory", "main")+ current = duckdb.sql("SELECT current_database(), current_schema()")+ if current is None:+ return []+ rows = current.fetchall()+ database, schema = rows[0] if rows else ("memory", "main")database, schema = database.lower(), schema.lower()- catalogs = {- row[0].lower()- for row in duckdb.sql(- "SELECT database_name FROM duckdb_databases()"- ).fetchall()- }+ catalog_result = duckdb.sql(+ "SELECT database_name FROM duckdb_databases()"+ )+ if catalog_result is None:+ return []+ catalogs = {row[0].lower() for row in catalog_result.fetchall()}+ schema_result = duckdb.sql(+ "SELECT database_name, schema_name FROM duckdb_schemas()"+ )+ if schema_result is None:+ return []schemas = {tuple(part.lower() for part in row)- for row in duckdb.sql(- "SELECT database_name, schema_name FROM duckdb_schemas()"- ).fetchall()+ for row in schema_result.fetchall()}+ relation_result = duckdb.sql(+ "SELECT database_name, schema_name, table_name, 'table' "+ "FROM duckdb_tables() UNION ALL "+ "SELECT database_name, schema_name, view_name, 'view' "+ "FROM duckdb_views()"+ )+ if relation_result is None:+ return []relations = {tuple(part.lower() for part in row)- for row in duckdb.sql(- "SELECT database_name, schema_name, table_name, 'table' "- "FROM duckdb_tables() UNION ALL "- "SELECT database_name, schema_name, view_name, 'view' "- "FROM duckdb_views()"- ).fetchall()+ for row in relation_result.fetchall()}except duckdb.Error:# An aborted transaction can make catalog inspection unavailable.diff --git a/marimo/_sql/engines/duckdb.py b/marimo/_sql/engines/duckdb.pyindex e2a8e58b24..e899e9e6cf 100644--- a/marimo/_sql/engines/duckdb.py+++ b/marimo/_sql/engines/duckdb.py@@ -84,7 +84,7 @@ class DuckDBEngine(SQLConnection[Optional["duckdb.DuckDBPyConnection"]]):@staticmethoddef execute_and_return_relation(query: str, params: list[Any] | None = None- ) -> duckdb.DuckDBPyRelation:+ ) -> duckdb.DuckDBPyRelation | None:"""Execute a query and return a relation. Supports parameters."""DependencyManager.duckdb.require("to execute sql")@@ -113,10 +113,15 @@ class DuckDBEngine(SQLConnection[Optional["duckdb.DuckDBPyConnection"]]):# Arrow PyCapsule path on older DuckDB or when pyarrow is missing.# batch_size of 100k bounds peak memory at ~10x less than DuckDB's# 1M default while keeping per-batch overhead negligible.+ import polars as pl+try:- return relation.pl(batch_size=100_000, lazy=True)+ result: object = relation.pl(batch_size=100_000, lazy=True)except (TypeError, ImportError, ModuleNotFoundError):return to_polars().lazy()+ if isinstance(result, pl.LazyFrame):+ return result+ return to_polars().lazy()return convert_to_output(sql_output_format=sql_output_format,diff --git a/marimo/_sql/parse.py b/marimo/_sql/parse.pyindex 34b7909d04..4ff920e138 100644--- a/marimo/_sql/parse.py+++ b/marimo/_sql/parse.py@@ -121,8 +121,8 @@ def _parse_sql_duckdb(except Exception as e:LOGGER.debug(f"Error sanitizing SQL query: {e}")- relation = duckdb.execute(json_serialize_query, [query])- fetch_result = relation.fetchone()+ rows = duckdb.execute(json_serialize_query, [query]).fetchall()+ fetch_result = rows[0] if rows else Noneif fetch_result is None:return None, "No result from DuckDB parse query"diff --git a/marimo/_sql/utils.py b/marimo/_sql/utils.pyindex 77e50f3d65..09e4c8d62e 100644--- a/marimo/_sql/utils.py+++ b/marimo/_sql/utils.py@@ -101,10 +101,18 @@ def _try_wasm_duckdb(return _NO_WASM_DUCKDB_RESULT if result is None else result.value+def _as_duckdb_relation(result: object) -> duckdb.DuckDBPyRelation | None:+ import duckdb++ if result is None or isinstance(result, duckdb.DuckDBPyRelation):+ return result+ raise TypeError("DuckDB SQL did not return a relation or None")++def wrapped_sql(query: str,connection: duckdb.DuckDBPyConnection | None,-) -> duckdb.DuckDBPyRelation:+) -> duckdb.DuckDBPyRelation | None:DependencyManager.duckdb.require("to execute sql")import duckdb@@ -117,6 +125,7 @@ def wrapped_sql(if connection is None:connection = cast(duckdb.DuckDBPyConnection, duckdb)+ relation: duckdb.DuckDBPyRelation | Nonetry:ctx = get_context()except ContextNotInitializedError:@@ -125,7 +134,7 @@ def wrapped_sql(# No WASM rewrite was needed; use DuckDB's normal SQL path.relation = connection.sql(query=query)else:- relation = cast(duckdb.DuckDBPyRelation, result)+ relation = _as_duckdb_relation(result)else:install_connection = (ctx.execution_context.with_connection@@ -147,7 +156,7 @@ def wrapped_sql({"query": query, "connection": connection},)else:- relation = cast(duckdb.DuckDBPyRelation, result)+ relation = _as_duckdb_relation(result)return relationdiff --git a/tests/_data/snapshots/column_preview_duckdb_bool_chart_spec.txt b/tests/_data/snapshots/column_preview_duckdb_bool_chart_spec.txtindex 5f0ea88e7b..8150568f0b 100644--- a/tests/_data/snapshots/column_preview_duckdb_bool_chart_spec.txt+++ b/tests/_data/snapshots/column_preview_duckdb_bool_chart_spec.txt@@ -7,13 +7,10 @@"background": "white","data": [{- "name": "data-834341051d0a090d8e3ab71fa64b81b9"+ "name": "source_0"},{- "name": "data_0"- },- {- "name": "data_1",+ "name": "data_0","values": [{"bool_col": true,@@ -32,7 +29,7 @@]},{- "name": "data_2",+ "name": "data_1","values": [{"bool_col": true,@@ -49,7 +46,7 @@]},{- "name": "data_2_color_domain_bool_col_0",+ "name": "data_1_color_domain_bool_col_0","values": [{"bool_col": true@@ -60,7 +57,7 @@]},{- "name": "data_1_color_domain_bool_col_1",+ "name": "data_0_color_domain_bool_col_1","values": [{"bool_col": true@@ -115,7 +112,7 @@}},"from": {- "data": "data_2"+ "data": "data_1"},"name": "layer_0_marks","style": [@@ -162,7 +159,7 @@}},"from": {- "data": "data_1"+ "data": "data_0"},"name": "layer_1_marks","style": [@@ -177,19 +174,19 @@"domain": {"fields": [{- "data": "data_2",+ "data": "data_1","field": "count_start"},{- "data": "data_2",+ "data": "data_1","field": "count_end"},{- "data": "data_1",+ "data": "data_0","field": "count_start"},{- "data": "data_1",+ "data": "data_0","field": "count_end"}]@@ -206,11 +203,11 @@"domain": {"fields": [{- "data": "data_2_color_domain_bool_col_0",+ "data": "data_1_color_domain_bool_col_0","field": "bool_col"},{- "data": "data_1_color_domain_bool_col_1",+ "data": "data_0_color_domain_bool_col_1","field": "bool_col"}],diff --git a/tests/_data/snapshots/column_preview_duckdb_categorical_chart_spec.txt b/tests/_data/snapshots/column_preview_duckdb_categorical_chart_spec.txtindex faaba167b6..adb6df91ea 100644--- a/tests/_data/snapshots/column_preview_duckdb_categorical_chart_spec.txt+++ b/tests/_data/snapshots/column_preview_duckdb_categorical_chart_spec.txt@@ -37,13 +37,10 @@},"data": [{- "name": "data-6f2678becc24b5ff9fbec3e85cf09cd0"+ "name": "source_0"},{- "name": "data_0"- },- {- "name": "data_2",+ "name": "data_1","values": [{"category": "A",@@ -68,7 +65,7 @@]},{- "name": "data_3",+ "name": "data_2","values": [{"category": "A",@@ -97,7 +94,7 @@]},{- "name": "data_0_y_domain_category",+ "name": "source_0_y_domain_category","values": [{"category": "A",@@ -146,7 +143,7 @@}},"from": {- "data": "data_3"+ "data": "data_2"},"name": "layer_0_marks","style": [@@ -187,7 +184,7 @@}},"from": {- "data": "data_2"+ "data": "data_1"},"name": "layer_1_marks","style": [@@ -202,15 +199,15 @@"domain": {"fields": [{- "data": "data_3",+ "data": "data_2","field": "count_start"},{- "data": "data_3",+ "data": "data_2","field": "count_end"},{- "data": "data_2",+ "data": "data_1","field": "count"}]@@ -228,7 +225,7 @@},{"domain": {- "data": "data_0_y_domain_category",+ "data": "source_0_y_domain_category","field": "category","sort": {"field": "sort_field",diff --git a/tests/_data/test_preview_column.py b/tests/_data/test_preview_column.pyindex 07f8198d72..af5b64071b 100644--- a/tests/_data/test_preview_column.py+++ b/tests/_data/test_preview_column.py@@ -284,6 +284,38 @@ def test_get_column_preview_for_duckdb_categorical() -> None:)[email protected]("duckdb", "altair", "pyarrow", "vegafusion")[email protected]("column_type", ["DATE", "TIMESTAMP"])[email protected]("row_count", [0, 3])+def test_get_column_preview_for_duckdb_missing_dates(+ column_type: str, row_count: int+) -> None:+ import duckdb++ table_name = "missing_dates_preview"+ duckdb.execute(+ f"CREATE OR REPLACE TABLE {table_name} AS "+ f"SELECT NULL::{column_type} AS value FROM range({row_count})"+ ).fetchall()+ try:+ result = get_column_preview_for_duckdb(+ fully_qualified_table_name=table_name,+ column_name="value",+ )+ assert result is not None+ assert result.error is None+ assert result.chart_spec is not None+ assert result.stats is not None+ assert (result.stats.total, result.stats.nulls) == (+ row_count,+ row_count if row_count else None,+ )+ assert duckdb.sql("SELECT 42").fetchall() == [(42,)]+ assert_serialize_roundtrip(result)+ finally:+ duckdb.execute(f"DROP TABLE {table_name}").fetchall()++@pytest.mark.skipif(not HAS_SQL_DEPS, reason="optional dependencies not installed")diff --git a/tests/_data/test_sql_summaries.py b/tests/_data/test_sql_summaries.pyindex f74e789e70..e31701713b 100644--- a/tests/_data/test_sql_summaries.py+++ b/tests/_data/test_sql_summaries.py@@ -213,3 +213,19 @@ def test_column_type_with_quoted_identifiers(schema: str, table: str) -> None:duckdb.execute(f"DROP SCHEMA IF EXISTS memory.{quote_sql_identifier(schema)} CASCADE")++[email protected](not HAS_DEPS, reason="duckdb not installed")[email protected]("column_name", ["id", "missing"])+def test_column_type_lookup_releases_connection_result(+ setup_test_db: Any, column_name: str+) -> None:+ import duckdb++ del setup_test_db+ if column_name == "missing":+ with pytest.raises(ValueError, match="Column missing not found"):+ get_column_type("test_table_3", column_name)+ else:+ assert get_column_type("test_table_3", column_name) == "integer"+ assert duckdb.sql("SELECT 42").fetchall() == [(42,)]diff --git a/tests/_plugins/ui/_impl/tables/test_geometry.py b/tests/_plugins/ui/_impl/tables/test_geometry.pyindex 88c22b2ad2..5272b6895c 100644--- a/tests/_plugins/ui/_impl/tables/test_geometry.py+++ b/tests/_plugins/ui/_impl/tables/test_geometry.py@@ -1052,7 +1052,15 @@ def test_duckdb_geometry_selection_rejects_out_of_bounds(@pytest.mark.requires("duckdb", "pyarrow")@pytest.mark.parametrize(- "name", ["__MARIMO_EXPORT_ROW", "__Marimo_Export_Row"]+ "name",+ [+ "__MARIMO_EXPORT_ROW",+ "__Marimo_Export_Row",+ "__MARIMO_EXPORT_ROW_REQUESTED",+ "__Marimo_Export_Row_Order",+ "row_index",+ "ordinal",+ ],)def test_duckdb_selection_avoids_case_insensitive_index_collision(name: str,diff --git a/tests/_runtime/test_duckdb_wasm.py b/tests/_runtime/test_duckdb_wasm.pyindex 63dbae77e2..3c609460e1 100644--- a/tests/_runtime/test_duckdb_wasm.py+++ b/tests/_runtime/test_duckdb_wasm.py@@ -18,6 +18,7 @@ from marimo._runtime._wasm._duckdb.sources import (remote_file_source_from_table,)from marimo._sql.engines.duckdb import DuckDBEngine+from marimo._sql.utils import is_duckdb_v2from tests.conftest import ExecReqProvider, mock_pyodidepytestmark = pytest.mark.requires("duckdb", "pandas", "sqlglot")@@ -349,6 +350,14 @@ def _query_parity_cases() -> list[QueryParityCase]:".geojson",b'{"type":"FeatureCollection","features":[]}',)+ populated_geojson = RemoteFixture(+ "https://example.com/populated.geojson",+ ".geojson",+ (+ b'{"type":"FeatureCollection","features":['+ b'{"type":"Feature","properties":{"name":"marimo"},"geometry":null}]}'+ ),+ )text = RemoteFixture("https://example.com/a.txt", ".txt", b"hello")blob = RemoteFixture("https://example.com/a.bin", ".bin", b"\x00\x01")return [@@ -491,9 +500,14 @@ def _query_parity_cases() -> list[QueryParityCase]:),QueryParityCase("geojson-reader",- f"SELECT type FROM read_json_auto('{geojson.url}')",+ f"SELECT * FROM read_json_auto('{geojson.url}')",(geojson,),),+ QueryParityCase(+ "populated-geojson-reader",+ f"SELECT * FROM read_json_auto('{populated_geojson.url}')",+ (populated_geojson,),+ ),QueryParityCase("text-and-blob",f"""@@ -778,9 +792,50 @@ class TestDuckDBWasmQueryPatch:assert (patch_result.query == "SELECT * FROM __marimo_wasm_duckdb_remote_0")- assert _records(next(iter(patch_result.tables.values()))) == [- {"type": "FeatureCollection", "features": []}- ]+ expected = (+ []+ if is_duckdb_v2()+ else [{"type": "FeatureCollection", "features": []}]+ )+ assert _records(next(iter(patch_result.tables.values()))) == expected++ @staticmethod+ def test_rewrites_populated_geojson_literal() -> None:+ with (+ mock_pyodide(),+ patch(+ "marimo._runtime._wasm._fetch.fetch_url_bytes",+ return_value=(+ b'{"type":"FeatureCollection","features":['+ b'{"type":"Feature","properties":{"name":"marimo"},"geometry":null}]}'+ ),+ ),+ ):+ patch_result = patch_duckdb_query_for_wasm(+ "FROM 'https://example.com/a.geojson'",+ )++ assert patch_result is not None+ assert (+ patch_result.query == "SELECT * FROM __marimo_wasm_duckdb_remote_0"+ )+ expected = (+ [{"geometry": None, "name": "marimo"}]+ if is_duckdb_v2()+ else [+ {+ "type": "FeatureCollection",+ "features": [+ {+ "type": "Feature",+ "properties": {"name": "marimo"},+ "geometry": None,+ }+ ],+ }+ ]+ )+ assert _records(next(iter(patch_result.tables.values()))) == expected@staticmethoddef test_avoids_reserved_table_names() -> None:@@ -924,7 +979,15 @@ class TestDuckDBWasmMoSqlIntegration:])- assert executing_kernel.globals["result"] is None+ result = executing_kernel.globals["result"]+ if is_duckdb_v2():+ import duckdb++ assert isinstance(result, duckdb.DuckDBPyRelation)+ assert result.columns == ["Count"]+ assert result.fetchall() == [(2,)]+ else:+ assert result is Noneassert executing_kernel.globals["rows"] == [("ford", 25),("toyota", 18),diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 5b8deeca67..1e6e47b688 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -3829,7 +3829,7 @@ class TestSQL:"WHERE table_catalog = 'memory' AND table_schema = ? ""AND table_name = 'manual-holdings'",[resolved_schema],- ).fetchone()+ ).fetchall()[0]assert row is not Nonereturn bool(row[0] > 0)@@ -3891,7 +3891,7 @@ class TestSQL:AND table_schema = 'main'AND table_name = 'holdings'"""- ).fetchone()+ ).fetchall()[0]assert row is not Nonereturn bool(row[0] > 0)@@ -3903,7 +3903,7 @@ class TestSQL:AND table_schema = 'other_db'AND table_name = 'holdings'"""- ).fetchone()+ ).fetchall()[0]assert row is not Nonereturn bool(row[0] > 0)@@ -3955,7 +3955,7 @@ class TestSQL:"SELECT count(*) FROM information_schema.tables ""WHERE table_catalog = 'memory' AND table_schema = 'main' ""AND table_name = 'holdings'"- ).fetchone()+ ).fetchall()[0]assert row is not Nonereturn bool(row[0] > 0)diff --git a/tests/_sql/test_duckdb.py b/tests/_sql/test_duckdb.pyindex 994e838bc1..27b2f5be37 100644--- a/tests/_sql/test_duckdb.py+++ b/tests/_sql/test_duckdb.py@@ -2,6 +2,7 @@from __future__ import annotations+import jsonimport sysfrom copy import deepcopyfrom typing import TYPE_CHECKING, Any, Literal@@ -9,13 +10,16 @@ from unittest import mockimport pytest+from marimo._config.config import SqlOutputTypefrom marimo._data.models import Database, DataTable, DataTableColumn, Schemafrom marimo._dependencies.dependencies import DependencyManager+from marimo._plugins.ui._impl.table import SearchTableArgs, tablefrom marimo._sql.engines.duckdb import DuckDBEnginefrom marimo._sql.engines.types import EngineCatalog, QueryEnginefrom marimo._sql.get_engines import engine_to_data_source_connectionfrom marimo._sql.sql import sqlfrom marimo._sql.sql_quoting import quote_qualified_name+from marimo._sql.utils import is_duckdb_v2from marimo._types.ids import VariableNameHAS_DUCKDB = DependencyManager.duckdb.has()@@ -51,7 +55,7 @@ def duckdb_connection() -> Generator[duckdb.DuckDBPyConnection, None, None]:(3, 'Charlie');""")- sql("INSERT INTO test (id, name) VALUES (4, 'Rose')", engine=conn)+ conn.execute("INSERT INTO test (id, name) VALUES (4, 'Rose')").fetchall()yield connconn.execute("DROP TABLE test")conn.close()@@ -156,6 +160,8 @@ def test_duckdb_engine_get_databases(@pytest.mark.skipif(not HAS_DUCKDB, reason="DuckDB not installed")def test_duckdb_engine_get_databases_no_conn() -> None:"""Test DuckDBEngine get_databases method."""+ import duckdb+engine = DuckDBEngine(None, engine_name=None)initial_databases = engine.get_databases(include_schemas=False,@@ -171,18 +177,18 @@ def test_duckdb_engine_get_databases_no_conn() -> None:assert engine.get_default_database() == "memory"assert engine.get_default_schema() == "main"- engine.execute(+ duckdb.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, name VARCHAR(255))"- )+ ).fetchall()try:- engine.execute(+ duckdb.execute("""INSERT INTO test VALUES(1, 'Alice'),(2, 'Bob'),(3, 'Charlie');"""- )+ ).fetchall()databases = engine.get_databases(include_schemas=True,include_tables=True,@@ -204,7 +210,7 @@ def test_duckdb_engine_get_databases_no_conn() -> None:),]finally:- engine.execute("DROP TABLE test")+ duckdb.execute("DROP TABLE test").fetchall()@pytest.mark.skipif(not HAS_DUCKDB, reason="duckdb not installed")@@ -242,15 +248,17 @@ def test_get_current_database_schema() -> None:assert duckdb_engine.get_default_database() == "memory"assert duckdb_engine.get_default_schema() == "main"- sql("CREATE SCHEMA test_schema;", engine=engine)- sql("CREATE TABLE test_schema.test_table (id INTEGER);", engine=engine)- sql("USE test_schema;", engine=engine)+ engine.execute("CREATE SCHEMA test_schema;").fetchall()+ engine.execute(+ "CREATE TABLE test_schema.test_table (id INTEGER);"+ ).fetchall()+ engine.execute("USE test_schema;").fetchall()assert duckdb_engine.get_default_database() == "memory"assert duckdb_engine.get_default_schema() == "test_schema"- sql("DROP TABLE test_schema.test_table;", engine=engine)- sql("DROP SCHEMA test_schema;", engine=engine)+ engine.execute("DROP TABLE test_schema.test_table;").fetchall()+ engine.execute("DROP SCHEMA test_schema;").fetchall()@pytest.mark.skipif(@@ -837,7 +845,7 @@ def test_duckdb_keeps_tables_when_column_discovery_fails(failure: str) -> None:if failure == "placeholder":oid = connection.execute("SELECT oid FROM duckdb_schemas() WHERE database_name = 'memory' AND schema_name = 'main'"- ).fetchone()[0]+ ).fetchall()[0][0]return [("memory", oid, "example", "__", "INTEGER")]return []return query(sql, params)@@ -952,3 +960,87 @@ def test_duckdb_nested_schema_discovery(include_tables: bool) -> None:)== [])++[email protected]("duckdb", "pandas", "polars", "pyarrow")+ "output_format", ["native", "polars", "lazy-polars", "pandas"]+)[email protected]("output", [False, True])+def test_duckdb_command_results(+ output_format: SqlOutputType, output: bool+) -> None:+ import duckdb+ import pandas as pd+ import polars as pl++ def assert_rows(result: object, expected: list[tuple[int]]) -> None:+ if output_format == "native":+ assert isinstance(result, duckdb.DuckDBPyRelation)+ assert result.columns == ["Count"]+ assert result.fetchall() == expected+ elif output_format == "pandas":+ assert isinstance(result, pd.DataFrame)+ assert list(result.columns) == ["Count"]+ assert list(result.itertuples(index=False, name=None)) == expected+ else:+ if output_format == "lazy-polars":+ assert isinstance(result, pl.LazyFrame)+ result = result.collect()+ assert isinstance(result, pl.DataFrame)+ assert result.columns == ["Count"]+ assert result.rows() == expected++ with (+ duckdb.connect() as connection,+ mock.patch.object(+ DuckDBEngine, "sql_output_format", return_value=output_format+ ),+ mock.patch("marimo._sql.sql.replace") as replace,+ mock.patch(+ "marimo._output.formatters.df_formatters.include_opinionated",+ return_value=True,+ ),+ ):+ connection.execute("CREATE SEQUENCE command_counter")+ commands = [+ (+ (+ "CREATE TABLE command_result AS "+ "SELECT nextval('command_counter') AS value FROM range(2)"+ ),+ 2,+ ),+ ("INSERT INTO command_result VALUES (3)", 1),+ ]+ for query, count in commands:+ replace.reset_mock()+ result = sql(query, engine=connection, output=output)+ if is_duckdb_v2():+ assert_rows(result, [(count,)])+ assert replace.call_count == int(output)+ if output:+ rendered = replace.call_args.args[0]+ assert isinstance(rendered, table)+ page = rendered._search(+ SearchTableArgs(page_size=10, page_number=0)+ )+ assert json.loads(page.data) == [{"Count": count}]+ else:+ assert result is None+ replace.assert_not_called()+ assert connection.execute("SELECT 42").fetchall() == [(42,)]+ assert_rows(+ sql(+ 'INSERT INTO command_result VALUES (17) RETURNING value AS "Count"',+ engine=connection,+ output=output,+ ),+ [(17,)],+ )+ assert connection.execute(+ "SELECT value FROM command_result ORDER BY value"+ ).fetchall() == [(1,), (2,), (3,), (17,)]+ assert connection.execute(+ "SELECT currval('command_counter')"+ ).fetchall() == [(2,)]diff --git a/tests/_sql/test_get_engines.py b/tests/_sql/test_get_engines.pyindex 529b6b1a78..409f0c21f3 100644--- a/tests/_sql/test_get_engines.py+++ b/tests/_sql/test_get_engines.py@@ -22,7 +22,6 @@ from marimo._sql.get_engines import (engine_to_data_source_connection,get_engines_from_variables,)-from marimo._sql.sql import sqlfrom marimo._types.ids import VariableNameHAS_SQLALCHEMY = DependencyManager.sqlalchemy.has()@@ -330,6 +329,8 @@ def test_get_engines_from_variables_sqlite3_is_not_adbc() -> None:@pytest.mark.skipif(not HAS_DUCKDB, reason="DuckDB not installed")def test_get_engines_duckdb_databases() -> None:+ import duckdb+duckdb_engine = DuckDBEngine(None)# Test display name when the name is DEFAULT_ENGINE_NAME@@ -350,11 +351,7 @@ def test_get_engines_duckdb_databases() -> None:assert connection.default_database == "memory"assert connection.default_schema == "main"- with patch(- "marimo._sql.sql.get_configured_sql_output_format",- return_value="native",- ):- sql("CREATE TABLE test_table (id INTEGER);")+ duckdb.execute("CREATE TABLE test_table (id INTEGER);").fetchall()try:# Reload the connection to get the new table@@ -377,11 +374,7 @@ def test_get_engines_duckdb_databases() -> None:assert table.columns == []assert table.num_columns is Nonefinally:- with patch(- "marimo._sql.sql.get_configured_sql_output_format",- return_value="native",- ):- sql("DROP TABLE test_table;")+ duckdb.execute("DROP TABLE test_table;").fetchall()@pytest.mark.skipif(not HAS_SQLALCHEMY, reason="SQLAlchemy not installed")diff --git a/tests/_sql/test_sql.py b/tests/_sql/test_sql.pyindex ce1786d5bd..eb350590a9 100644--- a/tests/_sql/test_sql.py+++ b/tests/_sql/test_sql.py@@ -76,24 +76,22 @@ def duckdb_connection() -> Generator[duckdb.DuckDBPyConnection, None, None]:import duckdbconn = duckdb.connect(":memory:")- sql(+ conn.execute("""CREATE TABLE test (id INTEGER PRIMARY KEY,name TEXT);- """,- engine=conn,- )- sql(+ """+ ).fetchall()+ conn.execute("""INSERT INTO test VALUES(1, 'Alice'),(2, 'Bob'),(3, 'Charlie');- """,- engine=conn,- )+ """+ ).fetchall()yield connconn.close()@@ -693,7 +691,7 @@ logical_planwith duckdb.connect(":memory:") as conn:conn.sql("CREATE TABLE t AS SELECT * FROM range(5) tbl(id)")query = "EXPLAIN SELECT * FROM t"- expected = conn.execute(query).fetchone()+ expected = conn.execute(query).fetchall()[0]assert expected is not Noneresult = extract_explain_content(conn.sql(query))diff --git a/tests/_sql/test_sql_error_handling.py b/tests/_sql/test_sql_error_handling.pyindex 5a7a4f6738..0863318666 100644--- a/tests/_sql/test_sql_error_handling.py+++ b/tests/_sql/test_sql_error_handling.py@@ -54,11 +54,9 @@ class TestDuckDBRuntimeErrors:@pytest.mark.skipif(not HAS_DUCKDB, reason="DuckDB not installed")def test_column_not_found_error(self, duckdb_conn):"""Test error when referencing a non-existent column."""- # Create a test table first- sql(- "CREATE OR REPLACE TABLE test_error_table (id INTEGER, name TEXT)",- engine=duckdb_conn,- )+ duckdb_conn.execute(+ "CREATE OR REPLACE TABLE test_error_table (id INTEGER, name TEXT)"+ ).fetchall()with pytest.raises(MarimoSQLException) as exc_info:sql(diff --git a/tests/_sql/test_sql_parse.py b/tests/_sql/test_sql_parse.pyindex 416fac339e..038d6763aa 100644--- a/tests/_sql/test_sql_parse.py+++ b/tests/_sql/test_sql_parse.py@@ -959,3 +959,15 @@ class TestFormatQueryWithGlobals:query, {"col": "id"}, lambda key: key.upper())assert result_with_uppercase == "SELECT id FROM TABLE"++[email protected]("duckdb")[email protected]("query", ["SELECT 1", "SELECT FROM"])+def test_parse_releases_connection_result(query: str) -> None:+ import duckdb++ result, error = parse_sql(query, "duckdb")+ assert error is None+ assert result is not None+ assert result.success == (query == "SELECT 1")+ assert duckdb.sql("SELECT 42").fetchall() == [(42,)]