romainfrancois commented on a change in pull request #8246:
URL: https://github.com/apache/arrow/pull/8246#discussion_r494093172



##########
File path: r/tests/testthat/test-arrow.R
##########
@@ -47,3 +47,12 @@ r_only({
     )
   })
 })
+
+test_that("arrow gracefully fails to load objects from other sessions 
(ARROW-10071)", {
+  a <- Array$create(1:10)
+  tf <- tempfile(); on.exit(unlink(tf))
+  saveRDS(a, tf)
+
+  b <- readRDS(tf)

Review comment:
       serialization of external pointer loses the pointer

##########
File path: r/src/arrow_cpp11.h
##########
@@ -157,8 +157,15 @@ struct ns {
 
 template <typename Pointer>
 Pointer r6_to_pointer(SEXP self) {
-  return reinterpret_cast<Pointer>(
-      R_ExternalPtrAddr(Rf_findVarInFrame(self, arrow::r::symbols::xp)));
+  void* p = R_ExternalPtrAddr(Rf_findVarInFrame(self, arrow::r::symbols::xp));
+  if (p == nullptr) {
+    SEXP klass = Rf_getAttrib(self, R_ClassSymbol);
+    std::string first_class(Rf_isNull(klass) ? "ArrowObject"
+                                             : CHAR(STRING_ELT(klass, 0)));

Review comment:
       I'll refine about the NULL case, but the class we do want is the first 
one: 
   
   ``` r
   library(arrow, warn.conflicts = FALSE)
   
   a <- Array$create(1:10)
   class(a)
   #> [1] "Array"       "ArrowObject" "R6"
   ```
   
   <sup>Created on 2020-09-24 by the [reprex 
package](https://reprex.tidyverse.org) (v0.3.0.9001)</sup>

##########
File path: r/src/arrow_cpp11.h
##########
@@ -157,8 +157,15 @@ struct ns {
 
 template <typename Pointer>
 Pointer r6_to_pointer(SEXP self) {
-  return reinterpret_cast<Pointer>(
-      R_ExternalPtrAddr(Rf_findVarInFrame(self, arrow::r::symbols::xp)));
+  void* p = R_ExternalPtrAddr(Rf_findVarInFrame(self, arrow::r::symbols::xp));
+  if (p == nullptr) {

Review comment:
       We can't really do that because of the implicit `else NULL` case: 
   
   ```r
   shared_ptr <- function(class, xp) {
     if (!shared_ptr_is_null(xp)) class$new(xp)
   }
   ```
   
   There are cases where we want an R NULL when the internal C++ shared pointer 
holds a C++ null pointer, e.g. when we call: 
   
   ```c++
   std::shared_ptr<Array> RecordBatch::GetColumnByName(const std::string& name) 
const {
     auto i = schema_->GetFieldIndex(name);
     return i == -1 ? NULLPTR : column(i);
   }
   ```
   
   For example in this tests: 
   
   ```r
   test_that("[[ and $ on RecordBatch", {
     [...]
     expect_null(batch$qwerty)
     [...]
   })
   ```
   
   having the extra layer with the R function `shared_ptr(T, .)` maybe calling 
`T$new(.)` gives the NULL. 

##########
File path: r/src/arrow_cpp11.h
##########
@@ -157,8 +157,15 @@ struct ns {
 
 template <typename Pointer>
 Pointer r6_to_pointer(SEXP self) {
-  return reinterpret_cast<Pointer>(
-      R_ExternalPtrAddr(Rf_findVarInFrame(self, arrow::r::symbols::xp)));
+  void* p = R_ExternalPtrAddr(Rf_findVarInFrame(self, arrow::r::symbols::xp));
+  if (p == nullptr) {

Review comment:
       Another way would be to have the ability to directly create the R6 
objects internally, either by having more code in 
   
   ```cpp
   template <typename T>
   SEXP as_sexp(const std::shared_ptr<T>& ptr) {
     return cpp11::external_pointer<std::shared_ptr<T>>(new 
std::shared_ptr<T>(ptr));
   }
   ```
   
   but we would need some sort of dispatch from `T` to the R6 object. 
   
   Or we would need to change the interface of many functions: 
   
   ```cpp
   // [[arrow::export]]
   std::shared_ptr<arrow::Array> StructArray__field(
       const std::shared_ptr<arrow::StructArray>& array, int i) {
     return array->field(i);
   }
   ```
   
   perhaps to: 
   
   ```cpp
   // [[arrow::export]]
   R6 StructArray__field(
       const std::shared_ptr<arrow::StructArray>& array, int i) {
     return R6(array->field(i), "Array");
   }
   ```
   
   but I don't think it's worth 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:
us...@infra.apache.org


Reply via email to