Skip to content

Conversation

@bxff
Copy link

@bxff bxff commented Feb 1, 2026

📝 Summary

Closes #8080

🔍 Description of Changes

Problem: Marimo's SQL static analysis fails with sqlglot.errors.OptimizeError: Alias already used when parsing valid DuckDB syntax using unnest with JSON access (e.g., SELECT unnest([1,2,3])->>'$' FROM (SELECT 1)).

Root Cause: The build_scope() function from sqlglot's optimizer raises an OptimizeError when SQL contains duplicate aliases. This occurs with:

  • Multiple unnest expressions with JSON access that generate identical internal aliases
  • Cross-joined subqueries with the same column alias (e.g., SELECT * FROM (SELECT 1 as x), (SELECT 2 as x))

This is valid DuckDB syntax but not handled by sqlglot's scope optimizer.

Solution: Wrap the build_scope() call in find_sql_refs() with a try-except block. When OptimizeError is caught, fall back to extracting table references directly using expression.find_all(exp.Table) without scope analysis.

Changes:

  • marimo/_ast/sql_visitor.py: Added error handling for OptimizeError with fallback to direct table extraction
  • tests/_ast/test_sql_visitor.py: Added 3 test cases covering the regression and fallback behavior

📋 Checklist

  • I have read the contributor guidelines.
  • For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on Discord, or the community discussions (Please provide a link if applicable).
  • Tests have been added for the changes made.
  • Documentation has been updated where applicable, including docstrings for API changes.
  • Pull request title is a good summary of the changes - it will be used in the release notes.
@bxff bxff requested a review from dmadisetti as a code owner February 1, 2026 01:18
@vercel
Copy link

vercel bot commented Feb 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Feb 1, 2026 1:19am

Request Review

@github-actions
Copy link

github-actions bot commented Feb 1, 2026

All contributors have signed the CLA ✍️ ��
Posted by the CLA Assistant Lite bot.

@bxff
Copy link
Author

bxff commented Feb 1, 2026

I have read the CLA Document and I hereby sign the CLA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant