Skip to content

fix: push bounded dataset-global queries into SQLite (#218) - #228

Merged
codeasier merged 8 commits into
mainfrom
fix/218-sql-pushdown
Oct 10, 2026
Merged

codeasier merged 8 commits into
mainfrom
fix/218-sql-pushdown

Conversation

@codeasier

Copy link
Copy Markdown
Owner

依赖

依赖 #225(包含 #224),暂以 main 为目标触发完整 CI。建议依次合并依赖后复核本项差异。

变更

  • SQL 计算各指标独立峰值,保持并列时最早事件语义。
  • event/allocation 使用分片候选页再全局分页,保留过滤前 actual_range 与 exact_total。
  • 按二进制完整堆栈文本聚合,合并计数/总和后算均值;显式 BINARY 防止数据库列 NOCASE 合并不同堆栈或改变页边界。
  • 使用只读 ATTACH/CTE 在过滤、分页前完成生命周期证明、冲突检查和最新观测归并。
  • 共享截止时间,实际取回及输出行继续计入预算。

验证

  • 两个各 500k 事件的分片,六类有界查询在默认预算下通过;百万事件总数保留,取回/输出预算为 3–22 行。这不等于 SQLite 只处理这些行。
  • 主实现全套 1959 项测试通过;最终跟进 67 项相关测试、独立 21 项 SQL 测试通过。最终完整 CI 待本 PR 运行。
  • 整组集成版本 200 seeds × 20 查询形式的 4000 次差分全通过;安装 wheel 的 500002 事件真实 importer→默认分片 exporter→CLI/API 端到端验收通过,详见后续 [测试] 分片夹具布局不真实,缺少属性测试和超预算规模测试 #223 PR。
  • 最终 Ruff/Black/type 检查通过;远端 tree 与最终本地源码一致。

保留限制

无限制输出、大 offset、高不同堆栈基数仍可能触发预算。ordered-frame 统计保留受预算约束的回源路径。超过 SQLite ATTACH 上限(通常 10 个成员)的生命周期查询仍回退原受预算约束算法,尚未解决任意分片数量的规模问题。因此不自动关闭 #218,也不承诺百万行无界输出。

未修改 snapshot 源码,无需追加 snapshot provenance。
Related to #218, #217

Copy link
Copy Markdown
Owner Author

验证更新:当前提交 98fac137d26f99872252de5a3877b04d25202f56 的完整 CI 已通过。PR 描述中的规模限制仍适用,特别是超过 SQLite ATTACH 上限时的生命周期回退;#218 保持开放,不将这项剩余范围宣称已解决。

@codeasier codeasier left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

交叉检视摘要(3 个独立模型审查,quorum 2/3 满足,父会话核验)

未发现阻塞性正确性缺陷。两位审查者对下推语义(peak 并列规则、NULL 排序、COLLATE BINARY、lifecycle 的证明/冲突/最新观测、只读 ATTACH)的结论与代码抽查一致;本摘要不代表逐行复核。

本次共 1 条 Medium、3 条 Low 行内评论。其中 Medium 项已在代码中核实,建议合并前修复。

result.extend(rows)
budget.remaining()
return result
except sqlite3.Error as exc:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Medium] 超出 int64 的整数参数会绕过域异常转换

_AttachedSources.read 仅捕获 sqlite3.Error。block / leak_detection 的 min_*、max_* 过滤值,以及 offset + limit 形成的 candidate_limit,会直接绑定到 SQLite;超出 int64 时 CPython sqlite3 抛出 OverflowError(不是 sqlite3.Error),会原样穿透本函数。已在本地用 sqlite3 复现该异常,端到端未复现。

  • CLI 的 _safe_call(src/pt_snap_cli/cli.py:1027)不捕获该异常,用户会看到未处理的 traceback;API/MCP 层未核实。
  • 对照:事件路径经 DatasetSourceResolver.read 与执行器的 except Exception 被转换为 QueryExecutionError,standalone 路径也是域错误。因此 dataset 与 standalone 的错误语义不一致。这里是报错而非静默近似结果。
  • 相关绑定点:line 156(leak_detection 的 min_size)、line 165(通用过滤)、line 228(candidate_limit)。query/config.py 的 QueryParameter.validate 对 int 没有范围约束,全仓库测试也没有 int64 边界用例。

建议:在 dataset 路径校验 int64 范围并返回 InvalidParameterError;或在此处将 OverflowError 转换为域错误。补充 min_size 与 offset 超过 2^63 的回归测试(event 与 block 各一条)。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 f645cfca662a1fee08fb45429440d7673b1083d1。

已修复:_AttachedSources.read 现在将 sqlite3 的 OverflowError 转换为 QueryExecutionError,并保留超时优先及游标/进度回调清理。新增 int64 两端合法值、上下溢出、offset + limit 溢出的回归;覆盖 block/leak 的 min_size、event 的整数过滤和 event/block offset。CLI/API 契约测试确认返回域异常及 QUERY_FAILED JSON,不再泄漏 traceback。

最终云端完整测试 2003/2003,通过相关 CLI/API 契约、lint/format/type 与独立复核。新提交 CI 另行核验。



def lifecycle_query(sources, items, template, params, scope, candidate_limit):
if len(sources.device.slices) > _attachment_limit():

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] 分片门控与 ATTACH 使用的是整个 device 的分片数

  • line 170:len(sources.device.slices) > _attachment_limit() 决定是否回退到 Python 实现。
  • line 45:__enter__ 对 self.sources.device.slices 全量 ATTACH,即使 items 只含一个成员(例如 --slice 作用域)。

正确性不受影响,但单成员查询会对全部分片做 ATTACH 与 _proof 的 EXISTS 检查;分片数超过 10 的设备即使只需 1–2 个分片,也总会回退。建议按本次查询实际引用的分片(observations 及其 proof 所需分片)做门控与 ATTACH。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 f645cfca662a1fee08fb45429440d7673b1083d1。

已按本次查询所需分片门控及 ATTACH:先用 SQL 发现选定观察中的 alloc/free 引用所属分片,再联合观察分片。证明来源仍可跨出 --slice 范围,且发现发生在过滤/分页前;超出实际附加上限仍回退。12 个设备分片只需 3 个分片的用例已验证实际 ATTACH 列表、跨片分配/释放证明及超过上限后的结果一致性。

最终云端完整测试 2003/2003,通过相关 CLI/API 契约、lint/format/type 与独立复核。新提交 CI 另行核验。

cte = _lifecycle_cte(sources, items)
with _AttachedSources(sources) as database:
summary = database.read(
cte + f"""SELECT COUNT(*) AS lifecycles,

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] summary / page 各自重复执行完整 CTE

同一查询中,summary 与 page 各执行一次完整的 UNION ALL 观测、proof 与 ROW_NUMBER CTE;freed_block_lifetime 则是 summary 与 buckets 各一次(见 line 207、225)。这些 SQLite 内部扫描不计入预算,单次查询的工作量约翻倍。

模块 docstring(line 3–5)写的是 “only aggregate/page rows cross the Python boundary”,只描述了跨 Python 边界的行数,容易被误读为完整成本模型。建议在 docstring 中注明 SQLite 内部扫描与临时空间不受预算约束;或考虑将 summary 与 page 合并为一次查询。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 f645cfca662a1fee08fb45429440d7673b1083d1。

已在模块 docstring 及中英查询文档明确:行数/序列化字节预算只计取回和输出,不限制 SQLite 内部扫描及临时空间;summary 与 page/buckets 会各自计算完整生命周期 CTE,所有语句继续共用同一 deadline。本次保留现有双查询实现,未引入临时合并数据库。

最终云端完整测试 2003/2003,通过相关 CLI/API 契约、lint/format/type 与独立复核。新提交 CI 另行核验。

@@ -0,0 +1,291 @@
"""Bounded global SQL reductions preserve full-scope evidence and lifecycle proof."""

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Low] 测试缺口

  1. 本 PR 新增的 pushdown 测试中,没有 slice 作用域(--slice / slice_index)下 block / leak_detection 下推与回退的一致性用例(diff 中检索不到任何 slice 作用域调用)。
  2. 全仓库没有 int64 边界参数的测试(参见 dataset_lifecycle_sql.py line 82 的评论)。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新至 f645cfca662a1fee08fb45429440d7673b1083d1。

已补充 slice_index=0/3/7 的 block、freed_block_lifetime 下推/强制回退差分,比较行、total、has_more 及完整来源覆盖。leak_detection 按现有契约要求 terminal dataset scope,--slice 在进入两种路径前均拒绝,因此补充一致拒绝测试而未引入新的片内泄漏语义。另补 int64 边界/溢出及 CLI/API 错误契约测试。

最终云端完整测试 2003/2003,通过相关 CLI/API 契约、lint/format/type 与独立复核。新提交 CI 另行核验。

Copy link
Copy Markdown
Owner Author

本轮检视修正的最新提交 f645cfca662a1fee08fb45429440d7673b1083d1 已通过完整 CI。验证覆盖新 head,不使用旧提交结果替代。PR 仍为草稿,未执行合并。

Copy link
Copy Markdown
Owner Author

已将 #226/#227 合入后的最新 main(9ff9c655)整合到本分支,保留 SQL 下推、溢出错误归一化、proof-owner 分片裁剪、source/cache 校验、可取消验证及 pinned report 语义。中文文档冲突同时保留 SQL/预算限制和验证/缓存边界。双父提交,无强推。

最终树 7797f60d2ac1acc305d803046eb038682c402b08 已通过全套2068项测试、298项专项、92.74%覆盖率及全部关键覆盖率门槛;lint/format/typecheck、构建、wheel audit/clean-wheel acceptance 均通过。最新 main 的源码树已与预组合时的已测 #227 树逐字节核对一致。新 head CI 待终态,之后再按授权合入。

@codeasier
codeasier marked this pull request as ready for review October 10, 2026 13:44
@codeasier
codeasier merged commit c11a3ab into main Oct 10, 2026
6 checks passed

Copy link
Copy Markdown
Owner Author

最终 head 23770f8 的完整 CI 已成功:https://github.com/codeasier/pt-snap-cli/actions/runs/38056323162 。已按授权 squash 合入 main c11a3ab;合入后 push CI 另行核验。#218 的剩余规模限制不因本次合入消失,继续保留开放。

Copy link
Copy Markdown
Owner Author

合入后的 main c11a3ab 已通过完整 push CI(全部6个jobs):https://github.com/codeasier/pt-snap-cli/actions/runs/38056916125 。远端源码树7797f60d2ac1acc305d803046eb038682c402b08与本地完整2068项测试的版本一致,本次冲突整合和合入验证完成。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant