Skip to content

n() ignores ordering in window_frame #700

@mattcane

Description

@mattcane

When using n() in an ordered window frame the ordering seems to be ignored when it's translated to SQL. Use case would be knowing how many rows are actually included in a rolling sum/average/etc.

mf <- memdb_frame(
  tibble(
    grp = c(rep('a',4), rep('b', 5)),
    order = seq(1,9),
    val = seq(11,19)
  )
)

mf %>%
  group_by(
    grp
  ) %>%
  # Arrange by order
  window_order(order) %>%
  # Calc rolling 2 row sum
  window_frame(-2,-1) %>%
  mutate(
    roll_last_2_sum = sum(val),
    # Get number of rows in frame
    n_rows_in_frame = n()
  ) %>%
  show_query()

# Output is:
# SELECT `grp`, `order`, `val`, SUM(`val`) OVER (PARTITION BY `grp` ORDER BY `order` ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS `roll_last_2_sum`, COUNT(*) OVER (PARTITION BY `grp`) AS `n_rows_in_frame`

# Output should be:
# SELECT `grp`, `order`, `val`, SUM(`val`) OVER (PARTITION BY `grp` ORDER BY `order` ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS `roll_last_2_sum`, COUNT(*) OVER (PARTITION BY `grp` ORDER BY `order` ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING) AS `n_rows_in_frame`

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementfunc trans 🌍Translation of individual functions to SQL

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions