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
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{
ColumnPath, PathMember, Primitive, Signature, SyntaxShape, TaggedDictBuilder,
UnspannedPathMember, UntaggedValue, Value,
};
use nu_value_ext::{as_string, get_data_by_column_path};
pub struct Command;
impl WholeStreamCommand for Command {
fn name(&self) -> &str {
"select"
}
fn signature(&self) -> Signature {
Signature::build("select").rest(
SyntaxShape::ColumnPath,
"the columns to select from the table",
)
}
fn usage(&self) -> &str {
"Down-select table to only these columns."
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let columns: Vec<ColumnPath> = args.rest(0)?;
let input = args.input;
let name = args.call_info.name_tag;
select(name, columns, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Select just the name column",
example: "ls | select name",
result: None,
},
Example {
description: "Select the name and size columns",
example: "ls | select name size",
result: None,
},
]
}
}
fn select(
name: Tag,
columns: Vec<ColumnPath>,
input: InputStream,
) -> Result<OutputStream, ShellError> {
if columns.is_empty() {
return Err(ShellError::labeled_error(
"Select requires columns to select",
"needs parameter",
name,
));
}
let mut bring_back: indexmap::IndexMap<String, Vec<Value>> = indexmap::IndexMap::new();
for value in input {
for path in &columns {
let fetcher = get_data_by_column_path(
&value,
path,
move |obj_source, path_member_tried, error| {
if let PathMember {
unspanned: UnspannedPathMember::String(column),
..
} = path_member_tried
{
return ShellError::labeled_error_with_secondary(
"No data to fetch.",
format!("Couldn't select column \"{}\"", column),
path_member_tried.span,
"How about exploring it with \"get\"? Check the input is appropriate originating from here",
obj_source.tag.span);
}
error
},
);
let field = path.clone();
let key = as_string(
&UntaggedValue::Primitive(Primitive::ColumnPath(field.clone()))
.into_untagged_value(),
)?;
match fetcher {
Ok(results) => match results.value {
UntaggedValue::Table(records) => {
for x in records {
let mut out = TaggedDictBuilder::new(name.clone());
out.insert_untagged(&key, x.value.clone());
let group = bring_back.entry(key.clone()).or_insert(vec![]);
group.push(out.into_value());
}
}
x => {
let mut out = TaggedDictBuilder::new(name.clone());
out.insert_untagged(&key, x.clone());
let group = bring_back.entry(key.clone()).or_insert(vec![]);
group.push(out.into_value());
}
},
Err(reason) => {
let strict: Option<bool> = None;
if strict.is_some() {
return Err(reason);
}
bring_back
.entry(key.clone())
.or_insert(vec![])
.push(UntaggedValue::nothing().into());
}
}
}
}
let mut max = 0;
if let Some(max_column) = bring_back.values().max() {
max = max_column.len();
}
let keys = bring_back.keys().cloned().collect::<Vec<String>>();
Ok(((0..max).map(move |current| {
let mut out = TaggedDictBuilder::new(name.clone());
for k in &keys {
let new_key = k.replace(".", "_");
let nothing = UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value();
let subsets = bring_back.get(k);
match subsets {
Some(set) => match set.get(current) {
Some(row) => out.insert_untagged(new_key, row.get_data(k).borrow().clone()),
None => out.insert_untagged(new_key, nothing.clone()),
},
None => out.insert_untagged(new_key, nothing.clone()),
}
}
out.into_value()
}))
.into_output_stream())
}
#[cfg(test)]
mod tests {
use nu_protocol::ColumnPath;
use nu_source::Span;
use nu_source::SpannedItem;
use nu_source::Tag;
use nu_stream::InputStream;
use nu_test_support::value::nothing;
use nu_test_support::value::row;
use nu_test_support::value::string;
use super::select;
use super::Command;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(Command {})
}
#[test]
fn select_using_sparse_table() {
let input = vec![
row(indexmap! {"col_foo".into() => string("foo")}),
row(indexmap! {"col_bar".into() => string("bar")}),
row(indexmap! {"col_foo".into() => string("foo")}),
];
let expected = vec![
row(
indexmap! {"col_none".into() => nothing(), "col_foo".into() => string("foo"), "col_bar".into() => nothing()},
),
row(
indexmap! {"col_none".into() => nothing(), "col_foo".into() => nothing(), "col_bar".into() => string("bar")},
),
row(
indexmap! {"col_none".into() => nothing(), "col_foo".into() => string("foo"), "col_bar".into() => nothing()},
),
];
let actual = select(
Tag::unknown(),
vec![
ColumnPath::build(&"col_none".to_string().spanned(Span::unknown())),
ColumnPath::build(&"col_foo".to_string().spanned(Span::unknown())),
ColumnPath::build(&"col_bar".to_string().spanned(Span::unknown())),
],
input.into(),
);
assert_eq!(Ok(expected), actual.map(InputStream::into_vec));
}
}