From 3125e6ce064c1b8f4eab1df4a214f3b495cc47ea Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 12 Sep 2026 07:30:38 -0400 Subject: [PATCH 1/5] Add failing test --- r/tests/testthat/test-dplyr-summarize.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/r/tests/testthat/test-dplyr-summarize.R b/r/tests/testthat/test-dplyr-summarize.R index bb18c01666ed..2cd68528ac5f 100644 --- a/r/tests/testthat/test-dplyr-summarize.R +++ b/r/tests/testthat/test-dplyr-summarize.R @@ -1336,3 +1336,13 @@ test_that(".by argument", { "Can't supply `\\.by` when `\\.data` is grouped data" ) }) + +test_that("summarize() after arrange() (GH-45373)", { + compare_dplyr_binding( + .input |> + arrange(int) |> + summarize(min_int = min(int, na.rm = TRUE)) |> + collect(), + tbl + ) +}) From 0464c683f441aac8fc5ed6019f8665957d4f24ce Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 12 Sep 2026 07:47:44 -0400 Subject: [PATCH 2/5] Remove arrange before summarise/mutate --- r/R/dplyr-mutate.R | 2 ++ r/R/dplyr-summarize.R | 2 ++ r/tests/testthat/test-dplyr-mutate.R | 11 +++++++++++ r/tests/testthat/test-dplyr-summarize.R | 9 +++++++++ 4 files changed, 24 insertions(+) diff --git a/r/R/dplyr-mutate.R b/r/R/dplyr-mutate.R index a62149f6743e..9c41a660b8f1 100644 --- a/r/R/dplyr-mutate.R +++ b/r/R/dplyr-mutate.R @@ -75,6 +75,8 @@ mutate.arrow_dplyr_query <- function( # Make a copy of .data, do the aggregations on it, and then left_join on # the group_by variables. agg_query <- as_adq(.data) + agg_query$arrange_vars <- list() + agg_query$arrange_desc <- logical() # These may be computed by .by, make sure they're set agg_query$group_by_vars <- grv agg_query$aggregations <- mask$.aggregations diff --git a/r/R/dplyr-summarize.R b/r/R/dplyr-summarize.R index 7c2a44eec3d5..0d44f741a116 100644 --- a/r/R/dplyr-summarize.R +++ b/r/R/dplyr-summarize.R @@ -88,6 +88,8 @@ do_arrow_summarize <- function(.data, ..., .groups = NULL) { # Apply the results to the .data object. # First, the aggregations .data$aggregations <- mask$.aggregations + .data$arrange_vars <- list() + .data$arrange_desc <- logical() # Then collapse the query so that the resulting query object can have # additional operations applied to it out <- collapse.arrow_dplyr_query(.data) diff --git a/r/tests/testthat/test-dplyr-mutate.R b/r/tests/testthat/test-dplyr-mutate.R index 63f69227b289..9accf48eecdc 100644 --- a/r/tests/testthat/test-dplyr-mutate.R +++ b/r/tests/testthat/test-dplyr-mutate.R @@ -775,3 +775,14 @@ test_that("across() does not select grouping variables within transmute()", { "Column `chr` doesn't exist" ) }) + +test_that("mutate() with aggregations after arrange() (GH-45373)", { + compare_dplyr_binding( + .input |> + select(int, chr) |> + arrange(int) |> + mutate(avg_int = mean(int)) |> + collect(), + tbl + ) +}) diff --git a/r/tests/testthat/test-dplyr-summarize.R b/r/tests/testthat/test-dplyr-summarize.R index 2cd68528ac5f..b58102597a5f 100644 --- a/r/tests/testthat/test-dplyr-summarize.R +++ b/r/tests/testthat/test-dplyr-summarize.R @@ -1345,4 +1345,13 @@ test_that("summarize() after arrange() (GH-45373)", { collect(), tbl ) + compare_dplyr_binding( + .input |> + arrange(dbl) |> + group_by(some_grouping) |> + summarize(total = sum(int, na.rm = TRUE)) |> + arrange(some_grouping) |> + collect(), + tbl + ) }) From bc9b73fef65fa336ec5753ea32ec8212b027ea4f Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Sat, 12 Sep 2026 08:41:15 -0400 Subject: [PATCH 3/5] Add test to appease false positive reviews --- r/tests/testthat/test-dplyr-mutate.R | 10 ++++++++++ r/tests/testthat/test-dplyr-summarize.R | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/r/tests/testthat/test-dplyr-mutate.R b/r/tests/testthat/test-dplyr-mutate.R index 9accf48eecdc..033d8f32ffb0 100644 --- a/r/tests/testthat/test-dplyr-mutate.R +++ b/r/tests/testthat/test-dplyr-mutate.R @@ -785,4 +785,14 @@ test_that("mutate() with aggregations after arrange() (GH-45373)", { collect(), tbl ) + # A row limit between arrange() and mutate() still uses the sorted rows + compare_dplyr_binding( + .input |> + select(int, chr) |> + arrange(int) |> + head(3) |> + mutate(max_int = max(int, na.rm = TRUE)) |> + collect(), + tbl + ) }) diff --git a/r/tests/testthat/test-dplyr-summarize.R b/r/tests/testthat/test-dplyr-summarize.R index b58102597a5f..0e2ce8465574 100644 --- a/r/tests/testthat/test-dplyr-summarize.R +++ b/r/tests/testthat/test-dplyr-summarize.R @@ -1354,4 +1354,13 @@ test_that("summarize() after arrange() (GH-45373)", { collect(), tbl ) + # A row limit between arrange() and summarize() still uses the sorted rows + compare_dplyr_binding( + .input |> + arrange(int) |> + head(3) |> + summarize(max_int = max(int, na.rm = TRUE)) |> + collect(), + tbl + ) }) From ad71f3505956b12a0c2bedc2fb0906cdc3d2e6d1 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Wed, 16 Sep 2026 11:35:51 -0500 Subject: [PATCH 4/5] Appease copilot --- r/tests/testthat/test-dplyr-mutate.R | 2 +- r/tests/testthat/test-dplyr-summarize.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/r/tests/testthat/test-dplyr-mutate.R b/r/tests/testthat/test-dplyr-mutate.R index 033d8f32ffb0..5a5c6181a11e 100644 --- a/r/tests/testthat/test-dplyr-mutate.R +++ b/r/tests/testthat/test-dplyr-mutate.R @@ -789,7 +789,7 @@ test_that("mutate() with aggregations after arrange() (GH-45373)", { compare_dplyr_binding( .input |> select(int, chr) |> - arrange(int) |> + arrange(desc(int)) |> head(3) |> mutate(max_int = max(int, na.rm = TRUE)) |> collect(), diff --git a/r/tests/testthat/test-dplyr-summarize.R b/r/tests/testthat/test-dplyr-summarize.R index 0e2ce8465574..b2c9adeace5e 100644 --- a/r/tests/testthat/test-dplyr-summarize.R +++ b/r/tests/testthat/test-dplyr-summarize.R @@ -1357,7 +1357,7 @@ test_that("summarize() after arrange() (GH-45373)", { # A row limit between arrange() and summarize() still uses the sorted rows compare_dplyr_binding( .input |> - arrange(int) |> + arrange(desc(int)) |> head(3) |> summarize(max_int = max(int, na.rm = TRUE)) |> collect(), From 372432740c42410a6f72133e3a1cfd85208d8d37 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Wed, 16 Sep 2026 13:18:53 -0500 Subject: [PATCH 5/5] do min not max --- r/tests/testthat/test-dplyr-mutate.R | 2 +- r/tests/testthat/test-dplyr-summarize.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/r/tests/testthat/test-dplyr-mutate.R b/r/tests/testthat/test-dplyr-mutate.R index 5a5c6181a11e..d8f5579c36e9 100644 --- a/r/tests/testthat/test-dplyr-mutate.R +++ b/r/tests/testthat/test-dplyr-mutate.R @@ -791,7 +791,7 @@ test_that("mutate() with aggregations after arrange() (GH-45373)", { select(int, chr) |> arrange(desc(int)) |> head(3) |> - mutate(max_int = max(int, na.rm = TRUE)) |> + mutate(min_int = min(int, na.rm = TRUE)) |> collect(), tbl ) diff --git a/r/tests/testthat/test-dplyr-summarize.R b/r/tests/testthat/test-dplyr-summarize.R index b2c9adeace5e..4e5b0b93cc7a 100644 --- a/r/tests/testthat/test-dplyr-summarize.R +++ b/r/tests/testthat/test-dplyr-summarize.R @@ -1359,7 +1359,7 @@ test_that("summarize() after arrange() (GH-45373)", { .input |> arrange(desc(int)) |> head(3) |> - summarize(max_int = max(int, na.rm = TRUE)) |> + summarize(min_int = min(int, na.rm = TRUE)) |> collect(), tbl )