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

across() and pick() not translated inside distinct() #1125

Closed
lgaborini opened this issue Feb 3, 2023 · 3 comments
Closed

across() and pick() not translated inside distinct() #1125

lgaborini opened this issue Feb 3, 2023 · 3 comments
Milestone

Comments

@lgaborini
Copy link

lgaborini commented Feb 3, 2023

Since {dbplyr} 2.3.0 I cannot select columns from a lazy table using a vector of column names.
My distinct(across(all_of(...))) code is no longer working, not even with pick(...) .

library(dplyr)
library(dbplyr)

df <- mtcars
vec_cols <- c("gear", "cyl")

# newly deprecated in dplyr 1.1.0?
df |> distinct(across(all_of(vec_cols)))
#>                   gear cyl
#> Mazda RX4            4   6
#> Datsun 710           4   4
#> Hornet 4 Drive       3   6
#> Hornet Sportabout    3   8
#> Toyota Corona        3   4
#> Porsche 914-2        5   4
#> Ford Pantera L       5   8
#> Ferrari Dino         5   6

df |> distinct(across(.cols = all_of(vec_cols)))
#>                   gear cyl
#> Mazda RX4            4   6
#> Datsun 710           4   4
#> Hornet 4 Drive       3   6
#> Hornet Sportabout    3   8
#> Toyota Corona        3   4
#> Porsche 914-2        5   4
#> Ford Pantera L       5   8
#> Ferrari Dino         5   6

# Suggested replacement: working fine
df |> distinct(pick(all_of(vec_cols)))
#>                   gear cyl
#> Mazda RX4            4   6
#> Datsun 710           4   4
#> Hornet 4 Drive       3   6
#> Hornet Sportabout    3   8
#> Toyota Corona        3   4
#> Porsche 914-2        5   4
#> Ford Pantera L       5   8
#> Ferrari Dino         5   6

With dbplyr (I tried MSSQL and SQLite backends):

df_lazy <- dbplyr::tbl_lazy(df = mtcars, con = simulate_mssql())

# used to work
df_lazy |> distinct(across(all_of(vec_cols)))
#> Error in is_missing(x): promise already under evaluation: recursive default argument reference or earlier problems?
df_lazy |> distinct(across(.cols = all_of(vec_cols)))
#> Error in is_missing(x): promise already under evaluation: recursive default argument reference or earlier problems?

# not working
df_lazy |> distinct(pick(all_of(vec_cols)))
#> Error in var_is_null[[cur_var]] <- FALSE: attempt to select less than one element in OneIndex

I have also tried to force evaluation of vec_cols with !! but it didn't get it to work.
Am I using pick() and across() wrong now?

Created on 2023-02-03 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31 ucrt)
#>  os       Windows 10 x64 (build 19045)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_Switzerland.utf8
#>  ctype    English_Switzerland.utf8
#>  tz       Europe/Berlin
#>  date     2023-02-03
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package     * version date (UTC) lib source
#>    assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.2.0)
#>    blob          1.2.3   2022-04-10 [1] CRAN (R 4.2.0)
#>    cli           3.6.0   2023-01-09 [1] CRAN (R 4.2.2)
#>    DBI           1.1.3   2022-06-18 [1] CRAN (R 4.2.0)
#>    dbplyr      * 2.3.0   2023-01-16 [1] CRAN (R 4.2.2)
#>    digest        0.6.31  2022-12-11 [1] CRAN (R 4.2.2)
#>    dplyr       * 1.1.0   2023-01-29 [1] CRAN (R 4.2.2)
#>    evaluate      0.20    2023-01-17 [1] CRAN (R 4.2.2)
#>    fansi         1.0.4   2023-01-22 [1] CRAN (R 4.2.2)
#>    fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
#>    fs            1.6.0   2023-01-23 [1] CRAN (R 4.2.2)
#>    generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.1)
#>  P glue          1.6.2   2022-02-24 [?] CRAN (R 4.2.0)
#>    htmltools     0.5.4   2022-12-07 [1] CRAN (R 4.2.2)
#>    knitr         1.42    2023-01-25 [1] CRAN (R 4.2.2)
#>  P lifecycle     1.0.3   2022-10-07 [?] CRAN (R 4.2.1)
#>  P magrittr      2.0.3   2022-03-30 [?] CRAN (R 4.2.0)
#>  P pillar        1.8.1   2022-08-19 [?] CRAN (R 4.2.1)
#>  P pkgconfig     2.0.3   2019-09-22 [?] CRAN (R 4.2.0)
#>    purrr         1.0.1   2023-01-10 [1] CRAN (R 4.2.2)
#>  P R.cache       0.16.0  2022-07-21 [?] CRAN (R 4.2.1)
#>  P R.methodsS3   1.8.2   2022-06-13 [?] CRAN (R 4.2.0)
#>  P R.oo          1.25.0  2022-06-12 [?] CRAN (R 4.2.0)
#>    R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.2.2)
#>  P R6            2.5.1   2021-08-19 [?] CRAN (R 4.2.0)
#>    reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.1)
#>  P rlang         1.0.6   2022-09-24 [?] CRAN (R 4.2.1)
#>    rmarkdown     2.20    2023-01-19 [1] CRAN (R 4.2.2)
#>    rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.1)
#>    sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>    styler        1.9.0   2023-01-15 [1] CRAN (R 4.2.2)
#>  P tibble        3.1.8   2022-07-22 [?] CRAN (R 4.2.1)
#>    tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.2.1)
#>  P utf8          1.2.2   2021-07-24 [?] CRAN (R 4.2.0)
#>    vctrs         0.5.2   2023-01-23 [1] CRAN (R 4.2.2)
#>  P withr         2.5.0   2022-03-03 [?] CRAN (R 4.2.0)
#>    xfun          0.36    2022-12-21 [1] CRAN (R 4.2.2)
#>    yaml          2.3.7   2023-01-23 [1] CRAN (R 4.2.2)
#> 
#>  P ── Loaded and on-disk path mismatch.
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@lgaborini
Copy link
Author

I confirm it was working fine with dbplyr 2.2.1 and dplyr 1.1.0 (providing pick()):

df_lazy <- dbplyr::tbl_lazy(df = mtcars, con = simulate_mssql())


df_lazy |> distinct(across(all_of(vec_cols)))
#> <SQL>
#> SELECT DISTINCT `gear`, `cyl`
#> FROM `df`
df_lazy |> distinct(across(.cols = all_of(vec_cols)))
#> <SQL>
#> SELECT DISTINCT `gear`, `cyl`
#> FROM `df`

df_lazy |> distinct(pick(all_of(vec_cols)))
#> <SQL>
#> SELECT DISTINCT pick(all_of(('gear', 'cyl'))) AS `pick(all_of(vec_cols))`
#> FROM `df`

Created on 2023-02-03 with reprex v2.0.2

@mgirlich mgirlich added this to the 2.3.1 milestone Feb 3, 2023
@mgirlich
Copy link
Collaborator

mgirlich commented Feb 6, 2023

Thanks for filing the issue. Fixed by 9eaaa8c.

@mgirlich mgirlich closed this as completed Feb 6, 2023
@lgaborini
Copy link
Author

Thanks! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants