paleolimbot commented on pull request #11652:
URL: https://github.com/apache/arrow/pull/11652#issuecomment-967345651


   I think I found it! What's happening is this:
   
   ``` r
   arrow:::eval_array_expression("/", 1L, 0L)$cast(arrow::int32(), 
allow_float_truncate = TRUE)
   #> Scalar
   #> 2147483647
   ```
   
   Without `allow_float_truncate = TRUE` we get:
   
   ``` r
   arrow:::eval_array_expression("/", 1L, 0L)$cast(arrow::int32())
   #> Error: Invalid: Float value inf was truncated converting to int32
   #> 
/Users/deweydunnington/Desktop/rscratch/arrow/cpp/src/arrow/compute/kernels/scalar_cast_numeric.cc:177
  CheckFloatToIntTruncation(batch[0], *out)
   #> 
/Users/deweydunnington/Desktop/rscratch/arrow/cpp/src/arrow/compute/exec.cc:703 
 kernel_->exec(kernel_ctx_, batch, &out)
   #> 
/Users/deweydunnington/Desktop/rscratch/arrow/cpp/src/arrow/compute/exec.cc:641 
 ExecuteBatch(batch, listener)
   #> 
/Users/deweydunnington/Desktop/rscratch/arrow/cpp/src/arrow/compute/function.cc:239
  executor->Execute(implicitly_cast_args, &listener)
   ```
   
   With the latest commit this feels much better (using your if_else 
suggestion!):
   
   ``` r
   # remotes::install_github("apache/arrow/r#11652")
   library(arrow, warn.conflicts = FALSE)
   library(dplyr, warn.conflicts = FALSE)
   
   tbl <- tibble::tibble(
     integers = c(1:4, NA_integer_),
     doubles = c(as.numeric(1:4), NA_real_)
   )
   
   RecordBatch$create(!!! tbl) %>% 
     mutate(
       int_div_dbl = integers %/% 2,
       int_div_int = integers %/% 2L,
       int_div_zero_int = integers %/% 0L,
       int_div_zero_dbl = integers %/% 0,
       dbl_div_dbl = doubles %/% 2,
       dbl_div_int = doubles %/% 2L,
       dbl_div_zero_int = doubles %/% 0L,
       dbl_div_zero_dbl = doubles %/% 0
     ) %>% 
     collect() %>% 
     glimpse()
   #> Rows: 5
   #> Columns: 10
   #> $ integers         <int> 1, 2, 3, 4, NA
   #> $ doubles          <dbl> 1, 2, 3, 4, NA
   #> $ int_div_dbl      <dbl> 0, 1, 1, 2, NA
   #> $ int_div_int      <int> 0, 1, 1, 2, NA
   #> $ int_div_zero_int <int> NA, NA, NA, NA, NA
   #> $ int_div_zero_dbl <dbl> Inf, Inf, Inf, Inf, NA
   #> $ dbl_div_dbl      <dbl> 0, 1, 1, 2, NA
   #> $ dbl_div_int      <dbl> 0, 1, 1, 2, NA
   #> $ dbl_div_zero_int <dbl> Inf, Inf, Inf, Inf, NA
   #> $ dbl_div_zero_dbl <dbl> Inf, Inf, Inf, Inf, NA
   ```
   
   <sup>Created on 2021-11-12 by the [reprex 
package](https://reprex.tidyverse.org) (v2.0.1)</sup>


-- 
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.

To unsubscribe, e-mail: [email protected]

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


Reply via email to