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

manipulating a variable inside a mutate statement more than two times times fails silently #412

Closed
lorenzwalthert opened this issue Apr 6, 2020 · 2 comments
Labels
bug an unexpected problem or unintended behavior verb trans 🤖 Translation of dplyr verbs to SQL

Comments

@lorenzwalthert
Copy link
Contributor

lorenzwalthert commented Apr 6, 2020

It seems to works for two re-assignments, but after that, it looks as the first assignment is taken twice and the second one is left out.
I use the current CRAN version 1.4.2 of {dbplyr}.

library(dbplyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:dbplyr':
#> 
#>     ident, sql
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
tbl_lazy(df = mtcars) %>%
  mutate(
    hp = .data$hp + 1,
    hp = .data$hp + 2,
    hp = .data$hp + 4
  ) %>%
  show_query()
#> <SQL>
#> SELECT `mpg`, `cyl`, `disp`, `hp` + 4.0 AS `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`
#> FROM (SELECT `mpg`, `cyl`, `disp`, `hp` + 1.0 AS `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`
#> FROM (SELECT `mpg`, `cyl`, `disp`, `hp` + 1.0 AS `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb`
#> FROM `df`) `dbplyr_001`) `dbplyr_002`

Created on 2020-04-06 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.3 (2020-02-29)
#>  os       Ubuntu 18.04.4 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Zurich               
#>  date     2020-04-06                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  ! package                * version    date       lib
#>    askpass                  1.1        2019-01-13 [1]
#>    assertthat               0.2.1      2019-03-21 [1]
#>    backports                1.1.5      2019-10-02 [1]
#>    base64enc                0.1-3      2015-07-28 [1]
#>    base64url                1.4        2018-05-14 [1]
#>    callr                    3.4.2      2020-02-12 [1]
#>    class                    7.3-16     2020-03-25 [4]
#>    cli                      2.0.2      2020-02-28 [1]
#>    colorspace               1.4-1      2019-03-18 [1]
#>    crayon                   1.3.4      2017-09-16 [1]
#>    DBI                      1.1.0      2019-12-15 [1]
#>    dbplyr                 * 1.4.2      2019-06-17 [1]
#>    desc                     1.2.0      2018-05-01 [1]
#>    devtools                 2.2.2      2020-02-17 [1]
#>    digest                   0.6.25     2020-02-23 [1]
#>    dplyr                  * 0.8.5      2020-03-07 [1]
#>    drake                    7.12.0     2020-03-25 [1]
#>    drogger                  0.1.9003   2019-12-05 [1]
#>    edison.infra           * 0.0.1.9043 2020-04-06 [1]
#>  P edison.model.reoprices * 0.0.0.9000 2020-03-09 [?]
#>    ellipsis                 0.3.0      2019-09-20 [1]
#>    evaluate                 0.14       2019-05-28 [1]
#>    fansi                    0.4.1      2020-01-08 [1]
#>    feather                  0.3.5      2019-09-15 [1]
#>    filelock                 1.0.2      2018-10-05 [1]
#>    forge                    0.2.0      2019-02-26 [1]
#>    formatR                  1.7        2019-06-11 [1]
#>    fs                       1.4.1      2020-04-04 [1]
#>    futile.logger            1.4.3      2016-07-10 [1]
#>    futile.options           1.0.1      2018-04-20 [1]
#>    generics                 0.0.2      2018-11-29 [1]
#>    ggplot2                  3.3.0      2020-03-05 [1]
#>    glue                     1.4.0      2020-04-03 [1]
#>    gower                    0.2.1      2019-05-14 [1]
#>    gtable                   0.3.0      2019-03-25 [1]
#>    highr                    0.8        2019-03-20 [1]
#>    hms                      0.5.3      2020-01-08 [1]
#>    htmltools                0.4.0      2019-10-04 [1]
#>    httpuv                   1.5.2      2019-09-11 [1]
#>    httr                     1.4.1      2019-08-05 [1]
#>    igraph                   1.2.5      2020-03-19 [1]
#>    ini                      0.3.1      2018-05-20 [1]
#>    ipred                    0.9-9      2019-04-28 [1]
#>    jsonlite                 1.6.1      2020-02-02 [1]
#>    knitr                    1.28       2020-02-06 [1]
#>    lambda.r                 1.2.4      2019-09-18 [1]
#>    later                    1.0.0      2019-10-04 [1]
#>    lattice                  0.20-40    2020-02-19 [4]
#>    lava                     1.6.7      2020-03-05 [1]
#>    lifecycle                0.2.0      2020-03-06 [1]
#>    lubridate                1.7.4      2018-04-11 [1]
#>    magrittr               * 1.5        2014-11-22 [1]
#>    MASS                     7.3-51.5   2019-12-20 [4]
#>    Matrix                   1.2-18     2019-11-27 [4]
#>    memoise                  1.1.0      2017-04-21 [1]
#>    mlflow                   1.6.0      2020-04-06 [1]
#>    munsell                  0.5.0      2018-06-12 [1]
#>    nnet                     7.3-13     2020-02-25 [4]
#>    openssl                  1.4.1      2019-07-18 [1]
#>    pillar                   1.4.3      2019-12-20 [1]
#>    pkgbuild                 1.0.6      2019-10-09 [1]
#>    pkgconfig                2.0.3      2019-09-22 [1]
#>    pkgload                  1.0.2      2018-10-29 [1]
#>    prettyunits              1.1.1      2020-01-24 [1]
#>    processx                 3.4.2      2020-02-09 [1]
#>    prodlim                  2019.11.13 2019-11-17 [1]
#>    progress                 1.2.2      2019-05-16 [1]
#>    promises                 1.1.0      2019-10-04 [1]
#>    ps                       1.3.2      2020-02-13 [1]
#>    purrr                    0.3.3      2019-10-18 [1]
#>    R6                       2.4.1      2019-11-12 [1]
#>    Rcpp                     1.0.4      2020-03-17 [1]
#>    recipes                  0.1.10     2020-03-18 [1]
#>    remotes                  2.1.1      2020-02-15 [1]
#>    reticulate               1.15       2020-04-02 [1]
#>    rlang                    0.4.5      2020-03-01 [1]
#>    rmarkdown                2.1        2020-01-20 [1]
#>    rpart                    4.1-15     2019-04-12 [4]
#>    rprojroot                1.3-2      2018-01-03 [1]
#>    rstudioapi               0.11       2020-02-07 [1]
#>    scales                   1.1.0      2019-11-18 [1]
#>    sessioninfo              1.1.1      2018-11-05 [1]
#>    storr                    1.2.1      2018-10-18 [1]
#>    stringi                  1.4.6      2020-02-17 [1]
#>    stringr                  1.4.0      2019-02-10 [1]
#>    survival                 3.1-11     2020-03-07 [4]
#>    swagger                  3.9.2      2018-03-23 [1]
#>    testthat                 2.3.2      2020-03-02 [1]
#>    tibble                   3.0.0      2020-03-30 [1]
#>    tidyselect               1.0.0      2020-01-27 [1]
#>    timeDate                 3043.102   2018-02-21 [1]
#>    txtq                     0.2.0      2019-10-15 [1]
#>    udldb                    0.0.0.9006 2020-04-01 [1]
#>    usethis                * 1.5.1      2019-07-04 [1]
#>    vctrs                    0.2.4      2020-03-10 [1]
#>    withr                    2.1.2      2018-03-15 [1]
#>    xfun                     0.12       2020-01-13 [1]
#>    yaml                     2.2.1      2020-02-01 [1]
#>    zeallot                  0.1.0      2018-01-28 [1]
#>  source                                 
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  Github (lorenzwalthert/drogger@aab9768)
#>  local                                  
#>  local                                  
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  Github (lorenzwalthert/mlflow@9e2d8f3) 
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.1)                         
#>  local                                  
#>  CRAN (R 3.6.1)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.2)                         
#>  CRAN (R 3.6.3)                         
#>  CRAN (R 3.6.1)                         
#> 
#> [1] /home/walor/R/x86_64-pc-linux-gnu-library/3.6
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library
#> 
#>  P ── Loaded and on-disk path mismatch.

Works for local tibbles as expected:

local <- mutate(mtcars,
  hp = .data$hp + 1,
  hp = .data$hp + 2,
  hp = .data$hp + 4
)
all(local$hp == mtcars$hp + 7)
#> [1] TRUE

Also thanks for this package it's really a wonderful tool I use all the time and it saves me so much time and keeps code base maintenance efforts low.

@hadley
Copy link
Member

hadley commented Apr 14, 2020

More minimal reprex:

library(dbplyr)
library(dplyr, warn.conflicts = FALSE)

db <- memdb_frame(x = 1)

db %>% mutate(x = x + 1, x = x + 2, x = x + 4) %>% show_query()
#> <SQL>
#> SELECT `x` + 4.0 AS `x`
#> FROM (SELECT `x` + 1.0 AS `x`
#> FROM (SELECT `x` + 1.0 AS `x`
#> FROM `dbplyr_001`))

Created on 2020-04-14 by the reprex package (v0.3.0)

@hadley hadley added bug an unexpected problem or unintended behavior verb trans 🤖 Translation of dplyr verbs to SQL labels Apr 14, 2020
@hadley
Copy link
Member

hadley commented Sep 24, 2020

Still more minimal reprex:

library(dbplyr)
db <- lazy_frame(x = 1)
db %>% dplyr::mutate(x = x + 1L, x = x + 2L, x = x + 4L)
#> <SQL>
#> SELECT `x` + 4 AS `x`
#> FROM (SELECT `x` + 1 AS `x`
#> FROM (SELECT `x` + 1 AS `x`
#> FROM `df`) `q01`) `q02`

Created on 2020-09-24 by the reprex package (v0.3.0.9001)

@hadley hadley closed this as completed in 3f4ef11 Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior verb trans 🤖 Translation of dplyr verbs to SQL
Projects
None yet
Development

No branches or pull requests

2 participants