1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::ops::Range;

use super::{CellValue, SimpleColIter, SimpleColRange, SimpleValue};
use crate::columnar::{
    column_range::{RleRange, ValueIter, ValueRange},
    encoding::{col_error::DecodeColumnError, RleDecoder},
};

/// A group column range is one with a "num" column and zero or more "grouped" columns. The "num"
/// column contains RLE encoded u64s, each `u64` represents the number of values to read from each
/// of the grouped columns in order to produce a `CellValue::Group` for the current row.
#[derive(Debug, Clone)]
pub(crate) struct GroupRange {
    pub(crate) num: RleRange<u64>,
    pub(crate) values: Vec<GroupedColumnRange>,
}

impl GroupRange {
    pub(crate) fn new(num: RleRange<u64>, values: Vec<GroupedColumnRange>) -> Self {
        Self { num, values }
    }

    #[allow(dead_code)]
    pub(crate) fn iter<'a>(&self, data: &'a [u8]) -> GroupIter<'a> {
        GroupIter {
            num: self.num.decoder(data),
            values: self.values.iter().map(|v| v.iter(data)).collect(),
        }
    }

    pub(crate) fn range(&self) -> Range<usize> {
        let start = self.num.start();
        let end = self
            .values
            .last()
            .map(|v| v.range().end)
            .unwrap_or_else(|| self.num.end());
        start..end
    }
}

/// The type of ranges which can be the "grouped" columns in a `GroupRange`
#[derive(Debug, Clone)]
pub(crate) enum GroupedColumnRange {
    Value(ValueRange),
    Simple(SimpleColRange),
}

impl GroupedColumnRange {
    fn iter<'a>(&self, data: &'a [u8]) -> GroupedColIter<'a> {
        match self {
            Self::Value(vr) => GroupedColIter::Value(vr.iter(data)),
            Self::Simple(sc) => GroupedColIter::Simple(sc.iter(data)),
        }
    }

    pub(crate) fn range(&self) -> Range<usize> {
        match self {
            Self::Value(vr) => vr.range(),
            Self::Simple(s) => s.range(),
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct GroupIter<'a> {
    num: RleDecoder<'a, u64>,
    values: Vec<GroupedColIter<'a>>,
}

impl<'a> GroupIter<'a> {
    fn try_next(&mut self) -> Result<Option<CellValue>, DecodeColumnError> {
        let num = self
            .num
            .next()
            .transpose()
            .map_err(|e| DecodeColumnError::decode_raw("num", e))?;
        match num {
            None => Ok(None),
            Some(None) => Err(DecodeColumnError::unexpected_null("num")),
            Some(Some(num)) => {
                let mut row = Vec::new();
                for _ in 0..num {
                    let mut inner_row = Vec::new();
                    for (index, value_col) in self.values.iter_mut().enumerate() {
                        match value_col.next().transpose()? {
                            None => {
                                return Err(DecodeColumnError::unexpected_null(format!(
                                    "col {}",
                                    index
                                )))
                            }
                            Some(v) => {
                                inner_row.push(v);
                            }
                        }
                    }
                    row.push(inner_row);
                }
                Ok(Some(CellValue::Group(row)))
            }
        }
    }
}

impl<'a> Iterator for GroupIter<'a> {
    type Item = Result<CellValue, DecodeColumnError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.try_next().transpose()
    }
}

#[derive(Debug, Clone)]
enum GroupedColIter<'a> {
    Value(ValueIter<'a>),
    Simple(SimpleColIter<'a>),
}

impl<'a> GroupedColIter<'a> {
    fn try_next(&mut self) -> Result<Option<SimpleValue>, DecodeColumnError> {
        match self {
            Self::Value(viter) => Ok(viter.next().transpose()?.map(SimpleValue::Value)),
            Self::Simple(siter) => siter
                .next()
                .transpose()
                .map_err(|e| DecodeColumnError::decode_raw("a simple column", e)),
        }
    }
}

impl<'a> Iterator for GroupedColIter<'a> {
    type Item = Result<SimpleValue, DecodeColumnError>;

    fn next(&mut self) -> Option<Self::Item> {
        self.try_next().transpose()
    }
}