19
19
# The following S3 methods are registered on load if dplyr is present
20
20
21
21
arrange.arrow_dplyr_query <- function (.data , ... , .by_group = FALSE ) {
22
- call <- match.call()
23
- .data <- as_adq(.data )
24
- exprs <- expand_across(.data , quos(... ))
22
+ try_arrow_dplyr({
23
+ .data <- as_adq(.data )
24
+ exprs <- expand_across(.data , quos(... ))
25
25
26
- if (.by_group ) {
27
- # when the data is grouped and .by_group is TRUE, order the result by
28
- # the grouping columns first
29
- exprs <- c(quos(!!! dplyr :: groups(.data )), exprs )
30
- }
31
- if (length(exprs ) == 0 ) {
32
- # Nothing to do
33
- return (.data )
34
- }
35
- .data <- as_adq(.data )
36
- # find and remove any dplyr::desc() and tidy-eval
37
- # the arrange expressions inside an Arrow data_mask
38
- sorts <- vector(" list" , length(exprs ))
39
- descs <- logical (0 )
40
- mask <- arrow_mask(.data )
41
- for (i in seq_along(exprs )) {
42
- x <- find_and_remove_desc(exprs [[i ]])
43
- exprs [[i ]] <- x [[" quos" ]]
44
- sorts [[i ]] <- arrow_eval(exprs [[i ]], mask )
45
- names(sorts )[i ] <- format_expr(exprs [[i ]])
46
- if (inherits(sorts [[i ]], " try-error" )) {
47
- msg <- paste(" Expression" , names(sorts )[i ], " not supported in Arrow" )
48
- return (abandon_ship(call , .data , msg ))
26
+ if (.by_group ) {
27
+ # when the data is grouped and .by_group is TRUE, order the result by
28
+ # the grouping columns first
29
+ exprs <- c(quos(!!! dplyr :: groups(.data )), exprs )
49
30
}
50
- if (length(mask $ .aggregations )) {
51
- # dplyr lets you arrange on e.g. x < mean(x), but we haven't implemented it.
52
- # But we could, the same way it works in mutate() via join, if someone asks.
53
- # Until then, just error.
54
- # TODO: add a test for this
55
- msg <- paste(" Expression" , format_expr(expr ), " not supported in arrange() in Arrow" )
56
- return (abandon_ship(call , .data , msg ))
31
+ if (length(exprs ) == 0 ) {
32
+ # Nothing to do
33
+ return (.data )
57
34
}
58
- descs [i ] <- x [[" desc" ]]
59
- }
60
- .data $ arrange_vars <- c(sorts , .data $ arrange_vars )
61
- .data $ arrange_desc <- c(descs , .data $ arrange_desc )
62
- .data
35
+ .data <- as_adq(.data )
36
+ # find and remove any dplyr::desc() and tidy-eval
37
+ # the arrange expressions inside an Arrow data_mask
38
+ sorts <- vector(" list" , length(exprs ))
39
+ descs <- logical (0 )
40
+ mask <- arrow_mask(.data )
41
+ for (i in seq_along(exprs )) {
42
+ x <- find_and_remove_desc(exprs [[i ]])
43
+ exprs [[i ]] <- x [[" quos" ]]
44
+ sorts [[i ]] <- arrow_eval(exprs [[i ]], mask )
45
+ names(sorts )[i ] <- format_expr(exprs [[i ]])
46
+ if (length(mask $ .aggregations )) {
47
+ # dplyr lets you arrange on e.g. x < mean(x), but we haven't implemented it.
48
+ # But we could, the same way it works in mutate() via join, if someone asks.
49
+ # Until then, just error.
50
+ # TODO: add a test for this
51
+ arrow_not_supported(
52
+ .actual_msg = " Expression not supported in arrange() in Arrow" ,
53
+ call = expr
54
+ )
55
+ }
56
+ descs [i ] <- x [[" desc" ]]
57
+ }
58
+ .data $ arrange_vars <- c(sorts , .data $ arrange_vars )
59
+ .data $ arrange_desc <- c(descs , .data $ arrange_desc )
60
+ .data
61
+ })
63
62
}
64
63
arrange.Dataset <- arrange.ArrowTabular <- arrange.RecordBatchReader <- arrange.arrow_dplyr_query
65
64
@@ -73,10 +72,9 @@ find_and_remove_desc <- function(quosure) {
73
72
expr <- quo_get_expr(quosure )
74
73
descending <- FALSE
75
74
if (length(all.vars(expr )) < 1L ) {
76
- stop(
77
- " Expression in arrange() does not contain any field names: " ,
78
- deparse(expr ),
79
- call. = FALSE
75
+ validation_error(
76
+ " Expression in arrange() does not contain any field names" ,
77
+ call = quosure
80
78
)
81
79
}
82
80
# Use a while loop to remove any number of nested pairs of enclosing
@@ -90,7 +88,10 @@ find_and_remove_desc <- function(quosure) {
90
88
# ensure desc() has only one argument (when an R expression is a function
91
89
# call, length == 2 means it has exactly one argument)
92
90
if (length(expr ) > 2 ) {
93
- stop(" desc() expects only one argument" , call. = FALSE )
91
+ validation_error(
92
+ " desc() expects only one argument" ,
93
+ call = expr
94
+ )
94
95
}
95
96
# remove desc() and toggle descending
96
97
expr <- expr [[2 ]]
0 commit comments