paleolimbot opened a new pull request #12467:
URL: https://github.com/apache/arrow/pull/12467


   This PR implements extension type support and registration in the R bindings 
(as has been possible in the Python bindings for some time). The details still 
need to be worked out, but we at least have a working pattern:
   
   ``` r
   library(arrow, warn.conflicts = FALSE)
   library(R6)
   
   SomeExtensionTypeSubclass <- R6Class(
     "SomeExtensionTypeSubclass", inherit = arrow:::ExtensionType,
     public = list(
       some_custom_method = function() {
         private$some_custom_field
       },
       
       .Deserialize = function(storage_type, extension_name, 
extension_metadata) {
         private$some_custom_field <- head(extension_metadata, 5)
       }
     ),
     private = list(
       some_custom_field = NULL
     )
   )
   
   SomeExtensionArraySubclass <- R6Class(
     "SomeExtensionArraySubclass", inherit = arrow:::ExtensionArray,
     public = list(
       some_custom_method = function() {
         self$type$some_custom_method()
       }
     )
   )
   
   type <- arrow:::MakeExtensionType(
     int32(),
     "some_extension_subclass",
     charToRaw("some custom metadata"),
     type_class = SomeExtensionTypeSubclass,
     array_class = SomeExtensionArraySubclass
   )
   
   
   arrow:::RegisterExtensionType(type)
   
   # survives the C API round trip
   ptr_type <- arrow:::allocate_arrow_schema()
   type$export_to_c(ptr_type)
   type2 <- arrow:::DataType$import_from_c(ptr_type)
   
   
   type2
   #> SomeExtensionTypeSubclass
   #> SomeExtensionTypeSubclass <some custom metadata>
   type2$some_custom_method()
   #> [1] 73 6f 6d 65 20
   
   (array <- type$WrapArray(Array$create(1:10)))
   #> SomeExtensionArraySubclass
   #> <SomeExtensionTypeSubclass <some custom metadata>>
   #> [
   #>   1,
   #>   2,
   #>   3,
   #>   4,
   #>   5,
   #>   6,
   #>   7,
   #>   8,
   #>   9,
   #>   10
   #> ]
   array$some_custom_method()
   #> [1] 73 6f 6d 65 20
   
   ptr_array <- arrow:::allocate_arrow_array()
   array$export_to_c(ptr_array, ptr_type)
   (array2 <- Array$import_from_c(ptr_array, ptr_type))
   #> SomeExtensionArraySubclass
   #> <SomeExtensionTypeSubclass <some custom metadata>>
   #> [
   #>   1,
   #>   2,
   #>   3,
   #>   4,
   #>   5,
   #>   6,
   #>   7,
   #>   8,
   #>   9,
   #>   10
   #> ]
   
   arrow:::delete_arrow_schema(ptr_type)
   arrow:::delete_arrow_array(ptr_array)
   ```
   
   <sup>Created on 2022-02-18 by the [reprex 
package](https://reprex.tidyverse.org) (v2.0.1)</sup>


-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to