From 459bde8b4bac76d31e70b8fcf45088ef1d99ea8b Mon Sep 17 00:00:00 2001 From: Bryce Mecum Date: Fri, 24 Jan 2025 16:12:54 +0000 Subject: [PATCH] Remove data.table from class attribute Revert "Remove data.table from class attribute" This reverts commit 9827ff12e12fc2df62da0e1bff06a380b7955edc. Use setDT when applying metadata --- r/R/metadata.R | 4 ++++ r/tests/testthat/test-metadata.R | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/r/R/metadata.R b/r/R/metadata.R index 61e412be62450..23a217063453d 100644 --- a/r/R/metadata.R +++ b/r/R/metadata.R @@ -228,6 +228,10 @@ apply_arrow_r_metadata <- function(x, r_metadata) { attr(x, ".group_vars") <- NULL attr(x, ".group_by_drop") <- NULL } + # GH-45300: Prevent warning about "Invalid .internal.selfref..." + if (inherits(x, "data.table") && requireNamespace("data.table", quietly = TRUE)) { + data.table::setDT(x) + } } }, error = function(e) { diff --git a/r/tests/testthat/test-metadata.R b/r/tests/testthat/test-metadata.R index 06aa1535e0a36..7ed5dc948f349 100644 --- a/r/tests/testthat/test-metadata.R +++ b/r/tests/testthat/test-metadata.R @@ -486,3 +486,22 @@ test_that("data.frame class attribute is not saved", { df_arrow <- arrow_table(df) expect_identical(df_arrow$r_metadata, list(attributes = list(foo = "bar"), columns = list(x = NULL))) }) + +test_that("data.tables don't warn when read back in from parquet", { + # See GH-45300 + skip_if_not_installed("data.table") + + tf <- tempfile() + on.exit(unlink(tf)) + + dt_in <- data.table::data.table(x = 1:3) + arrow::write_parquet(dt_in, tf) + dt_out <- read_parquet(tf) + + expect_no_warning({ + dt_out <- dt_out[, z := 4:6] + }) + # testthat will skip this entire test if we don't have another assertion + # so we need this here + expect_equal(ncol(dt_out), 2) +})