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: preserve SQL definitions around DuckDB comment tokens (#11137)

Shahmir Varqhaca6dd42d6f96
diff --git a/marimo/_ast/sql_visitor.py b/marimo/_ast/sql_visitor.py
index 3d78864968..4e0decbd23 100644
--- a/marimo/_ast/sql_visitor.py
+++ b/marimo/_ast/sql_visitor.py
@@ -184,6 +184,10 @@ def find_sql_defs(sql_statement: str) -> SQLDefs:
import duckdb
tokens = duckdb.tokenize(sql_statement)
+ # DuckDB 2 emits comments as tokens; definition matching needs only syntax.
+ comment_type = getattr(duckdb.token_type, "comment", None)
+ if comment_type is not None:
+ tokens = [token for token in tokens if token[1] != comment_type]
token_extractor = _TokenExtractor(
sql_statement=sql_statement, tokens=tokens
)
diff --git a/tests/_ast/test_sql_visitor.py b/tests/_ast/test_sql_visitor.py
index 68eac28b59..c9cf691b39 100644
--- a/tests/_ast/test_sql_visitor.py
+++ b/tests/_ast/test_sql_visitor.py
@@ -293,6 +293,18 @@ class TestFindSQLDefs:
name VARCHAR
) -- Comment at the end
""",
+ """
+ CREATE/* after CREATE */TABLE/* after TABLE */
+ IF/* after IF */NOT/* after NOT */EXISTS
+ -- Before the table name
+ my_table (id INT)
+ """,
+ """
+ CREATE -- Before OR
+ OR /* Before REPLACE */ REPLACE
+ /* Before TEMP */ TEMP -- Before TABLE
+ TABLE /* Before the table name */ my_table (id INT)
+ """,
],
)
def test_find_sql_defs_many_comments(
@@ -390,10 +402,18 @@ class TestFindSQLDefs:
)
@staticmethod
- def test_find_sql_defs_with_catalog_and_schema() -> None:
- sql = """
- CREATE TABLE my_catalog.my_schema.my_table (id INT);
- """
+ @pytest.mark.parametrize(
+ "sql",
+ [
+ "CREATE TABLE my_catalog.my_schema.my_table (id INT);",
+ """
+ CREATE TABLE my_catalog /* Before the schema */ . my_schema
+ -- Before the table
+ . my_table (id INT);
+ """,
+ ],
+ )
+ def test_find_sql_defs_with_catalog_and_schema(sql: str) -> None:
assert find_sql_defs(sql) == SQLDefs(
tables=[
SQLRef(