ella_common/row/
tuple.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
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
139
140
141
142
143
use super::{RowBatchBuilder, RowFormat, RowFormatView, RowViewIter};
use datafusion::arrow::datatypes::Field;
use std::{marker::PhantomData, sync::Arc};

#[derive(Debug, Clone)]
pub struct TupleBuilder<T> {
    builders: T,
    len: usize,
}

#[derive(Debug, Clone)]
pub struct TupleView<T, V> {
    values: V,
    len: usize,
    _type: PhantomData<T>,
}

macro_rules! impl_tuple_row {
    ($([$i:literal $($t:ident)*])+) => {
        paste::paste! {
        $(
        impl<$($t),*> RowFormat for ($($t,)*)
        where $($t: RowFormat),*
        {
            const COLUMNS: usize = $(<$t as RowFormat>::COLUMNS + )* 0;
            type Builder = TupleBuilder<($($t::Builder,)*)>;
            type View = TupleView<($($t,)*), ($($t::View,)*)>;

            #[allow(unused_assignments, unused_mut)]
            fn builder(mut fields: &[Arc<Field>]) -> crate::Result<Self::Builder> {
                if fields.len() != Self::COLUMNS {
                    return Err(crate::Error::ColumnCount(Self::COLUMNS, fields.len()));
                }
                $(
                    let cols = <$t as RowFormat>::COLUMNS;
                    let [< $t:lower >] = $t::builder(&fields[..cols])?;
                    fields = &fields[cols..];
                )*

                Ok(TupleBuilder {
                    builders: ($([< $t:lower >],)*),
                    len: 0,
                })
            }

            #[allow(unused_assignments, unused_mut, unused_variables)]
            fn view(rows: usize, mut fields: &[Arc<Field>], mut arrays: &[datafusion::arrow::array::ArrayRef]) -> crate::Result<Self::View> {
                if arrays.len() != Self::COLUMNS {
                    return Err(crate::Error::ColumnCount(Self::COLUMNS, arrays.len()));
                }
                $(
                    let cols = <$t as RowFormat>::COLUMNS;
                    let [< $t:lower >] = $t::view(rows, &fields[..cols], &arrays[..cols])?;
                    debug_assert_eq!([< $t:lower >].len(), rows);
                    fields = &fields[cols..];
                    arrays = &arrays[cols..];
                )*

                Ok(TupleView {
                    values: ($([< $t:lower >],)*),
                    _type: PhantomData,
                    len: rows,
                })
            }
        }

        impl<$($t),*> RowBatchBuilder<($($t,)*)> for TupleBuilder<($($t::Builder,)*)>
        where $($t: RowFormat),*
        {
            #[inline]
            fn len(&self) -> usize {
                self.len
            }

            fn push(&mut self, ($([<row_ $t:lower>],)*): ($($t,)*)) {
                let ($(ref mut [< $t:lower >],)*) = &mut self.builders;
                $(
                    [< $t:lower >].push([<row_ $t:lower>]);
                )*
                self.len += 1;
            }

            #[allow(unused_mut)]
            fn build_columns(&mut self) -> crate::Result<Vec<datafusion::arrow::array::ArrayRef>> {
                let mut cols = Vec::with_capacity(<($($t,)*) as RowFormat>::COLUMNS);
                let ($(ref mut [< $t:lower >],)*) = &mut self.builders;
                $(
                    cols.extend([< $t:lower >].build_columns()?);
                )*
                self.len = 0;
                Ok(cols)
            }
        }

        impl<$($t),*> RowFormatView<($($t,)*)> for TupleView<($($t,)*), ($($t::View,)*)>
        where $($t: RowFormat),*
        {
            #[inline]
            fn len(&self) -> usize {
                self.len
            }

            #[allow(unused_variables, clippy::unused_unit)]
            fn row(&self, i: usize) -> ($($t,)*) {
                let ($(ref [< $t:lower >],)*) = &self.values;
                ($(
                    [< $t:lower >].row(i),
                )*)
            }

            #[allow(unused_variables, clippy::unused_unit)]
            unsafe fn row_unchecked(&self, i: usize) -> ($($t,)*) {
                let ($(ref [< $t:lower >],)*) = &self.values;
                ($(
                    [< $t:lower >].row_unchecked(i),
                )*)
            }
        }

        impl<$($t),*> IntoIterator for TupleView<($($t,)*), ($($t::View,)*)>
        where $($t: RowFormat),*
        {
            type Item = ($($t,)*);
            type IntoIter = RowViewIter<($($t,)*), Self>;

            fn into_iter(self) -> Self::IntoIter {
                RowViewIter::new(self)
            }
        }
        )+
        }
    };
}

impl_tuple_row!(
    [0]
    [1 T1]
    [2 T1 T2]
    [3 T1 T2 T3]
    [4 T1 T2 T3 T4]
    [5 T1 T2 T3 T4 T5]
    [6 T1 T2 T3 T4 T5 T6]
);