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: handle sqlglot OptimizeError for DuckDB unnest with JSON access (#8083)

Dex Devlonc374384799be
diff --git a/marimo/_ast/sql_visitor.py b/marimo/_ast/sql_visitor.py
index 7437de33ea..f4ac96f37b 100644
--- a/marimo/_ast/sql_visitor.py
+++ b/marimo/_ast/sql_visitor.py
@@ -504,6 +504,7 @@ def find_sql_refs(sql_statement: str) -> set[SQLRef]:
DependencyManager.sqlglot.require(why="SQL parsing")
from sqlglot import exp, parse
+ from sqlglot.errors import OptimizeError
from sqlglot.optimizer.scope import build_scope
def get_ref_from_table(table: exp.Table) -> Optional[SQLRef]:
@@ -553,12 +554,25 @@ def find_sql_refs(sql_statement: str) -> set[SQLRef]:
if ref := get_ref_from_table(table):
refs.add(ref)
- # build_scope only works for select statements
- if root := build_scope(expression):
- for scope in root.traverse(): # type: ignore
- for _node, source in scope.selected_sources.values():
- if isinstance(source, exp.Table):
- if ref := get_ref_from_table(source):
- refs.add(ref)
+ # build_scope only works for select statements.
+ # It may raise OptimizeError for valid SQL with duplicate aliases
+ # (e.g., "SELECT * FROM (SELECT 1 as x), (SELECT 2 as x)")
+ # In that case, fall back to extracting table references directly.
+ try:
+ if root := build_scope(expression):
+ for scope in root.traverse(): # type: ignore
+ for _node, source in scope.selected_sources.values():
+ if isinstance(source, exp.Table):
+ if ref := get_ref_from_table(source):
+ refs.add(ref)
+ except OptimizeError:
+ # Fall back to extracting table references without scope analysis.
+ # This can happen with valid SQL that has duplicate aliases
+ # (e.g., cross-joined subqueries with the same column alias).
+ # We prefer build_scope when possible because it correctly handles
+ # CTEs - find_all would incorrectly report CTE names as table refs.
+ for table in expression.find_all(exp.Table):
+ if ref := get_ref_from_table(table):
+ refs.add(ref)
return refs
diff --git a/tests/_ast/test_sql_visitor.py b/tests/_ast/test_sql_visitor.py
index 1c43f24c67..33d2374626 100644
--- a/tests/_ast/test_sql_visitor.py
+++ b/tests/_ast/test_sql_visitor.py
@@ -897,3 +897,47 @@ class TestFindSQLRefs:
assert find_sql_refs(sql) == {
SQLRef(table="test_table", schema="my_schema")
}
+
+ def test_unnest_with_json_access(self) -> None:
+ # Regression test for issue #8080
+ # sqlglot's build_scope raises OptimizeError for unnest with JSON access
+ # because it generates duplicate empty aliases internally
+ sql = "SELECT unnest([1,2,3])->>'$' FROM (SELECT 1)"
+ # Should not raise OptimizeError; falls back to direct table extraction
+ assert find_sql_refs(sql) == set()
+
+ def test_duplicate_alias_in_subqueries(self) -> None:
+ # sqlglot's build_scope raises OptimizeError for duplicate aliases
+ # in cross-joined subqueries which is valid DuckDB syntax
+ sql = "SELECT * FROM (SELECT 1 as x), (SELECT 2 as x)"
+ # Should not raise OptimizeError; falls back to direct table extraction
+ assert find_sql_refs(sql) == set()
+
+ def test_duplicate_alias_with_table_refs(self) -> None:
+ # Test that table refs are still extracted when OptimizeError occurs
+ sql = """
+ SELECT * FROM (SELECT * FROM table1),
+ (SELECT * FROM table2 as x),
+ (SELECT * FROM table3 as x)
+ """
+ # Should not raise OptimizeError and should extract table refs
+ assert find_sql_refs(sql) == {
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
+ SQLRef(table="table3"),
+ }
+
+ def test_multiple_statements_with_optimize_error(self) -> None:
+ # Verify that OptimizeError in one statement doesn't affect others.
+ # The try/except is inside the loop, so each statement is independent.
+ sql = """
+ SELECT * FROM table1;
+ SELECT * FROM (SELECT 1 as x), (SELECT 2 as x);
+ SELECT * FROM table2;
+ """
+ # table1 and table2 should be extracted normally via build_scope;
+ # the middle statement falls back but has no table refs
+ assert find_sql_refs(sql) == {
+ SQLRef(table="table1"),
+ SQLRef(table="table2"),
+ }