Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid dtype comparison failure in take -- upcast indices in take_strict_sorted #464

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 18 additions & 3 deletions vortex-array/src/array/chunked/compute/take.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use itertools::Itertools;
use vortex_dtype::PType;
use vortex_error::VortexResult;
use vortex_scalar::Scalar;

use crate::array::chunked::ChunkedArray;
use crate::array::primitive::PrimitiveArray;
Expand Down Expand Up @@ -88,8 +89,22 @@ fn take_strict_sorted(chunked: &ChunkedArray, indices: &Array) -> VortexResult<A
// Now we can say the slice of indices belonging to this chunk is [pos, chunk_end_pos)
let chunk_indices = slice(indices, pos, chunk_end_pos)?;

// Adjust the indices so they're relative to the chunk
let chunk_indices = subtract_scalar(&chunk_indices, &chunk_begin.into())?;
// Indices might not have a dtype big enough to fit chunk_begin after cast,
// if it does cast the scalar otherwise upcast the indices.
let chunk_indices = if chunk_begin < PType::try_from(chunk_indices.dtype())?.max_value() {
subtract_scalar(
&chunk_indices,
&Scalar::from(chunk_begin).cast(chunk_indices.dtype())?,
)?
} else {
// TODO: this is unnecessary, could instead upcast in the subtract.
Copy link
Contributor

Choose a reason for hiding this comment

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

i think it's better to assert on it here, b/c you can do subtract_scalar with negative value. let's remove the TODO?

Copy link
Member Author

Choose a reason for hiding this comment

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

will update comment

let u64_chunk_indices = try_cast(&chunk_indices, PType::U64.into())
.expect("safe to upcast since all indices are positive");

// Adjust the indices so they're relative to the chunk
subtract_scalar(&u64_chunk_indices, &chunk_begin.into())?
};

indices_by_chunk[chunk_idx] = Some(chunk_indices);

pos = chunk_end_pos;
Expand Down Expand Up @@ -124,7 +139,7 @@ mod test {
.unwrap();
assert_eq!(arr.nchunks(), 3);
assert_eq!(arr.len(), 9);
let indices = vec![0, 0, 6, 4].into_array();
let indices = vec![0u64, 0, 6, 4].into_array();

let result = &ChunkedArray::try_from(take(arr.as_array_ref(), &indices).unwrap())
.unwrap()
Expand Down
8 changes: 7 additions & 1 deletion vortex-dtype/src/ptype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ macro_rules! match_each_integer_ptype {
PType::U16 => __with__! { u16 },
PType::U32 => __with__! { u32 },
PType::U64 => __with__! { u64 },
_ => panic!("Unsupported ptype {}", $self),
PType::F16 => panic!("Unsupported ptype f16"),
PType::F32 => panic!("Unsupported ptype f32"),
PType::F64 => panic!("Unsupported ptype f64"),
Comment on lines +107 to +109
Copy link
Contributor

Choose a reason for hiding this comment

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

was this necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

to remove any string concat at compile time

Copy link
Contributor

Choose a reason for hiding this comment

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

(So max_value can be a const fn since format! isn't const)

}
})
}
Expand Down Expand Up @@ -164,6 +166,10 @@ impl PType {
self.byte_width() * 8
}

pub const fn max_value(&self) -> usize {
match_each_integer_ptype!(self, |$T| $T::MAX as usize)
}

pub fn to_signed(self) -> Self {
match self {
Self::U8 => Self::I8,
Expand Down
Loading