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
I'll start by exploring the repository structure and understanding the relevant code.
Assistant
Let me look at the SQL visitor and VariableData to understand
qualified_name,kind, andlanguage.Assistant
Now let me understand the current behavior. The
get_multiply_definedreturns a list of names. The contract says it should returnlist[tuple[Name, set[CellId_t]]]. Let me look more carefully at the contract.Contract:
- Type: Function
- Name: get_multiply_defined
Assistant
Let me look at existing tests for the definitions registry.
Assistant
No tests reference these. Let me search more broadly for tests of the dataflow.
Assistant
Let me look at the SQL defs structure to understand
qualified_namefor unqualified tables.Assistant
So for an unqualified table "xx", the qualified_name is "xx". For
finance.datalake.xx, the qualified_name is "finance.datalake.xx".Now let me look at the SQLDefs tables structure and how
_table.tableand_table.qualified_nameare formed.Assistant
Let me look at all the callers in detail.
Assistant
This is a complex caller. Let me understand the broader picture. Let me look at the git log for definitions.py to see recent context.
Assistant
Let me look at
_definein the visitor and theNametype.Assistant
Let me write a reproduction script to confirm the issue.
Assistant
Now let me write a reproduction script using the graph directly.
Assistant
Hmm, the SQL cells register "mo" as multiply defined (because both cells import marimo as mo). But the SQL tables "xx" aren't showing up in definitions at all! Let me check why. The definitions only show "mo". So the SQL table definitions aren't being registered. Let me check if duckdb is available and the SQL parsing works.
Wait, both cells do
import marimo as mo, so "mo" is multiply defined. That's expected (each cell imports mo). But actually in a real marimo notebook, imports are deduplicated. Hmm, but for the graph, each cell defines "mo". Actually that's a separate concern.But the SQL "xx" tables aren't showing up. Let me check why. Maybe duckdb isn't installed, or the SQL def extraction failed.
Let me debug.
Assistant
Tables aren't being found. Let me debug the tokenizer.
Assistant
Now SQL defs are found. Let me re-run the repro.
Assistant
I've reproduced the issue — both "mo" and "xx" are flagged. "mo" is from the explicit import in my repro; in real marimo notebooks
mois a built-in. Let me make a cleaner repro that focuses on the SQL definitions using the registry directly.Assistant
Let me understand how SQL table names become user-facing variables.
Assistant
Let me look at how the SQL defs are used at runtime - specifically how a SQL table def becomes a variable. Let me search for where SQL definitions get registered into the kernel globals.
Assistant
Let me check how
register_definition's return value (siblings) is used.Assistant
The return value of
register_definition(siblings) is not used inregister_cell— it just callsregister_definitionand ignores the return. Let me confirm.Assistant
Let me understand how SQL cells register output variables at runtime, to understand "user-facing table variable".
Assistant
Now I understand the structure. The
mo.sql(...)cells define a user-facing variable equal to the table's leaf name (e.g.xx), withqualified_namebeing the fullfinance.datalake.xx. Let me look at the full_code_modecaller since it doesset(graph.get_multiply_defined())which would break with tuple+set returns.Assistant
Now I have a complete understanding. Let me check the imports in
graph.pyand look at theMultipleDefinitionErrorand thedefinitionsfield naming.Assistant
Let me examine how
edges.pyuses the definition registry, to ensure my changes don't break edge computation.Assistant
I now have a thorough understanding. The fix:
- Track each cell's qualified name per leaf name in
DefinitionRegistry. - Rewrite
get_multiply_definedto do conflict-aware grouping by qualified name, returninglist[tuple[Name, set[CellId_t]]]. - Update all callers to handle the new return type.
Let me look at the full current
definitions.pyonce more with line numbers, then implement.- Track each cell's qualified name per leaf name in
Assistant
Now let me implement the changes. First, add the
qualified_definitionsfield:Assistant
I need to use the correct format with
>>> SEARCHand>>> REPLACEmarkers.Assistant
I made a typo with
<<< REPLACE. Let me redo with the correct>>> REPLACEmarker.Assistant
Now let me add the qualified-name recording in
register_definition:Assistant
Now I need to update
unregister_definitionsto clean up thequalified_definitionsentries (including orphaned ones from the cross-kind branch). Let me view and rewrite it:Assistant
Now let me rewrite
get_multiply_definedwith conflict-aware grouping:Assistant
Now let me update the
graph.pywrapper's return type:Assistant
Now update the callers. First
app.py:Assistant
Now
to_ir.py:Assistant
Now the
_code_mode/_context.pycaller. Let me update it to use names (hashable) for the set operations:Assistant
Now the
new_multiply_definedset computation also usesset(graph.get_multiply_defined())which would break with tuples containing sets. Let me fix that:Assistant
Now the AI dependency_graph caller:
Assistant
Now let me check for any other callers I might have missed, including the
__init__.pyand elsewhere:Assistant
All callers updated. Now let me run the reproduction scripts to confirm the fix:
Assistant
The false conflict is gone. Now let me test all the edge cases — genuine collisions should still be detected:
Assistant
All edge cases pass. Now let me test the cross-kind orphan cleanup (cell registered but never added to
definitions, then unregistered in different orders):Assistant
Orphan cleanup works in both orders. Now let me run the existing test suite for the affected areas. Let me find relevant tests:
Assistant
There are tests expecting the old
list[Name]return format. Per instructions, I should not modify existing tests — these break because they reflect the old interface and will be ignored by evaluation. But let me examine them to confirm my change is correct and not breaking legitimate behavior:Assistant
The 2 failing tests confirm my behavior is correct — they fail only because of the mandated return-type change (
["t1"]→[("t1", {"0","1"})]). The diff-schema test (test_redefine_sql_table_diff_schema) passes. Per instructions, I won't modify existing tests. Let me run the other affected test files to check for additional breakage:Assistant
inline_snapshotis missing. Let me install it, or skip that test file. Let me run the others first.Assistant
Many failures in
test_code_mode_context.py. Let me check whether these are pre-existing (environment) or caused by my change. Let me look at one failure detail and also compare against a clean checkout:Assistant
These failures are due to missing
pytest-asyncio(theasyncio_modeconfig is unknown), not my change. Let me install it and re-run:Assistant
The remaining 10
TestPackages::*failures are environment-related ("pixi is not available"), not my change. Let me confirm they're pre-existing by stashing my changes:Assistant
Confirmed pre-existing (pixi environment). Now let me run the code_mode tests that specifically exercise the multiply-defined validation path:
Assistant
The 2 failures are again
TestPackages::*(pixi). Let me find the code_mode tests that specifically test multiply-defined name validation:Assistant
The code_mode multiply-defined validation tests pass. Now let me run the full relevant test suites once more (excluding the pixi/environment-broken package tests):
Assistant
A new failure in
test_multiply_defined_variables—ValueError: not enough values to unpack. Let me look at that test; it likely mocksget_multiply_defined:Assistant
All 3 failures are due to the mandated return-type change (mocks/assertions using the old
list[Name]format). These are "tests that need to be updated to reflect the changes" — I must not modify them. Let me verify there are no other places using the old format. Let me grep all tests forget_multiply_defined:Assistant
Let me run
test_validate_graph.pyand the app tests in full (these exercise theMultipleDefinitionErrorpath and graph validation), and also confirm the diff-schema test still passes:Assistant
All pass (XPASS are pre-existing flaky tests). Now let me reproduce the original issue end-to-end with actual SQL cells in a
DirectedGraph, exactly as the issue describes:Assistant
The end-to-end issue is fixed —
definitionsstill tracks both cells as owning "xx" (ownership preserved), but no false conflict. Let me also verify genuine same-qualified collisions are still caught end-to-end:Assistant
Both genuine collision cases are still detected. Now let me run a broader runtime test sweep to ensure nothing else broke from the interface change. Let me check the messaging errors test and runtime tests that might use the validation path:
Assistant
Let me check if these 3
test_export_watch_*failures are pre-existing:Assistant
Confirmed pre-existing (Static files not found — environment). The only test breakage from my change is the 3 tests asserting the old
list[Name]format, which the contract's mandatedlist[tuple[Name, set[CellId_t]]]return type necessarily invalidates — these are "tests that need to be updated to reflect the changes" and must be left unmodified.Let me do a final review of the complete
definitions.py:Assistant
The implementation is clean and robust. Let me run the full
test_dataflow.pyto confirm only the 2 format-related failures remain (and the diff-schema test passes):Assistant
Only the 2 format-related failures remain (confirming
('t1', {'1', '0'})— the new correct tuple format). The diff-schema test passes. Let me clean up temp scripts and do a final review of the complete diff:Assistant
The diff is clean and complete. Let me do a final import sanity check and re-run the key reproduction to confirm everything works:
Assistant
Everything works. Let me run a final broader sweep of the dataflow and edges tests one more time to confirm the state is stable:
Assistant
Only the 2 expected format-related failures remain (mandated by the contract's new return type); all other tests pass and the diff-schema test passes. The remaining failures (
TestPackagespixi,test_export_watchstatic files) are pre-existing environment issues confirmed viagit stash.Let me verify no temp files remain and do a final status check:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.