Skip to content

Commit 8805697

Browse files
committed
test nested cases
1 parent 8eb77e0 commit 8805697

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

r/src/materialize.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ static int nanoarrow_materialize_nanoarrow_vctr(struct RConverter* converter,
375375
array_export(array_xptr, out_array);
376376
R_SetExternalPtrTag(array_out_xptr, schema_xptr);
377377

378+
// Update the offset/length in case a slice is being requested from the
379+
// converter.
380+
out_array->offset += converter->src.offset;
381+
out_array->length = converter->src.length;
382+
378383
// Get the cached copy of the pairlist node at the end of the current
379384
// chunks list.
380385
SEXP chunks_tail_sym = PROTECT(Rf_install("chunks_tail"));

r/tests/testthat/test-convert-array.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,50 @@ test_that("batched convert to vector works for nanoarrow_vctr()", {
216216
)
217217
})
218218

219+
test_that("convert to vector works for data.frame(nanoarrow_vctr())", {
220+
array <- as_nanoarrow_array(data.frame(x = 1:5))
221+
df_vctr <- convert_array(array, data.frame(x = nanoarrow_vctr()))
222+
expect_s3_class(df_vctr$x, "nanoarrow_vctr")
223+
expect_identical(
224+
convert_array_stream(as_nanoarrow_array_stream(df_vctr$x)),
225+
1:5
226+
)
227+
})
228+
229+
test_that("convert to vector works for list_of(nanoarrow_vctr())", {
230+
skip_if_not_installed("arrow")
231+
skip_if_not_installed("vctrs")
232+
233+
array <- as_nanoarrow_array(
234+
list(1:5, 6:10, NULL, 11:13),
235+
schema = na_list(na_int32())
236+
)
237+
238+
list_vctr <- convert_array(array, vctrs::list_of(nanoarrow_vctr()))
239+
240+
# Each item in the list should be a vctr with one chunk that is a slice
241+
# of the original array
242+
expect_s3_class(list_vctr[[1]], "nanoarrow_vctr")
243+
vctr_array <- attr(list_vctr[[1]], "chunks")[[1]]
244+
expect_identical(vctr_array$offset, 0L)
245+
expect_identical(vctr_array$length, 5L)
246+
expect_identical(convert_buffer(vctr_array$buffers[[2]]), 1:5)
247+
248+
expect_s3_class(list_vctr[[2]], "nanoarrow_vctr")
249+
vctr_array <- attr(list_vctr[[2]], "chunks")[[1]]
250+
expect_identical(vctr_array$offset, 5L)
251+
expect_identical(vctr_array$length, 5L)
252+
expect_identical(convert_buffer(vctr_array$buffers[[2]]), 1:10)
253+
254+
expect_null(list_vctr[[3]])
255+
256+
expect_s3_class(list_vctr[[4]], "nanoarrow_vctr")
257+
vctr_array <- attr(list_vctr[[4]], "chunks")[[1]]
258+
expect_identical(vctr_array$offset, 10L)
259+
expect_identical(vctr_array$length, 3L)
260+
expect_identical(convert_buffer(vctr_array$buffers[[2]]), 1:13)
261+
})
262+
219263
test_that("batched convert to vector works for nanoarrow_vctr() keeps subclass", {
220264
vctr_ptype <- nanoarrow_vctr(subclass = "some_subclass")
221265

0 commit comments

Comments
 (0)