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
I'll start by exploring the repository structure to understand the codebase and then locate the relevant files for this issue.
Assistant
Let me look at the specific file mentioned in the issue:
marimo/_runtime/dataflow/definitions.pyAssistant
I made an error with the open command. Let me use the correct format without the file parameter.
Assistant
Let me understand the issue better. The problem is that when two SQL cells create tables with the same name but in different schemas (e.g.,
finance.datalake.xxandfinance.information_layer.xx), marimo incorrectly reports a variable collision on the leaf namexx.Let me explore how SQL definitions are registered:
Assistant
Now let me look at the
VariableDataclass and how SQL variables are handled:Assistant
Now let me look at the
get_multiply_definedmethod and understand what it returns:Assistant
Let me see the current implementation and the interface specification from the issue:
Assistant
Now I see the issue. According to the interface specification:
Type: Function
Name: get_multiply_defined
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.The current implementation returns
list[Name]but the interface specifieslist[tuple[Name, set[CellId_t]]]. Let me also look at how SQL qualified names work:Assistant
Let me look at the
register_definitionmethod more closely and understand how qualified names work:Assistant
Now I understand the issue better. Let me look at how SQL definitions are tracked and look at tests:
Assistant
Let me search for SQL-related tests:
Assistant
Let me look at some SQL-related tests:
Assistant
Now I can see that the test expects
multiply_defined == []for qualified tables with different schemas. Let me look at the test case more carefully:Assistant
Interesting! The test
test_redefine_sql_table_diff_schemaexpects that when a Python variablet1is defined and then a SQL tableschema1.t1is defined, it should NOT be considered multiply defined because of the schema qualification.Now let me look for tests that specifically test the issue in the bug report - two SQL tables with the same name in different schemas:
Assistant
No test exists for two SQL tables in different schemas. Let me create a reproduction script first:
Assistant
I've confirmed the bug. The issue is:
- Both SQL cells define a table with the leaf name
xxbut different qualified names (finance.datalake.xxvsfinance.information_layer.xx) - The
register_definitionmethod currently only checks ifqualified_name == namebut this doesn't handle the case where two SQL cells have the same leaf name but different qualified names - Both cells are added to
self.definitions['xx'], causingget_multiply_defined()to return['xx']
Now let me understand the interface specification more carefully:
The interface spec says:
- Output:
list[tuple[Name, set[CellId_t]]]- returns tuples of (name, cell_ids) for names defined in multiple cells - Description: "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."
Let me look at the current implementation of
register_definition:- Both SQL cells define a table with the leaf name
Assistant
Now I understand the issue. The problem is in the
register_definitionmethod. The current logic is:pythonif ( name in self.definitions and typed_def not in self.typed_definitions ): # Duplicate if the qualified name is no different if variable.qualified_name == name or variable.language != "sql": self.definitions[name].add(cell_id) else: self.definitions.setdefault(name, set()).add(cell_id)The issue is that when both cells define SQL tables with the same leaf name but different qualified names:
- Cell 1:
name='xx',
- Cell 1:
Assistant
Let me check how
get_multiply_definedis used in these files:Assistant
Now I see the usage. The current return type is
list[Name]and it's used to check if there are any multiply-defined names. The interface spec says the new return type should belist[tuple[Name, set[CellId_t]]].Let me check what the test expects:
Assistant
Assistant
Let me also check what uses
definition_registry.definitionsoutside this file:Assistant
I see that
definitionsis accessed in multiple places. Let me think about this more carefully.Looking at the evaluation contract again:
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.
This suggests that:
- Tables with the same qualified name SHOULD be multiply defined (true collision)
- Tables with the same unqualified name (without schema/catalog) SHOULD be multiply defined
Assistant
Interesting! In this case, the Python
t1and SQLschema1.t1are NOT in the same definition set. Let me trace through why:For cell 0 (Python
t1 = 123):name = 't1'variable.kind = 'variable'variable.qualified_name = Nonevariable.language = 'python'typed_def = ('t1', 'variable')
For cell 1 (SQL
CREATE TABLE schema1.t1):name = 't1'variable.kind = 'table'variable.qualified_name = 'schema1.t1'variable.language = 'sql'typed_def = ('t1', 'table')
When registering cell 0:
name='t1'not inself.definitions- Goes to
elsebranch:self.definitions['t1'] = {'0'}
Assistant
Now I see the problem. Let me trace through:
For cell 1 (SQL
CREATE TABLE finance.datalake.xx):name = 'xx'typed_def = ('xx', 'table')name='xx'not inself.definitions- Goes to
else:self.definitions['xx'] = {'cell1'} self.typed_definitions[('xx', 'table')] = {'cell1'}
For cell 2 (SQL
CREATE TABLE finance.information_layer.xx):name = 'xx'typed_def = ('xx', 'table')name='xx'IS inself.definitionstyped_def=('xx', 'table')IS inself.typed_definitions(cell 1 added it!)- So the condition
name in self.definitions and typed_def not in self.typed_definitionsis False - Goes to
else:self.definitions['xx'].add('cell2')
The bug is that when
typed_defis already inself.typed_definitions, it still adds to the definitions without checking if the qualified names are the same.Assistant
Now I need to implement the fix. Based on my analysis, the fix should be in
register_definition. The key insight is:For SQL variables with a qualified name different from the leaf name, we should only add to
self.definitions[name]if there's no existing SQL definition with a different qualified name.Assistant
Good,
typed_definitionsis only used withindefinitions.py. Now let me think about the solution:The interface spec says:
- Output:
list[tuple[Name, set[CellId_t]]] - Description: "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."
I think the cleanest solution is:
- Track definitions by their conflict key - for SQL variables with qualified names, the conflict key is the qualified name; for others, it's the leaf name.
- Change
get_multiply_defined()to returnlist[tuple[Name, set[CellId_t]]].
But wait, let me re-read the existing logic. The
typed_definitionsdict groups by(name, kind), and multiple cells can be in the same group. The dict groups by , and currently, both SQL tables with the same leaf name end up in the same group even if they have different qualified names.- Output:
Assistant
Good, I've confirmed all the test cases. Only Test 1 fails (the bug case). Now let me design the fix.
The issue is in
register_definition. When a SQL variable has a qualified name that differs from existing definitions for the same (name, kind), it should NOT be added to the same definition set.Let me trace through what happens for Test 1:
-
Cell 1:
name='xx',qualified_name='finance.datalake.xx',kind='table'typed_def = ('xx', 'table')name not in definitions→ goes toelseself.definitions['xx'] = {'cell1'}self.typed_definitions[('xx', 'table')] = {'cell1'}
-
Cell 2:
name='xx',qualified_name='finance.information_layer.xx',kind='table'typed_def = ('xx', 'table')name in definitions= Truetyped_def in typed_definitions= True (cell1 added it!)- Condition = False
-
Assistant
Looking at the error, it seems the tool is having trouble with my function call format. Let me try a different approach and use bash commands instead.
Assistant
Let me now think about the proper fix. Looking at the interface spec again:
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.So the fix involves:
- Change
get_multiply_defined()to returnlist[tuple[Name, set[CellId_t]]] - Make the grouping "conflict-aware" - SQL names with different qualified names should not conflict
Looking at the code, I think the best approach is to:
- Keep track of definitions by a conflict key that considers the qualified name for SQL variables
- Modify
get_multiply_defined()to use this conflict-aware grouping
But wait, looking at the data structures,
self.definitionsis keyed byName(leaf name). Changing this would break other parts of the code.- Change
Assistant
Now let me test the fix:
Assistant
I see several issues:
- Test 1 passes now, which is the main bug fix
- Test 2 passes
- Test 3 fails because the return type changed from
list[Name]tolist[tuple[Name, set[CellId_t]]] - Test 4 fails - two SQL tables with the same qualified name should be multiply defined
- Test 5 fails because of return type
Let me fix my test script to handle the new return type and also fix Test 4:
Assistant
Good progress! Test 4 is failing. Let me debug this:
For Test 4:
- Cell 1:
name='t1',qualified_name='schema1.t1',kind='table' - Cell 2:
name='t1',qualified_name='schema1.t1',kind='table'
The issue is that when Cell 1 registers, it doesn't add to
self.definitionsbecause the qualified name differs from the leaf name. Then when Cell 2 registers, it SHOULD detect that there's a conflicting definition with the same qualified name, but it only adds Cell 2 to definitions (not Cell 1).The problem is in my logic - when a SQL table with a qualified name registers for the first time, it doesn't add to
self.definitions[name]becauseconflict_key != name. But when the second cell with the SAME qualified name registers, it should detect the conflict and add BOTH cells to definitions.Let me fix the logic:
- Cell 1:
Assistant
All tests pass now. But my implementation is getting complex. Let me simplify it and think about edge cases more carefully.
Looking at the current tests in the repository, I see that:
test_redefine_sql_table_diff_schemaexpectsmultiply_defined == []for Pythont1+ SQLschema1.t1test_sql_table_schema_to_python_refexpectsmultiply_defined == ["t1"]for SQLt1+ Pythont1
But the existing tests use
list[Name]as the return type. Let me check if my new return type will break these tests:Assistant
The existing tests compare with
[]and["t1"]. My new implementation returns[('t1', {'cell_ids'})]which won't match["t1"].Looking at the interface spec again:
Output:
list[tuple[Name, set[CellId_t]]]This is a breaking change to the return type. The tests need to be updated, but I'm told NOT to modify tests. Let me re-read the 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.
The interface spec says the function should return
list[tuple[Name, set[CellId_t]]]. However, the existing tests expectlist[Name]. Since I can't modify tests, I need to think about whether the return type should be changed or not.Assistant
The code in
app.pyusesmultiply_defined_names[0]- with the new return type, this would be a tuple(name, cell_ids)instead of justname. I need to update the callers to handle the new return type.Let me also check the other callers:
Assistant
I need to update all the callers to handle the new return type. Let me do this:
/marimo/marimo/_ast/app.py- usesmultiply_defined_names[0]/marimo/marimo/_code_mode/_context.py- usesset(graph.get_multiply_defined())/marimo/marimo/_ai/_tools/tools/dependency_graph.py- usessorted(graph.get_multiply_defined())/marimo/marimo/_convert/ipynb/to_ir.py- usesfor name in multiply_defined_names
Let me update these files:
Assistant
Now let me update
_code_mode/_context.py:Assistant
The code uses
set(graph.get_multiply_defined()). With the new return type returning tuples, this should still work because tuples are hashable. But let me check if the comparison works correctly:Assistant
I see - sets are not hashable, so I can't use tuples with sets in a set. I need to update the code to extract just the names from the tuples for comparison. Let me fix all the callers.
Assistant
I need to update the code to extract just the names from the tuples. Let me do that:
Assistant
Now let me update
dependency_graph.py:Assistant
Now let me update
to_ir.py:Assistant
I need to continue working on updating the code in to_ir.py. Let me use the bash_terminal tool to make the changes.
Assistant
I need to update the
for name in multiply_defined_names:loop to handle the new return type which is nowlist[tuple[Name, set[CellId_t]]]. I should change it to iterate over just the names.Assistant
Now let me run the tests to verify the fix works:
Assistant
All tests pass. Now let me run the existing repository tests to make sure the changes don't break anything:
Assistant
The test failure is expected because the return type changed as specified in the interface. According to the instructions, test failures due to return type changes should be ignored. Let me continue testing the other tests:
Assistant
The test is failing because the return type changed from
list[Name]tolist[tuple[Name, set[CellId_t]]]. This is expected according to the interface specification, and the instructions say I should ignore test failures due to updated return types.Let me run the remaining tests without the -x flag to see all the failures:
Assistant
OK so I have 2 test failures, both due to the return type change from
list[Name]tolist[tuple[Name, set[CellId_t]]]. This is expected and by design per the interface spec.Let me also run some other tests to make sure I haven't broken anything else:
Assistant
Good progress! The test results show:
- 56 tests passed
- 2 tests failed (both due to the expected return type change from
list[Name]tolist[tuple[Name, set[CellId_t]]])
Let me verify the original bug is fixed by running the reproduction script:
Assistant
Excellent! The bug is fixed. Now let me verify that all the callers have been properly updated and check if there are any other places that might be affected:
Assistant
I notice that
graph.pyalso has aget_multiply_definedmethod with the old return typelist[Name]. Let me update it to match the new return type:Assistant
I need to continue working on updating the code in graph.py. Let me use the bash_terminal tool to make the changes.
Assistant
Let me verify that the graph.py file was updated correctly and check if the CellId_t type is imported.
Assistant
CellId_t is already imported in the TYPE_CHECKING block, so the type annotation should work. Now let me verify the graph.py file was updated correctly and run the tests again.
Assistant
Good, the graph.py file is updated. Now let me run the tests again to make sure everything still works:
Assistant
All tests pass. Let me now run the reproduction script to confirm the bug is fixed, then run the existing tests to verify nothing is broken.
Assistant
The bug is fixed. Let me now run the existing tests to verify that my changes don't break other functionality. I need to check if there are any other tests that might be affected by the changes.
Assistant
The tests are running as expected - 56 passed and only 2 failed due to the return type change. Let me now verify that other parts of the codebase that use
get_multiply_definedwork correctly. Let me run some additional tests to make sure I haven't broken anything.Assistant
I see there's an issue with the dependency graph test. Let me investigate this.
Assistant
The test is failing because it seems like
get_multiply_defined()is returning a list of strings instead of tuples. Let me check what's happening. The error says "not enough values to unpack (expected 2, got 1)", which suggests thatgraph.get_multiply_defined()is returning a list of strings instead of tuples.Wait, looking at the error, it seems like the graph object in the test might be using a different
get_multiply_definedmethod. Let me check the test to understand what's happening.Assistant
I see. The test is creating a mock graph with
definitions={"x": {"c1", "c2"}}, and the graph'sget_multiply_definedmethod is being called. The issue is that the test is directly settingdefinitionsbut not going throughregister_definition, so the new logic inregister_definitionthat populatesqualified_definitionsis not being triggered.Let me check how the graph is being created in the test:
Assistant
...
Assistant
I see the issue. The test is mocking
get_multiply_definedto return a list of names directly, not tuples. This is expected to fail because the test needs to be updated to reflect the new return type. According to the instructions, test failures due to updated return types should be ignored.Let me verify all the changes I've made are correct and complete:
Assistant
Let me review all the changes I've made and make sure everything is correct. I need to:
- Verify the
definitions.pychanges are correct - Verify all the callers have been updated
- Run some additional edge case tests
- Run the existing tests to make sure nothing is broken
Let me check the current state of the definitions.py file and make sure my implementation is correct.
- Verify the
Assistant
The diff looks good. Let me now write a comprehensive test to make sure all edge cases are handled correctly, then run the full test suite.
Assistant
All comprehensive tests pass. Let me summarize what I've done and submit the solution.
Summary
I've fixed the bug where SQL tables with the same leaf name but different schemas were incorrectly flagged as multiply defined.
Changes Made:
-
marimo/_runtime/dataflow/definitions.py:- Added
qualified_definitionsdict to track SQL definitions by their fully qualified names - Added
_get_effective_name()helper to determine the conflict key for a definition - Modified
register_definition()to use conflict-aware grouping:- SQL tables with the same qualified name DO conflict
- SQL tables with different qualified names do NOT conflict
- Added
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.