risingwave_common/row/
slice.rs

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
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::ops::{Range, RangeBounds};

use super::Row;
use crate::types::DatumRef;

/// Row for the [`slice`](super::RowExt::slice) method.
#[derive(Debug, Clone)]
pub struct Slice<R> {
    row: R,
    range: Range<usize>,
}

impl<R: Row> PartialEq for Slice<R> {
    fn eq(&self, other: &Self) -> bool {
        self.iter().eq(other.iter())
    }
}
impl<R: Row> Eq for Slice<R> {}

impl<R: Row> Row for Slice<R> {
    #[inline]
    fn datum_at(&self, index: usize) -> DatumRef<'_> {
        assert!(index < self.len());
        // SAFETY: we have checked that `self.indices` are all valid in `new`.
        unsafe { self.row.datum_at_unchecked(self.range.start + index) }
    }

    #[inline]
    unsafe fn datum_at_unchecked(&self, index: usize) -> DatumRef<'_> {
        self.row.datum_at_unchecked(self.range.start + index)
    }

    #[inline]
    fn len(&self) -> usize {
        self.range.len()
    }

    #[inline]
    fn iter(&self) -> impl Iterator<Item = DatumRef<'_>> {
        self.row
            .iter()
            .skip(self.range.start)
            .take(self.range.len())
    }
}

impl<R: Row> Slice<R> {
    pub(crate) fn new(row: R, range: impl RangeBounds<usize>) -> Self {
        use std::ops::Bound::*;
        let start = match range.start_bound() {
            Included(&i) => i,
            Excluded(&i) => i + 1,
            Unbounded => 0,
        };
        let end = match range.end_bound() {
            Included(&i) => i + 1,
            Excluded(&i) => i,
            Unbounded => row.len(),
        };
        let range = start..end;
        assert!(range.end <= row.len(), "range out of bounds");
        Self { row, range }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::row::{OwnedRow, RowExt};
    use crate::types::{ScalarImpl, ScalarRefImpl};

    #[test]
    fn test_slice() {
        let r0 = OwnedRow::new((0..=8).map(|i| Some(ScalarImpl::Int64(i))).collect());
        let r_expected = OwnedRow::new((2..4).map(|i| Some(ScalarImpl::Int64(i))).collect());

        let r = r0.slice(2..4);
        assert_eq!(r.len(), 2);
        assert!(r.iter().eq(r_expected.iter()));

        for i in 0..2 {
            assert_eq!(r.datum_at(i), Some(ScalarRefImpl::Int64(i as i64 + 2)));
        }
    }
}