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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Source implementation for CSV files.

mod errors;
mod typesystem;

pub use self::errors::CSVSourceError;
pub use self::typesystem::CSVTypeSystem;
use super::{PartitionParser, Produce, Source, SourcePartition};
use crate::{data_order::DataOrder, errors::ConnectorXError, sql::CXQuery};
use anyhow::anyhow;
use chrono::{DateTime, Utc};
use fehler::{throw, throws};
#[cfg(feature = "src_csv")]
use regex::{Regex, RegexBuilder};
use std::collections::HashSet;
use std::fs::File;

pub struct CSVSource {
    schema: Vec<CSVTypeSystem>,
    files: Vec<CXQuery<String>>,
    names: Vec<String>,
}

impl CSVSource {
    pub fn new(schema: &[CSVTypeSystem]) -> Self {
        CSVSource {
            schema: schema.to_vec(),
            files: vec![],
            names: vec![],
        }
    }

    #[throws(CSVSourceError)]
    pub fn infer_schema(&mut self) -> Vec<CSVTypeSystem> {
        // regular expressions for infer CSVTypeSystem from string
        let decimal_re: Regex = Regex::new(r"^-?(\d+\.\d+)$")?;
        let integer_re: Regex = Regex::new(r"^-?(\d+)$")?;
        let boolean_re: Regex = RegexBuilder::new(r"^(true)$|^(false)$")
            .case_insensitive(true)
            .build()?;
        let datetime_re: Regex = Regex::new(r"^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d$")?;

        // read max_records rows to infer possible CSVTypeSystems for each field
        let mut reader = csv::ReaderBuilder::new()
            .has_headers(true)
            .from_reader(File::open(self.files[0].as_str())?);

        let max_records_to_read = 50;
        let num_cols = self.names.len();

        let mut column_types: Vec<HashSet<CSVTypeSystem>> = vec![HashSet::new(); num_cols];
        let mut nulls: Vec<bool> = vec![false; num_cols];

        let mut record = csv::StringRecord::new();

        for _record_counter in 0..max_records_to_read {
            if !reader.read_record(&mut record)? {
                break;
            }
            for field_counter in 0..num_cols {
                if let Some(string) = record.get(field_counter) {
                    if string.is_empty() {
                        nulls[field_counter] = true;
                    } else {
                        let dt: CSVTypeSystem;

                        if string.starts_with('"') {
                            dt = CSVTypeSystem::String(false);
                        } else if boolean_re.is_match(string) {
                            dt = CSVTypeSystem::Bool(false);
                        } else if decimal_re.is_match(string) {
                            dt = CSVTypeSystem::F64(false);
                        } else if integer_re.is_match(string) {
                            dt = CSVTypeSystem::I64(false);
                        } else if datetime_re.is_match(string) {
                            dt = CSVTypeSystem::DateTime(false);
                        } else {
                            dt = CSVTypeSystem::String(false);
                        }
                        column_types[field_counter].insert(dt);
                    }
                }
            }
        }

        // determine CSVTypeSystem based on possible candidates
        let mut schema = vec![];

        for field_counter in 0..num_cols {
            let possibilities = &column_types[field_counter];
            let has_nulls = nulls[field_counter];

            match possibilities.len() {
                1 => {
                    for dt in possibilities.iter() {
                        match *dt {
                            CSVTypeSystem::I64(false) => {
                                schema.push(CSVTypeSystem::I64(has_nulls));
                            }
                            CSVTypeSystem::F64(false) => {
                                schema.push(CSVTypeSystem::F64(has_nulls));
                            }
                            CSVTypeSystem::Bool(false) => {
                                schema.push(CSVTypeSystem::Bool(has_nulls));
                            }
                            CSVTypeSystem::String(false) => {
                                schema.push(CSVTypeSystem::String(has_nulls));
                            }
                            CSVTypeSystem::DateTime(false) => {
                                schema.push(CSVTypeSystem::DateTime(has_nulls));
                            }
                            _ => {}
                        }
                    }
                }
                2 => {
                    if possibilities.contains(&CSVTypeSystem::I64(false))
                        && possibilities.contains(&CSVTypeSystem::F64(false))
                    {
                        // Integer && Float -> Float
                        schema.push(CSVTypeSystem::F64(has_nulls));
                    } else {
                        // Conflicting CSVTypeSystems -> String
                        schema.push(CSVTypeSystem::String(has_nulls));
                    }
                }
                _ => {
                    // Conflicting CSVTypeSystems -> String
                    schema.push(CSVTypeSystem::String(has_nulls));
                }
            }
        }
        schema
    }
}

impl Source for CSVSource {
    const DATA_ORDERS: &'static [DataOrder] = &[DataOrder::RowMajor];
    type Partition = CSVSourcePartition;
    type TypeSystem = CSVTypeSystem;
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn set_data_order(&mut self, data_order: DataOrder) {
        if !matches!(data_order, DataOrder::RowMajor) {
            throw!(ConnectorXError::UnsupportedDataOrder(data_order))
        }
    }

    fn set_queries<Q: ToString>(&mut self, queries: &[CXQuery<Q>]) {
        self.files = queries.iter().map(|q| q.map(Q::to_string)).collect();
    }

    fn set_origin_query(&mut self, _query: Option<String>) {}

    #[throws(CSVSourceError)]
    fn fetch_metadata(&mut self) {
        let mut reader = csv::ReaderBuilder::new()
            .has_headers(true)
            .from_reader(File::open(self.files[0].as_str())?);
        let header = reader.headers()?;

        self.names = header.iter().map(|s| s.to_string()).collect();

        if self.schema.is_empty() {
            self.schema = self.infer_schema()?;
        }

        assert_eq!(header.len(), self.schema.len());
    }

    #[throws(CSVSourceError)]
    fn result_rows(&mut self) -> Option<usize> {
        None
    }

    fn names(&self) -> Vec<String> {
        self.names.clone()
    }

    fn schema(&self) -> Vec<Self::TypeSystem> {
        self.schema.clone()
    }

    #[throws(CSVSourceError)]
    fn partition(self) -> Vec<Self::Partition> {
        let mut partitions = vec![];
        for file in self.files {
            partitions.push(CSVSourcePartition::new(file)?);
        }
        partitions
    }
}

pub struct CSVSourcePartition {
    records: Vec<csv::StringRecord>,
    counter: usize,
    nrows: usize,
    ncols: usize,
}

impl CSVSourcePartition {
    #[throws(CSVSourceError)]
    pub fn new(fname: CXQuery<String>) -> Self {
        let reader = csv::ReaderBuilder::new()
            .has_headers(true)
            .from_reader(File::open(fname.as_str())?);
        let mut records = vec![];
        reader
            .into_records()
            .try_for_each(|v| -> Result<(), CSVSourceError> {
                records.push(v.map_err(|e| anyhow!(e))?);
                Ok(())
            })?;

        let nrows = records.len();
        let ncols = if nrows > 0 { records[0].len() } else { 0 };

        Self {
            records,
            counter: 0,
            nrows,
            ncols,
        }
    }
}

impl SourcePartition for CSVSourcePartition {
    type TypeSystem = CSVTypeSystem;
    type Parser<'a> = CSVSourcePartitionParser<'a>;
    type Error = CSVSourceError;

    /// The parameter `query` is the path of the csv file
    #[throws(CSVSourceError)]
    fn result_rows(&mut self) {}

    fn nrows(&self) -> usize {
        self.nrows
    }

    fn ncols(&self) -> usize {
        self.ncols
    }

    #[throws(CSVSourceError)]
    fn parser(&mut self) -> Self::Parser<'_> {
        CSVSourcePartitionParser {
            records: &mut self.records,
            counter: &mut self.counter,
            ncols: self.ncols,
        }
    }
}

pub struct CSVSourcePartitionParser<'a> {
    records: &'a mut [csv::StringRecord],
    counter: &'a mut usize,
    ncols: usize,
}

impl<'a> CSVSourcePartitionParser<'a> {
    fn next_val(&mut self) -> &str {
        let v: &str = self.records[*self.counter / self.ncols][*self.counter % self.ncols].as_ref();
        *self.counter += 1;

        v
    }
}

impl<'a> PartitionParser<'a> for CSVSourcePartitionParser<'a> {
    type TypeSystem = CSVTypeSystem;
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn fetch_next(&mut self) -> (usize, bool) {
        (self.records.len(), true)
    }
}

impl<'r, 'a> Produce<'r, i64> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> i64 {
        let v = self.next_val();
        v.parse()
            .map_err(|_| ConnectorXError::cannot_produce::<i64>(Some(v.into())))?
    }
}

impl<'r, 'a> Produce<'r, Option<i64>> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> Option<i64> {
        let v = self.next_val();
        if v.is_empty() {
            return None;
        }
        let v = v
            .parse()
            .map_err(|_| ConnectorXError::cannot_produce::<Option<i64>>(Some(v.into())))?;

        Some(v)
    }
}

impl<'r, 'a> Produce<'r, f64> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> f64 {
        let v = self.next_val();
        v.parse()
            .map_err(|_| ConnectorXError::cannot_produce::<f64>(Some(v.into())))?
    }
}

impl<'r, 'a> Produce<'r, Option<f64>> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> Option<f64> {
        let v = self.next_val();
        if v.is_empty() {
            return None;
        }
        let v = v
            .parse()
            .map_err(|_| ConnectorXError::cannot_produce::<Option<f64>>(Some(v.into())))?;

        Some(v)
    }
}

impl<'r, 'a> Produce<'r, bool> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> bool {
        let v = self.next_val();
        v.parse()
            .map_err(|_| ConnectorXError::cannot_produce::<bool>(Some(v.into())))?
    }
}

impl<'r, 'a> Produce<'r, Option<bool>> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> Option<bool> {
        let v = self.next_val();
        if v.is_empty() {
            return None;
        }
        let v = v
            .parse()
            .map_err(|_| ConnectorXError::cannot_produce::<Option<bool>>(Some(v.into())))?;

        Some(v)
    }
}

impl<'r, 'a> Produce<'r, String> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> String {
        let v = self.next_val();
        String::from(v)
    }
}

impl<'a, 'r> Produce<'r, Option<String>> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&'r mut self) -> Option<String> {
        let v = self.next_val();

        Some(String::from(v))
    }
}

impl<'r, 'a> Produce<'r, DateTime<Utc>> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> DateTime<Utc> {
        let v = self.next_val();
        v.parse()
            .map_err(|_| ConnectorXError::cannot_produce::<DateTime<Utc>>(Some(v.into())))?
    }
}

impl<'r, 'a> Produce<'r, Option<DateTime<Utc>>> for CSVSourcePartitionParser<'a> {
    type Error = CSVSourceError;

    #[throws(CSVSourceError)]
    fn produce(&mut self) -> Option<DateTime<Utc>> {
        let v = self.next_val();
        if v.is_empty() {
            return None;
        }
        let v = v
            .parse()
            .map_err(|_| ConnectorXError::cannot_produce::<DateTime<Utc>>(Some(v.into())))?;
        Some(v)
    }
}