|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +#![cfg(feature = "unstable_encodings")] |
| 5 | + |
| 6 | +#[cfg(test)] |
| 7 | +mod tests { |
| 8 | + use rstest::rstest; |
| 9 | + use vortex_array::ArrayId; |
| 10 | + use vortex_array::ArrayRef; |
| 11 | + use vortex_array::Canonical; |
| 12 | + use vortex_array::ExecutionCtx; |
| 13 | + use vortex_array::IntoArray; |
| 14 | + use vortex_array::VTable; |
| 15 | + use vortex_array::VortexSessionExecute; |
| 16 | + use vortex_array::array_session; |
| 17 | + use vortex_array::arrays::PrimitiveArray; |
| 18 | + use vortex_array::assert_arrays_eq; |
| 19 | + use vortex_btrblocks::ArrayAndStats; |
| 20 | + use vortex_btrblocks::CascadingCompressor; |
| 21 | + use vortex_btrblocks::CompressorContext; |
| 22 | + use vortex_btrblocks::Scheme; |
| 23 | + use vortex_btrblocks::SchemeExt; |
| 24 | + use vortex_btrblocks::schemes::integer::DeltaScheme; |
| 25 | + use vortex_btrblocks::schemes::integer::IntRLEScheme; |
| 26 | + use vortex_compressor::scheme::CompressionEstimate; |
| 27 | + use vortex_compressor::scheme::EstimateVerdict; |
| 28 | + use vortex_error::VortexResult; |
| 29 | + use vortex_fastlanes::Delta; |
| 30 | + use vortex_fastlanes::RLE; |
| 31 | + use vortex_session::registry::CachedId; |
| 32 | + |
| 33 | + static DELTA_V2_ID: CachedId = CachedId::new("test.delta_v2"); |
| 34 | + static DELTA_V1: DeltaScheme = DeltaScheme::new(1.25); |
| 35 | + |
| 36 | + #[derive(Debug)] |
| 37 | + struct DeltaV2; |
| 38 | + |
| 39 | + impl Scheme for DeltaV2 { |
| 40 | + fn scheme_name(&self) -> &'static str { |
| 41 | + "test.delta_v2" |
| 42 | + } |
| 43 | + |
| 44 | + fn matches(&self, canonical: &Canonical) -> bool { |
| 45 | + DELTA_V1.matches(canonical) |
| 46 | + } |
| 47 | + |
| 48 | + fn produced_encodings(&self) -> Vec<ArrayId> { |
| 49 | + vec![*DELTA_V2_ID] |
| 50 | + } |
| 51 | + |
| 52 | + fn predecessor(&self) -> Option<&'static dyn Scheme> { |
| 53 | + Some(&DELTA_V1) |
| 54 | + } |
| 55 | + |
| 56 | + fn num_children(&self) -> usize { |
| 57 | + 2 |
| 58 | + } |
| 59 | + |
| 60 | + fn expected_compression_ratio( |
| 61 | + &self, |
| 62 | + _data: &ArrayAndStats, |
| 63 | + _compress_ctx: CompressorContext, |
| 64 | + _exec_ctx: &mut ExecutionCtx, |
| 65 | + ) -> CompressionEstimate { |
| 66 | + CompressionEstimate::Verdict(EstimateVerdict::Skip) |
| 67 | + } |
| 68 | + |
| 69 | + fn compress( |
| 70 | + &self, |
| 71 | + _compressor: &CascadingCompressor, |
| 72 | + data: &ArrayAndStats, |
| 73 | + _compress_ctx: CompressorContext, |
| 74 | + _exec_ctx: &mut ExecutionCtx, |
| 75 | + ) -> VortexResult<ArrayRef> { |
| 76 | + Ok(data.array().clone()) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + #[rstest] |
| 81 | + #[case::predecessor(Delta.id(), true)] |
| 82 | + #[case::replacement(*DELTA_V2_ID, false)] |
| 83 | + fn rle_respects_selected_delta_version( |
| 84 | + #[case] allowed_delta: ArrayId, |
| 85 | + #[case] expect_delta: bool, |
| 86 | + ) -> VortexResult<()> { |
| 87 | + let session = array_session(); |
| 88 | + vortex_fastlanes::initialize(&session); |
| 89 | + let compressor = CascadingCompressor::new(vec![&IntRLEScheme, &DeltaV2]) |
| 90 | + .with_allowed_serialized_ids([RLE.id(), allowed_delta].into_iter().collect()); |
| 91 | + assert!(compressor.has_scheme_family(DELTA_V1.id())); |
| 92 | + let array = PrimitiveArray::from_iter((0..65_536u32).map(|i| (i / 64) % 100)).into_array(); |
| 93 | + let mut ctx = session.create_execution_ctx(); |
| 94 | + let compressed = compressor.compress(&array, &mut ctx)?; |
| 95 | + assert_eq!(compressed.encoding_id(), RLE.id()); |
| 96 | + let has_delta = compressed |
| 97 | + .depth_first_traversal() |
| 98 | + .any(|array| array.encoding_id() == Delta.id()); |
| 99 | + assert_eq!(has_delta, expect_delta); |
| 100 | + assert_arrays_eq!(compressed, array, &mut ctx); |
| 101 | + Ok(()) |
| 102 | + } |
| 103 | +} |
0 commit comments