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
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> {
select(args)
}
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(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let columns: Vec<ColumnPath> = args.rest(0)?;
let input = args.input;
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![]);
}
}
}
}
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())
}