paleolimbot commented on code in PR #39: URL: https://github.com/apache/arrow-nanoarrow/pull/39#discussion_r960614278
########## r/R/pkg-arrow.R: ########## @@ -0,0 +1,168 @@ +# 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. + +# exported in zzz.R +infer_type.nanoarrow_array <- function(x, ...) { + arrow::as_data_type(infer_nanoarrow_schema(x, ...)) +} + +as_data_type.nanoarrow_schema <- function(x, ...) { + exportable_schema <- nanoarrow_allocate_schema() + nanoarrow_pointer_export(x, exportable_schema) + asNamespace("arrow")$DataType$import_from_c(exportable_schema) +} + +as_schema.nanoarrow_schema <- function(x, ...) { + exportable_schema <- nanoarrow_allocate_schema() + nanoarrow_pointer_export(x, exportable_schema) + arrow::Schema$import_from_c(exportable_schema) +} + +as_arrow_array.nanoarrow_array <- function(x, ..., type = NULL) { + exportable_schema <- nanoarrow_allocate_schema() + exportable_array <- nanoarrow_allocate_array() + + schema <- .Call(nanoarrow_c_infer_schema_array, x) + nanoarrow_pointer_export(schema, exportable_schema) + nanoarrow_pointer_export(x, exportable_array) + + result <- arrow::Array$import_from_c(exportable_array, exportable_schema) + + if (!is.null(type)) { + result$cast(type) + } else { + result + } +} + +as_chunked_array.nanoarrow_array <- function(x, ..., type = NULL) { + arrow::as_chunked_array(as_arrow_array.nanoarrow_array(x, ..., type = type)) +} + +as_record_batch.nanoarrow_array <- function(x, ..., schema = NULL) { + exportable_schema <- nanoarrow_allocate_schema() + exportable_array <- nanoarrow_allocate_array() + + nanoarrow_pointer_export( + .Call(nanoarrow_c_infer_schema_array, x), + exportable_schema + ) + nanoarrow_pointer_export(x, exportable_array) + + result <- arrow::RecordBatch$import_from_c(exportable_array, exportable_schema) + + if (!is.null(schema)) { + arrow::as_record_batch(result, schema = schema) + } else { + result + } +} + +as_arrow_table.nanoarrow_array <- function(x, ..., schema = NULL) { + arrow::as_arrow_table( + as_record_batch.nanoarrow_array(x, schema = schema) + ) +} + +as_record_batch_reader.nanoarrow_array_stream <- function(x, ..., schema = NULL) { + stopifnot(is.null(schema)) + + # RecordBatchReaders are mutable and shouldn't be pulled from more than one + # place, so it's safe to move them and invalidate any R references to `x` + arrow::RecordBatchReader$import_from_c(x) +} + +#' @export +as_nanoarrow_schema.DataType <- function(x, ...) { + schema <- nanoarrow_allocate_schema() + x$export_to_c(schema) + schema +} + +#' @export +as_nanoarrow_schema.Field <- function(x, ...) { + schema <- nanoarrow_allocate_schema() + x$export_to_c(schema) + schema +} + +#' @export +as_nanoarrow_schema.Schema <- function(x, ...) { + schema <- nanoarrow_allocate_schema() + x$export_to_c(schema) + schema +} + +#' @export +as_nanoarrow_array.Array <- function(x, ..., schema = NULL) { + imported_schema <- nanoarrow_allocate_schema() + array <- nanoarrow_allocate_array() + + if (!is.null(schema)) { + x <- x$cast(arrow::as_data_type(schema)) + } + + x$export_to_c(array, imported_schema) + + nanoarrow_array_set_schema(array, imported_schema) + array +} + +#' @export +as_nanoarrow_array.ChunkedArray <- function(x, ..., schema = NULL) { + if (is.null(schema)) { + array <- arrow::as_arrow_array(x) + } else { + array <- arrow::as_arrow_array(x, type = arrow::as_data_type(schema)) Review Comment: It does concatenate the chunks, although this won't be how chunked arrays get passed to Python. The `as_nanoarrow_array()` generic is supposed to be along the lines of "get me a `struct ArrowArray` with `schema` if you can, or error if you can't". Sometimes you really do just need a continguous chunk of values and because everything in arrow returns a `Table`, not having this method end up being pretty annoying. I didn't implement it here yet but `as_arrow_array_stream.ChunkedArray()` exists in narrow and is what I used in geoarrow to do all conversion to R anything. (Arrow C++ doesn't let you stream anything that isn't a StructArray i.e., RecordBatch, and I haven't implemented that stream stuff here yet). One could also make a chunked array-ish thing that is a `list()` of `nanoarrow_array` (I did this in geoarrow to [implement a vctrs class](https://github.com/paleolimbot/geoarrow/blob/master/R/vctr.R#L126-L137) that's along the lines of a `factor()` but where the levels are a `list()` of `nanoarrow_array`). I'm hesitant to add another class (I think streaming will probably be fine). -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
