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

[R] stringr helper functions drop calling environment when evaluating #36771

Closed
paleolimbot opened this issue Jul 19, 2023 · 0 comments · Fixed by #36784
Closed

[R] stringr helper functions drop calling environment when evaluating #36771

paleolimbot opened this issue Jul 19, 2023 · 0 comments · Fixed by #36784

Comments

@paleolimbot
Copy link
Member

Describe the bug, including details regarding any error messages, version, and platform.

I noticed when reviewing #36720 that we drop the calling environment when evaluating stringr modifier functions. This means potentially unexpected behaviour if one of the arguments is a symbol or function call.

ensure_opts(eval(pattern))

In practice it probably hasn't come up because (1) these arguments are usually literals and not defined by a variable and (2) if they are defined by a variable, that variable is probably in the global environment. The example below fails because int32 is a name in the arrow package namespace.

library(arrow, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(stringr)

int32 <- "123"

tibble(x = "abc123") |> 
  filter(str_detect(x, regex(int32)))
#> # A tibble: 1 × 1
#>   x     
#>   <chr> 
#> 1 abc123

arrow_table(x = "abc123") |> 
  filter(str_detect(x, regex(int32)))
#> Warning: Expression str_detect(x, regex(int32)) not supported in Arrow; pulling
#> data into R
#> # A tibble: 1 × 1
#>   x     
#>   <chr> 
#> 1 abc123

arrow_table(x = "abc123") |> 
  filter(str_detect(x, regex("123")))
#> Table (query)
#> x: string
#> 
#> * Filter: match_substring_regex(x, {pattern="123", ignore_case=false})
#> See $.data for the source Arrow object

Created on 2023-07-19 with reprex v2.0.2

The solution is to use eval_tidy() with a data mask instead of eval().

Component(s)

R

@thisisnic thisisnic self-assigned this Jul 20, 2023
thisisnic added a commit that referenced this issue Aug 30, 2023
…evaluating (#36784)

### What changes are included in this PR?

Update internals of `get_stringr_pattern_options()` to use `eval_tidy()` instead of `eval()` to ensure we're evaluating things in the right environment.

### Are these changes tested?

Yes

### Are there any user-facing changes?

Yes
* Closes: #36771

Authored-by: Nic Crane <thisisnic@gmail.com>
Signed-off-by: Nic Crane <thisisnic@gmail.com>
@thisisnic thisisnic added this to the 14.0.0 milestone Aug 30, 2023
loicalleyne pushed a commit to loicalleyne/arrow that referenced this issue Nov 13, 2023
… when evaluating (apache#36784)

### What changes are included in this PR?

Update internals of `get_stringr_pattern_options()` to use `eval_tidy()` instead of `eval()` to ensure we're evaluating things in the right environment.

### Are these changes tested?

Yes

### Are there any user-facing changes?

Yes
* Closes: apache#36771

Authored-by: Nic Crane <thisisnic@gmail.com>
Signed-off-by: Nic Crane <thisisnic@gmail.com>
dgreiss pushed a commit to dgreiss/arrow that referenced this issue Feb 19, 2024
… when evaluating (apache#36784)

### What changes are included in this PR?

Update internals of `get_stringr_pattern_options()` to use `eval_tidy()` instead of `eval()` to ensure we're evaluating things in the right environment.

### Are these changes tested?

Yes

### Are there any user-facing changes?

Yes
* Closes: apache#36771

Authored-by: Nic Crane <thisisnic@gmail.com>
Signed-off-by: Nic Crane <thisisnic@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants