mirror of
https://github.com/nushell/nushell
synced 2025-01-13 13:49:21 +00:00
Suggest a column name in case one unknown column is supplied.
This commit is contained in:
parent
0611f56776
commit
f1630da2cc
4 changed files with 62 additions and 6 deletions
|
@ -249,7 +249,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
|
||||||
| command | description |
|
| command | description |
|
||||||
| ------------- | ------------- |
|
| ------------- | ------------- |
|
||||||
| add column-or-column-path value | Add a new column to the table |
|
| add column-or-column-path value | Add a new column to the table |
|
||||||
| count | Show the total number of cells |
|
| count | Show the total number of rows |
|
||||||
| edit column-or-column-path value | Edit an existing column to have a new value |
|
| edit column-or-column-path value | Edit an existing column to have a new value |
|
||||||
| embed column | Creates a new table of one column with the given name, and places the current table inside of it |
|
| embed column | Creates a new table of one column with the given name, and places the current table inside of it |
|
||||||
| first amount | Show only the first number of rows |
|
| first amount | Show only the first number of rows |
|
||||||
|
|
|
@ -20,7 +20,7 @@ impl WholeStreamCommand for Count {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Show the total number of cells."
|
"Show the total number of rows."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
|
|
|
@ -40,10 +40,41 @@ fn group_by(
|
||||||
let values: Vec<Tagged<Value>> = input.values.collect().await;
|
let values: Vec<Tagged<Value>> = input.values.collect().await;
|
||||||
let mut groups = indexmap::IndexMap::new();
|
let mut groups = indexmap::IndexMap::new();
|
||||||
|
|
||||||
for row in values {
|
for value in values {
|
||||||
let key = row.get_data_by_key(&column_name.item).unwrap().as_string()?;
|
let group_key = value.get_data_by_key(&column_name.item);
|
||||||
let mut group = groups.entry(key).or_insert(vec![]);
|
|
||||||
group.push(row);
|
if group_key.is_none() {
|
||||||
|
|
||||||
|
let possibilities = value.data_descriptors();
|
||||||
|
|
||||||
|
let mut possible_matches: Vec<_> = possibilities
|
||||||
|
.iter()
|
||||||
|
.map(|x| (natural::distance::levenshtein_distance(x, &column_name.item), x))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
possible_matches.sort();
|
||||||
|
|
||||||
|
let err = {
|
||||||
|
if possible_matches.len() > 0 {
|
||||||
|
ShellError::labeled_error(
|
||||||
|
"Unknown column",
|
||||||
|
format!("did you mean '{}'?", possible_matches[0].1),
|
||||||
|
&column_name.tag,)
|
||||||
|
} else {
|
||||||
|
ShellError::labeled_error(
|
||||||
|
"Unknown column",
|
||||||
|
"row does not contain this column",
|
||||||
|
&column_name.tag,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
yield Err(err)
|
||||||
|
} else {
|
||||||
|
let group_key = group_key.unwrap().as_string()?;
|
||||||
|
let mut group = groups.entry(group_key).or_insert(vec![]);
|
||||||
|
group.push(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut out = TaggedDictBuilder::new(name.clone());
|
let mut out = TaggedDictBuilder::new(name.clone());
|
||||||
|
|
|
@ -31,6 +31,31 @@ fn group_by() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn group_by_errors_if_unknown_column_name() {
|
||||||
|
Playground::setup("group_by_test_2", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
|
"los_tres_caballeros.csv",
|
||||||
|
r#"
|
||||||
|
first_name,last_name,rusty_luck,type
|
||||||
|
Andrés,Robalino,1,A
|
||||||
|
Jonathan,Turner,1,B
|
||||||
|
Yehuda,Katz,1,A
|
||||||
|
"#,
|
||||||
|
)]);
|
||||||
|
|
||||||
|
let actual = nu_error!(
|
||||||
|
cwd: dirs.test(), h::pipeline(
|
||||||
|
r#"
|
||||||
|
open los_tres_caballeros.csv
|
||||||
|
| group-by ttype
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(actual.contains("Unknown column"));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn first_gets_first_rows_by_amount() {
|
fn first_gets_first_rows_by_amount() {
|
||||||
Playground::setup("first_test_1", |dirs, sandbox| {
|
Playground::setup("first_test_1", |dirs, sandbox| {
|
||||||
|
|
Loading…
Reference in a new issue