Skip to content

Commit

Permalink
stub extension type implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed Aug 24, 2023
1 parent 4ace1a2 commit 17eaf46
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
12 changes: 12 additions & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ S3method(as_nanoarrow_array,factor)
S3method(as_nanoarrow_array,nanoarrow_array)
S3method(as_nanoarrow_array,nanoarrow_buffer)
S3method(as_nanoarrow_array,vctrs_unspecified)
S3method(as_nanoarrow_array_extension,default)
S3method(as_nanoarrow_array_stream,RecordBatchReader)
S3method(as_nanoarrow_array_stream,data.frame)
S3method(as_nanoarrow_array_stream,default)
S3method(as_nanoarrow_array_stream,nanoarrow_array_stream)
S3method(as_nanoarrow_array_stream_extension,default)
S3method(as_nanoarrow_buffer,default)
S3method(as_nanoarrow_buffer,nanoarrow_buffer)
S3method(as_nanoarrow_schema,DataType)
Expand All @@ -46,10 +48,12 @@ S3method(convert_array,default)
S3method(convert_array,double)
S3method(convert_array,factor)
S3method(convert_array,vctrs_partial_frame)
S3method(convert_array_extension,default)
S3method(format,nanoarrow_array)
S3method(format,nanoarrow_array_stream)
S3method(format,nanoarrow_buffer)
S3method(format,nanoarrow_schema)
S3method(infer_nanoarrow_ptype_extension,default)
S3method(infer_nanoarrow_schema,Array)
S3method(infer_nanoarrow_schema,ArrowTabular)
S3method(infer_nanoarrow_schema,ChunkedArray)
Expand Down Expand Up @@ -94,15 +98,19 @@ S3method(str,nanoarrow_buffer)
S3method(str,nanoarrow_schema)
export(array_stream_set_finalizer)
export(as_nanoarrow_array)
export(as_nanoarrow_array_extension)
export(as_nanoarrow_array_stream)
export(as_nanoarrow_array_stream_extension)
export(as_nanoarrow_buffer)
export(as_nanoarrow_schema)
export(basic_array_stream)
export(collect_array_stream)
export(convert_array)
export(convert_array_extension)
export(convert_array_stream)
export(convert_buffer)
export(infer_nanoarrow_ptype)
export(infer_nanoarrow_ptype_extension)
export(infer_nanoarrow_schema)
export(na_binary)
export(na_bool)
Expand Down Expand Up @@ -151,6 +159,7 @@ export(nanoarrow_array_modify)
export(nanoarrow_array_set_schema)
export(nanoarrow_buffer_append)
export(nanoarrow_buffer_init)
export(nanoarrow_extension_spec)
export(nanoarrow_pointer_addr_chr)
export(nanoarrow_pointer_addr_dbl)
export(nanoarrow_pointer_addr_pretty)
Expand All @@ -162,6 +171,9 @@ export(nanoarrow_pointer_set_protected)
export(nanoarrow_schema_modify)
export(nanoarrow_schema_parse)
export(nanoarrow_version)
export(register_nanoarrow_extension)
export(resolve_nanoarrow_extension)
export(unregister_nanoarrow_extension)
importFrom(utils,getFromNamespace)
importFrom(utils,str)
useDynLib(nanoarrow, .registration = TRUE)
125 changes: 125 additions & 0 deletions r/R/type-extension.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# 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.

#' Register Arrow extension types
#'
#' @param extension_name An Arrow extension type name (e.g., arrow.r.vctrs)
#' @param extension_spec An extension specification inheriting from
#' 'nanoarrow_extension_spec'.
#' @param data Optional data to include in the extension type specification
#' @param subclass A subclass for the extension type specification. Extension
#' methods will dispatch on this object.
#'
#' @return
#' - `nanoarrow_extension_spec()` returns an object of class
#' 'nanoarrow_extension_spec'.
#' - `register_nanoarrow_extension()` returns `extension_spec`, invisibly.
#' - `unregister_nanoarrow_extension()` returns `extension_name`, invisibly.
#' - `resolve_nanoarrow_extension()` returns an object of class
#' 'nanoarrow_extension_spec' or NULL if the extension type was not
#' registered.
#' @export
#'
#' @examples
#' nanoarrow_extension_spec("mynamespace.mytype", subclass = "mypackage_mytype_spec")
nanoarrow_extension_spec <- function(extension_name, data = NULL,
subclass = character()) {
structure(
list(extension_name = extension_name, data = data),
class = union(subclass, "nanoarrow_extension_spec")
)
}

#' @rdname nanoarrow_extension_spec
#' @export
register_nanoarrow_extension <- function(extension_spec) {
extension_registry[[extension_spec$extension_name]] <- extension_spec
invisible(extension_spec)
}

#' @rdname nanoarrow_extension_spec
#' @export
unregister_nanoarrow_extension <- function(extension_name) {
extension_registry[[extension_name]] <- NULL
invisible(extension_name)
}

#' @rdname nanoarrow_extension_spec
#' @export
resolve_nanoarrow_extension <- function(extension_name) {
extension_registry[[extension_name]]
}


#' Implement Arrow extension types
#'
#' @inheritParams nanoarrow_extension_spec
#' @param x,array,to,schema,... Passed from [infer_nanoarrow_ptype()],
#' [convert_array()], [as_nanoarrow_array()], and/or
#' [as_nanoarrow_array_stream()].
#'
#' @return
#' - `infer_nanoarrow_ptype_extension()`: The R vector prototype to be used
#' as the default conversion target.
#' - `convert_array_extension()`: An R vector of type `to`.
#' - `as_nanoarrow_array_extension()`: A [nanoarrow_array][as_nanoarrow_array].
#' @export
#'
infer_nanoarrow_ptype_extension <- function(extension_spec, x) {
UseMethod("infer_nanoarrow_ptype_extension")
}

#' @export
infer_nanoarrow_ptype_extension.default <- function(extension_spec, x) {
stop("not implemented")
}

#' @rdname infer_nanoarrow_ptype_extension
#' @export
convert_array_extension <- function(extension_spec, array, to, ...) {
UseMethod("convert_array_extension")
}

#' @export
convert_array_extension.default <- function(extension_spec, array, to, ...) {
stop_cant_convert_array(array, to)
}

#' @rdname infer_nanoarrow_ptype_extension
#' @export
as_nanoarrow_array_extension <- function(extension_spec, x, ..., schema = NULL) {
UseMethod("as_nanoarrow_array_extension")
}

#' @export
as_nanoarrow_array_extension.default <- function(extension_spec, x, ..., schema = NULL) {
stop("Not implemented")
}

#' @rdname infer_nanoarrow_ptype_extension
#' @export
as_nanoarrow_array_stream_extension <- function(extension_spec, x, ..., schema = NULL) {
UseMethod("as_nanoarrow_array_stream_extension")
}

#' @export
as_nanoarrow_array_stream_extension.default <- function(extension_spec, x, ..., schema = NULL) {
stop("not implemented")
}

# Mutable registry to look up extension specifications
extension_registry <- new.env(parent = emptyenv())
36 changes: 36 additions & 0 deletions r/man/infer_nanoarrow_ptype_extension.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions r/man/nanoarrow_extension_spec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions r/tests/testthat/test-type-extension.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.

test_that("multiplication works", {
expect_equal(2 * 2, 4)
})

0 comments on commit 17eaf46

Please sign in to comment.