Skip to content

fix: csv dump corrupts literal \N and NULL text values (matrixorigin/matrixone#30568) - #63

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
ck89119:issue-30568-main
Oct 22, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
ck89119:issue-30568-main

Conversation

@ck89119

@ck89119 ck89119 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In -csv mode, mo-dump silently turned two kinds of legal text values into SQL NULL on restore (matrixorigin/matrixone#30568):

  • A literal text value \N was exported as the same raw bytes \N that SQL NULL uses, because escapeString special-cased s == "\\N" and could not tell the NULL marker apart from data. LOAD DATA reads the raw field \N as NULL (even inside quotes), so the text value was lost.
  • A literal text word NULL was written unquoted, and both MatrixOne and MySQL read an unquoted NULL word as SQL NULL when FIELDS ENCLOSED BY is non-empty.

Restore exited 0 and the dump was marked MODUMP SUCCESS, i.e. silent data loss on backup/restore of text columns.

Changes

  • Distinguish NULL structurally: NULL-ness now comes from nil RawBytes, never from string content, so a text \N is escaped to \\N and can no longer collide with the NULL marker on the wire.
  • Explicit CSV framing (writeCsvRecord / csvFieldNeedsQuote) instead of encoding/csv: fields containing the delimiter, ", \r, \n, or equal to NULL (case-insensitive) are quoted, so a literal NULL restores as the string 'NULL' (MySQL-documented behavior for quoted NULL); the SQL NULL marker \N stays unquoted.
  • Escape exactly once: backslash (and , under -enable-escape for string columns) is escaped a single time, replacing the escapeChars + escapeString double escape that corrupted values like a,b into a\,b.
  • Byte-level escaping/quoting keeps binary / invalid-UTF-8 values intact instead of rewriting them as U+FFFD.

Tests

  • Unit tests (cmd/mo-dump): table-driven cases for NULL, text \N / \\N / NULL / null, comma, double quote, LF, empty string, binary bytes, | delimiter, and -enable-escape.
  • End-to-end against a live MatrixOne instance (dump -csv + LOAD DATA LOCAL INFILE restore, compared per row via HEX(v) / v IS NULL):
    • default , delimiter — round-trip identical, including the issue's exact repro rows
    • -csv-field-delimiter '|' — identical
    • -enable-escape=true — identical (a,b stays a,b)
    • default (non-csv) INSERT mode — regression check, identical
    • table with VARBINARY rows containing 0xFF, embedded \\, " and LF — byte-identical

Fixes matrixorigin/matrixone#30568

In -csv mode, SQL NULL and the literal text value \N were written as the
same bytes, and a literal NULL word was written unquoted; LOAD DATA then
restored all of them as SQL NULL (silent data loss, MODUMP SUCCESS
notwithstanding).

- distinguish SQL NULL by nil RawBytes, not by string content, so a text
  \N is escaped to \\N and can no longer collide with the NULL marker
- write CSV records with explicit quoting instead of encoding/csv: a
  literal NULL word is forced into ".." quotes, which both MatrixOne and
  MySQL read back as the string 'NULL'; the SQL NULL marker \N stays
  unquoted
- escape backslash (and ',' under -enable-escape) exactly once instead
  of the escapeChars/escapeString double escape, and keep binary
  (non-UTF-8) values byte-identical while escaping and quoting
@ck89119 ck89119 changed the title fix #30568: csv dump corrupts literal \N and NULL text values fix: csv dump corrupts literal \N and NULL text values (matrixorigin/matrixone#30568) Oct 20, 2026
… delimiter

Regression fixes for the CSV writer switch in the previous commit:

- quote fields whose first rune is whitespace (as encoding/csv did):
  unquoted whitespace-only records are skipped as empty lines by older
  loaders, silently dropping rows on restore
- checkFieldDelimiter now rejects '"', '\r', '\n' (and NUL/RuneError),
  matching encoding/csv's delimiter rule, so an unusable delimiter fails
  fast instead of producing a successful-but-unrestorable dump

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

APPROVE at 14475e6 (base 324c33d).

The CSV framing boundary now distinguishes SQL NULL structurally from text, escapes bytes once, forces quoting for literal NULL, and preserves invalid UTF-8. Replacing encoding/csv is justified by the required field-quoting contract; obsolete regexp escaping/state is removed. Full production/test delta reviewed, including delimiter rejection and per-row state reuse.

Independent validation: all 47 package test/subtest events pass; build/vet/gofmt/diff checks pass. Fifteen keyed text/NULL/control cases restore identically twice for default delimiter, pipe delimiter and enable-escape; three binary cases also restore identically twice. The broader initial corpus uncovered two existing loader/export gaps: a one-column empty-string record is lost, and a pure CR value restores as empty. Both were independently reproduced on exact clean base 324c33d; they are disclosed here, not counted as regressions introduced by this fix. No universal CSV round-trip claim is made.

A focused writer microbenchmark shows improved medium/large-field CPU time but increased allocation bytes; no zero-cost claim is warranted. Merge with #57 must retain Rows.Err() as well as Flush() errors; the two current heads conflict in that boundary and tests. This approval covers the scoped NULL/escaping fix at this exact head. Live tests used an existing prebuilt service, not latest-main validation. No subagents or source edits.

@ck89119
ck89119 marked this pull request as ready for review October 20, 2026 14:25
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-20T14:31:52.782640Z 14475e6 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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.

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.

[Bug]: mo-dump CSV silently restores literal \N and NULL strings as SQL NULL

2 participants