lance_encoding/encodings/physical/
constant.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4//! Routines for compressing and decompressing constant-encoded data
5
6use crate::{
7    buffer::LanceBuffer,
8    compression::{BlockDecompressor, FixedPerValueDecompressor},
9    data::{AllNullDataBlock, ConstantDataBlock, DataBlock, FixedWidthDataBlock},
10};
11
12use lance_core::Result;
13
14/// A decompressor for constant-encoded data
15#[derive(Debug)]
16pub struct ConstantDecompressor {
17    scalar: Option<LanceBuffer>,
18}
19
20impl ConstantDecompressor {
21    pub fn new(scalar: Option<LanceBuffer>) -> Self {
22        Self { scalar }
23    }
24}
25
26impl BlockDecompressor for ConstantDecompressor {
27    fn decompress(&self, _data: LanceBuffer, num_values: u64) -> Result<DataBlock> {
28        if let Some(scalar) = self.scalar.clone() {
29            Ok(DataBlock::Constant(ConstantDataBlock {
30                data: scalar,
31                num_values,
32            }))
33        } else {
34            Ok(DataBlock::AllNull(AllNullDataBlock { num_values }))
35        }
36    }
37}
38
39impl FixedPerValueDecompressor for ConstantDecompressor {
40    fn decompress(&self, _data: FixedWidthDataBlock, num_values: u64) -> Result<DataBlock> {
41        if let Some(scalar) = self.scalar.clone() {
42            Ok(DataBlock::Constant(ConstantDataBlock {
43                data: scalar,
44                num_values,
45            }))
46        } else {
47            Ok(DataBlock::AllNull(AllNullDataBlock { num_values }))
48        }
49    }
50
51    fn bits_per_value(&self) -> u64 {
52        self.scalar
53            .as_ref()
54            .map(|s| s.len() as u64 * 8)
55            .unwrap_or(0)
56    }
57}