Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 5 additions & 20 deletions vortex-array/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,42 +454,27 @@ impl<V: VTable> Array for ArrayAdapter<V> {
return Ok(self.to_array());
}

assert!(
vortex_ensure!(
start <= self.len(),
"OutOfBounds: start {start} > length {}",
self.len()
);
assert!(
vortex_ensure!(
stop <= self.len(),
"OutOfBounds: stop {stop} > length {}",
self.len()
);

assert!(start <= stop, "start ({start}) must be <= stop ({stop})");
vortex_ensure!(start <= stop, "start ({start}) must be <= stop ({stop})");

if start == stop {
return Ok(Canonical::empty(self.dtype()).into_array());
}

let sliced = V::slice(&self.0, range.clone())?
.unwrap_or_else(|| SliceArray::new(self.to_array(), range).to_array())
let sliced = SliceArray::try_new(self.to_array(), range)?
.into_array()
.optimize()?;

assert_eq!(
sliced.len(),
stop - start,
"Slice length mismatch {}",
self.encoding_id()
);

// Slightly more expensive, so only do this in debug builds.
debug_assert_eq!(
sliced.dtype(),
self.dtype(),
"Slice dtype mismatch {}",
self.encoding_id()
);

// Propagate some stats from the original array to the sliced array.
if !sliced.is::<ConstantVTable>() {
self.statistics().with_iter(|iter| {
Expand Down
7 changes: 0 additions & 7 deletions vortex-array/src/arrays/shared/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::hash::Hash;
use std::ops::Range;

use vortex_dtype::DType;
use vortex_error::VortexExpect;
Expand All @@ -15,7 +14,6 @@ use crate::ArrayRef;
use crate::Canonical;
use crate::EmptyMetadata;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::Precision;
use crate::arrays::shared::SharedArray;
use crate::hash::ArrayEq;
Expand Down Expand Up @@ -94,11 +92,6 @@ impl VTable for SharedVTable {
fn canonicalize(array: &Self::Array, ctx: &mut ExecutionCtx) -> VortexResult<Canonical> {
array.canonicalize(ctx)
}

fn slice(array: &Self::Array, range: Range<usize>) -> VortexResult<Option<ArrayRef>> {
let sliced = array.current_array_ref().slice(range)?;
Ok(Some(SharedArray::new(sliced).into_array()))
}
}

impl BaseArrayVTable<SharedVTable> for SharedVTable {
Expand Down
12 changes: 9 additions & 3 deletions vortex-array/src/arrays/slice/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use std::ops::Range;

use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_panic;

use crate::ArrayRef;
Expand All @@ -21,19 +23,23 @@ pub struct SliceArrayParts {
}

impl SliceArray {
pub fn new(child: ArrayRef, range: Range<usize>) -> Self {
pub fn try_new(child: ArrayRef, range: Range<usize>) -> VortexResult<Self> {
if range.end > child.len() {
vortex_panic!(
"SliceArray range out of bounds: range {:?} exceeds child array length {}",
range,
child.len()
);
}
Self {
Ok(Self {
child,
range,
stats: ArrayStats::default(),
}
})
}

pub fn new(child: ArrayRef, range: Range<usize>) -> Self {
Self::try_new(child, range).vortex_expect("failed")
}

/// The range used to slice the child array.
Expand Down
18 changes: 0 additions & 18 deletions vortex-array/src/arrays/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use vortex_error::VortexResult;
pub use vtable::*;

use crate::ArrayRef;
use crate::Canonical;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::kernel::ExecuteParentKernel;
use crate::matcher::Matcher;
use crate::optimizer::rules::ArrayParentReduceRule;
Expand Down Expand Up @@ -54,16 +52,6 @@ pub trait SliceKernel: VTable {
) -> VortexResult<Option<ArrayRef>>;
}

pub fn precondition<V: VTable>(array: &V::Array, range: &Range<usize>) -> Option<ArrayRef> {
if range.start == 0 && range.end == array.len() {
return Some(array.to_array());
};
if range.start == range.end {
return Some(Canonical::empty(array.dtype()).into_array());
}
None
}

#[derive(Default, Debug)]
pub struct SliceReduceAdaptor<V>(pub V);

Expand All @@ -80,9 +68,6 @@ where
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
assert_eq!(child_idx, 0);
if let Some(result) = precondition::<V>(array, &parent.range) {
return Ok(Some(result));
}
<V as SliceReduce>::slice(array, parent.range.clone())
}
}
Expand All @@ -104,9 +89,6 @@ where
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
assert_eq!(child_idx, 0);
if let Some(result) = precondition::<V>(array, &parent.range) {
return Ok(Some(result));
}
<V as SliceKernel>::slice(array, parent.range.clone(), ctx)
}
}
30 changes: 0 additions & 30 deletions vortex-array/src/vtable/dyn_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::marker::PhantomData;
use std::ops::Range;

use arcref::ArcRef;
use vortex_dtype::DType;
Expand Down Expand Up @@ -68,8 +67,6 @@ pub trait DynVTable: 'static + private::Sealed + Send + Sync + Debug {
child_idx: usize,
ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>>;

fn slice(&self, array: &ArrayRef, range: Range<usize>) -> VortexResult<Option<ArrayRef>>;
}

/// Adapter struct used to lift the [`VTable`] trait into an object-safe [`DynVTable`]
Expand Down Expand Up @@ -187,33 +184,6 @@ impl<V: VTable> DynVTable for ArrayVTableAdapter<V> {

Ok(Some(result))
}

fn slice(&self, array: &ArrayRef, range: Range<usize>) -> VortexResult<Option<ArrayRef>> {
vortex_ensure!(
range.end <= array.len(),
"slice range {}..{} out of bounds for array of length {}",
range.start,
range.end,
array.len()
);

let Some(sliced) = V::slice(downcast::<V>(array), range.clone())? else {
return Ok(None);
};
vortex_ensure!(
sliced.len() == range.len(),
"Sliced array length mismatch: expected {}, got {}",
range.len(),
sliced.len()
);
vortex_ensure!(
sliced.dtype() == array.dtype(),
"Sliced array dtype mismatch: expected {}, got {}",
array.dtype(),
sliced.dtype()
);
Ok(Some(sliced))
}
}

fn downcast<V: VTable>(array: &ArrayRef) -> &V::Array {
Expand Down
16 changes: 0 additions & 16 deletions vortex-array/src/vtable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod visitor;

use std::fmt::Debug;
use std::ops::Deref;
use std::ops::Range;

pub use array::*;
pub use compute::*;
Expand Down Expand Up @@ -186,21 +185,6 @@ pub trait VTable: 'static + Sized + Send + Sync + Debug {
_ = (array, parent, child_idx);
Ok(None)
}

/// Perform a constant-time slice of the array.
///
/// If an encoding cannot perform this slice in constant time, it should instead return Ok(None).
///
/// This function returns [`ArrayRef`] since some encodings can return a simpler array for
/// some slices, for example a [`crate::arrays::ChunkedArray`] may slice into a single chunk.
///
/// ## Preconditions
///
/// Bounds-checking has already been performed by the time this function is called.
fn slice(array: &Self::Array, range: Range<usize>) -> VortexResult<Option<ArrayRef>> {
_ = (array, range);
Ok(None)
}
}

/// Placeholder type used to indicate when a particular vtable is not supported by the encoding.
Expand Down
Loading