|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Sparse encoding for fixed-size lists with many null rows. |
| 5 | +
|
| 6 | +use vortex_array::ArrayId; |
| 7 | +use vortex_array::ArrayRef; |
| 8 | +use vortex_array::Canonical; |
| 9 | +use vortex_array::ExecutionCtx; |
| 10 | +use vortex_array::IntoArray; |
| 11 | +use vortex_array::VTable; |
| 12 | +use vortex_array::arrays::PrimitiveArray; |
| 13 | +use vortex_array::arrays::primitive::PrimitiveArrayExt; |
| 14 | +use vortex_array::scalar::Scalar; |
| 15 | +use vortex_array::validity::Validity; |
| 16 | +use vortex_buffer::Buffer; |
| 17 | +use vortex_compressor::scheme::ChildSelection; |
| 18 | +use vortex_compressor::scheme::CompressionEstimate; |
| 19 | +use vortex_compressor::scheme::DescendantExclusion; |
| 20 | +use vortex_compressor::scheme::EstimateVerdict; |
| 21 | +use vortex_error::VortexResult; |
| 22 | +use vortex_mask::AllOr; |
| 23 | +use vortex_mask::Mask; |
| 24 | +use vortex_sparse::Sparse; |
| 25 | + |
| 26 | +use crate::ArrayAndStats; |
| 27 | +use crate::CascadingCompressor; |
| 28 | +use crate::CompressorContext; |
| 29 | +use crate::Scheme; |
| 30 | +use crate::SchemeExt; |
| 31 | +use crate::schemes::integer::IntDictScheme; |
| 32 | +use crate::schemes::integer::IntRLEScheme; |
| 33 | +use crate::schemes::integer::RunEndScheme; |
| 34 | +use crate::schemes::integer::SparseScheme as IntSparseScheme; |
| 35 | + |
| 36 | +/// The smallest estimated ratio of the dense list's size to the sparse one's worth encoding for. |
| 37 | +const MIN_ESTIMATED_RATIO: f64 = 1.1; |
| 38 | + |
| 39 | +/// Sparse encoding of a fixed-size list that stores only its valid rows. |
| 40 | +/// |
| 41 | +/// A fixed-size list keeps `list_size` elements for every row, null or not, so compressing its |
| 42 | +/// elements has to encode the arbitrary values under null rows too. This scheme instead stores the |
| 43 | +/// positions of the valid rows and a list of only those rows, with null as the fill value. |
| 44 | +#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 45 | +pub struct FixedSizeListSparseScheme; |
| 46 | + |
| 47 | +impl Scheme for FixedSizeListSparseScheme { |
| 48 | + fn scheme_name(&self) -> &'static str { |
| 49 | + "vortex.fixed_size_list.sparse" |
| 50 | + } |
| 51 | + |
| 52 | + fn matches(&self, canonical: &Canonical) -> bool { |
| 53 | + matches!(canonical, Canonical::FixedSizeList(_)) && canonical.dtype().is_nullable() |
| 54 | + } |
| 55 | + |
| 56 | + fn produced_encodings(&self) -> Vec<ArrayId> { |
| 57 | + vec![Sparse.id()] |
| 58 | + } |
| 59 | + |
| 60 | + /// Children: values=0, indices=1. |
| 61 | + fn num_children(&self) -> usize { |
| 62 | + 2 |
| 63 | + } |
| 64 | + |
| 65 | + /// Sparse indices (child 1) are monotonically increasing positions with all unique values. |
| 66 | + fn descendant_exclusions(&self) -> Vec<DescendantExclusion> { |
| 67 | + vec![ |
| 68 | + DescendantExclusion { |
| 69 | + excluded: IntDictScheme.id(), |
| 70 | + children: ChildSelection::One(1), |
| 71 | + }, |
| 72 | + DescendantExclusion { |
| 73 | + excluded: RunEndScheme.id(), |
| 74 | + children: ChildSelection::One(1), |
| 75 | + }, |
| 76 | + DescendantExclusion { |
| 77 | + excluded: IntRLEScheme.id(), |
| 78 | + children: ChildSelection::One(1), |
| 79 | + }, |
| 80 | + DescendantExclusion { |
| 81 | + excluded: IntSparseScheme.id(), |
| 82 | + children: ChildSelection::One(1), |
| 83 | + }, |
| 84 | + ] |
| 85 | + } |
| 86 | + |
| 87 | + /// Compares the list's size with the valid rows plus their bit-packed positions. |
| 88 | + fn expected_compression_ratio( |
| 89 | + &self, |
| 90 | + data: &ArrayAndStats, |
| 91 | + _compress_ctx: CompressorContext, |
| 92 | + exec_ctx: &mut ExecutionCtx, |
| 93 | + ) -> CompressionEstimate { |
| 94 | + let array = data.array(); |
| 95 | + let len = array.len(); |
| 96 | + let Ok(mask) = validity_mask(array, exec_ctx) else { |
| 97 | + return CompressionEstimate::Verdict(EstimateVerdict::Skip); |
| 98 | + }; |
| 99 | + let valid = mask.true_count(); |
| 100 | + if valid == 0 || valid == len { |
| 101 | + return CompressionEstimate::Verdict(EstimateVerdict::Skip); |
| 102 | + } |
| 103 | + |
| 104 | + let dense_bytes = array.nbytes() as f64; |
| 105 | + let row_bytes = dense_bytes / len as f64; |
| 106 | + let index_bytes = f64::from(usize::BITS - (len - 1).leading_zeros()) / 8.0; |
| 107 | + let sparse_bytes = valid as f64 * (row_bytes + index_bytes); |
| 108 | + |
| 109 | + let ratio = dense_bytes / sparse_bytes; |
| 110 | + if ratio < MIN_ESTIMATED_RATIO { |
| 111 | + return CompressionEstimate::Verdict(EstimateVerdict::Skip); |
| 112 | + } |
| 113 | + CompressionEstimate::Verdict(EstimateVerdict::Ratio(ratio)) |
| 114 | + } |
| 115 | + |
| 116 | + fn compress( |
| 117 | + &self, |
| 118 | + compressor: &CascadingCompressor, |
| 119 | + data: &ArrayAndStats, |
| 120 | + compress_ctx: CompressorContext, |
| 121 | + exec_ctx: &mut ExecutionCtx, |
| 122 | + ) -> VortexResult<ArrayRef> { |
| 123 | + let array = data.array(); |
| 124 | + let mask = validity_mask(array, exec_ctx)?; |
| 125 | + let AllOr::Some(valid_indices) = mask.indices() else { |
| 126 | + // The estimate skips lists without both valid and null rows. |
| 127 | + return Ok(array.clone()); |
| 128 | + }; |
| 129 | + |
| 130 | + let values = array.filter(mask.clone())?; |
| 131 | + let compressed_values = |
| 132 | + compressor.compress_child(&values, &compress_ctx, self.id(), 0, exec_ctx)?; |
| 133 | + |
| 134 | + let indices = PrimitiveArray::new( |
| 135 | + valid_indices |
| 136 | + .iter() |
| 137 | + .map(|&idx| idx as u64) |
| 138 | + .collect::<Buffer<u64>>(), |
| 139 | + Validity::NonNullable, |
| 140 | + ) |
| 141 | + .narrow(exec_ctx)?; |
| 142 | + let compressed_indices = compressor.compress_child( |
| 143 | + &indices.into_array(), |
| 144 | + &compress_ctx, |
| 145 | + self.id(), |
| 146 | + 1, |
| 147 | + exec_ctx, |
| 148 | + )?; |
| 149 | + |
| 150 | + Ok(Sparse::try_new( |
| 151 | + compressed_indices, |
| 152 | + compressed_values, |
| 153 | + array.len(), |
| 154 | + Scalar::null(array.dtype().clone()), |
| 155 | + )? |
| 156 | + .into_array()) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/// Returns which rows of `array` are valid. |
| 161 | +fn validity_mask(array: &ArrayRef, exec_ctx: &mut ExecutionCtx) -> VortexResult<Mask> { |
| 162 | + array.validity()?.execute_mask(array.len(), exec_ctx) |
| 163 | +} |
0 commit comments