diff --git a/r/R/arrow-datum.R b/r/R/arrow-datum.R index adffe5c8fc1c..557321f68dbb 100644 --- a/r/R/arrow-datum.R +++ b/r/R/arrow-datum.R @@ -232,6 +232,7 @@ is.sliceable <- function(i) { is.numeric(i) && length(i) > 0 && all(i > 0) && + i[1] <= i[length(i)] && identical(as.integer(i), i[1]:i[length(i)]) } diff --git a/r/R/arrowExports.R b/r/R/arrowExports.R index 3d8724729963..cca2c0ad1524 100644 --- a/r/R/arrowExports.R +++ b/r/R/arrowExports.R @@ -288,8 +288,8 @@ ExecPlan_create <- function(use_threads) { .Call(`_arrow_ExecPlan_create`, use_threads) } -ExecPlan_run <- function(plan, final_node, sort_options) { - .Call(`_arrow_ExecPlan_run`, plan, final_node, sort_options) +ExecPlan_run <- function(plan, final_node, sort_options, head) { + .Call(`_arrow_ExecPlan_run`, plan, final_node, sort_options, head) } ExecPlan_StopProducing <- function(plan) { diff --git a/r/R/query-engine.R b/r/R/query-engine.R index aa9740196bbf..bfd489260ffc 100644 --- a/r/R/query-engine.R +++ b/r/R/query-engine.R @@ -176,7 +176,7 @@ ExecPlan <- R6Class("ExecPlan", if (length(.data$arrange_vars)) { node$sort <- list( names = names(.data$arrange_vars), - orders = as.integer(.data$arrange_desc), + orders = .data$arrange_desc, temp_columns = names(.data$temp_columns) ) } @@ -194,29 +194,41 @@ ExecPlan <- R6Class("ExecPlan", }, Run = function(node) { assert_is(node, "ExecNode") - # TODO (ARROW-12763): pass head/tail to ExecPlan_run so we can maybe TopK - out <- ExecPlan_run(self, node, node$sort %||% list()) - if (is.null(node$sort)) { + # Sorting and head/tail (if sorted) are handled in the SinkNode, + # created in ExecPlan_run + sorting <- node$sort %||% list() + select_k <- node$head %||% -1L + has_sorting <- length(sorting) > 0 + if (has_sorting) { + if (!is.null(node$tail)) { + # Reverse the sort order and take the top K, then after we'll reverse + # the resulting rows so that it is ordered as expected + sorting$orders <- !sorting$orders + select_k <- node$tail + } + sorting$orders <- as.integer(sorting$orders) + } + + out <- ExecPlan_run(self, node, sorting, select_k) + + if (!has_sorting) { # Since ExecPlans don't scan in deterministic order, head/tail are both # essentially taking a random slice from somewhere in the dataset. # And since the head() implementation is way more efficient than tail(), # just use it to take the random slice slice_size <- node$head %||% node$tail if (!is.null(slice_size)) { + # TODO (ARROW-14289): make the head methods return RBR not Table out <- head(out, slice_size) } - # TODO (ARROW-12763): delete these else cases because they'll be handled - # with SelectK - } else if (!is.null(node$head)) { - # These methods are on RecordBatchReader (but return Table) - # TODO (ARROW-14289): make the head/tail methods return RBR not Table - out <- head(out, node$head) - # We can now tell `self` to StopProducing: we already have - # everything we need for the head - self$Stop() + # Can we now tell `self$Stop()` to StopProducing? We already have + # everything we need for the head (but it seems to segfault: ARROW-14329) } else if (!is.null(node$tail)) { - out <- tail(out, node$tail) + # Reverse the row order to get back what we expect + # TODO: don't return Table, return RecordBatchReader + out <- out$read_table() + out <- out[rev(seq_len(nrow(out))), , drop = FALSE] } out diff --git a/r/src/arrowExports.cpp b/r/src/arrowExports.cpp index 9299366ddf06..f693f15262e8 100644 --- a/r/src/arrowExports.cpp +++ b/r/src/arrowExports.cpp @@ -1126,17 +1126,18 @@ extern "C" SEXP _arrow_ExecPlan_create(SEXP use_threads_sexp){ // compute-exec.cpp #if defined(ARROW_R_WITH_ARROW) -std::shared_ptr ExecPlan_run(const std::shared_ptr& plan, const std::shared_ptr& final_node, cpp11::list sort_options); -extern "C" SEXP _arrow_ExecPlan_run(SEXP plan_sexp, SEXP final_node_sexp, SEXP sort_options_sexp){ +std::shared_ptr ExecPlan_run(const std::shared_ptr& plan, const std::shared_ptr& final_node, cpp11::list sort_options, int64_t head); +extern "C" SEXP _arrow_ExecPlan_run(SEXP plan_sexp, SEXP final_node_sexp, SEXP sort_options_sexp, SEXP head_sexp){ BEGIN_CPP11 arrow::r::Input&>::type plan(plan_sexp); arrow::r::Input&>::type final_node(final_node_sexp); arrow::r::Input::type sort_options(sort_options_sexp); - return cpp11::as_sexp(ExecPlan_run(plan, final_node, sort_options)); + arrow::r::Input::type head(head_sexp); + return cpp11::as_sexp(ExecPlan_run(plan, final_node, sort_options, head)); END_CPP11 } #else -extern "C" SEXP _arrow_ExecPlan_run(SEXP plan_sexp, SEXP final_node_sexp, SEXP sort_options_sexp){ +extern "C" SEXP _arrow_ExecPlan_run(SEXP plan_sexp, SEXP final_node_sexp, SEXP sort_options_sexp, SEXP head_sexp){ Rf_error("Cannot call ExecPlan_run(). See https://arrow.apache.org/docs/r/articles/install.html for help installing Arrow C++ libraries. "); } #endif @@ -7223,7 +7224,7 @@ static const R_CallMethodDef CallEntries[] = { { "_arrow_io___CompressedOutputStream__Make", (DL_FUNC) &_arrow_io___CompressedOutputStream__Make, 2}, { "_arrow_io___CompressedInputStream__Make", (DL_FUNC) &_arrow_io___CompressedInputStream__Make, 2}, { "_arrow_ExecPlan_create", (DL_FUNC) &_arrow_ExecPlan_create, 1}, - { "_arrow_ExecPlan_run", (DL_FUNC) &_arrow_ExecPlan_run, 3}, + { "_arrow_ExecPlan_run", (DL_FUNC) &_arrow_ExecPlan_run, 4}, { "_arrow_ExecPlan_StopProducing", (DL_FUNC) &_arrow_ExecPlan_StopProducing, 1}, { "_arrow_ExecNode_output_schema", (DL_FUNC) &_arrow_ExecNode_output_schema, 1}, { "_arrow_ExecNode_Scan", (DL_FUNC) &_arrow_ExecNode_Scan, 4}, diff --git a/r/src/compute-exec.cpp b/r/src/compute-exec.cpp index 8d41a98440db..10cfbee53a26 100644 --- a/r/src/compute-exec.cpp +++ b/r/src/compute-exec.cpp @@ -57,18 +57,31 @@ std::shared_ptr MakeExecNodeOrStop( // [[arrow::export]] std::shared_ptr ExecPlan_run( const std::shared_ptr& plan, - const std::shared_ptr& final_node, cpp11::list sort_options) { + const std::shared_ptr& final_node, cpp11::list sort_options, + int64_t head = -1) { // For now, don't require R to construct SinkNodes. // Instead, just pass the node we should collect as an argument. arrow::AsyncGenerator> sink_gen; // Sorting uses a different sink node; there is no general sort yet if (sort_options.size() > 0) { - MakeExecNodeOrStop("order_by_sink", plan.get(), {final_node.get()}, - compute::OrderBySinkNodeOptions{ - *std::dynamic_pointer_cast( - make_compute_options("sort_indices", sort_options)), - &sink_gen}); + if (head >= 0) { + // Use the SelectK node to take only what we need + MakeExecNodeOrStop( + "select_k_sink", plan.get(), {final_node.get()}, + compute::SelectKSinkNodeOptions{ + arrow::compute::SelectKOptions( + head, std::dynamic_pointer_cast( + make_compute_options("sort_indices", sort_options)) + ->sort_keys), + &sink_gen}); + } else { + MakeExecNodeOrStop("order_by_sink", plan.get(), {final_node.get()}, + compute::OrderBySinkNodeOptions{ + *std::dynamic_pointer_cast( + make_compute_options("sort_indices", sort_options)), + &sink_gen}); + } } else { MakeExecNodeOrStop("sink", plan.get(), {final_node.get()}, compute::SinkNodeOptions{&sink_gen}); diff --git a/r/tests/testthat/test-dplyr-collapse.R b/r/tests/testthat/test-dplyr-collapse.R index f87d963f5529..13d870faedd5 100644 --- a/r/tests/testthat/test-dplyr-collapse.R +++ b/r/tests/testthat/test-dplyr-collapse.R @@ -197,6 +197,7 @@ See $.data for the source Arrow object", q %>% head(1) %>% collect(), tibble::tibble(lgl = FALSE, total = 8L, extra = 40) ) + skip("TODO (ARROW-1XXXX): implement sorting option about where NAs go") expect_equal( q %>% tail(1) %>% collect(), tibble::tibble(lgl = NA, total = 25L, extra = 125) diff --git a/r/tests/testthat/test-dplyr-query.R b/r/tests/testthat/test-dplyr-query.R index cd0cdb088946..07cdf081fb37 100644 --- a/r/tests/testthat/test-dplyr-query.R +++ b/r/tests/testthat/test-dplyr-query.R @@ -226,7 +226,8 @@ test_that("head", { test_that("arrange then head returns the right data (ARROW-14162)", { expect_dplyr_equal( input %>% - arrange(mpg) %>% + # mpg has ties so we need to sort by two things to get deterministic order + arrange(mpg, disp) %>% head(4) %>% collect(), mtcars, @@ -234,6 +235,18 @@ test_that("arrange then head returns the right data (ARROW-14162)", { ) }) +test_that("arrange then tail returns the right data", { + expect_dplyr_equal( + input %>% + # mpg has ties so we need to sort by two things to get deterministic order + arrange(mpg, disp) %>% + tail(4) %>% + collect(), + mtcars, + ignore_attr = "row.names" + ) +}) + test_that("tail", { batch <- record_batch(tbl) diff --git a/r/tests/testthat/test-python.R b/r/tests/testthat/test-python.R index 82b3bfc01982..5ad7513fbb12 100644 --- a/r/tests/testthat/test-python.R +++ b/r/tests/testthat/test-python.R @@ -17,6 +17,7 @@ test_that("install_pyarrow", { skip_on_cran() + skip_if_offline() skip_if_not_dev_mode() # Windows CI machine doesn't pick up the right python or something skip_on_os("windows")