Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions r/src/extension-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ bool RExtensionType::ExtensionEquals(const arrow::ExtensionType& other) const {
}

// With any ambiguity, we need to materialize the R6 instance and call its
// ExtensionEquals method. We can't do this on the non-R thread.
// ExtensionEquals method. This is only possible from the R thread or from
// inside RunWithCapturedR().
arrow::Result<bool> result = SafeCallIntoR<bool>(
[&]() {
cpp11::environment instance = r6_instance();
Expand All @@ -52,8 +53,14 @@ bool RExtensionType::ExtensionEquals(const arrow::ExtensionType& other) const {
},
"RExtensionType$ExtensionEquals()");

// An error here means R could not be called from this thread (e.g., when a
// Dataset is scanned on worker threads). Throwing would terminate the process,
// because this is not the R thread (GH-55605). Fall back to the default
// ExtensionEquals() behaviour instead: the serialized metadata differs, so the
// types are not equal. If the R method itself errored, RunWithCapturedR()
// re-raises that error.
if (!result.ok()) {
throw std::runtime_error(result.status().message());
return false;
}

return result.ValueUnsafe();
Expand Down
26 changes: 26 additions & 0 deletions r/tests/testthat/test-extension.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,32 @@ test_that("extension subclasses can override the ExtensionEquals method", {
unregister_extension_type("some_extension_subclass")
})

test_that("comparing extension types on a non-R thread does not abort (GH-55605)", {
skip_if_not_available("dataset")

unit_type <- function(unit) {
new_extension_type(int32(), "arrow.test.unit", charToRaw(unit))
}
register_extension_type(unit_type("m"))
on.exit(unregister_extension_type("arrow.test.unit"))

tf <- tempfile()
dir.create(tf)
on.exit(unlink(tf, recursive = TRUE), add = TRUE)

for (unit in c("m", "km")) {
write_ipc_file(
arrow_table(x = unit_type(unit)$WrapArray(Array$create(1:3))),
file.path(tf, paste0(unit, ".arrow"))
)
}
ds <- open_dataset(tf, format = "arrow")

# The parts have different extension metadata, so they can't be combined
expect_error(dplyr::collect(ds), "not permitted")
expect_error(Scanner$create(ds)$ToTable(), "not permitted")
})

test_that("vctrs extension type works", {
custom_vctr <- vctrs::new_vctr(
1:4,
Expand Down
Loading