Skip to content

Commit 042394b

Browse files
committed
implement append_to_builder for Patched
We call append_to_builder on the inner first, then just do a single pass and overwrite what it just wrote. Signed-off-by: Andrew Duffy <andrew@a10y.dev>
1 parent 8ebe6c6 commit 042394b

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

vortex-array/src/arrays/patched/vtable/mod.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::hash::Hasher;
1010
use std::sync::Arc;
1111

1212
use vortex_buffer::Buffer;
13+
use vortex_error::VortexExpect;
1314
use vortex_error::VortexResult;
1415
use vortex_error::vortex_bail;
1516
use vortex_error::vortex_ensure;
@@ -34,6 +35,8 @@ use crate::arrays::patched::patch_lanes;
3435
use crate::arrays::patched::vtable::kernels::PARENT_KERNELS;
3536
use crate::arrays::primitive::PrimitiveArrayParts;
3637
use crate::buffer::BufferHandle;
38+
use crate::builders::ArrayBuilder;
39+
use crate::builders::PrimitiveBuilder;
3740
use crate::dtype::DType;
3841
use crate::dtype::NativePType;
3942
use crate::match_each_native_ptype;
@@ -169,6 +172,56 @@ impl VTable for Patched {
169172
Ok(ProstMetadata(inner))
170173
}
171174

175+
fn append_to_builder(
176+
array: &Self::Array,
177+
builder: &mut dyn ArrayBuilder,
178+
ctx: &mut ExecutionCtx,
179+
) -> VortexResult<()> {
180+
let dtype = array.dtype();
181+
182+
if !dtype.is_primitive() {
183+
// Default pathway: canonicalize and propagate.
184+
let canonical = array.to_array().execute::<Canonical>(ctx)?.into_array();
185+
builder.extend_from_array(&canonical);
186+
return Ok(());
187+
}
188+
189+
let ptype = dtype.as_ptype();
190+
191+
let len = array.len();
192+
array.inner.append_to_builder(builder, ctx)?;
193+
194+
let offset = array.offset;
195+
let lane_offsets: Buffer<u32> =
196+
Buffer::from_byte_buffer(array.lane_offsets.clone().unwrap_host());
197+
let indices: Buffer<u16> = Buffer::from_byte_buffer(array.indices.clone().unwrap_host());
198+
let values = array.values.clone().execute::<PrimitiveArray>(ctx)?;
199+
200+
match_each_native_ptype!(ptype, |V| {
201+
let typed_builder = builder
202+
.as_any_mut()
203+
.downcast_mut::<PrimitiveBuilder<V>>()
204+
.vortex_expect("correctly typed builder");
205+
206+
// Overwrite the last `len` elements of the builder. These would have been
207+
// populated by the inner.append_to_builder() call above.
208+
let mut output = typed_builder.values_mut();
209+
210+
apply_patches_primitive::<V>(
211+
&mut output[(values.len() - len)..],
212+
offset,
213+
len,
214+
array.n_chunks,
215+
array.n_lanes,
216+
&lane_offsets,
217+
&indices,
218+
values.as_slice::<V>(),
219+
);
220+
});
221+
222+
Ok(())
223+
}
224+
172225
fn build(
173226
dtype: &DType,
174227
len: usize,

vortex-array/src/builders/primitive.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ impl<T: NativePType> PrimitiveBuilder<T> {
6262
self.values.as_ref()
6363
}
6464

65+
/// Returns the raw primitive values in this builder as a mutable slice.
66+
pub fn values_mut(&mut self) -> &[T] {
67+
self.values.as_mut()
68+
}
69+
6570
/// Create a new handle to the next `len` uninitialized values in the builder.
6671
///
6772
/// All reads/writes through the handle to the values buffer or the validity buffer will operate

0 commit comments

Comments
 (0)