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
59 changes: 59 additions & 0 deletions vortex-btrblocks/src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ impl Scheme for NullDominated {

#[cfg(test)]
mod tests {

use std::iter;

use vortex_array::Array;
Expand Down Expand Up @@ -561,3 +562,61 @@ mod tests {
assert!(compressed.is::<SparseVTable>());
}
}

/// Tests to verify that each float compression scheme produces the expected encoding.
#[cfg(test)]
mod scheme_selection_tests {

use vortex_alp::ALPVTable;
use vortex_array::arrays::ConstantVTable;
use vortex_array::arrays::DictVTable;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::builders::ArrayBuilder;
use vortex_array::builders::PrimitiveBuilder;
use vortex_array::validity::Validity;
use vortex_buffer::Buffer;
use vortex_dtype::Nullability;
use vortex_sparse::SparseVTable;

use crate::Compressor;
use crate::float::FloatCompressor;

#[test]
fn test_constant_compressed() {
let values: Vec<f64> = vec![42.5; 100];
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = FloatCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<ConstantVTable>());
}

#[test]
fn test_alp_compressed() {
let values: Vec<f64> = (0..1000).map(|i| (i as f64) * 0.01).collect();
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = FloatCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<ALPVTable>());
}

#[test]
fn test_dict_compressed() {
let distinct_values = [1.1, 2.2, 3.3, 4.4, 5.5];
let values: Vec<f64> = (0..1000)
.map(|i| distinct_values[i % distinct_values.len()])
.collect();
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = FloatCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<DictVTable>());
}

#[test]
fn test_null_dominated_compressed() {
let mut builder = PrimitiveBuilder::<f64>::with_capacity(Nullability::Nullable, 100);
for i in 0..5 {
builder.append_value(i as f64);
}
builder.append_nulls(95);
let array = builder.finish_into_primitive();
let compressed = FloatCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<SparseVTable>());
}
}
116 changes: 116 additions & 0 deletions vortex-btrblocks/src/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,119 @@ mod tests {
Ok(())
}
}

/// Tests to verify that each integer compression scheme produces the expected encoding.
#[cfg(test)]
mod scheme_selection_tests {
use std::iter;

use vortex_array::arrays::ConstantVTable;
use vortex_array::arrays::DictVTable;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::validity::Validity;
use vortex_buffer::Buffer;
use vortex_fastlanes::BitPackedVTable;
use vortex_fastlanes::FoRVTable;
use vortex_fastlanes::RLEVTable;
use vortex_runend::RunEndVTable;
use vortex_sequence::SequenceVTable;
use vortex_sparse::SparseVTable;

use crate::Compressor;
use crate::integer::IntCompressor;

#[test]
fn test_constant_compressed() {
let values: Vec<i32> = iter::repeat_n(42, 100).collect();
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<ConstantVTable>());
}

#[test]
fn test_for_compressed() {
let values: Vec<i32> = (0..1000).map(|i| 1_000_000 + ((i * 37) % 100)).collect();
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<FoRVTable>());
}

#[test]
fn test_bitpacking_compressed() {
let values: Vec<u32> = (0..1000).map(|i| i % 16).collect();
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<BitPackedVTable>());
}

#[test]
fn test_sparse_compressed() {
let mut values: Vec<i32> = Vec::new();
for i in 0..1000 {
if i % 20 == 0 {
values.push(2_000_000 + (i * 7) % 1000);
} else {
values.push(1_000_000);
}
}
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<SparseVTable>());
}

#[test]
fn test_dict_compressed() {
use rand::RngCore;
use rand::SeedableRng;
use rand::rngs::StdRng;

let mut codes = Vec::with_capacity(65_535);
let numbers: Vec<i32> = [0, 10, 50, 100, 1000, 3000]
.into_iter()
.map(|i| 1234 * i)
.collect();

let mut rng = StdRng::seed_from_u64(1u64);
while codes.len() < 64000 {
let run_length = rng.next_u32() % 5;
let value = numbers[rng.next_u32() as usize % numbers.len()];
for _ in 0..run_length {
codes.push(value);
}
}

let array = PrimitiveArray::new(Buffer::copy_from(&codes), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<DictVTable>());
}

#[test]
fn test_runend_compressed() {
let mut values: Vec<i32> = Vec::new();
for i in 0..100 {
values.extend(iter::repeat_n(1_000_000 + i, 10));
}
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<RunEndVTable>());
}

#[test]
fn test_sequence_compressed() {
let values: Vec<i32> = (0..1000).map(|i| i * 7).collect();
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<SequenceVTable>());
}

#[test]
fn test_rle_compressed() {
let mut values: Vec<i32> = Vec::new();
for i in 0..10 {
values.extend(iter::repeat_n(i, 100));
}
let array = PrimitiveArray::new(Buffer::copy_from(&values), Validity::NonNullable);
let compressed = IntCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<RLEVTable>());
}
}
51 changes: 50 additions & 1 deletion vortex-btrblocks/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl Scheme for NullDominated {
) -> VortexResult<ArrayRef> {
assert!(allowed_cascading > 0);

// We pass None as we only run this pathway for NULL-dominated float arrays
// We pass None as we only run this pathway for NULL-dominated string arrays
let sparse_encoded = SparseArray::encode(stats.src.as_ref(), None)?;

if let Some(sparse) = sparse_encoded.as_opt::<SparseVTable>() {
Expand Down Expand Up @@ -440,6 +440,7 @@ impl Scheme for NullDominated {

#[cfg(test)]
mod tests {

use vortex_array::arrays::VarBinViewArray;
use vortex_array::builders::ArrayBuilder;
use vortex_array::builders::VarBinViewBuilder;
Expand Down Expand Up @@ -482,3 +483,51 @@ mod tests {
assert!(compressed.is::<SparseVTable>());
}
}

/// Tests to verify that each string compression scheme produces the expected encoding.
#[cfg(test)]
mod scheme_selection_tests {

use vortex_array::arrays::ConstantVTable;
use vortex_array::arrays::DictVTable;
use vortex_array::arrays::VarBinViewArray;
use vortex_dtype::DType;
use vortex_dtype::Nullability;
use vortex_fsst::FSSTVTable;

use crate::Compressor;
use crate::string::StringCompressor;

#[test]
fn test_constant_compressed() {
let strings: Vec<Option<&str>> = vec![Some("constant_value"); 100];
let array = VarBinViewArray::from_iter(strings, DType::Utf8(Nullability::NonNullable));
let compressed = StringCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<ConstantVTable>());
}

#[test]
fn test_dict_compressed() {
let distinct_values = ["apple", "banana", "cherry"];
let mut strings = Vec::with_capacity(1000);
for i in 0..1000 {
strings.push(Some(distinct_values[i % 3]));
}
let array = VarBinViewArray::from_iter(strings, DType::Utf8(Nullability::NonNullable));
let compressed = StringCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<DictVTable>());
}

#[test]
fn test_fsst_compressed() {
let mut strings = Vec::with_capacity(1000);
for i in 0..1000 {
strings.push(Some(format!(
"this_is_a_common_prefix_with_some_variation_{i}_and_a_common_suffix_pattern"
)));
}
let array = VarBinViewArray::from_iter(strings, DType::Utf8(Nullability::NonNullable));
let compressed = StringCompressor::compress(&array, false, 3, &[]).unwrap();
assert!(compressed.is::<FSSTVTable>());
}
}
Loading