fastmash 0.5.0

Fast command-line statistics on text data, compatible with GNU datamash
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
//! Ordered Command preparation with private payloads for existing runners.
//! Cases certify completed preparation; startup and Input checks can still fail.
use super::{
    Failure, Kind, binding, failure, grammar,
    options::{Options, Setting},
    projected_sort,
    records::Separator,
    sort_route, unsupported,
};

#[derive(Default)]
pub(super) struct Controls {
    calculation: Option<&'static [u8]>,
    selection: Option<&'static [u8]>,
    comparison: Option<&'static [u8]>,
    csv_input: Option<&'static [u8]>,
}

impl Controls {
    pub(super) fn record(&mut self, setting: Setting, spelling: &'static [u8]) {
        let (calculation, selection, comparison) = match setting {
            Setting::Full => (true, false, true),
            Setting::TableOnly | Setting::Filler | Setting::Collapse | Setting::Seed => {
                (true, true, true)
            }
            Setting::ResultName | Setting::Format | Setting::Round => (true, true, false),
            Setting::Vnlog | Setting::Unavailable => (false, true, true),
            Setting::HeaderOut
            | Setting::CsvOut
            | Setting::CsvIn
            | Setting::Csv
            | Setting::Na
            | Setting::Sort
            | Setting::IgnoreCase
            | Setting::Group
            | Setting::Output => (true, false, false),
            Setting::ZeroTerminated
            | Setting::Input
            | Setting::Whitespace
            | Setting::HeaderIn
            | Setting::Headers
            | Setting::Comments
            | Setting::Color
            | Setting::NoColor
            | Setting::Help
            | Setting::Version => (false, false, false),
        };
        if calculation {
            self.calculation.get_or_insert(spelling);
        }
        if selection {
            self.selection.get_or_insert(spelling);
        }
        if comparison {
            self.comparison.get_or_insert(spelling);
        }
    }
    pub(super) fn record_unavailable(&mut self, spelling: &'static [u8]) {
        self.calculation.get_or_insert(spelling);
    }
    pub(super) fn record_csv_input(&mut self, spelling: &'static [u8]) {
        self.csv_input.get_or_insert(spelling);
    }
}

fn validate_comparison(options: &Options) -> Result<(), Failure> {
    if let Some(option) = options.controls.comparison {
        return Err(unsupported(&format!(
            "compare does not support --{}",
            String::from_utf8_lossy(option)
        )));
    }
    Ok(())
}
fn validate_selection(options: &Options) -> Result<(), Failure> {
    if let Some(option) = options.controls.selection {
        return Err(unsupported(&format!(
            "Top-N selection does not support --{}",
            String::from_utf8_lossy(option)
        )));
    }
    Ok(())
}

fn validate_csv(options: &Options, mode: super::grammar::Mode) -> Result<(), Failure> {
    if !options.csv_in && !options.csv_out {
        return Ok(());
    }
    if options.csv_out && options.supplied_output {
        return Err(failure(
            b"CSV output conflicts with --output-delimiter\n".to_vec(),
        ));
    }
    if options.record_end == 0 {
        return Err(failure(b"CSV conflicts with --zero-terminated\n".to_vec()));
    }
    if options.vnlog {
        return Err(failure(b"CSV conflicts with --vnlog\n".to_vec()));
    }
    if options.csv_in
        && let Some(option) = options.controls.csv_input
    {
        return Err(failure(
            format!(
                "CSV input conflicts with --{}\n",
                String::from_utf8_lossy(option)
            )
            .into_bytes(),
        ));
    }
    if !matches!(
        mode,
        super::grammar::Mode::Aggregate
            | super::grammar::Mode::Line
            | super::grammar::Mode::Select(_)
            | super::grammar::Mode::Compare
    ) {
        return Err(unsupported(
            "CSV is supported only for aggregate and per-row operations",
        ));
    }
    Ok(())
}

fn validate_health(options: &Options) -> Result<(), Failure> {
    if let Some(option) = options.controls.calculation {
        return Err(failure(
            format!(
                "health does not support calculation option '--{}'\n",
                String::from_utf8_lossy(option)
            )
            .into_bytes(),
        ));
    }
    if options.vnlog && options.input != Separator::Whitespace {
        return Err(failure(
            b"vnlog processing is whitespace-delimited\n".to_vec(),
        ));
    }
    Ok(())
}

fn validate_annotation(options: &Options) -> Result<(), Failure> {
    if !options.vnlog {
        return Ok(());
    }
    let message: Option<&[u8]> = if options.filler.as_ref() != b"-" {
        Some(b"vnlog processing always uses '-' for empty fields\n")
    } else if options.input != Separator::Whitespace {
        Some(b"vnlog processing is whitespace-delimited\n")
    } else if options.output != b' ' {
        Some(b"vnlog processing always uses ' ' to separate output fields\n")
    } else if options.explicit_output {
        Some(b"vnlog processing always uses the default output delimiter\n")
    } else if options.record_end != b'\n' {
        Some(b"vnlog processing always uses '\\n' to terminate output lines\n")
    } else {
        None
    };
    message.map_or(Ok(()), |message| Err(failure(message.to_vec())))
}

fn require_header(options: &Options, named: bool) -> Result<(), Failure> {
    if named && !options.header_in {
        return Err(failure(
            b"-H or --header-in must be used with named columns\n".to_vec(),
        ));
    }
    Ok(())
}

fn validate_comparison_request(
    command: &grammar::Command,
    options: &Options,
) -> Result<(), Failure> {
    if command.operations.iter().any(|request| {
        request.kind.is_line()
            || matches!(
                request.kind,
                Kind::First | Kind::Last | Kind::Rand | Kind::Unique | Kind::Collapse
            )
    }) {
        return Err(unsupported(
            "compare requires numerical aggregate operations",
        ));
    }
    require_header(
        options,
        !options.header_in
            && (command
                .keys
                .iter()
                .any(|field| matches!(field, grammar::Field::Name(_)))
                || command
                    .operations
                    .iter()
                    .any(|request| match &request.selector {
                        grammar::Selector::Single(field) => {
                            matches!(field, grammar::Field::Name(_))
                        }
                        grammar::Selector::Pair { left, right } => {
                            matches!(left, grammar::Field::Name(_))
                                || matches!(right, grammar::Field::Name(_))
                        }
                    })),
    )?;
    options.result_names.validate(
        grammar::Mode::Aggregate,
        options.explicit_header_out,
        command.operations.len(),
        options.output,
        options.record_end,
        options.csv_out,
    )?;
    Ok(())
}

pub(super) enum Prepared<'o, 'p> {
    Ordinary(Ordinary<'o, 'p>),
    Selection(Selection<'o>),
    Comparison(Comparison<'o>),
    Health(Health<'o>),
    Table(Table<'o>),
}

pub(super) struct Ordinary<'o, 'p> {
    options: &'o Options,
    binding: super::binding::Binding<'p>,
    needs: super::operation_set::Needs,
    route: Option<super::sort_route::Route>,
}
impl<'o, 'p> Ordinary<'o, 'p> {
    pub(super) fn into_parts(
        self,
    ) -> (
        &'o Options,
        super::binding::Binding<'p>,
        super::operation_set::Needs,
        Option<super::sort_route::Route>,
    ) {
        (self.options, self.binding, self.needs, self.route)
    }
}

pub(super) struct Selection<'o> {
    options: &'o Options,
    request: super::selection::Selection,
    binding: super::binding::Selection,
}
impl<'o> Selection<'o> {
    pub(super) fn into_parts(
        self,
    ) -> (
        &'o Options,
        super::selection::Selection,
        super::binding::Selection,
    ) {
        (self.options, self.request, self.binding)
    }
}

pub(super) struct Comparison<'o> {
    options: &'o Options,
    request: super::comparison::Request,
    operations: Vec<grammar::Request>,
    keys: Vec<grammar::Field>,
}
impl<'o> Comparison<'o> {
    pub(super) fn into_parts(
        self,
    ) -> (
        &'o Options,
        super::comparison::Request,
        Vec<grammar::Request>,
        Vec<grammar::Field>,
    ) {
        (self.options, self.request, self.operations, self.keys)
    }
}

pub(super) struct Health<'o> {
    options: &'o Options,
}
impl<'o> Health<'o> {
    pub(super) fn into_options(self) -> &'o Options {
        self.options
    }
}

pub(super) struct Table<'o> {
    options: &'o Options,
    mode: grammar::Mode,
    keys: Vec<grammar::Field>,
}
impl<'o> Table<'o> {
    pub(super) fn into_parts(self) -> (&'o Options, grammar::Mode, Vec<grammar::Field>) {
        (self.options, self.mode, self.keys)
    }
}

/// Completes the existing pre-startup checks without acquiring any transport.
#[cfg_attr(feature = "hotpath", hotpath::measure)]
pub(super) fn prepare<'o, 'p>(
    options: &'o mut Options,
    command: super::grammar::Command,
    environment: &super::Environment,
    name: &'p [u8],
) -> Result<Prepared<'o, 'p>, Failure> {
    let policy = environment.locale;
    match command.mode {
        grammar::Mode::Compare => {
            validate_csv(options, command.mode)?;
            validate_comparison(options)?;
            validate_comparison_request(&command, options)?;
            policy.numbers()?;
            if options.ignore_case {
                policy.case_folding()?;
            }
            return Ok(Prepared::Comparison(Comparison {
                options,
                request: command.comparison.expect("comparison request"),
                operations: command.operations,
                keys: command.keys,
            }));
        }
        grammar::Mode::Health => {
            validate_health(options)?;
            policy.numbers()?;
            return Ok(Prepared::Health(Health { options }));
        }
        grammar::Mode::Reverse
        | grammar::Mode::Transpose
        | grammar::Mode::Noop
        | grammar::Mode::Check { .. }
        | grammar::Mode::Dedup => {
            prepare_formats_and_mode(options, &command, policy)?;
            validate_annotation(options)?;
            return Ok(Prepared::Table(Table {
                options,
                mode: command.mode,
                keys: command.keys,
            }));
        }
        grammar::Mode::Select(request) => {
            let field = command.ranking.expect("selection has one ranking Field");
            require_header(
                options,
                matches!(field, grammar::Field::Name(_))
                    || command
                        .keys
                        .iter()
                        .any(|key| matches!(key, grammar::Field::Name(_))),
            )?;
            validate_csv(options, command.mode)?;
            validate_selection(options)?;
            if options.ignore_case && !command.keys.is_empty() {
                policy.case_folding()?;
            }
            if options.sort && !command.keys.is_empty() {
                policy.sorting()?;
                projected_sort::admit_memory(options.sort_memory.as_deref())?;
            }
            let binding = binding::Selection::new(field, command.keys)?;
            policy.numbers()?;

            return Ok(Prepared::Selection(Selection {
                options,
                request,
                binding,
            }));
        }
        grammar::Mode::Aggregate | grammar::Mode::Line | grammar::Mode::Crosstab => {}
    }
    prepare_formats_and_mode(options, &command, policy)?;
    let binding = binding::Binding::new(name, command.operations, command.keys)?;
    let needs = binding
        .operations
        .needs(options.presentation.spec.is_some());
    if needs.numeric_locale {
        policy.numbers()?;
    }
    require_header(options, binding.names_fields())?;
    validate_annotation(options)?;
    // The Sort route of a sorted Command with Grouping keys.
    let route = if options.sort && !binding.keys.is_empty() {
        policy.sorting()?;
        if options.csv_in {
            // Decoded Records use their own lossless retention route, covering
            // every admitted Operation without a text-sort fallback.
            None
        } else {
            let native = binding.operations.native_sort();
            let facts = sort_route::Facts {
                hash_eligible: projected_sort::hash_eligible(options, &binding.operations),
                grouping: projected_sort::grouping(environment.grouping.as_deref())?,
                language: policy.language(),
                packed: projected_sort::packed(options, native),
                hold: sort_route::Hold::setting(environment.pipe_grouping.as_deref())?,
            };
            Some(sort_route::plan(facts))
        }
    } else {
        None
    };

    Ok(Prepared::Ordinary(Ordinary {
        options,
        binding,
        needs,
        route,
    }))
}

/// Common format and Mode prefix, before family-specific fallible preparation.
fn prepare_formats_and_mode(
    options: &mut Options,
    command: &super::grammar::Command,
    policy: super::locale::Policy,
) -> Result<(), Failure> {
    validate_csv(options, command.mode)?;
    options.result_names.validate(
        command.mode,
        options.explicit_header_out,
        command.operations.len(),
        options.output,
        options.record_end,
        options.csv_out,
    )?;
    options.linewise = command.mode == grammar::Mode::Line;
    options.crosstab = command.mode == grammar::Mode::Crosstab;
    // GNU's rmdup compares keys exactly; -i only changes its sort order.
    if options.ignore_case
        && match command.mode {
            grammar::Mode::Aggregate | grammar::Mode::Crosstab => true,
            grammar::Mode::Dedup => options.sort,
            _ => false,
        }
    {
        policy.case_folding()?;
    }

    Ok(())
}