← Back to Pull requests

GH-51368: [C++] Fix CSV reader dropping '\n' after a straddling '\r' inside a quoted field #51394

Closed
unknown closed this pull request without merging on Sep 28, 2026 14:42 UTC
View on GitHub
unknown unknown commented on Sep 19, 2026 15:42 UTC
Author

Rationale for this change

With newlines_in_values = true, a quoted CRLF sequence split across two input buffers was silently corrupted: "…\r at the end of one buffer and \n… at the start of the next lost the \n (it stayed inside the field as \r only). Reported in #51368.

The root cause is in CSVBufferIterator, which unconditionally skipped a leading \n whenever the previous buffer ended with \r. At that layer there is no quoting context, so the code cannot distinguish a \r\n that is a physical line separator from \r\n that is the contents of an unfinished quoted field.

What changes are included in this PR?

  • CSVBufferIterator no longer resolves straddling CRLF sequences (it still strips a leading UTF-8 BOM).

  • BlockReader now owns the decision via trailing_cr_ + a SkipStraddlingCRLF helper: the next buffer's leading \n is skipped only when the previous buffer's trailing \r was fully consumed as a line separator. When the \r belongs to an unfinished quoted field, it is carried in partial_ (non-empty), the flag stays false, and the \n is preserved as field contents.

  • The flag is updated in both SerialBlockReader (through consume_bytes, driven by how much of the buffer the parser consumed) and ThreadedBlockReader (through next_partial from the chunker), including the row-skipping paths. Empty buffers never overwrite the state.

  • An initial prev_ended_cr flag is plumbed through the block-reader factories for the edge case where a header row ends exactly at a buffer boundary with \r (the post-header buffer is then empty and the next buffer starts with \n).

  • New regression test TestStraddlingCRLF covering a quoted \r\n split across the boundary, a line-separator \r\n split across the boundary, and a header row ending with \r at the boundary. Registered for the serial, threaded-async and streaming readers.
  • Are these changes tested?

    Yes — a new StraddlingCRLF test in csv/reader_test.cc covers all three cases above with a block size that forces the split. (Note: the change was developed and cross-validated against a transliteration of the reader/chunker/parser pipeline; the gtest cases should be run in CI.)

    Are there any user-facing changes?

    No API changes. Files with newlines_in_values enabled whose quoted fields contain \r\n straddling a read block boundary are now parsed correctly instead of losing the \n.

    Closes #51368

  • GitHub Issue: #51368
  • Was AI used for this PR?

    PR code and description written by:

  • AI
  • Reviewed before submission by:

  • AI
  • Diff: 325 lines (+243 / -82) across 4 files Reviewer: Reranko05 Review duration: 12900m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 325 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.346
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #51394"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42% (MEDIUM) · Higher = larger blast radius
    42%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.6% · Safe to merge
    16.6%
    Repository