Skip to content

Commit 763781c

Browse files
+ Coerce() skeleton
1 parent fe15fbd commit 763781c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

r/src/altrep.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ struct AltrepArrayPrimitive {
280280
}
281281

282282
static SEXP Unserialize(SEXP /* class_ */, SEXP state) { return state; }
283+
284+
SEXP Coerce(int type) {
285+
// Just let R handle it for now
286+
return NULL;
287+
}
283288
};
284289
template <int sexp_type>
285290
R_altrep_class_t AltrepArrayPrimitive<sexp_type>::class_t;
@@ -344,6 +349,11 @@ SEXP Unserialize(SEXP class_, SEXP state) {
344349
return AltrepClass::Unserialize(class_, state);
345350
}
346351

352+
template <typename AltrepClass>
353+
SEXP Coerce(SEXP alt, int type) {
354+
return AltrepClass(alt).Coerce(type);
355+
}
356+
347357
static std::shared_ptr<arrow::compute::ScalarAggregateOptions> NaRmOptions(
348358
const std::shared_ptr<Array>& array, bool na_rm) {
349359
auto options = std::make_shared<arrow::compute::ScalarAggregateOptions>(
@@ -435,6 +445,7 @@ void InitAltrepMethods(R_altrep_class_t class_t, DllInfo* dll) {
435445
R_set_altrep_Duplicate_method(class_t, Duplicate<AltrepClass>);
436446
R_set_altrep_Serialized_state_method(class_t, Serialized_state<AltrepClass>);
437447
R_set_altrep_Unserialize_method(class_t, Unserialize<AltrepClass>);
448+
R_set_altrep_Coerce_method(class_t, Coerce<AltrepClass>);
438449
}
439450

440451
template <typename AltrepClass>

r/tests/testthat/test-altrep.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,16 @@ test_that("altrep min/max/sum identical to R versions for int", {
192192

193193
test_that("altrep vectors handle serialization", {
194194
ints <- c(1L, 2L, NA_integer_)
195-
dbls <- c(3, 4, NA_real_)
195+
dbls <- c(1, 2, NA_real_)
196196

197197
expect_identical(ints, unserialize(serialize(Array$create(ints)$as_vector(), NULL)))
198198
expect_identical(dbls, unserialize(serialize(Array$create(dbls)$as_vector(), NULL)))
199199
})
200200

201+
test_that("altrep vectors handle coercion", {
202+
ints <- c(1L, 2L, NA_integer_)
203+
dbls <- c(1, 2, NA_real_)
204+
205+
expect_identical(ints, as.integer(Array$create(dbls)$as_vector()))
206+
expect_identical(dbls, as.numeric(Array$create(ints)$as_vector()))
207+
})

0 commit comments

Comments
 (0)