tidyverse / dbplyr

Database (DBI) backend for dplyr
https://dbplyr.tidyverse.org
Other
474 stars 173 forks source link

Object `.data[[some_string]]` is not translated correctly when used with `dpyr::across` in `dplyr::mutate` #1520

Open andreassoteriadesmoj opened 3 months ago

andreassoteriadesmoj commented 3 months ago

Object .data[[some_string]] Is not translated correctly when used with dpyr::across in dplyr::mutate. Find below two examples: one without the use of dpyr::across that correctly translates .data[[some_string]]; and one with the use of dpyr::across that fails to correctly translate .data[[some_string]].

library(magrittr)

mtcars_db <- dbplyr::memdb_frame(mtcars)
my_col <- 'carb'

# Trivial mutating: when gear >= 4, assign NA, otherwise assign values from carb #
# Attempt 1: without dplyr::across
x1 <- mtcars_db %>% 
  dplyr::mutate(
    x = dplyr::case_when(
      gear >= 4 ~ NA,
      TRUE ~ .data[[my_col]]
    )
  )

# Attempt 2: with dplyr::across
x2 <- mtcars_db %>% 
  dplyr::mutate(
    dplyr::across(
      dplyr::all_of(
        'gear'
      ),
      ~ 
        dplyr::case_when(
          . >= 4 ~ NA,
          TRUE ~ .data[[my_col]]
        ),

      .names = 'x'
    )
  )

x1

# # Source:   SQL [?? x 12]
# # Database: sqlite 3.46.0 [:memory:]
# mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb     x
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#   1  21       6  160    110  3.9   2.62  16.5     0     1     4     4    NA
# 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4    NA
# 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1    NA
# 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1     1
# 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2     2
# 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1     1
# 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4     4
# 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2    NA
# 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2    NA
# 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4    NA
# # ℹ more rows
# # ℹ Use `print(n = ...)` to see more rows

x2

# Error in `collect()`:
#   ! Failed to collect lazy table.
# Caused by error:
#   ! no such column: .data.carb
# Run `rlang::last_trace()` to see where the error occurred.

In x1, .data[[my_col]] is correctly translated:

dbplyr::sql_render(x1)

# <SQL> SELECT
# `dbplyr_MemBTBwTpc`.*,
# CASE WHEN (`gear` >= 4.0) THEN NULL ELSE `carb` END AS `x`

In x2, .data[[my_col]] isn't correctly translated (see .data.carb in the SQL code):

dbplyr::sql_render(x2)

# <SQL> SELECT
#   `dbplyr_MemBTBwTpc`.*,
#   CASE WHEN (`gear` >= 4.0) THEN NULL ELSE (`.data`.`carb`) END AS `x`
# FROM `dbplyr_MemBTBwTpc`

Session info:

R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] magrittr_2.0.3   offendeR_1.2.2   Rdbtools_0.5.2   testthat_3.2.1.1

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.1             dplyr_1.1.4                  blob_1.2.4                  
 [4] fastmap_1.2.0                promises_1.3.0               digest_0.6.35               
 [7] mime_0.12                    lifecycle_1.0.4              ellipsis_0.3.2              
[10] RSQLite_2.3.7                paws.storage_0.6.0           compiler_4.4.1              
[13] rlang_1.1.4                  config_0.3.2                 tools_4.4.1                 
[16] utf8_1.2.4                   yaml_2.3.8                   data.table_1.15.4           
[19] knitr_1.47                   htmlwidgets_1.6.4            bit_4.0.5                   
[22] pkgbuild_1.4.4               curl_5.2.1                   xml2_1.3.6                  
[25] pkgload_1.3.4                miniUI_0.1.1.1               withr_3.0.0                 
[28] purrr_1.0.2                  paws_0.6.0                   desc_1.4.3                  
[31] fansi_1.0.6                  urlchecker_1.0.1             profvis_0.3.8               
[34] xtable_1.8-4                 cli_3.6.2                    rmarkdown_2.27              
[37] crayon_1.5.2                 generics_0.1.3               remotes_2.5.0               
[40] rstudioapi_0.16.0            httr_1.4.7                   tzdb_0.4.0                  
[43] sessioninfo_1.2.2            DBI_1.2.3                    cachem_1.1.0                
[46] stringr_1.5.1                assertthat_0.2.1             vctrs_0.6.5                 
[49] devtools_2.4.5               jsonlite_1.8.8               hms_1.1.3                   
[52] bit64_4.0.5                  noctua_2.6.2                 glue_1.7.0                  
[55] stringi_1.8.4                later_1.3.2                  tibble_3.2.1                
[58] pillar_1.9.0                 htmltools_0.5.8.1            brio_1.1.5                  
[61] paws.security.identity_0.6.1 R6_2.5.1                     dbplyr_2.5.0                
[64] paws.analytics_0.6.0         rprojroot_2.0.4              evaluate_0.24.0             
[67] shiny_1.8.1.1                readr_2.1.5                  memoise_2.0.1               
[70] renv_0.15.4                  httpuv_1.6.15                paws.common_0.7.3           
[73] Rcpp_1.0.12                  uuid_1.2-0                   xfun_0.45                   
[76] fs_1.6.4                     usethis_2.2.3                pkgconfig_2.0.3