2019-10-21 01:42:07 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::prelude::*;
|
2020-09-25 05:44:24 +02:00
|
|
|
use crate::utils::suggestions::suggestions;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-05-18 14:56:01 +02:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::Tagged;
|
2020-06-17 19:33:50 +02:00
|
|
|
use nu_value_ext::as_string;
|
2019-10-21 01:42:07 +02:00
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
pub struct Command;
|
2019-10-21 01:42:07 +02:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2020-10-03 16:06:02 +02:00
|
|
|
pub struct Arguments {
|
2020-07-14 15:45:19 +02:00
|
|
|
grouper: Option<Value>,
|
2019-10-21 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 10:22:52 +02:00
|
|
|
#[async_trait]
|
2020-10-03 16:06:02 +02:00
|
|
|
impl WholeStreamCommand for Command {
|
2019-10-21 01:42:07 +02:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"group-by"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-15 11:16:09 +02:00
|
|
|
Signature::build("group-by").optional(
|
2020-07-14 15:45:19 +02:00
|
|
|
"grouper",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the grouper value to use",
|
2020-05-15 11:16:09 +02:00
|
|
|
)
|
2019-10-21 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-09-18 08:13:53 +02:00
|
|
|
"Create a new table grouped."
|
2019-10-21 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
group_by(args).await
|
2019-10-21 01:42:07 +02:00
|
|
|
}
|
2020-05-12 17:54:29 +02:00
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
#[allow(clippy::unwrap_used)]
|
2020-05-18 14:56:01 +02:00
|
|
|
fn examples(&self) -> Vec<Example> {
|
2020-10-03 16:06:02 +02:00
|
|
|
use nu_data::value::date_naive_from_str as date;
|
|
|
|
|
2020-05-18 14:56:01 +02:00
|
|
|
vec![
|
|
|
|
Example {
|
2020-07-14 15:45:19 +02:00
|
|
|
description: "group items by column named \"type\"",
|
2020-05-18 14:56:01 +02:00
|
|
|
example: r#"ls | group-by type"#,
|
2020-10-03 16:06:02 +02:00
|
|
|
result: Some(vec![UntaggedValue::row(indexmap! {
|
|
|
|
"File".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"name".to_string() => UntaggedValue::string("Andrés.txt").into(),
|
|
|
|
"type".to_string() => UntaggedValue::string("File").into(),
|
|
|
|
"chickens".to_string() => UntaggedValue::int(10).into(),
|
2020-10-20 11:07:13 +02:00
|
|
|
"modified".to_string() => date("2019-07-23".tagged_unknown()).unwrap().into(),
|
2020-10-03 16:06:02 +02:00
|
|
|
}).into(),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"name".to_string() => UntaggedValue::string("Andrés.txt").into(),
|
|
|
|
"type".to_string() => UntaggedValue::string("File").into(),
|
|
|
|
"chickens".to_string() => UntaggedValue::int(20).into(),
|
2020-10-20 11:07:13 +02:00
|
|
|
"modified".to_string() => date("2019-09-24".tagged_unknown()).unwrap().into(),
|
2020-10-03 16:06:02 +02:00
|
|
|
}).into(),
|
|
|
|
]).into(),
|
|
|
|
"Dir".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"name".to_string() => UntaggedValue::string("Jonathan").into(),
|
|
|
|
"type".to_string() => UntaggedValue::string("Dir").into(),
|
|
|
|
"chickens".to_string() => UntaggedValue::int(5).into(),
|
2020-10-20 11:07:13 +02:00
|
|
|
"modified".to_string() => date("2019-07-23".tagged_unknown()).unwrap().into(),
|
2020-10-03 16:06:02 +02:00
|
|
|
}).into(),
|
|
|
|
UntaggedValue::row(indexmap! {
|
|
|
|
"name".to_string() => UntaggedValue::string("Yehuda").into(),
|
|
|
|
"type".to_string() => UntaggedValue::string("Dir").into(),
|
|
|
|
"chickens".to_string() => UntaggedValue::int(4).into(),
|
2020-10-20 11:07:13 +02:00
|
|
|
"modified".to_string() => date("2019-09-24".tagged_unknown()).unwrap().into(),
|
2020-10-03 16:06:02 +02:00
|
|
|
}).into(),
|
|
|
|
]).into(),
|
|
|
|
})
|
|
|
|
.into()]),
|
2020-07-14 15:45:19 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "you can also group by raw values by leaving out the argument",
|
2020-05-18 14:56:01 +02:00
|
|
|
example: "echo [1 3 1 3 2 1 1] | group-by",
|
|
|
|
result: Some(vec![UntaggedValue::row(indexmap! {
|
|
|
|
"1".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
]).into(),
|
|
|
|
|
|
|
|
"3".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::int(3).into(),
|
|
|
|
UntaggedValue::int(3).into(),
|
|
|
|
]).into(),
|
|
|
|
|
|
|
|
"2".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::int(2).into(),
|
|
|
|
]).into(),
|
|
|
|
})
|
|
|
|
.into()]),
|
|
|
|
},
|
2020-07-14 15:45:19 +02:00
|
|
|
Example {
|
2020-10-03 16:06:02 +02:00
|
|
|
description:
|
|
|
|
"use the block form to generate a grouping key when each row gets processed",
|
|
|
|
example: "echo [1 3 1 3 2 1 1] | group-by { = ($it - 1) mod 3 }",
|
|
|
|
result: Some(vec![UntaggedValue::row(indexmap! {
|
|
|
|
"0".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
UntaggedValue::int(1).into(),
|
|
|
|
|
|
|
|
]).into(),
|
|
|
|
"2".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::int(3).into(),
|
|
|
|
UntaggedValue::int(3).into(),
|
|
|
|
]).into(),
|
|
|
|
"1".to_string() => UntaggedValue::Table(vec![
|
|
|
|
UntaggedValue::int(2).into(),
|
|
|
|
]).into(),
|
|
|
|
})
|
|
|
|
.into()]),
|
2020-07-14 15:45:19 +02:00
|
|
|
},
|
2020-05-18 14:56:01 +02:00
|
|
|
]
|
2020-05-12 17:54:29 +02:00
|
|
|
}
|
2019-10-21 01:42:07 +02:00
|
|
|
}
|
|
|
|
|
2020-06-17 19:33:50 +02:00
|
|
|
enum Grouper {
|
|
|
|
ByColumn(Option<Tagged<String>>),
|
2020-07-14 15:45:19 +02:00
|
|
|
ByBlock,
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
pub async fn group_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
let name = args.call_info.name_tag.clone();
|
2020-12-18 08:53:49 +01:00
|
|
|
let context = Arc::new(EvaluationContext::from_raw(&args));
|
|
|
|
let (Arguments { grouper }, input) = args.process().await?;
|
2020-07-14 15:45:19 +02:00
|
|
|
|
2020-06-08 06:48:10 +02:00
|
|
|
let values: Vec<Value> = input.collect().await;
|
2020-07-14 15:45:19 +02:00
|
|
|
let mut keys: Vec<Result<String, ShellError>> = vec![];
|
|
|
|
let mut group_strategy = Grouper::ByColumn(None);
|
|
|
|
|
|
|
|
match grouper {
|
|
|
|
Some(Value {
|
|
|
|
value: UntaggedValue::Block(block_given),
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
let block = Arc::new(block_given);
|
|
|
|
let error_key = "error";
|
|
|
|
|
|
|
|
for value in values.iter() {
|
|
|
|
let run = block.clone();
|
|
|
|
let context = context.clone();
|
|
|
|
|
2020-12-18 08:53:49 +01:00
|
|
|
match crate::commands::each::process_row(run, context, value.clone()).await {
|
2020-07-14 15:45:19 +02:00
|
|
|
Ok(mut s) => {
|
|
|
|
let collection: Vec<Result<ReturnSuccess, ShellError>> =
|
|
|
|
s.drain_vec().await;
|
|
|
|
|
|
|
|
if collection.len() > 1 {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"expected one value from the block",
|
|
|
|
"requires a table with one value for grouping",
|
|
|
|
&name,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = match collection.get(0) {
|
|
|
|
Some(Ok(return_value)) => {
|
|
|
|
return_value.raw_value().unwrap_or_else(|| {
|
|
|
|
UntaggedValue::string(error_key).into_value(&name)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Some(Err(_)) | None => {
|
|
|
|
UntaggedValue::string(error_key).into_value(&name)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
keys.push(as_string(&value));
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
keys.push(Ok(error_key.into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
group_strategy = Grouper::ByBlock;
|
|
|
|
}
|
|
|
|
Some(other) => {
|
|
|
|
group_strategy = Grouper::ByColumn(Some(as_string(&other)?.tagged(&name)));
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2020-06-08 06:48:10 +02:00
|
|
|
|
|
|
|
if values.is_empty() {
|
2020-06-17 19:33:50 +02:00
|
|
|
return Err(ShellError::labeled_error(
|
2020-07-14 15:45:19 +02:00
|
|
|
"expected table from pipeline",
|
2020-06-08 06:48:10 +02:00
|
|
|
"requires a table input",
|
|
|
|
name,
|
2020-06-17 19:33:50 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
let first = values[0].clone();
|
|
|
|
|
|
|
|
let name = if first.tag.anchor().is_some() {
|
|
|
|
first.tag
|
|
|
|
} else {
|
|
|
|
name
|
|
|
|
};
|
|
|
|
|
2020-06-17 19:33:50 +02:00
|
|
|
let values = UntaggedValue::table(&values).into_value(&name);
|
|
|
|
|
2020-08-03 00:34:33 +02:00
|
|
|
let group_value = match group_strategy {
|
2020-07-14 15:45:19 +02:00
|
|
|
Grouper::ByBlock => {
|
|
|
|
let map = keys.clone();
|
|
|
|
|
|
|
|
let block = Box::new(move |idx: usize, row: &Value| match map.get(idx) {
|
|
|
|
Some(Ok(key)) => Ok(key.clone()),
|
|
|
|
Some(Err(reason)) => Err(reason.clone()),
|
|
|
|
None => as_string(row),
|
|
|
|
});
|
|
|
|
|
2020-10-03 16:06:02 +02:00
|
|
|
nu_data::utils::group(&values, &Some(block), name)
|
2020-07-14 15:45:19 +02:00
|
|
|
}
|
2020-10-03 16:06:02 +02:00
|
|
|
Grouper::ByColumn(column_name) => group(&column_name, &values, &name),
|
2020-08-03 00:34:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(OutputStream::one(ReturnSuccess::value(group_value?)))
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
|
|
|
|
2019-10-29 22:04:31 +01:00
|
|
|
pub fn group(
|
2020-06-17 19:33:50 +02:00
|
|
|
column_name: &Option<Tagged<String>>,
|
|
|
|
values: &Value,
|
2019-10-29 22:04:31 +01:00
|
|
|
tag: impl Into<Tag>,
|
2019-11-21 15:33:14 +01:00
|
|
|
) -> Result<Value, ShellError> {
|
2020-06-17 19:33:50 +02:00
|
|
|
let name = tag.into();
|
|
|
|
|
|
|
|
let grouper = if let Some(column_name) = column_name {
|
|
|
|
Grouper::ByColumn(Some(column_name.clone()))
|
|
|
|
} else {
|
|
|
|
Grouper::ByColumn(None)
|
|
|
|
};
|
|
|
|
|
|
|
|
match grouper {
|
|
|
|
Grouper::ByColumn(Some(column_name)) => {
|
2020-07-14 15:45:19 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| {
|
2020-06-17 19:33:50 +02:00
|
|
|
match row.get_data_by_key(column_name.borrow_spanned()) {
|
|
|
|
Some(group_key) => Ok(as_string(&group_key)?),
|
|
|
|
None => Err(suggestions(column_name.borrow_tagged(), &row)),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::group(&values, &Some(block), &name)
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
|
|
|
Grouper::ByColumn(None) => {
|
2020-08-03 19:43:27 +02:00
|
|
|
let block = Box::new(move |_, row: &Value| as_string(row));
|
2020-06-17 19:33:50 +02:00
|
|
|
|
2020-08-18 09:00:02 +02:00
|
|
|
nu_data::utils::group(&values, &Some(block), &name)
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
2020-07-14 15:45:19 +02:00
|
|
|
Grouper::ByBlock => Err(ShellError::unimplemented(
|
|
|
|
"Block not implemented: This should never happen.",
|
|
|
|
)),
|
2020-06-17 19:33:50 +02:00
|
|
|
}
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-06-17 19:33:50 +02:00
|
|
|
use super::group;
|
2020-10-20 11:07:13 +02:00
|
|
|
use nu_data::utils::helpers::committers;
|
2020-01-02 05:02:46 +01:00
|
|
|
use nu_errors::ShellError;
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::*;
|
2020-10-20 11:07:13 +02:00
|
|
|
use nu_test_support::value::{date, int, row, string, table};
|
2019-10-29 22:04:31 +01:00
|
|
|
|
2019-11-12 08:07:43 +01:00
|
|
|
#[test]
|
2020-01-02 05:02:46 +01:00
|
|
|
fn groups_table_by_date_column() -> Result<(), ShellError> {
|
2020-06-17 19:33:50 +02:00
|
|
|
let for_key = Some(String::from("date").tagged_unknown());
|
2020-08-04 00:47:19 +02:00
|
|
|
let sample = table(&committers());
|
2019-10-29 22:04:31 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-06-17 19:33:50 +02:00
|
|
|
group(&for_key, &sample, Tag::unknown())?,
|
2019-10-29 22:04:31 +01:00
|
|
|
row(indexmap! {
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-07-23".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-07-23"), "chickens".into() => int(10) }),
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-07-23"), "chickens".into() => int(5) }),
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-07-23"), "chickens".into() => int(2) })
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-10-10".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-10-10"), "chickens".into() => int(6) }),
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-10-10"), "chickens".into() => int(15) }),
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-10-10"), "chickens".into() => int(30) })
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
2020-08-04 00:47:19 +02:00
|
|
|
"2019-09-24".into() => table(&[
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-09-24"), "chickens".into() => int(20) }),
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-09-24"), "chickens".into() => int(4) }),
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-09-24"), "chickens".into() => int(10) })
|
2019-10-29 22:04:31 +01:00
|
|
|
]),
|
|
|
|
})
|
|
|
|
);
|
2020-01-02 05:02:46 +01:00
|
|
|
|
|
|
|
Ok(())
|
2019-10-29 22:04:31 +01:00
|
|
|
}
|
2019-11-12 08:07:43 +01:00
|
|
|
|
|
|
|
#[test]
|
2020-01-02 05:02:46 +01:00
|
|
|
fn groups_table_by_country_column() -> Result<(), ShellError> {
|
2020-06-17 19:33:50 +02:00
|
|
|
let for_key = Some(String::from("country").tagged_unknown());
|
2020-08-04 00:47:19 +02:00
|
|
|
let sample = table(&committers());
|
2019-11-12 08:07:43 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
2020-06-17 19:33:50 +02:00
|
|
|
group(&for_key, &sample, Tag::unknown())?,
|
2019-11-12 08:07:43 +01:00
|
|
|
row(indexmap! {
|
2019-12-31 08:36:08 +01:00
|
|
|
"EC".into() => table(&[
|
2020-08-04 00:47:19 +02:00
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-07-23"), "chickens".into() => int(10) }),
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-09-24"), "chickens".into() => int(20) }),
|
|
|
|
row(indexmap!{"name".into() => string("AR"), "country".into() => string("EC"), "date".into() => date("2019-10-10"), "chickens".into() => int(30) })
|
2019-11-12 08:07:43 +01:00
|
|
|
]),
|
2019-12-31 08:36:08 +01:00
|
|
|
"NZ".into() => table(&[
|
2020-08-04 00:47:19 +02:00
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-07-23"), "chickens".into() => int(5) }),
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-10-10"), "chickens".into() => int(15) }),
|
|
|
|
row(indexmap!{"name".into() => string("JT"), "country".into() => string("NZ"), "date".into() => date("2019-09-24"), "chickens".into() => int(10) })
|
2019-11-12 08:07:43 +01:00
|
|
|
]),
|
2019-12-31 08:36:08 +01:00
|
|
|
"US".into() => table(&[
|
2020-08-04 00:47:19 +02:00
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-10-10"), "chickens".into() => int(6) }),
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-09-24"), "chickens".into() => int(4) }),
|
|
|
|
row(indexmap!{"name".into() => string("YK"), "country".into() => string("US"), "date".into() => date("2019-07-23"), "chickens".into() => int(2) }),
|
2019-11-12 08:07:43 +01:00
|
|
|
]),
|
|
|
|
})
|
|
|
|
);
|
2020-01-02 05:02:46 +01:00
|
|
|
|
|
|
|
Ok(())
|
2019-11-12 08:07:43 +01:00
|
|
|
}
|
2019-10-21 01:42:07 +02:00
|
|
|
}
|