Repository navigation
Description
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 # failsImpact
- 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
- Use SQLAlchemy's dialect/compile hooks (e.g., a custom
Selectcompilation that appends the snapshot suffix per table token) instead of regex on the compiled string — or minimally extend the regex to handledb.tableand derived tables. - 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.
- 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.
- Escape the snapshot name (family fix) and attach the monkey-patch behind a documented flag rather than a global import side effect.
Activity
fix(python): unify SDK query and analytics lifecycle contracts (#31774)
XuPeng-SH commentedon Oct 30, 2026
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.Ariznawlll commentedon Oct 30, 2026
结论
测试不通过。快照查询的核心数据正确性场景通过,但 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。hasattr(Select, 'with_snapshot')为 False,导入之后立即变为 True,3/3;当前clients/python/matrixone/sqlalchemy_ext/snapshot.py仍在模块导入时直接执行Select.with_snapshot = _with_snapshot,并非显式启用。请确认是否调整该合同或补充修复。待解决点
Ariznawlll commentedon Oct 30, 2026
回归位置更正:#295 已关闭并汇总至 MOTR #298;本 issue 的定向用例已补入 #298,之前所述测试结果及未覆盖项不变。#298 尚未合入,完整 MOTR suite 未运行。
fix(python-sdk): unify SQL, transaction and driver boundaries (#31889)
XuPeng-SH commentedon Oct 30, 2026
新修复转 QA:#31889 已合入 main,合并提交
91dfab2132c4c4fcd3f9a115dbfa968b196b8b14。模块导入直接 Select.with_snapshot 赋值已删除,改为导出的with_snapshot与显式enable_snapshot_method,重复启用幂等且不覆盖其他owner;对应旧版本当前未完成的导入副作用要求。
已核对 issue 正文、完整评论及当前合并源码/测试内容。历史失败按其原 SHA 保留;新修复直接覆盖上述残留,未发现适用于新修复的已验证反例或尚未完成的开发 follow-up。本轮仅源码/测试内容静态核对,没有运行 MatrixOne 服务或 SDK 回归;作者在 PR 内的运行结果是作者证据,不是本轮独立测试。转 Ariznawlll 继续验证,不代表验收通过。
QA 待验范围:全新进程导入无全局变更、显式启用和冲突拒绝;限定表/两侧JOIN历史值、派生表明确拒绝,非法快照名和无快照对照。
Ariznawlll commentedon Oct 31, 2026
结论
测试通过。派生表快照查询目前明确拒绝,并非支持该查询形态。
关键证据
main@4f894474ad10e1b9dc338868145a118a71c1fa23,修复 fix(python-sdk): unify SQL, transaction and driver boundaries #31889 已合入其祖先链;Python 3.12、SQLAlchemy 1.4.54。Select;显式启用有效且重复调用幂等,已有其他 owner 时拒绝覆盖,均 3/3。回归及其具体断言见 MOTR #298 中script/14_issue_regression/issue_31404_sdk_snapshot_select.py(提交334fd1db);该 PR 尚未合入,完整 suite 未运行。待解决点