Expression support polars replace and polars replace-all (#13726)

# Description
Adds the ability for `polars replace` and `polars replace-all` to work
as expressions.

# User-Facing Changes
- `polars replace` can be used with polars expressions
- `polars replace-all` can be used with polars expressions
This commit is contained in:
Jack Wright 2024-08-29 13:59:44 -07:00 committed by GitHub
parent 84e1ac27e5
commit 0119534f61
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 208 additions and 61 deletions

View File

@ -1,4 +1,10 @@
use crate::{missing_flag_error, values::CustomValueSupport, PolarsPlugin}; use crate::{
missing_flag_error,
values::{
cant_convert_err, CustomValueSupport, NuExpression, PolarsPluginObject, PolarsPluginType,
},
PolarsPlugin,
};
use super::super::super::values::{Column, NuDataFrame}; use super::super::super::values::{Column, NuDataFrame};
@ -7,7 +13,7 @@ use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value, Value,
}; };
use polars::prelude::{IntoSeries, StringNameSpaceImpl}; use polars::prelude::{lit, IntoSeries, StringNameSpaceImpl};
#[derive(Clone)] #[derive(Clone)]
pub struct Replace; pub struct Replace;
@ -37,17 +43,44 @@ impl PluginCommand for Replace {
"replacing string", "replacing string",
Some('r'), Some('r'),
) )
.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("dataframe".into())) .category(Category::Custom("dataframe".into()))
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
Example {
description: "Replaces string in column",
example:
"[[a]; [abc] [abcabc]] | polars into-df | polars select (polars col a | polars replace --pattern ab --replace AB) | polars collect",
result: Some(
NuDataFrame::try_from_columns(
vec![Column::new(
"a".to_string(),
vec![
Value::test_string("ABc"),
Value::test_string("ABcabc"),
],
)],
None,
)
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Replaces string", description: "Replaces string",
example: "[abc abc abc] | polars into-df | polars replace --pattern ab --replace AB", example:
"[abc abc abc] | polars into-df | polars replace --pattern ab --replace AB",
result: Some( result: Some(
NuDataFrame::try_from_columns( NuDataFrame::try_from_columns(
vec![Column::new( vec![Column::new(
@ -63,7 +96,8 @@ impl PluginCommand for Replace {
.expect("simple df for test should not fail") .expect("simple df for test should not fail")
.into_value(Span::test_data()), .into_value(Span::test_data()),
), ),
}] },
]
} }
fn run( fn run(
@ -73,15 +107,56 @@ impl PluginCommand for Replace {
call: &EvaluatedCall, call: &EvaluatedCall,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, LabeledError> { ) -> Result<PipelineData, LabeledError> {
command(plugin, engine, call, input).map_err(LabeledError::from) let value = input.into_value(call.head)?;
match PolarsPluginObject::try_from_value(plugin, &value)? {
PolarsPluginObject::NuDataFrame(df) => command_df(plugin, engine, call, df),
PolarsPluginObject::NuLazyFrame(lazy) => {
command_df(plugin, engine, call, lazy.collect(call.head)?)
}
PolarsPluginObject::NuExpression(expr) => command_expr(plugin, engine, call, expr),
_ => Err(cant_convert_err(
&value,
&[
PolarsPluginType::NuDataFrame,
PolarsPluginType::NuLazyFrame,
PolarsPluginType::NuExpression,
],
)),
}
.map_err(LabeledError::from)
} }
} }
fn command( fn command_expr(
plugin: &PolarsPlugin, plugin: &PolarsPlugin,
engine: &EngineInterface, engine: &EngineInterface,
call: &EvaluatedCall, call: &EvaluatedCall,
input: PipelineData, expr: NuExpression,
) -> Result<PipelineData, ShellError> {
let pattern: String = call
.get_flag("pattern")?
.ok_or_else(|| missing_flag_error("pattern", call.head))?;
let pattern = lit(pattern);
let replace: String = call
.get_flag("replace")?
.ok_or_else(|| missing_flag_error("replace", call.head))?;
let replace = lit(replace);
let res: NuExpression = expr
.into_polars()
.str()
.replace(pattern, replace, false)
.into();
res.to_pipeline_data(plugin, engine, call.head)
}
fn command_df(
plugin: &PolarsPlugin,
engine: &EngineInterface,
call: &EvaluatedCall,
df: NuDataFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let pattern: String = call let pattern: String = call
.get_flag("pattern")? .get_flag("pattern")?
@ -90,7 +165,6 @@ fn command(
.get_flag("replace")? .get_flag("replace")?
.ok_or_else(|| missing_flag_error("replace", call.head))?; .ok_or_else(|| missing_flag_error("replace", call.head))?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?;
let series = df.as_series(call.head)?; let series = df.as_series(call.head)?;
let chunked = series.str().map_err(|e| ShellError::GenericError { let chunked = series.str().map_err(|e| ShellError::GenericError {
error: "Error conversion to string".into(), error: "Error conversion to string".into(),

View File

@ -1,4 +1,10 @@
use crate::{missing_flag_error, values::CustomValueSupport, PolarsPlugin}; use crate::{
missing_flag_error,
values::{
cant_convert_err, CustomValueSupport, NuExpression, PolarsPluginObject, PolarsPluginType,
},
PolarsPlugin,
};
use super::super::super::values::{Column, NuDataFrame}; use super::super::super::values::{Column, NuDataFrame};
@ -7,7 +13,7 @@ use nu_protocol::{
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type,
Value, Value,
}; };
use polars::prelude::{IntoSeries, StringNameSpaceImpl}; use polars::prelude::{lit, IntoSeries, StringNameSpaceImpl};
#[derive(Clone)] #[derive(Clone)]
pub struct ReplaceAll; pub struct ReplaceAll;
@ -37,15 +43,42 @@ impl PluginCommand for ReplaceAll {
"replacing string", "replacing string",
Some('r'), Some('r'),
) )
.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("dataframe".into())) .category(Category::Custom("dataframe".into()))
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
Example {
description: "Replaces string in a column",
example:
"[[a]; [abac] [abac] [abac]] | polars into-df | polars select (polars col a | polars replace-all --pattern a --replace A) | polars collect",
result: Some(
NuDataFrame::try_from_columns(
vec![Column::new(
"a".to_string(),
vec![
Value::test_string("AbAc"),
Value::test_string("AbAc"),
Value::test_string("AbAc"),
],
)],
None,
)
.expect("simple df for test should not fail")
.into_value(Span::test_data()),
),
},
Example {
description: "Replaces string", description: "Replaces string",
example: example:
"[abac abac abac] | polars into-df | polars replace-all --pattern a --replace A", "[abac abac abac] | polars into-df | polars replace-all --pattern a --replace A",
@ -64,7 +97,8 @@ impl PluginCommand for ReplaceAll {
.expect("simple df for test should not fail") .expect("simple df for test should not fail")
.into_value(Span::test_data()), .into_value(Span::test_data()),
), ),
}] },
]
} }
fn run( fn run(
@ -74,15 +108,55 @@ impl PluginCommand for ReplaceAll {
call: &EvaluatedCall, call: &EvaluatedCall,
input: PipelineData, input: PipelineData,
) -> Result<PipelineData, LabeledError> { ) -> Result<PipelineData, LabeledError> {
command(plugin, engine, call, input).map_err(LabeledError::from) let value = input.into_value(call.head)?;
match PolarsPluginObject::try_from_value(plugin, &value)? {
PolarsPluginObject::NuDataFrame(df) => command_df(plugin, engine, call, df),
PolarsPluginObject::NuLazyFrame(lazy) => {
command_df(plugin, engine, call, lazy.collect(call.head)?)
}
PolarsPluginObject::NuExpression(expr) => command_expr(plugin, engine, call, expr),
_ => Err(cant_convert_err(
&value,
&[
PolarsPluginType::NuDataFrame,
PolarsPluginType::NuLazyFrame,
PolarsPluginType::NuExpression,
],
)),
}
.map_err(LabeledError::from)
} }
} }
fn command( fn command_expr(
plugin: &PolarsPlugin, plugin: &PolarsPlugin,
engine_state: &EngineInterface, engine_state: &EngineInterface,
call: &EvaluatedCall, call: &EvaluatedCall,
input: PipelineData, expr: NuExpression,
) -> Result<PipelineData, ShellError> {
let pattern: String = call
.get_flag("pattern")?
.ok_or_else(|| missing_flag_error("pattern", call.head))?;
let pattern = lit(pattern);
let replace: String = call
.get_flag("replace")?
.ok_or_else(|| missing_flag_error("replace", call.head))?;
let replace = lit(replace);
let res: NuExpression = expr
.into_polars()
.str()
.replace_all(pattern, replace, false)
.into();
res.to_pipeline_data(plugin, engine_state, call.head)
}
fn command_df(
plugin: &PolarsPlugin,
engine_state: &EngineInterface,
call: &EvaluatedCall,
df: NuDataFrame,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let pattern: String = call let pattern: String = call
.get_flag("pattern")? .get_flag("pattern")?
@ -91,7 +165,6 @@ fn command(
.get_flag("replace")? .get_flag("replace")?
.ok_or_else(|| missing_flag_error("replace", call.head))?; .ok_or_else(|| missing_flag_error("replace", call.head))?;
let df = NuDataFrame::try_from_pipeline_coerce(plugin, input, call.head)?;
let series = df.as_series(call.head)?; let series = df.as_series(call.head)?;
let chunked = series.str().map_err(|e| ShellError::GenericError { let chunked = series.str().map_err(|e| ShellError::GenericError {
error: "Error conversion to string".into(), error: "Error conversion to string".into(),

View File

@ -185,8 +185,8 @@ impl CustomValueType {
} }
} }
pub fn cant_convert_err(value: &Value, types: &[PolarsPluginType]) -> ShellError { pub fn cant_convert_err(value: &Value, expected_types: &[PolarsPluginType]) -> ShellError {
let type_string = types let type_string = expected_types
.iter() .iter()
.map(ToString::to_string) .map(ToString::to_string)
.collect::<Vec<String>>() .collect::<Vec<String>>()