nealrichardson commented on a change in pull request #9745:
URL: https://github.com/apache/arrow/pull/9745#discussion_r600615554
##########
File path: r/R/dplyr.R
##########
@@ -689,17 +741,77 @@ abandon_ship <- function(call, .data, msg = NULL) {
eval.parent(call, 2)
}
-arrange.arrow_dplyr_query <- function(.data, ...) {
+arrange.arrow_dplyr_query <- function(.data, ..., .by_group = FALSE) {
+ call <- match.call()
+ exprs <- quos(...)
+ if (.by_group) {
+ # when the data is is grouped and .by_group is TRUE, order the result by
+ # the grouping columns first
+ exprs <- c(quos(!!!dplyr::groups(.data)), exprs)
+ }
+ if (length(exprs) == 0) {
+ # Nothing to do
+ return(.data)
+ }
.data <- arrow_dplyr_query(.data)
- if (query_on_dataset(.data)) {
- not_implemented_for_dataset("arrange()")
+ # find and remove any dplyr::desc() and tidy-eval
+ # the arrange expressions inside an Arrow data_mask
+ sorts <- vector("list", length(exprs))
+ descs <- logical(0)
+ mask <- arrow_mask(.data)
+ for (i in seq_along(exprs)) {
+ x <- find_and_remove_desc(exprs[[i]])
+ exprs[[i]] <- x[["quos"]]
+ sorts[[i]] <- arrow_eval(exprs[[i]], mask)
+ if (inherits(sorts[[i]], "try-error")) {
+ msg <- paste('Expression', as_label(exprs[[i]]), 'not supported in
Arrow')
+ return(abandon_ship(call, .data, msg))
+ }
+ names(sorts)[i] <- as_label(exprs[[i]])
+ descs[i] <- x[["desc"]]
}
- # TODO(ARROW-11703) move this to Arrow
- call <- match.call()
- abandon_ship(call, .data)
+ .data$arrange_vars <- c(sorts, .data$arrange_vars)
+ .data$arrange_desc <- c(descs, .data$arrange_desc)
+ .data
}
arrange.Dataset <- arrange.ArrowTabular <- arrange.arrow_dplyr_query
+# Helper to handle desc() in arrange()
+# * Takes a quosure as input
+# * Returns a list with two elements:
+# 1. The quosure with any wrapping parentheses and desc() removed
+# 2. A logical value indicating whether desc() was found
+# * Performs some other validation
+find_and_remove_desc <- function(quosure) {
+ expr <- quo_get_expr(quosure)
+ descending <- FALSE
+ if (length(all.vars(expr)) < 1L) {
+ stop(
+ "Expression in arrange() does not contain any field names: ",
+ deparse(expr),
+ call. = FALSE
+ )
+ }
+ while (identical(typeof(expr), "language") && is.call(expr)) {
Review comment:
What use case does this `while` handle?
##########
File path: r/tests/testthat/test-compute-sort.R
##########
@@ -0,0 +1,143 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+context("compute: sorting")
+
+library(dplyr)
+
+tbl <- example_data_for_sorting
+
+test_that("sort(Scalar) is identity function", {
+ expect_identical(
+ as.vector(sort(Scalar$create(42L))),
+ 42L
+ )
+ expect_identical(
+ as.vector(sort(Scalar$create("foo"))),
+ "foo"
+ )
+})
+
+test_that("Array$SortIndices()", {
+ expect_equal(
+ Array$create(tbl$int)$SortIndices(),
+ Array$create(0L:9L, type = uint64())
Review comment:
`:` creates integers already so you don't need the `L`s:
```
> typeof(1:9)
[1] "integer"
```
##########
File path: r/tests/testthat/test-dplyr-arrange.R
##########
@@ -0,0 +1,172 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+library(dplyr)
+
+tbl <- example_data_for_sorting
+
+test_that("arrange", {
+ expect_dplyr_equal(
+ input %>%
+ arrange(int, chr) %>%
+ collect(),
+ tbl %>%
+ slice_sample(prop = 1L)
Review comment:
It took me a minute to realize that you're using `slice_sample` to
shuffle the rows. What if you instead defined `tbl <-
slice_sample(example_data_for_sorting, prop = 1L)` (with a comment that this is
just shuffling rows) at the top of the file?
##########
File path: r/tests/testthat/test-compute-sort.R
##########
@@ -0,0 +1,143 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+context("compute: sorting")
+
+library(dplyr)
+
+tbl <- example_data_for_sorting
+
+test_that("sort(Scalar) is identity function", {
+ expect_identical(
+ as.vector(sort(Scalar$create(42L))),
+ 42L
+ )
+ expect_identical(
+ as.vector(sort(Scalar$create("foo"))),
+ "foo"
+ )
+})
+
+test_that("Array$SortIndices()", {
+ expect_equal(
+ Array$create(tbl$int)$SortIndices(),
+ Array$create(0L:9L, type = uint64())
+ )
+ expect_equal(
+ Array$create(rev(tbl$int))$SortIndices(descending = TRUE),
+ Array$create(c(1L:9L, 0L), type = uint64())
+ )
+})
+
+test_that("ChunkedArray$SortIndices()", {
+ expect_equal(
+ ChunkedArray$create(tbl$int[1:5], tbl$int[6:10])$SortIndices(),
+ Array$create(0L:9L, type = uint64())
+ )
+ expect_equal(
+ ChunkedArray$create(rev(tbl$int)[1:5],
rev(tbl$int)[6:10])$SortIndices(descending = TRUE),
+ Array$create(c(1L:9L, 0L), type = uint64())
+ )
+})
+
+test_that("sort(vector), sort(Array), sort(ChunkedArray) give equivalent
results on integers", {
+ expect_vector_equal(
+ sort(input),
+ tbl$int
+ )
+ expect_vector_equal(
+ sort(input, na.last = NA),
+ tbl$int
+ )
+ expect_vector_equal(
+ sort(input, na.last = TRUE),
+ tbl$int
+ )
+ expect_vector_equal(
+ sort(input, na.last = FALSE),
+ tbl$int
+ )
+ expect_vector_equal(
+ sort(input, decreasing = TRUE),
+ tbl$int,
+ )
+ expect_vector_equal(
+ sort(input, decreasing = TRUE, na.last = TRUE),
+ tbl$int,
+ )
+ expect_vector_equal(
+ sort(input, decreasing = TRUE, na.last = FALSE),
+ tbl$int,
+ )
+})
+
+test_that("sort(vector), sort(Array), sort(ChunkedArray) give equivalent
results on strings", {
+ skip_if_not(
+ identical(Sys.getlocale("LC_COLLATE"), "C"),
+ "Unexpected LC_COLLATE"
+ )
+ expect_vector_equal(
+ sort(input, decreasing = TRUE, na.last = FALSE),
+ tbl$chr
+ )
+ expect_vector_equal(
+ sort(input, decreasing = TRUE, na.last = FALSE),
+ tbl$chr
+ )
+})
+
+test_that("sort(vector), sort(Array), sort(ChunkedArray) give equivalent
results on floats", {
+ skip("is.na() evaluates to FALSE on Arrow NaN values (ARROW-12055)")
Review comment:
This skip is because of your workaround for `na.last`? If so, you may
also want to mention that jira issue here so we can remove the skip when that's
implemented in C++
##########
File path: r/tests/testthat/test-compute-sort.R
##########
@@ -0,0 +1,143 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+context("compute: sorting")
+
+library(dplyr)
+
+tbl <- example_data_for_sorting
+
+test_that("sort(Scalar) is identity function", {
+ expect_identical(
+ as.vector(sort(Scalar$create(42L))),
+ 42L
+ )
Review comment:
This might be clearer:
```suggestion
expect_equal(sort(Scalar$create(42L)), Scalar$create(42L))
```
or
```suggestion
s <- Scalar$create(42L)
expect_equal(sort(s), s)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]