use vortex_array::ArrayRef;
use vortex_array::IntoArray;
use vortex_array::ToCanonical;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::primitive::PrimitiveArrayExt;
use vortex_array::patches::Patches;
use vortex_error::VortexError;
use vortex_error::VortexResult;
pub fn compress_patches(patches: Patches) -> VortexResult<Patches> {
let indices = patches.indices().to_primitive().narrow()?.into_array();
let values = patches.values();
let values = if values
.statistics()
.compute_is_constant()
.unwrap_or_default()
{
ConstantArray::new(values.scalar_at(0)?, values.len()).into_array()
} else {
values.clone()
};
let chunk_offsets = patches
.chunk_offsets()
.as_ref()
.map(|offsets| Ok::<ArrayRef, VortexError>(offsets.to_primitive().narrow()?.into_array()))
.transpose()?;
Patches::new(
patches.array_len(),
patches.offset(),
indices,
values,
chunk_offsets,
)
}