r-lib / vctrs

Generic programming with typed R vectors
https://vctrs.r-lib.org
Other
290 stars 66 forks source link

`vec_ptype()` materializes date, time, and logical vectors #1956

Open krlmlr opened 1 month ago

krlmlr commented 1 month ago

I thought that getting the ptype should be possible without accessing the vector. This currently fails for date, time, and logical columns.

Important for duckplyr because we want to do things differently depending on the column type but without materializing. I can work around if I must, ideally, I'd just rely on vctrs.

CC @hannes.

options(conflicts.policy = list(warn = FALSE))
library(duckplyr)
#> ✔ Overwriting dplyr methods with duckplyr methods.
#> ℹ Turn off with `duckplyr::methods_restore()`.

Sys.setenv(DUCKPLYR_FORCE = TRUE)

data.frame(a = Sys.Date()) |>
  pull() |>
  vctrs::vec_ptype()
#> materializing:
#> ---------------------
#> --- Relation Tree ---
#> ---------------------
#> Projection [a as a]
#>   r_dataframe_scan(0x150fb1fb0)
#> 
#> ---------------------
#> -- Result Columns  --
#> ---------------------
#> - a (DATE)
#> Date of length 0

data.frame(a = clock::date_time_set_zone(Sys.time(), "UTC")) |>
  pull() |>
  vctrs::vec_ptype()
#> materializing:
#> ---------------------
#> --- Relation Tree ---
#> ---------------------
#> r_dataframe_scan(0x130efe8b0)
#> 
#> ---------------------
#> -- Result Columns  --
#> ---------------------
#> - a (TIMESTAMP)
#> 
#> materializing:
#> ---------------------
#> --- Relation Tree ---
#> ---------------------
#> Projection [a as a]
#>   r_dataframe_scan(0x130efe8b0)
#> 
#> ---------------------
#> -- Result Columns  --
#> ---------------------
#> - a (TIMESTAMP)
#> POSIXct of length 0

data.frame(a = FALSE) |>
  pull() |>
  vctrs::vec_ptype()
#> materializing:
#> ---------------------
#> --- Relation Tree ---
#> ---------------------
#> Projection [a as a]
#>   r_dataframe_scan(0x127b87498)
#> 
#> ---------------------
#> -- Result Columns  --
#> ---------------------
#> - a (BOOLEAN)
#> logical(0)

data.frame(a = 1) |>
  pull() |>
  vctrs::vec_ptype()
#> numeric(0)

data.frame(a = 1L) |>
  pull() |>
  vctrs::vec_ptype()
#> integer(0)

data.frame(a = "x") |>
  pull() |>
  vctrs::vec_ptype()
#> character(0)

Created on 2024-10-23 with reprex v2.1.1