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): Reject non-integral offset and length in AExpr::Slice #16874

Merged
merged 3 commits into from
Jun 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ impl OptimizationRule for TypeCoercionRule {
options,
})
},
AExpr::Slice { offset, length, .. } => {
let input_schema = get_schema(lp_arena, lp_node);
let (_, offset_dtype) =
unpack!(get_aexpr_and_type(expr_arena, offset, &input_schema));
polars_ensure!(offset_dtype.is_integer(), InvalidOperation: "offset must be integral for slice, not {}", offset_dtype);
let (_, length_dtype) =
unpack!(get_aexpr_and_type(expr_arena, length, &input_schema));
polars_ensure!(length_dtype.is_integer(), InvalidOperation: "length must be integral for slice, not {}", length_dtype);
None
},
_ => None,
};
Ok(out)
Expand Down
2 changes: 1 addition & 1 deletion crates/polars/tests/it/lazy/expressions/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn test_slice_args() -> PolarsResult<()> {
]?
.lazy()
.group_by_stable([col("groups")])
.agg([col("vals").slice(lit(0i64), len() * lit(0.2))])
.agg([col("vals").slice(lit(0i64), (len() * lit(0.2)).cast(DataType::Int32))])
.collect()?;

let out = df.column("vals")?.explode()?;
Expand Down
6 changes: 5 additions & 1 deletion py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4851,7 +4851,11 @@ def tail(self, n: int | Expr = 10) -> Self:
│ 7 │
└─────┘
"""
offset = -self._from_pyexpr(parse_into_expression(n))
# This cast enables tail with expressions that return unsigned integers,
# for which negate otherwise raises InvalidOperationError.
offset = -self._from_pyexpr(
parse_into_expression(n).cast(Int64, strict=False, wrap_numerical=True)
)
return self.slice(offset, n)

def limit(self, n: int | Expr = 10) -> Self:
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,7 @@ def test_group_by_slice_expression_args() -> None:

out = (
df.group_by("groups", maintain_order=True)
.agg([pl.col("vals").slice(pl.len() * 0.1, (pl.len() // 5))])
.agg([pl.col("vals").slice((pl.len() * 0.1).cast(int), (pl.len() // 5))])
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
.explode("vals")
)

Expand Down
17 changes: 15 additions & 2 deletions py-polars/tests/unit/expr/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ def test_head() -> None:
assert df.select(pl.col("a").head(10)).to_dict(as_series=False) == {
"a": [1, 2, 3, 4, 5]
}
assert df.select(pl.col("a").head(pl.len() / 2)).to_dict(as_series=False) == {
assert df.select(pl.col("a").head(pl.len() // 2)).to_dict(as_series=False) == {
"a": [1, 2]
}

Expand All @@ -690,7 +690,7 @@ def test_tail() -> None:
assert df.select(pl.col("a").tail(10)).to_dict(as_series=False) == {
"a": [1, 2, 3, 4, 5]
}
assert df.select(pl.col("a").tail(pl.len() / 2)).to_dict(as_series=False) == {
assert df.select(pl.col("a").tail(pl.len() // 2)).to_dict(as_series=False) == {
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
"a": [4, 5]
}

Expand Down Expand Up @@ -732,3 +732,16 @@ def test_replace_no_cse() -> None:
.explain()
)
assert "POLARS_CSER" not in plan


def test_slice_rejects_non_integral() -> None:
df = pl.LazyFrame({"a": [0, 1, 2, 3], "b": [1.5, 2, 3, 4]})

with pytest.raises(pl.exceptions.InvalidOperationError):
df.select(pl.col("a").slice(pl.col("b").slice(0, 1), None)).collect()

with pytest.raises(pl.exceptions.InvalidOperationError):
df.select(pl.col("a").slice(0, pl.col("b").slice(1, 2))).collect()

with pytest.raises(pl.exceptions.InvalidOperationError):
df.select(pl.col("a").slice(pl.lit("1"), None)).collect()