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 duckdb refs with empty quotes (#4195)

Myles Scolnick2b9d175b7725
diff --git a/marimo/_ast/sql_visitor.py b/marimo/_ast/sql_visitor.py
index f080e4e040..d0efe4f483 100644
--- a/marimo/_ast/sql_visitor.py
+++ b/marimo/_ast/sql_visitor.py
@@ -81,8 +81,7 @@ def normalize_sql_f_string(node: ast.JoinedStr) -> str:
return "null"
result = "".join(print_part(part) for part in node.values)
- # remove any double '' created by the f-string
- return result.replace("''", "'")
+ return result
class TokenExtractor:
@@ -326,8 +325,8 @@ def find_sql_refs(
"""
# Use sqlglot to parse ast (https://github.com/tobymao/sqlglot/blob/main/posts/ast_primer.md)
- if not DependencyManager.sqlglot.has():
- return []
+
+ DependencyManager.sqlglot.require(why="SQL parsing")
from sqlglot import exp, parse
from sqlglot.errors import ParseError
diff --git a/tests/_ast/test_sql_visitor.py b/tests/_ast/test_sql_visitor.py
index 37fcc901a2..a9a0c8aa1c 100644
--- a/tests/_ast/test_sql_visitor.py
+++ b/tests/_ast/test_sql_visitor.py
@@ -762,3 +762,16 @@ class TestFindSQLRefs:
"table2",
"table3",
]
+
+ @staticmethod
+ def test_find_sql_refs_with_regex() -> None:
+ # regex and replacement
+ sql = """
+ SELECT name, REGEXP_REPLACE(name, '^.*/', '') regex_name
+ FROM pdb.database
+ LIMIT 10;
+ """
+ assert find_sql_refs(sql) == [
+ "pdb",
+ "database",
+ ]
diff --git a/tests/_ast/test_visitor.py b/tests/_ast/test_visitor.py
index 6968066592..0c45f45a94 100644
--- a/tests/_ast/test_visitor.py
+++ b/tests/_ast/test_visitor.py
@@ -1041,6 +1041,19 @@ def test_print_f_string() -> None:
)
+def test_normalize_sql_f_string_with_empty_quotes() -> None:
+ import ast
+
+ joined_str = ast.parse(
+ "f'SELECT comment, REGEXP_REPLACE(comment, \\'*/.\\', \\'\\') regex_name,'"
+ )
+ assert isinstance(joined_str.body[0].value, ast.JoinedStr) # type: ignore
+ assert (
+ normalize_sql_f_string(joined_str.body[0].value)
+ == "SELECT comment, REGEXP_REPLACE(comment, '*/.', '') regex_name,"
+ ) # type: ignore
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="Requires duckdb")
def test_sql_empty_statement() -> None:
code = "mo.sql('')"