ianmcook commented on a change in pull request #9745: URL: https://github.com/apache/arrow/pull/9745#discussion_r601843172
########## 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: I added a few more tests with bad inputs in 49aaf1ef8547519fbe780f579a3eab9061a9eef8 and a4e69c2266465b59a60647776b532cadddd11103. I opened ARROW-12093 to improve handling of some other kinds of bad inputs to this and other dplyr verbs. I tried to find something that would cause the loop to error but nothing did. -- 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: us...@infra.apache.org