Skip to content

Commit 257d235

Browse files
author
Nicolas Trinquier
committed
Add support for null values in limit and filter
1 parent f0578f6 commit 257d235

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

rust/arrow/src/compute/array_ops.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ macro_rules! filter_array {
216216
let mut builder = $array_type::builder(b.len());
217217
for i in 0..b.len() {
218218
if $filter.value(i) {
219-
builder.append_value(b.value(i))?;
219+
if b.is_null(i) {
220+
builder.append_null()?;
221+
} else {
222+
builder.append_value(b.value(i))?;
223+
}
220224
}
221225
}
222226
Ok(Arc::new(builder.finish()))
@@ -258,7 +262,11 @@ macro_rules! limit_array {
258262
let b = $array.as_any().downcast_ref::<$array_type>().unwrap();
259263
let mut builder = $array_type::builder($num_elements);
260264
for i in 0..$num_elements {
261-
builder.append_value(b.value(i))?;
265+
if b.is_null(i) {
266+
builder.append_null()?;
267+
} else {
268+
builder.append_value(b.value(i))?;
269+
}
262270
}
263271
Ok(Arc::new(builder.finish()))
264272
}};
@@ -474,6 +482,16 @@ mod tests {
474482
assert_eq!("world", d.get_string(1));
475483
}
476484

485+
#[test]
486+
fn test_filter_array_with_null() {
487+
let a = Int32Array::from(vec![Some(5), None]);
488+
let b = BooleanArray::from(vec![false, true]);
489+
let c = filter(&a, &b).unwrap();
490+
let d = c.as_ref().as_any().downcast_ref::<Int32Array>().unwrap();
491+
assert_eq!(1, d.len());
492+
assert_eq!(true, d.is_null(0));
493+
}
494+
477495
#[test]
478496
fn test_limit_array() {
479497
let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
@@ -495,6 +513,15 @@ mod tests {
495513
assert_eq!(" ", c.get_string(1));
496514
}
497515

516+
#[test]
517+
fn test_limit_array_with_null() {
518+
let a = Int32Array::from(vec![None, Some(5)]);
519+
let b = limit(&a, 1).unwrap();
520+
let c = b.as_ref().as_any().downcast_ref::<Int32Array>().unwrap();
521+
assert_eq!(1, c.len());
522+
assert_eq!(true, c.is_null(0));
523+
}
524+
498525
#[test]
499526
fn test_limit_array_with_limit_too_large() {
500527
let a = Int32Array::from(vec![5, 6, 7, 8, 9]);

0 commit comments

Comments
 (0)