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

Support 'Range between' along with 'rows between' in window_frame #1435

Open
talegari opened this issue Jan 2, 2024 · 1 comment
Open

Support 'Range between' along with 'rows between' in window_frame #1435

talegari opened this issue Jan 2, 2024 · 1 comment
Labels
feature a feature request or enhancement

Comments

@talegari
Copy link

talegari commented Jan 2, 2024

Right now, dbplyr::window_frame checks if from and to are numbers.

  check_number_whole(from, allow_infinite = TRUE)
  check_number_whole(to, allow_infinite = TRUE)

It would be great to have support interval arguments for from/to so that dbplyr::window_frame will support 'Range between' queries. Here is an modified example from duckdb's window functions page:

SELECT "plant", "date",
    min("mwh") OVER seven AS "min_mwh",
FROM "history"
WINDOW seven AS (
    PARTITION BY "plant"
    ORDER BY "date" ASC
    RANGE BETWEEN INTERVAL 1 MONTHS PRECEDING
              AND INTERVAL 3 DAYS FOLLOWING)
ORDER BY 1, 2;

Since, lubridate provides period/interval objects like lubridate::days(), dbplyr should ideally translate the below code to the sql code above:

history_tbl |>
  group_by(plant) |>
  window_order(date) |>
  window_frame(from = months(1), to = days(3)) |> # slider like syntax
  mutate(min_mwh = min(mwh)) |>
  ungroup() |>
  arrange(plant, date)
@hadley
Copy link
Member

hadley commented Jan 10, 2024

I don't think this is a simple change to window_frame() because you can also use numeric values in RANGE mode. So this would likely need to either be a separate function or we'd need to add a mode argument to window_frame().

@hadley hadley added the feature a feature request or enhancement label Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants