Skip to content

Commit

Permalink
Expand regex function input (#3613)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jun 7, 2022
1 parent 033b039 commit 4fa8fad
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
3 changes: 2 additions & 1 deletion polars/polars-lazy/src/dsl/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ pub fn argsort_by<E: AsRef<[Expr]>>(by: E, reverse: &[bool]) -> Expr {
#[cfg(feature = "concat_str")]
#[cfg_attr(docsrs, doc(cfg(feature = "concat_str")))]
/// Horizontally concat string columns in linear time
pub fn concat_str(s: Vec<Expr>, sep: &str) -> Expr {
pub fn concat_str<E: AsRef<[Expr]>>(s: E, sep: &str) -> Expr {
let s = s.as_ref().to_vec();
let sep = sep.to_string();
let function = NoEq::new(Arc::new(move |s: &mut [Series]| {
polars_core::functions::concat_str(s, &sep).map(|ca| ca.into_series())
Expand Down
22 changes: 15 additions & 7 deletions polars/polars-lazy/src/logical_plan/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ fn prepare_excluded(expr: &Expr, schema: &Schema, keys: &[Expr]) -> Vec<Arc<str>
fn expand_function_list_inputs(mut expr: Expr, schema: &Schema) -> Expr {
expr.mutate().apply(|e| {
match e {
Expr::AnonymousFunction { input, options, .. } if options.input_wildcard_expansion => {
Expr::AnonymousFunction { input, options, .. }
| Expr::Function { input, options, .. }
if options.input_wildcard_expansion =>
{
if input
.iter()
.any(|e| matches!(e, Expr::Columns(_) | Expr::DtypeColumn(_)))
Expand Down Expand Up @@ -291,7 +294,10 @@ fn expand_function_list_inputs(mut expr: Expr, schema: &Schema) -> Expr {
fn function_wildcard_expansion(mut expr: Expr, schema: &Schema, exclude: &[Arc<str>]) -> Expr {
expr.mutate().apply(|e| {
match e {
Expr::AnonymousFunction { input, options, .. } if options.input_wildcard_expansion => {
Expr::AnonymousFunction { input, options, .. }
| Expr::Function { input, options, .. }
if options.input_wildcard_expansion =>
{
let mut new_inputs = Vec::with_capacity(input.len());

input.iter_mut().for_each(|e| {
Expand Down Expand Up @@ -356,14 +362,16 @@ pub(crate) fn rewrite_projections(exprs: Vec<Expr>, schema: &Schema, keys: &[Exp
replace_nth(&mut expr, schema);
}

if has_wildcard(&expr) {
let function_input_expansion = has_expr(
&expr,
|e| matches!(e, Expr::AnonymousFunction { options, .. } | Expr::Function {options, ..} if options.input_wildcard_expansion),
);

if has_wildcard(&expr) || function_input_expansion {
// keep track of column excluded from the wildcard
let exclude = prepare_excluded(&expr, schema, keys);
// this path prepares the wildcard as input for the Function Expr
if has_expr(
&expr,
|e| matches!(e, Expr::AnonymousFunction { options, .. } if options.input_wildcard_expansion),
) {
if function_input_expansion {
expr = function_wildcard_expansion(expr, schema, &exclude);
result.push(expr);
continue;
Expand Down
18 changes: 18 additions & 0 deletions polars/polars-lazy/src/tests/projection_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,21 @@ fn scan_join_same_file() -> Result<()> {
);
Ok(())
}

#[test]
fn concat_str_regex_expansion() -> Result<()> {
let df = df![
"a"=> [1, 1, 1],
"b_a_1"=> ["a--", "", ""],
"b_a_2"=> ["", "b--", ""],
"b_a_3"=> ["", "", "c--"]
]?
.lazy();
let out = df
.select([concat_str([col(r"^b_a_\d$")], ";").alias("concatenated")])
.collect()?;
let s = out.column("concatenated")?;
assert_eq!(s, &Series::new("concatenated", ["a--;;", ";b--;", ";;c--"]));

Ok(())
}
2 changes: 1 addition & 1 deletion py-polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn toggle_string_cache(toggle: bool) {

#[pyfunction]
fn concat_str(s: Vec<dsl::PyExpr>, sep: &str) -> dsl::PyExpr {
let s = s.into_iter().map(|e| e.inner).collect();
let s = s.into_iter().map(|e| e.inner).collect::<Vec<_>>();
polars::lazy::dsl::concat_str(s, sep).into()
}

Expand Down

0 comments on commit 4fa8fad

Please sign in to comment.