Skip to content

Commit 1db7f90

Browse files
authored
feat(r): Add experimental nanoarrow_vctr to wrap a list of arrays (#461)
This PR adds the `nanoarrow_vctr`, which is an R translation of the Python `Array` class in nanoarrow's Python bindings. This is implemented like an R `factor()` in the sense that under the hood it is a sequence of integers (`0:(array$length - 1)` at the beginning) with attributes that give those integers context. This is implemented in such a way that it is "tacked on" to the existing conversions. The existing conversions do need a refactoring ( #392 ), but that is a heavy change for this point in the release cycle. The only change needed to the existing conversion was a slight refactor of the "consume array stream" code that correctly gave each array in the stream its own R object to manage its lifecycle (before each array was "materialized" and then immediately released because no previous conversion code required an ArrowArray to live beyond the conversion. The motivation for this change is converting GeoArrow extension types. In the geoarrow package, we implement an efficient conversion from a stream of arrays to various types of R-spatial objects (e.g., sf); however, we really don't want to invoke the default conversion for those types because they have awful performance (e.g., the multipolygon would be a `list(list(list(data.frame))))`) and there's no need to invoke that number of R object conversions between the initial state (an arrow array) and the final state (an sfc column). The nanoarrow_vctr allows something like: ```r df <- convert_array(some_array_containing_a_geoarrow_col) st_as_sfc(df$geometry) # or s2::as_s2_geography(df$geometry), or something else ``` A side-effect of this change is that we have an escape hatch for conversions that are lossy or contain types with no R equivalent. A quick demo: ``` r library(nanoarrow) arrays <- lapply( list(1:5, 6:10, 11:13), as_nanoarrow_array ) # A vctr can be created from any stream (vctr <- as_nanoarrow_vctr(basic_array_stream(arrays))) #> <nanoarrow_vctr int32[13]> #> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 # Under the hood this is something like a factor() where levels are # a list of arrays with cached offsets. This is like an Arrow ChunkedArray str(vctr) #> <nanoarrow_vctr int32[13]> #> List of 3 #> $ :<nanoarrow_array int32[5]> #> ..$ length : int 5 #> ..$ null_count: int 0 #> ..$ offset : int 0 #> ..$ buffers :List of 2 #> .. ..$ :<nanoarrow_buffer validity<bool>[0][0 b]> `` #> .. ..$ :<nanoarrow_buffer data<int32>[5][20 b]> `1 2 3 4 5` #> ..$ dictionary: NULL #> ..$ children : list() #> $ :<nanoarrow_array int32[5]> #> ..$ length : int 5 #> ..$ null_count: int 0 #> ..$ offset : int 0 #> ..$ buffers :List of 2 #> .. ..$ :<nanoarrow_buffer validity<bool>[0][0 b]> `` #> .. ..$ :<nanoarrow_buffer data<int32>[5][20 b]> `6 7 8 9 10` #> ..$ dictionary: NULL #> ..$ children : list() #> $ :<nanoarrow_array int32[3]> #> ..$ length : int 3 #> ..$ null_count: int 0 #> ..$ offset : int 0 #> ..$ buffers :List of 2 #> .. ..$ :<nanoarrow_buffer validity<bool>[0][0 b]> `` #> .. ..$ :<nanoarrow_buffer data<int32>[3][12 b]> `11 12 13` #> ..$ dictionary: NULL #> ..$ children : list() # vctrs can be sliced: head(vctr) #> <nanoarrow_vctr int32[6]> #> [1] 1 2 3 4 5 6 # ...and can live in a data.frame head(tibble::tibble(x = vctr)) #> # A tibble: 6 × 1 #> x #> <nnrrw_vc> #> 1 1 #> 2 2 #> 3 3 #> 4 4 #> 5 5 #> 6 6 # They can be used as zero-copy conversion targets array <- as_nanoarrow_array(1:5) convert_array(array, nanoarrow_vctr()) #> <nanoarrow_vctr int32[5]> #> [1] 1 2 3 4 5 # ...also works in a nested ptype array <- as_nanoarrow_array(data.frame(x = 1:5)) convert_array(array, tibble::tibble(x = nanoarrow_vctr())) #> # A tibble: 5 × 1 #> x #> <nnrrw_vc> #> 1 1 #> 2 2 #> 3 3 #> 4 4 #> 5 5 # For nested list output, it will give a slice of the original array for # each list item array <- as_nanoarrow_array( list(1:5, 6:10, NULL, 11:13), schema = na_list(na_int32()) ) (lst_of <- convert_array(array, vctrs::list_of(nanoarrow_vctr()))) #> <list_of<nanoarrow_vctr>[4]> #> [[1]] #> <nanoarrow_vctr int32[5]> #> [1] 1 2 3 4 5 #> #> [[2]] #> <nanoarrow_vctr int32[5]> #> [1] 6 7 8 9 10 #> #> [[3]] #> NULL #> #> [[4]] #> <nanoarrow_vctr int32[3]> #> [1] 11 12 13 for (i in seq_along(lst_of)) { array <- attr(lst_of[[i]], "chunks")[[1]] cat(sprintf("offset: %d, length: %d\n", array$offset, array$length)) } #> offset: 0, length: 5 #> offset: 5, length: 5 #> offset: 10, length: 3 ``` <sup>Created on 2024-05-10 with [reprex v2.1.0](https://reprex.tidyverse.org)</sup>
1 parent 15bf791 commit 1db7f90

File tree

16 files changed

+1172
-57
lines changed

16 files changed

+1172
-57
lines changed

r/NAMESPACE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ S3method("$",nanoarrow_buffer)
66
S3method("$",nanoarrow_schema)
77
S3method("$<-",nanoarrow_array)
88
S3method("$<-",nanoarrow_schema)
9+
S3method("[",nanoarrow_vctr)
10+
S3method("[<-",nanoarrow_vctr)
911
S3method("[[",nanoarrow_array)
1012
S3method("[[",nanoarrow_array_stream)
1113
S3method("[[",nanoarrow_buffer)
1214
S3method("[[",nanoarrow_schema)
1315
S3method("[[<-",nanoarrow_array)
1416
S3method("[[<-",nanoarrow_schema)
17+
S3method("[[<-",nanoarrow_vctr)
1518
S3method(as.data.frame,nanoarrow_array)
1619
S3method(as.data.frame,nanoarrow_array_stream)
20+
S3method(as.data.frame,nanoarrow_vctr)
1721
S3method(as.raw,nanoarrow_buffer)
1822
S3method(as.vector,nanoarrow_array)
1923
S3method(as.vector,nanoarrow_array_stream)
@@ -48,22 +52,27 @@ S3method(as_nanoarrow_array_stream,data.frame)
4852
S3method(as_nanoarrow_array_stream,default)
4953
S3method(as_nanoarrow_array_stream,nanoarrow_array)
5054
S3method(as_nanoarrow_array_stream,nanoarrow_array_stream)
55+
S3method(as_nanoarrow_array_stream,nanoarrow_vctr)
5156
S3method(as_nanoarrow_buffer,default)
5257
S3method(as_nanoarrow_buffer,nanoarrow_buffer)
5358
S3method(as_nanoarrow_schema,DataType)
5459
S3method(as_nanoarrow_schema,Field)
5560
S3method(as_nanoarrow_schema,Schema)
5661
S3method(as_nanoarrow_schema,nanoarrow_schema)
62+
S3method(as_nanoarrow_schema,nanoarrow_vctr)
63+
S3method(c,nanoarrow_vctr)
5764
S3method(convert_array,default)
5865
S3method(convert_array,double)
5966
S3method(convert_array,factor)
67+
S3method(convert_array,nanoarrow_vctr)
6068
S3method(convert_array,vctrs_partial_frame)
6169
S3method(convert_array_extension,default)
6270
S3method(convert_array_extension,nanoarrow_extension_spec_vctrs)
6371
S3method(format,nanoarrow_array)
6472
S3method(format,nanoarrow_array_stream)
6573
S3method(format,nanoarrow_buffer)
6674
S3method(format,nanoarrow_schema)
75+
S3method(format,nanoarrow_vctr)
6776
S3method(infer_nanoarrow_ptype_extension,default)
6877
S3method(infer_nanoarrow_ptype_extension,nanoarrow_extension_spec_vctrs)
6978
S3method(infer_nanoarrow_schema,Array)
@@ -93,6 +102,7 @@ S3method(infer_nanoarrow_schema,list)
93102
S3method(infer_nanoarrow_schema,logical)
94103
S3method(infer_nanoarrow_schema,nanoarrow_array)
95104
S3method(infer_nanoarrow_schema,nanoarrow_array_stream)
105+
S3method(infer_nanoarrow_schema,nanoarrow_vctr)
96106
S3method(infer_nanoarrow_schema,raw)
97107
S3method(infer_nanoarrow_schema,vctrs_list_of)
98108
S3method(infer_nanoarrow_schema,vctrs_unspecified)
@@ -108,19 +118,22 @@ S3method(print,nanoarrow_array)
108118
S3method(print,nanoarrow_array_stream)
109119
S3method(print,nanoarrow_buffer)
110120
S3method(print,nanoarrow_schema)
121+
S3method(print,nanoarrow_vctr)
111122
S3method(read_nanoarrow,character)
112123
S3method(read_nanoarrow,connection)
113124
S3method(read_nanoarrow,raw)
114125
S3method(str,nanoarrow_array)
115126
S3method(str,nanoarrow_array_stream)
116127
S3method(str,nanoarrow_buffer)
117128
S3method(str,nanoarrow_schema)
129+
S3method(str,nanoarrow_vctr)
118130
export(array_stream_set_finalizer)
119131
export(as_nanoarrow_array)
120132
export(as_nanoarrow_array_extension)
121133
export(as_nanoarrow_array_stream)
122134
export(as_nanoarrow_buffer)
123135
export(as_nanoarrow_schema)
136+
export(as_nanoarrow_vctr)
124137
export(basic_array_stream)
125138
export(collect_array_stream)
126139
export(convert_array)
@@ -191,6 +204,7 @@ export(nanoarrow_pointer_release)
191204
export(nanoarrow_pointer_set_protected)
192205
export(nanoarrow_schema_modify)
193206
export(nanoarrow_schema_parse)
207+
export(nanoarrow_vctr)
194208
export(nanoarrow_version)
195209
export(read_nanoarrow)
196210
export(register_nanoarrow_extension)

r/R/convert-array.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ convert_fallback_other <- function(array, offset, length, to) {
139139
convert_array(array, to, .from_c = TRUE)
140140
}
141141

142+
#' @export
143+
convert_array.nanoarrow_vctr <- function(array, to, ...) {
144+
schema <- attr(to, "schema", exact = TRUE)
145+
if (is.null(schema)) {
146+
schema <- infer_nanoarrow_schema(array)
147+
}
148+
149+
new_nanoarrow_vctr(list(array), schema, class(to))
150+
}
151+
142152
#' @export
143153
convert_array.double <- function(array, to, ...) {
144154
# Handle conversion from decimal128 via arrow

0 commit comments

Comments
 (0)