You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ro...@apache.org on 2019/06/12 10:39:55 UTC

[arrow] branch master updated: ARROW-3794 [R]: Consider mapping INT8 to integer() not raw()

This is an automated email from the ASF dual-hosted git repository.

romainfrancois 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 365ec5b  ARROW-3794 [R]: Consider mapping INT8 to integer() not raw()
365ec5b is described below

commit 365ec5b0bd8c27f4b1d80d8d34071a8a9b62179b
Author: Romain Francois <ro...@rstudio.com>
AuthorDate: Wed Jun 12 12:39:37 2019 +0200

    ARROW-3794 [R]: Consider mapping INT8 to integer() not raw()
    
    R vectors of raw types are converted to arrays of type uint8
    
    ``` r
    arrow::array(as.raw(1:10))$type
    #> arrow::UInt8
    #> uint8
    ```
    
    but otoh, arrays of type uint8 are converted to R integer vectors.
    
    ``` r
    library(arrow, warn.conflicts = FALSE)
    
    a <- array((-128):127)$cast(int8())
    str(a$as_vector())
    #>  int [1:256] -128 -127 -126 -125 -124 -123 -122 -121 -120 -119 ...
    
    a <- array(0:255)$cast(uint8())
    str(a$as_vector())
    #>  int [1:256] 0 1 2 3 4 5 6 7 8 9 ...
    ```
    
    <sup>Created on 2019-06-10 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0.9000)</sup>
    
    Author: Romain Francois <ro...@rstudio.com>
    
    Closes #4507 from romainfrancois/ARROW-3794/INT8 and squashes the following commits:
    
    19ff70b1 <Romain Francois> not echo line number in arrowExports.cpp, as this would create some unnecessary changes
    524bcb06 <Romain Francois> better tests
    68a4aa2b <Romain Francois> raw R vectors are converted to uint8 arrays uint8 arrays are converted to integer vectors
---
 r/src/array__to_vector.cpp         |  7 ++++---
 r/src/array_from_vector.cpp        |  2 +-
 r/tests/testthat/test-Array.R      | 13 +++++++++++++
 r/tests/testthat/test-read-write.R | 11 ++++++++---
 4 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/r/src/array__to_vector.cpp b/r/src/array__to_vector.cpp
index 65670e8..17d0600 100644
--- a/r/src/array__to_vector.cpp
+++ b/r/src/array__to_vector.cpp
@@ -530,9 +530,6 @@ class Converter_Int64 : public Converter {
 std::shared_ptr<Converter> Converter::Make(const ArrayVector& arrays) {
   switch (arrays[0]->type_id()) {
     // direct support
-    case Type::INT8:
-      return std::make_shared<arrow::r::Converter_SimpleArray<RAWSXP>>(arrays);
-
     case Type::INT32:
       return std::make_shared<arrow::r::Converter_SimpleArray<INTSXP>>(arrays);
 
@@ -557,6 +554,10 @@ std::shared_ptr<Converter> Converter::Make(const ArrayVector& arrays) {
       return std::make_shared<arrow::r::Converter_Date64>(arrays);
 
       // promotions to integer vector
+    case Type::INT8:
+      return std::make_shared<arrow::r::Converter_Promotion<INTSXP, arrow::Int8Type>>(
+          arrays);
+
     case Type::UINT8:
       return std::make_shared<arrow::r::Converter_Promotion<INTSXP, arrow::UInt8Type>>(
           arrays);
diff --git a/r/src/array_from_vector.cpp b/r/src/array_from_vector.cpp
index 98e59ff..0c21e94 100644
--- a/r/src/array_from_vector.cpp
+++ b/r/src/array_from_vector.cpp
@@ -897,7 +897,7 @@ std::shared_ptr<arrow::Array> Array__from_vector_reuse_memory(SEXP x) {
       }
       return MakeSimpleArray<REALSXP, DoubleType>(x);
     case RAWSXP:
-      return MakeSimpleArray<RAWSXP, Int8Type>(x);
+      return MakeSimpleArray<RAWSXP, UInt8Type>(x);
     default:
       break;
   }
diff --git a/r/tests/testthat/test-Array.R b/r/tests/testthat/test-Array.R
index b15b4bd..68fd9dc 100644
--- a/r/tests/testthat/test-Array.R
+++ b/r/tests/testthat/test-Array.R
@@ -397,3 +397,16 @@ test_that("array() does not convert doubles to integer", {
   }
 })
 
+test_that("array() converts raw vectors to uint8 arrays (ARROW-3794)", {
+  expect_equal(array(as.raw(1:10))$type, uint8())
+})
+
+test_that("Array<int8>$as_vector() converts to integer (ARROW-3794)", {
+  a <- array((-128):127)$cast(int8())
+  expect_equal(a$type, int8())
+  expect_equal(a$as_vector(), (-128):127)
+
+  a <- array(0:255)$cast(uint8())
+  expect_equal(a$type, uint8())
+  expect_equal(a$as_vector(), 0:255)
+})
diff --git a/r/tests/testthat/test-read-write.R b/r/tests/testthat/test-read-write.R
index c56a7d3..fb442a2 100644
--- a/r/tests/testthat/test-read-write.R
+++ b/r/tests/testthat/test-read-write.R
@@ -76,7 +76,7 @@ test_that("arrow::table round trip", {
   chunked_array_raw <- col_raw$data()
   expect_equal(chunked_array_raw$length(), 10L)
   expect_equal(chunked_array_raw$null_count, 0L)
-  expect_equal(chunked_array_raw$as_vector(), tbl$raw)
+  expect_equal(chunked_array_raw$as_vector(), as.integer(tbl$raw))
 
   # arrow::Array
   chunks_raw <- chunked_array_raw$chunks
@@ -88,7 +88,9 @@ test_that("arrow::table round trip", {
   write_arrow(tbl, tf)
 
   res <- read_arrow(tf)
-  expect_identical(tbl, res)
+  expect_identical(tbl$int, res$int)
+  expect_identical(tbl$dbl, res$dbl)
+  expect_identical(as.integer(tbl$raw), res$raw)
   unlink(tf)
 })
 
@@ -119,7 +121,10 @@ test_that("arrow::table round trip handles NA in integer and numeric", {
   write_arrow(tbl, tf)
 
   res <- read_arrow(tf)
-  expect_identical(tbl, res)
+  expect_identical(tbl$int, res$int)
+  expect_identical(tbl$dbl, res$dbl)
+  expect_identical(as.integer(tbl$raw), res$raw)
+
   expect_true(is.na(res$int[1]))
   expect_true(is.na(res$dbl[6]))
   expect_true(is.na(res$dbl[10]))