The DuckDB connector cannot attach files named `memory.duckdb`, `main.duckdb` or `temp.duckdb`

Aperta Adatta ai principianti
#2,875 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 4 giorni

@anasdahani220 ci sta già lavorando.

Dal 21/11/2026.

  • #2876 di @anasdahani220 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
77/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
database

Direzione di ricerca

Inizia da core/wren/src/wren/connector/duckdb.py, concentrandoti su _attach_database e sull’helper per gli alias che chiama. Leggi come vengono generati gli alias e come vengono gestiti gli errori di attach, poi riproduci il problema con file validi chiamati memory.duckdb, main.duckdb e temp.duckdb. Il lavoro è completato quando ogni file viene collegato con un alias sicuro e gli errori di attach seguono il percorso WrenError documentato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment

  • Canner/WrenAI at commit 6975f829de046d446ab16801cbec9a4d81dae88c
  • core/wren/src/wren/connector/duckdb.py, _attach_database (lines 138-141)
  • Python 3.12, duckdb 1.5.0

Summary

_attach_database derives the ATTACH alias from the file's base name and only checks it against aliases it has already handed out. It never checks DuckDB's built-in catalog names. A data directory containing memory.duckdb, main.duckdb, temp.duckdb or system.duckdb therefore makes DuckDBConnector.__init__ fail outright, and the raw duckdb.BinderException escapes instead of a WrenError.

Root cause

# core/wren/src/wren/connector/duckdb.py:138-141
alias = unique_alias.replace('"', '""')
self.connection.execute(
    f"ATTACH DATABASE '{escaped_file}' AS \"{alias}\" (READ_ONLY);"
)

unique_alias only avoids collisions inside used_aliases. It does not exclude memory, system, temp or main, and main already exists in every fresh DuckDB connection.

Affected code

core/wren/src/wren/connector/duckdb.py, _attach_database and the alias helper it calls.

Reproduction

With the pinned connector:

  1. Create a directory containing a valid DuckDB database named memory.duckdb (or main.duckdb, temp.duckdb).
  2. Construct DuckDBConnector(LocalFileConnectionInfo(url=directory, format="duckdb")).

Observed:

file result
ok.duckdb (control) attaches; SELECT * FROM ok.main.t returns [(1,)]
memory.duckdb BinderException: Failed to attach database: database with name "memory" already exists
main.duckdb BinderException: Attached database name "main" cannot be used because it is a reserved name
temp.duckdb BinderException: Attached database name "temp" cannot be used because it is a reserved name

The failing statements were executed exactly as generated:

ATTACH DATABASE '/tmp/.../memory.duckdb' AS "memory" (READ_ONLY);
ATTACH DATABASE '/tmp/.../main.duckdb' AS "main" (READ_ONLY);

No WrenError / DUCKDB_FILE_NOT_FOUND path is reached: only self._IOException and self._HTTPException are caught, so the BinderException propagates out of __init__.

Expected behavior

A valid DuckDB database file attaches under a generated alias regardless of its file name. If the name collides with a built-in catalog, the connector should pick a safe alias (for example wren_memory) rather than fail. If attaching still fails, the error should be a WrenError with the documented code, not a raw BinderException.

Actual behavior

A database file named memory, main, temp or system makes the data source impossible to open, even though the file and directory are valid. The facade raises duckdb's internal binder error, so callers see an unclassified exception instead of the connector's error surface.

Suggested fix

Reserve the built-in names in the alias generator, prefix all generated aliases (wren_<base>), or bump the numeric suffix when the base name matches memory, main, temp or system:

RESERVED = {"memory", "main", "temp", "system"}

def _safe_alias(base: str, used: set[str]) -> str:
    candidate = base
    if candidate.lower() in RESERVED:
        candidate = f"wren_{base}"
    alias = candidate
    n = 2
    while alias in used:
        alias = f"{candidate}_{n}"
        n += 1
    return alias

Also wrap duckdb.BinderException into WrenError(ErrorCode.ATTACH_DUCKDB_ERROR, ...) so a failure surfaces through the documented error path.

Lingua principale
Python
Stelle
17.8k
Fork
2k
Merge medio
5g 5h
PR unite (30g)
21

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Canner/WrenAI

Tutte le issue di Canner/WrenAI

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.