Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust, python): fix regression in regex expansion #9952

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions polars/polars-lazy/polars-plan/src/logical_plan/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn replace_regex(
result: &mut Vec<Expr>,
schema: &Schema,
exclude: &PlHashSet<Arc<str>>,
has_exclude: bool,
) -> PolarsResult<()> {
let roots = expr_to_leaf_column_names(expr);
let mut regex = None;
Expand All @@ -139,18 +140,19 @@ fn replace_regex(
match regex {
None => {
regex = Some(name);
if exclude.is_empty() {
expand_regex(expr, result, schema, name, exclude)?
} else {
if has_exclude {
// iterate until we find the Exclude node
// we remove that node from the expression
// the `exclude` set is already filled
// by prepare exclude
for e in expr.into_iter() {
if let Expr::Exclude(e, _) = e {
expand_regex(e, result, schema, name, exclude)?;
break;
return Ok(());
}
}
}
expand_regex(expr, result, schema, name, exclude)?
}
Some(r) => {
polars_ensure!(
Expand Down Expand Up @@ -274,7 +276,13 @@ fn prepare_excluded(
match to_exclude_single {
Excluded::Name(name) => {
let e = Expr::Column(name.clone());
replace_regex(&e, &mut buf, schema, &Default::default())?;
replace_regex(
&e,
&mut buf,
schema,
&Default::default(),
has_exclude,
)?;
// we cannot loop because of bchck
while let Some(col) = buf.pop() {
if let Expr::Column(name) = col {
Expand Down Expand Up @@ -512,7 +520,7 @@ fn replace_and_add_to_results(
{
// keep track of column excluded from the dtypes
let exclude = prepare_excluded(&expr, schema, keys, flags.has_exclude)?;
replace_regex(&expr, result, schema, &exclude)?;
replace_regex(&expr, result, schema, &exclude, flags.has_exclude)?;
}
#[cfg(not(feature = "regex"))]
{
Expand Down
5 changes: 5 additions & 0 deletions py-polars/tests/unit/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,8 @@ def test_selector_expr_dispatch() -> None:
pl.when(nan_or_inf).then(0.0).otherwise(cs.float()).keep_name()
).fill_null(0),
)


def test_regex_expansion_groupby_9947() -> None:
df = pl.DataFrame({"g": [3], "abc": [1], "abcd": [3]})
assert df.groupby("g").agg(pl.col("^ab.*$")).columns == ["g", "abc", "abcd"]