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 and understanding the issue. The problem is about SQL cells in marimo where two tables with the same name but in different schemas are incorrectly flagged as redefining variables.
Let me first look at the relevant file mentioned in the interface section:
marimo/_runtime/dataflow/definitions.py - Assistant
Let me explore the repository one step at a time.
- Assistant
Now let me look at how SQL variables are parsed and registered:
- Assistant
Now let me look at how SQL variable definitions are processed in the visitor:
- Assistant
Let me look at the visitor.py more closely to understand how SQL definitions are registered.
- Assistant
Let me search for the relevant part in the visitor that handles SQL definitions:
- Assistant
Let me see the full context of how SQL definitions are registered in the visitor:
- Assistant
Let me see how
_defineworks in the visitor and understand the flow better: - Assistant
Let me look at the
_defineand_define_in_blockmethods, and also howregister_definitionis called: - Assistant
Let me see where
register_definitionis called: - Assistant
Let me see the graph.py to understand how
register_definitionis called: - Assistant
Now let me look at where
get_multiply_definedis called to understand how the returned values are used: - Assistant
Let me look at how it's used in graph.py and app.py:
- Assistant
Let me see how it's used in app.py to understand how the return value is consumed:
- Assistant
Now let me look at the tests to understand the expected behavior:
- Assistant
Let me look at the tests for definitions and the graph:
- Assistant
Let me look for tests related to the definitions registry and SQL:
- Assistant
Let me look at the test_dataflow.py for relevant tests:
- Assistant
Let me look at the test
test_redefine_sql_table_diff_schema- this is directly related to the issue: - Assistant
Let me look at how the current
register_definitionhandles the case: - Assistant
Let me look at the full
register_definitionlogic and howget_multiply_definedworks: - Assistant
Let me look at how
get_multiply_definedis used across the codebase: - Assistant
Let me look at how the error is reported to understand what format is needed:
- Assistant
Let me look at the AI tools and other callers to understand what format they expect:
- Assistant
Let me look at the tests for the multiply-defined SQL scenario more closely:
- Assistant
Let me look at the graph's
get_multiply_definedand how it's used: - Assistant
Now let me understand how the SQL names flow through the system. Let me trace through the logic:
- Assistant
Now let me understand the full flow. Let me look at how the register_definition handles the case:
- Assistant
Now I have a good understanding of the issue. Let me first create a reproduction script to confirm the bug:
- Assistant
Let me install the required dependencies first.
- Assistant
Now let me run the reproduction script:
- Assistant
Let me check how the tests do it.
- Assistant
Let me run the test to see the existing behavior.
- Assistant
The
dfmultiply defined is from the Python assignment. Let me fix the reproduction: - Assistant
Interesting - it doesn't show the bug. Let me look more closely at how the register_definition works. Let me check what happens with the variable names:
- Assistant
The SQL parsing requires
mo.sql()orduckdb.sql()calls. Let me check how defs are populated: - Assistant
Let me look at the existing test case that tests for diff schema:
- Assistant
Hmm, the SQL cell's defs only show
df, nott1. The table definition is being tracked differently. Let me check how the SQL definitions are actually handled: - Assistant
The SQL cells are different from
mo.sql()calls. Let me look at how SQL cells are handled: - Assistant
Let me check the
compile_cellfunction to understand how SQL cells are handled: - Assistant
Let me look for how SQL cells are compiled differently:
- Assistant
Let me check how SQL cells are handled (they're just raw SQL, not wrapped in mo.sql()):
- Assistant
Let me look at how the visitor handles SQL code:
- Assistant
Now I understand the issue. The problem is in the
register_definitionmethod. When both cells define a table named "xx" (leaf name), even though they have different qualified names, they both get added toself.definitions["xx"]. Let me reproduce the bug: - Assistant
I can reproduce the bug. The issue is that when two SQL cells define tables with the same leaf name but different schemas, they get registered with the same name "xx" in
self.definitions, causingget_multiply_defined()to falsely report them as multiply defined.Let me trace through the logic:
- Cell 1: registers
name="xx",typed_def=("xx", "table")→ goes to else branch, adds to definitions - Cell 2:
name="xx"IS in definitions ANDtyped_def=("xx", "table")IS in typed_definitions → else branch, adds to definitions get_multiply_defined()returns["xx"]incorrectly
Now let me look at the existing tests more closely and then implement the fix:
- Cell 1: registers
- Assistant
Now I have a clear picture of the bug. Let me trace through the exact problem:
- Cell 1:
CREATE TABLE finance.datalake.xx→ registersname="xx"withqualified_name="finance.datalake.xx" - Cell 2:
CREATE TABLE finance.information_layer.xx→ registersname="xx"withqualified_name="finance.information_layer.xx"
In
register_definition, Cell 2 falls to theelsebranch (sincetyped_def=("xx","table")IS already intyped_definitions) and gets added toself.definitions["xx"], making it appear multiply-defined.The fix needs to:
- Track qualified names per cell in the registry
- Use conflict-aware grouping in
get_multiply_defined
Let me also look at the existing test that already handles the Python+SQL schema case:
- Cell 1:
- Assistant
Now let me implement the fix. I need to:
- Track qualified names per cell in the registry
- Make
get_multiply_definedconflict-aware - Update the graph wrapper and callers
- Assistant
Now let me implement the fix. I'll modify the
DefinitionRegistryto track qualified names and makeget_multiply_definedconflict-aware: - Assistant
Let me verify the updated file looks correct:
- Assistant
Now I need to update the graph wrapper and all callers. Let me update the graph wrapper:
- Assistant
I need to update the graph wrapper to handle the new return type from the registry. Let me update it to extract just the names for backward compatibility with all callers:
- Assistant
Let me verify the graph.py change:
- Assistant
Now let me test the fix with the reproduction script:
- Assistant
The fix works for the main case. Now let me also verify the existing test scenarios still pass:
- Assistant
All edge case tests pass. Now let me run the existing test suite to make sure nothing is broken:
- Assistant
Let me run without the timeout flag.
- Assistant
All tests pass. The SQL tests are skipped because duckdb is not installed. Let me install duckdb and run the SQL tests:
- Assistant
Let me run the SQL tests now:
- Assistant
The test_sql_chain failure looks unrelated to my change. Let me check if it was already failing before my changes:
- Assistant
Good - the test was already failing before my changes. Let me restore my changes and verify all the tests that were passing before still pass:
- Assistant
The failures are in
graph.parentsassertions - not related toget_multiply_defined. Let me verify these were already failing: - Assistant
Same 9 failures exist without my changes. Let me restore and run only the multiply-defined related tests:
- Assistant
Both the redefine tests pass. Let me also run the specific multiply-defined related tests:
- Assistant
Let me run all the non-SQL tests to make sure I haven't broken anything:
- Assistant
All 44 non-SQL tests pass. Let me also verify the SQL tests that were passing before still pass:
- Assistant
All relevant tests pass. Let me also run other test files that use
get_multiply_defined: - Assistant
All tests pass. Let me also run the AI tools dependency graph test:
- Assistant
All tests pass. Let me now run a comprehensive test with the full issue scenario using the actual marimo API:
- Assistant
The fix works perfectly. Let me review the complete changes:
- Assistant
The changes are clean and minimal. Let me run one final comprehensive test to make sure everything works:
- Assistant
All 10 edge case tests pass. Let me do a final run of the full test suite to confirm:
- Assistant
All 79 tests pass (1 skipped due to missing polars, 14 deselected are pre-existing failures unrelated to my change).
Let me now submit the solution. First, let me do a final review of the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.