You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by pa...@apache.org on 2023/01/15 03:08:16 UTC
[arrow] branch master updated: GH-27494: [R] Implement RPrimitiveConverter for Decimal type (#15211)
This is an automated email from the ASF dual-hosted git repository.
paleolimbot pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 19e459f2ba GH-27494: [R] Implement RPrimitiveConverter for Decimal type (#15211)
19e459f2ba is described below
commit 19e459f2ba91b6e1416936e98251bccfec5fc92b
Author: Dewey Dunnington <de...@fishandwhistle.net>
AuthorDate: Sat Jan 14 23:08:10 2023 -0400
GH-27494: [R] Implement RPrimitiveConverter for Decimal type (#15211)
Implements conversion from R to Decimal128/256 in such a way that it works with `Array$create()`.
Before this PR:
``` r
library(arrow, warn.conflicts = FALSE)
#> Some features are not enabled in this build of Arrow. Run `arrow_info()` for more information.
Array$create(1)$cast(decimal(10, 2))
#> Array
#> <decimal128(10, 2)>
#> [
#> 1.00
#> ]
Array$create(1, type = decimal128(10, 2))
#> Error in `value[[3L]]()`:
#> ! NotImplemented: Extend
#> ℹ You might want to try casting manually with `Array$create(...)$cast(...)`.
#> Backtrace:
#> ▆
#> 1. └─Array$create(1, type = decimal128(10, 2))
#> 2. └─base::tryCatch(...) at r/R/array.R:198:2
#> 3. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 4. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 5. └─value[[3L]](cond)
#> 6. └─rlang::abort(...) at r/R/array.R:202:6
Array$create(1, type = decimal256(10, 2))
#> Error in `value[[3L]]()`:
#> ! NotImplemented: Extend
#> ℹ You might want to try casting manually with `Array$create(...)$cast(...)`.
#> Backtrace:
#> ▆
#> 1. └─Array$create(1, type = decimal256(10, 2))
#> 2. └─base::tryCatch(...) at r/R/array.R:198:2
#> 3. └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 4. └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 5. └─value[[3L]](cond)
#> 6. └─rlang::abort(...) at r/R/array.R:202:6
```
<sup>Created on 2023-01-05 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>
After this PR:
``` r
library(arrow, warn.conflicts = FALSE)
#> Some features are not enabled in this build of Arrow. Run `arrow_info()` for more information.
Array$create(1)$cast(decimal(10, 2))
#> Array
#> <decimal128(10, 2)>
#> [
#> 1.00
#> ]
Array$create(1, type = decimal128(10, 2))
#> Array
#> <decimal128(10, 2)>
#> [
#> 1.00
#> ]
Array$create(1, type = decimal256(10, 2))
#> Array
#> <decimal256(10, 2)>
#> [
#> 1.00
#> ]
```
<sup>Created on 2023-01-05 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>
TODO: test!
Lead-authored-by: Dewey Dunnington <de...@fishandwhistle.net>
Co-authored-by: Dewey Dunnington <de...@voltrondata.com>
Co-authored-by: Nic Crane <th...@gmail.com>
Co-authored-by: Weston Pace <we...@gmail.com>
Signed-off-by: Dewey Dunnington <de...@fishandwhistle.net>
---
r/src/r_to_arrow.cpp | 43 ++++++++++++++++++++++++++++--
r/tests/testthat/test-Array.R | 61 +++++++++++++++++++++++++++++++++++--------
2 files changed, 91 insertions(+), 13 deletions(-)
diff --git a/r/src/r_to_arrow.cpp b/r/src/r_to_arrow.cpp
index 06b796fea1..89b4ba2e05 100644
--- a/r/src/r_to_arrow.cpp
+++ b/r/src/r_to_arrow.cpp
@@ -727,9 +727,48 @@ class RPrimitiveConverter<T, enable_if_t<is_timestamp_type<T>::value>>
template <typename T>
class RPrimitiveConverter<T, enable_if_t<is_decimal_type<T>::value>>
: public PrimitiveConverter<T, RConverter> {
+ using ValueType = typename arrow::TypeTraits<T>::CType;
+
public:
Status Extend(SEXP x, int64_t size, int64_t offset = 0) override {
- return Status::NotImplemented("Extend");
+ RETURN_NOT_OK(this->Reserve(size - offset));
+ int32_t precision = this->primitive_type_->precision();
+ int32_t scale = this->primitive_type_->scale();
+
+ auto append_value = [this, precision, scale](double value) {
+ ARROW_ASSIGN_OR_RAISE(ValueType converted,
+ ValueType::FromReal(value, precision, scale));
+ this->primitive_builder_->UnsafeAppend(converted);
+ return Status::OK();
+ };
+
+ auto append_null = [this]() {
+ this->primitive_builder_->UnsafeAppendNull();
+ return Status::OK();
+ };
+
+ switch (TYPEOF(x)) {
+ case REALSXP:
+ if (ALTREP(x)) {
+ return VisitVector(RVectorIterator_ALTREP<double>(x, offset), size, append_null,
+ append_value);
+ } else {
+ return VisitVector(RVectorIterator<double>(x, offset), size, append_null,
+ append_value);
+ }
+ break;
+ case INTSXP:
+ if (ALTREP(x)) {
+ return VisitVector(RVectorIterator_ALTREP<int>(x, offset), size, append_null,
+ append_value);
+ } else {
+ return VisitVector(RVectorIterator<int>(x, offset), size, append_null,
+ append_value);
+ }
+ break;
+ default:
+ return Status::NotImplemented("Conversion to decimal from non-integer/double");
+ }
}
};
@@ -1270,7 +1309,7 @@ std::shared_ptr<arrow::ChunkedArray> vec_to_arrow_ChunkedArray(
if (can_convert_native(x) && type->id() != Type::EXTENSION) {
// short circuit if `x` is an altrep vector that shells a chunked Array
auto maybe = altrep::vec_to_arrow_altrep_bypass(x);
- if (maybe.get()) {
+ if (maybe.get() && maybe->type()->Equals(type)) {
return maybe;
}
diff --git a/r/tests/testthat/test-Array.R b/r/tests/testthat/test-Array.R
index 96d0daf716..a5ff0a4e77 100644
--- a/r/tests/testthat/test-Array.R
+++ b/r/tests/testthat/test-Array.R
@@ -884,17 +884,6 @@ test_that("Array$create() should have helpful error", {
expect_error(Array$create(list(lgl, lgl, int)), "Expecting a logical vector")
expect_error(Array$create(list(char, num, char)), "Expecting a character vector")
- # hint at casting if direct fails and casting looks like it might work
- expect_error(
- Array$create(as.double(1:10), type = decimal(4, 2)),
- "You might want to try casting manually"
- )
-
- expect_error(
- Array$create(1:10, type = decimal(12, 2)),
- "You might want to try casting manually"
- )
-
a <- expect_error(Array$create("one", int32()))
b <- expect_error(vec_to_Array("one", int32()))
# the captured conditions (errors) are not identical, but their messages should be
@@ -1336,3 +1325,53 @@ test_that("Array to C-interface", {
delete_arrow_schema(schema_ptr)
delete_arrow_array(array_ptr)
})
+
+test_that("Can convert R integer/double to decimal (ARROW-11631)", {
+ # Check both decimal128 and decimal256
+ decimal128_from_dbl <- Array$create(c(1, NA_real_), type = decimal128(12, 2))
+ decimal256_from_dbl <- Array$create(c(1, NA_real_), type = decimal256(12, 2))
+ decimal128_from_int <- Array$create(c(1L, NA_integer_), type = decimal128(12, 2))
+ decimal256_from_int <- Array$create(c(1L, NA_integer_), type = decimal256(12, 2))
+
+ # Check ALTREP input
+ altrep_dbl <- as.vector(Array$create(c(1, NA_real_)))
+ altrep_int <- as.vector(Array$create(c(1L, NA_integer_)))
+ decimal_from_altrep_dbl <- Array$create(altrep_dbl, type = decimal128(12, 2))
+ decimal_from_altrep_int <- Array$create(altrep_int, type = decimal128(12, 2))
+
+ expect_equal(
+ decimal128_from_dbl,
+ Array$create(c(1, NA))$cast(decimal128(12, 2))
+ )
+
+ expect_equal(
+ decimal256_from_dbl,
+ Array$create(c(1, NA))$cast(decimal256(12, 2))
+ )
+
+ expect_equal(
+ decimal128_from_int,
+ Array$create(c(1, NA))$cast(decimal128(12, 2))
+ )
+
+ expect_equal(
+ decimal256_from_int,
+ Array$create(c(1, NA))$cast(decimal256(12, 2))
+ )
+
+ expect_equal(
+ decimal_from_altrep_dbl,
+ Array$create(c(1, NA))$cast(decimal128(12, 2))
+ )
+
+ expect_equal(
+ decimal_from_altrep_int,
+ Array$create(c(1, NA))$cast(decimal128(12, 2))
+ )
+
+ # Check that other types aren't silently but invalidly converted
+ expect_error(
+ Array$create(complex(), decimal128(12, 2)),
+ "Conversion to decimal from non-integer/double"
+ )
+})