Skip to content

Commit

Permalink
typed null array
Browse files Browse the repository at this point in the history
  • Loading branch information
scsmithr committed Jan 17, 2025
1 parent 011bcc5 commit e305258
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
50 changes: 38 additions & 12 deletions crates/rayexec_execution/src/arrays/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,26 @@ where
}

/// Create a new typed null array.
///
/// This will internally create an array buf with a capacity of 1, mark that
/// value as null, then select to the desired capacity.
pub fn try_new_typed_null(
manager: &Arc<B>,
datatype: DataType,
capacity: usize,
) -> Result<Self> {
let mut arr = Self::try_new(manager, datatype, 1)?;
arr.put_validity(Validity::new_all_invalid(1))?;
arr.select(manager, Selection::constant(capacity, 0))?;
pub fn try_new_typed_null(manager: &Arc<B>, datatype: DataType, len: usize) -> Result<Self> {
let val_buffer = array_buffer_for_datatype(manager, &datatype, 1)?;
let validity = Validity::new_all_invalid(1);

let mut buf = ArrayBuffer::with_primary_capacity::<PhysicalConstant>(manager, len)?;
buf.put_secondary_buffer(SecondaryBuffer::Constant(ConstantBuffer {
validity,
buffer: ArrayData::owned(val_buffer),
}));

Ok(arr)
Ok(Array {
datatype,
selection2: None,
validity2: None,
data2: ArrayData2::UntypedNull(UntypedNullStorage(len)),
next: Some(ArrayNextInner {
validity: Validity::new_all_valid(len),
data: ArrayData::owned(buf),
}),
})
}

// TODO: Remove
Expand Down Expand Up @@ -647,6 +654,15 @@ where

/// Set a scalar value at a given index.
pub fn set_value(&mut self, idx: usize, val: &ScalarValue) -> Result<()> {
// TODO: Handle constant, dictionary
//
// - Constant => convert to dictionary
// - Dictionary => add new value, update selection to point to value

if self.is_dictionary() || self.is_constant() {
not_implemented!("set value for dictionary/constant arrays")
}

if idx >= self.capacity() {
return Err(RayexecError::new("Index out of bounds")
.with_field("idx", idx)
Expand Down Expand Up @@ -1665,6 +1681,16 @@ mod tests {
assert_arrays_eq(&expected, &arr);
}

#[test]
fn new_typed_null_array() {
let arr =
Array::try_new_typed_null(&Arc::new(NopBufferManager), DataType::Int32, 4).unwrap();
let expected = Array::try_from_iter::<[Option<i32>; 4]>([None, None, None, None]).unwrap();
assert_arrays_eq(&expected, &arr);

assert_eq!(ScalarValue::Null, arr.get_value(2).unwrap());
}

#[test]
fn select_no_change() {
let mut arr = Array::try_from_iter(["a", "b", "c"]).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl BinaryNonNullUpdater {
Output: ?Sized,
for<'a> State: AggregateState<(&'a S1::StorageType, &'a S2::StorageType), Output>,
{
// TODO: Dictionary
// TODO: Dictionary/constant

// TODO: Length check.

Expand Down
38 changes: 38 additions & 0 deletions crates/rayexec_execution/src/arrays/format/pretty/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,12 @@ const fn elide_index<T>(v: &[T]) -> usize {

#[cfg(test)]
mod tests {
use std::sync::Arc;

use stdutil::iter::TryFromExactSizeIterator;

use super::*;
use crate::arrays::array::buffer_manager::NopBufferManager;
use crate::arrays::array::selection::Selection;
use crate::arrays::field::Field;

Expand Down Expand Up @@ -1394,4 +1397,39 @@ mod tests {
];
assert_eq_print(expected.join("\n"), table.to_string())
}

#[test]
fn batch_with_constant_rows_exceeds_max_rows() {
let schema = Schema::new(vec![
Field::new("a", DataType::Utf8, true),
Field::new("b", DataType::Int32, true),
]);

let mut batch = Batch::try_from_arrays(vec![
Array::try_new_constant(&Arc::new(NopBufferManager), &"1".into(), 6).unwrap(),
Array::try_new_constant(&Arc::new(NopBufferManager), &2.into(), 6).unwrap(),
])
.unwrap();

batch.select(Selection::slice(&[0, 0, 1, 1, 0, 0])).unwrap();

let table = pretty_format_batches(&schema, &[batch], 40, Some(5)).unwrap();

let expected = [
"┌────────┬────────┐",
"│ a │ b │",
"│ Utf8 │ Int32 │",
"├────────┼────────┤",
"│ 1 │ 2 │",
"│ 1 │ 2 │",
"│ 1 │ 2 │",
"│ … │ … │",
"│ 1 │ 2 │",
"│ 1 │ 2 │",
"├────────┴────────┤",
"│ 6 rows, 5 shown │",
"└─────────────────┘",
];
assert_eq_print(expected.join("\n"), table.to_string())
}
}

0 comments on commit e305258

Please sign in to comment.