From bef36bb634b1c2d307960d3d8320f58cf65115d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drago=C8=99=20Moldovan-Gr=C3=BCnfeld?= Date: Fri, 4 Mar 2022 13:48:26 +0000 Subject: [PATCH 1/2] replaced `Expression$create()` with `build_expr()` --- r/R/dplyr-funcs-type.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/r/R/dplyr-funcs-type.R b/r/R/dplyr-funcs-type.R index fa839269abe..7fd3a7c63c9 100644 --- a/r/R/dplyr-funcs-type.R +++ b/r/R/dplyr-funcs-type.R @@ -43,13 +43,13 @@ register_bindings_type_cast <- function() { # as.* type casting functions # as.factor() is mapped in expression.R register_binding("as.character", function(x) { - Expression$create("cast", x, options = cast_options(to_type = string())) + build_expr("cast", x, options = cast_options(to_type = string())) }) register_binding("as.double", function(x) { - Expression$create("cast", x, options = cast_options(to_type = float64())) + build_expr("cast", x, options = cast_options(to_type = float64())) }) register_binding("as.integer", function(x) { - Expression$create( + build_expr( "cast", x, options = cast_options( @@ -60,7 +60,7 @@ register_bindings_type_cast <- function() { ) }) register_binding("as.integer64", function(x) { - Expression$create( + build_expr( "cast", x, options = cast_options( @@ -71,10 +71,10 @@ register_bindings_type_cast <- function() { ) }) register_binding("as.logical", function(x) { - Expression$create("cast", x, options = cast_options(to_type = boolean())) + build_expr("cast", x, options = cast_options(to_type = boolean())) }) register_binding("as.numeric", function(x) { - Expression$create("cast", x, options = cast_options(to_type = float64())) + build_expr("cast", x, options = cast_options(to_type = float64())) }) register_binding("as.Date", function(x, format = NULL, From 9a3a3e1e56fa0c02061066957bb1898cc1818aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Drago=C8=99=20Moldovan-Gr=C3=BCnfeld?= Date: Sat, 5 Mar 2022 13:37:17 +0000 Subject: [PATCH 2/2] addintional tests for coercing R objects directly --- r/tests/testthat/test-dplyr-funcs-type.R | 39 +++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/r/tests/testthat/test-dplyr-funcs-type.R b/r/tests/testthat/test-dplyr-funcs-type.R index 18efc5326dc..b1a5212623c 100644 --- a/r/tests/testthat/test-dplyr-funcs-type.R +++ b/r/tests/testthat/test-dplyr-funcs-type.R @@ -100,6 +100,14 @@ test_that("explicit type conversions with as.*()", { dbl2dbl = as.double(dbl), dbl2int = as.integer(dbl), dbl2num = as.numeric(dbl), + rint2chr = as.character(1L), + rint2dbl = as.double(1), + rint2int = as.integer(1L), + rint2num = as.numeric(1.5), + rdbl2chr = as.character(1.5), + rdbl2dbl = as.double(1.5), + rdbl2int = as.integer(1.5), + rdbl2num = as.numeric(1.5) ) %>% collect(), tbl @@ -110,7 +118,11 @@ test_that("explicit type conversions with as.*()", { chr2chr = as.character(chr), chr2dbl = as.double(chr), chr2int = as.integer(chr), - chr2num = as.numeric(chr) + chr2num = as.numeric(chr), + rchr2chr = as.character("string"), + rchr2dbl = as.double("1.5"), + rchr2int = as.integer("1"), + rchr2num = as.numeric("1.5") ) %>% collect(), tibble(chr = c("1", "2", "3")) @@ -121,6 +133,9 @@ test_that("explicit type conversions with as.*()", { chr2i64 = as.integer64(chr), dbl2i64 = as.integer64(dbl), i642i64 = as.integer64(i64), + rchr2i64 = as.integer64("10000000000"), + rdbl2i64 = as.integer64(10000000000), + ri642i64 = as.integer64(as.integer64(1e10)) ) %>% collect(), tibble(chr = "10000000000", dbl = 10000000000, i64 = as.integer64(1e10)) @@ -130,7 +145,10 @@ test_that("explicit type conversions with as.*()", { transmute( chr2lgl = as.logical(chr), dbl2lgl = as.logical(dbl), - int2lgl = as.logical(int) + int2lgl = as.logical(int), + rchr2lgl = as.logical("TRUE"), + rdbl2lgl = as.logical(0), + rint2lgl = as.logical(1L) ) %>% collect(), tibble( @@ -153,11 +171,24 @@ test_that("explicit type conversions with as.*()", { lgl2chr = as.character(lgl), # Arrow returns "true", "false" here ... lgl2dbl = as.double(lgl), lgl2int = as.integer(lgl), - lgl2lgl = as.logical(lgl) + lgl2lgl = as.logical(lgl), + rdbl2chr = as.character(1.5), + rdbl2dbl = as.double(1.5), + rdbl2int = as.integer(1.5), + rdbl2lgl = as.logical(1), + rint2chr = as.character(1L), + rint2dbl = as.double(1L), + rint2int = as.integer(1L), + rint2lgl = as.logical(1L), + rlgl2chr = as.character(TRUE), # Arrow returns "true", "false" here ... + rlgl2dbl = as.double(FALSE), + rlgl2int = as.integer(NA), + rlgl2lgl = as.logical(FALSE) ) %>% collect() %>% # need to use toupper() *after* collect() or else skip if utf8proc not available - mutate(lgl2chr = toupper(lgl2chr)), # ... but we need "TRUE", "FALSE" + mutate(lgl2chr = toupper(lgl2chr), + rlgl2chr = toupper(rlgl2chr)), # ... but we need "TRUE", "FALSE" tibble( dbl = c(1, 0, NA_real_), int = c(1L, 0L, NA_integer_),