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

Implement collect_list in window context #1527

Open
talegari opened this issue Jul 27, 2024 · 0 comments
Open

Implement collect_list in window context #1527

talegari opened this issue Jul 27, 2024 · 0 comments

Comments

@talegari
Copy link

From this posit community post: https://forum.posit.co/t/unexpected-sql-with-mutate-in-sparklyr/189492/2

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(lubridate))

rep_df <-tibble::tibble(
    user_id = sample(1:100, 1000, replace = TRUE),
    dates   = seq(as_date("2024-01-01"), as_date("2024-02-01"), 1) %>% 
      sample(1000, replace = TRUE),
    amount  = sample(seq(100, 900, 100), 1000, replace = TRUE)
  ) %>% 
  summarise(amount = sum(amount), .by = c(user_id, dates))

dbplyr::lazy_frame(rep_df) |> 
  group_by(user_id) %>%
  dbplyr::window_order(dates) %>%
  dbplyr::window_frame(-Inf, -1) %>%
  mutate(list_amount = mean(amount)) %>% 
  dbplyr::sql_render()
#> Warning: Missing values are always removed in SQL aggregation functions.
#> Use `na.rm = TRUE` to silence this warning
#> This warning is displayed once every 8 hours.
#> <SQL> SELECT
#>   `df`.*,
#>   AVG(`amount`) OVER (PARTITION BY `user_id` ORDER BY `dates` ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) AS `list_amount`
#> FROM `df`
dbplyr::lazy_frame(rep_df) |> 
  group_by(user_id) %>%
  dbplyr::window_order(dates) %>%
  dbplyr::window_frame(-Inf, -1) %>%
  mutate(list_amount = my_func(amount)) %>% 
  dbplyr::sql_render()
#> <SQL> SELECT `df`.*, my_func(`amount`) AS `list_amount`
#> FROM `df`
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

1 participant