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



##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,163 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+template <int sexp_type>
+struct ArrayNoNull {
+  using data_type = typename std::conditional<sexp_type == INTSXP, int, 
double>::type;
+
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& 
array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new 
std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    const auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());

Review comment:
       ```
   > .Internal(inspect(as.vector(Array$create(1:10))))
   @7fc930ddbbc8 13 INTSXP g0c0 [REF(65535)] arrow::Array<int32, NONULL> 
len=10, Array=<0x7fc92b27ad98>, xp=<0x7fc930ddbc38>)
   ```

##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,163 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+template <int sexp_type>
+struct ArrayNoNull {
+  using data_type = typename std::conditional<sexp_type == INTSXP, int, 
double>::type;
+
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& 
array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new 
std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    const auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());

Review comment:
       Alternatively, we could call the `inspect_subtree()` function on the xp, 
and then get something like this: 
   
   ```
   > .Internal(inspect(as.vector(Array$create(1:10))))
   @7feaa7212e38 13 INTSXP g0c0 [REF(65535)] arrow::Array<int32, NONULL> 
len=10, Array=<0x7fea9ed38948>, xp=<0x7feaa7212ea8>)
     @7feaa7212ea8 22 EXTPTRSXP g0c0 [REF(4)] 
   ``` 

##########
File path: r/src/altrep.cpp
##########
@@ -0,0 +1,163 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <cpp11/altrep.hpp>
+
+#include "./arrow_types.h"
+
+#if defined(HAS_ALTREP)
+
+#include <R_ext/Altrep.h>
+#include <arrow/array.h>
+
+namespace arrow {
+namespace r {
+
+template <int sexp_type>
+struct ArrayNoNull {
+  using data_type = typename std::conditional<sexp_type == INTSXP, int, 
double>::type;
+
+  // altrep object around an Array with no nulls
+  // data1: an external pointer to a shared pointer to the Array
+  // data2: not used
+
+  static SEXP Make(R_altrep_class_t class_t, const std::shared_ptr<Array>& 
array) {
+    // we don't need the whole r6 object, just an external pointer
+    // that retain the array
+    cpp11::external_pointer<std::shared_ptr<Array>> xp(new 
std::shared_ptr<Array>(array));
+
+    SEXP res = R_new_altrep(class_t, xp, R_NilValue);
+    MARK_NOT_MUTABLE(res);
+
+    return res;
+  }
+
+  static Rboolean Inspect(SEXP x, int pre, int deep, int pvec,
+                          void (*inspect_subtree)(SEXP, int, int, int)) {
+    const auto& array = Get(x);
+    Rprintf("std::shared_ptr<arrow::Array, %s, NONULL> (len=%d, ptr=%p)\n",
+            array->type()->ToString().c_str(), array->length(), array.get());

Review comment:
       ``` r
   library(arrow, warn.conflicts = FALSE)
   #> See arrow_info() for available features
   .Internal(inspect(as.vector(Array$create(1:10))))
   #> @7f91050adcf0 13 INTSXP g0c0 [REF(65535)] arrow::Array<int32, NONULL> 
len=10, Array=<0x7f90fd0c8ec8>
   #>   @7f91050add60 22 EXTPTRSXP g0c0 [REF(4)]
   ```
   
   <sup>Created on 2021-06-21 by the [reprex 
package](https://reprex.tidyverse.org) (v2.0.0.9000)</sup>




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