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
pub use super::uniq;
use nu_engine::column::nonexistent_column;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
    record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};

#[derive(Clone)]
pub struct UniqBy;

impl Command for UniqBy {
    fn name(&self) -> &str {
        "uniq-by"
    }

    fn signature(&self) -> Signature {
        Signature::build("uniq-by")
            .input_output_types(vec![
                (Type::Table(vec![]), Type::Table(vec![])),
                (
                    Type::List(Box::new(Type::Any)),
                    Type::List(Box::new(Type::Any)),
                ),
            ])
            .rest("columns", SyntaxShape::Any, "The column(s) to filter by.")
            .switch(
                "count",
                "Return a table containing the distinct input values together with their counts",
                Some('c'),
            )
            .switch(
                "repeated",
                "Return the input values that occur more than once",
                Some('d'),
            )
            .switch(
                "ignore-case",
                "Ignore differences in case when comparing input values",
                Some('i'),
            )
            .switch(
                "unique",
                "Return the input values that occur once only",
                Some('u'),
            )
            .allow_variants_without_examples(true)
            .category(Category::Filters)
    }

    fn usage(&self) -> &str {
        "Return the distinct values in the input by the given column(s)."
    }

    fn search_terms(&self) -> Vec<&str> {
        vec!["distinct", "deduplicate"]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let columns: Vec<String> = call.rest(engine_state, stack, 0)?;

        if columns.is_empty() {
            return Err(ShellError::MissingParameter {
                param_name: "columns".into(),
                span: call.head,
            });
        }

        let metadata = input.metadata();

        let vec: Vec<_> = input.into_iter().collect();
        match validate(&vec, &columns, call.head) {
            Ok(_) => {}
            Err(err) => {
                return Err(err);
            }
        }

        let mapper = Box::new(item_mapper_by_col(columns));

        uniq(engine_state, stack, call, vec, mapper, metadata)
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            description: "Get rows from table filtered by column uniqueness ",
            example: "[[fruit count]; [apple 9] [apple 2] [pear 3] [orange 7]] | uniq-by fruit",
            result: Some(Value::test_list(vec![
                Value::test_record(record! {
                    "fruit" => Value::test_string("apple"),
                    "count" => Value::test_int(9),
                }),
                Value::test_record(record! {
                    "fruit" => Value::test_string("pear"),
                    "count" => Value::test_int(3),
                }),
                Value::test_record(record! {
                    "fruit" => Value::test_string("orange"),
                    "count" => Value::test_int(7),
                }),
            ])),
        }]
    }
}

fn validate(vec: &[Value], columns: &[String], span: Span) -> Result<(), ShellError> {
    let first = vec.first();
    if let Some(v) = first {
        let val_span = v.span();
        if let Value::Record { val: record, .. } = &v {
            if columns.is_empty() {
                // This uses the same format as the 'requires a column name' error in split_by.rs
                return Err(ShellError::GenericError {
                    error: "expected name".into(),
                    msg: "requires a column name to filter table data".into(),
                    span: Some(span),
                    help: None,
                    inner: vec![],
                });
            }

            if let Some(nonexistent) = nonexistent_column(columns, record.columns()) {
                return Err(ShellError::CantFindColumn {
                    col_name: nonexistent,
                    span,
                    src_span: val_span,
                });
            }
        }
    }

    Ok(())
}

fn get_data_by_columns(columns: &[String], item: &Value) -> Vec<Value> {
    columns
        .iter()
        .filter_map(|col| item.get_data_by_key(col))
        .collect::<Vec<_>>()
}

fn item_mapper_by_col(cols: Vec<String>) -> impl Fn(crate::ItemMapperState) -> crate::ValueCounter {
    let columns = cols;

    Box::new(move |ms: crate::ItemMapperState| -> crate::ValueCounter {
        let item_column_values = get_data_by_columns(&columns, &ms.item);

        let col_vals = Value::list(item_column_values, Span::unknown());

        crate::ValueCounter::new_vals_to_compare(ms.item, ms.flag_ignore_case, col_vals, ms.index)
    })
}

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

    #[test]
    fn test_examples() {
        use crate::test_examples;

        test_examples(UniqBy {})
    }
}