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(runtime): quote DuckDB identifiers when dropping in-memory tables (#10341)
Abdul Rehmanf5359203b311
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.pyindex a271799c12..b39253c73e 100644--- a/marimo/_runtime/runtime.py+++ b/marimo/_runtime/runtime.py@@ -156,6 +156,10 @@ from marimo._sql.engines.types import (from marimo._sql.get_engines import (get_engines_from_variables,)+from marimo._sql.sql_quoting import (+ quote_qualified_name,+ quote_sql_identifier,+)from marimo._tracer import attach_trace_context, kernel_tracerfrom marimo._types.ids import CellId_t, UIElementId, VariableNamefrom marimo._types.lifespan import Lifespan@@ -1007,7 +1011,8 @@ class Kernel:# We only drop in-memory tables: we don't want to drop tables# on databases!try:- duckdb.execute(f"DROP TABLE IF EXISTS memory.main.{name}")+ qualified = quote_qualified_name("memory", "main", name)+ duckdb.execute(f"DROP TABLE IF EXISTS {qualified}")except Exception as e:LOGGER.warning("Failed to drop table %s: %s", name, str(e))elif variable.kind == "view" and DependencyManager.duckdb.has():@@ -1015,14 +1020,16 @@ class Kernel:# We only drop in-memory views for the same reason.try:- duckdb.execute(f"DROP VIEW IF EXISTS memory.main.{name}")+ qualified = quote_qualified_name("memory", "main", name)+ duckdb.execute(f"DROP VIEW IF EXISTS {qualified}")except Exception as e:LOGGER.warning("Failed to drop view %s: %s", name, str(e))elif variable.kind == "catalog" and DependencyManager.duckdb.has():import duckdbtry:- duckdb.execute(f"DETACH DATABASE IF EXISTS {name}")+ identifier = quote_sql_identifier(name)+ duckdb.execute(f"DETACH DATABASE IF EXISTS {identifier}")except Exception as e:LOGGER.warning("Failed to detach catalog %s: %s", name, str(e)diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.pyindex 59bc726d06..20a8ef30af 100644--- a/tests/_runtime/test_runtime.py+++ b/tests/_runtime/test_runtime.py@@ -3512,6 +3512,43 @@ class TestSQL:# cell 1 should re-run but will fail to find t1assert "df" not in k.globals+ async def test_sql_table_with_special_char_name_is_dropped(+ self, k: Kernel+ ) -> None:+ # Regression test for #10338: an in-memory table whose name needs+ # quoting (e.g. a hyphen) must still be cleaned up. The name was+ # previously interpolated raw, producing invalid SQL+ # (`DROP TABLE IF EXISTS memory.main.manual-holdings`).+ import duckdb++ def table_exists() -> bool:+ return (+ duckdb.execute(+ "SELECT count(*) FROM information_schema.tables "+ "WHERE table_name = 'manual-holdings'"+ ).fetchone()[0]+ > 0+ )++ await k.run(+ [+ ExecuteCellCommand(cell_id="0", code="import marimo as mo"),+ ExecuteCellCommand(+ cell_id="1",+ code=(+ 'mo.sql(\'CREATE OR REPLACE TABLE "manual-holdings" '+ "AS SELECT 1 AS a')"+ ),+ ),+ ]+ )+ assert not k.errors+ assert table_exists()++ # Deleting the defining cell triggers cleanup of the in-memory table.+ await k.delete_cell(DeleteCellCommand(cell_id="1"))+ assert not table_exists()+async def test_sql_query_as_local_df(self, k: Kernel) -> None:await k.run([