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

Input data.table can be modified despite having immutable=TRUE in lazy_dt() #210

Closed
jatherrien opened this issue Mar 2, 2021 · 1 comment · Fixed by #366
Closed

Input data.table can be modified despite having immutable=TRUE in lazy_dt() #210

jatherrien opened this issue Mar 2, 2021 · 1 comment · Fixed by #366
Labels
bug an unexpected problem or unintended behavior

Comments

@jatherrien
Copy link

jatherrien commented Mar 2, 2021

Despite specifying immutable=TRUE in lazy_dt, if I have a mutate followed by a summarize followed by a mutate, the first mutate will get applied to the input data.table object, when it should not be modified. I can't seem to trigger it unless I have a mutate after the summarize.

library(data.table)
library(dtplyr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:data.table':
#> 
#>     between, first, last
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

d1= data.table(x=c(1,1,1,2,2,2), y=c(1,2,3,4,5,6))

d2 = lazy_dt(d1, immutable = TRUE) %>%
  mutate(
    y=-y,
    z = x+y
    ) %>%
  summarize(
    mean_y = mean(y),
    mean_z = mean(z)
    ) %>%
  mutate(
    sum_mean = mean_y + mean_z
  ) %>%
  as.data.table()


d1
#>    x  y  z
#> 1: 1 -1  0
#> 2: 1 -2 -1
#> 3: 1 -3 -2
#> 4: 2 -4 -2
#> 5: 2 -5 -3
#> 6: 2 -6 -4

Here's my sessionInfo():

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

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

other attached packages:
[1] dplyr_1.0.3       dtplyr_1.1.0      data.table_1.13.6

loaded via a namespace (and not attached):
 [1] assertthat_0.2.1 crayon_1.4.0     R6_2.5.0         DBI_1.1.1        lifecycle_0.2.0  magrittr_2.0.1   pillar_1.4.7     rlang_0.4.10    
 [9] vctrs_0.3.6      generics_0.1.0   ellipsis_0.3.1   tools_4.0.3      glue_1.4.2       purrr_0.3.4      compiler_4.0.3   pkgconfig_2.0.3 
[17] tidyselect_1.1.0 tibble_3.0.5 
@hadley
Copy link
Member

hadley commented Mar 2, 2021

Somewhat more minimal reprex:

library(data.table)
library(dtplyr)
library(dplyr, warn.conflicts = FALSE)

d1 <- data.table(x = c(1,1,1,2,2,2), y = c(1,2,3,4,5,6))

lazy_dt(d1) %>%
  mutate(z1 = 1) %>%
  summarize(z2 = 2) %>%
  mutate(z3 = 4) %>%
  show_query()
#> `_DT1`[, `:=`(z1 = 1)][, .(z2 = 2)][, `:=`(z3 = 4)]

Created on 2021-03-02 by the reprex package (v1.0.0)

Probably something is getting confused here because while the summarise() creates an implicit copy, there's still a mutate before it.

@markfairbanks markfairbanks added the bug an unexpected problem or unintended behavior label Jun 21, 2022
This was referenced Jun 22, 2022
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants