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.

improvement: track duckdb.sql and duckdb.execute SQL statements in dep graph (#2427)

Myles Scolnick846a8730c484
diff --git a/marimo/_ast/visitor.py b/marimo/_ast/visitor.py
index b66d308499..eb307176c8 100644
--- a/marimo/_ast/visitor.py
+++ b/marimo/_ast/visitor.py
@@ -378,16 +378,21 @@ class ScopedVisitor(ast.NodeVisitor):
def visit_Call(self, node: ast.Call) -> None:
# If the call name is sql and has one argument, and the argument is
# a string literal, then it's likely to be a SQL query.
- # It must also come from the `mo` module.
+ # It must also come from the `mo` or `duckdb` module.
#
# This check is brittle, since we can't detect at parse time whether
# 'mo'/'marimo' actually refer to the marimo library, but it gets
# the job done.
+ valid_sql_calls = [
+ "marimo.sql",
+ "mo.sql",
+ "duckdb.execute",
+ "duckdb.sql",
+ ]
if (
isinstance(node.func, ast.Attribute)
and isinstance(node.func.value, ast.Name)
- and (node.func.value.id == "mo" or node.func.value.id == "marimo")
- and node.func.attr == "sql"
+ and f"{node.func.value.id}.{node.func.attr}" in valid_sql_calls
and len(node.args) == 1
):
self.language = "sql"
diff --git a/tests/_ast/test_compiler.py b/tests/_ast/test_compiler.py
index 1073fe4bbf..0add58d903 100644
--- a/tests/_ast/test_compiler.py
+++ b/tests/_ast/test_compiler.py
@@ -273,6 +273,40 @@ class TestParseSQLCell:
assert cell.language == "sql"
assert not cell.variable_data
+ @staticmethod
+ @pytest.mark.parametrize(
+ "code",
+ [
+ 'duckdb.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")',
+ 'duckdb.execute("CREATE TABLE t1 (i INTEGER, j INTEGER)")',
+ ],
+ )
+ def test_table_definition_duckdb(code: str) -> None:
+ cell = compile_cell(code)
+ assert cell.key == hash(code)
+ assert cell.code == code
+ assert cell.defs == set(["t1"])
+ assert cell.refs == set(["duckdb"])
+ assert cell.language == "sql"
+ assert cell.variable_data == {"t1": [VariableData("table")]}
+
+ @staticmethod
+ @pytest.mark.parametrize(
+ "code",
+ [
+ 'duckdb.sql("SELECT * from t1")',
+ 'duckdb.execute("SELECT * from t1")',
+ ],
+ )
+ def test_table_reference_duckdb(code: str) -> None:
+ cell = compile_cell(code)
+ assert cell.key == hash(code)
+ assert cell.code == code
+ assert not cell.defs
+ assert cell.refs == set(["duckdb", "t1"])
+ assert cell.language == "sql"
+ assert not cell.variable_data
+
class TestCellFactory:
@staticmethod
diff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.py
index 175359bfae..069faf8f45 100644
--- a/tests/_ast/test_visitor.py
+++ b/tests/_ast/test_visitor.py
@@ -926,6 +926,22 @@ def test_sql_statement_with_marimo_sql() -> None:
assert v.refs == set(["cars", "marimo"])
[email protected](not HAS_DEPS, reason="Requires duckdb")
+ "code",
+ [
+ "df = duckdb.sql('select * from cars')",
+ "df = duckdb.execute('select * from cars')",
+ ],
+)
+def test_sql_statement_with_duckdb_sql(code: str) -> None:
+ v = visitor.ScopedVisitor()
+ mod = ast.parse(code)
+ v.visit(mod)
+ assert v.defs == set(["df"])
+ assert v.refs == set(["cars", "duckdb"])
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="Requires duckdb")
def test_sql_statement_with_f_string() -> None:
code = "\n".join(
@@ -974,11 +990,7 @@ def test_print_f_string() -> None:
@pytest.mark.skipif(not HAS_DEPS, reason="Requires duckdb")
def test_sql_empty_statement() -> None:
- code = "\n".join(
- [
- "mo.sql('')",
- ]
- )
+ code = "mo.sql('')"
v = visitor.ScopedVisitor()
mod = ast.parse(code)
v.visit(mod)
@@ -986,6 +998,16 @@ def test_sql_empty_statement() -> None:
assert v.refs == set(["mo"])
[email protected](not HAS_DEPS, reason="Requires duckdb")
+def test_sql_empty_statement_duckdb() -> None:
+ code = "duckdb.sql('')"
+ v = visitor.ScopedVisitor()
+ mod = ast.parse(code)
+ v.visit(mod)
+ assert v.defs == set([])
+ assert v.refs == set(["duckdb"])
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="Requires duckdb")
def test_sql_multiple_tables() -> None:
code = "\n".join(
diff --git a/tests/_runtime/test_dataflow.py b/tests/_runtime/test_dataflow.py
index 1bbe0da0b8..14a4666e7e 100644
--- a/tests/_runtime/test_dataflow.py
+++ b/tests/_runtime/test_dataflow.py
@@ -229,18 +229,37 @@ def test_register_with_stale_ancestor() -> None:
@pytest.mark.skipif(not HAS_DUCKDB, reason="duckdb is required")
class TestSQL:
- def test_sql_chain(self) -> None:
+ @pytest.mark.parametrize(
+ ("code1", "code2"),
+ [
+ (
+ 'mo.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")',
+ 'mo.sql("SELECT * from t1")',
+ ),
+ (
+ 'duckdb.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")',
+ 'duckdb.sql("SELECT * from t1")',
+ ),
+ (
+ 'duckdb.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")',
+ 'mo.sql("SELECT * from t1")',
+ ),
+ (
+ 'duckdb.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")',
+ 'duckdb.execute("SELECT * from t1")',
+ ),
+ ],
+ )
+ def test_sql_chain(self, code1: str, code2: str) -> None:
graph = dataflow.DirectedGraph()
- code = 'mo.sql("CREATE TABLE t1 (i INTEGER, j INTEGER)")'
- first_cell = parse_cell(code)
+ first_cell = parse_cell(code1)
graph.register_cell("0", first_cell)
assert graph.cells == {"0": first_cell}
assert graph.parents == {"0": set()}
assert graph.children == {"0": set()}
- code = 'mo.sql("SELECT * from t1")'
- second_cell = parse_cell(code)
+ second_cell = parse_cell(code2)
graph.register_cell("1", second_cell)
assert graph.cells == {"0": first_cell, "1": second_cell}
diff --git a/tests/_runtime/test_runtime.py b/tests/_runtime/test_runtime.py
index 49cae4df8d..f180bdabab 100644
--- a/tests/_runtime/test_runtime.py
+++ b/tests/_runtime/test_runtime.py
@@ -2157,6 +2157,43 @@ class TestSQL:
# cell 1 should re-run but will fail to find t1
assert "df" not in k.globals
+ async def test_sql_table_with_duckdb(self, k: Kernel) -> None:
+ await k.run(
+ [
+ ExecutionRequest(
+ cell_id="0",
+ code="import marimo as mo",
+ ),
+ ExecutionRequest(
+ cell_id="1", code="df = duckdb.sql('SELECT * from t1')"
+ ),
+ ]
+ )
+ assert "df" not in k.globals
+
+ await k.run(
+ [
+ ExecutionRequest(
+ cell_id="2",
+ code="import polars as pl; t1_df = pl.from_dict({'a': [42]})", # noqa: E501
+ ),
+ # cell 1 should automatically execute due to the definition of
+ # t1
+ ExecutionRequest(
+ cell_id="3",
+ code="duckdb.sql('CREATE OR REPLACE TABLE t1 as SELECT * FROM t1_df')", # noqa: E501
+ ),
+ ]
+ )
+
+ # make sure cell 1 executed, defining df
+ assert k.globals["t1_df"].to_dict(as_series=False) == {"a": [42]}
+
+ await k.delete_cell(DeleteCellRequest(cell_id="3"))
+ # t1 should be dropped since it's an in-memory table;
+ # cell 1 should re-run but will fail to find t1
+ assert "df" not in k.globals
+
async def test_sql_view(self, k: Kernel) -> None:
await k.run(
[