Skip to content

fix(load): keep trailing-CR strip inside the current field - #30498

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
iamlinjunhong:fix-30462-csv-cr-strip
Oct 18, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
iamlinjunhong:fix-30462-csv-cr-strip

Conversation

@iamlinjunhong

@iamlinjunhong iamlinjunhong commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #30462

What this PR does / why we need it:

LOAD DATA with LINES TERMINATED BY '\n' drops a trailing \r at end of record so CRLF files load cleanly. The strip ran after field boundaries had already been recorded, so a \r sitting before a field separator — e.g. the four-byte file a~\r0 with FIELDS TERMINATED BY '0' ENCLOSED BY '~' — was removed from an already-bounded field: the recorded boundary then pointed past the end of the record buffer, and decoding the row panicked with slice bounds out of range [:3] with length 2 (recovered as ERROR 20101).

Fix: only strip the trailing \r when it belongs to the current, not-yet-bounded field (len(recordBuffer)-1 >= last recorded boundary). CRLF handling for genuinely trailing \r is unchanged. The invariant fieldIndexes[i] <= len(recordBuffer) is restored at its single producer, covering readRow, the TrimLastSep shortcut and readColumns (header) consumers with no consumer-side changes.

Result for the issue input is a normal row ('a~\r', '') instead of a panic; a real unterminated quote still returns the existing unterminated quoted field error.

Validation:

  • TestTrailingCRBelongsToSealedField covers 11 exact-value scenarios: issue input, quote-free variants, repeated empty trailing fields, CRLF controls, TrimLastSep, header decoding, unterminated quotes and small-buffer reuse. The five-byte-buffer case now puts CR at the refill boundary, preserves the sealed field plus its trailing empty field, then strips ordinary CRLF on the next row.
  • Negative control: retaining the strengthened small-buffer test and restoring only the original parser through a Go source overlay reproduces slice bounds out of range [:5] with length 4. The corrected parser passes the same scenario.
  • After rebase onto main 8ed73ff8153, complete pkg/sql/util/csvparser and pkg/sql/colexec/external normal and race UT pass through the repository CGo wrapper (-count=1, Go 1.27.1, darwin/arm64). Native inputs and artifact provenance were verified.
  • Incremental gofmt, vet, pinned molint and golangci-lint 2.14.0 checks cover csvparser and its direct production consumers external/plan. No new diagnostics; five molint consumer diagnostics reproduce identically on the main baseline. Golangci reports 0 issues.
  • Historical standalone end-to-end validation before this rebase: the issue's exact LOAD DATA succeeds (5 executions), row HEX is 617E0D (a~\r) plus an empty non-NULL second field; CRLF controls and server health pass. This SQL validation was not rerun for the test-only follow-up.

The follow-up replaces an existing test fixture and its expected values; production logic is unchanged.

Remaining checks: full distributed load_data BVT and CI for the updated head are pending and are not claimed as passed.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

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

Reviewed head 84a2413b735c2ecb7abccc4ed44d6e93cf9e27fa against 887019c3dc2a19896c03f2a2cd192299a5c1b3db. No blocking findings.

The ownership/design is appropriate: readRecord produces the field boundaries, so enforcing the trailing-CR rule there protects readRow, header decoding and TrimLastSep together. The guard preserves already-sealed fields without duplicating validation in consumers, introducing state, or changing the input-position/framing path. The issue input correctly becomes a normal row; a quote that actually opens an unterminated field still returns the existing syntax error.

Validation on macOS/arm64:

  • Full csvparser and external/... package tests passed through the repository CGo wrapper; the unrelated MinIO-dependent Arrow test was skipped.
  • Restoring only the base parser implementation reproduced the reported [:3] with length 2 panic with the regression test.
  • Independent parser probes passed for sealed/current CRs, small-buffer boundaries, multibyte separators, quoted/escaped fields, subsequent records, ScanRecord positions, and reset after failure.
  • A public SQL integration probe passed repeated LOAD DATA operations with exact HEX/non-NULL assertions, normal CRLF controls, malformed-quote failure, subsequent connection health, and cleanup.
  • Production delta is +12/-1; tests +133/-0; documentation unchanged. The added work is one existing-boundary lookup only when the old trailing-CR condition is reached. Precompiled base/head microbenchmarks run in ABBA order (8 samples each) showed median differences of about +1.0% for LF and +1.1% for CRLF, within much wider sample variation, with identical allocation counts. This does not establish a performance regression or prove zero overhead; it is not a TP workload benchmark.
  • Current CI is green. There were no existing substantive review threads to resolve.

One non-blocking test-quality improvement is noted inline: make the small-read-block scenario reach the CR-finalization branch it claims to cover. The core regression and independent boundary probe already validate the fix.

iamlinjunhong and others added 2 commits October 18, 2026 09:46
…gin#30462)

readRecord drops a trailing \r when LINES TERMINATED BY '\n' so CRLF files
load cleanly. The strip ran after field boundaries had already been recorded,
so a \r sitting before a field separator (e.g. 'a~\r0' with FIELDS
TERMINATED BY '0') was removed from an already-bounded field: the recorded
boundary then pointed past the end of the record buffer and readRow panicked
with 'slice bounds out of range'.

Only strip the \r when it belongs to the current, not-yet-bounded field
(len(recordBuffer)-1 >= last recorded boundary). CRLF handling is unchanged
for bounded fields. Adds regression coverage for the issue repro, quote-free
variants, repeated empty trailing fields, CRLF controls, TrimLastSep, header
(HeaderSchemaMatch) consumers, unterminated-quote errors and small read
blocks.
@mergify

mergify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-18 02:48 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-10-18 02:48 UTC · at 154a24d463d60ae507471b8ae58f56a4d4055541 · squash

This pull request spent 12 seconds in the queue, including 1 second running CI.

Required conditions to merge

@mergify
mergify Bot merged commit 154a24d into matrixorigin:main Oct 18, 2026
@mergify mergify Bot removed the queued label Oct 18, 2026
XuPeng-SH added a commit that referenced this pull request Nov 15, 2026
## 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:

Related to #30462; backport of main #30498.

## What this PR does / why we need it:

The end-of-record CR strip can remove a byte from an already sealed CSV
field. Its stored boundary then exceeds the row buffer and row/header
decoding panics. Preserve sealed-field bytes by stripping CR only within
the current open field. Genuine CRLF terminators retain their existing
behavior.

Backport the producer-side guard from #30498 to 4.2-dev, with
exact-value controls for row/header decoding, TrimLastSep, refill/reuse,
unterminated quotes and 4.2 retained-field metadata.

Validation on Linux/amd64, CPU-only, Go 1.26.4:
- The same regression tests with original 4.2 source reproduce
slice-bounds panic; the fixed full csvparser package passes normal, race
and vet.
- The diagnostic 4,176 input/config/block combinations reproduce 44
panicking combinations at baseline and zero with this fix.
- Real LOAD DATA on one private CN preserves the issue field as HEX
617E0D and an empty non-NULL second field; normal CRLF and
sealed/current CR controls pass. An unterminated enclosure returns the
existing error (20301); repeated LOAD and subsequent queries succeed.
- Incremental golangci-lint reports 0 issues. Final retained-field test
addition leaves production/native inputs identical to the SQL-tested
build. Test services and data have been removed.

Full hosted CI is pending. This PR is independent of the CSV allocation
optimization.

This branch was previously deployed

1 inactive deployment
ci — e392b527 Deployed Oct 18, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants