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: Properly raise on invalid selector expressions #18511

Merged
merged 1 commit into from
Sep 2, 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
8 changes: 4 additions & 4 deletions crates/polars-plan/src/plans/conversion/expr_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,15 +922,15 @@ pub(super) fn expand_selector(
replace_selector_inner(s, &mut members, &mut vec![], schema, keys)?;

if members.len() <= 1 {
Ok(members
members
.into_iter()
.map(|e| {
let Expr::Column(name) = e else {
unreachable!()
polars_bail!(InvalidOperation: "invalid selector expression: {}", e)
};
name
Ok(name)
})
.collect())
.collect()
} else {
// Ensure that multiple columns returned from combined/nested selectors remain in schema order
let selected = schema
Expand Down
15 changes: 14 additions & 1 deletion py-polars/tests/unit/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import polars.selectors as cs
from polars._typing import SelectorType
from polars.dependencies import _ZONEINFO_AVAILABLE
from polars.exceptions import ColumnNotFoundError
from polars.exceptions import ColumnNotFoundError, InvalidOperationError
from polars.selectors import expand_selector, is_selector
from polars.testing import assert_frame_equal
from tests.unit.conftest import INTEGER_DTYPES, TEMPORAL_DTYPES
Expand Down Expand Up @@ -809,3 +809,16 @@ def test_selector_result_order(df: pl.DataFrame, selector: SelectorType) -> None
"qqR": pl.String,
}
)


def test_selector_list_of_lists_18499() -> None:
lf = pl.DataFrame(
{
"foo": [1, 2, 3, 1],
"bar": ["a", "a", "a", "a"],
"ham": ["b", "b", "b", "b"],
}
)

with pytest.raises(InvalidOperationError, match="invalid selector expression"):
lf.unique(subset=[["bar", "ham"]]) # type: ignore[list-item]
Loading