1#[cfg(feature = "sqlite")]
2use crate::database::SQLiteQueryBuilder;
3use nu_engine::command_prelude::*;
4use nu_protocol::{
5 DeprecationEntry, DeprecationType, PipelineIterator, ReportMode, ast::PathMember,
6 casing::Casing, shell_error::generic::GenericError,
7};
8use std::collections::BTreeSet;
9
10#[derive(Clone)]
11pub struct Select;
12
13impl Command for Select {
14 fn name(&self) -> &str {
15 "select"
16 }
17
18 fn signature(&self) -> Signature {
20 Signature::build("select")
21 .input_output_types(vec![
22 (Type::record(), Type::record()),
23 (Type::table(), Type::table()),
24 (Type::List(Box::new(Type::Any)), Type::Any),
25 #[cfg(feature = "sqlite")]
26 (
27 Type::Custom("SQLiteQueryBuilder".into()),
28 Type::Custom("SQLiteQueryBuilder".into()),
29 ),
30 ])
31 .switch(
32 "optional",
33 "Make all cell path members optional (returns `null` for missing values).",
34 Some('o'),
35 )
36 .switch(
37 "ignore-case",
38 "Make all cell path members case insensitive.",
39 None,
40 )
41 .switch(
42 "ignore-errors",
43 "Ignore missing data (make all cell path members optional) (deprecated).",
44 Some('i'),
45 )
46 .rest(
47 "rest",
48 SyntaxShape::CellPath,
49 "The columns to select from the table.",
50 )
51 .allow_variants_without_examples(true)
52 .category(Category::Filters)
53 }
54
55 fn description(&self) -> &str {
56 "Select only these columns or rows from the input. Opposite of `reject`."
57 }
58
59 fn extra_description(&self) -> &str {
60 "This differs from `get` in that, rather than accessing the given value in the data structure,
61it removes all non-selected values from the structure. Hence, using `select` on a table will
62produce a table, a list will produce a list, and a record will produce a record."
63 }
64
65 fn search_terms(&self) -> Vec<&str> {
66 vec!["pick", "choose", "get", "retain"]
67 }
68
69 fn run(
70 &self,
71 engine_state: &EngineState,
72 stack: &mut Stack,
73 call: &Call,
74 input: PipelineData,
75 ) -> Result<PipelineData, ShellError> {
76 let columns: Vec<Value> = call.rest(engine_state, stack, 0)?;
77 let mut new_columns: Vec<CellPath> = vec![];
78 for col_val in columns {
79 let col_span = col_val.span();
80 match col_val {
81 Value::CellPath { val, .. } => {
82 new_columns.push(val);
83 }
84 Value::String { val, .. } => {
85 let cv = CellPath {
86 members: vec![PathMember::String {
87 val,
88 span: col_span,
89 optional: false,
90 casing: Casing::Sensitive,
91 }],
92 };
93 new_columns.push(cv);
94 }
95 Value::Int { val, .. } => {
96 if val < 0 {
97 return Err(ShellError::CantConvert {
98 to_type: "cell path".into(),
99 from_type: "negative number".into(),
100 span: col_span,
101 help: None,
102 });
103 }
104 let cv = CellPath {
105 members: vec![PathMember::Int {
106 val: val as usize,
107 span: col_span,
108 optional: false,
109 }],
110 };
111 new_columns.push(cv);
112 }
113 x => {
114 return Err(ShellError::CantConvert {
115 to_type: "cell path".into(),
116 from_type: x.get_type().to_string(),
117 span: x.span(),
118 help: None,
119 });
120 }
121 }
122 }
123 let optional = call.has_flag(engine_state, stack, "optional")?
124 || call.has_flag(engine_state, stack, "ignore-errors")?;
125 let ignore_case = call.has_flag(engine_state, stack, "ignore-case")?;
126 let span = call.head;
127
128 if optional {
129 for cell_path in &mut new_columns {
130 cell_path.make_optional();
131 }
132 }
133
134 if ignore_case {
135 for cell_path in &mut new_columns {
136 cell_path.make_insensitive();
137 }
138 }
139
140 select(engine_state, span, new_columns, input)
141 }
142
143 fn deprecation_info(&self) -> Vec<DeprecationEntry> {
144 vec![DeprecationEntry {
145 ty: DeprecationType::Flag("ignore-errors".into()),
146 report_mode: ReportMode::FirstUse,
147 since: Some("0.106.0".into()),
148 expected_removal: None,
149 help: Some(
150 "This flag has been renamed to `--optional (-o)` to better reflect its behavior."
151 .into(),
152 ),
153 }]
154 }
155
156 fn examples(&self) -> Vec<Example<'_>> {
157 vec![
158 Example {
159 description: "Select a column in a table.",
160 example: "[{a: a b: b}] | select a",
161 result: Some(Value::test_list(vec![Value::test_record(record! {
162 "a" => Value::test_string("a")
163 })])),
164 },
165 Example {
166 description: "Select a column even if some rows are missing that column.",
167 example: "[{a: a0 b: b0} {b: b1}] | select -o a",
168 result: Some(Value::test_list(vec![
169 Value::test_record(record! {
170 "a" => Value::test_string("a0")
171 }),
172 Value::test_record(record! {
173 "a" => Value::test_nothing()
174 }),
175 ])),
176 },
177 Example {
178 description: "Select a field in a record.",
179 example: "{a: a b: b} | select a",
180 result: Some(Value::test_record(record! {
181 "a" => Value::test_string("a")
182 })),
183 },
184 Example {
185 description: "Select just the `name` column.",
186 example: "ls | select name",
187 result: None,
188 },
189 Example {
190 description: "Select the first four rows (this is the same as `first 4`).",
191 example: "ls | select 0 1 2 3",
192 result: None,
193 },
194 Example {
195 description: "Select multiple columns.",
196 example: "[[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select name type",
197 result: Some(Value::test_list(vec![
198 Value::test_record(record! {
199 "name" => Value::test_string("Cargo.toml"),
200 "type" => Value::test_string("toml"),
201 }),
202 Value::test_record(record! {
203 "name" => Value::test_string("Cargo.lock"),
204 "type" => Value::test_string("toml")
205 }),
206 ])),
207 },
208 Example {
209 description: "Select multiple columns by spreading a list.",
210 example: "let cols = [name type]; [[name type size]; [Cargo.toml toml 1kb] [Cargo.lock toml 2kb]] | select ...$cols",
211 result: Some(Value::test_list(vec![
212 Value::test_record(record! {
213 "name" => Value::test_string("Cargo.toml"),
214 "type" => Value::test_string("toml")
215 }),
216 Value::test_record(record! {
217 "name" => Value::test_string("Cargo.lock"),
218 "type" => Value::test_string("toml")
219 }),
220 ])),
221 },
222 ]
223 }
224}
225
226fn select(
227 engine_state: &EngineState,
228 call_span: Span,
229 columns: Vec<CellPath>,
230 mut input: PipelineData,
231) -> Result<PipelineData, ShellError> {
232 let mut unique_rows: BTreeSet<usize> = BTreeSet::new();
233
234 let mut new_columns = vec![];
235
236 for column in columns {
237 let CellPath { ref members } = column;
238 match members.first() {
239 Some(PathMember::Int { val, span, .. }) => {
240 if members.len() > 1 {
241 return Err(ShellError::Generic(GenericError::new(
242 "Select only allows row numbers for rows",
243 "extra after row number",
244 *span,
245 )));
246 }
247 unique_rows.insert(*val);
248 }
249 _ => {
250 if !new_columns.contains(&column) {
251 new_columns.push(column)
252 }
253 }
254 };
255 }
256 let columns = new_columns;
257
258 let input = if !unique_rows.is_empty() {
259 let metadata = input.take_metadata();
260 let pipeline_iter: PipelineIterator = input.into_iter();
261
262 NthIterator {
263 input: pipeline_iter,
264 rows: unique_rows.into_iter().peekable(),
265 current: 0,
266 }
267 .into_pipeline_data_with_metadata(
268 call_span,
269 engine_state.signals().clone(),
270 metadata,
271 )
272 } else {
273 input
274 };
275
276 #[cfg(feature = "sqlite")]
277 if let PipelineData::Value(Value::Custom { val, .. }, ..) = &input
279 && let Some(table) = val.as_any().downcast_ref::<SQLiteQueryBuilder>()
280 {
281 let select_columns: Option<Vec<String>> = columns
284 .iter()
285 .map(|column| match column.members.as_slice() {
286 [PathMember::String { val, .. }] => Some(val.clone()),
287 _ => None,
288 })
289 .collect();
290
291 if let Some(select_columns) = select_columns.filter(|selected| !selected.is_empty())
292 && let Some(new_table) = table.project_output_columns(&select_columns)
293 {
294 return Ok(Value::custom(Box::new(new_table), call_span).into_pipeline_data());
295 }
296 }
297
298 match input {
299 PipelineData::Value(v, metadata, ..) => {
300 let span = v.span();
301 match v {
302 Value::List {
303 vals: input_vals, ..
304 } => Ok(input_vals
305 .into_iter()
306 .map(move |input_val| {
307 if !columns.is_empty() {
308 let mut record = Record::new();
309 for path in &columns {
310 match input_val.follow_cell_path(&path.members) {
311 Ok(fetcher) => {
312 record.push(path.to_column_name(), fetcher.into_owned());
313 }
314 Err(e) => return Value::error(e, call_span),
315 }
316 }
317
318 Value::record(record, span)
319 } else {
320 input_val.clone()
321 }
322 })
323 .into_pipeline_data_with_metadata(
324 call_span,
325 engine_state.signals().clone(),
326 metadata,
327 )),
328 _ => {
329 if !columns.is_empty() {
330 let mut record = Record::new();
331
332 for cell_path in columns {
333 let result = v.follow_cell_path(&cell_path.members)?;
334 record.push(cell_path.to_column_name(), result.into_owned());
335 }
336
337 Ok(Value::record(record, call_span)
338 .into_pipeline_data_with_metadata(metadata))
339 } else {
340 Ok(v.into_pipeline_data_with_metadata(metadata))
341 }
342 }
343 }
344 }
345 PipelineData::ListStream(stream, metadata, ..) => Ok(stream
346 .map(move |x| {
347 if !columns.is_empty() {
348 let mut record = Record::new();
349 for path in &columns {
350 match x.follow_cell_path(&path.members) {
351 Ok(value) => {
352 record.push(path.to_column_name(), value.into_owned());
353 }
354 Err(e) => return Value::error(e, call_span),
355 }
356 }
357 Value::record(record, call_span)
358 } else {
359 x
360 }
361 })
362 .into_pipeline_data_with_metadata(call_span, engine_state.signals().clone(), metadata)),
363 _ => Ok(PipelineData::empty()),
364 }
365}
366
367struct NthIterator {
368 input: PipelineIterator,
369 rows: std::iter::Peekable<std::collections::btree_set::IntoIter<usize>>,
370 current: usize,
371}
372
373impl Iterator for NthIterator {
374 type Item = Value;
375
376 fn next(&mut self) -> Option<Self::Item> {
377 loop {
378 if let Some(row) = self.rows.peek() {
379 if self.current == *row {
380 self.rows.next();
381 self.current += 1;
382 return self.input.next();
383 } else {
384 self.current += 1;
385 let _ = self.input.next()?;
386 continue;
387 }
388 } else {
389 return None;
390 }
391 }
392 }
393}
394
395#[cfg(test)]
396mod tests {
397 use super::*;
398
399 #[test]
400 fn test_examples() -> nu_test_support::Result {
401 nu_test_support::test().examples(Select)
402 }
403}