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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use std::io::{Read, Seek, Write};
use std::sync::Arc;

use anyhow::{anyhow, bail, ensure, Context, Result};
use arrow2::array::{
    Array, BooleanArray, MutableBooleanArray, MutablePrimitiveArray, MutableUtf8Array,
    PrimitiveArray, TryExtend, Utf8Array,
};
use arrow2::datatypes::{DataType, Field, Schema};
use arrow2::io::parquet::read;
use arrow2::io::parquet::read::RowGroupMetaData;
use arrow2::io::parquet::write::Encoding;
use log::info;

use crate::table::VarArray;
use crate::{Kind, Packer, TableField};

#[derive(Clone)]
pub struct OutField {
    pub name: String,
    pub data_type: DataType,
    pub nullable: bool,

    pub encoding: Encoding,
}

// struct Transform {
//     input: String,
//     output: OutField,
//     func: Box<dyn FnMut(Box<dyn Array>, &mut Table, usize) -> Result<()>>,
// }

pub struct Split {
    pub output: Vec<OutField>,
    pub func: Box<dyn Send + FnMut(Arc<dyn Array>, &mut [&mut VarArray]) -> Result<()>>,
}

pub enum Action {
    ErrorOut,
    Drop,
    Copy,
    // Transform(Transform),
    Split(Split),
}

pub struct Op {
    pub input: String,
    pub action: Action,
}

pub struct Repack {
    pub ops: Vec<Op>,
}

#[inline]
pub fn find_field<'f>(schema: &'f Schema, name: &str) -> Option<(usize, &'f Field)> {
    schema
        .fields
        .iter()
        .enumerate()
        .find(|(_, f)| f.name == name)
}

pub enum LoopDecision {
    Include,
    Skip,
    Break,
}

pub fn read_single_column(
    mut f: impl Read + Seek,
    rg_meta: &RowGroupMetaData,
    field_meta: Field,
) -> Result<Arc<dyn Array>> {
    let name = field_meta.name.to_string();
    let col = read::read_columns(&mut f, rg_meta.columns(), &name)?;
    let mut des = read::to_deserializer(
        col,
        field_meta,
        rg_meta
            .num_rows()
            .try_into()
            .expect("row count fits in memory"),
        None,
    )?;

    let ret = des
        .next()
        .ok_or_else(|| anyhow!("expected at least one column"))??;
    ensure!(des.next().is_none(), "expected exactly one column");

    Ok(ret)
}

pub fn transform<W: Write + Send + 'static>(
    mut f: impl Read + Seek,
    out: W,
    repack: &mut Repack,
    mut rg_filter: impl FnMut(usize, &RowGroupMetaData) -> LoopDecision,
) -> Result<W> {
    let metadata = read::read_metadata(&mut f)?;
    let in_schema = read::infer_schema(&metadata)?;

    let out_schema = repack
        .ops
        .iter()
        .flat_map(|op| -> Vec<Result<OutField>> {
            match &op.action {
                Action::Drop | Action::ErrorOut => Vec::new(),
                Action::Copy => vec![
                    try {
                        let (_, x) = find_field(&in_schema, &op.input)
                            .ok_or_else(|| anyhow!("field has gone missing?"))?;
                        OutField {
                            name: x.name.to_string(),
                            data_type: x.data_type.clone(),
                            nullable: x.is_nullable,
                            encoding: Encoding::Plain,
                        }
                    },
                ],

                Action::Split(split) => split.output.iter().cloned().map(Ok).collect(),
            }
        })
        .collect::<Result<Vec<OutField>>>()?;

    let table_schema = out_schema
        .iter()
        .map(|v| -> Result<TableField> {
            Ok(TableField {
                name: v.name.to_string(),
                kind: Kind::from_arrow(&v.data_type)
                    .with_context(|| anyhow!("converting {:?} to a Kind", v.name))?,
                nullable: false,
                encoding: Encoding::Plain,
            })
        })
        .collect::<Result<Vec<_>>>()
        .with_context(|| anyhow!("generating an internal schema for the output"))?;

    let mut writer = Packer::new(out, &table_schema)?;

    for (rg, rg_meta) in metadata.row_groups.iter().enumerate() {
        info!(
            "handling rg {}/{} ({} rows)",
            rg,
            metadata.row_groups.len(),
            rg_meta.num_rows()
        );

        match rg_filter(rg, rg_meta) {
            LoopDecision::Include => (),
            LoopDecision::Skip => continue,
            LoopDecision::Break => break,
        };

        for op in &mut repack.ops {
            let (_field, field_meta) = find_field(&in_schema, &op.input)
                .ok_or_else(|| anyhow!("looking up input field {:?}", op.input))?;

            let arr = read_single_column(&mut f, rg_meta, field_meta.clone())?;

            match &mut op.action {
                Action::ErrorOut => bail!("asked to error out after loading {:?}", field_meta.name),
                Action::Drop => unimplemented!("drop"),
                Action::Copy => {
                    let (output, _) = writer.find_field(&op.input).expect("created above");

                    let output = writer.table().get(output);

                    if let Some(output) = output.downcast_mut::<MutableUtf8Array<i32>>() {
                        output
                            .try_extend(
                                arr.as_any()
                                    .downcast_ref::<Utf8Array<i32>>()
                                    .expect("input=output")
                                    .iter(),
                            )
                            .with_context(|| {
                                anyhow!("copying {} rows of {:?}", metadata.num_rows, op.input)
                            })?;
                    } else if let Some(output) = output.downcast_mut::<MutablePrimitiveArray<i64>>()
                    {
                        output.extend(
                            arr.as_any()
                                .downcast_ref::<PrimitiveArray<i64>>()
                                .expect("input=output")
                                .iter()
                                .map(|v| v.map(|x| *x)),
                        );
                    } else if let Some(output) = output.downcast_mut::<MutablePrimitiveArray<i32>>()
                    {
                        output.extend(
                            arr.as_any()
                                .downcast_ref::<PrimitiveArray<i32>>()
                                .expect("input=output")
                                .iter()
                                .map(|v| v.map(|x| *x)),
                        );
                    } else if let Some(output) = output.downcast_mut::<MutableBooleanArray>() {
                        output.extend(
                            arr.as_any()
                                .downcast_ref::<BooleanArray>()
                                .expect("input=output")
                                .iter(),
                        );
                    } else if let Some(output) = output.downcast_mut::<MutablePrimitiveArray<f64>>()
                    {
                        output.extend(
                            arr.as_any()
                                .downcast_ref::<PrimitiveArray<f64>>()
                                .expect("input=output")
                                .iter()
                                .map(|v| v.map(|x| *x)),
                        );
                    } else {
                        bail!(
                            "copy for {:?} columns ({:?})",
                            field_meta.data_type,
                            field_meta.name
                        )
                    }
                }
                Action::Split(s) => {
                    let fields: Vec<usize> = s
                        .output
                        .iter()
                        .map(|f| {
                            writer
                                .find_field(&f.name)
                                .expect("created based on input")
                                .0
                        })
                        .collect();
                    (s.func)(arr, &mut writer.table().get_many(&fields))?;
                }
            }
        }

        writer.table().finish_bulk_push()?;
        writer.consider_flushing()?;
    }

    writer.finish()
}