Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions r/R/arrow-datum.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ is.sliceable <- function(i) {
is.numeric(i) &&
length(i) > 0 &&
all(i > 0) &&
i[1] <= i[length(i)] &&
Comment thread
nealrichardson marked this conversation as resolved.
Resolved
identical(as.integer(i), i[1]:i[length(i)])
}

Expand Down
4 changes: 2 additions & 2 deletions r/R/arrowExports.R

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 26 additions & 14 deletions r/R/query-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
Expand All @@ -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
Expand Down
11 changes: 6 additions & 5 deletions r/src/arrowExports.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions r/src/compute-exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,31 @@ std::shared_ptr<compute::ExecNode> MakeExecNodeOrStop(
// [[arrow::export]]
std::shared_ptr<arrow::RecordBatchReader> ExecPlan_run(
const std::shared_ptr<compute::ExecPlan>& plan,
const std::shared_ptr<compute::ExecNode>& final_node, cpp11::list sort_options) {
const std::shared_ptr<compute::ExecNode>& 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<arrow::util::optional<compute::ExecBatch>> 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<compute::SortOptions>(
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<compute::SortOptions>(
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<compute::SortOptions>(
make_compute_options("sort_indices", sort_options)),
&sink_gen});
}
} else {
MakeExecNodeOrStop("sink", plan.get(), {final_node.get()},
compute::SinkNodeOptions{&sink_gen});
Expand Down
1 change: 1 addition & 0 deletions r/tests/testthat/test-dplyr-collapse.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion r/tests/testthat/test-dplyr-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,27 @@ 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,
ignore_attr = "row.names"
)
})

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case for a bare tail with no arrange? I think it might be treated as a head but I might not be following the logic right.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there already was one from the last PR.

Expand Down
1 change: 1 addition & 0 deletions r/tests/testthat/test-python.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down