Skip to content

Commit 8c4cc7b

Browse files
committed
Fix SequenceVTable
Signed-off-by: blaginin <[email protected]>
1 parent 5405737 commit 8c4cc7b

File tree

1 file changed

+30
-1
lines changed
  • encodings/sequence/src/compute

1 file changed

+30
-1
lines changed

encodings/sequence/src/compute/take.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use vortex_dtype::{
1111
DType, IntegerPType, NativePType, Nullability, match_each_integer_ptype,
1212
match_each_native_ptype,
1313
};
14-
use vortex_error::{VortexExpect, VortexResult};
14+
use vortex_error::{VortexExpect, VortexResult, vortex_bail};
1515
use vortex_mask::{AllOr, Mask};
1616
use vortex_scalar::Scalar;
1717

@@ -25,6 +25,8 @@ impl TakeKernel for SequenceVTable {
2525

2626
Ok(match_each_integer_ptype!(indices.ptype(), |T| {
2727
let indices = indices.as_slice::<T>();
28+
check_bounds(indices, array.len())?;
29+
2830
match_each_native_ptype!(array.ptype(), |S| {
2931
let mul = array.multiplier().cast::<S>();
3032
let base = array.base().cast::<S>();
@@ -34,6 +36,17 @@ impl TakeKernel for SequenceVTable {
3436
}
3537
}
3638

39+
fn check_bounds<T: IntegerPType>(indices: &[T], len: usize) -> VortexResult<()> {
40+
for &i in indices {
41+
let i = i.as_();
42+
if i >= len {
43+
vortex_bail!(OutOfBounds: i, 0, len);
44+
}
45+
}
46+
47+
Ok(())
48+
}
49+
3750
fn take<T: IntegerPType, S: NativePType>(
3851
mul: S,
3952
base: S,
@@ -76,6 +89,7 @@ register_kernel!(TakeKernelAdapter(SequenceVTable).lift());
7689
#[cfg(test)]
7790
mod test {
7891
use rstest::rstest;
92+
use vortex_array::compute::take;
7993
use vortex_dtype::Nullability;
8094

8195
use crate::SequenceArray;
@@ -133,4 +147,19 @@ mod test {
133147
use vortex_array::compute::conformance::take::test_take_conformance;
134148
test_take_conformance(sequence.as_ref());
135149
}
150+
151+
#[test]
152+
fn test_bounds_check() {
153+
let array = SequenceArray::typed_new(0i32, 1i32, Nullability::NonNullable, 10).unwrap();
154+
let indices = vortex_array::arrays::PrimitiveArray::from_iter([0i32, 20]);
155+
let result = take(array.as_ref(), indices.as_ref());
156+
assert!(result.is_err());
157+
assert!(
158+
result
159+
.err()
160+
.unwrap()
161+
.to_string()
162+
.contains("out of bounds from")
163+
);
164+
}
136165
}

0 commit comments

Comments
 (0)