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 understanding the issue and the repository structure. The issue is about SQL cells in marimo where creating tables with the same name in different schemas leads to a variable redefinition error, even though they're different tables.
Let me first explore the repository structure and understand the codebase.
Assistant
Let me look at the file mentioned in the interface -
marimo/_runtime/dataflow/definitions.py.Assistant
Let me understand more about the VariableData class and how SQL references work.
Assistant
Let me look at the VariableData class and the SQL visitor to understand how SQL definitions work.
Assistant
Let me look at the SQL visitor to understand how SQL defs and refs work, particularly how qualified names are handled.
Assistant
Let me look at the SQL visitor to understand how SQL definitions work.
Assistant
Now let me understand the issue better. The
get_multiply_definedmethod indefinitions.pycurrently just groups by name (unqualified), sofinance.datalake.xxandfinance.information_layer.xxwould both map toxxand be considered multiply defined.The fix needs to track definitions by their qualified name for SQL variables. Let me look at where
get_multiply_definedis called to understand the full context.Let me also look at how
register_definitionworks and how thequalified_nameis used.Assistant
Let me look at where
get_multiply_definedis used to understand how it's used, particularly inapp.pyand_context.py.Assistant
Let me look at how
get_multiply_definedis used inapp.pyand the_context.pyto understand what the output is used for.Assistant
Now let me look at how
register_definitionhandles qualified names, and understand the flow better.Assistant
Let me look at how
register_definitionis called to understand the flow:Assistant
Let me look at how
register_cellin graph.py callsregister_definition:Assistant
Now let me look at how SQL definitions are populated in the visitor to understand how
nameandqualified_nameare set:Assistant
Let me look at the
_definemethod in the visitor:Assistant
Now let me understand the full flow. Let me look at the
_definemethod:Assistant
Now let me look at the critical part of how SQL defs are processed in the visitor, to understand the name and qualified_name:
Let me look at lines 710-780 of visitor.py:
Assistant
Assistant
I need to search for
get_defining_cellsusage in the repository. Let me use the bash tool to grep.Assistant
Now I have a good understanding. Let me think about the approach. The key insight from the interface specification is:
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.
So the fix is:
- In
register_definition, for SQL variables wherequalified_name != name, usequalified_nameas the key inself.definitions - Change
get_multiply_definedto returnlist[tuple[Name, set[CellId_t]]] - Update
get_defining_cellsto handle both qualified and unqualified lookups - Update
unregister_definitionsto handle the new key system - Keep
keysproperty working
Let me also think about
keys:<tool_call> <function=bash_terminal> <parameter=command> grep -rn ".keys" /marimo/marimo/_runtime/dataflow/definitions.py </tool_call>
- In
Assistant
Let me search for
.keysin the context where it's used with the definition registry.Assistant
So
definitions.keysis used in several places:ctx.graph.definitions.keys()- to get all defined namesself.definition_registry.definitions.keys()- for set operations with refs
Now let me look at line 316 of graph.py more closely:
queue: set[Name] = refs & self.definition_registry.definitions.keys()This computes the intersection of refs and defined names. If definitions are now keyed by qualified names (e.g., "finance.datalake.xx") instead of unqualified names (e.g., "xx"), we need to make sure this still works.
Looking at the ref side, refs are stored as
sql_ref.qualified_name, so they'd be "finance.datalake.xx" too. This should work!But there's also
self._defs = self._graph.definitions.keys()in app.py line 604, which stores all defined names. If these are now qualified names, would that cause issues elsewhere?Let me think about what
self._defsis used for:<tool_call> <function=bash_terminal> <parameter=command> grep -rn "_defs|.defs\b" /marimo/marimo/_ast/app.py | head -20 </tool_call>
Assistant
Let me search for _defs usage in app.py.
Assistant
Assistant
Let me search for
find_sql_hierarchical_matchesusage in the repository.Assistant
Let me check the
edges.pyfile to see howfind_sql_hierarchical_matchesis used.Assistant
Assistant
Let me search for how
_defsis used in app.py.Assistant
So
_defsis used in_globals_to_defsto filter globals. The globals dict has unqualified names, so_defsshould only include names that exist in the globals dict.With my change,
definitions.keys()would include qualified names like "finance.datalake.xx". Butglblswould only have "xx". So the checkif name in glblswould filter out qualified names, and we'd only get variables that are actually in globals.But wait,
self._defsis also used elsewhere? Let me check line 931 and 1085.Actually,
self._defsseems to be used primarily for_globals_to_defs. The checkif name in glblshandles the case where a name in_defsdoesn't exist in globals.But is
self._defsused elsewhere? Let me search more broadly:Assistant
Assistant
Let me search for tests related to multiply_defined or SQL definitions.
Assistant
Let me look at the test files to understand how
get_multiply_definedis tested.Assistant
Let me look at the test code to understand how the tests work.
Assistant
Assistant
Let me look at more of the test file to understand the test context.
Assistant
Now I have a good understanding of the issue. Let me read the test file more carefully to understand the expected behavior:
Assistant
Now I have a clear understanding of the issue. Let me trace through the bug:
- Cell 1:
CREATE TABLE finance.datalake.xx (id INT)→ name="xx", qualified_name="finance.datalake.xx", kind="table" - Cell 2:
CREATE TABLE finance.information_layer.xx (id INT)→ name="xx", qualified_name="finance.information_layer.xx", kind="table"
In
register_definition, both cells end up indefinitions["xx"]because:- Cell 1:
typed_def = ("xx", "table")not intyped_definitions→ else branch → adds todefinitions["xx"] - Cell 2:
typed_def = ("xx", "table")IS intyped_definitions→ conditionname in definitions AND typed_def NOT in typed_definitionsis False → goes to else → adds todefinitions["xx"]
The fix: use qualified name as the key in
definitionsfor SQL variables with qualified names.Let me implement the fix:
- Cell 1:
Assistant
Looking at the test and the code, I can now see the bug clearly:
- Cell 1:
CREATE TABLE schema1.t1 (i INTEGER)→ name="t1", qualified_name="schema1.t1", kind="table" - Cell 0:
t1 = 123→ Python variable, name="t1"
The test expects
multiply_defined == []because the SQL table is qualified with a schema. But currently, both cells end up indefinitions["t1"]becausedefinitionsuses unqualified names as keys.The fix is to use the qualified name as the key in
definitionsfor SQL variables wherequalified_name != name. Let me implement this.- Cell 1:
Assistant
Now let me run the existing tests to see what breaks:
Assistant
Let me install the missing dependencies and try again.
Assistant
Let me try running the tests again:
Assistant
Let me install all needed dependencies:
Assistant
Let me look at the repository structure to understand how to run tests properly.
Assistant
Let me try installing the package in development mode to get all dependencies.
Assistant
Let me try running the tests again:
Assistant
Let me find the correct test names:
Assistant
Let me find the correct test class names and run them.
Assistant
Let me check the test structure to find the correct test class names:
Assistant
Good, so the test class is
TestSQL. Let me run the tests from that class.Assistant
Let me install duckdb and run the tests again:
Assistant
Let me run the tests now that duckdb is installed.
Assistant
The tests show that my logic is correct -
t1IS being flagged as multiply defined (which is the correct behavior for unqualified names). The issue is just the return type changed fromlist[Name]tolist[tuple[Name, set[CellId_t]]]. I need to update the callers to handle the new return type.Let me find all callers and update them:
Assistant
Now I need to update all callers to handle the new return type. Let me update each file.
Assistant
I need to update the return type in
graph.pyand all callers to handle the new tuple return type. Let me update each file.Assistant
Let me now update the graph.py file to match the new return type, and then update all callers.
Assistant
Let me update all callers to handle the new return type. First, let me look at each caller:
Assistant
Let me update each caller to handle the new return type. First, let me check each one:
Assistant
Let me now update all callers to handle the new
list[tuple[Name, set[CellId_t]]]return type:Assistant
Let me update the
_ai/_tools/tools/dependency_graph.pyfile:Assistant
I need to update all callers to handle the new
list[tuple[Name, set[CellId_t]]]return type. Let me update each file:Assistant
Let me now update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. I'll update each file one by one.Assistant
I need to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me check each caller and update them.Assistant
Now I need to update all callers to handle the new return type
list[tuple[Name, set[CellId_t]]]. Let me find and update each one:/marimo/marimo/_ai/_tools/tools/dependency_graph.py:191-multiply_defined = sorted(graph.get_multiply_defined())/marimo/marimo/_code_mode/_context.py:1561-existing_multiply_defined = set(graph.get_multiply_defined())/marimo/marimo/_code_mode/_context.py:1593-set(graph.get_multiply_defined()) - existing_multiply_defined/marimo/marimo/_ast/app.py:612-multiply_defined_names = self._graph.get_multiply_defined()/marimo/marimo/_convert/ipynb/to_ir.py:164-multiply_defined_names = graph.get_multiply_defined()
Assistant
Now I need to update all callers to handle the new
list[tuple[Name, set[CellId_t]]]return type. Let me check and update each file.Assistant
Now I need to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me update each file:Assistant
I need to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me update each file:Assistant
I need to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me update each file.Assistant
Let me look at each caller more carefully and update them one by one.
Assistant
Let me look at the full context of how
multiply_definedis used in the dependency_graph.py file:Assistant
Let me update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type.Assistant
Let me update all callers of
get_multiply_definedto handle the new return type.Assistant
Now I need to update the
_build_next_stepsfunction signature and usage in dependency_graph.py, and all other callers.Assistant
Let me update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type.Assistant
Let me update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. First, let me update the_build_next_stepsfunction independency_graph.py:Assistant
The user wants me to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me fix my tool call syntax and update each file one by one.I need to:
- Update
_build_next_stepsindependency_graph.py - Update
_context.pyin code_mode - Update
app.pyin ast - Update
to_ir.pyin convert/ipynb - Update the test file
Let me start with the edit command.
- Update
Assistant
The user wants me to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me fix my tool call syntax and update each file one by one.I need to use the
editcommand with proper syntax. Let me try again.Assistant
I need to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me update each file one by one using theeditcommand with proper syntax.Let me first update the
_build_next_stepsfunction independency_graph.py.Assistant
The user wants me to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. I need to use the correct tool call syntax for theeditcommand. Let me try using thebashcommand to make the changes instead.Assistant
Let me update the
_build_next_stepsfunction independency_graph.pyto handle the new return type. I need to change the join line to extract just the name from each tuple.Assistant
I need to update all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me check the current state of the files and update them.Let me first check the current state of the dependency_graph.py file, then update the remaining callers.
Assistant
Now I need to update all the remaining callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me check each one and update them.Assistant
I need to update the
_context.pyfile to handle the new return type. Let me update the set operations and iteration.Assistant
I need to continue updating all callers of
get_multiply_definedto handle the newlist[tuple[Name, set[CellId_t]]]return type. Let me check and update the remaining files.Assistant
I need to update the
_context.pyfile to handle the new return type. Let me update the set operations and iteration.Assistant
Let me check the current state of the _context.py file to see if the sed command worked correctly.
Assistant
I need to fix the extra
)that was left behind. Let me clean that up.Assistant
I need to remove the extra
)on line 1596. Let me fix this.Assistant
Let me verify the fix is correct.
Assistant
Let me verify the fix is correct and continue updating the remaining callers.
Assistant
Now I need to update the remaining callers of
get_multiply_defined. Let me updateapp.pyandto_ir.py:Assistant
Let me continue updating the remaining callers of
get_multiply_defined. I need to updateapp.pyandto_ir.py:Assistant
Now I need to update the remaining callers of
get_multiply_defined. Let me checkto_ir.py:Assistant
Let me continue updating the remaining callers of
get_multiply_defined. I need to updateto_ir.py:Assistant
Now let me update the test file to handle the new return type:
Assistant
Now let me update the test file to handle the new return type:
Assistant
I need to update the test file to handle the new return type. Let me check and fix the tests.
Assistant
The user wants me to continue updating the remaining callers of
get_multiply_defined. Let me check and update any remaining files that need changes, and then run the tests.Assistant
Let me verify all callers are updated and run the tests:
Assistant
All three SQL-related tests pass. Let me now run the full test suite for the dataflow module to make sure nothing is broken:
Assistant
Assistant
Let me look at the
_resolve_variable_namefunction and understand the edge computation issue.Assistant
Let me look at the
_resolve_variable_namefunction.Assistant
Assistant
The user wants me to investigate the failing tests and fix the issues. Let me check the test results and understand what's happening.
Assistant
Let me check what's happening with the test failures by looking at the test code and understanding the dataflow computation.
Assistant
Let me debug this by running a simple test to understand what's happening with the definitions:
Assistant
The user wants me to debug the failing tests. Let me find the correct import for
parse_celland understand what's happening.Assistant
The grep command returned an error (exit code 1 means no matches found). Let me search for parse_cell differently.
Assistant
The user wants me to debug the failing tests. Let me find where
parse_cellis imported from in the test file and understand the dataflow issue.Assistant
The user wants me to debug the failing tests. Let me run a simple test to understand what's happening with the definitions after my changes.
Assistant
Looking at the output, I can see the issue:
- The first cell (
CREATE TABLE t1) definest1withqualified_name=t1(the same as the unqualified name since it's not schema-qualified) - The second cell (
SELECT * from t1) hasrefs: {'mo'}and NO ref tot1!
The issue is that
t1is listed indefined_namesin the visitor (line 742:defined_names.add(_table.qualified_name)), so when the visitor processes refs for the second cell, it skipst1because it's in .- The first cell (
Assistant
Let me continue debugging the issue. I need to understand why the SQL ref isn't being detected properly.
Assistant
Let me continue debugging. I need to install
sqlglotto properly debug the issue.Assistant
I need to debug the failing tests to understand why SQL chains aren't working with my changes. Let me run the debug script again now that sqlglot is installed.
Assistant
I need to debug the failing tests. Let me run a more comprehensive test to understand the dataflow issue.
Assistant
Good, the test is passing when I run it manually! Let me re-run the pytest tests to see if they pass now:
Assistant
The test_sql_chain tests are now passing! Let me run all the failing tests to see which ones are still failing:
<tool_call> <function=bash_terminal> <parameter=command> cd /marimo && python -m pytest tests/_runtime/test_dataflow.py::TestSQL -v 2>&1 | tail -40 </tool_call>
Assistant
The test_sql_chain tests are now passing! Let me run all the SQL tests to see which ones are still failing.
Assistant
All SQL tests are passing. Let me run the full test suite for the dataflow module.
Assistant
All 58 tests in the dataflow test suite pass. Let me run the tests for the other files I modified to make sure nothing else is broken.
Assistant
The user's tests are passing for the main dataflow tests, but there's a failure in the dependency graph test. Let me investigate the test failure.
Assistant
Let me look at the failing test to understand the issue.
Assistant
Let me look at the test file to understand the mock setup and fix the issue properly.
Assistant
The issue is that the test mock for
get_multiply_definedreturns a list of strings, but my updated code expectslist[tuple[Name, set[CellId_t]]]. Let me fix the test mock.Assistant
The issue is clear. The test mock for
get_multiply_definedreturns a list of strings (the old format), but my updated code expectslist[tuple[Name, set[CellId_t]]]. I need to fix the test mock intest_dependency_graph.pyto return the new format.Assistant
The issue is clear. The test mock for
get_multiply_definedreturns a list of strings (the old format), but my updated code expectslist[tuple[Name, set[CellId_t]]]. I need to fix the test mock intest_dependency_graph.pyto return the new format.Assistant
The user wants me to fix the test mock for
get_multiply_definedto return the new formatlist[tuple[Name, set[CellId_t]]]. Let me fix this properly.Assistant
I see there's a syntax error - there's a
]on line 64 that shouldn't be there. Let me fix this properly.Assistant
The sed command introduced a syntax error. Let me fix the test file properly.
Assistant
The sed command introduced a syntax error. Let me fix the test file properly.
Assistant
Let me fix the test file properly. The sed command introduced a syntax error. I need to fix line 62-64.
Assistant
Let me fix the test file properly. The sed command introduced a syntax error. I need to fix line 64.
Assistant
Let me check the current state of the test file.
Assistant
Let me fix the test file properly. The list comprehension for
get_multiply_defined.return_valueneeds to return tuples instead of just names.Assistant
Let me check the current state of the test file to see if the sed command worked correctly.
Assistant
Let me remove the duplicate line that was created by the sed command.
Assistant
Let me check the current state of the test file.
Assistant
Let me now run the tests to see if the fix works.
Assistant
The test is failing because the
GetCellDependencyGraphOutputnow hasmultiply_definedaslist[tuple[str, set[str]]]instead oflist[str]. The test mock returns the correct new format, but the test assertion still expects the old format. I need to update the test assertion.Assistant
Let me fix the test assertion on line 263 to expect the new tuple format.
Assistant
Let me check the current state of the test file and run the tests again.
Assistant
All 10 tests in the dependency graph test suite pass. Now let me run the full test suite for the code_mode tests to make sure the changes there are correct.
Assistant
Let me install the missing module and run the tests again.
Assistant
The user wants me to continue running the tests to verify my changes. Let me run the tests.
Assistant
Let me look at the test failures to understand what's going wrong. The sed command I used earlier likely introduced a syntax error.
Assistant
Let me look at the actual test failure to understand the issue.
Assistant
Let me look at the actual test failure to understand the issue.
Assistant
The test failures are due to missing pytest-asyncio plugin, not my code changes. Let me verify my changes work correctly by creating a simple test script.
Assistant
The user wants me to continue fixing the test failures. The test failures in
test_code_mode_context.pyare due to missing pytest-asyncio plugin. Let me install the plugin and verify the changes work correctly.Assistant
The user wants me to continue fixing the test failures. The test failures in
test_code_mode_context.pyare likely due to my changes to theget_multiply_definedreturn type. Let me check what's going on.Assistant
Now that pytest-asyncio is installed, let me run the full test suite for the code_mode tests.
Assistant
The user wants me to continue fixing the test failures. The test failures in
test_code_mode_context.pyare related to package management tests (pixi not being available), not my changes toget_multiply_defined. Let me verify that the tests directly related to my changes pass.Assistant
All 38 non-Package tests pass. The Package tests fail because pixi is not available in the environment, which is unrelated to my changes. Let me now run the ipynb conversion tests to verify the to_ir.py changes.
Assistant
Let me find the relevant tests for ipynb conversion.
Assistant
Let me run the ipynb tests to verify the to_ir.py changes.
Assistant
All tests pass. Let me now run a final verification to make sure the core fix works correctly.
Assistant
All edge cases pass. Let me now run the final comprehensive test suite to verify the complete fix.
Assistant
All 176 tests pass. Let me review all the changes I made and submit them.
Assistant
Let me submit my changes. The fix has been verified with comprehensive tests.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.