sayuks opened a new issue, #55793:
URL: https://github.com/apache/arrow/issues/55793

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   When an arrow dplyr verb is reached through a function that forwards `...`, 
and the expression cannot be translated, the user gets
   
   ```
   Error in `call$.data <- dplyr::collect(.data)`:
   ! object of type 'special' is not subsettable
   ```
   
   instead of either the documented fallback to R or the `arrow_not_supported` 
error that `abandon_ship()` is written to raise in this situation.
   
   #### Reproducer
   
   ``` r
   library(arrow, warn.conflicts = FALSE)
   library(dplyr, warn.conflicts = FALSE)
   
   tab <- arrow_table(k = c("a", "a", "b"), v = 1:3)
   
   # Direct call: an expression Arrow cannot translate warns and falls back to 
R.
   summarise(tab, out = dplyr::first(v))
   #> Warning: In dplyr::first(v): 
   #> ℹ Expression not supported in Arrow
   #> → Pulling data into R
   #> # A tibble: 1 × 1
   #>     out
   #>   <int>
   #> 1     1
   
   # The same call through a wrapper that forwards `...` errors instead.
   wrapper <- function(.data, ...) dplyr::summarise(.data, ...)
   wrapper(tab, out = dplyr::first(v))
   #> Warning: In dplyr::first(v): 
   #> ℹ Expression not supported in Arrow
   #> → Pulling data into R
   #> Error in `call$.data <- dplyr::collect(.data)`:
   #> ! object of type 'special' is not subsettable
   
   # The condition carries no Arrow class.
   err <- tryCatch(
     suppressWarnings(wrapper(tab, out = dplyr::first(v))),
     error = identity
   )
   class(err)
   #> [1] "notSubsettableError" "error"               "condition"
   
   # What abandon_ship()'s lookup returns when `call` was never bound in `env`:
   env <- new.env()
   found <- try(get("call", envir = env), silent = TRUE)
   identical(found, base::call)
   #> [1] TRUE
   inherits(found, "try-error")
   #> [1] FALSE
   
   # With `inherits = FALSE` the existing guard would fire:
   found <- try(get("call", envir = env, inherits = FALSE), silent = TRUE)
   inherits(found, "try-error")
   #> [1] TRUE
   ```
   
   <sup>Created on 2026-11-26 with [reprex 
v2.1.1](https://reprex.tidyverse.org)</sup>
   
   `summarise()` is only the example. `mutate()`, `filter()` and `arrange()` 
fail the same way through an equivalent wrapper, as do grouped queries and 
`RecordBatch` input. Only in-memory input is affected: a `Dataset` through the 
same wrapper already gets `arrow_not_supported`, and an expression Arrow can 
translate works through the wrapper as usual.
   
   #### Cause
   
   `try_arrow_dplyr()` captures the verb's call for later use 
([`r/R/dplyr-eval.R#L233-L236`](https://github.com/apache/arrow/blob/88e5154544b4d73d6298dad1eac6b75031d16313/r/R/dplyr-eval.R#L233-L236)):
   
   ```r
   try(
     evalq(call <- match.call(), parent),
     silent = !getOption("arrow.debug", FALSE)
   )
   ```
   
   When the verb's own call contains `...`, this `match.call()` fails with `... 
used in a situation where it does not exist`, so `call` is never bound in 
`parent`. The error is silenced unless `options(arrow.debug = TRUE)` is set. It 
does not need arrow to reproduce:
   
   ``` r
   # try_arrow_dplyr()'s capture, reduced to base R.
   capture <- function() {
     parent <- parent.frame()
     try(evalq(call <- match.call(), parent))
     exists("call", envir = parent, inherits = FALSE)
   }
   verb <- function(.data, ...) capture()
   wrapper <- function(.data, ...) verb(.data, ...)
   
   verb(1, a = 1)
   #> [1] TRUE
   wrapper(1, a = 1)
   #> Error in match.call() : ... used in a situation where it does not exist
   #> [1] FALSE
   ```
   
   <sup>Created on 2026-11-26 with [reprex 
v2.1.1](https://reprex.tidyverse.org)</sup>
   
   `abandon_ship()` anticipates the missing binding 
([`#L252-L255`](https://github.com/apache/arrow/blob/88e5154544b4d73d6298dad1eac6b75031d16313/r/R/dplyr-eval.R#L252-L255)):
   
   ```r
   # If there's no call (see comment in try_arrow_dplyr), we can't eval with
   # dplyr even if the data is in memory already
   call <- try(get("call", envir = env), silent = TRUE)
   if (query_on_dataset(.data) || inherits(call, "try-error")) {
   ```
   
   but `get()` inherits by default, so the lookup walks out of `env` and 
returns `base::call`. That is not a `try-error`, the guard is skipped, and 
execution reaches `call$.data <- dplyr::collect(.data)`, which tries to subset 
a special. Since R evaluates the right-hand side first, the whole input is 
collected before the error is raised.
   
   #### Suggested fix
   
   ```diff
   - call <- try(get("call", envir = env), silent = TRUE)
   + call <- try(get("call", envir = env, inherits = FALSE), silent = TRUE)
   ```
   
   With only that line patched in a session, the wrapper gets the classed error 
and the direct call is unaffected:
   
   ``` r
   library(arrow, warn.conflicts = FALSE)
   library(dplyr, warn.conflicts = FALSE)
   
   # Patch the one lookup in abandon_ship() for this session.
   ns <- asNamespace("arrow")
   fn <- get("abandon_ship", ns)
   src <- deparse(body(fn))
   src <- sub(
     'get("call", envir = env)',
     'get("call", envir = env, inherits = FALSE)',
     src,
     fixed = TRUE
   )
   body(fn) <- parse(text = src)[[1]]
   assignInNamespace("abandon_ship", fn, ns)
   
   tab <- arrow_table(k = c("a", "a", "b"), v = 1:3)
   wrapper <- function(.data, ...) dplyr::summarise(.data, ...)
   
   # Through the wrapper: the classed error the source comments describe.
   wrapper(tab, out = dplyr::first(v))
   #> Error in `dplyr::first()`:
   #> ! Expression not supported in Arrow
   #> → Call collect() first to pull data into R.
   err <- tryCatch(wrapper(tab, out = dplyr::first(v)), error = identity)
   class(err)
   #> [1] "arrow_not_supported" "rlang_error"         "error"              
   #> [4] "condition"
   
   # Direct call: the fallback is unchanged.
   summarise(tab, out = dplyr::first(v))
   #> Warning: In dplyr::first(v): 
   #> ℹ Expression not supported in Arrow
   #> → Pulling data into R
   #> # A tibble: 1 × 1
   #>     out
   #>   <int>
   #> 1     1
   ```
   
   <sup>Created on 2026-11-26 with [reprex 
v2.1.1](https://reprex.tidyverse.org)</sup>
   
   This makes the guard do what it says. It does not make the fallback work 
through a wrapper: that would need the call captured some other way than 
`match.call()` under `evalq()`, which I have not attempted here.
   
   #### History
   
   The `evalq(call <- match.call(), parent)` capture and the inheriting 
`get("call", envir = env)` both date from #41576, which changed 
`abandon_ship()` to look the call up instead of receiving it as an argument. 
#43498 then wrapped the `match.call()` in `try()`, moved the `get()` into a 
`try()` of its own, and added the `inherits(call, "try-error")` test, to stop 
`tidyquery`'s revdep failure seen in #43317. That PR notes that no reproducer 
could be written at the time; the wrapper above is one, and it shows the guard 
added there has not been firing.
   
   I ran the code above on 26.0.1.1 only. From reading the source, the same 
lines are in every CRAN release from 17.0.0 (built from `maint-17.0.0-r`, which 
carries #43498) and on `main`. Up to 16.x, `abandon_ship()` received the call 
as an argument and this path did not exist.
   
   #### Why it matters for calling code
   
   Forwarding `...` to a dplyr verb is the ordinary way to write a function or 
package on top of dplyr, so this is reachable without anything unusual. The 
resulting `notSubsettableError` cannot be told apart from any other misuse of 
`$`, whereas `arrow_not_supported` can be caught and names the offending 
expression.
   
   #### Versions
   
   - arrow 26.0.1.1 (CRAN)
   - dplyr 1.2.1
   - R 4.6.2
   - macOS 27.2.1, aarch64
   
   Drafted with an AI assistant (Claude). The three code blocks marked as 
reprex output are unedited output from the versions listed.
   
   ### Component(s)
   
   R
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to