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

vec_match() fails on class "Date" and "IDate" with obscure error #1549

Open
beansrowning opened this issue Mar 25, 2022 · 2 comments
Open

vec_match() fails on class "Date" and "IDate" with obscure error #1549

beansrowning opened this issue Mar 25, 2022 · 2 comments
Labels

Comments

@beansrowning
Copy link

I've noticed in several of my projects where I sometimes use data.table and dplyr together that an obscure error can arise if you attempt to perform a mutating join on a Date formated column with an inferred IDate/Date column as read in by data.table::fread()

Error: Can't join on `x$date` x `y$date` because of incompatible types.
i `x$date` is of type <date>>.
i `y$date` is of type <date>>.
Run `rlang::last_error()` to see where the error occurred.

This error was unintelligible to me and took a while to debug. I was able to browse into dplyr:::join_rows() and saw the offending line was the vec_match() call on line 7, yet the join columns of both were reported to be of type "Date" via the error and print.tbl_df:

image
image

I finally realized the column in the tibble originally sourced from fread() was of class "IDate", "Date". Which seems to trip vec_match()

image
image

I'm not sure what solution makes sense to make this more explicit to the end user, but as it stands it's hard to reason through.

Reprex

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

a <- tibble(
  a = letters,
  b = seq(Sys.Date(), Sys.Date() + 25, "days")
)

print(a)
#> # A tibble: 26 x 2
#>    a     b         
#>    <chr> <date>    
#>  1 a     2022-03-25
#>  2 b     2022-03-26
#>  3 c     2022-03-27
#>  4 d     2022-03-28
#>  5 e     2022-03-29
#>  6 f     2022-03-30
#>  7 g     2022-03-31
#>  8 h     2022-04-01
#>  9 i     2022-04-02
#> 10 j     2022-04-03
#> # ... with 16 more rows

write_csv(a, "test.csv")

b <- fread("test.csv") %>%
  mutate(d = LETTERS)

a %>%
  as_tibble() %>%
  print()
#> # A tibble: 26 x 2
#>    a     b         
#>    <chr> <date>    
#>  1 a     2022-03-25
#>  2 b     2022-03-26
#>  3 c     2022-03-27
#>  4 d     2022-03-28
#>  5 e     2022-03-29
#>  6 f     2022-03-30
#>  7 g     2022-03-31
#>  8 h     2022-04-01
#>  9 i     2022-04-02
#> 10 j     2022-04-03
#> # ... with 16 more rows

left_join(a, b)
#> Joining, by = c("a", "b")
#> Error: Can't join on `x$b` x `y$b` because of incompatible types.
#> i `x$b` is of type <date>>.
#> i `y$b` is of type <date>>.

sessionInfo()
#> R version 4.1.2 (2021-11-01)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19044)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> system code page: 65001
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] readr_2.1.0       data.table_1.14.2 dplyr_1.0.7      
#> 
#> loaded via a namespace (and not attached):
#>  [1] pillar_1.6.4      compiler_4.1.2    highr_0.9         R.methodsS3_1.8.1
#>  [5] R.utils_2.11.0    tools_4.1.2       bit_4.0.4         digest_0.6.28    
#>  [9] evaluate_0.14     lifecycle_1.0.1   tibble_3.1.6      R.cache_0.15.0   
#> [13] pkgconfig_2.0.3   rlang_0.4.12      reprex_2.0.1      cli_3.1.0        
#> [17] DBI_1.1.2         parallel_4.1.2    yaml_2.2.1        xfun_0.28        
#> [21] fastmap_1.1.0     withr_2.4.3       styler_1.6.2      stringr_1.4.0    
#> [25] knitr_1.37        hms_1.1.1         generics_0.1.1    fs_1.5.0         
#> [29] vctrs_0.3.8       bit64_4.0.5       tidyselect_1.1.1  glue_1.5.0       
#> [33] R6_2.5.1          fansi_0.5.0       vroom_1.5.6       rmarkdown_2.11   
#> [37] tzdb_0.2.0        purrr_0.3.4       magrittr_2.0.1    backports_1.3.0  
#> [41] ellipsis_0.3.2    htmltools_0.5.2   assertthat_0.2.1  utf8_1.2.2       
#> [45] stringi_1.7.5     crayon_1.4.2      R.oo_1.24.0

Created on 2022-03-25 by the reprex package (v2.0.1)

@DavisVaughan
Copy link
Member

It is somewhat confusing that vec_ptype_abbr() and vec_ptype_full() can utilize inheritance, meaning that the vec_ptype_abbr.Date method is hit here, which is why the results look so confusing.

library(vctrs)

x <- Sys.Date()
y <- data.table::as.IDate(x)

y
#> [1] "2022-03-25"
class(y)
#> [1] "IDate" "Date"

# Calls `vec_ptype_abbr.Date` through inheritance
vec_ptype_abbr(y)
#> [1] "date"

# Making this confusing
vec_ptype2(x, y)
#> Error in `stop_vctrs()`:
#> ! Can't combine <date> and <date>.



# Inject methods
vec_ptype_full.IDate <- function(x, ...) {
  "idate"
}
vec_ptype_abbr.IDate <- function(x, ...) {
  "idate"
}

vec_ptype_abbr(y)
#> [1] "idate"

vec_ptype2(x, y)
#> Error in `stop_vctrs()`:
#> ! Can't combine <date> and <idate>.

I'm not sure that we should try and provide ptype2 methods for Date and IDate. That doesn't seem like our place since we don't own IDate.

It is possible we should make vec_ptype_abbr() and vec_ptype_full() not utilize inheritance though.

@DavisVaughan
Copy link
Member

See also tidyverse/dplyr#6230 (comment)

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

No branches or pull requests

3 participants