nealrichardson commented on a change in pull request #9875:
URL: https://github.com/apache/arrow/pull/9875#discussion_r607381273



##########
File path: r/R/compute.R
##########
@@ -80,6 +80,46 @@ collect_arrays_from_dots <- function(dots) {
   ChunkedArray$create(!!!arrays)
 }
 
+#' @export
+quantile.ArrowDatum <- function(x,
+                                probs = seq(0, 1, 0.25),
+                                na.rm = FALSE,
+                                type = 7,
+                                interpolation = c("linear", "lower", "higher", 
"nearest", "midpoint"),
+                                ...) {
+  if (inherits(x, "Scalar")) x <- Array$create(x)
+  assert_is(probs, c("numeric", "integer"))
+  assert_that(length(probs) > 0)
+  assert_that(all(probs >= 0 & probs <= 1))
+  if (!na.rm && x$null_count > 0) {
+    stop("Missing values not allowed if 'na.rm' is FALSE", call. = FALSE)
+  }
+  if (type != 7) {
+    stop(
+      "Argument `type` not supported in Arrow. To control the quantile ",
+      "interpolation algorithm, set argument `interpolation` to one of: ",
+      "\"linear\" (the default), \"lower\", \"higher\", \"nearest\", or ",
+      "\"midpoint\".",
+      call. = FALSE
+    )
+  }
+  interpolation <- QuantileInterpolation[[toupper(match.arg(interpolation))]]
+  out <- call_function("quantile", x, options = list(q = probs, interpolation 
= interpolation))
+  if (length(out) == 0) {

Review comment:
       Why would `out` be length 0?

##########
File path: r/tests/testthat/test-compute-aggregate.R
##########
@@ -199,6 +199,114 @@ test_that("Edge cases", {
   }
 })
 
+test_that("quantile.Array and quantile.ChunkedArray", {
+  a <- Array$create(c(0, 1, 2, 3))
+  ca <- ChunkedArray$create(c(0, 1), c(2, 3))
+  probs <- c(0.49, 0.51)
+  for(ad in list(a, ca)) {
+    for (type in c(int32(), uint64(), float64())) {
+      expect_equal(
+        quantile(ad$cast(type), probs = probs, interpolation = "linear"),
+        Array$create(c(1.47, 1.53))
+      )
+      expect_equal(
+        quantile(ad$cast(type), probs = probs, interpolation = "lower"),
+        Array$create(c(1, 1))$cast(type)
+      )
+      expect_equal(
+        quantile(ad$cast(type), probs = probs, interpolation = "higher"),
+        Array$create(c(2, 2))$cast(type)
+      )
+      expect_equal(
+        quantile(ad$cast(type), probs = probs, interpolation = "nearest"),
+        Array$create(c(1, 2))$cast(type)
+      )
+      expect_equal(
+        quantile(ad$cast(type), probs = probs, interpolation = "midpoint"),
+        Array$create(c(1.5, 1.5))
+      )
+    }
+  }
+})
+
+test_that("quantile and median NAs, edge cases, and exceptions", {
+  expect_equal(
+    quantile(Array$create(c(1, 2)), probs = c(0, 1)),
+    Array$create(c(1, 2))
+  )
+  expect_error(
+    quantile(Array$create(c(1, 2, NA))),
+    "Missing values not allowed if 'na.rm' is FALSE"
+  )
+  expect_equal(
+    quantile(Array$create(numeric(0))),
+    Array$create(rep(NA_real_, 5))
+  )
+  expect_equal(
+    quantile(Array$create(rep(NA_integer_, 3)), na.rm = TRUE),
+    Array$create(rep(NA_real_, 5))
+  )
+  expect_error(
+    median(Array$create(c(1, 2)), probs = c(.25, .75)),
+    "formal argument \"probs\" matched by multiple actual arguments"

Review comment:
       This sounds like a base R error message; is it important that we assert 
it?




-- 
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]


Reply via email to