Skip to content

Commit 44e8eb9

Browse files
authored
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>
1 parent 503548b commit 44e8eb9

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

r/R/convert-array.R

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#' dispatching on `to`: developers may implement their own S3 methods for
2525
#' custom vector types.
2626
#'
27+
#' Note that unregistered extension types will by default issue a warning.
28+
#' Use `options(nanoarrow.warn_unregistered_extension = FALSE)` to disable
29+
#' this behaviour.
30+
#'
2731
#' @param array A [nanoarrow_array][as_nanoarrow_array].
2832
#' @param to A target prototype object describing the type to which `array`
2933
#' should be converted, or `NULL` to use the default conversion as

r/R/extension.R

+19-14
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,28 @@ nanoarrow_extension_array <- function(storage_array, extension_name,
164164
}
165165

166166
warn_unregistered_extension_type <- function(x) {
167+
# Allow an opt-out of this warning for consumers that don't have
168+
# control over their source and want to drop unknown extensions
169+
if (!getOption("nanoarrow.warn_unregistered_extension", TRUE)) {
170+
return()
171+
}
172+
167173
# Warn that we're about to ignore an extension type
174+
message <- sprintf(
175+
paste0(
176+
"Converting unknown extension %s as storage type\n",
177+
"Disable warning with ",
178+
"options(nanoarrow.warn_unregistered_extension = FALSE)"
179+
),
180+
nanoarrow_schema_formatted(x)
181+
)
182+
183+
# Add the field name if we know it
168184
if (!is.null(x$name) && !identical(x$name, "")) {
169-
warning(
170-
sprintf(
171-
"%s: Converting unknown extension %s as storage type",
172-
x$name,
173-
nanoarrow_schema_formatted(x)
174-
)
175-
)
176-
} else {
177-
warning(
178-
sprintf(
179-
"Converting unknown extension %s as storage type",
180-
nanoarrow_schema_formatted(x)
181-
)
182-
)
185+
message <- paste0(x$name, ": ", message)
183186
}
187+
188+
warning(message)
184189
}
185190

186191
# Mutable registry to look up extension specifications

r/man/convert_array.Rd

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

r/tests/testthat/test-extension.R

+22
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,28 @@ test_that("as_nanoarrow_array() dispatches on registered extension spec", {
126126
)
127127
})
128128

129+
test_that("inferring the type of an unregistered extension warns", {
130+
unknown_extension <- na_extension(na_int32(), "definitely not registered")
131+
expect_warning(
132+
infer_nanoarrow_ptype(unknown_extension),
133+
"Converting unknown extension"
134+
)
135+
136+
# Check that warning contains a field name if present
137+
struct_with_unknown_ext <- na_struct(list(some_col = unknown_extension))
138+
expect_warning(
139+
infer_nanoarrow_ptype(struct_with_unknown_ext),
140+
"some_col: Converting unknown extension"
141+
)
142+
143+
previous_opts <- options(nanoarrow.warn_unregistered_extension = FALSE)
144+
on.exit(options(previous_opts))
145+
expect_warning(
146+
infer_nanoarrow_ptype(unknown_extension),
147+
NA
148+
)
149+
})
150+
129151
test_that("extensions can infer a schema of a nanoarrow_vctr() subclass", {
130152
register_nanoarrow_extension(
131153
"some_ext",

0 commit comments

Comments
 (0)