Skip to content

[Bug]: snapshot hint regex misses qualified/derived tables and joins #31404

Description

@Ariznawlll

DRAFT 25 (SDK, 2026-10-27) — filing now

Title: [Bug]: compile_select snapshot hint silently vanishes for qualified tables and derived tables; JOINs only get it on the first table

Description

The SQLAlchemy snapshot integration (sqlalchemy_ext/snapshot.py) implements point-in-time queries by regex-injecting {snapshot = 'name'} into compiled SQL:

pattern = r'(\bFROM\s+)(\w+)(\s|,|$)'
return re.sub(pattern, replace_func, sql, count=1)

Verified outputs (pure-python repro of the exact function):

Compiled SQL Result Consequence
FROM users WHERE... FROM users {snapshot='snap'} WHERE... ✓ works
FROM mydb.users WHERE... unchanged — no hint qualified table silently queries LIVE data
FROM (SELECT ...) sub unchanged — no hint derived-table queries silently query live data
FROM a JOIN b ON... FROM a {snapshot} JOIN b ON... only a is point-in-time; b is live — mixed-era join

The failure is silent in every case: the caller asked for a snapshot read, gets current data, no error, no warning. The qualified-table case (mydb.users) is routine for any multi-schema or explicitly-schema-qualified application, and the SDK's own docs for other managers recommend db.table forms.

The JOIN case is a data-correctness hazard: joining a historical a against live b produces rows that existed at no single point in time.

Also adjacent in the same file (recording for the same fix): with_snapshot() monkey-patches Select globally on import, and the snapshot name is interpolated into the hint with no escaping ({snapshot = '{name}'}) — one more member of the escaping family.

Text-surgery on compiled SQL is fundamentally the wrong layer (see also today's #31381 count() and the qualifier-stripping regex in the ORM). The correct implementations exist: MatrixOne's own grammar supports per-table snapshot suffixes (FROM tbl{snapshot=...} / qualified db.tbl{...}) which is precisely why hand-INSERTING after the first FROM \w+ misfires.

Reproduction

from matrixone.sqlalchemy_ext import select, compile_select
from sqlalchemy import MetaData, Table, Column, Integer

users = Table("users", MetaData(), Column("id", Integer))
stmt = select(users.c.id, snapshot="nightly")   # compiles to SELECT users.id FROM users
sql_plain = compile_select(stmt)                # hint OK

users_q = Table("users", MetaData(), Column("id", Integer), schema="mydb")
stmt_q = select(users_q.c.id, snapshot="nightly")
sql_q = compile_select(stmt_q)                  # "SELECT ... FROM mydb.users" — NO hint
assert "{snapshot" in sql_q   # fails

Impact

  • Snapshot reads via the documented SQLAlchemy path silently read live data for any schema-qualified table and any subquery-in-FROM shape; the primary use of snapshots (auditable historical reads, backups verification) is undermined by returning current data with no signal.
  • JOIN queries get mixed-era result sets — worse than either extreme and invisible to the user.
  • The empty-hint cases also mean the with_snapshot() chain is a silent no-op in those shapes, contradicting the "100% compatible drop-in" message of the module docs.

Expected behavior

  1. Use SQLAlchemy's dialect/compile hooks (e.g., a custom Select compilation that appends the snapshot suffix per table token) instead of regex on the compiled string — or minimally extend the regex to handle db.table and derived tables.
  2. For JOINs, apply the snapshot hint to every joined table of the statement (MatrixOne grammar allows per-table hints) or refuse multi-table snapshots with a clear error.
  3. Fail loudly when the hint cannot be applied: any statement whose compiled SQL contains a FROM but receives no injection should raise, not silently skip — a snapshot query that silently degrades is the exact opposite of the feature's purpose.
  4. Escape the snapshot name (family fix) and attach the monkey-patch behind a documented flag rather than a global import side effect.

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. added a commit that references this issue on Oct 30, 2026
    bbfe556
  5. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31774 (main bbfe55669a1440424914d45a283a1d0e36f722fa): please verify the reported public SDK contract and its failure/recovery controls for snapshot hint regex misses qualified/derived tables and joins. This isolated QA run inspected the issue and merged PR but did not run tests or a live MatrixOne service at this SHA because the existing resource guard left insufficient headroom. The PR's author-reported validation is separate evidence, not a result of this QA run.

  6. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试不通过。快照查询的核心数据正确性场景通过,但 issue 同时要求全局 monkey-patch 改为显式启用,该项仍未满足;暂不关闭,转回研发确认和处理。

    关键证据

    • 官方 main@d39d8a40343ccd40bf665036388df7a40ee867f8;修复 PR fix(python): unify SDK query and analytics lifecycle contracts #31774 的合并提交 bbfe55669a1440424914d45a283a1d0e36f722fa 在祖先链。运行二进制及 SDK 源码对应同一完整 SHA;隔离单机 1 CN / 1 TN / 1 LogService,Python 3.12。
    • 独立测试库中创建两表和数据库快照,快照值为 10、20,再将实时值更新为 100、200。限定库名的查询返回历史值 10;JOIN 两侧返回历史值 10、20,未混入实时值;无快照对照返回 100、200。派生表入口明确抛出 snapshot 相关 ValueError,而非静默读取实时数据。以上实际执行 3/3;非法快照名拒绝,测试快照和数据库每轮清理。
    • issue 的 Expected behavior 第 4 项还要求全局 monkey-patch 由文档说明的开关控制。三个全新 Python 进程中,在导入 SDK snapshot 模块之前 hasattr(Select, 'with_snapshot') 为 False,导入之后立即变为 True,3/3;当前 clients/python/matrixone/sqlalchemy_ext/snapshot.py 仍在模块导入时直接执行 Select.with_snapshot = _with_snapshot,并非显式启用。请确认是否调整该合同或补充修复。
    • 快照数据正确性回归已补充至 MOTR #295,定向 3/3;该回归记录派生表明确拒绝,不将其描述为已支持。Add build constraint to assembly source files #295 尚未合入,默认跳过的 SDK 用例不算通过。

    待解决点

    • 全局导入副作用是否需按 issue 要求消除仍待研发处理;本次未跑完整 MOTR suite,也未覆盖多 CN 或网络故障。现有局部通过不代表 issue 全部验收完成。
  7. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    回归位置更正:#295 已关闭并汇总至 MOTR #298;本 issue 的定向用例已补入 #298,之前所述测试结果及未覆盖项不变。#298 尚未合入,完整 MOTR suite 未运行。

  8. added a commit that references this issue on Oct 30, 2026
    91dfab2
  9. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    新修复转 QA:#31889 已合入 main,合并提交 91dfab2132c4c4fcd3f9a115dbfa968b196b8b14。

    模块导入直接 Select.with_snapshot 赋值已删除,改为导出的with_snapshot与显式enable_snapshot_method,重复启用幂等且不覆盖其他owner;对应旧版本当前未完成的导入副作用要求。

    已核对 issue 正文、完整评论及当前合并源码/测试内容。历史失败按其原 SHA 保留;新修复直接覆盖上述残留,未发现适用于新修复的已验证反例或尚未完成的开发 follow-up。本轮仅源码/测试内容静态核对,没有运行 MatrixOne 服务或 SDK 回归;作者在 PR 内的运行结果是作者证据,不是本轮独立测试。转 Ariznawlll 继续验证,不代表验收通过。

    QA 待验范围:全新进程导入无全局变更、显式启用和冲突拒绝;限定表/两侧JOIN历史值、派生表明确拒绝,非法快照名和无快照对照。

  10. Ariznawlll commented on Oct 31, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。派生表快照查询目前明确拒绝,并非支持该查询形态。

    关键证据

    • 2026-10-31 隔离本地双 CN、TN、LogService;服务二进制及 Python SDK 源码均为官方 main@4f894474ad10e1b9dc338868145a118a71c1fa23,修复 fix(python-sdk): unify SQL, transaction and driver boundaries #31889 已合入其祖先链;Python 3.12、SQLAlchemy 1.4.54。
    • 每轮建立独立数据库和快照,将两表历史值 10、20 更新为实时值 100、200:限定表返回 10,JOIN 返回 10、20,无快照对照返回 100、200;派生表抛出明确的 snapshot 错误,非法快照名被拒绝;执行 3/3,测试库与快照每轮清理。
    • 全新进程导入不再修改 Select;显式启用有效且重复调用幂等,已有其他 owner 时拒绝覆盖,均 3/3。回归及其具体断言见 MOTR #298 中 script/14_issue_regression/issue_31404_sdk_snapshot_select.py(提交 334fd1db);该 PR 尚未合入,完整 suite 未运行。

    待解决点

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions