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
use std::{cmp::Reverse, collections::HashMap};

use bstr::ByteSlice;
use csv::{self, ByteRecord};
use rayon::prelude::*;

use crate::collections::{FixedReverseHeap, SortedInsertHashmap};
use crate::config::{Config, Delimiter};
use crate::select::SelectColumns;
use crate::util;
use crate::CliResult;

type GroupKey = Vec<Vec<u8>>;
type ValueKey = Vec<u8>;

static USAGE: &str = "
Compute a frequency table on CSV data.

The resulting frequency table will look like this:

field - Name of the column
value - Some distinct value of the column
count - Number of rows containing this value

By default, there is a row for the N most frequent values for each field in the
data. The number of values can be tweaked with --limit and --threshold flags
respectively.

Since this computes an exact frequency table, memory proportional to the
cardinality of each selected column is required.

To compute custom aggregations per group, beyond just counting, please be sure to
check the `xan groupby` command instead.

Usage:
    xan frequency [options] [<input>]
    xan freq [options] [<input>]

frequency options:
    -s, --select <arg>     Select a subset of columns to compute frequencies
                           for. See 'xan select --help' for the format
                           details. This is provided here because piping 'xan
                           select' into 'xan frequency' will disable the use
                           of indexing.
    --sep <char>           Split the cell into multiple values to count using the
                           provided separator.
    -g, --groupby <cols>   If given, will compute frequency tables per group
                           as defined by the given columns.
    -l, --limit <arg>      Limit the frequency table to the N most common
                           items. Set to <=0 to disable a limit. It is combined
                           with -t/--threshold.
                           [default: 10]
    -t, --threshold <arg>  If set, won't return items having a count less than
                           this given threshold. It is combined with -l/--limit.
    -N, --no-extra         Don't include empty cells & remaining counts.
    -p, --parallel         Allow sorting to be done in parallel. This is only
                           useful with -l/--limit set to 0, i.e. no limit.

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 included
                           in the frequency table. Additionally, the 'field'
                           column will be 1-based indices instead of header
                           names.
    -d, --delimiter <arg>  The field delimiter for reading CSV data.
                           Must be a single character.
";

#[derive(Clone, Deserialize)]
struct Args {
    arg_input: Option<String>,
    flag_select: SelectColumns,
    flag_sep: Option<String>,
    flag_limit: usize,
    flag_threshold: Option<u64>,
    flag_no_extra: bool,
    flag_output: Option<String>,
    flag_no_headers: bool,
    flag_delimiter: Option<Delimiter>,
    flag_parallel: bool,
    flag_groupby: Option<SelectColumns>,
}

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

    let rconf = Config::new(&args.arg_input)
        .delimiter(args.flag_delimiter)
        .no_headers(args.flag_no_headers)
        .select(args.flag_select);

    let mut rdr = rconf.reader()?;
    let mut wtr = Config::new(&args.flag_output).writer()?;

    let headers = rdr.byte_headers()?.clone();
    let mut sel = rconf.selection(&headers)?;
    let groupby_sel_opt = args
        .flag_groupby
        .map(|cols| Config::new(&None).select(cols).selection(&headers))
        .transpose()?;

    // No need to consider the grouping column when counting frequencies
    if let Some(gsel) = &groupby_sel_opt {
        sel.subtract(gsel);
    }

    // Nothing was selected
    if sel.is_empty() {
        return Ok(());
    }

    let field_names: Vec<Vec<u8>> = if args.flag_no_headers {
        sel.indices()
            .map(|i| i.to_string().as_bytes().to_vec())
            .collect()
    } else {
        sel.select(&headers).map(|h| h.to_vec()).collect()
    };

    fn coerce_cell(cell: &[u8], no_extra: bool) -> Option<&[u8]> {
        if !no_extra {
            if cell.is_empty() {
                Some(b"<empty>")
            } else {
                Some(cell)
            }
        } else if cell.is_empty() {
            None
        } else {
            Some(cell)
        }
    }

    if let Some(groupby_sel) = groupby_sel_opt {
        let mut groups_to_fields_to_counter: SortedInsertHashmap<
            GroupKey,
            Vec<HashMap<ValueKey, u64>>,
        > = SortedInsertHashmap::new();

        let output_headers = {
            let mut r = ByteRecord::new();
            r.push_field(b"field");

            for col_name in groupby_sel.select(&headers) {
                r.push_field(col_name);
            }

            r.push_field(b"value");
            r.push_field(b"count");
            r
        };

        wtr.write_byte_record(&output_headers)?;

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

        let mut insert = |g: &Vec<Vec<u8>>, i: usize, c: &[u8]| {
            groups_to_fields_to_counter.insert_with_or_else(
                g.clone(),
                || {
                    let mut list = Vec::with_capacity(sel.len());

                    for _ in 0..sel.len() {
                        list.push(HashMap::new());
                    }

                    list[i]
                        .entry(c.to_vec())
                        .and_modify(|count| *count += 1)
                        .or_insert(1);

                    list
                },
                |list| {
                    list[i]
                        .entry(c.to_vec())
                        .and_modify(|count| *count += 1)
                        .or_insert(1);
                },
            );
        };

        // Aggregating
        while rdr.read_byte_record(&mut record)? {
            let group: Vec<_> = groupby_sel
                .select(&record)
                .map(|cell| cell.to_vec())
                .collect();

            for (i, cell) in sel.select(&record).enumerate() {
                if let Some(sep) = &args.flag_sep {
                    for sub_cell in cell.split_str(sep) {
                        let sub_cell = match coerce_cell(sub_cell, args.flag_no_extra) {
                            Some(c) => c,
                            None => continue,
                        };

                        insert(&group, i, sub_cell);
                    }
                } else {
                    let cell = match coerce_cell(cell, args.flag_no_extra) {
                        Some(c) => c,
                        None => continue,
                    };

                    insert(&group, i, cell);
                }
            }
        }

        // Writing output
        for (i, name) in field_names.into_iter().enumerate() {
            for (group, counters) in groups_to_fields_to_counter.iter() {
                let counter = &counters[i];

                let mut total: u64 = 0;

                // NOTE: if the limit is less than half of the dataset, we fallback to a heap
                let items = if args.flag_limit != 0
                    && args.flag_limit < (counter.len() as f64 / 2.0).floor() as usize
                {
                    let mut heap: FixedReverseHeap<(u64, Reverse<&ValueKey>)> =
                        FixedReverseHeap::with_capacity(args.flag_limit);

                    for (value, count) in counter {
                        total += count;

                        heap.push((*count, Reverse(value)));
                    }

                    heap.into_sorted_vec()
                        .into_iter()
                        .map(|(count, Reverse(value))| (value, count))
                        .collect()
                } else {
                    let mut items = counter
                        .iter()
                        .map(|(v, c)| (v, *c))
                        .inspect(|(_, c)| total += c)
                        .collect::<Vec<_>>();

                    if args.flag_parallel {
                        items.par_sort_unstable_by(|a, b| {
                            a.1.cmp(&b.1).reverse().then_with(|| a.0.cmp(b.0))
                        });
                    } else {
                        items.sort_unstable_by(|a, b| {
                            a.1.cmp(&b.1).reverse().then_with(|| a.0.cmp(b.0))
                        });
                    }

                    if args.flag_limit != 0 {
                        items.truncate(args.flag_limit);
                    }

                    items
                };

                let mut emitted: u64 = 0;

                for (value, count) in items {
                    if let Some(threshold) = args.flag_threshold {
                        if count < threshold {
                            break;
                        }
                    }

                    emitted += count;

                    record.clear();
                    record.push_field(&name);

                    for cell in group {
                        record.push_field(cell);
                    }

                    record.push_field(value);
                    record.push_field(count.to_string().as_bytes());
                    wtr.write_byte_record(&record)?;
                }

                let remaining = total - emitted;

                if !args.flag_no_extra && remaining > 0 {
                    record.clear();
                    record.push_field(&name);

                    for cell in group {
                        record.push_field(cell);
                    }

                    record.push_field(b"<rest>");
                    record.push_field(remaining.to_string().as_bytes());
                    wtr.write_byte_record(&record)?;
                }
            }
        }
    } else {
        let mut fields: Vec<HashMap<ValueKey, u64>> =
            (0..sel.len()).map(|_| HashMap::new()).collect();

        let output_headers = {
            let mut r = ByteRecord::new();
            r.push_field(b"field");
            r.push_field(b"value");
            r.push_field(b"count");
            r
        };

        wtr.write_byte_record(&output_headers)?;

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

        // Aggregating
        while rdr.read_byte_record(&mut record)? {
            for (cell, counter) in sel.select(&record).zip(fields.iter_mut()) {
                if let Some(sep) = &args.flag_sep {
                    for sub_cell in cell.split_str(sep) {
                        let sub_cell = match coerce_cell(sub_cell, args.flag_no_extra) {
                            Some(c) => c,
                            None => continue,
                        };

                        counter
                            .entry(sub_cell.to_vec())
                            .and_modify(|count| *count += 1)
                            .or_insert(1);
                    }
                } else {
                    let cell = match coerce_cell(cell, args.flag_no_extra) {
                        Some(c) => c,
                        None => continue,
                    };

                    counter
                        .entry(cell.to_vec())
                        .and_modify(|count| *count += 1)
                        .or_insert(1);
                }
            }
        }

        // Writing output
        for (name, counter) in field_names.into_iter().zip(fields.into_iter()) {
            let mut total: u64 = 0;

            // NOTE: if the limit is less than half of the dataset, we fallback to a heap
            let items = if args.flag_limit != 0
                && args.flag_limit < (counter.len() as f64 / 2.0).floor() as usize
            {
                let mut heap: FixedReverseHeap<(u64, Reverse<ValueKey>)> =
                    FixedReverseHeap::with_capacity(args.flag_limit);

                for (value, count) in counter {
                    total += count;

                    heap.push((count, Reverse(value)));
                }

                heap.into_sorted_vec()
                    .into_iter()
                    .map(|(count, Reverse(value))| (value, count))
                    .collect()
            } else {
                let mut items = counter
                    .into_iter()
                    .inspect(|(_, c)| total += c)
                    .collect::<Vec<_>>();

                if args.flag_parallel {
                    items.par_sort_unstable_by(|a, b| {
                        a.1.cmp(&b.1).reverse().then_with(|| a.0.cmp(&b.0))
                    });
                } else {
                    items.sort_unstable_by(|a, b| {
                        a.1.cmp(&b.1).reverse().then_with(|| a.0.cmp(&b.0))
                    });
                }

                if args.flag_limit != 0 {
                    items.truncate(args.flag_limit);
                }

                items
            };

            let mut emitted: u64 = 0;

            for (value, count) in items {
                if let Some(threshold) = args.flag_threshold {
                    if count < threshold {
                        break;
                    }
                }

                emitted += count;

                record.clear();
                record.push_field(&name);
                record.push_field(&value);
                record.push_field(count.to_string().as_bytes());
                wtr.write_byte_record(&record)?;
            }

            let remaining = total - emitted;

            if !args.flag_no_extra && remaining > 0 {
                record.clear();
                record.push_field(&name);
                record.push_field(b"<rest>");
                record.push_field(remaining.to_string().as_bytes());
                wtr.write_byte_record(&record)?;
            }
        }
    }

    Ok(wtr.flush()?)
}