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
-- Cell 1 CREATE TABLE finance.datalake.xx (id INT);-- Cell 2 CREATE TABLE finance.information_layer.xx (id INT);ERROR
This 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
requires-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
Type: Function Name: get_multiply_defined Path: marimo/_runtime/dataflow/definitions.py Input: self Output: list[tuple[Name, set[CellId_t]]] Description: Returns a list of (name, cell_ids) tuples for names that are defined in multiple cells. Uses 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.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository using command. Additionally, you can use and to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me start by exploring the repository structure and understanding the codebase.
Assistant
Let me understand the issue. The current
get_multiply_definedreturns a list ofName. But the interface says it should returnlist[tuple[Name, set[CellId_t]]]. Let me re-read.The interface says:
Type: Function Name: get_multiply_defined Path: marimo/_runtime/dataflow/definitions.py Input: self Output: list[tuple[Name, set[CellId_t]]]So the output should be a list of tuples. But currently it returns
list[Name]. Let me check howget_multiply_definedis used in the codebase.Assistant
Let me look at how
get_multiply_definedis used in each place to understand the expected interface.Assistant
Now let me look at the usages in
_ai/_tools/tools/dependency_graph.py,_code_mode/_context.py, and_convert/ipynb/to_ir.py.Assistant
Let me look at the
VariableDataandSQLRefstructures and how qualified names work.Assistant
Let me look at how the SQL defs are created in the visitor.
Assistant
So for a table
finance.datalake.xx,_table.tableis the leaf namexxand_table.qualified_nameisfinance.datalake.xx. The_defineis called with name=xxand VariableData(kind="table", qualified_name="finance.datalake.xx").Now let me understand the registration logic better and the
_definemethod.Assistant
Now let me understand the registration in graph.py.
Assistant
Now let me look at the test_dataflow.py tests to understand the expected behavior for
get_multiply_defined.Assistant
Let me check the test mock and the AI tools test.
Assistant
Let me look at how
register_definitionsiblings are used (the runtime "redefines variables" error).Assistant
Let me see where
register_cell's return / siblings are used in graph.py.register_cellcallsregister_definitionbut doesn't capture the return value (siblings). Let me check.Assistant
The sibling return value is unused in graph.py. Let me find where the actual "redefines variables" runtime error comes from.
Assistant
Let me find the runtime kernel code that raises the "redefines variables" error when running a cell.
Assistant
Let me find where the multiple-defs error data is generated in the backend.
Assistant
So
MultipleDefinitionErrorhasnameandcells. Let me find where this is constructed in the runtime to report the per-cell error.Assistant
Let me search for where the messaging
MultipleDefinitionError(withcells) is constructed.Assistant
Let me find where the runtime constructs the
MultipleDefinitionErrorwithcells(the messaging error). Let me search in the broader codebase including_server/_runtime.Assistant
The messaging
MultipleDefinitionError(withcells) - let me find where it's instantiated for runtime. Let me look at runtime.py around line 1220 and 1947.Assistant
Let me find where
all_errorsis built - the registration errors that produceMultipleDefinitionErrorwith cells.Assistant
Now I understand the full picture. The runtime error is generated in
check_for_multiple_definitionsinvalidate_graph.py. Let me look at the_context.pyusage more closely.Assistant
Now let me look at
delete_cellandunregister_definitionsto understand the lifecycle.Assistant
Let me write a reproduction script to confirm the bug first.
Assistant
The package may need build. Let me check if it installed. Let me try running with PYTHONPATH.
Assistant
Interesting! The reproduction shows the multiply-defined is on
df, notxx. Let me check what's in the cell defs and variable_data.Assistant
The SQL defs aren't being picked up. Let me check if duckdb/sqlglot are installed and how SQL detection works.
Assistant
Now with duckdb installed, SQL defs are picked up. Let me re-run the repro.
Assistant
The bug is reproduced. The
dfcollision is because I used the same LHS — let me reproduce the exact issue without LHS (pure SQL cell). Let me check how marimo compiles a SQL cell.Assistant
Let me reproduce the exact issue with SQL cells (no LHS collision).
Assistant
Now I've reproduced the exact issue. The
xxis falsely reported as multiply defined even though the tables are in different schemas. Let me check for other places that might treatdefinitions[name]len > 1 as a conflict.Assistant
Let me check
runtime.py:1943to make sure it's not affected.Assistant
Now I understand the full picture. The bug has two sources:
DefinitionRegistry.get_multiply_definedreturns leaf names without considering qualified identity.check_for_multiple_definitionsinvalidate_graph.pyusesgraph.definitions[name]directly, also ignoring qualified identity (this is what generates the actual runtime "redefines variables" error).
Let me look at the full current
definitions.pyonce more and implement the fix.Assistant
Now let me implement the fix. First, let me edit
definitions.pyto add qualified identity tracking and a conflict-awareget_multiply_defined.Assistant
Now let me replace the
get_multiply_definedmethod with the conflict-aware version:Assistant
Now update
graph.get_multiply_definedto extract names from the tuples (keeping backward compatibility for callers):Assistant
Now update
check_for_multiple_definitionsinvalidate_graph.pyto use conflict-aware grouping:Assistant
Now let me run the reproduction to confirm the fix:
Assistant
The bug is fixed. Let me verify the registry's
get_multiply_definedreturns tuples directly:Assistant
All cases work correctly. Now let me run the existing dataflow tests to ensure I didn't break anything:
Assistant
All 58 dataflow tests pass. Let me run the validate_graph tests and SQL-related tests:
Assistant
Hmm, the tests aren't running — output shows 0 tests. Let me check the actual test count.
Assistant
0 items collected. Maybe pytest config excludes some. Let me check the pytest.ini / pyproject for testpaths or markers. Actually maybe duckdb/sqlglot not having a fixture or the tests require additional deps. Let me run with more verbosity.
Assistant
Individually each runs. Let me run them separately to find which caused the collection issue:
Assistant
tests/_sql/test_sql.pyhas a collection error. Let me check it:Assistant
The collection error is due to a missing
inline_snapshotmodule — unrelated to my change. Let me install it and run the SQL tests:Assistant
All SQL tests pass. Let me run the broader relevant test files for the callers I might have affected:
Assistant
Let me run the app and runtime tests to ensure nothing broke:
Assistant
Let me check whether these failures are pre-existing (caused by missing deps) or caused by my change. Let me look at one failure first:
Assistant
These failures are due to missing
pytest-asyncio(async tests), not my change. Let me install it and re-run:Assistant
The failures were environment-related (missing async plugin). Now let me run the runtime tests that exercise multiple-definition detection and cell registration lifecycle:
Assistant
Let me search for tests that specifically test multiple-definition errors and run them:
Assistant
Let me check if these failures are pre-existing by stashing my changes:
Assistant
The 2 failures are pre-existing (they fail without my changes too — environment-related, package manager). Let me confirm what they need:
Assistant
These are about package managers (pixi not available) — unrelated to my change. Let me run the full relevant test suite once more to confirm everything passes:
Assistant
All 358 tests pass. Now let me test edge cases: 3-level qualified names (catalog.schema.table), views in different schemas, and the deletion/re-registration lifecycle:
Assistant
All edge cases work correctly. Let me also test the exact issue scenario via
app.run()(which raisesMultipleDefinitionErrorat the AST level):Assistant
The NameError is because
mois not defined in the cell. Let me addimport marimo as moin the cells or pass defs. Let me fix the test.Assistant
The multiple-definition error is gone — now it's only failing because
polars[pyarrow]isn't installed for SQL execution (a runtime/dependency issue, not a graph validation issue). Let me install polars to fully confirmapp.run()succeeds:Assistant
Now the error is a DuckDB execution error (catalog
financedoesn't exist), NOT a marimo multiple-definition error. This confirms the graph validation passes. Let me create the catalog/schema first to fully confirmapp.run()succeeds:Assistant
The graph validation clearly passes (we get a DuckDB execution error, not
MultipleDefinitionError). Let me do a clean 2-level (schema.table) test that fully executes to confirm end-to-end:Assistant
End-to-end success. Now let me verify the negative case — that a genuine collision IS still detected at
app.run():Assistant
Genuine collisions are still detected. Let me also confirm the unqualified collision (two
CREATE TABLE xx) is still detected:Assistant
Both genuine collision cases are still detected. The fix is complete. Let me clean up temp files and review the final diff:
Assistant
The diff looks clean. Let me run a final comprehensive test sweep on all affected areas:
Assistant
All 557 tests pass. Let me run the app tests one final time to confirm the
app.run()path works:Assistant
All tests pass. Let me verify
definition_registryis accessible as a public attribute onDirectedGraph(used bycheck_for_multiple_definitions):Assistant
definition_registryis a public field. Let me confirmgraph.definitions(already used by the original code) reaches into the registry, confirming my access pattern is consistent:Assistant
Confirmed — the existing code already reached into
definition_registryvia thedefinitionsproperty. My change is consistent with the existing pattern.Let me run a final broader sweep of the runtime tests touching multiple-definition detection:
Assistant
All tests pass. Let me do a final check of git status to confirm only the intended files changed and no temp files remain:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.