Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions crates/analysis/src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3355,7 +3355,9 @@ fn dissolve(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String> {
"Dissolve: {not_polygons} feature(s) left out, with their values: no geometry, an empty polygon, or a point or line rather than a polygon"
) });
}
messages.extend(rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default()));
let (said, rounded) = rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit, left for the release quality pass: after this change rounded holds a Vec<LossMessage> rather than the rounding counts, and said holds the messages. The shadowing works (the right side is evaluated first), but a name like (notices, rounded_losses) would read more clearly. The same pattern repeats at the Summarize Within, Summary Statistics, Pivot Table and Spatial Join call sites.

Category: quality

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Left for the release quality pass: a naming point, no change in behaviour.

messages.extend(said);
losses.extend(rounded);
Ok(ToolResult { losses, output: Output::Layer(out), messages })
}

Expand Down Expand Up @@ -6054,8 +6056,9 @@ fn count_points_on(inp: &dyn Inputs, p: &Json, threads: Option<usize>) -> Result
}
}
let mut messages = vec![format!("Summarize Within: {total} points counted")];
messages.extend(rounded_notice(&rounded, out.fields.get(first..).unwrap_or_default()));
Ok(ToolResult { losses: Vec::new(), output: Output::Layer(out), messages })
let (said, losses) = rounded_notice(&rounded, out.fields.get(first..).unwrap_or_default());
messages.extend(said);
Ok(ToolResult { losses, output: Output::Layer(out), messages })
}

/// A lon/lat line or ring as a `geo` line string, in degrees.
Expand Down Expand Up @@ -6471,8 +6474,9 @@ fn summary_statistics(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String>
}
let n = out.features.len();
let mut messages = vec![format!("Summary Statistics: {n} rows")];
messages.extend(rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default()));
Ok(ToolResult { losses: Vec::new(), output: Output::Table(out), messages })
let (said, losses) = rounded_notice(&rounded, out.fields.get(first_added..).unwrap_or_default());
messages.extend(said);
Ok(ToolResult { losses, output: Output::Table(out), messages })
}

/// The output fields of a statistics list over `ds`: `<STAT>_<field>` for each request, typed by
Expand Down Expand Up @@ -6581,17 +6585,23 @@ fn statistic_values(
/// naming their output fields `fields` (one per request, as `rounded` counts them), as a decimal
/// field holds whole numbers exactly only up to 2^53; and the whole numbers written as text past
/// the i64 range that were read as decimals, whose statistics aren't checked, naming their input
/// fields. Empty when nothing was rounded.
fn rounded_notice(rounded: &Rounded, fields: &[Field]) -> Vec<String> {
/// fields. The first is a loss, with its code; the second says they may be rounded, so it is a
/// plain notice. Both empty when nothing was rounded.
fn rounded_notice(rounded: &Rounded, fields: &[Field]) -> (Vec<String>, Vec<rustgis_data::loss::LossMessage>) {
let mut notices = Vec::new();
let mut losses = Vec::new();
// Each counted result came out rounded: a value changed.
let total: usize = rounded.results.iter().sum();
if total > 0 {
let names: Vec<&str> = rounded.results.iter().zip(fields).filter(|(n, _)| **n > 0).map(|(_, f)| f.name.as_str()).collect();
let results = if total == 1 { "result" } else { "results" };
notices.push(format!(
"{total} {results} rounded in {}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)",
names.join(", ")
));
losses.push(rustgis_data::loss::LossMessage {
code: rustgis_data::loss::LossCode::ChangedRounded,
text: format!(
"{total} {results} rounded in {}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)",
names.join(", ")
),
});
}
let total: usize = rounded.texts.iter().map(|(_, n)| n).sum();
if total > 0 {
Expand All @@ -6603,7 +6613,7 @@ fn rounded_notice(rounded: &Rounded, fields: &[Field]) -> Vec<String> {
names.join(", ")
));
}
notices
(notices, losses)
}

/// The statistics list of a tool that adds statistics to its own output (Dissolve, Summarize
Expand Down Expand Up @@ -6810,8 +6820,9 @@ fn pivot_table(inp: &dyn Inputs, p: &Json) -> Result<ToolResult, String> {
}
let n = out.features.len();
let mut messages = vec![format!("Pivot Table: {n} rows, {} pivot fields", columns.len())];
messages.extend(rounded_notice(&rounded, out.fields.get(first_pivot_field..).unwrap_or_default()));
Ok(ToolResult { losses: Vec::new(), output: Output::Table(out), messages })
let (said, losses) = rounded_notice(&rounded, out.fields.get(first_pivot_field..).unwrap_or_default());
messages.extend(said);
Ok(ToolResult { losses, output: Output::Table(out), messages })
}

/// Whether a pivot value is missing: null, or a number that isn't one.
Expand Down Expand Up @@ -8380,7 +8391,9 @@ fn spatial_join_on(inp: &dyn Inputs, p: &Json, threads: Option<usize>) -> Result
"{total} matched {numbers} in {names} {were} left out: a whole-number sum takes whole numbers in the 64-bit range, the other rules numbers a decimal holds"
) });
}
messages.extend(rounded_notice(&rounded, &merged_fields));
let (said, rounded) = rounded_notice(&rounded, &merged_fields);
messages.extend(said);
losses.extend(rounded);
Ok(ToolResult { losses, output: Output::Layer(out), messages })
}

Expand Down
4 changes: 2 additions & 2 deletions crates/analysis/tests/integration/spatial_join_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn join(layers: &Layers, merge: Json) -> ToolResult {
const JOINED: &str = "Spatial Join: 4 features joined (8 matches)";

fn rounded(n: &str, fields: &str) -> String {
format!("{n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)")
format!("ChangedRounded: {n} rounded in {fields}: a decimal field holds whole numbers exactly only up to 2^53 (9007199254740992)")
}

#[test]
Expand Down Expand Up @@ -299,7 +299,7 @@ fn a_number_too_large_for_a_decimal_is_left_out_and_counted() {
};
let res = join(&layers, json!([{"field": "n", "rule": "mean"}, {"field": "d", "rule": "sum"}]));
// D's mean of n leaves out "1e309", and its sum of d "-1E400"; C's mean of n is still rounded.
assert_eq!(said(&res), [JOINED.to_string(), rounded("1 result", "join_n"), notice("2 matched numbers", "join_n, join_d")]);
assert_eq!(said(&res), [JOINED.to_string(), notice("2 matched numbers", "join_n, join_d"), rounded("1 result", "join_n")]);
let out = layer(res);
assert_eq!((column(&out, "join_n")[3].clone(), column(&out, "join_d")[3].clone()), (Value::Float(3.0), Value::Float(4.5)));
for (n_rule, d_rule) in [("max", "min"), ("min", "max")] {
Expand Down
Loading
Loading