MagicStack / MagicStack/asyncpg

Support for `WHERE` clause when usng `copy_to_table` method

未关闭
#939 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Python
星标
8.1k
派生
468
PR 合并指标
30 天内没有已合并 PR

描述

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.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先检查 Connection.copy_to_table 和 Connection._format_copy_opts,然后跟踪它们如何进入私有的 Connection._protocol._copy_in 机制。检查如何处理 PostgreSQL 版本兼容性,并在决定 public option 应如何行为之前,找出相关测试。完成的标准是:提供一种受支持的方式来应用 COPY ... FROM WHERE 条件,并对较早的 PostgreSQL 版本采取适当的行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
postgresql, python
领域
databases
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。