You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/18 14:35:38 UTC

[GitHub] [arrow-nanoarrow] paleolimbot commented on a diff in pull request #65: [R] Complete ptype inferences and array conversions

paleolimbot commented on code in PR #65:
URL: https://github.com/apache/arrow-nanoarrow/pull/65#discussion_r1026511358


##########
r/src/convert.c:
##########
@@ -0,0 +1,483 @@
+// 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.
+
+#define R_NO_REMAP
+#include <R.h>
+#include <Rinternals.h>
+
+#include "nanoarrow.h"
+
+#include "array.h"
+#include "convert.h"
+#include "materialize.h"
+#include "schema.h"
+
+static R_xlen_t nanoarrow_vec_size(SEXP vec_sexp, struct PTypeView* ptype_view) {
+  if (ptype_view->vector_type == VECTOR_TYPE_DATA_FRAME) {
+    if (Rf_length(vec_sexp) > 0) {
+      // Avoid materializing the row.names if we can
+      return Rf_xlength(VECTOR_ELT(vec_sexp, 0));
+    } else {
+      return Rf_xlength(Rf_getAttrib(vec_sexp, R_RowNamesSymbol));

Review Comment:
   Thanks! I played with this a little bit and if I go this route I have to implement inspecting for `c(NA, nrow)`, `c(NA, -nrow)` myself and I'm a little worried I will mess this up. It looks like rownames are sufficiently ALTREP even when expanded in recent R (although I don't know how far back this goes) such that computing the length shouldn't be an expensive operation?
   
   ``` r
   df <- nanoarrow:::new_data_frame(x = 1:1e9, 1e9)
   .Internal(inspect(df))
   #> @141c96758 13 INTSXP g0c0 [OBJ,REF(2),ATT]  wrapper [srt=-2147483648,no_na=0]
   #>   @141c93060 13 INTSXP g0c0 [REF(65535)]  1 : 1000000000 (compact)
   #> ATTRIB:
   #>   @141c96720 02 LISTSXP g0c0 [REF(1)] 
   #>     TAG: @15780c9f0 01 SYMSXP g1c0 [MARK,REF(65535),LCK,gp=0x4000] "row.names" (has value)
   #>     @141d29098 13 INTSXP g0c1 [REF(1)] (len=2, tl=0) -2147483648,1000000000
   #>     TAG: @15780d1d0 01 SYMSXP g1c0 [MARK,REF(38209),LCK,gp=0x6000] "class" (has value)
   #>     @141d29290 16 STRSXP g0c1 [REF(65535)] (len=1, tl=0)
   #>       @157891e08 09 CHARSXP g1c2 [MARK,REF(576),gp=0x61,ATT] [ASCII] [cached] "data.frame"
   str(attr(df, "row.names"))
   #>  int [1:1000000000] 1 2 3 4 5 6 7 8 9 10 ...
   bench::mark(attr(df, "row.names"))
   #> # A tibble: 1 × 6
   #>   expression                 min   median `itr/sec` mem_alloc `gc/sec`
   #>   <bch:expr>            <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
   #> 1 attr(df, "row.names")     41ns     82ns  8860040.        0B        0
   ```
   
   <sup>Created on 2022-11-18 with [reprex v2.0.2](https://reprex.tidyverse.org)</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.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org