Skip to content

fread: parse quoted numeric fields containing leading or trailing whitespace - #7962

Merged
aitap merged 6 commits into
Rdatatable:masterfrom
loganb7869:fix-fread-quoted-numbers-whitespace
Nov 29, 2026
Merged

aitap merged 6 commits into
Rdatatable:masterfrom
loganb7869:fix-fread-quoted-numbers-whitespace

Conversation

@lds295

@lds295 lds295 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes an issue where fread() incorrectly parsed numbers enclosed in quotation marks as character when leading or trailing whitespace was present inside the quotes (e.g. " 123" or "123 "), whereas base read.table(..., strip.white=TRUE) parses them as numeric.

Root Cause

During type detection and data reading in src/fread.c, fread advanced over the opening quote (ch++) directly onto whitespace without calling skip_white(). Because numeric parsers strictly require immediate signs or digits, they rejected the whitespace and defaulted the column type to CT_STRING.

Adding skip_white() after opening quotes and before closing quotes allows fread to skip inner whitespace around numbers, matching the behavior of unquoted numbers and read.table().

Example

tmp <- tempfile(fileext = ".csv")
writeLines(c(
  '"stringvar";"numvar"',
  '"abc";" 123"',
  '"def";" 456"',
  '"ghi";" 789"'
), tmp)

fread(tmp, sep=";", quote='"', strip.white=TRUE)
# Before: numvar was <character>
# After:  numvar is  <integer>

Checklist

  • Added unit tests in inst/tests/tests.Rraw
  • Added entry to NEWS.md
  • Tests pass

@aitap

aitap commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Did @DiamondCat106 also work on this PR?

@lds295

lds295 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

@DiamondCat also worked on this

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.01%. Comparing base (fd2dc3c) to head (fac8ca2).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7962   +/-   ##
=======================================
  Coverage   99.01%   99.01%           
=======================================
  Files          88       88           
  Lines       17493    17495    +2     
=======================================
+ Hits        17321    17323    +2     
  Misses        172      172           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aitap aitap left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Excellent work, thank you!

@aitap
aitap merged commit 1d749e2 into Rdatatable:master Nov 29, 2026
15 checks passed
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.

strip.white applied after type detection, causing numeric columns with leading/trailing spaces to be read as character

3 participants