nealrichardson commented on a change in pull request #9952:
URL: https://github.com/apache/arrow/pull/9952#discussion_r610747896



##########
File path: r/tests/testthat/test-dplyr.R
##########
@@ -358,7 +358,70 @@ test_that("relocate with selection helpers", {
   )
 })
 
-test_that("explicit type conversions", {
+test_that("explicit type conversions with cast()", {
+  num_int32 <- 12L
+  num_int64 <- bit64::as.integer64(10)
+
+  int_types <- c(int8(), int16(), int32(), int64())
+  uint_types <- c(uint8(), uint16(), uint32(), uint64())
+  float_types <- c(float32(), float64())
+
+  types <- c(
+    int_types,
+    uint_types,
+    float_types,
+    double(), # not actually a type, a base R function but should be alias for 
float64
+    string()
+  )
+
+  for (type in types) {
+    expect_type_equal(
+      {
+        t1 <- Table$create(x = num_int32) %>%
+          transmute(x = cast(x, type)) %>%
+          compute()
+        t1$schema[[1]]$type
+      },
+      as_type(type)
+    )
+    expect_type_equal(
+      {
+        t1 <- Table$create(x = num_int64) %>%
+          transmute(x = cast(x, type)) %>%
+          compute()
+        t1$schema[[1]]$type
+      },
+      as_type(type)
+    )
+  }
+
+  # Arrow errors when truncating floats...
+  expect_error(
+    expect_type_equal(
+      {
+        t1 <- Table$create(pi = pi) %>%
+          transmute(three = cast(pi, int32())) %>%
+          compute()
+        t1$schema[[1]]$type
+      },
+      as_type(int32())

Review comment:
       `int32()` is a type so you don't need `as_type()` do you? (I'm guessing 
this is copypasta from the above tests, where you do need it to handle 
`double()`)
   
   ```suggestion
         int32()
   ```

##########
File path: r/R/type.R
##########
@@ -156,8 +156,8 @@ NestedType <- R6Class("NestedType", inherit = DataType)
 #' * `float16()` and `halffloat()`
 #' * `float32()` and `float()`
 #' * `bool()` and `boolean()`
-#' * Called from `schema()` or `struct()`, `double()` also is supported as a
-#' way of creating a `float64()`
+#' * Called from `schema()` or `struct()` or from `cast()` in a dplyr verb,

Review comment:
       ```suggestion
   #' * When called inside an `arrow` function, such as `schema()` or `cast()`,
   ```

##########
File path: r/R/type.R
##########
@@ -413,16 +413,28 @@ FixedSizeListType <- R6Class("FixedSizeListType",
 fixed_size_list_of <- function(type, list_size) fixed_size_list__(type, 
list_size)
 
 as_type <- function(type, name = "type") {
-  if (identical(type, double())) {
-    # Magic so that we don't have to mask this base function
-    type <- float64()
-  }
+  # work around other packages possibly masking the Arrow data type functions,
+  # for example rlang masking string()
+  type <- unmask_type_fun(enexpr(type)) %||% type
+
+  # magic so we don't have to mask base::double()
+  if (identical(type, double())) type <- float64()

Review comment:
       ```suggestion
     if (identical(type, double())) {
       type <- float64()
     }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to