Skip to content

Commit

Permalink
move error to c
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed Dec 14, 2024
1 parent 407ad03 commit 119d6ef
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 33 deletions.
1 change: 0 additions & 1 deletion r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ S3method(c,nanoarrow_vctr)
S3method(convert_array,default)
S3method(convert_array,double)
S3method(convert_array,factor)
S3method(convert_array,matrix)
S3method(convert_array,nanoarrow_vctr)
S3method(convert_array,vctrs_partial_frame)
S3method(convert_array_extension,default)
Expand Down
31 changes: 0 additions & 31 deletions r/R/convert-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,37 +173,6 @@ convert_array.double <- function(array, to, ...) {
}
}

#' @export
convert_array.matrix <- function(array, to, ...) {
schema <- infer_nanoarrow_schema(array)
parsed <- nanoarrow_schema_parse(schema)
if (parsed$type != "fixed_size_list") {
stop_cant_convert_array(array, to)
}

if (identical(to, matrix())) {
to <- matrix(
infer_nanoarrow_ptype(array$children[[1]]),
nrow = 0,
ncol = parsed$fixed_size
)
}

if (ncol(to) != parsed$fixed_size) {
stop(
sprintf(
"Can't convert fixed_size_list(list_size=%d) to matrix with %d cols",
parsed$fixed_size,
ncol(to)
)
)
}

attributes(to) <- NULL
child_vctr <- convert_array(array$children[[1]], to)
matrix(child_vctr, ncol = parsed$fixed_size, byrow = TRUE)
}

#' @export
convert_array.vctrs_partial_frame <- function(array, to, ...) {
ptype <- infer_nanoarrow_ptype(array)
Expand Down
6 changes: 6 additions & 0 deletions r/src/materialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,12 @@ static int nanoarrow_materialize_matrix(struct RConverter* converter,
return EINVAL;
}

if (converter->schema_view.fixed_size != Rf_ncols(converter->ptype_view.ptype)) {
Rf_error("Can't convert fixed_size_list(list_size=%d) to matrix with %d cols",
(int)converter->schema_view.fixed_size,
Rf_ncols(converter->ptype_view.ptype));
}

int64_t raw_src_offset = src->array_view->array->offset + src->offset;
int64_t list_length = src->array_view->layout.child_size_elements;
int64_t offset;
Expand Down
1 change: 0 additions & 1 deletion r/tests/testthat/test-convert-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,6 @@ test_that("convert to vector works for fixed_size_list_of() -> matrix()", {
mat <- matrix(1:6, ncol = 2, byrow = TRUE)
array <- as_nanoarrow_array(mat)

expect_identical(convert_array(array, matrix()), mat)
expect_identical(
convert_array(array, matrix(double(), ncol = 2)),
matrix(as.double(1:6), ncol = 2, byrow = TRUE)
Expand Down

0 comments on commit 119d6ef

Please sign in to comment.