edponce commented on a change in pull request #11225:
URL: https://github.com/apache/arrow/pull/11225#discussion_r718023948



##########
File path: r/src/altrep.cpp
##########
@@ -274,210 +257,342 @@ struct AltrepArrayPrimitive {
 
   // This cannot keep the external pointer to an Arrow object through
   // R serialization, so return the materialized
-  SEXP Serialized_state() {
-    Materialize();
-    return R_altrep_data2(alt_);
-  }
+  static SEXP Serialized_state(SEXP alt_) { return 
R_altrep_data2(Materialize(alt_)); }
 
   static SEXP Unserialize(SEXP /* class_ */, SEXP state) { return state; }
 
-  SEXP Coerce(int type) {
-    // Just let R handle it for now
-    return NULL;
+  static SEXP Coerce(SEXP alt_, int type) {
+    return Rf_coerceVector(Materialize(alt_), type);
+  }
+
+  static std::shared_ptr<arrow::compute::ScalarAggregateOptions> NaRmOptions(
+      const std::shared_ptr<Array>& array, bool na_rm) {
+    auto options = std::make_shared<arrow::compute::ScalarAggregateOptions>(
+        arrow::compute::ScalarAggregateOptions::Defaults());
+    options->min_count = 0;
+    options->skip_nulls = na_rm;
+    return options;
+  }
+
+  template <bool Min>
+  static SEXP MinMax(SEXP alt_, Rboolean narm) {
+    using data_type = typename std::conditional<sexp_type == REALSXP, double, 
int>::type;
+    using scalar_type =
+        typename std::conditional<sexp_type == INTSXP, Int32Scalar, 
DoubleScalar>::type;
+
+    const auto& array_ = array(alt_);
+    bool na_rm = narm == TRUE;
+    auto n = array_->length();
+    auto null_count = array_->null_count();
+    if ((na_rm || n == 0) && null_count == n) {
+      return Rf_ScalarReal(Min ? R_PosInf : R_NegInf);
+    }
+    if (!na_rm && null_count > 0) {
+      return cpp11::as_sexp(cpp11::na<data_type>());
+    }
+
+    auto options = NaRmOptions(array_, na_rm);
+
+    const auto& minmax =
+        ValueOrStop(arrow::compute::CallFunction("min_max", {array_}, 
options.get()));
+    const auto& minmax_scalar =
+        internal::checked_cast<const StructScalar&>(*minmax.scalar());
+
+    const auto& result_scalar = internal::checked_cast<const scalar_type&>(
+        *ValueOrStop(minmax_scalar.field(Min ? "min" : "max")));
+    return cpp11::as_sexp(result_scalar.value);
+  }
+
+  static SEXP Min(SEXP alt_, Rboolean narm) { return MinMax<true>(alt_, narm); 
}
+
+  static SEXP Max(SEXP alt_, Rboolean narm) { return MinMax<false>(alt_, 
narm); }
+
+  static SEXP Sum(SEXP alt_, Rboolean narm) {
+    using data_type = typename std::conditional<sexp_type == REALSXP, double, 
int>::type;
+
+    const auto& array_ = array(alt_);
+    bool na_rm = narm == TRUE;
+    auto null_count = array_->null_count();
+
+    if (!na_rm && null_count > 0) {
+      return cpp11::as_sexp(cpp11::na<data_type>());
+    }
+    auto options = NaRmOptions(array_, na_rm);
+
+    const auto& sum =
+        ValueOrStop(arrow::compute::CallFunction("sum", {array_}, 
options.get()));
+
+    if (sexp_type == INTSXP) {
+      // When calling the "sum" function on an int32 array, we get an Int64 
scalar
+      // in case of overflow, make it a double like R
+      int64_t value = internal::checked_cast<const 
Int64Scalar&>(*sum.scalar()).value;
+      if (value <= INT32_MIN || value > INT32_MAX) {
+        return Rf_ScalarReal(static_cast<double>(value));
+      } else {
+        return Rf_ScalarInteger(static_cast<int>(value));
+      }
+    } else {
+      return Rf_ScalarReal(
+          internal::checked_cast<const DoubleScalar&>(*sum.scalar()).value);
+    }
   }
 };
 template <int sexp_type>
-R_altrep_class_t AltrepArrayPrimitive<sexp_type>::class_t;
+R_altrep_class_t AltrepVectorPrimitive<sexp_type>::class_t;
 
-// The methods below are how R interacts with the altrep objects.
-//
-// They all use the same pattern: create a C++ object of the
-// class parameter, and then call the method.
-template <typename AltrepClass>
-R_xlen_t Length(SEXP alt) {
-  return AltrepClass(alt).Length();
-}
+// Implementation for string arrays
+template <typename Type>
+struct AltrepVectorString : public AltrepVectorBase {
+  static R_altrep_class_t class_t;
+  using StringArrayType = typename TypeTraits<Type>::ArrayType;
 
-template <typename AltrepClass>
-Rboolean Inspect(SEXP alt, int pre, int deep, int pvec,
-                 void (*inspect_subtree)(SEXP, int, int, int)) {
-  return AltrepClass(alt).Inspect(pre, deep, pvec, inspect_subtree);
-}
+  static SEXP Make(const std::shared_ptr<Array>& array) {
+    return AltrepVectorBase::Make(class_t, array);
+  }
 
-template <typename AltrepClass>
-const void* Dataptr_or_null(SEXP alt) {
-  return AltrepClass(alt).Dataptr_or_null();
-}
+  // Get a single string, as a CHARSXP SEXP
+  // data2 is initialized, the CHARSXP is generated from the Array data
+  // and stored in data2, so that this only needs to expand a given string once
+  static SEXP Elt(SEXP alt_, R_xlen_t i) {
+    if (IsMaterialized(alt_)) {
+      return STRING_ELT(R_altrep_data2(alt_), i);
+    }
 
-template <typename AltrepClass>
-void* Dataptr(SEXP alt, Rboolean writeable) {
-  return AltrepClass(alt).Dataptr(writeable);
-}
+    // nul -> to NA_STRING
+    if (array(alt_)->IsNull(i)) {
+      return NA_STRING;
+    }
 
-template <typename AltrepClass>
-SEXP Duplicate(SEXP alt, Rboolean deep) {
-  return AltrepClass(alt).Duplicate(deep);
-}
+    // not nul, but we need care about embedded nuls
+    // this needs to call an R api function: Rf_mkCharLenCE() that
+    // might jump, i.e. throw an R error, which is dealt with using
+    // BEGIN_CPP11/END_CPP11/cpp11::unwind_protect()
+
+    BEGIN_CPP11
+
+    // C++ objects that will properly be destroyed by END_CPP11
+    // before it resumes the unwinding - and perhaps let
+    // the R error pass through
+    auto array_ = array(alt_);

Review comment:
       `array()` returns a const-reference, so shouldn't it be `const auto&`? 
Although it does not matter because it is being used as read-only and not being 
casted.




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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to