Skip to content

Commit c9ec777

Browse files
Mavrick-Fclaude
andcommitted
fix(analysis): an integer field's sum takes whole numbers held as decimals
A dataset doesn't hold its field types to its values, so an Integer field can hold a whole number as a decimal: the sum merge rule adds it, and skips a value that isn't whole, instead of dropping every decimal. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 606b281 commit c9ec777

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎crates/analysis/src/join.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl Merge {
201201

202202
/// The matches' values (in match order) merged into one. Every rule but `first` and `last`
203203
/// skips nulls; with nothing left a numeric rule gives null, `count` zero and `join` "".
204-
/// An integer sum too large for 64 bits is null.
204+
/// An integer field's sum adds its whole numbers (one too large for 64 bits is null).
205205
pub fn apply(&self, ty: FieldType, values: &[&Value]) -> Value {
206206
let present = || values.iter().copied().filter(|v| !matches!(v, Value::Null));
207207
let numbers: Vec<f64> = present().filter_map(Value::as_f64).collect();
@@ -211,7 +211,12 @@ impl Merge {
211211
Rule::Count => Value::Int(i64::try_from(present().count()).unwrap_or(i64::MAX)),
212212
Rule::Join => Value::Text(present().map(ToString::to_string).collect::<Vec<_>>().join(&self.delimiter)),
213213
Rule::Sum if ty == FieldType::Integer => {
214-
let ints: Vec<i64> = present().filter_map(|v| if let Value::Int(i) = v { Some(*i) } else { None }).collect();
214+
// Whole numbers, however they are held (a whole decimal counts; 2.5 can't be one).
215+
let whole = |v: &Value| match v {
216+
Value::Int(i) => Some(*i),
217+
v => v.as_f64().filter(|f| f.fract() == 0.0 && f.abs() < 9.2e18).map(|f| f as i64),
218+
};
219+
let ints: Vec<i64> = present().filter_map(whole).collect();
215220
if ints.is_empty() {
216221
return Value::Null;
217222
}

‎crates/analysis/src/tools.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,6 +3146,22 @@ mod tests {
31463146
let many = serde_json::json!({"target": "districts", "join": "permits", "operation": "Join one to many", "merge": [{"field": "units", "rule": "sum"}]});
31473147
assert!(crate::run("spatialJoin", &m, &many).is_err());
31483148

3149+
// An integer field's sum takes a whole number held as a decimal, and skips one that isn't
3150+
// whole.
3151+
let mut mixed = m.0["permits"].clone();
3152+
mixed.features[1].attrs[0] = Value::Float(4.0);
3153+
mixed.features[2].attrs[0] = Value::Float(2.5);
3154+
let m_mixed = Map(HashMap::from([("districts".to_string(), m.0["districts"].clone()), ("permits".to_string(), mixed)]));
3155+
let summed = layer(
3156+
crate::run(
3157+
"spatialJoin",
3158+
&m_mixed,
3159+
&serde_json::json!({"target": "districts", "join": "permits", "merge": [{"field": "units", "rule": "sum"}]}),
3160+
)
3161+
.unwrap(),
3162+
);
3163+
assert_eq!(at(&summed, "join_units"), Value::Int(6));
3164+
31493165
// An integer sum past 64 bits is null, not wrapped.
31503166
let mut big = m.0["permits"].clone();
31513167
for f in &mut big.features {

0 commit comments

Comments
 (0)