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 to understand the codebase
Assistant
Let me look at the specific file mentioned in the issue.
Assistant
let me understand the broader context and how this is used.
Assistant
me look at the tests for this file to understand the expected behavior:
Assistant
Let me explore the broader context of the variable_data structure and qualified_name attribute:Let me look at how
qualified_nameandvariable_datawork:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[grep -rn "qualified_name" marimo/ --include="*.py" | head -30]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Let me try again with proper command structure
Assistant
Let me reproduce the bug first:
Assistant
Let me look at the existing test for
test_redefine_sql_table_diff_schemato understand what's the expected behavior:Assistant
Let me re-read the
register_definitionmethod more carefully:Assistant
Let me look at how
definitionsandtyped_definitionsare used in the rest of the code:Assistant
me look at how
get_multiply_definedis used in callers:Assistant
Let me check how refs work for SQL cells:
Assistant
OK so for SQL refs, the name stored in
refsis the qualified_name. So when edges.py doesdefinitions.definitions.get(name, ...), it would look up by qualified name. That confirms that storing SQL definitions by qualified name would work for edges.py.But wait, there's a subtle issue. Let me check what happens in Python code that references SQL variables. For example, a Python cell that does
df = mo.sql("SELECT * FROM xx")- what's in refs?Actually that's a SQL cell. Let me think of a Python cell that references a SQL table.
A Python cell could be
df(just the name). The visitor would addxxto refs if it's a SQL table, but as a Python name.Actually wait, looking at
_define:for _table in sql_defs.tables: self._define( None, _table.table, VariableData( "table", qualified_name=_table.qualified_name ), )It uses
_table.tableas the name (just the leaf name like "xx"). But for refs:for ref in sql_refs: name = ref.qualified_name # Uses qualified name self._add_ref( None, name, deleted=False, sql_ref=ref )Assistant
check where
get_defining_cellsis called:Assistant
OK so
get_defining_cells(name)is called from various places to get cells that define a name. For the bug case, if we have two cells with different qualified SQL names but same leaf name, callingget_defining_cells("xx")would return both cells.Hmm, this might be an issue. Let me check
runtime.py:1947:referring_cells |= self.graph.get_referring_cells( name, language="python" ) - self.graph.get_defining_cells(name)This is used for UI element values. We don't want to exclude cells that define the same leaf name in a different SQL schema. Actually wait, the code is
get_referring_cells(...) - get_defining_cells(name). The intent is to never rerun the cell that created the name. So for a UI element, we don't want to rerun the cell that defined it.If a SQL cell with name "xx" defines a UI element, and another SQL cell with name "schema.xx" defines another UI element... well, the UI element would be Python (probably), so this might not apply.
Let me check
cell_runner.py:579:Assistant
Let me look at the
test_sql_creation_and_select_from_same_celltest:Assistant
Let me check what happens for qualified SQL refs:
Assistant
Now I have a good understanding. Let me implement the fix:
Assistant
Now let me write the updated file with the fix:
Assistant
Now I need to update the callers of
get_multiply_definedsince the return type has changed:Assistant
Now let me update the other callers:
Assistant
Now let me update the other callers:
Assistant
Wait, let me check the actual format needed:
Assistant
I should fix the type annotation -
multiply_definedshould belist[str]since I extract names:Assistant
Now let me update the code_mode context:
Assistant
Now let me run the reproduce script to verify the fix:
Assistant
fix is working correctly for the bug case. Let me run the existing tests to make sure I haven't broken anything:
Assistant
This test failure is expected because the test was checking against
["t1"]but the new format is[("t1", {cell_ids})]. According to the instructions, I should ignore this since the contract specifies the new return type.Let me run all tests excluding the ones that check the specific format:
Assistant
All other tests pass. Let me also run other related tests:
Assistant
Let me check the third failure to see if it's related to my changes:
Assistant
This failure is unrelated to my changes - it's a permission error related to
os.setsid()in a subprocess. Let me confirm:Assistant
The third failure was already there before my changes. Let me write a comprehensive test to verify all edge cases:
Assistant
All 11 test cases pass. Let me also test the unregister functionality:
Assistant
Let me also write a more comprehensive test that simulates the actual bug scenario:
Assistant
The fix correctly resolves the original bug from the issue. The
dfbeing reported as multiply-defined is actually correct because each cell is assigning todf(Python variable). The user can use different df names if needed.Let me also write a more comprehensive integration test that uses SQL refs to other tables:
Assistant
The fix works correctly. Let me run the AI tools tests:
Assistant
This test uses a mock graph where
get_multiply_definedreturns a list of strings (not tuples). The test is using a mock that doesn't match the new interface. Let me look at the testAssistant
The test failure is due to a mock setup that returns
["x"](a list of strings) instead of[("x", {...})](a list of tuples). Since I can't modify the tests, but the test mocks return the old format, this test would fail.But looking at the test, it's a mock test that doesn't actually test the real graph. The test is testing the tool's interface, and the mock setup doesn't match the new contract. This is a test that needs updating but I shouldn't modify it.
Let me check if the failure is related to my code changes:
Assistant
this is a mock test that needs updating but I cannot modify. Let me ignore it as per instructions. Let me now check the code_mode context and make sure it handles everything correctly:
Assistant
Let me test more edge cases:
Assistant
My test expectation was wrong -
dfis only in cell 0, so it shouldn't be in multiply_defined. The actual output is correct:Assistant
Let me check the existing test that explicitly tests table vs view behavior:
Assistant
I need to update my identity to include the kind, so that a table and view with the same name don't collide:
Assistant
Let me check if there are any existing tests for table vs view:
Assistant
Let me update to remove kind from identity to match the contract:
Assistant
The "Table vs View same name" case is correctly multiply defined per the contract (same fully qualified name). My test expectation was wrong:
Assistant
me run all the existing tests to see the impact:
Assistant
Only the 2 expected failures due to the interface change. Let me also verify the unregister tests still work:
Assistant
All tests pass. Let me also run the broader test suite to make sure I haven't broken anything:
Assistant
me also check the AI tools tests:
Assistant
test failure is due to the mock setup using the old interface (returning strings instead of tuples). The mock would need to be updated to match the new contract. Since I cannot modify tests, this is an expected failure.
Let me clean up the temporary test files:
Assistant
Let me also think about whether the test
test_redefine_sql_table_diff_schemastill works correctly. Let me check what it expects:def test_redefine_sql_table_diff_schema(self): graph = dataflow.DirectedGraph() code = "t1 = 123" first_cell = parse_cell(code) graph.register_cell("0", first_cell) code = 'df = mo.sql("CREATE TABLE schema1.t1 (i INTEGER, j INTEGER)")' second_cell = parse_cell(code) graph.register_cell("1", second_cell) assert graph.cells == { "0": first_cell, "1": second_cell, } # Because t1 is qualified with schema1, it is not considered multiply defined multiply_defined = graph.get_multiply_defined() assert multiply_defined == []Assistant
Let me try again with proper command structure
Assistant
me do a final verification of the fix with the exact scenario from the issue:
Assistant
The fix works correctly. Now let me run the tests once more to confirm only the expected failures:Let me run the tests once more to confirm:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[.venv/bin/python -m pytest tests/_runtime/test_dataflow.py tests/_convert/ipynb/ tests/_ast/ -q 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try the command again with proper structure
Assistant
Only the 2 expected failures (which are due to the interface change as documented in the contract). All other tests pass. Let me clean up the temp files:
Assistant
Now let me also think about whether there are any other tests or callers I might have missed. Let me grep for all callers of the definitions-related methods:Let me also check there are no other callers of
get_multiply_definedI might have missed:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[grep -rn "get_multiply_defined" --include="*.py" .]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Let me try with proper structure
Assistant
All the callers in the marimo/ directory are updated. The test files (in tests/) are expected to fail with the interface change, but they are tests so I'll leave them.
Let me also check for callers of
register_definition:Let me check there are no other callers ofregister_definitionI should check:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[grep -rn "register_definition|unregister_definitions" --include="*.py" marimo/ | head -20]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>Assistant
Let me retry the command with proper structure
Assistant
These callers use the methods correctly - I didn't change the signature of register_definition or unregister_definitions. Good.
Let me also do a final check to make sure my changes don't break anything by running a broader test suite:Let me run the tests one more time to confirm the fix is complete:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[.venv/bin/python -m pytest tests/_runtime/test_dataflow.py -v 2>&1 | tail -25]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try again with proper structure
Assistant
Only 2 expected failures, both due to the interface change specified in the contract.
Let me also check the diff to see if my changes are correct and complete:Let me also check the diff to see my changes are correct and complete:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[git diff marimo/_runtime/dataflow/definitions.py]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try with the proper format
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.