Skip to main content

grafeo_core/execution/
factorized_chunk.rs

1//! FactorizedChunk - multi-level factorized data representation.
2//!
3//! A `FactorizedChunk` organizes columns into levels, where each level can have
4//! different factorization (multiplicity). This avoids materializing the full
5//! Cartesian product during multi-hop graph traversals.
6//!
7//! # Example
8//!
9//! For a 2-hop query `MATCH (a)-[e1]->(b)-[e2]->(c)`:
10//!
11//! ```text
12//! Level 0 (flat):   [a1, a2]           (2 source nodes)
13//! Level 1 (unflat): [b1, b2, b3, b4]   (4 first-hop neighbors)
14//!                   offsets: [0, 2, 4]  (a1 has 2 neighbors, a2 has 2)
15//! Level 2 (unflat): [c1, c2, ..., c8]  (8 second-hop neighbors)
16//!                   offsets: [0, 2, 4, 6, 8]
17//!
18//! Logical rows = 2 * 2 * 2 = 8, but physical storage = 2 + 4 + 8 = 14 values
19//! vs flat storage = 8 * 5 columns = 40 values
20//! ```
21
22use std::sync::Arc;
23
24use super::chunk::DataChunk;
25use super::chunk_state::ChunkState;
26use super::factorized_vector::FactorizedVector;
27use super::vector::ValueVector;
28
29/// A chunk that supports factorized representation across multiple levels.
30///
31/// Columns are organized in groups by their factorization level:
32/// - Level 0 (flat): Base columns, one value per logical row
33/// - Level 1 (unflat): First expansion, grouped by level 0
34/// - Level 2 (unflat): Second expansion, grouped by level 1
35/// - And so on...
36///
37/// # State Management
38///
39/// The chunk maintains a [`ChunkState`] that provides:
40/// - Cached multiplicities for O(1) aggregate access
41/// - Selection vector support for lazy filtering
42/// - Generation tracking for cache invalidation
43#[derive(Debug, Clone)]
44pub struct FactorizedChunk {
45    /// Column groups organized by factorization level.
46    levels: Vec<FactorizationLevel>,
47    /// Total logical row count (product of all multiplicities).
48    logical_row_count: usize,
49    /// Unified state tracking (caching, selection, etc.).
50    state: ChunkState,
51}
52
53/// A factorization level containing columns at the same nesting depth.
54#[derive(Debug, Clone)]
55pub struct FactorizationLevel {
56    /// Columns at this level.
57    columns: Vec<FactorizedVector>,
58    /// Column names or identifiers (for schema mapping).
59    column_names: Vec<String>,
60    /// Number of groups at this level.
61    group_count: usize,
62    /// Multiplicities for each group (how many children per parent).
63    /// For level 0, this is vec![1; group_count].
64    /// For level N, multiplicities[i] = number of values for parent i.
65    multiplicities: Vec<usize>,
66}
67
68impl FactorizationLevel {
69    /// Creates a new flat level (level 0) from columns.
70    #[must_use]
71    pub fn flat(columns: Vec<FactorizedVector>, column_names: Vec<String>) -> Self {
72        let group_count = columns.first().map_or(0, FactorizedVector::physical_len);
73        let multiplicities = vec![1; group_count];
74        Self {
75            columns,
76            column_names,
77            group_count,
78            multiplicities,
79        }
80    }
81
82    /// Creates a new unflat level with the given multiplicities.
83    ///
84    /// Note: `multiplicities[i]` is the number of values for parent i.
85    /// The total number of values (group_count) is the sum of all multiplicities.
86    #[must_use]
87    pub fn unflat(
88        columns: Vec<FactorizedVector>,
89        column_names: Vec<String>,
90        multiplicities: Vec<usize>,
91    ) -> Self {
92        // group_count is the total number of values at this level (sum of multiplicities)
93        let group_count = multiplicities.iter().sum();
94        Self {
95            columns,
96            column_names,
97            group_count,
98            multiplicities,
99        }
100    }
101
102    /// Returns the number of columns at this level.
103    #[must_use]
104    pub fn column_count(&self) -> usize {
105        self.columns.len()
106    }
107
108    /// Returns the number of groups at this level.
109    #[must_use]
110    pub fn group_count(&self) -> usize {
111        self.group_count
112    }
113
114    /// Returns the total physical value count across all columns.
115    #[must_use]
116    pub fn physical_value_count(&self) -> usize {
117        self.columns
118            .iter()
119            .map(FactorizedVector::physical_len)
120            .sum()
121    }
122
123    /// Returns the multiplicities for this level.
124    #[must_use]
125    pub fn multiplicities(&self) -> &[usize] {
126        &self.multiplicities
127    }
128
129    /// Returns a column by index.
130    #[must_use]
131    pub fn column(&self, index: usize) -> Option<&FactorizedVector> {
132        self.columns.get(index)
133    }
134
135    /// Returns a mutable column by index.
136    pub fn column_mut(&mut self, index: usize) -> Option<&mut FactorizedVector> {
137        self.columns.get_mut(index)
138    }
139
140    /// Returns the column names.
141    #[must_use]
142    pub fn column_names(&self) -> &[String] {
143        &self.column_names
144    }
145}
146
147impl FactorizedChunk {
148    /// Creates an empty factorized chunk.
149    #[must_use]
150    pub fn empty() -> Self {
151        Self {
152            levels: Vec::new(),
153            logical_row_count: 0,
154            state: ChunkState::flat(0),
155        }
156    }
157
158    /// Creates a factorized chunk from a flat `DataChunk`.
159    ///
160    /// The resulting chunk has a single level (level 0) with all columns flat.
161    #[must_use]
162    pub fn from_flat(chunk: &DataChunk, column_names: Vec<String>) -> Self {
163        let columns: Vec<FactorizedVector> = chunk
164            .columns()
165            .iter()
166            .map(|c| FactorizedVector::flat(c.clone()))
167            .collect();
168
169        let row_count = chunk.row_count();
170        let level = FactorizationLevel::flat(columns, column_names);
171
172        Self {
173            levels: vec![level],
174            logical_row_count: row_count,
175            state: ChunkState::unflat(1, row_count),
176        }
177    }
178
179    /// Creates a factorized chunk with a single flat level.
180    #[must_use]
181    pub fn with_flat_level(columns: Vec<ValueVector>, column_names: Vec<String>) -> Self {
182        let row_count = columns.first().map_or(0, ValueVector::len);
183        let factorized_columns: Vec<FactorizedVector> =
184            columns.into_iter().map(FactorizedVector::flat).collect();
185
186        let level = FactorizationLevel::flat(factorized_columns, column_names);
187
188        Self {
189            levels: vec![level],
190            logical_row_count: row_count,
191            state: ChunkState::unflat(1, row_count),
192        }
193    }
194
195    /// Returns the number of factorization levels.
196    #[must_use]
197    pub fn level_count(&self) -> usize {
198        self.levels.len()
199    }
200
201    /// Returns the logical row count (full Cartesian product size).
202    #[must_use]
203    pub fn logical_row_count(&self) -> usize {
204        self.logical_row_count
205    }
206
207    /// Returns the physical storage size (actual values stored).
208    #[must_use]
209    pub fn physical_size(&self) -> usize {
210        self.levels
211            .iter()
212            .map(FactorizationLevel::physical_value_count)
213            .sum()
214    }
215
216    /// Returns the chunk state.
217    #[must_use]
218    pub fn chunk_state(&self) -> &ChunkState {
219        &self.state
220    }
221
222    /// Returns mutable access to the chunk state.
223    pub fn chunk_state_mut(&mut self) -> &mut ChunkState {
224        &mut self.state
225    }
226
227    /// Returns path multiplicities, computing once and caching.
228    ///
229    /// This is the key optimization for aggregation: multiplicities are
230    /// computed once and reused for all aggregates (COUNT, SUM, AVG, etc.).
231    ///
232    /// # Example
233    ///
234    /// ```ignore
235    /// let mults = chunk.path_multiplicities_cached();
236    /// let sum = chunk.sum_deepest_with_multiplicities(0, &mults);
237    /// let avg = chunk.avg_deepest_with_multiplicities(0, &mults);
238    /// ```
239    pub fn path_multiplicities_cached(&mut self) -> Arc<[usize]> {
240        // Check if already cached
241        if let Some(cached) = self.state.cached_multiplicities() {
242            return Arc::clone(cached);
243        }
244
245        // Compute and cache
246        let mults = self.compute_path_multiplicities();
247        let arc_mults: Arc<[usize]> = mults.into();
248        self.state.set_cached_multiplicities(Arc::clone(&arc_mults));
249        arc_mults
250    }
251
252    /// Returns a level by index.
253    #[must_use]
254    pub fn level(&self, index: usize) -> Option<&FactorizationLevel> {
255        self.levels.get(index)
256    }
257
258    /// Returns a mutable level by index.
259    pub fn level_mut(&mut self, index: usize) -> Option<&mut FactorizationLevel> {
260        self.levels.get_mut(index)
261    }
262
263    /// Adds a new factorization level for expansion results.
264    ///
265    /// The new level's multiplicities determine how many values each parent
266    /// in the previous level expands to.
267    ///
268    /// # Arguments
269    ///
270    /// * `columns` - Columns at the new level
271    /// * `column_names` - Names for the new columns
272    /// * `offsets` - Offset array where `offsets[i]` is the start index for parent `i`
273    pub fn add_level(
274        &mut self,
275        columns: Vec<ValueVector>,
276        column_names: Vec<String>,
277        offsets: &[u32],
278    ) {
279        let parent_count = offsets.len().saturating_sub(1);
280
281        // Compute multiplicities from offsets
282        let multiplicities: Vec<usize> = (0..parent_count)
283            .map(|i| (offsets[i + 1] - offsets[i]) as usize)
284            .collect();
285
286        // Create unflat factorized vectors
287        let factorized_columns: Vec<FactorizedVector> = columns
288            .into_iter()
289            .map(|data| FactorizedVector::unflat(data, offsets.to_vec(), parent_count))
290            .collect();
291
292        let level =
293            FactorizationLevel::unflat(factorized_columns, column_names, multiplicities.clone());
294        self.levels.push(level);
295
296        // Update logical row count
297        // New count = previous count * sum of new multiplicities / parent_count
298        // Actually: each parent's contribution is multiplied by its multiplicity
299        if self.levels.len() == 1 {
300            // First level - logical count is just the sum of multiplicities (or total values)
301            self.logical_row_count = multiplicities.iter().sum();
302        } else {
303            // For subsequent levels, we need to compute based on parent multiplicities
304            self.recompute_logical_row_count();
305        }
306
307        // Update state (invalidates cached multiplicities)
308        self.update_state();
309    }
310
311    /// Adds a level with pre-computed factorized vectors.
312    pub fn add_factorized_level(&mut self, level: FactorizationLevel) {
313        self.levels.push(level);
314        self.recompute_logical_row_count();
315        self.update_state();
316    }
317
318    /// Updates the ChunkState to reflect current structure.
319    fn update_state(&mut self) {
320        self.state = ChunkState::unflat(self.levels.len(), self.logical_row_count);
321    }
322
323    /// Recomputes the logical row count from all levels.
324    fn recompute_logical_row_count(&mut self) {
325        if self.levels.is_empty() {
326            self.logical_row_count = 0;
327            return;
328        }
329
330        // Start with level 0 count
331        let level0_count = self.levels[0].group_count;
332        if self.levels.len() == 1 {
333            self.logical_row_count = level0_count;
334            return;
335        }
336
337        // For multi-level: compute recursively
338        // Each parent at level N-1 contributes its multiplicity to level N
339        let mut counts = vec![1usize; level0_count];
340
341        for level_idx in 1..self.levels.len() {
342            let level = &self.levels[level_idx];
343            let mut new_counts = Vec::new();
344
345            for (parent_idx, &parent_count) in counts.iter().enumerate() {
346                // This parent expands to level.multiplicities[parent_idx] children
347                if parent_idx < level.multiplicities.len() {
348                    let child_mult = level.multiplicities[parent_idx];
349                    for _ in 0..child_mult {
350                        new_counts.push(parent_count);
351                    }
352                }
353            }
354
355            counts = new_counts;
356        }
357
358        self.logical_row_count = counts.len();
359    }
360
361    /// Flattens to a regular `DataChunk` (materializes the Cartesian product).
362    ///
363    /// All levels are expanded into flat rows.
364    #[must_use]
365    pub fn flatten(&self) -> DataChunk {
366        if self.levels.is_empty() {
367            return DataChunk::empty();
368        }
369
370        // Collect all column types across all levels
371        let mut all_columns: Vec<ValueVector> = Vec::new();
372
373        // For a single level, just flatten each column
374        if self.levels.len() == 1 {
375            let level = &self.levels[0];
376            for col in &level.columns {
377                all_columns.push(col.flatten(None));
378            }
379            return DataChunk::new(all_columns);
380        }
381
382        // Multi-level: need to expand according to multiplicities
383        // Build column data by iterating through logical rows
384        let row_iter = self.logical_row_iter();
385        let total_cols: usize = self.levels.iter().map(|l| l.column_count()).sum();
386
387        // Pre-allocate output columns
388        let mut output_columns: Vec<ValueVector> = Vec::with_capacity(total_cols);
389        for level in &self.levels {
390            for col in &level.columns {
391                output_columns.push(ValueVector::with_capacity(
392                    col.data_type(),
393                    self.logical_row_count,
394                ));
395            }
396        }
397
398        // Iterate through all logical rows
399        for indices in row_iter {
400            let mut col_offset = 0;
401            for (level_idx, level) in self.levels.iter().enumerate() {
402                let level_idx_value = indices.get(level_idx).copied().unwrap_or(0);
403                for (col_idx, col) in level.columns.iter().enumerate() {
404                    if let Some(value) = col.get_physical(level_idx_value) {
405                        output_columns[col_offset + col_idx].push_value(value);
406                    }
407                }
408                col_offset += level.column_count();
409            }
410        }
411
412        DataChunk::new(output_columns)
413    }
414
415    /// Returns an iterator over logical rows without materializing.
416    ///
417    /// Each iteration yields a vector of physical indices, one per level.
418    pub fn logical_row_iter(&self) -> FactorizedRowIterator<'_> {
419        FactorizedRowIterator::new(self)
420    }
421
422    /// Gets the total number of columns across all levels.
423    #[must_use]
424    pub fn total_column_count(&self) -> usize {
425        self.levels.iter().map(|l| l.column_count()).sum()
426    }
427
428    /// Gets all column names in order across all levels.
429    #[must_use]
430    pub fn all_column_names(&self) -> Vec<String> {
431        self.levels
432            .iter()
433            .flat_map(|l| l.column_names.iter().cloned())
434            .collect()
435    }
436
437    /// Filters the deepest level in-place using a predicate on column values.
438    ///
439    /// This is the key optimization: instead of flattening and filtering all rows,
440    /// we filter only at the deepest level and update parent multiplicities.
441    ///
442    /// # Arguments
443    ///
444    /// * `column_idx` - Column index within the deepest level to filter on
445    /// * `predicate` - Function that returns true for values to keep
446    ///
447    /// # Returns
448    ///
449    /// A new FactorizedChunk with filtered values, or None if all rows are filtered out.
450    #[must_use]
451    pub fn filter_deepest<F>(&self, column_idx: usize, predicate: F) -> Option<Self>
452    where
453        F: Fn(&grafeo_common::types::Value) -> bool,
454    {
455        if self.levels.is_empty() {
456            return None;
457        }
458
459        let deepest_idx = self.levels.len() - 1;
460        let deepest = &self.levels[deepest_idx];
461
462        // Get the column to filter on
463        let filter_col = deepest.column(column_idx)?;
464
465        // Build filtered columns for the deepest level
466        let mut new_columns: Vec<ValueVector> = (0..deepest.column_count())
467            .map(|i| ValueVector::with_type(deepest.column(i).unwrap().data_type()))
468            .collect();
469
470        // Track new multiplicities for each parent
471        let parent_count = filter_col.parent_count();
472        let mut new_multiplicities: Vec<usize> = vec![0; parent_count];
473        let mut new_offsets: Vec<u32> = vec![0];
474
475        // Filter each parent's children
476        for parent_idx in 0..parent_count {
477            let (start, end) = filter_col.range_for_parent(parent_idx);
478
479            for phys_idx in start..end {
480                // Check if this value passes the filter
481                if let Some(value) = filter_col.get_physical(phys_idx) {
482                    if predicate(&value) {
483                        // Copy all columns for this row
484                        for col_idx in 0..deepest.column_count() {
485                            if let Some(col) = deepest.column(col_idx) {
486                                if let Some(v) = col.get_physical(phys_idx) {
487                                    new_columns[col_idx].push_value(v);
488                                }
489                            }
490                        }
491                        new_multiplicities[parent_idx] += 1;
492                    }
493                }
494            }
495
496            new_offsets.push(new_columns[0].len() as u32);
497        }
498
499        // Check if we have any rows left
500        let total_remaining: usize = new_multiplicities.iter().sum();
501        if total_remaining == 0 {
502            return Some(Self::empty());
503        }
504
505        // Build the new factorized vectors
506        let new_factorized_cols: Vec<FactorizedVector> = new_columns
507            .into_iter()
508            .map(|data| FactorizedVector::unflat(data, new_offsets.clone(), parent_count))
509            .collect();
510
511        let new_level = FactorizationLevel::unflat(
512            new_factorized_cols,
513            deepest.column_names().to_vec(),
514            new_multiplicities,
515        );
516
517        // Build the result chunk
518        let mut result = Self {
519            levels: self.levels[..deepest_idx].to_vec(),
520            logical_row_count: 0,
521            state: ChunkState::flat(0),
522        };
523        result.levels.push(new_level);
524        result.recompute_logical_row_count();
525        result.update_state();
526
527        Some(result)
528    }
529
530    /// Filters the deepest level using a multi-column predicate.
531    ///
532    /// This allows filtering based on values from multiple columns in the deepest level.
533    ///
534    /// # Arguments
535    ///
536    /// * `predicate` - Function that takes a slice of values (one per column) and returns true to keep
537    #[must_use]
538    pub fn filter_deepest_multi<F>(&self, predicate: F) -> Option<Self>
539    where
540        F: Fn(&[grafeo_common::types::Value]) -> bool,
541    {
542        if self.levels.is_empty() {
543            return None;
544        }
545
546        let deepest_idx = self.levels.len() - 1;
547        let deepest = &self.levels[deepest_idx];
548        let col_count = deepest.column_count();
549
550        if col_count == 0 {
551            return None;
552        }
553
554        let first_col = deepest.column(0)?;
555        let parent_count = first_col.parent_count();
556
557        // Build filtered columns
558        let mut new_columns: Vec<ValueVector> = (0..col_count)
559            .map(|i| ValueVector::with_type(deepest.column(i).unwrap().data_type()))
560            .collect();
561
562        let mut new_multiplicities: Vec<usize> = vec![0; parent_count];
563        let mut new_offsets: Vec<u32> = vec![0];
564        let mut row_values: Vec<grafeo_common::types::Value> = Vec::with_capacity(col_count);
565
566        for parent_idx in 0..parent_count {
567            let (start, end) = first_col.range_for_parent(parent_idx);
568
569            for phys_idx in start..end {
570                // Collect values from all columns
571                row_values.clear();
572                for col_idx in 0..col_count {
573                    if let Some(col) = deepest.column(col_idx) {
574                        if let Some(v) = col.get_physical(phys_idx) {
575                            row_values.push(v);
576                        }
577                    }
578                }
579
580                // Apply predicate
581                if predicate(&row_values) {
582                    for (col_idx, v) in row_values.iter().enumerate() {
583                        new_columns[col_idx].push_value(v.clone());
584                    }
585                    new_multiplicities[parent_idx] += 1;
586                }
587            }
588
589            new_offsets.push(new_columns[0].len() as u32);
590        }
591
592        // Check if any rows remain
593        let total: usize = new_multiplicities.iter().sum();
594        if total == 0 {
595            return Some(Self::empty());
596        }
597
598        // Build new level
599        let new_factorized_cols: Vec<FactorizedVector> = new_columns
600            .into_iter()
601            .map(|data| FactorizedVector::unflat(data, new_offsets.clone(), parent_count))
602            .collect();
603
604        let new_level = FactorizationLevel::unflat(
605            new_factorized_cols,
606            deepest.column_names().to_vec(),
607            new_multiplicities,
608        );
609
610        let mut result = Self {
611            levels: self.levels[..deepest_idx].to_vec(),
612            logical_row_count: 0,
613            state: ChunkState::flat(0),
614        };
615        result.levels.push(new_level);
616        result.recompute_logical_row_count();
617        result.update_state();
618
619        Some(result)
620    }
621
622    // ========================================================================
623    // Factorized Aggregation Methods
624    // ========================================================================
625
626    /// Computes COUNT(*) without flattening - returns the logical row count.
627    ///
628    /// This is O(n) where n is the number of physical values, instead of
629    /// O(m) where m is the number of logical rows (which can be exponentially larger).
630    ///
631    /// # Example
632    ///
633    /// For a 3-level chunk:
634    /// - Level 0: 100 sources
635    /// - Level 1: 10 neighbors each = 1,000 physical
636    /// - Level 2: 10 neighbors each = 10,000 physical
637    /// - Logical rows = 100 * 10 * 10 = 10,000
638    ///
639    /// `count_rows()` returns 10,000 by computing from multiplicities, not by
640    /// iterating through all logical rows.
641    #[must_use]
642    pub fn count_rows(&self) -> usize {
643        self.logical_row_count()
644    }
645
646    /// Computes the effective multiplicity for each value at the deepest level.
647    ///
648    /// This is how many times each value would appear in the flattened result.
649    /// For example, if a source has 3 first-hop neighbors and each has 2 second-hop
650    /// neighbors, each first-hop value has multiplicity 2 (appearing in 2 paths).
651    ///
652    /// # Returns
653    ///
654    /// A vector where `result[i]` is the multiplicity of physical value `i` at the
655    /// deepest level. The sum of all multiplicities equals `logical_row_count()`.
656    ///
657    /// # Note
658    ///
659    /// For repeated access (e.g., computing multiple aggregates), prefer using
660    /// [`path_multiplicities_cached`](Self::path_multiplicities_cached) which
661    /// caches the result and avoids O(levels) recomputation.
662    #[must_use]
663    pub fn compute_path_multiplicities(&self) -> Vec<usize> {
664        if self.levels.is_empty() {
665            return Vec::new();
666        }
667
668        // For a single level, each value has multiplicity 1
669        if self.levels.len() == 1 {
670            return vec![1; self.levels[0].group_count];
671        }
672
673        // Start with multiplicity 1 for each value at level 0
674        let mut parent_multiplicities = vec![1usize; self.levels[0].group_count];
675
676        // Propagate multiplicities through each level
677        for level_idx in 1..self.levels.len() {
678            let level = &self.levels[level_idx];
679            let mut child_multiplicities = Vec::with_capacity(level.group_count);
680
681            // For each parent, its children inherit its multiplicity
682            for (parent_idx, &parent_mult) in parent_multiplicities.iter().enumerate() {
683                let child_count = if parent_idx < level.multiplicities.len() {
684                    level.multiplicities[parent_idx]
685                } else {
686                    0
687                };
688
689                // Each child of this parent inherits the parent's multiplicity
690                for _ in 0..child_count {
691                    child_multiplicities.push(parent_mult);
692                }
693            }
694
695            parent_multiplicities = child_multiplicities;
696        }
697
698        parent_multiplicities
699    }
700
701    /// Computes SUM on a numeric column at the deepest level without flattening.
702    ///
703    /// Each value is multiplied by its effective multiplicity (how many times
704    /// it would appear in the flattened result).
705    ///
706    /// # Arguments
707    ///
708    /// * `column_idx` - Column index within the deepest level
709    ///
710    /// # Returns
711    ///
712    /// The sum as f64, or None if the column doesn't exist or contains non-numeric values.
713    #[must_use]
714    pub fn sum_deepest(&self, column_idx: usize) -> Option<f64> {
715        if self.levels.is_empty() {
716            return None;
717        }
718
719        let deepest_idx = self.levels.len() - 1;
720        let deepest = &self.levels[deepest_idx];
721        let col = deepest.column(column_idx)?;
722
723        // Compute multiplicity for each physical value
724        let multiplicities = self.compute_path_multiplicities();
725
726        let mut sum = 0.0;
727        for (phys_idx, mult) in multiplicities.iter().enumerate() {
728            if let Some(value) = col.get_physical(phys_idx) {
729                // Try to convert to f64
730                let num_value = match &value {
731                    grafeo_common::types::Value::Int64(v) => *v as f64,
732                    grafeo_common::types::Value::Float64(v) => *v,
733                    _ => continue, // Skip non-numeric values
734                };
735                sum += num_value * (*mult as f64);
736            }
737        }
738        Some(sum)
739    }
740
741    /// Computes AVG on a numeric column at the deepest level without flattening.
742    ///
743    /// This is equivalent to `sum_deepest() / count_rows()`.
744    ///
745    /// # Arguments
746    ///
747    /// * `column_idx` - Column index within the deepest level
748    ///
749    /// # Returns
750    ///
751    /// The average as f64, or None if the column doesn't exist or the chunk is empty.
752    #[must_use]
753    pub fn avg_deepest(&self, column_idx: usize) -> Option<f64> {
754        let count = self.logical_row_count();
755        if count == 0 {
756            return None;
757        }
758
759        let sum = self.sum_deepest(column_idx)?;
760        Some(sum / count as f64)
761    }
762
763    /// Computes MIN on a column at the deepest level without flattening.
764    ///
765    /// Unlike SUM/AVG, MIN doesn't need multiplicities - we just find the minimum
766    /// among all physical values.
767    ///
768    /// # Arguments
769    ///
770    /// * `column_idx` - Column index within the deepest level
771    ///
772    /// # Returns
773    ///
774    /// The minimum value, or None if the column doesn't exist or is empty.
775    #[must_use]
776    pub fn min_deepest(&self, column_idx: usize) -> Option<grafeo_common::types::Value> {
777        if self.levels.is_empty() {
778            return None;
779        }
780
781        let deepest_idx = self.levels.len() - 1;
782        let deepest = &self.levels[deepest_idx];
783        let col = deepest.column(column_idx)?;
784
785        let mut min_value: Option<grafeo_common::types::Value> = None;
786
787        for phys_idx in 0..col.physical_len() {
788            if let Some(value) = col.get_physical(phys_idx) {
789                min_value = Some(match min_value {
790                    None => value,
791                    Some(current) => {
792                        if Self::value_less_than(&value, &current) {
793                            value
794                        } else {
795                            current
796                        }
797                    }
798                });
799            }
800        }
801
802        min_value
803    }
804
805    /// Computes MAX on a column at the deepest level without flattening.
806    ///
807    /// Unlike SUM/AVG, MAX doesn't need multiplicities - we just find the maximum
808    /// among all physical values.
809    ///
810    /// # Arguments
811    ///
812    /// * `column_idx` - Column index within the deepest level
813    ///
814    /// # Returns
815    ///
816    /// The maximum value, or None if the column doesn't exist or is empty.
817    #[must_use]
818    pub fn max_deepest(&self, column_idx: usize) -> Option<grafeo_common::types::Value> {
819        if self.levels.is_empty() {
820            return None;
821        }
822
823        let deepest_idx = self.levels.len() - 1;
824        let deepest = &self.levels[deepest_idx];
825        let col = deepest.column(column_idx)?;
826
827        let mut max_value: Option<grafeo_common::types::Value> = None;
828
829        for phys_idx in 0..col.physical_len() {
830            if let Some(value) = col.get_physical(phys_idx) {
831                max_value = Some(match max_value {
832                    None => value,
833                    Some(current) => {
834                        if Self::value_less_than(&current, &value) {
835                            value
836                        } else {
837                            current
838                        }
839                    }
840                });
841            }
842        }
843
844        max_value
845    }
846
847    /// Compares two Values for ordering (a < b).
848    ///
849    /// Comparison rules:
850    /// - Null is always less than non-null
851    /// - Numeric types are compared by value
852    /// - Strings are compared lexicographically
853    /// - Other types use debug string comparison as fallback
854    fn value_less_than(a: &grafeo_common::types::Value, b: &grafeo_common::types::Value) -> bool {
855        use grafeo_common::types::Value;
856
857        match (a, b) {
858            // Null handling
859            (Value::Null, Value::Null) => false,
860            (Value::Null, _) => true,
861            (_, Value::Null) => false,
862
863            // Numeric comparisons
864            (Value::Int64(x), Value::Int64(y)) => x < y,
865            (Value::Float64(x), Value::Float64(y)) => x < y,
866            (Value::Int64(x), Value::Float64(y)) => (*x as f64) < *y,
867            (Value::Float64(x), Value::Int64(y)) => *x < (*y as f64),
868
869            // String comparison
870            (Value::String(x), Value::String(y)) => x.as_ref() < y.as_ref(),
871
872            // Bool comparison (false < true)
873            (Value::Bool(x), Value::Bool(y)) => !x && *y,
874
875            // Fallback for incompatible types - not comparable
876            // Return false to keep the current value (arbitrary but consistent)
877            _ => false,
878        }
879    }
880
881    // ========================================================================
882    // Projection and Column Operations
883    // ========================================================================
884
885    /// Projects specific columns from the factorized chunk without flattening.
886    ///
887    /// # Arguments
888    ///
889    /// * `column_specs` - List of (level_idx, column_idx, new_name) tuples
890    ///
891    /// # Returns
892    ///
893    /// A new FactorizedChunk with only the specified columns.
894    #[must_use]
895    pub fn project(&self, column_specs: &[(usize, usize, String)]) -> Self {
896        if self.levels.is_empty() || column_specs.is_empty() {
897            return Self::empty();
898        }
899
900        // Group specs by level
901        let mut level_specs: Vec<Vec<(usize, String)>> = vec![Vec::new(); self.levels.len()];
902        for (level_idx, col_idx, name) in column_specs {
903            if *level_idx < self.levels.len() {
904                level_specs[*level_idx].push((*col_idx, name.clone()));
905            }
906        }
907
908        // Build new levels with projected columns
909        let mut new_levels = Vec::new();
910
911        for (level_idx, specs) in level_specs.iter().enumerate() {
912            if specs.is_empty() {
913                continue;
914            }
915
916            let src_level = &self.levels[level_idx];
917
918            let columns: Vec<FactorizedVector> = specs
919                .iter()
920                .filter_map(|(col_idx, _)| src_level.column(*col_idx).cloned())
921                .collect();
922
923            let names: Vec<String> = specs.iter().map(|(_, name)| name.clone()).collect();
924
925            if level_idx == 0 {
926                new_levels.push(FactorizationLevel::flat(columns, names));
927            } else {
928                let mults = src_level.multiplicities().to_vec();
929                new_levels.push(FactorizationLevel::unflat(columns, names, mults));
930            }
931        }
932
933        if new_levels.is_empty() {
934            return Self::empty();
935        }
936
937        let mut result = Self {
938            levels: new_levels,
939            logical_row_count: 0,
940            state: ChunkState::flat(0),
941        };
942        result.recompute_logical_row_count();
943        result.update_state();
944        result
945    }
946}
947
948/// Iterator over logical rows in a factorized chunk.
949///
950/// Instead of materializing all rows, this iterator yields index tuples
951/// that can be used to access values at each level.
952///
953/// # Alternatives
954///
955/// For better performance, consider using the iterators from [`factorized_iter`](super::factorized_iter):
956///
957/// - [`PrecomputedIter`](super::factorized_iter::PrecomputedIter) - Pre-computes all indices
958///   for O(1) random access and better cache locality
959/// - [`StreamingIter`](super::factorized_iter::StreamingIter) - More memory-efficient
960///   streaming iteration with SmallVec stack allocation
961/// - [`RowView`](super::factorized_iter::RowView) - Zero-copy access to row values
962pub struct FactorizedRowIterator<'a> {
963    chunk: &'a FactorizedChunk,
964    /// Current physical indices at each level.
965    indices: Vec<usize>,
966    /// Maximum physical index at each level (per parent).
967    /// This is updated as we traverse.
968    exhausted: bool,
969}
970
971impl<'a> FactorizedRowIterator<'a> {
972    fn new(chunk: &'a FactorizedChunk) -> Self {
973        let indices = vec![0; chunk.level_count()];
974        let mut exhausted = chunk.levels.is_empty() || chunk.levels[0].group_count == 0;
975
976        let mut iter = Self {
977            chunk,
978            indices,
979            exhausted,
980        };
981
982        // If initial position is invalid (e.g., first parent has 0 children), advance to valid position
983        if !exhausted && !iter.has_valid_deepest_range() {
984            if !iter.advance() {
985                exhausted = true;
986            }
987            iter.exhausted = exhausted;
988        }
989
990        iter
991    }
992
993    /// Advances the indices like a mixed-radix counter.
994    fn advance(&mut self) -> bool {
995        if self.exhausted || self.chunk.levels.is_empty() {
996            return false;
997        }
998
999        // Start from the deepest level and work backwards
1000        for level_idx in (0..self.chunk.levels.len()).rev() {
1001            let level = &self.chunk.levels[level_idx];
1002
1003            // Get the parent index for this level
1004            let parent_idx = if level_idx == 0 {
1005                // Level 0 has no parent - just check bounds
1006                self.indices[0] + 1
1007            } else {
1008                // Get current parent's physical index
1009                self.indices[level_idx - 1]
1010            };
1011
1012            // Get the range of valid indices for this parent
1013            let (_start, end) = if level_idx == 0 {
1014                (0, level.group_count)
1015            } else {
1016                // For unflat levels, get range from parent
1017                if let Some(col) = level.columns.first() {
1018                    col.range_for_parent(parent_idx)
1019                } else {
1020                    (0, 0)
1021                }
1022            };
1023
1024            let current = self.indices[level_idx];
1025            if current + 1 < end {
1026                // Can advance at this level
1027                self.indices[level_idx] = current + 1;
1028                // Reset all deeper levels to their start positions
1029                for deeper_idx in (level_idx + 1)..self.chunk.levels.len() {
1030                    if let Some(deeper_col) = self.chunk.levels[deeper_idx].columns.first() {
1031                        let (deeper_start, _) =
1032                            deeper_col.range_for_parent(self.indices[deeper_idx - 1]);
1033                        self.indices[deeper_idx] = deeper_start;
1034                    }
1035                }
1036
1037                // Check if the deepest level has valid range - if any parent has 0 children,
1038                // we need to keep advancing instead of returning this invalid row
1039                if self.has_valid_deepest_range() {
1040                    return true;
1041                }
1042                // Otherwise, recursively try to advance again from the new position
1043                // This handles sparse data where many parents have 0 children
1044                return self.advance();
1045            }
1046            // Can't advance at this level - try parent level
1047        }
1048
1049        // Couldn't advance at any level - exhausted
1050        self.exhausted = true;
1051        false
1052    }
1053
1054    /// Checks if the deepest level has a valid (non-empty) range for its current parent.
1055    fn has_valid_deepest_range(&self) -> bool {
1056        if self.chunk.levels.len() <= 1 {
1057            return true; // Single level or empty - always valid
1058        }
1059
1060        let deepest_idx = self.chunk.levels.len() - 1;
1061        let parent_idx = self.indices[deepest_idx - 1];
1062
1063        if let Some(col) = self.chunk.levels[deepest_idx].columns.first() {
1064            let (start, end) = col.range_for_parent(parent_idx);
1065            start < end // Valid if range is non-empty
1066        } else {
1067            false
1068        }
1069    }
1070}
1071
1072impl Iterator for FactorizedRowIterator<'_> {
1073    type Item = Vec<usize>;
1074
1075    fn next(&mut self) -> Option<Self::Item> {
1076        if self.exhausted {
1077            return None;
1078        }
1079
1080        // Return current indices, then advance
1081        let result = self.indices.clone();
1082        self.advance();
1083        Some(result)
1084    }
1085}
1086
1087/// A chunk that can be either flat (DataChunk) or factorized (FactorizedChunk).
1088#[derive(Debug, Clone)]
1089pub enum ChunkVariant {
1090    /// A flat chunk with all rows materialized.
1091    Flat(DataChunk),
1092    /// A factorized chunk with multi-level representation.
1093    Factorized(FactorizedChunk),
1094}
1095
1096impl ChunkVariant {
1097    /// Creates a flat variant from a DataChunk.
1098    #[must_use]
1099    pub fn flat(chunk: DataChunk) -> Self {
1100        Self::Flat(chunk)
1101    }
1102
1103    /// Creates a factorized variant from a FactorizedChunk.
1104    #[must_use]
1105    pub fn factorized(chunk: FactorizedChunk) -> Self {
1106        Self::Factorized(chunk)
1107    }
1108
1109    /// Ensures the chunk is flat, flattening if necessary.
1110    #[must_use]
1111    pub fn ensure_flat(self) -> DataChunk {
1112        match self {
1113            Self::Flat(chunk) => chunk,
1114            Self::Factorized(chunk) => chunk.flatten(),
1115        }
1116    }
1117
1118    /// Returns the logical row count.
1119    #[must_use]
1120    pub fn logical_row_count(&self) -> usize {
1121        match self {
1122            Self::Flat(chunk) => chunk.row_count(),
1123            Self::Factorized(chunk) => chunk.logical_row_count(),
1124        }
1125    }
1126
1127    /// Returns true if this is a factorized chunk.
1128    #[must_use]
1129    pub fn is_factorized(&self) -> bool {
1130        matches!(self, Self::Factorized(_))
1131    }
1132
1133    /// Returns true if this is a flat chunk.
1134    #[must_use]
1135    pub fn is_flat(&self) -> bool {
1136        matches!(self, Self::Flat(_))
1137    }
1138
1139    /// Returns true if the chunk is empty.
1140    #[must_use]
1141    pub fn is_empty(&self) -> bool {
1142        self.logical_row_count() == 0
1143    }
1144}
1145
1146impl From<DataChunk> for ChunkVariant {
1147    fn from(chunk: DataChunk) -> Self {
1148        Self::Flat(chunk)
1149    }
1150}
1151
1152impl From<FactorizedChunk> for ChunkVariant {
1153    fn from(chunk: FactorizedChunk) -> Self {
1154        Self::Factorized(chunk)
1155    }
1156}
1157
1158#[cfg(test)]
1159mod tests {
1160    use grafeo_common::types::{LogicalType, NodeId, Value};
1161
1162    use super::*;
1163
1164    fn make_flat_chunk() -> DataChunk {
1165        let mut col = ValueVector::with_type(LogicalType::Int64);
1166        col.push_int64(1);
1167        col.push_int64(2);
1168        DataChunk::new(vec![col])
1169    }
1170
1171    fn create_multi_level_chunk() -> FactorizedChunk {
1172        // 2 sources, each with 2 neighbors = 4 logical rows
1173        let mut sources = ValueVector::with_type(LogicalType::Int64);
1174        sources.push_int64(10);
1175        sources.push_int64(20);
1176
1177        let mut chunk = FactorizedChunk::with_flat_level(vec![sources], vec!["src".to_string()]);
1178
1179        let mut neighbors = ValueVector::with_type(LogicalType::Int64);
1180        neighbors.push_int64(1);
1181        neighbors.push_int64(2);
1182        neighbors.push_int64(3);
1183        neighbors.push_int64(4);
1184
1185        let offsets = vec![0, 2, 4];
1186        chunk.add_level(vec![neighbors], vec!["nbr".to_string()], &offsets);
1187        chunk
1188    }
1189
1190    #[test]
1191    fn test_from_flat() {
1192        let flat = make_flat_chunk();
1193        let factorized = FactorizedChunk::from_flat(&flat, vec!["col1".to_string()]);
1194
1195        assert_eq!(factorized.level_count(), 1);
1196        assert_eq!(factorized.logical_row_count(), 2);
1197        assert_eq!(factorized.physical_size(), 2);
1198    }
1199
1200    #[test]
1201    fn test_add_level() {
1202        // Start with 2 source nodes
1203        let mut col0 = ValueVector::with_type(LogicalType::Node);
1204        col0.push_node_id(NodeId::new(100));
1205        col0.push_node_id(NodeId::new(200));
1206
1207        let mut chunk = FactorizedChunk::with_flat_level(vec![col0], vec!["source".to_string()]);
1208
1209        assert_eq!(chunk.level_count(), 1);
1210        assert_eq!(chunk.logical_row_count(), 2);
1211
1212        // Add level 1: source 0 has 3 neighbors, source 1 has 2 neighbors
1213        let mut neighbors = ValueVector::with_type(LogicalType::Node);
1214        neighbors.push_node_id(NodeId::new(10));
1215        neighbors.push_node_id(NodeId::new(11));
1216        neighbors.push_node_id(NodeId::new(12));
1217        neighbors.push_node_id(NodeId::new(20));
1218        neighbors.push_node_id(NodeId::new(21));
1219
1220        let offsets = vec![0, 3, 5]; // source 0: 0..3, source 1: 3..5
1221        chunk.add_level(vec![neighbors], vec!["neighbor".to_string()], &offsets);
1222
1223        assert_eq!(chunk.level_count(), 2);
1224        assert_eq!(chunk.logical_row_count(), 5); // 3 + 2 neighbors
1225        assert_eq!(chunk.physical_size(), 2 + 5); // 2 sources + 5 neighbors
1226    }
1227
1228    #[test]
1229    fn test_flatten_single_level() {
1230        let flat = make_flat_chunk();
1231        let factorized = FactorizedChunk::from_flat(&flat, vec!["col1".to_string()]);
1232
1233        let flattened = factorized.flatten();
1234        assert_eq!(flattened.row_count(), 2);
1235        assert_eq!(flattened.column(0).unwrap().get_int64(0), Some(1));
1236        assert_eq!(flattened.column(0).unwrap().get_int64(1), Some(2));
1237    }
1238
1239    #[test]
1240    fn test_flatten_multi_level() {
1241        // 2 sources, each with 2 neighbors = 4 logical rows
1242        let mut sources = ValueVector::with_type(LogicalType::Int64);
1243        sources.push_int64(1);
1244        sources.push_int64(2);
1245
1246        let mut chunk = FactorizedChunk::with_flat_level(vec![sources], vec!["src".to_string()]);
1247
1248        let mut neighbors = ValueVector::with_type(LogicalType::Int64);
1249        neighbors.push_int64(10);
1250        neighbors.push_int64(11);
1251        neighbors.push_int64(20);
1252        neighbors.push_int64(21);
1253
1254        let offsets = vec![0, 2, 4];
1255        chunk.add_level(vec![neighbors], vec!["nbr".to_string()], &offsets);
1256
1257        let flat = chunk.flatten();
1258        assert_eq!(flat.row_count(), 4);
1259        assert_eq!(flat.column_count(), 2);
1260
1261        // Check that sources are duplicated correctly
1262        // Row 0: (1, 10), Row 1: (1, 11), Row 2: (2, 20), Row 3: (2, 21)
1263        assert_eq!(flat.column(0).unwrap().get_int64(0), Some(1));
1264        assert_eq!(flat.column(0).unwrap().get_int64(1), Some(1));
1265        assert_eq!(flat.column(0).unwrap().get_int64(2), Some(2));
1266        assert_eq!(flat.column(0).unwrap().get_int64(3), Some(2));
1267        assert_eq!(flat.column(1).unwrap().get_int64(0), Some(10));
1268        assert_eq!(flat.column(1).unwrap().get_int64(1), Some(11));
1269        assert_eq!(flat.column(1).unwrap().get_int64(2), Some(20));
1270        assert_eq!(flat.column(1).unwrap().get_int64(3), Some(21));
1271    }
1272
1273    #[test]
1274    fn test_logical_row_iter_single_level() {
1275        let flat = make_flat_chunk();
1276        let factorized = FactorizedChunk::from_flat(&flat, vec!["col1".to_string()]);
1277
1278        let indices: Vec<_> = factorized.logical_row_iter().collect();
1279        assert_eq!(indices.len(), 2);
1280        assert_eq!(indices[0], vec![0]);
1281        assert_eq!(indices[1], vec![1]);
1282    }
1283
1284    #[test]
1285    fn test_chunk_variant() {
1286        let flat = make_flat_chunk();
1287        let variant = ChunkVariant::flat(flat.clone());
1288
1289        assert!(variant.is_flat());
1290        assert!(!variant.is_factorized());
1291        assert_eq!(variant.logical_row_count(), 2);
1292
1293        let ensured = variant.ensure_flat();
1294        assert_eq!(ensured.row_count(), 2);
1295    }
1296
1297    #[test]
1298    fn test_chunk_variant_factorized() {
1299        let chunk = create_multi_level_chunk();
1300        let variant = ChunkVariant::factorized(chunk);
1301
1302        assert!(variant.is_factorized());
1303        assert!(!variant.is_flat());
1304        assert_eq!(variant.logical_row_count(), 4);
1305
1306        let flat = variant.ensure_flat();
1307        assert_eq!(flat.row_count(), 4);
1308    }
1309
1310    #[test]
1311    fn test_chunk_variant_from() {
1312        let flat = make_flat_chunk();
1313        let variant: ChunkVariant = flat.into();
1314        assert!(variant.is_flat());
1315
1316        let factorized = create_multi_level_chunk();
1317        let variant2: ChunkVariant = factorized.into();
1318        assert!(variant2.is_factorized());
1319    }
1320
1321    #[test]
1322    fn test_chunk_variant_is_empty() {
1323        let empty_flat = DataChunk::empty();
1324        let variant = ChunkVariant::flat(empty_flat);
1325        assert!(variant.is_empty());
1326
1327        let non_empty = make_flat_chunk();
1328        let variant2 = ChunkVariant::flat(non_empty);
1329        assert!(!variant2.is_empty());
1330    }
1331
1332    #[test]
1333    fn test_empty_chunk() {
1334        let chunk = FactorizedChunk::empty();
1335        assert_eq!(chunk.level_count(), 0);
1336        assert_eq!(chunk.logical_row_count(), 0);
1337        assert_eq!(chunk.physical_size(), 0);
1338
1339        let flat = chunk.flatten();
1340        assert!(flat.is_empty());
1341    }
1342
1343    #[test]
1344    fn test_all_column_names() {
1345        let mut sources = ValueVector::with_type(LogicalType::Int64);
1346        sources.push_int64(1);
1347
1348        let mut chunk = FactorizedChunk::with_flat_level(vec![sources], vec!["source".to_string()]);
1349
1350        let mut neighbors = ValueVector::with_type(LogicalType::Int64);
1351        neighbors.push_int64(10);
1352
1353        chunk.add_level(vec![neighbors], vec!["neighbor".to_string()], &[0, 1]);
1354
1355        let names = chunk.all_column_names();
1356        assert_eq!(names, vec!["source", "neighbor"]);
1357    }
1358
1359    #[test]
1360    fn test_level_mut() {
1361        let mut chunk = create_multi_level_chunk();
1362
1363        // Access level mutably
1364        let level = chunk.level_mut(0).unwrap();
1365        assert_eq!(level.column_count(), 1);
1366
1367        // Invalid level should return None
1368        assert!(chunk.level_mut(10).is_none());
1369    }
1370
1371    #[test]
1372    fn test_factorization_level_column_mut() {
1373        let mut chunk = create_multi_level_chunk();
1374
1375        let level = chunk.level_mut(0).unwrap();
1376        let col = level.column_mut(0);
1377        assert!(col.is_some());
1378
1379        // Invalid column should return None
1380        assert!(level.column_mut(10).is_none());
1381    }
1382
1383    #[test]
1384    fn test_factorization_level_physical_value_count() {
1385        let chunk = create_multi_level_chunk();
1386
1387        let level0 = chunk.level(0).unwrap();
1388        assert_eq!(level0.physical_value_count(), 2); // 2 sources
1389
1390        let level1 = chunk.level(1).unwrap();
1391        assert_eq!(level1.physical_value_count(), 4); // 4 neighbors
1392    }
1393
1394    #[test]
1395    fn test_count_rows() {
1396        let chunk = create_multi_level_chunk();
1397        assert_eq!(chunk.count_rows(), 4);
1398
1399        let empty = FactorizedChunk::empty();
1400        assert_eq!(empty.count_rows(), 0);
1401    }
1402
1403    #[test]
1404    fn test_compute_path_multiplicities() {
1405        let chunk = create_multi_level_chunk();
1406
1407        let mults = chunk.compute_path_multiplicities();
1408        // Each value at the deepest level has multiplicity 1 since each parent has 2 children
1409        assert_eq!(mults.len(), 4);
1410        assert!(mults.iter().all(|&m| m == 1));
1411    }
1412
1413    #[test]
1414    fn test_compute_path_multiplicities_single_level() {
1415        let mut col = ValueVector::with_type(LogicalType::Int64);
1416        col.push_int64(1);
1417        col.push_int64(2);
1418        col.push_int64(3);
1419
1420        let chunk = FactorizedChunk::with_flat_level(vec![col], vec!["val".to_string()]);
1421        let mults = chunk.compute_path_multiplicities();
1422
1423        // Single level: each value has multiplicity 1
1424        assert_eq!(mults.len(), 3);
1425        assert!(mults.iter().all(|&m| m == 1));
1426    }
1427
1428    #[test]
1429    fn test_compute_path_multiplicities_empty() {
1430        let chunk = FactorizedChunk::empty();
1431        let mults = chunk.compute_path_multiplicities();
1432        assert!(mults.is_empty());
1433    }
1434
1435    #[test]
1436    fn test_path_multiplicities_cached() {
1437        let mut chunk = create_multi_level_chunk();
1438
1439        // First call computes and caches
1440        let mults1 = chunk.path_multiplicities_cached();
1441        assert_eq!(mults1.len(), 4);
1442
1443        // Second call should return cached value
1444        let mults2 = chunk.path_multiplicities_cached();
1445        assert_eq!(mults1.len(), mults2.len());
1446    }
1447
1448    #[test]
1449    fn test_sum_deepest() {
1450        let chunk = create_multi_level_chunk();
1451
1452        // Deepest level has values [1, 2, 3, 4]
1453        let sum = chunk.sum_deepest(0);
1454        assert_eq!(sum, Some(10.0)); // 1 + 2 + 3 + 4
1455    }
1456
1457    #[test]
1458    fn test_sum_deepest_empty() {
1459        let chunk = FactorizedChunk::empty();
1460        assert!(chunk.sum_deepest(0).is_none());
1461    }
1462
1463    #[test]
1464    fn test_sum_deepest_invalid_column() {
1465        let chunk = create_multi_level_chunk();
1466        assert!(chunk.sum_deepest(10).is_none());
1467    }
1468
1469    #[test]
1470    fn test_avg_deepest() {
1471        let chunk = create_multi_level_chunk();
1472
1473        // Deepest level has values [1, 2, 3, 4], avg = 2.5
1474        let avg = chunk.avg_deepest(0);
1475        assert_eq!(avg, Some(2.5));
1476    }
1477
1478    #[test]
1479    fn test_avg_deepest_empty() {
1480        let chunk = FactorizedChunk::empty();
1481        assert!(chunk.avg_deepest(0).is_none());
1482    }
1483
1484    #[test]
1485    fn test_min_deepest() {
1486        let chunk = create_multi_level_chunk();
1487
1488        let min = chunk.min_deepest(0);
1489        assert_eq!(min, Some(Value::Int64(1)));
1490    }
1491
1492    #[test]
1493    fn test_min_deepest_empty() {
1494        let chunk = FactorizedChunk::empty();
1495        assert!(chunk.min_deepest(0).is_none());
1496    }
1497
1498    #[test]
1499    fn test_min_deepest_invalid_column() {
1500        let chunk = create_multi_level_chunk();
1501        assert!(chunk.min_deepest(10).is_none());
1502    }
1503
1504    #[test]
1505    fn test_max_deepest() {
1506        let chunk = create_multi_level_chunk();
1507
1508        let max = chunk.max_deepest(0);
1509        assert_eq!(max, Some(Value::Int64(4)));
1510    }
1511
1512    #[test]
1513    fn test_max_deepest_empty() {
1514        let chunk = FactorizedChunk::empty();
1515        assert!(chunk.max_deepest(0).is_none());
1516    }
1517
1518    #[test]
1519    fn test_value_less_than() {
1520        // Null handling
1521        assert!(FactorizedChunk::value_less_than(
1522            &Value::Null,
1523            &Value::Int64(1)
1524        ));
1525        assert!(!FactorizedChunk::value_less_than(
1526            &Value::Int64(1),
1527            &Value::Null
1528        ));
1529        assert!(!FactorizedChunk::value_less_than(
1530            &Value::Null,
1531            &Value::Null
1532        ));
1533
1534        // Int64
1535        assert!(FactorizedChunk::value_less_than(
1536            &Value::Int64(1),
1537            &Value::Int64(2)
1538        ));
1539        assert!(!FactorizedChunk::value_less_than(
1540            &Value::Int64(2),
1541            &Value::Int64(1)
1542        ));
1543
1544        // Float64
1545        assert!(FactorizedChunk::value_less_than(
1546            &Value::Float64(1.5),
1547            &Value::Float64(2.5)
1548        ));
1549
1550        // Mixed Int/Float
1551        assert!(FactorizedChunk::value_less_than(
1552            &Value::Int64(1),
1553            &Value::Float64(1.5)
1554        ));
1555        assert!(FactorizedChunk::value_less_than(
1556            &Value::Float64(0.5),
1557            &Value::Int64(1)
1558        ));
1559
1560        // String
1561        assert!(FactorizedChunk::value_less_than(
1562            &Value::String("apple".into()),
1563            &Value::String("banana".into())
1564        ));
1565
1566        // Bool (false < true)
1567        assert!(FactorizedChunk::value_less_than(
1568            &Value::Bool(false),
1569            &Value::Bool(true)
1570        ));
1571        assert!(!FactorizedChunk::value_less_than(
1572            &Value::Bool(true),
1573            &Value::Bool(false)
1574        ));
1575
1576        // Incompatible types return false
1577        assert!(!FactorizedChunk::value_less_than(
1578            &Value::Int64(1),
1579            &Value::String("hello".into())
1580        ));
1581    }
1582
1583    #[test]
1584    fn test_filter_deepest() {
1585        let chunk = create_multi_level_chunk();
1586
1587        // Filter to keep only values > 2
1588        let filtered = chunk.filter_deepest(0, |v| {
1589            if let Value::Int64(n) = v {
1590                *n > 2
1591            } else {
1592                false
1593            }
1594        });
1595
1596        let filtered = filtered.unwrap();
1597        assert_eq!(filtered.logical_row_count(), 2); // Only 3 and 4 remain
1598    }
1599
1600    #[test]
1601    fn test_filter_deepest_empty() {
1602        let chunk = FactorizedChunk::empty();
1603        assert!(chunk.filter_deepest(0, |_| true).is_none());
1604    }
1605
1606    #[test]
1607    fn test_filter_deepest_all_filtered() {
1608        let chunk = create_multi_level_chunk();
1609
1610        // Filter everything out
1611        let filtered = chunk.filter_deepest(0, |_| false);
1612
1613        let filtered = filtered.unwrap();
1614        assert_eq!(filtered.logical_row_count(), 0);
1615    }
1616
1617    #[test]
1618    fn test_filter_deepest_invalid_column() {
1619        let chunk = create_multi_level_chunk();
1620        assert!(chunk.filter_deepest(10, |_| true).is_none());
1621    }
1622
1623    #[test]
1624    fn test_filter_deepest_multi() {
1625        // Create a chunk with 2 columns at the deepest level
1626        let mut sources = ValueVector::with_type(LogicalType::Int64);
1627        sources.push_int64(1);
1628
1629        let mut chunk = FactorizedChunk::with_flat_level(vec![sources], vec!["src".to_string()]);
1630
1631        let mut col1 = ValueVector::with_type(LogicalType::Int64);
1632        col1.push_int64(10);
1633        col1.push_int64(20);
1634        col1.push_int64(30);
1635
1636        let mut col2 = ValueVector::with_type(LogicalType::Int64);
1637        col2.push_int64(1);
1638        col2.push_int64(2);
1639        col2.push_int64(3);
1640
1641        let offsets = vec![0, 3];
1642        chunk.add_level(
1643            vec![col1, col2],
1644            vec!["a".to_string(), "b".to_string()],
1645            &offsets,
1646        );
1647
1648        // Filter based on both columns
1649        let filtered = chunk.filter_deepest_multi(|values| {
1650            if values.len() == 2 {
1651                if let (Value::Int64(a), Value::Int64(b)) = (&values[0], &values[1]) {
1652                    return *a + *b > 15;
1653                }
1654            }
1655            false
1656        });
1657
1658        assert!(filtered.is_some());
1659        let filtered = filtered.unwrap();
1660        assert_eq!(filtered.logical_row_count(), 2); // (20,2) and (30,3) pass
1661    }
1662
1663    #[test]
1664    fn test_filter_deepest_multi_empty() {
1665        let chunk = FactorizedChunk::empty();
1666        assert!(chunk.filter_deepest_multi(|_| true).is_none());
1667    }
1668
1669    #[test]
1670    fn test_filter_deepest_multi_no_columns() {
1671        // Create a chunk with no columns at level 1
1672        let mut sources = ValueVector::with_type(LogicalType::Int64);
1673        sources.push_int64(1);
1674
1675        let mut chunk = FactorizedChunk::with_flat_level(vec![sources], vec!["src".to_string()]);
1676
1677        // Add empty level (edge case)
1678        let empty_level = FactorizationLevel::unflat(vec![], vec![], vec![0]);
1679        chunk.add_factorized_level(empty_level);
1680
1681        assert!(chunk.filter_deepest_multi(|_| true).is_none());
1682    }
1683
1684    #[test]
1685    fn test_project() {
1686        let mut sources = ValueVector::with_type(LogicalType::Int64);
1687        sources.push_int64(1);
1688        sources.push_int64(2);
1689
1690        let mut col2 = ValueVector::with_type(LogicalType::String);
1691        col2.push_string("a");
1692        col2.push_string("b");
1693
1694        let chunk = FactorizedChunk::with_flat_level(
1695            vec![sources, col2],
1696            vec!["num".to_string(), "str".to_string()],
1697        );
1698
1699        // Project only the first column
1700        let projected = chunk.project(&[(0, 0, "projected_num".to_string())]);
1701
1702        assert_eq!(projected.total_column_count(), 1);
1703        let names = projected.all_column_names();
1704        assert_eq!(names, vec!["projected_num"]);
1705    }
1706
1707    #[test]
1708    fn test_project_empty() {
1709        let chunk = FactorizedChunk::empty();
1710        let projected = chunk.project(&[(0, 0, "col".to_string())]);
1711        assert_eq!(projected.level_count(), 0);
1712    }
1713
1714    #[test]
1715    fn test_project_empty_specs() {
1716        let chunk = create_multi_level_chunk();
1717        let projected = chunk.project(&[]);
1718        assert_eq!(projected.level_count(), 0);
1719    }
1720
1721    #[test]
1722    fn test_project_invalid_level() {
1723        let chunk = create_multi_level_chunk();
1724
1725        // Project from invalid level
1726        let projected = chunk.project(&[(10, 0, "col".to_string())]);
1727        assert_eq!(projected.level_count(), 0);
1728    }
1729
1730    #[test]
1731    fn test_project_multi_level() {
1732        let chunk = create_multi_level_chunk();
1733
1734        // Project from both levels
1735        let projected =
1736            chunk.project(&[(0, 0, "source".to_string()), (1, 0, "neighbor".to_string())]);
1737
1738        assert_eq!(projected.level_count(), 2);
1739        assert_eq!(projected.total_column_count(), 2);
1740    }
1741
1742    #[test]
1743    fn test_total_column_count() {
1744        let chunk = create_multi_level_chunk();
1745        assert_eq!(chunk.total_column_count(), 2); // 1 at level 0, 1 at level 1
1746    }
1747
1748    #[test]
1749    fn test_chunk_state_access() {
1750        let mut chunk = create_multi_level_chunk();
1751
1752        let state = chunk.chunk_state();
1753        assert!(state.is_factorized());
1754
1755        let state_mut = chunk.chunk_state_mut();
1756        state_mut.invalidate_cache();
1757    }
1758
1759    #[test]
1760    fn test_logical_row_iter_multi_level() {
1761        let chunk = create_multi_level_chunk();
1762
1763        let indices: Vec<_> = chunk.logical_row_iter().collect();
1764        assert_eq!(indices.len(), 4);
1765
1766        // Verify structure: [source_idx, neighbor_idx]
1767        assert_eq!(indices[0], vec![0, 0]);
1768        assert_eq!(indices[1], vec![0, 1]);
1769        assert_eq!(indices[2], vec![1, 2]);
1770        assert_eq!(indices[3], vec![1, 3]);
1771    }
1772
1773    #[test]
1774    fn test_sum_deepest_with_float() {
1775        let mut sources = ValueVector::with_type(LogicalType::Int64);
1776        sources.push_int64(1);
1777
1778        let mut chunk = FactorizedChunk::with_flat_level(vec![sources], vec!["src".to_string()]);
1779
1780        let mut floats = ValueVector::with_type(LogicalType::Float64);
1781        floats.push_float64(1.5);
1782        floats.push_float64(2.5);
1783        floats.push_float64(3.0);
1784
1785        chunk.add_level(vec![floats], vec!["val".to_string()], &[0, 3]);
1786
1787        let sum = chunk.sum_deepest(0);
1788        assert_eq!(sum, Some(7.0)); // 1.5 + 2.5 + 3.0
1789    }
1790
1791    #[test]
1792    fn test_min_max_with_strings() {
1793        let mut sources = ValueVector::with_type(LogicalType::Int64);
1794        sources.push_int64(1);
1795
1796        let mut chunk = FactorizedChunk::with_flat_level(vec![sources], vec!["src".to_string()]);
1797
1798        let mut strings = ValueVector::with_type(LogicalType::String);
1799        strings.push_string("banana");
1800        strings.push_string("apple");
1801        strings.push_string("cherry");
1802
1803        chunk.add_level(vec![strings], vec!["fruit".to_string()], &[0, 3]);
1804
1805        let min = chunk.min_deepest(0);
1806        assert_eq!(min, Some(Value::String("apple".into())));
1807
1808        let max = chunk.max_deepest(0);
1809        assert_eq!(max, Some(Value::String("cherry".into())));
1810    }
1811
1812    #[test]
1813    fn test_recompute_logical_row_count_empty() {
1814        let mut chunk = FactorizedChunk::empty();
1815        chunk.recompute_logical_row_count();
1816        assert_eq!(chunk.logical_row_count(), 0);
1817    }
1818
1819    #[test]
1820    fn test_factorization_level_group_count() {
1821        let chunk = create_multi_level_chunk();
1822
1823        let level0 = chunk.level(0).unwrap();
1824        assert_eq!(level0.group_count(), 2);
1825
1826        let level1 = chunk.level(1).unwrap();
1827        assert_eq!(level1.group_count(), 4);
1828    }
1829
1830    #[test]
1831    fn test_factorization_level_multiplicities() {
1832        let chunk = create_multi_level_chunk();
1833
1834        let level1 = chunk.level(1).unwrap();
1835        let mults = level1.multiplicities();
1836        assert_eq!(mults, &[2, 2]); // Each source has 2 neighbors
1837    }
1838
1839    #[test]
1840    fn test_factorization_level_column_names() {
1841        let chunk = create_multi_level_chunk();
1842
1843        let level0 = chunk.level(0).unwrap();
1844        assert_eq!(level0.column_names(), &["src"]);
1845
1846        let level1 = chunk.level(1).unwrap();
1847        assert_eq!(level1.column_names(), &["nbr"]);
1848    }
1849}