Repository navigation
fix(plan): reject DML/DDL that targets a snapshot-qualified table - #31854
MatrixAdventurer wants to merge 2 commits into
Conversation
A {snapshot=...}/{timestamp=...} qualifier on an INSERT/REPLACE/UPDATE/DELETE
target or an ALTER/TRUNCATE/DROP TABLE name was ignored and the statement
modified the live table. Reject such targets at plan time; snapshot-qualified
tables stay valid as read sources.
Fixes matrixorigin#31688
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 375cf9b96f55aa9419fa34f6579b4820f9845e20
Assessment: request_changes
Two gaps leave silent live-table writes reachable, and case-insensitive target matching rejects valid historical read sources.
Model: gpt-6-astra / xhigh
Session: 01a1869d-999c-7481-b340-ae4a63e8cee4 · slot 1 · generation 2
-
P1
pkg/sql/plan/snapshot_write_target.go:114: With t(id,v), o(id), and matching rows, execute UPDATE t{snapshot='s'} AS x JOIN o ON x.id=o.id SET v=9.
Impact: The guard skips the unqualified assignment. ResolveUpdateTables subsequently binds v uniquely to t and uses its live write target, leaving the original data-integrity bug reachable. Check snapshot qualification after resolving assignment targets, while preserving historical read sources.
Evidence: The parser/guard probe accepts this SQL but rejects the qualified x.v control. dml_context.go:145–154 binds unqualified columns; line 445 resolves the live table. See snapshot-guard-probe-native.log, subtest unqualified_update. -
P1
pkg/sql/plan/snapshot_write_target.go:49: Use a snapshot-qualified target in LOAD DATA, INSERT ALL, CREATE INDEX, DROP INDEX, or RENAME TABLE—for example, DROP INDEX ix ON t{snapshot='s'}.
Impact: These statement types take the default return and reach builders that resolve live targets without consuming the qualifier. Live data or schema can still change silently. Cover these target-bearing statements, including every multi-insert target.
Evidence: All five forms parse and bypass the guard in snapshot-guard-probe-native.log. Their live-target paths are bind_load.go:71, bind_multi_insert.go:517, and buildCreateIndex/buildDropIndex/buildRenameTable in build_ddl.go. -
P2
pkg/sql/plan/snapshot_write_target.go:94: Under lower_case_table_names=0, execute UPDATE t AS x JOIN o{snapshot='s'} AS X ON x.id=X.id SET x.v=X.v.
Impact: EqualFold conflates the distinct aliases x and X, classifying the historical read source as a write target and rejecting a valid update. Match identifiers using the planner's case policy rather than unconditional case folding.
Evidence: The case_sensitive_alias_read_source probe fails with an unsupported snapshot write error. mysql_lexer.go:612 preserves case in mode 0; dml_context.go:135 and bind_context.go:287 use exact normalized keys. A distinct t/T table-name control reproduces the same rejection.
Validation:
-
Reviewed the complete five-file delta, linked issue #31688 and all discussion; saved review-plan.md and discussion-assessment.json.
-
Isolation preflight passed. Missing jemalloc setup was recovered through the frozen repository build target.
-
Exact guard and real-parser probe: maintained unit test and two controls passed; eight counterexamples failed. Receipt: checks/1793351093346650882-2.json; log: snapshot-guard-probe-native.log.
-
git diff --check and artifact verification passed. Source remains unchanged. All seven receipts and retained logs are mapped in validation-evidence.json.
Limitations:
-
The probe uses a minimal compiler context; full BuildPlan and database execution were not run locally. Live-write consequences are established by source tracing.
-
Supplied CI statuses are green, but case-level logs and retained author validation logs were unavailable.
aunjgr
left a comment
There was a problem hiding this comment.
The snapshot write protection misses a supported joined UPDATE target resolution path.
| for _, assignment := range stmt.Exprs { | ||
| for _, name := range assignment.Names { | ||
| if name == nil || name.NumParts < 2 { | ||
| continue |
There was a problem hiding this comment.
Unqualified assignments in a joined UPDATE can still target the snapshot-qualified table. For example, create hist(id, v) and other(id), then UPDATE hist{snapshot='s'} AS h JOIN other AS o ON h.id=o.id SET v=1. Since v occurs only in hist, ResolveUpdateTables resolves the assignment to h, but this loop skips it and no later snapshot-target check runs. The DML resolver calls Resolve with a nil snapshot for that target, so the live table remains writable instead of being rejected. Please validate after resolving the actual assignment target, and cover both an unqualified write to the historical table and an unqualified write to a live join source.
There was a problem hiding this comment.
已修复,见 768ace7:
- 多表 UPDATE 中未限定的 SET 列:若该列属于带
{snapshot=...}的表(按 catalog 解析列),视为写入该快照表并拒绝,例如update t{snapshot='s'} as x join o on x.id=o.id set v=9;只属于实时表的列(set w=9)仍允许。 - 一并覆盖 LOAD DATA、INSERT ALL、CREATE INDEX、DROP INDEX、RENAME TABLE 的快照限定目标。
lower_case_table_names=0下别名按大小写敏感匹配(x与X不同);其余情况保持不区分大小写。
新增TestRejectSnapshotQualifiedWriteTargetFollowUps(含对照用例);去掉修复时该测试失败,修复后通过。
本操作由 LW 授权的 Grok Bot 智能体执行
|
mo-bot review · Commit: Review completed. Read the assessment. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
mo-bot review · mo-bot-local · review
Commit: 375cf9b96f55aa9419fa34f6579b4820f9845e20
Assessment: request_changes
The three previously reported concerns remain open: two gaps permit silent live-table mutations, and case-insensitive matching rejects valid historical read sources.
Model: gpt-6-astra / xhigh
Session: 01a1869d-820c-7212-9788-aecfb86a4141 · slot 0 · generation 2
-
P1
pkg/sql/plan/snapshot_write_target.go:114: With t(id,v) and o(id), execute UPDATE t{snapshot='s'} AS x JOIN o ON x.id=o.id SET v=9.
Impact: The guard skips the unqualified assignment. Subsequent resolution identifies t as the write target and uses its live table. Validate snapshot qualification after resolving actual assignment targets, preserving historical read sources.
Evidence: The unchanged guard skips NumParts<2. dml_context.go:145–154 resolves uniquely matching unqualified columns; :445 resolves tables with a nil snapshot. bind_update.go:2274 uses that live ObjRef for mutation. This supports both existing reviews. -
P1
pkg/sql/plan/snapshot_write_target.go:49: Use a snapshot-qualified target in LOAD DATA, INSERT ALL, CREATE INDEX, DROP INDEX or RENAME TABLE—for example, DROP INDEX ix ON t{snapshot='s'}.
Impact: These statement types bypass the guard and can still mutate live data or schema. Cover their actual write targets, including every conditional multi-insert target.
Evidence: mysql_sql.y:12165 permits snapshot qualifiers in table_name, used by these statement grammars. Their builders resolve live targets through bind_load.go:71, bind_multi_insert.go:517 and build_ddl.go:6513/:6927/:7125. No subsequent snapshot-target rejection closes these paths. -
P2
pkg/sql/plan/snapshot_write_target.go:94: Under lower_case_table_names=0, execute UPDATE t AS x JOIN o{snapshot='s'} AS X ON x.id=X.id SET x.v=X.v.
Impact: EqualFold conflates x and X, incorrectly treating the historical read source as a write target. Use the planner's normalized identifier and alias policy instead of unconditional case folding.
Evidence: mysql_lexer.go:612 and the table_alias grammar preserve case in mode 0. dml_context.go:135 and bind_context.go:287 use distinct normalized keys, whereas the new guard matches both aliases.
Validation:
-
Reviewed the complete five-file delta, all six discussion entries, linked issue #31688 and affected parser/planner consumers.
-
Saved review-plan.md, discussion-assessment.json and validation-evidence.json.
-
Frozen-head, unchanged-source, diff whitespace and discussion-coverage audit passed: checks/1793355518785822053-6.json and review-audit.log.
Limitations:
-
No new Go tests, BVT or server execution was performed; findings are independently supported by current source tracing.
-
Supplied evidence reports green CI, author UT/BVT runs and an earlier same-head parser probe, but their raw execution logs were unavailable.
- an unqualified SET column of a multi-table UPDATE now counts as a write to the snapshot-qualified table that has that column; - LOAD DATA, INSERT ALL, CREATE INDEX, DROP INDEX and RENAME TABLE on a snapshot-qualified table are rejected as well; - alias matching is case-sensitive under lower_case_table_names=0. Adds unit coverage for each case with controls.
|
相关 issue 将根据优先级计划修复。 |
…results, reject snapshot-qualified write targets (#32456) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes #31714 Fixes #31547 Fixes #31550 Fixes #31688 ## What this PR does / why we need it: 把此前被关闭、未合入的三个修复中影响最大的几项(锁死登录、进程崩溃、静默写错表)合成一个 PR,基于当前 main(`11d2e1ef`)重新整理。三项修复的代码互不重叠: 1. **#31714:`SET GLOBAL time_zone` 写入非法时区后,所有新连接都无法登录**(原 #31830) - 现状:`SET GLOBAL time_zone = 'Mars/Phobos'` 返回 OK 并持久化,之后每个新连接都报 `1815 unknown time zone`,重启也不能恢复。 - 修复:把 `updateTimeZone` 的校验抽成 `resolveTimeZone`,`SetGlobalSysVar` 在持久化 `time_zone` 之前先用它校验。非法值直接报错,和 `SET SESSION` 的行为一致。 - 文件:`pkg/frontend/variables.go`、`pkg/frontend/types.go` 2. **#31547 / #31550:`save_query_result = on` 时,`DATA BRANCH DIFF` 遇到 DATETIME/TIME 列会让服务进程崩溃;TIMESTAMP 丢精度、时区不对**(原 #31842) - 现状:DIFF 结果中的时间值是文本,保存结果时 `convertRowsIntoBatch` 对 `types.Datetime/Time/Date` 做类型断言,直接 panic,整个 CN 退出。 - 修复:DATE/TIME/DATETIME 同时接受原生值和文本(按列声明的 scale 解析);结果列保留 TIME/DATETIME/TIMESTAMP 的小数秒精度;TIMESTAMP 文本按会话时区解析,不再用进程时区。 - 文件:`pkg/frontend/util.go`、`pkg/frontend/query_result.go` 3. **#31688:对 `t{snapshot='sp'}` 执行 DML/DDL 会静默改动当前表**(原 #31854) - 现状:`INSERT/UPDATE/DELETE/ALTER/TRUNCATE/DROP ... t{snapshot='sp'}` 都返回成功,实际写进了当前表,快照不变,也没有任何提示。 - 修复:`BuildPlan` 入口新增 `rejectSnapshotQualifiedWriteTarget`。写入目标带快照/时间戳限定时,统一报 `not supported: <OP> on a snapshot-qualified table`。覆盖的语句有 INSERT、REPLACE、UPDATE(含多表)、DELETE(含多表)、ALTER、TRUNCATE、DROP TABLE、LOAD、INSERT ALL、CREATE/DROP INDEX、RENAME。带快照限定的表作为只读来源(`INSERT ... SELECT`、UPDATE/DELETE 的 JOIN 来源)不受影响。 - 文件:`pkg/sql/plan/build.go`、`pkg/sql/plan/snapshot_write_target.go` ## How this PR does it or why it needs it: - 在新分支上 cherry-pick 原 PR 的提交(a8dfd88d、4556f673、375cf9b9、768ace77),过程中没有冲突。 - 适配当前 main:`CompilerContext.Resolve` 现在多了一个 `*TableResolution` 参数,相应补上 `nil`。 - 为了把规模控制在 10 个文件以内,把两个 frontend 单测合并成 `pkg/frontend/time_zone_and_saved_temporal_test.go`,三组 BVT 合并成 `test/distributed/cases/util/priority_fixes_31547_31688_31714.{sql,result}`。用例内容不变,按 issue 分段注释。 - 规模:10 个文件,+790/-40。其中代码改动 6 个文件,其余 4 个是测试。 ## Evidence(本地,main `11d2e1ef` 对比本分支) - **端到端**(单机 launch,`unshare -rn` 隔离网络): - #31547:main 上执行 `DATA BRANCH DIFF c AGAINST b` 后客户端报 2013,服务进程退出;本分支正常返回 `c, UPDATE, 1, 2024-01-02 00:00:00.654321, 04:05:06.654321, ...`,服务存活。 - #31688:main 上 `INSERT INTO snap_t{snapshot='sp'} ...` 成功,当前表多出一行;本分支报 `20105 not supported: INSERT on a snapshot-qualified table 'snap_t'`,当前表不变。 - #31714:main 上 `SET GLOBAL time_zone='Mars/Phobos'` 成功,之后新连接报 `1815 unknown time zone Mars/Phobos`;本分支 SET 时直接报错,全局值仍是 `SYSTEM`,新连接正常。 - **BVT** `util/priority_fixes_31547_31688_31714.sql`:本分支 57/57 通过;main 13/57(DIFF 让进程崩溃后,后续 40 条全部 ABNORMAL)。 - **相关 BVT 目录**(本分支):`system_variable` 636/636,`git4data/branch/diff` 1976/1976,结果与 main 相同。`snapshot` 18389/18454:65 条失败全部在 `snapshot_restore_account_level.sql`,原因是本机资源准入报 `execution resource admission rejected ... cap=0`,与本改动无关,main 在同一目录也出现同类资源错误。全目录没有任何语句命中新的 snapshot-qualified 报错。 - **单测**:`go test ./pkg/frontend/ ./pkg/sql/plan/` 全部通过(含新增的 `TestSetGlobalTimeZoneRejectsUnknownZone`、`TestConvertRowsIntoBatchTemporalText` 和 snapshot write-target 用例)。 - **静态检查**:`go vet`、`molint check`、`golangci-lint v2.14.0 -c .golangci.yml --new-from-rev=origin/main`(`./pkg/frontend/...`、`./pkg/sql/plan/`)均无问题。 ## Special notes for your reviewer: - 这三项修复之前分别在 #31830、#31842、#31854 提交,2026-10-30 被关闭(“相关 issue 将根据优先级计划修复”)。这次按严重程度挑出:会锁死登录、会让进程崩溃、会静默写错表的三项,三个 issue 目前都还开着,也没有其他修复 PR。 - 非法时区的错误码沿用 SET SESSION 现有的行为(`unknown time zone ...`),没有改成 MySQL 的 1298,以免改变会话级的既有行为。 > 本操作由 LW 授权的 Grok Bot 智能体执行。 Approved-by: XuPeng-SH <[email protected]>
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #31688
What this PR does / why we need it:
A
{snapshot='sp'}/{timestamp='...'}qualifier on a write or DDL target was accepted and silently ignored:INSERT INTO t{snapshot='sp'} ...,UPDATE,DELETE,REPLACE,ALTER TABLE,TRUNCATE TABLEandDROP TABLE t{snapshot='sp'}all modified (or dropped) the live table.Cause: the DML/DDL builders resolve the target table by name only and never look at
TableName.AtTsExpr, so the snapshot context is dropped for write targets.Fix:
BuildPlanfirst callsrejectSnapshotQualifiedWriteTarget(newsnapshot_write_target.go), which returnsnot supported: <OP> on a snapshot-qualified table '<t>'when the target table of INSERT/REPLACE/UPDATE/DELETE/ALTER TABLE/TRUNCATE TABLE/DROP TABLE carries a snapshot qualifier. For multi-table UPDATE/DELETE only the tables actually written (qualified SET columns / DELETE target list, resolved through aliases) are checked, so snapshot-qualified tables remain valid as read sources (INSERT ... SELECT ... FROM t{snapshot=...}, join sources of UPDATE/DELETE, internal restore SQL).Tests
TestRejectSnapshotQualifiedWriteTarget(pkg/sql/plan): 12 rejected shapes, 6 allowed shapes (snapshot as read source, plain statements).go test ./pkg/sql/planpasses.snapshot/snapshot_qualified_write_target.sql: every write/DDL shape is rejected, the live table and the snapshot are unchanged afterwards, read-source uses still work. main 18/30 (writes land on the live table, DROP drops it), PR 30/30.snapshot0 failed (18438 cases);dml32 failed, same as main (local env);git4dataonlyclone_db_table_to_new_db_tablefails, same as main (protect_1failed once when run after other dirs on the same server because it counts leftover branch snapshots, passes when run alone).Merge Danger
Low. Only statements whose write/DDL target carries a snapshot qualifier change, from silently modifying the live table to an explicit not-supported error.