Skip to content

Commit 4eaf0b0

Browse files
authored
refactor: Use nanoarrow_vctr in forthcoming nanoarrow release (#36)
GeoArrow was the wrong place for this class...it's much better in nanoarrow! See apache/arrow-nanoarrow#461 for the implementation.
1 parent 7065a18 commit 4eaf0b0

File tree

10 files changed

+19
-429
lines changed

10 files changed

+19
-429
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Encoding: UTF-8
1414
Roxygen: list(markdown = TRUE)
1515
RoxygenNote: 7.2.3
1616
Imports:
17-
nanoarrow (>= 0.3.0),
17+
nanoarrow (>= 0.5.0),
1818
wk (>= 0.6.0)
1919
LinkingTo:
2020
wk

NAMESPACE

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
S3method("[",geoarrow_vctr)
4-
S3method("[<-",geoarrow_vctr)
5-
S3method("[[<-",geoarrow_vctr)
63
S3method(as.character,geoarrow_vctr)
74
S3method(as_geoarrow_array,character)
85
S3method(as_geoarrow_array,default)
@@ -18,8 +15,6 @@ S3method(as_geoarrow_array_stream,geoarrow_vctr)
1815
S3method(as_geoarrow_array_stream,nanoarrow_array_stream)
1916
S3method(as_nanoarrow_array,sfc)
2017
S3method(as_nanoarrow_array_extension,geoarrow_extension_spec)
21-
S3method(as_nanoarrow_array_stream,geoarrow_vctr)
22-
S3method(as_nanoarrow_schema,geoarrow_vctr)
2318
S3method(convert_array,geoarrow_vctr)
2419
S3method(convert_array,sfc)
2520
S3method(convert_array,wk_wkb)
@@ -31,7 +26,6 @@ S3method(infer_geoarrow_schema,default)
3126
S3method(infer_geoarrow_schema,nanoarrow_array)
3227
S3method(infer_geoarrow_schema,nanoarrow_array_stream)
3328
S3method(infer_nanoarrow_ptype_extension,geoarrow_extension_spec)
34-
S3method(infer_nanoarrow_schema,geoarrow_vctr)
3529
S3method(infer_nanoarrow_schema,sfc)
3630
S3method(infer_nanoarrow_schema,wk_wkb)
3731
S3method(infer_nanoarrow_schema,wk_wkt)
@@ -53,7 +47,6 @@ export(na_extension_wkb)
5347
export(na_extension_wkt)
5448
importFrom(nanoarrow,as_nanoarrow_array)
5549
importFrom(nanoarrow,as_nanoarrow_array_extension)
56-
importFrom(nanoarrow,as_nanoarrow_array_stream)
5750
importFrom(nanoarrow,as_nanoarrow_schema)
5851
importFrom(nanoarrow,convert_array)
5952
importFrom(nanoarrow,convert_array_extension)

R/pkg-arrow.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ as_arrow_array.geoarrow_vctr <- function(x, ..., type = NULL) {
88
}
99
}
1010

11+
#' @importFrom nanoarrow as_nanoarrow_schema
1112
as_chunked_array.geoarrow_vctr <- function(x, ..., type = NULL) {
1213
if (is.null(type)) {
1314
schema <- NULL
@@ -18,7 +19,7 @@ as_chunked_array.geoarrow_vctr <- function(x, ..., type = NULL) {
1819
}
1920

2021
# as_nanoarrow_array_stream() applies the indices if vctr is sliced
21-
stream <- as_nanoarrow_array_stream(x, schema = schema)
22+
stream <- as_geoarrow_array_stream(x, schema = schema)
2223
chunks <- nanoarrow::collect_array_stream(stream, validate = FALSE)
2324
type <- arrow::as_data_type(type)
2425

R/pkg-nanoarrow.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ register_geoarrow_extension <- function() {
2020
#' @importFrom nanoarrow infer_nanoarrow_ptype_extension
2121
#' @export
2222
infer_nanoarrow_ptype_extension.geoarrow_extension_spec <- function(extension_spec, x, ...) {
23-
new_geoarrow_vctr(list(), x, integer())
23+
nanoarrow::nanoarrow_vctr(schema = x, subclass = "geoarrow_vctr")
2424
}
2525

2626
#' @importFrom nanoarrow convert_array_extension

R/vctr.R

Lines changed: 3 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,7 @@ as_geoarrow_vctr <- function(x, ..., schema = NULL) {
1818
}
1919

2020
stream <- as_geoarrow_array_stream(x, ..., schema = schema)
21-
chunks <- nanoarrow::collect_array_stream(stream, validate = FALSE)
22-
new_geoarrow_vctr(chunks, stream$get_schema())
23-
}
24-
25-
new_geoarrow_vctr <- function(chunks, schema, indices = NULL) {
26-
offsets <- .Call(geoarrow_c_vctr_chunk_offsets, chunks)
27-
if (is.null(indices)) {
28-
indices <- seq_len(offsets[length(offsets)])
29-
}
30-
31-
structure(
32-
indices,
33-
schema = schema,
34-
chunks = chunks,
35-
offsets = offsets,
36-
class = c("geoarrow_vctr", "wk_vctr")
37-
)
38-
}
39-
40-
#' @export
41-
`[.geoarrow_vctr` <- function(x, i) {
42-
attrs <- attributes(x)
43-
x <- NextMethod()
44-
45-
if (is.null(vctr_as_slice(x))) {
46-
stop(
47-
"Can't subset geoarrow_vctr with non-slice (e.g., only i:j indexing is supported)"
48-
)
49-
}
50-
51-
attributes(x) <- attrs
52-
x
53-
}
54-
55-
#' @export
56-
`[<-.geoarrow_vctr` <- function(x, i, value) {
57-
stop("subset assignment for geoarrow_vctr is not supported")
58-
}
59-
60-
#' @export
61-
`[[<-.geoarrow_vctr` <- function(x, i, value) {
62-
stop("subset assignment for geoarrow_vctr is not supported")
21+
nanoarrow::as_nanoarrow_vctr(stream, subclass = "geoarrow_vctr")
6322
}
6423

6524
#' @export
@@ -102,129 +61,8 @@ as.character.geoarrow_vctr <- function(x, ...) {
10261
format(x, ...)
10362
}
10463

105-
#' @export
106-
infer_nanoarrow_schema.geoarrow_vctr <- function(x, ...) {
107-
attr(x, "schema", exact = TRUE)
108-
}
109-
110-
# Because zero-length vctrs are R's way of communicating "type", implement
111-
# as_nanoarrow_schema() here so that it works in places that expect a type
112-
#' @importFrom nanoarrow as_nanoarrow_schema
113-
#' @export
114-
as_nanoarrow_schema.geoarrow_vctr <- function(x, ...) {
115-
attr(x, "schema", exact = TRUE)
116-
}
117-
11864
#' @export
11965
as_geoarrow_array_stream.geoarrow_vctr <- function(x, ..., schema = NULL) {
120-
as_nanoarrow_array_stream.geoarrow_vctr(x, ..., schema = schema)
121-
}
122-
123-
#' @importFrom nanoarrow as_nanoarrow_array_stream
124-
#' @export
125-
as_nanoarrow_array_stream.geoarrow_vctr <- function(x, ..., schema = NULL) {
126-
if (!is.null(schema)) {
127-
stream <- as_nanoarrow_array_stream(x, schema = NULL)
128-
return(as_geoarrow_array_stream(stream, schema = schema))
129-
}
130-
131-
slice <- vctr_as_slice(x)
132-
if (is.null(slice)) {
133-
stop("Can't resolve non-slice geoarrow_vctr to nanoarrow_array_stream")
134-
}
135-
136-
x_schema <- attr(x, "schema", exact = TRUE)
137-
138-
# Zero-size slice can be an array stream with zero batches
139-
if (slice[2] == 0) {
140-
return(nanoarrow::basic_array_stream(list(), schema = x_schema))
141-
}
142-
143-
# Full slice doesn't need slicing logic
144-
offsets <- attr(x, "offsets", exact = TRUE)
145-
batches <- attr(x, "chunks", exact = TRUE)
146-
if (slice[1] == 1 && slice[2] == max(offsets)) {
147-
return(
148-
nanoarrow::basic_array_stream(
149-
batches,
150-
schema = x_schema,
151-
validate = FALSE
152-
)
153-
)
154-
}
155-
156-
# Calculate first and last slice information
157-
first_index <- slice[1] - 1L
158-
end_index <- first_index + slice[2]
159-
last_index <- end_index - 1L
160-
first_chunk_index <- vctr_resolve_chunk(first_index, offsets)
161-
last_chunk_index <- vctr_resolve_chunk(last_index, offsets)
162-
163-
first_chunk_offset <- first_index - offsets[first_chunk_index + 1L]
164-
first_chunk_length <- offsets[first_chunk_index + 2L] - first_index
165-
last_chunk_offset <- 0L
166-
last_chunk_length <- end_index - offsets[last_chunk_index + 1L]
167-
168-
# Calculate first and last slices
169-
if (first_chunk_index == last_chunk_index) {
170-
batch <- vctr_array_slice(
171-
batches[[first_chunk_index + 1L]],
172-
first_chunk_offset,
173-
last_chunk_length - first_chunk_offset
174-
)
175-
176-
return(
177-
nanoarrow::basic_array_stream(
178-
list(batch),
179-
schema = x_schema,
180-
validate = FALSE
181-
)
182-
)
183-
}
184-
185-
batch1 <- vctr_array_slice(
186-
batches[[first_chunk_index + 1L]],
187-
first_chunk_offset,
188-
first_chunk_length
189-
)
190-
191-
batchn <- vctr_array_slice(
192-
batches[[last_chunk_index + 1L]],
193-
last_chunk_offset,
194-
last_chunk_length
195-
)
196-
197-
seq_mid <- seq_len(last_chunk_index - first_chunk_index - 1)
198-
batch_mid <- batches[first_chunk_index + seq_mid]
199-
200-
nanoarrow::basic_array_stream(
201-
c(
202-
list(batch1),
203-
batch_mid,
204-
list(batchn)
205-
),
206-
schema = x_schema,
207-
validate = FALSE
208-
)
209-
}
210-
211-
212-
# Utilities for vctr methods
213-
214-
vctr_resolve_chunk <- function(x, offsets) {
215-
.Call(geoarrow_c_vctr_chunk_resolve, x, offsets)
216-
}
217-
218-
vctr_as_slice <- function(x) {
219-
.Call(geoarrow_c_vctr_as_slice, x)
220-
}
221-
222-
vctr_array_slice <- function(x, offset, length) {
223-
new_offset <- x$offset + offset
224-
new_length <- length
225-
nanoarrow::nanoarrow_array_modify(
226-
x,
227-
list(offset = new_offset, length = new_length),
228-
validate = FALSE
229-
)
66+
stream <- nanoarrow::as_nanoarrow_array_stream(x)
67+
as_geoarrow_array_stream(stream, schema = schema)
23068
}

src/r-init.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ extern SEXP geoarrow_c_make_type(SEXP geometry_type_sexp, SEXP dimensions_sexp,
1414
SEXP coord_type_sexp);
1515
extern SEXP geoarrow_c_schema_init_extension(SEXP schema_xptr, SEXP type_sexp);
1616
extern SEXP geoarrow_c_schema_parse(SEXP schema_xptr, SEXP extension_name_sexp);
17-
extern SEXP geoarrow_c_vctr_chunk_offsets(SEXP array_list);
18-
extern SEXP geoarrow_c_vctr_chunk_resolve(SEXP indices_sexp, SEXP offsets_sexp);
19-
extern SEXP geoarrow_c_vctr_as_slice(SEXP indices_sexp);
2017
extern SEXP geoarrow_c_handle_stream(SEXP data, SEXP handler_xptr);
2118
extern SEXP geoarrow_c_writer_new(SEXP schema_xptr, SEXP array_out_xptr);
2219

@@ -28,9 +25,6 @@ static const R_CallMethodDef CallEntries[] = {
2825
{"geoarrow_c_make_type", (DL_FUNC)&geoarrow_c_make_type, 3},
2926
{"geoarrow_c_schema_init_extension", (DL_FUNC)&geoarrow_c_schema_init_extension, 2},
3027
{"geoarrow_c_schema_parse", (DL_FUNC)&geoarrow_c_schema_parse, 2},
31-
{"geoarrow_c_vctr_chunk_offsets", (DL_FUNC)&geoarrow_c_vctr_chunk_offsets, 1},
32-
{"geoarrow_c_vctr_chunk_resolve", (DL_FUNC)&geoarrow_c_vctr_chunk_resolve, 2},
33-
{"geoarrow_c_vctr_as_slice", (DL_FUNC)&geoarrow_c_vctr_as_slice, 1},
3428
{"geoarrow_c_handle_stream", (DL_FUNC)&geoarrow_c_handle_stream, 2},
3529
{"geoarrow_c_writer_new", (DL_FUNC)&geoarrow_c_writer_new, 2},
3630
{NULL, NULL, 0}};

src/r-vctr.c

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)