# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6)
)
method did not evaluate and take into consideration expression used with
flag.
To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.
# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:

After:

Another example:
Before:

After:

# Tests + Formatting
Added test reproducing some variants of original issue.
152 lines
4.4 KiB
Rust
152 lines
4.4 KiB
Rust
use super::super::values::{Column, NuDataFrame};
|
|
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{Command, EngineState, Stack},
|
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type,
|
|
Value,
|
|
};
|
|
use polars::prelude::{DataType, IntoSeries};
|
|
use polars_ops::prelude::{cum_max, cum_min, cum_sum};
|
|
|
|
enum CumType {
|
|
Min,
|
|
Max,
|
|
Sum,
|
|
}
|
|
|
|
impl CumType {
|
|
fn from_str(roll_type: &str, span: Span) -> Result<Self, ShellError> {
|
|
match roll_type {
|
|
"min" => Ok(Self::Min),
|
|
"max" => Ok(Self::Max),
|
|
"sum" => Ok(Self::Sum),
|
|
_ => Err(ShellError::GenericError {
|
|
error: "Wrong operation".into(),
|
|
msg: "Operation not valid for cumulative".into(),
|
|
span: Some(span),
|
|
help: Some("Allowed values: max, min, sum".into()),
|
|
inner: vec![],
|
|
}),
|
|
}
|
|
}
|
|
|
|
fn to_str(&self) -> &'static str {
|
|
match self {
|
|
CumType::Min => "cumulative_min",
|
|
CumType::Max => "cumulative_max",
|
|
CumType::Sum => "cumulative_sum",
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct Cumulative;
|
|
|
|
impl Command for Cumulative {
|
|
fn name(&self) -> &str {
|
|
"dfr cumulative"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Cumulative calculation for a series."
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.required("type", SyntaxShape::String, "rolling operation")
|
|
.switch("reverse", "Reverse cumulative calculation", Some('r'))
|
|
.input_output_type(
|
|
Type::Custom("dataframe".into()),
|
|
Type::Custom("dataframe".into()),
|
|
)
|
|
.category(Category::Custom("dataframe".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Cumulative sum for a series",
|
|
example: "[1 2 3 4 5] | dfr into-df | dfr cumulative sum",
|
|
result: Some(
|
|
NuDataFrame::try_from_columns(vec![Column::new(
|
|
"0_cumulative_sum".to_string(),
|
|
vec![
|
|
Value::test_int(1),
|
|
Value::test_int(3),
|
|
Value::test_int(6),
|
|
Value::test_int(10),
|
|
Value::test_int(15),
|
|
],
|
|
)])
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
command(engine_state, stack, call, input)
|
|
}
|
|
}
|
|
|
|
fn command(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let cum_type: Spanned<String> = call.req(engine_state, stack, 0)?;
|
|
let reverse = call.has_flag(engine_state, stack, "reverse")?;
|
|
|
|
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
|
let series = df.as_series(call.head)?;
|
|
|
|
if let DataType::Object(_) = series.dtype() {
|
|
return Err(ShellError::GenericError {
|
|
error: "Found object series".into(),
|
|
msg: "Series of type object cannot be used for cumulative operation".into(),
|
|
span: Some(call.head),
|
|
help: None,
|
|
inner: vec![],
|
|
});
|
|
}
|
|
|
|
let cum_type = CumType::from_str(&cum_type.item, cum_type.span)?;
|
|
let mut res = match cum_type {
|
|
CumType::Max => cum_max(&series, reverse),
|
|
CumType::Min => cum_min(&series, reverse),
|
|
CumType::Sum => cum_sum(&series, reverse),
|
|
}
|
|
.map_err(|e| ShellError::GenericError {
|
|
error: "Error creating cumulative".into(),
|
|
msg: e.to_string(),
|
|
span: Some(call.head),
|
|
help: None,
|
|
inner: vec![],
|
|
})?;
|
|
|
|
let name = format!("{}_{}", series.name(), cum_type.to_str());
|
|
res.rename(&name);
|
|
|
|
NuDataFrame::try_from_series(vec![res.into_series()], call.head)
|
|
.map(|df| PipelineData::Value(NuDataFrame::into_value(df, call.head), None))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::super::super::test_dataframe::test_dataframe;
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
test_dataframe(vec![Box::new(Cumulative {})])
|
|
}
|
|
}
|