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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use std::collections::hash_map::{Entry, HashMap};
use std::io;
use std::num::NonZeroUsize;

use bstr::ByteSlice;
use csv::ByteRecord;

use crate::config::{Config, Delimiter};
use crate::select::{SelectColumns, Selection};
use crate::util;
use crate::CliResult;

type IndexKey = Vec<Vec<u8>>;

fn get_row_key(sel: &Selection, row: &ByteRecord, case_insensitive: bool) -> IndexKey {
    sel.select(row)
        .map(|v| transform(v, case_insensitive))
        .collect()
}

fn transform(bs: &[u8], case_insensitive: bool) -> Vec<u8> {
    if !case_insensitive {
        bs.to_vec()
    } else {
        bs.to_lowercase()
    }
}

fn build_headers(
    left_headers: &ByteRecord,
    right_headers: &ByteRecord,
    left_prefix: &Option<String>,
    right_prefix: &Option<String>,
) -> ByteRecord {
    let mut headers = ByteRecord::new();

    for column in left_headers.iter() {
        if let Some(prefix) = left_prefix {
            headers.push_field(&[prefix.as_bytes(), column].concat());
        } else {
            headers.push_field(column);
        }
    }

    for column in right_headers.iter() {
        if let Some(prefix) = right_prefix {
            headers.push_field(&[prefix.as_bytes(), column].concat());
        } else {
            headers.push_field(column);
        }
    }

    headers
}

fn get_padding(headers: &ByteRecord) -> ByteRecord {
    (0..headers.len()).map(|_| b"").collect()
}

#[derive(Debug)]
struct IndexNode {
    record: ByteRecord,
    written: bool,
    next: Option<NonZeroUsize>,
}

impl IndexNode {
    fn new(record: ByteRecord) -> Self {
        Self {
            record,
            written: false,
            next: None,
        }
    }
}

// NOTE: I keep both head & tail to keep insertion order easily
// It is possible to keep only the tail instead of course to
// save up more memory, but the output is less understandable
// for the user and not aligned with usual affordances.
#[derive(Debug)]
struct Index {
    case_insensitive: bool,
    nulls: bool,
    map: HashMap<IndexKey, (usize, usize)>,
    nodes: Vec<IndexNode>,
}

impl Index {
    fn new(case_insensitive: bool, nulls: bool) -> Self {
        Self {
            case_insensitive,
            nulls,
            map: HashMap::new(),
            nodes: Vec::new(),
        }
    }

    fn from_csv_reader<R: io::Read>(
        reader: &mut csv::Reader<R>,
        sel: &Selection,
        case_insensitive: bool,
        nulls: bool,
    ) -> CliResult<Self> {
        let mut index = Index::new(case_insensitive, nulls);

        for result in reader.byte_records() {
            let record = result?;

            index.add(sel, record);
        }

        Ok(index)
    }

    fn add(&mut self, sel: &Selection, record: ByteRecord) {
        let key = get_row_key(sel, &record, self.case_insensitive);

        if !self.nulls && key.iter().all(|c| c.is_empty()) {
            return;
        }

        let next_id = self.nodes.len() + 1;

        match self.map.entry(key) {
            Entry::Occupied(mut entry) => {
                let (_, tail) = entry.get_mut();
                let new_node = IndexNode::new(record);
                self.nodes[*tail - 1].next = Some(NonZeroUsize::new(next_id).unwrap());
                *tail = next_id;
                self.nodes.push(new_node);
            }
            Entry::Vacant(entry) => {
                entry.insert((next_id, next_id));
                self.nodes.push(IndexNode::new(record));
            }
        };
    }

    fn for_each_node_mut<F, E>(
        &mut self,
        sel: &Selection,
        record: &ByteRecord,
        mut callback: F,
    ) -> Result<(), E>
    where
        F: FnMut(&mut IndexNode) -> Result<(), E>,
    {
        let key = get_row_key(sel, record, self.case_insensitive);

        if !self.nulls && key.iter().all(|c| c.is_empty()) {
            return Ok(());
        }

        if let Some((i, _)) = self.map.get(&key) {
            let mut current_node = &mut self.nodes[i - 1];

            callback(current_node)?;

            while let Some(previous_index) = current_node.next {
                current_node = &mut self.nodes[previous_index.get() - 1];
                callback(current_node)?;
            }
        }

        Ok(())
    }

    fn for_each_record<F, E>(
        &mut self,
        sel: &Selection,
        record: &ByteRecord,
        mut callback: F,
    ) -> Result<(), E>
    where
        F: FnMut(&ByteRecord) -> Result<(), E>,
    {
        self.for_each_node_mut(sel, record, |node| callback(&node.record))
    }

    fn records_not_written(&self) -> impl Iterator<Item = &ByteRecord> {
        self.nodes.iter().filter_map(|node| {
            if !node.written {
                Some(&node.record)
            } else {
                None
            }
        })
    }
}

static USAGE: &str = "
Join two sets of CSV data on the specified columns.

The default join operation is an \"inner\" join. This corresponds to the
intersection of rows on the keys specified. The command is also able to
perform a left outer join with --left, a right outer join with --right,
a full outer join with --full and finally a cartesian product/cross join
with --cross.

By default, joins are done case sensitively, but this can be disabled using
the -i, --ignore-case flag.

The column arguments specify the columns to join for each input. Columns can
be selected using the same syntax as the \"xan select\" command. Both selections
must return a same number of columns, for the join keys to be properly aligned.

Note that this command is able to consume streams such as stdin (in which case
the file name must be \"-\" to indicate which file will be read from stdin) and
gzipped files out of the box.

# Memory considerations

    - `inner join`: the command does not try to be clever and
                    always indexes the left file, while the right
                    file is streamed. Prefer placing the smaller file
                    on the left.
    - `left join`:  the command always indexes the right file and streams
                    the left file.
    - `right join`: the command always indexes the left file and streams
                    the right file.
    - `full join`:  the command does not try to be clever and
                    always indexes the left file, while the right
                    file is streamed. Prefer placing the smaller file
                    on the left.
    - `cross join`: the command does not try to be clever and
                    always indexes the left file, while the right
                    file is streamed. Prefer placing the smaller file
                    on the left.

Usage:
    xan join [options] <columns1> <input1> <columns2> <input2>
    xan join [options] --cross <input1> <input2>
    xan join --help

join options:
    --left                       Do an \"outer left\" join. This returns all rows in
                                 first CSV data set, including rows with no
                                 corresponding row in the second data set. When no
                                 corresponding row exists, it is padded out with
                                 empty fields. This is the reverse of --right.
    --right                      Do an \"outer right\" join. This returns all rows in
                                 second CSV data set, including rows with no
                                 corresponding row in the first data set. When no
                                 corresponding row exists, it is padded out with
                                 empty fields. This is the reverse of --left.
    --full                       Do a \"full outer\" join. This returns all rows in
                                 both data sets with matching records joined. If
                                 there is no match, the missing side will be padded
                                 out with empty fields.
    --cross                      This returns the cartesian product of the given CSV
                                 files. The number of rows emitted will be equal to N * M,
                                 where N and M correspond to the number of rows in the given
                                 data sets, respectively.
    -i, --ignore-case            When set, joins are done case insensitively.
    --nulls                      When set, joins will work on empty fields.
                                 Otherwise, empty keys are completely ignored, i.e. when
                                 column selection yield only empty cells.
    -L, --prefix-left <prefix>   Add a prefix to the names of the columns in the
                                 first dataset.
    -R, --prefix-right <prefix>  Add a prefix to the names of the columns in the
                                 second dataset.

Common options:
    -h, --help                  Display this message
    -o, --output <file>         Write output to <file> instead of stdout.
    -n, --no-headers            When set, the first row will not be interpreted
                                as headers. (i.e., They are not searched, analyzed,
                                sliced, etc.)
    -d, --delimiter <arg>       The field delimiter for reading CSV data.
                                Must be a single character.
";

#[derive(Deserialize)]
struct Args {
    arg_columns1: SelectColumns,
    arg_input1: String,
    arg_columns2: SelectColumns,
    arg_input2: String,
    flag_left: bool,
    flag_right: bool,
    flag_full: bool,
    flag_cross: bool,
    flag_output: Option<String>,
    flag_no_headers: bool,
    flag_ignore_case: bool,
    flag_nulls: bool,
    flag_delimiter: Option<Delimiter>,
    flag_prefix_left: Option<String>,
    flag_prefix_right: Option<String>,
}

type BoxedReader = csv::Reader<Box<dyn io::Read + Send>>;

impl Args {
    fn readers_and_selections(
        &self,
    ) -> CliResult<((BoxedReader, Selection), (BoxedReader, Selection))> {
        let left = Config::new(&Some(self.arg_input1.clone()))
            .delimiter(self.flag_delimiter)
            .no_headers(self.flag_no_headers)
            .select(self.arg_columns1.clone());

        let right = Config::new(&Some(self.arg_input2.clone()))
            .delimiter(self.flag_delimiter)
            .no_headers(self.flag_no_headers)
            .select(self.arg_columns2.clone());

        let mut left_reader = left.reader()?;
        let mut right_reader = right.reader()?;

        let left_sel = left.selection(left_reader.byte_headers()?)?;
        let right_sel = right.selection(right_reader.byte_headers()?)?;

        if left_sel.len() != right_sel.len() {
            Err("not the same number of columns selected on left & right!")?;
        }

        Ok(((left_reader, left_sel), (right_reader, right_sel)))
    }

    fn wconf(&self) -> Config {
        Config::new(&self.flag_output)
    }

    fn index(&self, reader: &mut BoxedReader, sel: &Selection) -> CliResult<Index> {
        Index::from_csv_reader(reader, sel, self.flag_ignore_case, self.flag_nulls)
    }

    fn write_headers<W: io::Write>(
        &self,
        writer: &mut csv::Writer<W>,
        left_headers: &ByteRecord,
        right_headers: &ByteRecord,
    ) -> CliResult<()> {
        if !self.flag_no_headers {
            writer.write_byte_record(&build_headers(
                left_headers,
                right_headers,
                &self.flag_prefix_left,
                &self.flag_prefix_right,
            ))?;
        }

        Ok(())
    }

    fn inner_join(self) -> CliResult<()> {
        let ((mut left_reader, left_sel), (mut right_reader, right_sel)) =
            self.readers_and_selections()?;

        let mut writer = self.wconf().writer()?;

        self.write_headers(
            &mut writer,
            left_reader.byte_headers()?,
            right_reader.byte_headers()?,
        )?;

        let mut index = self.index(&mut left_reader, &left_sel)?;

        let mut right_record = csv::ByteRecord::new();

        while right_reader.read_byte_record(&mut right_record)? {
            index.for_each_record(&right_sel, &right_record, |left_record| {
                writer.write_record(left_record.iter().chain(right_record.iter()))
            })?;
        }

        Ok(writer.flush()?)
    }

    fn full_outer_join(self) -> CliResult<()> {
        let ((mut left_reader, left_sel), (mut right_reader, right_sel)) =
            self.readers_and_selections()?;

        let mut writer = self.wconf().writer()?;

        let left_headers = left_reader.byte_headers()?.clone();
        let right_headers = right_reader.byte_headers()?.clone();

        let left_padding = get_padding(&left_headers);
        let right_padding = get_padding(&right_headers);

        self.write_headers(&mut writer, &left_headers, &right_headers)?;

        let mut index = self.index(&mut left_reader, &left_sel)?;

        let mut right_record = csv::ByteRecord::new();

        while right_reader.read_byte_record(&mut right_record)? {
            let mut something_was_written: bool = false;

            index.for_each_node_mut(&right_sel, &right_record, |left_node| {
                something_was_written = true;
                left_node.written = true;
                writer.write_record(left_node.record.iter().chain(right_record.iter()))
            })?;

            if !something_was_written {
                writer.write_record(left_padding.iter().chain(right_record.iter()))?;
            }
        }

        for left_record in index.records_not_written() {
            writer.write_record(left_record.iter().chain(right_padding.iter()))?;
        }

        Ok(writer.flush()?)
    }

    fn left_join(self) -> CliResult<()> {
        let ((mut left_reader, left_sel), (mut right_reader, right_sel)) =
            self.readers_and_selections()?;

        let mut writer = self.wconf().writer()?;

        let left_headers = left_reader.byte_headers()?.clone();
        let right_headers = right_reader.byte_headers()?.clone();

        let right_padding = get_padding(&right_headers);

        self.write_headers(&mut writer, &left_headers, &right_headers)?;

        let mut index = self.index(&mut right_reader, &right_sel)?;

        let mut left_record = csv::ByteRecord::new();

        while left_reader.read_byte_record(&mut left_record)? {
            let mut something_was_written: bool = false;

            index.for_each_record(&left_sel, &left_record, |right_record| {
                something_was_written = true;
                writer.write_record(left_record.iter().chain(right_record.iter()))
            })?;

            if !something_was_written {
                writer.write_record(left_record.iter().chain(right_padding.iter()))?;
            }
        }

        Ok(writer.flush()?)
    }

    fn right_join(self) -> CliResult<()> {
        let ((mut left_reader, left_sel), (mut right_reader, right_sel)) =
            self.readers_and_selections()?;

        let mut writer = self.wconf().writer()?;

        let left_headers = left_reader.byte_headers()?.clone();
        let right_headers = right_reader.byte_headers()?.clone();

        let left_padding = get_padding(&left_headers);

        self.write_headers(&mut writer, &left_headers, &right_headers)?;

        let mut index = self.index(&mut left_reader, &left_sel)?;

        let mut right_record = csv::ByteRecord::new();

        while right_reader.read_byte_record(&mut right_record)? {
            let mut something_was_written: bool = false;

            index.for_each_record(&right_sel, &right_record, |left_record| {
                something_was_written = true;
                writer.write_record(left_record.iter().chain(right_record.iter()))
            })?;

            if !something_was_written {
                writer.write_record(left_padding.iter().chain(right_record.iter()))?;
            }
        }

        Ok(writer.flush()?)
    }

    fn cross_join(self) -> CliResult<()> {
        let ((mut left_reader, _), (mut right_reader, _)) = self.readers_and_selections()?;

        let mut writer = self.wconf().writer()?;

        self.write_headers(
            &mut writer,
            left_reader.byte_headers()?,
            right_reader.byte_headers()?,
        )?;

        let index = right_reader
            .into_byte_records()
            .collect::<Result<Vec<_>, _>>()?;

        let mut left_record = csv::ByteRecord::new();

        while left_reader.read_byte_record(&mut left_record)? {
            for right_record in index.iter() {
                writer.write_record(left_record.iter().chain(right_record.iter()))?;
            }
        }

        Ok(writer.flush()?)
    }
}

pub fn run(argv: &[&str]) -> CliResult<()> {
    let args: Args = util::get_args(USAGE, argv)?;

    if [
        args.flag_left,
        args.flag_right,
        args.flag_full,
        args.flag_cross,
    ]
    .iter()
    .filter(|flag| **flag)
    .count()
        > 1
    {
        Err("Please pick exactly one join operation.")?;
    }

    if args.flag_left {
        args.left_join()
    } else if args.flag_right {
        args.right_join()
    } else if args.flag_full {
        args.full_outer_join()
    } else if args.flag_cross {
        args.cross_join()
    } else {
        args.inner_join()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rec(values: &[&str]) -> ByteRecord {
        let mut record = ByteRecord::new();

        for v in values.iter() {
            record.push_field(v.as_bytes());
        }

        record
    }

    impl Index {
        fn test_vec(&mut self, sel: &Selection, record: &ByteRecord) -> Vec<ByteRecord> {
            let mut v = Vec::new();

            self.for_each_node_mut(sel, record, |node| -> Result<(), ()> {
                v.push(node.record.clone());
                Ok(())
            })
            .unwrap();

            v
        }
    }

    #[test]
    fn test_index_linked_lists() {
        let mut index = Index::new(false, false);
        let sel = Selection::full(1);

        index.add(&sel, ByteRecord::from(rec(&["a", "one"])));
        index.add(&sel, ByteRecord::from(rec(&["b", "one"])));
        index.add(&sel, ByteRecord::from(rec(&["a", "two"])));
        index.add(&sel, ByteRecord::from(rec(&["a", "three"])));
        index.add(&sel, ByteRecord::from(rec(&["b", "two"])));
        index.add(&sel, ByteRecord::from(rec(&["c", "one"])));

        assert_eq!(
            index.test_vec(&sel, &rec(&["d", "one"])),
            Vec::<ByteRecord>::new()
        );
        assert_eq!(
            index.test_vec(&sel, &rec(&["a", "one"])),
            vec![rec(&["a", "one"]), rec(&["a", "two"]), rec(&["a", "three"])]
        );
        assert_eq!(
            index.test_vec(&sel, &rec(&["b", "one"])),
            vec![rec(&["b", "one"]), rec(&["b", "two"])]
        );
        assert_eq!(
            index.test_vec(&sel, &rec(&["c", "one"])),
            vec![rec(&["c", "one"])]
        );
    }
}