Skip to content

GH-45373: [R] summarize after arrange fails - #51312

Merged
thisisnic merged 5 commits into
apache:mainfrom
thisisnic:GH-45373-arrange-summarise
Sep 16, 2026
Merged

thisisnic merged 5 commits into
apache:mainfrom
thisisnic:GH-45373-arrange-summarise

Conversation

@thisisnic

@thisisnic thisisnic commented Sep 12, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

arrange() then summarise() failed

What changes are included in this PR?

Reset ordering vars before summarise like dplyr etc do anyway

Are these changes tested?

Yep

Are there any user-facing changes?

No

@thisisnic
thisisnic requested a review from jonkeane as a code owner September 12, 2026 11:48
Copilot AI lite review requested due to automatic review settings September 12, 2026 11:48
@thisisnic thisisnic changed the title GH-45373:[R] summarize after arrange fails GH-45373: [R] summarize after arrange fails Sep 12, 2026
@github-actions github-actions Bot added the awaiting committer review Awaiting committer review label Sep 12, 2026
@github-actions

Copy link
Copy Markdown

⚠️ GitHub issue #45373 has been automatically assigned in GitHub to PR creator.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Pending head()/tail() operations can still aggregate the wrong rows; apply slicing before aggregation and add regression tests.

Pull request overview

Fixes deferred arrange() metadata issues in R dplyr aggregation pipelines.

Changes:

  • Clears stale sorting before summarize() and aggregate-backed mutate().
  • Adds regression tests for affected pipelines.
File summaries
File Description
r/tests/testthat/test-dplyr-summarize.R Adds summarize regression tests.
r/tests/testthat/test-dplyr-mutate.R Adds aggregate-mutate regression tests.
r/R/dplyr-summarize.R Resets sorting before summarization.
r/R/dplyr-mutate.R Resets sorting in aggregation subqueries.
Review details

Suppressed comments (2)

r/R/dplyr-mutate.R:79

  • Clearing the sort here has the same incorrect interaction with a pending head()/tail() on the input. For example, arrange(int) |> head(3) |> mutate(avg = mean(int, na.rm = TRUE)) computes avg over the full input in the aggregation side of the join, then returns only three rows, rather than computing it over the three selected rows. Please apply the slice before aggregating (or reject this combination) and add a regression test.
      agg_query$arrange_vars <- list()
      agg_query$arrange_desc <- logical()

r/R/dplyr-summarize.R:92

  • This also drops the pending sort that may be needed by a following head()/tail() on the input. For example, arrange(int) |> head(3) |> summarize(total = sum(int, na.rm = TRUE)) now aggregates all rows and only then applies the fetch to the one-row aggregate, instead of aggregating the selected three rows; before this change it failed on the missing sort key. Please apply the slice before aggregating (or reject this combination) and add a regression test.
  .data$arrange_vars <- list()
  .data$arrange_desc <- logical()
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI review requested due to automatic review settings September 12, 2026 12:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved review issues remain, and regression tests are included.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 16, 2026 14:35
@thisisnic
thisisnic force-pushed the GH-45373-arrange-summarise branch from ad56e11 to bc9b73f Compare September 16, 2026 14:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The row-limit regression tests do not verify that sorting is honored because their input is already sorted.

Review details

Suppressed comments (1)

r/tests/testthat/test-dplyr-summarize.R:1361

  • This head(3) regression case does not distinguish sorted from unsorted input: tbl is example_data, whose int values are already ascending, so taking the first three rows gives the same result even if arrange() is ignored. Use a descending or shuffled input here (and in the analogous mutate test) so the assertion actually verifies that the row limit is applied after sorting.
    .input |>
      arrange(int) |>
      head(3) |>
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 16, 2026 16:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Two regression assertions do not adequately detect dropped sorting or limiting behavior.

Review details

Suppressed comments (2)

r/tests/testthat/test-dplyr-mutate.R:794

  • This assertion does not distinguish the sorted head(3) rows from the full input: with int = 1:3, NA, 5:10, max(int) is 10 in both cases. A regression that drops the limit/sort while building the aggregation query would still pass, so use an aggregate whose result changes (for example min_int, which should be 8 for the descending top three versus 1 for the full input).
      mutate(max_int = max(int, na.rm = TRUE)) |>

r/tests/testthat/test-dplyr-summarize.R:1362

  • This assertion does not distinguish the sorted head(3) rows from the full input: with int = 1:3, NA, 5:10, max(int) is 10 in both cases. A regression that drops the limit/sort while building the aggregation query would still pass, so use an aggregate whose result changes (for example min_int, which should be 8 for the descending top three versus 1 for the full input).
      summarize(max_int = max(int, na.rm = TRUE)) |>
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Copilot AI review requested due to automatic review settings September 16, 2026 18:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved review issues were identified.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Comment thread r/R/dplyr-mutate.R
Comment on lines +78 to +79
agg_query$arrange_vars <- list()
agg_query$arrange_desc <- logical()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these then just referenced elsewhere without checking if they exist? Was that the bug? Wild!

@jonkeane jonkeane left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this

@github-actions github-actions Bot added awaiting changes Awaiting changes and removed awaiting committer review Awaiting committer review labels Sep 16, 2026
@github-actions github-actions Bot added awaiting merge Awaiting merge and removed awaiting changes Awaiting changes labels Sep 16, 2026
@thisisnic
thisisnic merged commit f702184 into apache:main Sep 16, 2026
36 checks passed
@thisisnic thisisnic removed the awaiting merge Awaiting merge label Sep 16, 2026
pitrou pushed a commit to Alex-PLACET/arrow that referenced this pull request Sep 17, 2026
### Rationale for this change

arrange() then summarise() failed

### What changes are included in this PR?

Reset ordering vars before summarise like dplyr etc do anyway

### Are these changes tested?

Yep

### Are there any user-facing changes?

No
* GitHub Issue: apache#45373

Authored-by: Nic Crane <thisisnic@gmail.com>
Signed-off-by: Nic Crane <thisisnic@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants