Skip to content

Commit 29da540

Browse files
authored
fix: Signed bitpacked arrays handle searching for negative values (#1800)
1 parent 5a704cf commit 29da540

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

encodings/fastlanes/src/bitpacking/compute/search_sorted.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ where
110110
// in the BitPackedSearch. We need a type that impls fastlanes::BitPack, and it is a
111111
// precondition for BitPackedArray that all values must be non-negative, so promotion
112112
// is cheap and safe.
113-
let native_value: T = value
114-
.cast(&DType::from(array.ptype().to_unsigned()))?
115-
.try_into()?;
113+
let Ok(unsigned_value) = value.cast(&DType::from(array.ptype().to_unsigned())) else {
114+
// If the value can't be casted to unsigned dtype then it can't exist in the array and would be smaller than any value present
115+
return Ok(SearchResult::NotFound(0));
116+
};
117+
let native_value: T = unsigned_value.try_into()?;
116118
search_sorted_native(array, native_value, side)
117119
}
118120

@@ -330,4 +332,15 @@ mod test {
330332
]
331333
);
332334
}
335+
336+
#[test]
337+
fn test_missing_signed() {
338+
let bitpacked = BitPackedArray::encode(&buffer![1i32, 2, 3, 4, 5].into_array(), 2)
339+
.unwrap()
340+
.into_array();
341+
assert_eq!(
342+
search_sorted(&bitpacked, -4, SearchSortedSide::Left).unwrap(),
343+
SearchResult::NotFound(0)
344+
);
345+
}
333346
}

0 commit comments

Comments
 (0)