Skip to content

Commit 99edcbf

Browse files
committed
fixes
Signed-off-by: Robert Kruszewski <github@robertk.io>
1 parent 947a966 commit 99edcbf

3 files changed

Lines changed: 50 additions & 40 deletions

File tree

encodings/runend/src/arbitrary.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use arbitrary::Arbitrary;
55
use arbitrary::Result;
66
use arbitrary::Unstructured;
7+
use arbitrary::unstructured::Int;
78
use vortex_array::ArrayRef;
89
use vortex_array::IntoArray;
910
use vortex_array::arrays::PrimitiveArray;
@@ -13,6 +14,7 @@ use vortex_array::arrays::arbitrary::ArbitraryWith;
1314
use vortex_array::dtype::DType;
1415
use vortex_array::dtype::Nullability;
1516
use vortex_array::dtype::PType;
17+
use vortex_array::dtype::UnsignedPType;
1618
use vortex_array::validity::Validity;
1719
use vortex_buffer::Buffer;
1820
use vortex_error::VortexExpect;
@@ -91,64 +93,72 @@ fn random_strictly_sorted_ends(
9193
target_len: usize,
9294
) -> Result<ArrayRef> {
9395
// Choose a random unsigned PType for ends
94-
let ends_ptype = *u.choose(&[PType::U8, PType::U16, PType::U32, PType::U64])?;
96+
let mut ends_ptypes = vec![PType::U8, PType::U16, PType::U32, PType::U64];
97+
if target_len >= u8::MAX as usize {
98+
ends_ptypes.remove(0);
99+
}
100+
if target_len >= u16::MAX as usize {
101+
ends_ptypes.remove(0);
102+
}
103+
if target_len >= u32::MAX as usize {
104+
ends_ptypes.remove(0);
105+
}
106+
let ends_ptype = *u.choose(&ends_ptypes)?;
107+
108+
match ends_ptype {
109+
PType::U8 => random_strictly_sorted(
110+
u,
111+
num_runs,
112+
u8::try_from(target_len).vortex_expect("must fit in u8"),
113+
),
114+
PType::U16 => random_strictly_sorted(
115+
u,
116+
num_runs,
117+
u16::try_from(target_len).vortex_expect("must fit in u16"),
118+
),
119+
PType::U32 => random_strictly_sorted(
120+
u,
121+
num_runs,
122+
u32::try_from(target_len).vortex_expect("must fit in u32"),
123+
),
124+
PType::U64 => random_strictly_sorted(
125+
u,
126+
num_runs,
127+
u64::try_from(target_len).vortex_expect("must fit in u64"),
128+
),
129+
_ => unreachable!("Only unsigned integer types are valid for ends"),
130+
}
131+
}
95132

133+
fn random_strictly_sorted<T: UnsignedPType + Int>(
134+
u: &mut Unstructured,
135+
num_runs: usize,
136+
target: T,
137+
) -> Result<ArrayRef> {
96138
// Generate strictly increasing values
97139
// Start from 0, increment by at least 1 each time
98-
let mut ends: Vec<u64> = Vec::with_capacity(num_runs);
99-
let mut current: u64 = 0;
140+
let mut ends: Vec<T> = Vec::with_capacity(num_runs);
141+
let mut current = T::zero();
100142

101143
for i in 0..num_runs {
102144
// Each run must have at least length 1, so increment by at least 1
103145
let increment = match i == num_runs - 1 {
104146
true => {
105147
// Last element should reach target_len
106-
let target = target_len as u64;
107148
if target > current {
108149
target - current
109150
} else {
110-
1
151+
T::one()
111152
}
112153
}
113154
false => {
114155
// Random increment between 1 and 10
115-
u.int_in_range(1..=10)?
156+
u.int_in_range(T::one()..=T::from(10).vortex_expect("10 will fit in all T"))?
116157
}
117158
};
118159
current += increment;
119160
ends.push(current);
120161
}
121162

122-
// Convert to the chosen PType
123-
// The values are bounded: max is num_runs (20) * max_increment (10) = 200
124-
// This fits in all unsigned types
125-
let ends_array = match ends_ptype {
126-
PType::U8 => {
127-
let ends_typed: Vec<u8> = ends
128-
.iter()
129-
.map(|&e| u8::try_from(e).vortex_expect("end value fits in u8"))
130-
.collect();
131-
PrimitiveArray::new(Buffer::copy_from(ends_typed), Validity::NonNullable).into_array()
132-
}
133-
PType::U16 => {
134-
let ends_typed: Vec<u16> = ends
135-
.iter()
136-
.map(|&e| u16::try_from(e).vortex_expect("end value fits in u16"))
137-
.collect();
138-
PrimitiveArray::new(Buffer::copy_from(ends_typed), Validity::NonNullable).into_array()
139-
}
140-
PType::U32 => {
141-
let ends_typed: Vec<u32> = ends
142-
.iter()
143-
.map(|&e| u32::try_from(e).vortex_expect("end value fits in u32"))
144-
.collect();
145-
PrimitiveArray::new(Buffer::copy_from(ends_typed), Validity::NonNullable).into_array()
146-
}
147-
PType::U64 => {
148-
PrimitiveArray::new(Buffer::copy_from(ends), Validity::NonNullable).into_array()
149-
}
150-
_ => unreachable!("Only unsigned integer types are valid for ends"),
151-
};
152-
153-
Ok(ends_array)
163+
Ok(PrimitiveArray::new(Buffer::copy_from(ends), Validity::NonNullable).into_array())
154164
}

vortex-array/src/builders/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<O: IntegerPType> ListBuilder<O> {
176176
/// [`ListViewBuilder`](crate::builders::ListViewBuilder)), so it is never produced by
177177
/// [`builder_with_capacity`]. List encodings still dispatch into it via `match_each_list_builder`
178178
/// when a caller supplies one directly.
179-
pub(crate) fn append_list_array(
179+
pub fn append_list_array(
180180
&mut self,
181181
array: &ArrayRef,
182182
ctx: &mut ExecutionCtx,

vortex-array/src/builders/listview.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ impl<O: IntegerPType, S: IntegerPType> ListViewBuilder<O, S> {
228228

229229
/// Appends the values of a canonical [`ListViewArray`] to the builder, recursing into the
230230
/// elements builder.
231-
pub(crate) fn append_listview_array(
231+
pub fn append_listview_array(
232232
&mut self,
233233
array: &ListViewArray,
234234
ctx: &mut ExecutionCtx,

0 commit comments

Comments
 (0)