feat: --limit 全表扫描支持按真实 ID 排序/升降序/范围过滤

此前 --limit 仅按总排号排序取前 N 个。现扩展为:
- 排序按数据库真实 ID 列(config.id_field),新增 --order {asc,desc}(默认 asc)
- 新增 --range START,END 按真实 ID 闭区间过滤(与 --order/--limit 可组合)
- 上述三参数仅全表扫描生效,与 --id/--sn/--ids-file 互斥

db.py: fetch_all_ids 新增 order/id_min/id_max 参数,用派生表
  (内层 DISTINCT 取 (总排号,ID) 配对,外层按 ID 排序)规避
  SQL Server "SELECT DISTINCT 时 ORDER BY 列须在选择列表" 的限制;
  id_field 未配置时降级按总排号排序并告警。
write_attachments.py: 新增 --order/--range CLI 参数并接线,含 --range
  格式校验(须为 START,END 两个整数);模块 docstring 补示例。
README.md: 写入用法块与项目结构注释补充 --order/--range 说明。

自测(小数据量,只读+部分 dry-run):--limit 3 升/降序、--range 800,805
升序、--range 800,805 --order desc --limit 3 均验证返回总排号按真实 ID
正确排序且在范围内;dry-run 不落库;--range 格式错误正确报错退出。

Co-Authored-By: WorkBuddy <workbuddy@tencent.com>
This commit is contained in:
Misaka_Company
2026-07-24 16:27:16 +08:00
parent fd047e2143
commit 70b6fb3767
3 changed files with 92 additions and 18 deletions

View File

@@ -235,12 +235,21 @@ JSON 的括号、引号、转义更容易被模型写错。约定 LLM 只输出"
`Common.Attachment` 表,供下游系统按总排号查询该订单的附件类目。
```bash
# 全量写入(按总排号遍历源表全部记录)
# 全量写入(按真实 ID 升序遍历源表全部记录)
python write_attachments.py
# 仅前 N 个总排号(测试用,避免一次性消耗大量 LLM 额度)
# 仅前 N 个(按真实 ID 升序,测试用,避免一次性消耗大量 LLM 额度)
python write_attachments.py --limit 10
# 降序取前 N 个
python write_attachments.py --limit 10 --order desc
# 按真实 ID 闭区间过滤:仅处理 ID 在 [800,805] 的记录
python write_attachments.py --range 800,805
# 组合:区间内降序、再取前 3 个
python write_attachments.py --range 800,805 --order desc --limit 3
# 指定总排号文件(每行一个,键类型 sn
python write_attachments.py --ids-file ids.txt
@@ -252,7 +261,7 @@ python write_attachments.py --sn 26B742,26B743,26B744
python write_attachments.py --id 802
python write_attachments.py --id 802,803
# --id / --sn / --ids-file 可混用;提供任一后不再全表扫描(--limit 此时无效)
# --id / --sn / --ids-file 可混用;提供任一后不再全表扫描(--limit/--order/--range 此时无效)
# 粗分类写入(小类统一填占位值);不指定 --mode 则用配置文件 business.default_mode
python write_attachments.py --mode coarse
@@ -288,7 +297,7 @@ python write_attachments.py --mode fine --enable-other
```
├── config.yaml # 配置文件
├── main.py # 命令行入口:分类并输出 JSON Lines支持 --summary 在 stderr 打印批汇总;直接 import 同目录各模块)
├── write_attachments.py # 写入入口:分类结果落库到 Common.Attachment量/--limit/--id/--sn/--ids-file/--mode/--dry-run/--enable-other/--config运行后打印 token/缓存汇总)
├── write_attachments.py # 写入入口:分类结果落库到 Common.Attachment表扫描支持 --limit/--order/--range 按真实 ID 排序与范围过滤;--id/--sn/--ids-file/--mode/--dry-run/--enable-other/--config运行后打印 token/缓存汇总)
├── requirements.txt
├── config_loader.py # YAML 配置读取与校验
├── db.py # SQL Server 查询与落库fetch_params_by_ids 支持按 id/sn 双键查询、fetch_all_ids / upsert_attachmentspyodbc