Skip to content

fix: make SQL/CSV dump scripts restore-safe (#30596 #30598 #30587 #30590 #30628 #30481) - #58

Merged
aptend merged 3 commits into
mainfrom
fix-sql-literal-roundtrip-305xx
Oct 26, 2026
Merged

aptend merged 3 commits into
mainfrom
fix-sql-literal-roundtrip-305xx

Conversation

@aptend

@aptend aptend commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fix successful-looking SQL/CSV dumps that could not be restored or changed values/session state during restore.

  • Quote FLOAT/DOUBLE NaN and infinities. Emit CAST expressions for max finite FLOAT wire values and signed zero so SQL restore preserves their representation.
  • Save the restore session's SQL mode once, temporarily remove NO_BACKSLASH_ESCAPES while loading data, and restore the original mode at the end. The script-level saved variable is distinct from the case-insensitive per-view variable, so dumps containing views also restore the original mode.
  • Escape CSV paths and field delimiters as SQL literals. Reject a backslash field delimiter before connecting or writing output because it conflicts with LOAD DATA escapes and the NULL marker; single-quote and multibyte delimiters remain supported.
  • Use the CSV basename for LOAD DATA LOCAL so backups can be moved and imported from the directory containing the CSV files. Preserve an absolute path for non-LOCAL loads, which resolve files on the server.
  • Propagate CSV write, final-flush and close failures without emitting MODUMP SUCCESS.

Regression coverage includes CLI rejection of the backslash delimiter with empty stdout, separate script/view SQL-mode save and restore variables (compared case-insensitively), and LOCAL/non-LOCAL paths including quote/backslash identifiers. Delimiter validation checks both errors and the returned rune.

Rebased onto main 828bbfe after #66 merged; the catalog account scope, hidden-table filtering, empty relation kinds and SHOW CREATE guards are preserved. Independent subagent review of final head 0ccca91 found no unresolved blockers. The review caught an upstream external-table test with stale catalog mocks; its scoped queries/args are now updated without weakening the original assertions. Independent focused tests, the corrected external-table case and the final full make test pass. The rebased production code also passes the same 14 live scenarios.

Validation (local, Linux/amd64, Go 1.26.4):

  • The three follow-up regressions fail on the previous PR implementation and pass with these changes.
  • make build, make test (go test -mod=readonly -count=1 ./...), go vet -mod=readonly ./cmd/mo-dump, and incremental lint pass. The local golangci-lint v2 used a temporary migrated copy of the existing configuration; repository lint/CI configuration is unchanged.
  • Real MySQL-protocol export/restore against an isolated MatrixOne LOG/TN/CN instance: seven scenarios run twice on the same instance, 14/14 pass, with database teardown verified after each round.
  • Live cases cover FLOAT/DOUBLE NaN/infinities, signed zero, positive/negative max values and subnormals, NULL and escaped strings across five restore modes; two dependent views with different persisted modes and three target modes; relocated LOCAL CSV files with comma/single-quote delimiters; rejected backslash delimiter with no CSV output or source changes; non-LOCAL restoration from an apostrophe path in default/NO_BACKSLASH_ESCAPES modes; and a 1,024-byte file-size limit that returns exit 1 without LOAD DATA or a success marker.

MatrixOne #30640 (large-field CSV loading) remains outside this PR's scope.

Fixes matrixorigin/matrixone#30596
Fixes matrixorigin/matrixone#30598
Fixes matrixorigin/matrixone#30587
Fixes matrixorigin/matrixone#30590
Fixes matrixorigin/matrixone#30628
Fixes matrixorigin/matrixone#30481

@mergify

mergify Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@aptend

aptend commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Heads-up on overlap: matrixone-side sessions opened #60/#61 covering the same six issues with a different approach (sql_mode-independent CHAR(92)/doubled-quote literals for #30587 instead of the script-level sql_mode pin used here; quoted literals instead of CAST for #30598). Both are now closed as superseded by this PR, which is live-validated for all six. Two follow-up notes: (1) this PR has no unit tests — worth adding the regression matrix (float boundary, literal safety, infile path, final-flush error) in a follow-up; (2) the sql_mode pin approach mutates the restore session mode and must restore it afterwards — the body says it does; reviewers should double check the pinned/unpinned pairing.

@aptend

aptend commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main and hardened the #30587 sql_mode pin. New head 35e473c.

Merge resolution (fixes the DIRTY state vs. main):

Two functional changes to the pin itself:

  1. The SET @MODUMP_SAVED_SQL_MODE / SET SESSION sql_mode pair now fires once at the top of the script, before the database loop. Previously it sat inside the per-database "dump all tables" block, so (a) a multi-database dump overwrote @MODUMP_SAVED_SQL_MODE with the already-stripped mode and the end-of-script restore restored the wrong value, and (b) -tbl-filtered dumps skipped the pin entirely.
  2. The NO_BACKSLASH_ESCAPES removal no longer uses a bare REPLACE: it strips the mode from a comma-wrapped list — TRIM(BOTH ',' FROM REPLACE(CONCAT(',',@@session.sql_mode,','),',NO_BACKSLASH_ESCAPES,',',')) — so it cannot produce an invalid empty token (X, / ,X / X,,Y) regardless of the mode's position.

Unit-test regression matrix (float boundary, literal safety, infile path, final-flush error) still planned as a follow-up PR, per the earlier comment.

@aptend

aptend commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

合并了最新 main(#63 #65 已入库后出现冲突),现已解好:

  • showLoad 保留本 PR 的 plain-fname + sqlStringLiteral 语义(#30481 #30590),表名换成 main 统一的 quoteIdentifier。
  • 升级 4 处按字节精确比对的测试断言,纳入 #30587 的 sql_mode 钉入行(TestDumpIdentifiers / TestDumpOtherRelationIdentifiers / 外部表注释注入测试 / TestDumpViewDependencies 失败路径);成功路径的 restore 计数对 main 的 per-view sql_mode 包装保持按用例兼容。
  • 本地 make build + go test ./... 全绿。待 review。

@aptend

aptend commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH 您好,方便时帮忙 review 一下这个 PR?

它汇总了 mo_dump 侧六个 dump/restore 安全问题的修复(对应 matrixone 侧 #30596 #30598 #30587 #30590 #30628 #30481),#60/#61 已作为重复关闭:

现在只差一个 approve 就能进 Mergify 队列,有任何要改的我马上跟进。谢谢!

@aptend
aptend force-pushed the fix-sql-literal-roundtrip-305xx branch from ade575d to 1b1a4dd Compare October 26, 2026 04:12
- quote DOUBLE NaN/+Inf/-Inf like FLOAT; emit cast('<v>' as float) for
  finite float outside the float32 range and cast('-0' as <typ>) for
  negative zero, so restore keeps source values (matrixorigin/matrixone#30596 #30598)
- pin the restore session sql_mode to backslash-escapes mode and restore
  it afterwards (matrixorigin/matrixone#30587)
- emit LOAD DATA with an escaped relative CSV path and escaped delimiter
  instead of an unescaped absolute path (matrixorigin/matrixone#30590 #30481)
- propagate csv writer/close errors so a truncated dump exits nonzero
  instead of declaring MODUMP SUCCESS (matrixorigin/matrixone#30628)
@aptend
aptend force-pushed the fix-sql-literal-roundtrip-305xx branch from 1b1a4dd to 0ccca91 Compare October 26, 2026 05:03
@aptend

aptend commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Self-review completed for 0ccca91e21512c9bf5758d506a5789d7649e9678 against main 828bbfe, including a separate review by an independent subagent. No outstanding blockers for the changes in this PR.

The full diff and its restore consumers were reviewed, covering numeric roundtrip fidelity, script/view SQL-mode save and restore, CSV framing and SQL quoting, LOCAL/non-LOCAL path semantics, and CSV resource/error handling. The latest main's catalog account scope and table classification behavior are preserved.

Validation completed locally on Linux/amd64 (mo_dump: Go 1.26.4 with CGO; live MatrixOne consumer: c2365a4a84b1):

  • make build, make test, go vet, formatting/diff checks, and incremental lint pass.
  • Independent focused regression tests and the final full make test pass.
  • Real MySQL-protocol export/restore against an isolated LOG/TN/CN instance passes 14/14 scenarios: seven scenario groups run twice on the same instance, with teardown verified after each round.
  • Live coverage includes FLOAT/DOUBLE special and boundary values, signed zero and NULL, escaped strings across five restore modes, dependent views with different persisted modes, relocated CSV files, custom delimiters, non-LOCAL loads, and the file-size-limit error path.

The reviewed head is pushed and ready for maintainer review.

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