Repository navigation
fix(data): exports say text that a NUL, a CSV apostrophe or a blank KML field name loses on reading back (#2415) - #2749
Conversation
…ML field name loses on reading back (opengeos#2415) Three exports changed text with nothing said: - Shapefile: reading a .dbf keeps only the text before a NUL, so "ab\0cd" reads back as "ab" and "\0x" as null. TextLosses counts text with a NUL in the 254 bytes a Character column keeps. - CSV and WKT CSV: text such as '=x isn't guarded on write, as it already starts with an apostrophe, and reading takes the apostrophe for the guard and drops it. TextLosses counts text starting with an apostrophe before a formula's first character. - KML: a field whose name is blank is written as <Data name="">, which reading drops, column and values. kml_blank_names_note counts such fields and their written values. Each is said with a count and the fields, as opengeos#1907's notices are; a streamed CSV export adds the counts up a chunk at a time.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
| TextRead::Cell if s.is_empty() => &mut loss.blank, | ||
| TextRead::Cell | TextRead::Dbf if Value::is_null_marker(s) => &mut loss.markers, | ||
| // In the 254 bytes a Character column keeps. | ||
| TextRead::Dbf if s.bytes().take(254).any(|b| b == 0) => &mut loss.nul, |
There was a problem hiding this comment.
The NUL arm sits in the same match as the Dbf padding and blank arms, so a value that has both a NUL and spaces before it is counted only as nul. For example, "ab \0cd" reads back as "ab": the NUL cut and the trailing space drop both happen, but only the NUL is said. The space loss goes unreported, which is the silent-loss case AGENTS.md forbids. This is an edge case and I'm not certain how common it is in real dBase files (confidence about 50%), but the fix is small: count the NUL with its own if before the match, so the padding and blank arms still run for the same value.
Category: bug
There was a problem hiding this comment.
Fixed in c666d0a. The NUL is now counted apart, before the dBase arms. The text reading keeps before it is then judged as other dBase text, so its trailing spaces are counted as such. A blank one, which reads as null, is left to the NUL's notice, which now says so: "…keeps only the text before it, read as empty (null) when that is blank". So "ab \0cd" gives both notices.
How it was checked: shapefile_says_text_cut_at_a_nul now also writes "ab \0cd" and " \0y", and reads the file back.
- On the PR's previous code it fails: only the NUL notice, without "1 text value ends with spaces".
- With the fix it passes, and reading back gives
"ab", null and"ab"in 'a', and null for" \0y". - Also clean: the data crate's lib tests (apart from the
oversized_*tests, which compare a limit that follows free memory and failed while another build ran on this machine; this change doesn't touch them), its 70 integration tests, and clippy on stable and 1.100.0.
| /// (#2415). A field with no value written is said by [`kml_empty_fields_note`]. | ||
| fn kml_blank_names_note(ds: &Dataset, target: &str) -> Option<String> { | ||
| let blank: Vec<usize> = ds.fields.iter().enumerate().filter(|(_, f)| f.name.trim().is_empty()).map(|(k, _)| k).collect(); | ||
| let values: usize = ds |
There was a problem hiding this comment.
This does up to three passes over every feature, and the first runs even when there are no blank-named fields. Two costs follow. For a national KML layer with no blank names, it adds a full scan for nothing. Also, with_values is blank.len() × features in the worst case, because the any scans a whole layer for each blank field with no non-null value. Two suggestions: return early when blank is empty, and compute the per-field has_value flags and the total value count in one pass. The new loops also don't call progress::check(); the preceding kml_empty_fields_note pass does, so Cancel still works in practice, but the doc comment should say so.
Category: performance
There was a problem hiding this comment.
Fixed in c666d0a:
kml_blank_names_notereturns at once when no field has a blank name, so a layer without one costs no scan.- Otherwise it counts each blank-named field's values, and whether it has any, in one pass over the placemarks written.
- It calls
progress::check()per placemark, askml_empty_fields_notedoes, and returnsResult, so Cancel stops it too.
kml_says_fields_with_a_blank_name still passes with the same counts: 2 fields and 3 values, with no notice for a blank-named field with no values.
| a.padded += b.padded; | ||
| a.name_padded += b.name_padded; | ||
| a.nul += b.nul; | ||
| a.apostrophe += b.apostrophe; |
There was a problem hiding this comment.
The new nul and apostrophe += lines are not exercised by the chunked-equality test (text_losses_of_chunks_add_up_to_the_whole_layer, around line 7655), whose data has no NUL or apostrophe values. A missing or wrong += for either counter would pass that test. Adding a '=x value and a NUL-containing Shapefile value to a chunked case would cover them.
Category: quality
There was a problem hiding this comment.
Covered in c666d0a. text_losses_of_chunks_add_up_to_the_whole_layer now also adds up two layers in chunks of every size, checking that the sum equals the whole and that its notes say the loss:
- a CSV layer with
'=xand'+z; - a Shapefile layer with
"a\0b"and"ab \0c".
A missing or wrong += for apostrophe or nul now fails it.
Code reviewI reviewed the diff against No prior review threads exist on this PR, so no earlier concerns were left alone. Bugs
Security
Performance
Quality
AGENTS.md
Change sizeCompared with main (merge base 9cc7d88 to PR head 0c5ce67).
|
📖 Docs previewCommit |
…file text does, and is said so (opengeos#2415) Review on opengeos#2749: - "ab \0cd" reads back as "ab": the NUL cut and the space before it both go, but only the NUL was counted. The NUL is now counted apart, and the text reading keeps before it is judged as other dBase text: its trailing spaces counted, a blank one (read as null) left to the NUL's notice, which says so. - kml_blank_names_note returns at once without a blank-named field, and counts the fields and values in one pass, looking at Cancel. - The chunked TextLosses test covers the NUL and apostrophe counts.
| } | ||
|
|
||
| /// KML fields whose name is blank once trimmed, with values in the placemarks written: each is | ||
| /// written as `<Data name="">`, which reading drops, so the column and its values are gone |
There was a problem hiding this comment.
The doc says each blank-named field is written as <Data name="">, but the writer uses the field's name as it is (interop.rs, the <Data name=…> line). A name of spaces or a tab is written as <Data name=" "> or <Data name="">. The reader drops all of them because it trims the name, so the behaviour is right and only the example is off. Suggest saying so:
/// KML fields whose name is blank once trimmed, with values in the placemarks written: each is
/// written with its name as it is (`<Data name="">`, or spaces or a tab), which reading drops, so
/// the column and its values are gone (#2415). A field with no value written is said by
/// [`kml_empty_fields_note`].
Category: quality
| let ds = text_layer(&["note", "code"], &[&[Some("'=x"), Some("a")], &[Some("'kept"), Some("'-5")], &[Some("=y"), Some("b")]]); | ||
| let said = "t: 2 text values start with an apostrophe before =, +, -, @, a tab or a line break, which reading the file back takes for the formula guard and drops, in 'note', 'code'"; | ||
| assert_eq!(text_notes(&ds, "csv"), [said]); | ||
| assert_eq!(text_notes(&ds, "wkt"), [said]); |
There was a problem hiding this comment.
The PR says every new test writes the file and reads it back, so the notice describes what reading does. This one checks only the WKT notes. The WKT CSV path goes through the same CSV reader, which applies unguard, so the claim looks right, but nothing here shows it. Suggest reading the WKT file back too and checking that note comes back as =x, 'kept and =y, as the CSV read-back above does.
Category: quality
Code reviewBugsNone confirmed. I checked the new NUL, apostrophe and KML blank-name counts against the reader and writer code, and each count matches what the read-back does:
Confidence: about 85%. I didn't run the tests; the PR reports them passing. SecurityNone found. PerformanceNone new. The earlier concern about extra passes is fixed: Quality
AGENTS.mdNo breaches found. Each new notice is counted with a test both ways (lossy and clean). No new layer copy is added, and the new KML pass checks Cancel. The rows and layering rules don't apply to this file. Prior reviewsI left 3 earlier concerns alone, because the author fixed each in c666d0a and I confirmed the fixes in the code:
I posted 2 new inline comments, both quality. Change sizeCompared: the whole PR against main (merge base 9cc7d88 to PR head c666d0a).
|
Summary
#2415 lists exports that change text with no notice. Some are already handled on main:
interop::xmlrefuses text XML can't hold, naming the field and the character.folderorstyle: these come back under their own names.This PR covers the three that remain. Each gets a counted export notice that names its fields, like #1907's notices:
.dbfkeeps only the text before a NUL, so"ab\0cd"comes back as"ab"and"\0x"as null.TextLossescounts text with a NUL in the 254 bytes a Character column keeps, under its dBase read: "t: 2 text values hold a NUL character, and reading the file back keeps only the text before it, read as empty (null) when that is blank, in 'a'". The text kept before a NUL loses its trailing spaces as other dBase text does, which is counted as such.'=xisn't guarded on write, since it already starts with'. Reading takes the'for the guard and drops it, so it comes back as=x.TextLossescounts text that starts with'followed by a formula's first character (the same setunguardchecks), under its CSV read. That covers Data Interop's WKT CSV as well: "t: 2 text values start with an apostrophe before =, +, -, @, a tab or a line break, which reading the file back takes for the formula guard and drops, in 'note', 'code'". There's no way to write such text that today's reader takes back exactly, so the loss is reported rather than changed.<Data name="">. Reading drops every such entry, so the column and all its values are gone. The newkml_blank_names_note, besidekml_empty_fields_note, counts the fields and their written values: "t: 2 fields with a blank name, and 3 values in them, won't read back: reading KML drops extended data without a name". A blank-named field with no values written is already reported as a field with no values, so it isn't counted again.A streamed CSV export adds up the new counts a chunk at a time, like the others (
TextLosses += …).Reach every surface
export_losses_notesor the streamedTextLosses, so the notices appear wherever an export's notices are shown.Testing
shapefile_says_text_cut_at_a_nul: no notice (left: []).csv_says_an_apostrophe_reading_takes_for_the_guard: no notice (left: []).kml_says_fields_with_a_blank_name: no notice of the blank names. The only notice is "KML omitted 1 field with no values … (' ')", for the blank-named field that has no values.cargo test -p rustgis-data --lib: 1749 passed and 3 failed. The failures areoversized_shapefile_part_reports_whole_dataset_advice,oversized_geojson_publish_gives_vector_adviceandoversized_shapefile_dbf_read_for_publish_gives_dataset_advice. Each builds its expected size limit frommax_bytes(), which follows the memory free at the moment it's called, and another build was running on the machine. Run alone,cargo test -p rustgis-data --lib -- oversizedpassed all 11, and this change doesn't touch them.cargo test -p rustgis-data --test integration: 70 passed.cargo fmt --all --check: clean.cargo clippy -p rustgis-data --all-targets -- -D warnings: clean on stable and on 1.100.0, the CI toolchain.shapefile_says_text_cut_at_a_nul:"ab\0cd"and"\0x"give one notice counting 2 in 'a'. Read back they're"ab"and null. NUL-free text says nothing.csv_says_an_apostrophe_reading_takes_for_the_guard:'=xand'-5give one notice counting 2 in 'note' and 'code', for CSV and WKT CSV alike. Read back they're=xand-5, while'keptand the writer-guarded=ycome back as they were. Clean text ('kept,=y,it's) says nothing.kml_says_fields_with_a_blank_name: fields""and" "with 3 values give one notice. Read back, no column has a blank name. A blank-named field with no values gets no notice from this check.Review round (c666d0a)
"ab cd"reads back as"ab", losing both the text after the NUL and the space before it, but only the NUL was counted. The NUL is now counted apart, and the kept text is judged as other dBase text. Its trailing spaces are counted, and a blank one, read as null, is covered by the NUL's notice.shapefile_says_text_cut_at_a_nulnow writes"ab cd"and" y"too. On the previous code it failed, giving only the NUL notice. With the fix it passes, reading back"ab"and null.text_losses_of_chunks_add_up_to_the_whole_layercovers the NUL and apostrophe counts.oversized_*, which failed both alone and in the full run while another build ran on this machine. Their limit follows free memory, and below the full limittoo_large_held([Bug]: Undo steps on a large layer each hold a full copy of its rows, so memory grows with every edit #2431) words the error differently, so it doesn't parse as they expect. This change doesn't touch them, and CI ran them.UI evidence
No UI change. The notices appear in an export's existing list of notices.
Related issue
Part of #2415. Its parts handled before this PR are listed above.
Summary by CodeRabbit