feat(polars): add maintain-order flag to polars group-by and allow expression inputs in polars filter (#15865)

<!--
if this PR closes one or more issues, you can automatically link the PR
with
them by using one of the [*linking
keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword),
e.g.
- this PR should close #xxxx
- fixes #xxxx

you can also mention related issues, PRs or discussions!
-->

# Description
<!--
Thank you for improving Nushell. Please, check our [contributing
guide](../CONTRIBUTING.md) and talk to the core team before making major
changes.

Description of your pull request goes here. **Provide examples and/or
screenshots** if your changes affect the user experience.
-->
This PR involves two changes: (1) adding `maintain-order` flag to
`polars group-by` for stable sorting when aggregating and (2) allow
expression inputs in `polars filter`. The first change was necessary to
reliably test the second change, and the two commits are therefore
combined in one PR. See example:


```nushell
#  Filter a single column in a group-by context
  > [[a b]; [foo 1] [foo 2] [foo 3] [bar 2] [bar 3] [bar 4]] | polars into-df
                    | polars group-by a --maintain-order
                    | polars agg {
                        lt: (polars col b | polars filter ((polars col b) < 2) | polars sum)
                        gte: (polars col b | polars filter ((polars col b) >= 3) | polars sum)
                    }
                    | polars collect
  ╭───┬─────┬────┬─────╮
  │ # │  a  │ lt │ gte │
  ├───┼─────┼────┼─────┤
  │ 0 │ foo │  1 │   3 │
  │ 1 │ bar │  0 │   7 │
  ╰───┴─────┴────┴─────╯

```

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
No breaking changes.

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->
An example test was added to `polars filter` demonstrating both the
stable group-by feature and the expression filtering feature.

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
pyz4 2025-06-01 15:32:23 -04:00 committed by GitHub
parent ad9f051d61
commit 2b524cd861
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 79 additions and 11 deletions

View File

@ -31,6 +31,10 @@ impl PluginCommand for ToLazyGroupBy {
SyntaxShape::Any, SyntaxShape::Any,
"Expression(s) that define the lazy group-by", "Expression(s) that define the lazy group-by",
) )
.switch(
"maintain-order",
"Ensure that the order of the groups is consistent with the input data. This is slower than a default group by and cannot be run on the streaming engine.",
Some('m'))
.input_output_type( .input_output_type(
Type::Custom("dataframe".into()), Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()), Type::Custom("dataframe".into()),
@ -104,6 +108,7 @@ impl PluginCommand for ToLazyGroupBy {
let vals: Vec<Value> = call.rest(0)?; let vals: Vec<Value> = call.rest(0)?;
let expr_value = Value::list(vals, call.head); let expr_value = Value::list(vals, call.head);
let expressions = NuExpression::extract_exprs(plugin, expr_value)?; let expressions = NuExpression::extract_exprs(plugin, expr_value)?;
let maintain_order = call.has_flag("maintain-order")?;
if expressions if expressions
.iter() .iter()
@ -118,7 +123,7 @@ impl PluginCommand for ToLazyGroupBy {
let pipeline_value = input.into_value(call.head)?; let pipeline_value = input.into_value(call.head)?;
let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?; let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?;
command(plugin, engine, call, lazy, expressions) command(plugin, engine, call, lazy, expressions, maintain_order)
.map_err(LabeledError::from) .map_err(LabeledError::from)
.map(|pd| pd.set_metadata(metadata)) .map(|pd| pd.set_metadata(metadata))
} }
@ -130,8 +135,13 @@ fn command(
call: &EvaluatedCall, call: &EvaluatedCall,
mut lazy: NuLazyFrame, mut lazy: NuLazyFrame,
expressions: Vec<Expr>, expressions: Vec<Expr>,
maintain_order: bool,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let group_by = lazy.to_polars().group_by(expressions); let group_by = if maintain_order {
lazy.to_polars().group_by_stable(expressions)
} else {
lazy.to_polars().group_by(expressions)
};
let group_by = NuLazyGroupBy::new(group_by, lazy.from_eager, lazy.schema().clone()?); let group_by = NuLazyGroupBy::new(group_by, lazy.from_eager, lazy.schema().clone()?);
group_by.to_pipeline_data(plugin, engine, call.head) group_by.to_pipeline_data(plugin, engine, call.head)
} }

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
PolarsPlugin, PolarsPlugin,
dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame}, dataframe::values::{Column, NuDataFrame, NuExpression, NuLazyFrame},
values::CustomValueSupport, values::{CustomValueSupport, PolarsPluginObject, PolarsPluginType, cant_convert_err},
}; };
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand}; use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
@ -31,10 +31,16 @@ impl PluginCommand for LazyFilter {
SyntaxShape::Any, SyntaxShape::Any,
"Expression that define the column selection", "Expression that define the column selection",
) )
.input_output_type( .input_output_types(vec![
(
Type::Custom("dataframe".into()), Type::Custom("dataframe".into()),
Type::Custom("dataframe".into()), Type::Custom("dataframe".into()),
) ),
(
Type::Custom("expression".into()),
Type::Custom("expression".into()),
),
])
.category(Category::Custom("lazyframe".into())) .category(Category::Custom("lazyframe".into()))
} }
@ -99,6 +105,37 @@ impl PluginCommand for LazyFilter {
.into_value(Span::test_data()), .into_value(Span::test_data()),
), ),
}, },
Example {
description: "Filter a single column in a group-by context",
example: "[[a b]; [foo 1] [foo 2] [foo 3] [bar 2] [bar 3] [bar 4]] | polars into-df
| polars group-by a --maintain-order
| polars agg {
lt: (polars col b | polars filter ((polars col b) < 2) | polars sum)
gte: (polars col b | polars filter ((polars col b) >= 3) | polars sum)
}
| polars collect",
result: Some(
NuDataFrame::try_from_columns(
vec![
Column::new(
"a".to_string(),
vec![Value::test_string("foo"), Value::test_string("bar")],
),
Column::new(
"lt".to_string(),
vec![Value::test_int(1), Value::test_int(0)],
),
Column::new(
"gte".to_string(),
vec![Value::test_int(3), Value::test_int(7)],
),
],
None,
)
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
] ]
} }
@ -113,8 +150,29 @@ impl PluginCommand for LazyFilter {
let expr_value: Value = call.req(0)?; let expr_value: Value = call.req(0)?;
let filter_expr = NuExpression::try_from_value(plugin, &expr_value)?; let filter_expr = NuExpression::try_from_value(plugin, &expr_value)?;
let pipeline_value = input.into_value(call.head)?; let pipeline_value = input.into_value(call.head)?;
let lazy = NuLazyFrame::try_from_value_coerce(plugin, &pipeline_value)?;
match PolarsPluginObject::try_from_value(plugin, &pipeline_value)? {
PolarsPluginObject::NuDataFrame(df) => {
command(plugin, engine, call, df.lazy(), filter_expr)
}
PolarsPluginObject::NuLazyFrame(lazy) => {
command(plugin, engine, call, lazy, filter_expr) command(plugin, engine, call, lazy, filter_expr)
}
PolarsPluginObject::NuExpression(expr) => {
let res: NuExpression = expr.into_polars().filter(filter_expr.into_polars()).into();
res.to_pipeline_data(plugin, engine, call.head)
}
_ => Err(cant_convert_err(
&pipeline_value,
&[
// PolarsPluginType::NuDataFrame,
PolarsPluginType::NuLazyGroupBy,
PolarsPluginType::NuExpression,
],
)),
}
.map_err(LabeledError::from) .map_err(LabeledError::from)
.map(|pd| pd.set_metadata(metadata)) .map(|pd| pd.set_metadata(metadata))
} }