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
get_multiply_definedusage and how variable data is structuredAssistant
Let me look at the relevant test file:
Assistant
Now let me check the SQLRef class:
Assistant
me also look at SQL-related tests to better understand how things work:
Assistant
Let me check how this data is used elsewhere:
Assistant
Let me see how callers use
get_multiply_defined:Assistant
Let me check the test for
test_redefine_sql_table_diff_schema:Assistant
I've confirmed the bug. Let me also check what other tests are doing:
Assistant
Let me check the existing test cases more thoroughly to understand what they cover:
Assistant
Now let me look at the test file for definitions directly:
Assistant
Let me check all usages of
definitionsto understand the impact:Assistant
Let me check how
qualified_nameis set for unqualified SQL:Assistant
look at the test cases more carefully and check what should be the expected behavior:
Assistant
Let me also see the existing test for multiple definitions:
Assistant
Let me also check tests on DefinitionRegistry:
Assistant
Let me check all usages of
typed_definitions:Assistant
Only used within
definitions.py. Good, the change is contained.Let me also check
definition_types:Assistant
Now I have a clear understanding. Let me implement the fix:
Assistant
Now let me implement the fix:
Assistant
Now let me verify the fix works for the bug case:
Assistant
Let me check if
dfshould be considered a conflict or not:Assistant
Let me run existing tests to verify my fix:
Assistant
The failure is expected since the return format changed. Let me check the other failing tests:
Assistant
2 tests fail due to the API change (expected). Let me check the edges tests:
Assistant
All edges tests pass. Let me check other affected tests:
Assistant
me look at this test failure:
Assistant
Let me check the function
transform_fixup_multiple_definitions:Assistant
need to update the callers to handle the new return type. Let me update them:
Assistant
Let me update the caller in to_ir.py.
Assistant
Now let me update app.py:
Assistant
Now let me update _context.py:
Assistant
Now let me update dependency_graph.py:
Assistant
Now let me verify all callers are updated by running tests:
Assistant
tests pass. Let me run other relevant tests:
Assistant
me check if these failures are pre-existing:
Assistant
, these failures are pre-existing. Let me restore my changes:
Assistant
also check other tests that might be affected by my change:
Assistant
check the third failure:
Assistant
This failure is unrelated to my changes - it's a permission issue with the environment. Let me verify it's pre-existing:This is unrelated to my changes (permission error). Let me verify it's pre-existing:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && git stash && .venv/bin/python -m pytest "tests/_runtime/test_runtime.py::TestLaunchKernelEventLoop::test_non_windows_does_not_change_event_loop_policy" 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Let me try the command
Assistant
Confirmed pre-existing. Let me restore my changes.
Assistant
run a more thorough verification of the fix:
Assistant
The fix works correctly. Let me also test with the user's exact scenario (no mo.sql wrapper):
Assistant
also run more comprehensive tests to check for regressions:
Assistant
Let me look at this test:
Assistant
This test mocks
get_multiply_definedto return a list of names, but the implementation expects tuples. The test is testing the OLD behavior.Per the instructions, I can't modify tests. But this is a test that will break because of the API change. Let me see what the test expects:
Assistant
test break is expected (API change). Let me verify the other failures are also format-related:
Assistant
Both failures are format-related (expected from the API change). The semantic behavior is correct:
- For test_sql_table_schema_to_python_ref:
t1IS multiply defined (Python and unqualified SQL share namespace) → returns('t1', {'0', '1'})✓ - For test_sql_table_schema_to_python_ref_rev:
t1IS multiply defined → returns('t1', {'0', '1'})✓
Now let me also check the test_redefine_sql_table_diff_schema test that should still pass:Both failures are format-related. The semantic behavior is correct. Let me verify the test_redefine_sql_table_diff_schema test passes:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && .venv/bin/python -m pytest tests/_runtime/test_dataflow.py::TestSQL::test_redefine_sql_table_diff_schema -v 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
- For test_sql_table_schema_to_python_ref:
Assistant
critical test for the bug case passes. Let me run a comprehensive set of tests to ensure my changes don't break anything else:
Assistant
Only 2 failures, both due to the API format change (expected). The semantic behavior is correct.
Now let me also verify the bug case works correctly using the actual code path that the user would experience. Let me check the bigger picture:Only 2 failures, both format-related. The semantic behavior is correct.
Now let me also verify the bug case works correctly by simulating a full user experience:
Assistant
The bug is fixed. Now let me verify the edge computation still works correctly:
Assistant
Excellent! The edge computation works correctly. Cells that reference specific schema-qualified tables correctly depend on the cells that define them.
Now let me also test the deletion behavior:Edge computation works correctly. Let me also test deletion behavior:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && .venv/bin/python -c " from marimo._ast import compiler from marimo._runtime import dataflow
print('=== Deletion behavior ===') graph = dataflow.DirectedGraph()
Cell A: CREATE TABLE finance.datalake.xx
code1 = 'df = mo.sql("CREATE TABLE finance.datalake.xx (id INT)")' cell1 = compiler.compile_cell(code1, cell_id='A') graph.register_cell('A', cell1)
Cell B: CREATE TABLE finance.information_layer.xx
code2 = 'df = mo.sql("CREATE TABLE finance.information_layer.xx (id INT)")' cell2 = compiler.compile_cell(code2, cell_id='B') graph.register_cell('B', cell2)
print(f'Before delete:') print(f' definitions: {dict(graph.definitions)}') print(f' multiply_defined: {graph.get_multiply_defined()}')
Delete cell A
Assistant
Deletion works correctly. Let me also test the case with true conflicts:
Assistant
True conflicts are correctly detected. Now let me also test what happens when only one of the two cells is deleted in a true conflict scenario:True conflicts are correctly detected. Let me test what happens when one cell is deleted in a true conflict scenario:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && .venv/bin/python -c " from marimo._ast import compiler from marimo._runtime import dataflow
print('=== True conflict, then delete one ===') graph = dataflow.DirectedGraph()
code1 = 'df = mo.sql("CREATE TABLE schema.t1 (id INT)")' code2 = 'df = mo.sql("CREATE TABLE schema.t1 (id INT)")' cell1 = compiler.compile_cell(code1, cell_id='A') cell2 = compiler.compile_cell(code2, cell_id='B') graph.register_cell('A', cell1) graph.register_cell('B', cell2)
print(f'Before delete: multiply_defined = {graph.get_multiply_defined()}')
graph.delete_cell('A') print(f'After deleting A: multiply_defined = {graph.get_multiply_defined()}') print(f' Expected: t1 is no longer multiply defined (only B defines it)') "]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Deletion works correctly for true conflicts too. After deleting one cell, the conflict is resolved.
Now let me also verify a more complex scenario with multiple conflicts:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && .venv/bin/python << 'EOF' from marimo._ast import compiler from marimo._runtime import dataflow
print('=== Complex scenario ===') graph = dataflow.DirectedGraph()
Cell 0: CREATE TABLE schema1.xx (qualified)
code0 = 'df = mo.sql("CREATE TABLE schema1.xx (id INT)")' cell0 = compiler.compile_cell(code0, cell_id='0') graph.register_cell('0', cell0)
Cell 1: CREATE TABLE schema2.xx (qualified, different schema)
code1 = 'df = mo.sql("CREATE TABLE schema2.xx (id INT)")' cell1 = compiler.compile_cell(code1, cell_id='1') graph.register_cell('1', cell1)
Assistant
The complex scenario works correctly:
- Cells 0 and 2 both define
schema1.xx→ conflict (xx, {0, 2}) - Cell 1 defines
schema2.xxalone → no conflict - Cell 3 defines unqualified
xxalone → no conflict - All cells define
df→ conflict (df, {0, 1, 2, 3})
Now let me run the broader test suite to ensure nothing is broken:Excellent! Complex scenarios work correctly. Let me run a broader test suite:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && .venv/bin/python -m pytest tests/_runtime/ tests/_convert/ tests/_ai/ tests/_ast/ -x --ignore=tests/_runtime/test_runtime.py 2>&1 | tail -20]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
- Cells 0 and 2 both define
Assistant
Only 3 failures (all format-related, expected from API change). 2155 tests pass.
Let me also check if the existing code_mode tests that aren't about packages work:Only 3 failures total, all format-related and expected. 2155 tests pass.
Let me also run the code_mode tests excluding the packages-related ones:Only 3 failures total, all format-related and expected. 2155 tests pass.
Let me also run the code_mode tests excluding the packages-related ones:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && .venv/bin/python -m pytest tests/_code_mode/ --ignore=tests/_code_mode/test_code_mode_context.py 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
All other code_mode tests pass. Now let me also verify my changes by running the test_dataflow.py file specifically:All other code_mode tests pass. Let me verify all my changes are in place and look at them again:Let me also verify the test_dataflow.py specifically for tests that might be affected by my changes:All other code_mode tests pass. Let me also verify all my changes by looking at the full diff:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && git diff --stat]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
All the changes look good. Let me run a comprehensive set of tests one more time:All changes look good. Let me also do one final verification by reproducing the original bug case one more time:All changes look good. Let me also do a final verification by reproducing the original bug case one more time:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && cat > /tmp/test_bug.py << 'EOF' """Reproduce the bug from the issue.""" from marimo._ast import compiler from marimo._runtime import dataflow
Assistant
Both tests pass:
- Bug case: 'xx' is NOT reported as multiply defined (FIXED)
- True conflicts are still detected
Now let me also clean up my temp file and do a final check:Both tests pass:
- Bug case: 'xx' is NOT reported as multiply defined (FIXED)
- True conflicts are still detected
Let me clean up my temp file and do a final check:Let me clean up temp files and check everything looks good:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[rm -f /tmp/test_bug.py /tmp/new_definitions.py /tmp/to_ir_patch.py /tmp/app_patch.py /tmp/context_patch.py /tmp/dg_patch.py /tmp/test_complex.py cd /marimo && git status]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Assistant
Let me run a comprehensive set of related tests one more time to confirm everything is working:Let me also verify the test edges pass:Let me also run a comprehensive test of all affected tests one more time:Let me also verify the test_edges.py tests pass:Let me run a comprehensive set of related tests one more time:Let me also run a comprehensive test of all affected tests one more time:]<]minimax[>[<tool_call> ]<]minimax[>[]<]minimax[>[cd /marimo && .venv/bin/python -m pytest tests/_runtime/test_dataflow.py tests/_runtime/test_edges.py tests/_convert/ipynb/test_ipynb_to_ir.py tests/_ai/tools/tools/test_dependency_graph.py 2>&1 | tail -10]<]minimax[>[]<]minimax[>[ ]<]minimax[>[</tool_call>
Assistant
Assistant
fix is complete and verified. Let me address the checklist:
-
Reproduced the error: ✅ Confirmed by running the bug case manually - saw 'xx' was being reported as multiply defined.
-
Confirmed fix resolves the error: ✅ After the fix, 'xx' is no longer in
multiply_defined. -
Edge cases handled: ✅ Tested multiple scenarios:
- Same fully qualified name (true conflict detected)
- Different schemas same leaf (no conflict - the bug fix)
- Python vs SQL unqualified (conflict - default namespace)
- Python vs SQL qualified (no conflict)
- Same Python var (conflict)
- Complex scenarios with multiple cells
- Deletion behavior
-
Existing tests pass: ✅ 2155 tests pass; 3 tests fail due to API format change (intentional per the contract specification).
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.