ISSUE DESCRIPTION 🐛 Bug ❓: Variable name collision for SQL cells using same table name…
- User
ISSUE DESCRIPTION
🐛 Bug ❓: Variable name collision for SQL cells using same table name in different schemas
Discussed in https://github.com/marimo-team/marimo/discussions/9734
Originally posted by EED85 May 31, 2026
Description
When using multiple SQL cells in marimo with DuckDB, creating tables with the same name in different schemas leads to a variable redefinition error.
Although the tables are fully qualified and valid in DuckDB, marimo raises an error due to internal variable name collisions.
Minimal Reproducible Example
sql-- Cell 1 CREATE TABLE finance.datalake.xx (id INT);sql-- Cell 2 CREATE TABLE finance.information_layer.xx (id INT);ERROR
codeThis cell wasn't run because it has errors This cell redefines variables from other cells. 'xx' was also defined by: dl Fix: Wrap in a function Why can't I redefine variables?Expected Behavior
Since the tables are created in different schemas, both statements should execute without conflict:
Environment
coderequires-python = ">=3.13" dependencies = [ "duckdb", "ipykernel>=7.2.0", "marimo>=0.23.8", "pandas>=3.0.3", "plotly>=6.7.0", "polars>=1.41.2", "pyarrow>=24.0.0", "pydantic-ai>=1.104.0", "ruff>=0.15.15", "sqlglot<30", "tabulate>=0.10.0", ]That is my opinion. What is the marimo way, to handle this?
Best Eric
Evaluation contract
Track SQL definitions by their complete qualified identity. Tables with the same leaf name in different schemas or catalogs are not multiply defined; the same fully qualified name, or the same unqualified name, still is. Preserve the user-facing table variable, defining-cell ownership, deletion behavior, and conflict reporting for genuine collisions.
Interface
- Assistant
Let me start by understanding the issue and exploring the repository structure to find the relevant code.
The issue is about SQL cells in marimo using the same table name in different schemas causing a variable name collision. When you create
finance.datalake.xxandfinance.information_layer.xx, marimo treats both as defining a variablexx, causing a conflict.The evaluation contract says: "Track SQL definitions by their complete qualified identity. Tables with the same leaf name in different schemas or catalogs are not multiply defined; the same fully qualified name, or the same unqualified name, still is."
The interface says I need to modify
get_multiply_definedinmarimo/_runtime/dataflow/definitions.py.Let me explore the codebase first.
- Assistant
Let me look at the specific file mentioned in the interface.
- Assistant
Now I need to understand the SQL variable tracking to see how table definitions are registered. Let me look at the relevant SQL code to understand how definitions are created for SQL cells.
- Assistant
Let me explore the SQL visitor and related files to understand how SQL table names are extracted and turned into variable definitions.
- Assistant
Now let me look at the visitor.py to understand how SQL definitions are turned into variable names:
- Assistant
Let me search for the part where SQL definitions are added to the visitor:
- Assistant
Let me look at that section of the visitor to understand how SQL defs are registered.
- Assistant
Now I understand the issue! Let me look at how the definitions are registered and how
get_multiply_definedis called: - Assistant
Now let me understand the full flow. Let me check how
get_multiply_definedis used in graph.py and other callers: - Assistant
Let me look at each caller of
get_multiply_definedin detail: - Assistant
Let me look at the callers more carefully to understand how the return value is used:
- Assistant
Let me check how the multiply defined names are used in the runtime:
- Assistant
Let me also look at how multiply defined is used in the dependency graph tool:
- Assistant
Let me look at the ipynb converter:
- Assistant
Now let me look at the tests for the definitions module:
- Assistant
Let me check the test for dataflow to understand how multiply defined is tested:
- Assistant
Let me look at these test cases:
- Assistant
Let me look at the first test more closely to understand what's already working:
- Assistant
Now let me look at how the existing test for different schemas works and understand the current behavior:
- Assistant
Now let me understand how
siblingsfromregister_definitionis used: - Assistant
Let me see how siblings is used in graph.py:
- Assistant
Now let me check how
get_multiply_definedis used in the codebase to understand the interface changes needed: - Assistant
Now let me look at where
register_definitionreturn value (siblings) is used: - Assistant
Let me also check the test_edges.py file:
- Assistant
Let me look at the test_edges.py to understand how DefinitionRegistry is tested:
- Assistant
Now I have a good understanding of the codebase. Let me create a test script to reproduce the issue first:
- Assistant
Let me install the required dependencies first:
- Assistant
Now let me try to reproduce the issue:
- Assistant
Let me investigate more to understand the actual variable names:
- Assistant
I see -
dfis multiply defined because both cells assign todf. But the real issue is about the SQL table names. Let me check how SQL cells work in marimo: - Assistant
I need to understand how SQL cells work in marimo. Let me look at the SQL cell handling:
- Assistant
Let me look at the visitor code more carefully to understand how SQL cells work. I need to see the part where it processes
mo.sql()calls: - Assistant
I can see the issue clearly now. Both SQL cells define a table named
xx(the leaf name), and even though they have different qualified names (finance.datalake.xxvsfinance.information_layer.xx), theregister_definitionmethod adds both cells todefinitions["xx"]when thetyped_defalready exists. Let me trace through the exact logic to understand: - Assistant
I see -
mo.sql()without assignment doesn't produce defs. Let me look at how SQL cells work in the actual context of the visitor: - Assistant
Hmm, interesting - the SQL table definitions are not being extracted here. Let me look at how SQL cells actually work in marimo:
- Assistant
Let me look at how SQL cells are represented differently from Python cells. There must be some distinction.
- Assistant
Let me look at how SQL cells are compiled and what
compile_celldoes: - Assistant
Let me look at the
compile_cellfunction to understand how SQL cells are compiled: - Assistant
Let me look at how SQL cells are actually handled. Let me check if there's a separate SQL compilation path:
- Assistant
Let me look at the rest of compile_cell:
- Assistant
Let me check if the SQL defs extraction is working:
- Assistant
The SQL defs extraction returns empty. Let me check why:
- Assistant
DuckDB is not installed. Let me install it.
- Assistant
Now let me test again.
- Assistant
Good. Now let me reproduce the bug with duckdb installed:
- Assistant
I've successfully reproduced the bug. Now I can see that
xxis incorrectly reported as multiply defined even though the two tables have different schemas (finance.datalake.xxvsfinance.information_layer.xx). Let me now implement the fix.My plan is:
- Add a
_sql_qualified_namesfield toDefinitionRegistryto track which qualified name each cell defines - Modify
register_definitionto store qualified names - Modify
unregister_definitionsto clean up - Modify
get_multiply_definedto be conflict-aware - Update
DirectedGraph.get_multiply_definedto extract names from the new return format
Let me start with the implementation:
- Add a
- Assistant
Now I understand the full picture. Let me implement the fix. The key insight is:
- When two SQL cells define tables with the same leaf name but different qualified names (e.g.,
finance.datalake.xxandfinance.information_layer.xx), they should NOT be considered multiply defined. - When two SQL cells define tables with the same qualified name, they SHOULD be considered multiply defined.
- When one cell defines an unqualified table and another defines a qualified table with the same leaf name, they SHOULD be considered multiply defined.
- Python variables always conflict with same-name definitions.
Let me implement the fix:
- When two SQL cells define tables with the same leaf name but different qualified names (e.g.,
- Assistant
Now let me implement the changes to
definitions.py. First, I'll add the_sql_qualified_namesfield: - Assistant
Now let me implement the changes. First, add the
_sql_qualified_namesfield: - Assistant
Let me use the replace command instead:
- Assistant
Let me use a script to make the changes instead:
- Assistant
Let me verify the changes were applied correctly:
- Assistant
Good. Now I need to update
DirectedGraph.get_multiply_definedingraph.pyto extract just the names from the tuples: - Assistant
Now let me update
DirectedGraph.get_multiply_definedingraph.pyto extract just the names from the tuples: - Assistant
Let me verify the graph.py changes:
- Assistant
Good. Now let me test the fix by running the reproduction script:
- Assistant
The fix works for the basic case. Now let me test more edge cases:
- Assistant
All edge cases pass. Let me also test the
get_multiply_definedmethod at theDefinitionRegistrylevel to make sure it returns the correct tuples: - Assistant
Hmm, the registry-level method returns empty for
dftoo. Let me investigate why: - Assistant
This is correct! When using different variable names (
df1anddf2), there's nodfconflict. The fix is working perfectly -xxis no longer reported as multiply defined because the two tables have different qualified names. Let me test a couple more edge cases: - Assistant
All edge cases pass correctly. Now let me run the existing tests to make sure I haven't broken anything:
- Assistant
There's a test failure! Let me look at this test case:
- Assistant
This test failure is interesting - it's about the dependency edge computation, not about multiply-defined names. Let me investigate:
- Assistant
Let me check if this test was already failing before my changes by reverting and running:
- Assistant
Good - this test was already failing before my changes. It's a pre-existing issue likely related to missing
sqlglotdependency. Let me restore my changes and run the rest of the tests: - Assistant
Let me install the missing sqlglot dependency and rerun:
- Assistant
Now let me run all the SQL tests:
- Assistant
All 58 tests pass. Let me also run the test_edges.py tests:
- Assistant
All tests pass. Let me also run the dependency_graph test and other related tests:
- Assistant
Let me also run the SQL-related tests:
- Assistant
That's just a missing test dependency. Let me run the sql_visitor tests and other AST tests:
- Assistant
Let me also run the visitor tests:
- Assistant
Let me also run the code_mode tests:
- Assistant
Let me check what the error is:
- Assistant
That's just a missing test dependency (inline_snapshot). Let me also run the app test to verify:
- Assistant
Let me check if this test failure is pre-existing:
- Assistant
Good - this test failure is pre-existing. Let me restore my changes:
- Assistant
Now let me verify the convert/ipynb code still works:
- Assistant
All tests pass. Let me now verify the final state of my changes:
- Assistant
The changes look clean and minimal. Let me do a final comprehensive test to verify all the edge cases work correctly:
- Assistant
All 15 comprehensive tests pass. Let me run the full test suite one final time to confirm everything is working:
- Assistant
All 318 tests pass. Let me do a final review of my changes:
- Assistant
Let me do a final review of the complete changes:
- Assistant
Now let me clean up the temporary script and submit:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.