Skip to content
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

* Added `na.last` and `ties.method` arguments support to `rank()` (@Yousa-Mirage, #329).

* Better error message in `filter()` when a condition uses `=` instead of `==` (#340).

## Bug fixes

* Fix `NA` handling in `cummin()`, `cumprod()`, `cumsum()` (@Yousa-Mirage, #326).
Expand Down
18 changes: 18 additions & 0 deletions R/filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ filter.polars_data_frame <- function(.data, ..., .by = NULL) {
mo <- attributes(.data)$maintain_grp_order %||% FALSE
is_grouped <- !is.null(grps)

# Check whether user passed "=" instead of "=="
dots <- enquos(...)
elems_with_assignment <- dots[!is.null(names(dots)) & nzchar(names(dots))]
if (length(elems_with_assignment) > 0) {
msg <- paste0(
names(elems_with_assignment)[1],
" == ",
quo_text(elems_with_assignment[[1]])
)
cli_abort(
c(
"!" = "We detected a named input.",
"i" = "This usually means that you've used {.code =} instead of {.code ==}.",
"i" = "Did you mean {.code {msg}}?"
)
)
}

polars_exprs <- translate_dots(
.data,
...,
Expand Down
24 changes: 23 additions & 1 deletion tests/testthat/_snaps/filter-lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,27 @@
current$collect()
Condition
Error in `filter()`:
! Evaluation failed in `$filter()`.
! We detected a named input.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?

---

Code
current$collect()
Condition
Error in `filter()`:
! We detected a named input.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?

---

Code
current$collect()
Condition
Error in `filter()`:
! We detected a named input.
i This usually means that you've used `=` instead of `==`.
i Did you mean `a == mpg`?

26 changes: 24 additions & 2 deletions tests/testthat/_snaps/filter.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
# error message when using =

Code
filter(test_pl, chars = "a")
filter(test_pl, x = 1)
Condition
Error in `filter()`:
! Evaluation failed in `$filter()`.
! We detected a named input.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?

---

Code
filter(test_pl, !is.na(y), x = 1)
Condition
Error in `filter()`:
! We detected a named input.
i This usually means that you've used `=` instead of `==`.
i Did you mean `x == 1`?

---

Code
f_pl(mpg)
Condition
Error in `filter()`:
! We detected a named input.
i This usually means that you've used `=` instead of `==`.
i Did you mean `a == mpg`?

30 changes: 28 additions & 2 deletions tests/testthat/test-filter-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,36 @@ test_that("works with a local variable defined in a function", {
})

test_that("error message when using =", {
test_pl <- pl$LazyFrame(chars = letters[1:3])
test_df <- tibble(x = 1, y = 2)
test_pl <- as_polars_lf(test_df)

expect_both_error(
test_pl |> filter(x = 1),
test_df |> filter(x = 1)
)
expect_snapshot_lazy(
test_pl |> filter(x = 1),
error = TRUE
)

expect_both_error(
test_pl |> filter(!is.na(y), x = 1),
test_df |> filter(!is.na(y), x = 1)
)
expect_snapshot_lazy(
test_pl |> filter(!is.na(y), x = 1),
error = TRUE
)

f_pl <- function(x) {
filter(test_pl, "a" = {{ x }})
}
f_df <- function(x) {
filter(test_df, "a" = {{ x }})
}
expect_both_error(f_pl(mpg), f_df(mpg))
expect_snapshot_lazy(
test_pl |> filter(chars = "a"),
f_pl(mpg),
error = TRUE
)
})
Expand Down
30 changes: 28 additions & 2 deletions tests/testthat/test-filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,36 @@ test_that("works with a local variable defined in a function", {
})

test_that("error message when using =", {
test_pl <- pl$DataFrame(chars = letters[1:3])
test_df <- tibble(x = 1, y = 2)
test_pl <- as_polars_df(test_df)

expect_both_error(
test_pl |> filter(x = 1),
test_df |> filter(x = 1)
)
expect_snapshot(
test_pl |> filter(x = 1),
error = TRUE
)

expect_both_error(
test_pl |> filter(!is.na(y), x = 1),
test_df |> filter(!is.na(y), x = 1)
)
expect_snapshot(
test_pl |> filter(!is.na(y), x = 1),
error = TRUE
)

f_pl <- function(x) {
filter(test_pl, "a" = {{ x }})
}
f_df <- function(x) {
filter(test_df, "a" = {{ x }})
}
expect_both_error(f_pl(mpg), f_df(mpg))
expect_snapshot(
test_pl |> filter(chars = "a"),
f_pl(mpg),
error = TRUE
)
})
Expand Down
Loading