MagicStack / MagicStack/asyncpg
Support for `WHERE` clause when usng `copy_to_table` method
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 8.1k
- Fork
- 468
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Hi there!
Since version 12, PostgreSQL has supported a `WHERE` clause within `COPY ... FROM` statements:
```
COPY table_name [ ( column_name [, ...] ) ]
FROM { 'filename' | PROGRAM 'command' | STDIN }
[ [ WITH ] ( option [, ...] ) ]
[ WHERE condition ]
```
It functions similarly to the `WHERE` clause used within `SELECT` statements, subject to some restrictions regarding subqueries.
Recently I've found myself in a situation where I am performing a bulk insert of CSV-formatted data from a remote machine, but would like to skip certain rows - such as those containing null values. From what I can tell, the existing `Connection.copy_to_table` helper doesn't support this, and I'd need to drop down to private API (the underlying `Connection._protocol._copy_in` machinery)
I'd appreciate some publicly exposed way of doing this - the simplest approach that comes to mind would be adding a `where` kwarg to `Connection.copy_to_table` as well as the `Connection._format_copy_opts` method. Usage could then look something along the lines of
```py
await conn.copy_to_table("cool_table", source="important_file.csv", where="important_value = 42")
```
I'm not sure if this is satisfactory, though. There would also need to be some consideration regarding earlier Postgres versions that don't support `COPY ... FROM` using a `WHERE` clause.
Please let me know your thoughts - I'm happy to open a PR for implementation if necessary.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia esaminando Connection.copy_to_table e Connection._format_copy_opts, quindi traccia il modo in cui arrivano alla logica privata Connection._protocol._copy_in. Verifica come viene gestita la compatibilità con le versioni di PostgreSQL e individua i test pertinenti prima di decidere come dovrebbe comportarsi l'opzione pubblica. Il lavoro è completato quando esiste un modo supportato per applicare una condizione COPY ... FROM WHERE, con un comportamento appropriato per le versioni precedenti di PostgreSQL.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- postgresql, python
- Ambito
- databases
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100