From 562ff11c118386138a6dfb3bbcfa3733a7d3d1d0 Mon Sep 17 00:00:00 2001 From: mavrick Date: Fri, 27 Nov 2026 23:27:26 -0600 Subject: [PATCH] fix(data): count whitespace values read as null --- crates/data/src/io.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/data/src/io.rs b/crates/data/src/io.rs index 1dfab7d705..300f2cc72a 100644 --- a/crates/data/src/io.rs +++ b/crates/data/src/io.rs @@ -3131,7 +3131,7 @@ pub(crate) fn read_rows( let gt = if xy.is_some() { GeometryType::Point } else { GeometryType::None }; let mut ds = Dataset::new(name, gt, fields); let mut srid: Option = None; - let (mut unread_dates, mut null_text) = (Vec::new(), Vec::new()); + let (mut unread_dates, mut null_text, mut whitespace) = (Vec::new(), Vec::new(), Vec::new()); for (k, &i) in columns.iter().enumerate() { crate::source::unread_dates_by(&mut unread_dates, k, workbook.and_then(|(_, empty)| empty.get(i)).copied().unwrap_or(0)); } @@ -3200,6 +3200,9 @@ pub(crate) fn read_rows( crate::source::null_text_at(&mut null_text, k); } let value = Value::parse_file(&text, f.ty); + if value.is_null() && f.ty != FieldType::Text && !text.is_empty() && text.trim().is_empty() { + crate::source::null_text_at(&mut whitespace, k); + } // The marker is counted once, as one, not also as a date that isn't one. if f.ty.is_temporal() && !marker && !text.trim().is_empty() && value.is_null() { crate::source::unread_date(&mut unread_dates, k); @@ -3306,6 +3309,14 @@ pub(crate) fn read_rows( report.unread_dates = crate::source::unread_dates_note(&ds.fields, &unread_dates); report.messages.extend(report.unread_dates.clone()); report.messages.extend(crate::source::null_text_note(&ds.fields, &null_text)); + let total: usize = whitespace.iter().sum(); + if total > 0 { + let what = if total == 1 { "1 whitespace-only value was".into() } else { format!("{total} whitespace-only values were") }; + report.messages.push(crate::loss::Note::loss( + crate::loss::LossCode::DroppedValue, + format!("{what} read as empty (null), in {}", named_fields(&ds.fields, &whitespace)), + )); + } Ok((ds, report)) } @@ -8439,6 +8450,21 @@ mod tests { assert!(losses(&clean).is_empty()); } + #[test] + fn csv_counts_only_whitespace_values_that_numeric_typing_loses() { + let csv = "value,note\n1,keep\n\" \",\" \"\n\" \",two\n\"\t\",three\n\" \t \",four\n,empty\n,marker\n"; + let (ds, report) = read_csv(csv, "typed").unwrap(); + assert_eq!(ds.fields[0].ty, FieldType::Integer); + assert_eq!(ds.value(1, 0), &Value::Null); + assert_eq!(ds.value(1, 1), &Value::Text(" ".into())); + let losses = report.messages.iter().filter(|n| n.contains("whitespace-only")).collect::>(); + assert_eq!(losses.len(), 1, "{:?}", report.messages); + assert_eq!(losses[0].loss, Some(crate::loss::LossCode::DroppedValue)); + assert_eq!(losses[0].as_str(), "4 whitespace-only values were read as empty (null), in 'value'"); + let (_, clean) = read_csv("value,note\n1,keep\n,empty\n", "clean").unwrap(); + assert!(clean.messages.iter().all(|n| !n.contains("whitespace-only"))); + } + /// CSV and dBase read text ``, in any case and with spaces around it, as null. #[test] fn csv_and_shapefile_say_text_null_markers() {