This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 44e8eb9e feat(r): Allow opt-out of warning for unregistered extension 
types (#632)
44e8eb9e is described below

commit 44e8eb9e333fadc029ce1f463005aff33fcd1218
Author: Dewey Dunnington <[email protected]>
AuthorDate: Sun Sep 22 21:23:42 2024 -0500

    feat(r): Allow opt-out of warning for unregistered extension types (#632)
    
    Closes #631.
    
    ``` r
    # Using pak::pak("apache/arrow-nanoarrow/r#632")
    library(nanoarrow)
    array <- as_nanoarrow_array(data.frame(x = 1:5, y = letters[1:5]))
    array$children$z <- nanoarrow_extension_array(letters[1:5], 
"some_extension")
    
    # warns!
    tibble::as_tibble(array)
    #> Warning in warn_unregistered_extension_type(x): z: Converting unknown 
extension
    #> some_extension{string} as storage type
    #> Warning in warn_unregistered_extension_type(storage): z: Converting 
unknown
    #> extension some_extension{string} as storage type
    #> # A tibble: 5 × 3
    #>       x y     z
    #>   <int> <chr> <chr>
    #> 1     1 a     a
    #> 2     2 b     b
    #> 3     3 c     c
    #> 4     4 d     d
    #> 5     5 e     e
    
    # doesn't!
    options(nanoarrow.warn_unregistered_extension = FALSE)
    tibble::as_tibble(array)
    #> # A tibble: 5 × 3
    #>       x y     z
    #>   <int> <chr> <chr>
    #> 1     1 a     a
    #> 2     2 b     b
    #> 3     3 c     c
    #> 4     4 d     d
    #> 5     5 e     e
    ```
    
    <sup>Created on 2024-09-20 with [reprex
    v2.1.1](https://reprex.tidyverse.org)</sup>
---
 r/R/convert-array.R               |  4 ++++
 r/R/extension.R                   | 33 +++++++++++++++++++--------------
 r/man/convert_array.Rd            |  4 ++++
 r/tests/testthat/test-extension.R | 22 ++++++++++++++++++++++
 4 files changed, 49 insertions(+), 14 deletions(-)

diff --git a/r/R/convert-array.R b/r/R/convert-array.R
index a8dbbb10..a33273e7 100644
--- a/r/R/convert-array.R
+++ b/r/R/convert-array.R
@@ -24,6 +24,10 @@
 #' dispatching on `to`: developers may implement their own S3 methods for
 #' custom vector types.
 #'
+#' Note that unregistered extension types will by default issue a warning.
+#' Use `options(nanoarrow.warn_unregistered_extension = FALSE)` to disable
+#' this behaviour.
+#'
 #' @param array A [nanoarrow_array][as_nanoarrow_array].
 #' @param to A target prototype object describing the type to which `array`
 #'   should be converted, or `NULL` to use the default conversion as
diff --git a/r/R/extension.R b/r/R/extension.R
index 46fa03b2..216fbe84 100644
--- a/r/R/extension.R
+++ b/r/R/extension.R
@@ -164,23 +164,28 @@ nanoarrow_extension_array <- function(storage_array, 
extension_name,
 }
 
 warn_unregistered_extension_type <- function(x) {
+  # Allow an opt-out of this warning for consumers that don't have
+  # control over their source and want to drop unknown extensions
+  if (!getOption("nanoarrow.warn_unregistered_extension", TRUE)) {
+    return()
+  }
+
   # Warn that we're about to ignore an extension type
+  message <- sprintf(
+    paste0(
+      "Converting unknown extension %s as storage type\n",
+      "Disable warning with ",
+      "options(nanoarrow.warn_unregistered_extension = FALSE)"
+    ),
+    nanoarrow_schema_formatted(x)
+  )
+
+  # Add the field name if we know it
   if (!is.null(x$name) && !identical(x$name, "")) {
-    warning(
-      sprintf(
-        "%s: Converting unknown extension %s as storage type",
-        x$name,
-        nanoarrow_schema_formatted(x)
-      )
-    )
-  } else {
-    warning(
-      sprintf(
-        "Converting unknown extension %s as storage type",
-        nanoarrow_schema_formatted(x)
-      )
-    )
+    message <- paste0(x$name, ": ", message)
   }
+
+  warning(message)
 }
 
 # Mutable registry to look up extension specifications
diff --git a/r/man/convert_array.Rd b/r/man/convert_array.Rd
index 6ed09890..49e1d49f 100644
--- a/r/man/convert_array.Rd
+++ b/r/man/convert_array.Rd
@@ -28,6 +28,10 @@ dispatching on \code{to}: developers may implement their own 
S3 methods for
 custom vector types.
 }
 \details{
+Note that unregistered extension types will by default issue a warning.
+Use \code{options(nanoarrow.warn_unregistered_extension = FALSE)} to disable
+this behaviour.
+
 Conversions are implemented for the following R vector types:
 \itemize{
 \item \code{\link[=logical]{logical()}}: Any numeric type can be converted to 
\code{\link[=logical]{logical()}} in addition
diff --git a/r/tests/testthat/test-extension.R 
b/r/tests/testthat/test-extension.R
index b57c24f1..c2fdba1a 100644
--- a/r/tests/testthat/test-extension.R
+++ b/r/tests/testthat/test-extension.R
@@ -126,6 +126,28 @@ test_that("as_nanoarrow_array() dispatches on registered 
extension spec", {
   )
 })
 
+test_that("inferring the type of an unregistered extension warns", {
+  unknown_extension <- na_extension(na_int32(), "definitely not registered")
+  expect_warning(
+    infer_nanoarrow_ptype(unknown_extension),
+    "Converting unknown extension"
+  )
+
+  # Check that warning contains a field name if present
+  struct_with_unknown_ext <- na_struct(list(some_col = unknown_extension))
+  expect_warning(
+    infer_nanoarrow_ptype(struct_with_unknown_ext),
+    "some_col: Converting unknown extension"
+  )
+
+  previous_opts <- options(nanoarrow.warn_unregistered_extension = FALSE)
+  on.exit(options(previous_opts))
+  expect_warning(
+    infer_nanoarrow_ptype(unknown_extension),
+    NA
+  )
+})
+
 test_that("extensions can infer a schema of a nanoarrow_vctr() subclass", {
   register_nanoarrow_extension(
     "some_ext",

Reply via email to