Skip to content

Commit

Permalink
fix(rust, python): don't allow named expression in arr.eval (#5957)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 30, 2022
1 parent 10f2d61 commit ac3f093
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions polars/polars-lazy/src/dsl/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ pub trait ListNameSpaceExtension: IntoListNameSpace + Sized {
fn eval(self, expr: Expr, parallel: bool) -> Expr {
let this = self.into_list_name_space();

use crate::physical_plan::exotic::{prepare_eval_expr, prepare_expression_for_context};
use crate::physical_plan::exotic::prepare_expression_for_context;
use crate::physical_plan::state::ExecutionState;
let expr = prepare_eval_expr(expr);

let expr2 = expr.clone();
let func = move |s: Series| {
for name in expr_to_leaf_column_names(&expr) {
if !name.is_empty() {
return Err(PolarsError::ComputeError(r#"Named columns not allowed in 'arr.eval'. Consider using 'element' or 'col("")'."#.into()));
}
}

let lst = s.list()?;
if lst.is_empty() {
// ensure we get the new schema
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,12 @@ def test_invalid_dtype() -> None:
match=r"Given dtype: 'mayonnaise' is not a valid Polars data type and cannot be converted into one", # noqa: E501
):
pl.Series([1, 2], dtype="mayonnaise")


def test_arr_eval_named_cols() -> None:
df = pl.DataFrame({"A": ["a", "b"], "B": [["a", "b"], ["c", "d"]]})

with pytest.raises(
pl.ComputeError,
):
df.select(pl.col("B").arr.eval(pl.element().append(pl.col("A"))))

0 comments on commit ac3f093

Please sign in to comment.