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:
- When using SQL cells in marimo with DuckDB, creating tables with the same name in different schemas leads to a variable redefinition error.
- The issue is that marimo treats tables with the same name but in different schemas as multiply-defined variables.
- The fix should be in
get_multiply_definedinmarimo/_runtime/dataflow/definitions.py.
Let me first explore the repository structure and find the relevant file
Assistant
Let me look at the definitions.py file to understand the current code.
Assistant
Let me explore more about the data model - how SQL references and qualified
Assistant
Let me try one command at a time.
Assistant
Let me look at the sql_visitor.py to understand SQLRef and the data model
Assistant
Let me look at relevant test files and understand how
VariableDatais structured:Assistant
Let me look at the visitor.py to understand VariableData and how SQL definitions are
Assistant
Let me get the rest of the visitor.py file and see how SQL definitions are handled
Assistant
Let me see the visit_Call method and the surrounding code.
Assistant
Now let me look at the tests for definitions to understand expected behavior:
Assistant
Let me look at the test files
Assistant
Let me search for tests specifically for
get_multiply_defined:Assistant
Let me look at the test cases around line 579:
Assistant
Let me check how
get_multiply_definedis used throughout the codebaseAssistant
Let me look at the graph.py implementation and how get_multiply_defined is used
Assistant
Let me look at the callers to understand the expected return format
Assistant
Let me check all callers and how they use the
Assistant
Let me first write a script to reproduce the issue:
Assistant
Let me install dependencies and reproduce:
Assistant
Now let me reproduce the
Assistant
Let me adjust the reproduction script to avoid the
dfvariableAssistant
Let me test with the exact names from the issue:
Assistant
Let me add some debugging to understand what
Assistant
The issue is that
find_sql_defsreturns empty results. Let me investigate whyAssistant
DuckDB is not installed, which is why
find_sql_defsreturns empty. Let me install it:Assistant
Now let me reproduce the issue with DuckDB installed
Assistant
I've confirmed the bug. Now let me understand how
typed_definitionsis used throughout the codebase to ensure my fix doesn't break anything:Assistant
Let me check how
typed_definitionsis used in edges.py and understand the full picture:Assistant
Let me understand how definitions are unregistered to ensure my fix is complete:
Assistant
Let me look at the graph.py register_cell
Assistant
Now I have a clear understanding of the bug and the fix needed. Let me implement it:
The issue is in
register_definition: when a SQL variable has a qualified name (e.g.,schema.table), and the same(name, kind)pair already exists but with a different qualified name, it still adds toself.definitions[name], causing a false positive inget_multiply_defined.The fix:
- Add a
_qualified_definitionsfield to track definitions by qualified name - Modify
register_definitionto not add qualified SQL definitions toself.definitionswhen the qualified name differs - Update
unregister_definitionsto clean up `_qualified
- Add a
Assistant
Now let me implement the fix. My approach:
- Add a
_qualified_definitionsfield:dict[str, set[CellId_t]] - Modify
register_definitionto use qualified names for conflict detection - Update
unregister_definitionsto clean up `_qualified
- Add a
Assistant
Now let me verify the fix with the reproduce script
Assistant
The basic fix works. Now let me test all edge
Assistant
All edge cases pass. Now let me run the existing tests to make sure nothing is broken.
Assistant
Let me debug this test failure:
Assistant
Let me check the test more carefully to see what it expects:
Assistant
Let me check if this test was failing before my changes:
Assistant
The test was already failing before my changes — it's a pre-existing issue. Let me restore my changes and run the full test suite skipping that test.
Assistant
Let me run the full test suite for the dataflow tests, skipping the pre-existing failing test:
Assistant
Let me check if these 5 SQL tests were also failing before my changes:
Assistant
All 5 SQL test failures are pre-existing (not caused by my changes). The tests related to my changes (
test_redefine_sql_table_diff_schema,test_sql_table_schema_to_python_ref, etc.) all pass. Let me restore and also run the edges test:Assistant
All edges tests pass. Let me also run the AI dependency graph tool test:
Assistant
Let me also run a broader set of tests to make sure nothing is broken
Assistant
Let me also test the
unregister_definitionsbehavior to make sure cell deletion works correctly:Assistant
There's a cleanup issue in
unregister_definitions. Whendefinitions[name]becomes empty, the code incorrectly removes alltyped_definitionsfor that name even though cells with different qualified names still reference them. Let me fix this.Assistant
I need to fix
unregister_definitionsto handle the qualified definitions cleanup properly. Let me update it:Assistant
The edit seems to have mangled the file. Let me check the full content:
Assistant
The file is corrupted. Let me restore it from git and redo the edit properly:
Assistant
Let me write the entire fixed file properly:
Assistant
Let me verify the file is correct and run all tests
Assistant
All my tests pass. Now let me run the existing test suites:
Assistant
All non-pre-existing-failure tests pass (49 passed). Let me also verify the edges tests and AI tool tests still work:
Assistant
All tests pass. Let me now review the final state of the file:
Assistant
The file looks clean. Let me do a final comprehensive check by running the broader test suite:
Assistant
All 9 failures are pre-existing (same ones failing before my change). The tests directly related to my fix all pass. Let me check the final
Assistant
The changes are clean and all relevant tests pass. Let me verify the checklist:
Checklist
-
Reproduced the error: ✅ Yes, I confirmed the bug: two SQL tables with the same leaf name (
xx) but different schemas (finance.datalake.xxvsfinance.information_layer.xx) were incorrectly flagged as multiply defined byget_multiply_defined(). -
Fix resolves the error: ✅ Yes, after the fix,
get_multiply_defined()returns an empty list for tables in different schemas, while still correctly flagging genuine duplicates. -
Edge cases handled: ✅ All edge cases are properly handled:
- Same qualified name (e.g.,
schema1.xxtwice) → still multiply defined - Python variable vs unqualified SQL table → still multiply defined
- Python variable vs qualified SQL table → NOT multiply defined
- Two Python variables with same name → still multiply defined
- Cell deletion cleanup → correctly removes entries from all data structures without incorrectly removing typed definitions that other cells still depend on
- Same qualified name (e.g.,
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.