Repository navigation
fix(load): keep trailing-CR strip inside the current field - #30498
Conversation
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? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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
csvparserandexternal/...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 2panic 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.
…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.
84a2413 to
e392b52
Compare
Merge Queue Status
This pull request spent 12 seconds in the queue, including 1 second running CI. Required conditions to merge
|
## 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.
What type of PR is this?
Which issue(s) this PR fixes:
issue #30462
What this PR does / why we need it:
LOAD DATAwithLINES TERMINATED BY '\n'drops a trailing\rat end of record so CRLF files load cleanly. The strip ran after field boundaries had already been recorded, so a\rsitting before a field separator — e.g. the four-byte filea~\r0withFIELDS 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 withslice bounds out of range [:3] with length 2(recovered as ERROR 20101).Fix: only strip the trailing
\rwhen it belongs to the current, not-yet-bounded field (len(recordBuffer)-1 >= last recorded boundary). CRLF handling for genuinely trailing\ris unchanged. The invariantfieldIndexes[i] <= len(recordBuffer)is restored at its single producer, coveringreadRow, theTrimLastSepshortcut andreadColumns(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 existingunterminated quoted fielderror.Validation:
TestTrailingCRBelongsToSealedFieldcovers 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.slice bounds out of range [:5] with length 4. The corrected parser passes the same scenario.8ed73ff8153, completepkg/sql/util/csvparserandpkg/sql/colexec/externalnormal and race UT pass through the repository CGo wrapper (-count=1, Go 1.27.1, darwin/arm64). Native inputs and artifact provenance were verified.LOAD DATAsucceeds (5 executions), row HEX is617E0D(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.