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 repository structure and the issue at hand.
The issue is about SQL cells in marimo with DuckDB. When creating tables with the same name in different schemas, marimo raises a "variable redefinition" error. The core problem is in
marimo/_runtime/dataflow/definitions.pyin theget_multiply_definedmethod, which needs to use conflict-aware grouping so that qualified SQL names (e.g.,schema.table) with the same leaf name are not considered multiply defined unless they share the exact same qualified name.Let me explore the repository structure first.
Assistant
Let me look at the specific file mentioned in the issue:
Assistant
Let me open and examine this file.
Assistant
Let me look at the relevant types - the SQL visitor and the VariableData/Name types
Assistant
Let me look at both visitor.py and sql_visitor.py.
Assistant
Now let me look at the SQL visitor to understand how SQL definitions and references work
Assistant
Let me look at how SQL definitions are processed in the Cell implementation
Assistant
Let me find the CellImpl file.
Assistant
Let me look at graph.py to understand how register_definition is called.
Assistant
Let me explore how
get_multiply_definedis used in the codebase.Assistant
Let me look at the key usages and tests to understand how
get_multiply_definedis consumedAssistant
Let me check the context.py usage more carefully.
Assistant
Now let me look at
_ai/_tools/tools/dependency_graph.pyto see how it usesget_multiply_defined.Assistant
Let me look at the existing tests for
get_multiply_defined.Assistant
Let me look at the test that tests qualified SQL tables
Assistant
Let me look at the
test_sql_table_schema_to_python_reftest and surrounding tests more carefully.Assistant
Let me first create a reproduction script to confirm the issue exists
Assistant
Let me install the missing dependencies
Assistant
Now let me run the
Assistant
Confirmed the issue. Let me also test edge cases:
Assistant
Let me look at the edges module to understand how definitions are resolved for SQL references
Assistant
Now I have a clear understanding of the issue. Let me implement the fix.
The fix should be in
register_definitionindefinitions.py. The core issue is in the else branch when a second SQL cell with the same leaf name but a different qualified name comes in - it gets incorrectly added todefinitions[name].I'll:
- Add a
_sql_qualified_namesdict to track qualified names per simple name - Modify
register_definitionto check qualified names in the else branch - Update
unregister_definitionsto clean up the new dict
Let me open the file and make
- Add a
Assistant
Let me check what
cell.defscontains for SQL cellsAssistant
Let me look at how cell.defs are populated for SQL cells.
Assistant
Let me look at the CellImpl class more closely
Assistant
Let me look at how SQL definitions are processed in the compiler
Assistant
No results for "sql_defs" in compiler.py? Let me check where find_sql_defs is called
Assistant
Let me look at how SQL definitions are processed in visitor.py around line 719.
Assistant
Now I understand the full picture. Let me implement the fix. I'll take a careful approach that:
- Adds
_sql_qualified_namestracking to theDefinitionRegistry - Fixes
register_definitionto not add todefinitions[name]when qualified names differ - Updates
unregister_definitionsto properly clean up
Let me implement:
- Adds
Assistant
Now let me test my fix with the reproduction script:
Assistant
All tests pass. Let me also test the
unregister_definitionsmethod:Assistant
The fix works correctly. Now let me apply it to the actual file:
Assistant
Now let me apply the changes. First, let me open the file:
Assistant
Let me make the edits. First, add the
_sql_qualified_namesAssistant
Now update
register_definitionto handleAssistant
Now update
unregister_definitionsto properlyAssistant
Let me check the full file to make sure everything looks correct.
Assistant
The file has some corruption - the
get_defining_cellsmethod was accidentally merged withunregister_definitions. Let meAssistant
The file has too many corruptions. Let me restore from backup and redo the changes more carefully.
Assistant
Let me write the complete fixed file directly
Assistant
Let me verify the fix works by running the reproduction
Assistant
All tests pass. Now let me run the existing tests to make sure nothing is broken
Assistant
Let me install duckdb and run the SQL tests
Assistant
Now let me run the full test
Assistant
All relevant tests pass. Let me run the full test_dataflow suite to make sure nothing else is broken
Assistant
There's a failing test. Let me look at the test_sql_chain test
Assistant
Let me look at the parametrize decorator for this
Assistant
Let me look at the full parametrize block
Assistant
Let me debug this failure
Assistant
Let me check if this test was already failing before my changes
Assistant
The test was already failing before my changes - it's a pre-existing issue unrelated to my fix. Let me restore my fix.
Assistant
Let me run all the tests excluding the pre-existing failures
Assistant
Let me check if these failures are pre-existing too
Assistant
All failures are pre-existing. Let me restore my fix and verify the same results
Assistant
Now let me apply the fix again - writing the complete file
Assistant
Now let me run the comprehensive tests to verify
Assistant
Let me run the existing tests once more to confirm no
Assistant
Let me also run the edges tests to make sure nothing is broken there
Assistant
All 24 edge tests pass. Let me install polars and run the skipped tests too
Assistant
All 26 edge tests pass. Let me verify the final file and clean
Assistant
Let me verify the diff to see the net
Assistant
The diff looks clean. Let me now submit the changes
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.