Make grouping more flexible. (#1741)

This commit is contained in:
Andrés N. Robalino 2020-05-09 12:15:47 -05:00 committed by GitHub
parent 9a94b3c656
commit f93ff9ec33
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 55 deletions

View file

@ -1,15 +1,16 @@
use crate::commands::WholeStreamCommand; use crate::commands::WholeStreamCommand;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_source::Tagged; use nu_source::Tagged;
use nu_value_ext::{as_string, get_data_by_key};
pub struct GroupBy; pub struct GroupBy;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GroupByArgs { pub struct GroupByArgs {
column_name: Tagged<String>, column_name: Tagged<String>,
date: Tagged<bool>,
format: Option<Tagged<String>>,
} }
impl WholeStreamCommand for GroupBy { impl WholeStreamCommand for GroupBy {
@ -18,11 +19,19 @@ impl WholeStreamCommand for GroupBy {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("group-by").required( Signature::build("group-by")
.required(
"column_name", "column_name",
SyntaxShape::String, SyntaxShape::String,
"the name of the column to group by", "the name of the column to group by",
) )
.named(
"format",
SyntaxShape::String,
"Specify date and time formatting",
Some('f'),
)
.switch("date", "by date", Some('d'))
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -38,8 +47,17 @@ impl WholeStreamCommand for GroupBy {
} }
} }
enum Grouper {
Default,
ByDate(Option<String>),
}
pub fn group_by( pub fn group_by(
GroupByArgs { column_name }: GroupByArgs, GroupByArgs {
column_name,
date,
format,
}: GroupByArgs,
RunnableContext { input, name, .. }: RunnableContext, RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let stream = async_stream! { let stream = async_stream! {
@ -52,9 +70,38 @@ pub fn group_by(
column_name.span() column_name.span()
)) ))
} else { } else {
match group(&column_name, values, name) {
let grouper = if let Tagged { item: true, tag } = date {
if let Some(Tagged { item: fmt, tag }) = format {
Grouper::ByDate(Some(fmt))
} else {
Grouper::ByDate(None)
}
} else {
Grouper::Default
};
match grouper {
Grouper::Default => {
match crate::utils::data::group(column_name, &values, None, &name) {
Ok(grouped) => yield ReturnSuccess::value(grouped), Ok(grouped) => yield ReturnSuccess::value(grouped),
Err(err) => yield Err(err) Err(err) => yield Err(err),
}
}
Grouper::ByDate(None) => {
match crate::utils::data::group(column_name, &values, Some(Box::new(|row: &Value| row.format("%Y-%b-%d"))), &name) {
Ok(grouped) => yield ReturnSuccess::value(grouped),
Err(err) => yield Err(err),
}
}
Grouper::ByDate(Some(fmt)) => {
match crate::utils::data::group(column_name, &values, Some(Box::new(move |row: &Value| {
row.format(&fmt)
})), &name) {
Ok(grouped) => yield ReturnSuccess::value(grouped),
Err(err) => yield Err(err),
}
}
} }
} }
}; };
@ -67,50 +114,7 @@ pub fn group(
values: Vec<Value>, values: Vec<Value>,
tag: impl Into<Tag>, tag: impl Into<Tag>,
) -> Result<Value, ShellError> { ) -> Result<Value, ShellError> {
let tag = tag.into(); crate::utils::data::group(column_name.clone(), &values, None, tag)
let mut groups: indexmap::IndexMap<String, Vec<Value>> = indexmap::IndexMap::new();
for value in values {
let group_key = get_data_by_key(&value, column_name.borrow_spanned());
if let Some(group_key) = group_key {
let group_key = as_string(&group_key)?;
let group = groups.entry(group_key).or_insert(vec![]);
group.push(value);
} else {
let possibilities = value.data_descriptors();
let mut possible_matches: Vec<_> = possibilities
.iter()
.map(|x| (natural::distance::levenshtein_distance(x, column_name), x))
.collect();
possible_matches.sort();
if !possible_matches.is_empty() {
return Err(ShellError::labeled_error(
"Unknown column",
format!("did you mean '{}'?", possible_matches[0].1),
column_name.tag(),
));
} else {
return Err(ShellError::labeled_error(
"Unknown column",
"row does not contain this column",
column_name.tag(),
));
}
}
}
let mut out = TaggedDictBuilder::new(&tag);
for (k, v) in groups.iter() {
out.insert_untagged(k, UntaggedValue::table(v));
}
Ok(out.into_value())
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,3 +1,4 @@
pub mod data;
pub mod data_processing; pub mod data_processing;
use crate::path::canonicalize; use crate::path::canonicalize;

View file

@ -0,0 +1,62 @@
use indexmap::IndexMap;
use nu_errors::ShellError;
use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value};
use nu_source::{Tag, Tagged};
use nu_value_ext::{as_string, get_data_by_key};
#[allow(clippy::type_complexity)]
pub fn group(
column_name: Tagged<String>,
values: &[Value],
grouper: Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
tag: impl Into<Tag>,
) -> Result<Value, ShellError> {
let tag = tag.into();
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
for value in values {
let group_key = get_data_by_key(&value, column_name.borrow_spanned());
if let Some(group_key) = group_key {
let group_key = if let Some(ref grouper) = grouper {
grouper(&group_key)
} else {
as_string(&group_key)
};
let group = groups.entry(group_key?).or_insert(vec![]);
group.push((*value).clone());
} else {
let possibilities = value.data_descriptors();
let mut possible_matches: Vec<_> = possibilities
.iter()
.map(|x| (natural::distance::levenshtein_distance(x, &column_name), x))
.collect();
possible_matches.sort();
if !possible_matches.is_empty() {
return Err(ShellError::labeled_error(
"Unknown column",
format!("did you mean '{}'?", possible_matches[0].1),
column_name.tag(),
));
} else {
return Err(ShellError::labeled_error(
"Unknown column",
"row does not contain this column",
column_name.tag(),
));
}
}
}
let mut out = TaggedDictBuilder::new(&tag);
for (k, v) in groups.iter() {
out.insert_untagged(k, UntaggedValue::table(v));
}
Ok(out.into_value())
}

View file

@ -0,0 +1,3 @@
pub mod group;
pub use crate::utils::data::group::group;

View file

@ -272,6 +272,13 @@ impl Value {
} }
} }
pub fn format(&self, fmt: &str) -> Result<String, ShellError> {
match &self.value {
UntaggedValue::Primitive(Primitive::Date(dt)) => Ok(dt.format(fmt).to_string()),
_ => Err(ShellError::type_error("date", self.spanned_type_name())),
}
}
/// View into the borrowed string contents of a Value, if possible /// View into the borrowed string contents of a Value, if possible
pub fn as_forgiving_string(&self) -> Result<&str, ShellError> { pub fn as_forgiving_string(&self) -> Result<&str, ShellError> {
match &self.value { match &self.value {