Skip to main content

zu_vector/
chunk.rs

1//! Data chunks and factorized chunk sets (perf/02 section 1.4).
2//!
3//! The factorized model is exactly today's: a flat prefix chunk plus
4//! unflat list-level chunks, logical multiplicity the product of the
5//! level sizes, flattening lazy. Only the cell representation changed.
6
7use crate::sel::SelVector;
8use crate::vector::ValueVector;
9
10pub struct DataChunk {
11    pub vecs: Vec<ValueVector>,
12    /// One selection shared by every vector in the chunk. None is
13    /// identity.
14    pub sel: Option<SelVector>,
15    /// Row count before selection.
16    pub count: u32,
17    /// Some(i) pins the chunk flat at row i, the vectorized form of the
18    /// old executor's cur.
19    pub cur: Option<u32>,
20}
21
22impl DataChunk {
23    pub fn new(vecs: Vec<ValueVector>, count: u32) -> Self {
24        Self {
25            vecs,
26            sel: None,
27            count,
28            cur: None,
29        }
30    }
31
32    /// Rows visible after selection and pinning.
33    pub fn active_count(&self) -> usize {
34        if self.cur.is_some() {
35            return 1;
36        }
37        match &self.sel {
38            Some(sel) => sel.len(),
39            None => self.count as usize,
40        }
41    }
42}
43
44/// A factorized tuple group: chunks[0] is the flat prefix, chunks[1..]
45/// are unflat list levels.
46pub struct ChunkSet {
47    pub chunks: Vec<DataChunk>,
48}
49
50impl ChunkSet {
51    pub fn new(chunks: Vec<DataChunk>) -> Self {
52        Self { chunks }
53    }
54
55    /// Logical row multiplicity: the product of the active counts of the
56    /// unpinned levels.
57    pub fn multiplicity(&self) -> u64 {
58        self.chunks
59            .iter()
60            .map(|c| c.active_count() as u64)
61            .product()
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::arena::MorselArena;
69    use crate::vector::{PhysType, ValueVector};
70
71    #[test]
72    fn multiplicity_is_product_of_levels() {
73        let mut arena = MorselArena::new();
74        let flat = DataChunk::new(
75            vec![ValueVector::flat_from(
76                &mut arena,
77                PhysType::Int64,
78                &[1i64, 2],
79            )],
80            2,
81        );
82        let unflat = DataChunk::new(
83            vec![ValueVector::flat_from(
84                &mut arena,
85                PhysType::Int64,
86                &[10i64, 20, 30],
87            )],
88            3,
89        );
90        let set = ChunkSet::new(vec![flat, unflat]);
91        assert_eq!(set.multiplicity(), 6);
92        let mut pinned = set;
93        pinned.chunks[1].cur = Some(1);
94        assert_eq!(pinned.multiplicity(), 2);
95    }
96}