diff --git a/r/NAMESPACE b/r/NAMESPACE index eb053a26ea4f1..5aac994770a53 100644 --- a/r/NAMESPACE +++ b/r/NAMESPACE @@ -86,6 +86,7 @@ S3method(write_feather,default) S3method(write_feather_RecordBatch,"arrow::io::OutputStream") S3method(write_feather_RecordBatch,character) S3method(write_feather_RecordBatch,default) +export(Array) export(BufferOutputStream) export(BufferReader) export(CompressedInputStream) @@ -108,7 +109,6 @@ export(RecordBatchStreamWriter) export(StatusCode) export(TimeUnit) export(Type) -export(array) export(arrow_available) export(bool) export(boolean) diff --git a/r/R/ArrayData.R b/r/R/ArrayData.R index adf7c113c3004..3849b8928e25d 100644 --- a/r/R/ArrayData.R +++ b/r/R/ArrayData.R @@ -26,7 +26,7 @@ #' @section Usage: #' #' ``` -#' data <- array(x)$data() +#' data <- Array$create(x)$data() #' #' data$type() #' data$length() @@ -39,8 +39,8 @@ #' #' ... #' -#' @rdname arrow__ArrayData -#' @name arrow__ArrayData +#' @rdname ArrayData +#' @name ArrayData `ArrayData` <- R6Class("ArrayData", inherit = `arrow::Object`, active = list( diff --git a/r/R/ChunkedArray.R b/r/R/ChunkedArray.R index 4ce2a8416134d..b2f4bd7617723 100644 --- a/r/R/ChunkedArray.R +++ b/r/R/ChunkedArray.R @@ -32,7 +32,7 @@ `arrow::ChunkedArray` <- R6Class("arrow::ChunkedArray", inherit = `arrow::Object`, public = list( length = function() ChunkedArray__length(self), - chunk = function(i) `Array`$dispatch(ChunkedArray__chunk(self, i)), + chunk = function(i) Array$create(ChunkedArray__chunk(self, i)), as_vector = function() ChunkedArray__as_vector(self), Slice = function(offset, length = NULL){ if (is.null(length)) { @@ -50,7 +50,7 @@ active = list( null_count = function() ChunkedArray__null_count(self), num_chunks = function() ChunkedArray__num_chunks(self), - chunks = function() map(ChunkedArray__chunks(self), ~ `Array`$dispatch(.x)), + chunks = function() map(ChunkedArray__chunks(self), ~ Array$create(.x)), type = function() `arrow::DataType`$dispatch(ChunkedArray__type(self)) ) ) diff --git a/r/R/array.R b/r/R/array.R index 5c0b86c38770a..70fdca509233d 100644 --- a/r/R/array.R +++ b/r/R/array.R @@ -28,7 +28,7 @@ #' @section Usage: #' #' ``` -#' a <- array(x) +#' a <- Array$create(x) #' #' a$IsNull(i) #' a$IsValid(i) @@ -59,15 +59,16 @@ #' - `$type_id()`: type id #' - `$Equals(other)` : is this array equal to `other` #' - `$ApproxEquals(other)` : -#' - `$data()`: return the underlying [ArrayData][arrow__ArrayData] +#' - `$data()`: return the underlying [ArrayData][ArrayData] #' - `$as_vector()`: convert to an R vector #' - `$ToString()`: string representation of the array #' - `$Slice(offset, length = NULL)` : Construct a zero-copy slice of the array with the indicated offset and length. If length is `NULL`, the slice goes until the end of the array. #' - `$RangeEquals(other, start_idx, end_idx, other_start_idx)` : #' -#' @rdname arrow__Array -#' @name arrow__Array -`Array` <- R6Class("Array", +#' @rdname Array +#' @name Array +#' @export +Array <- R6Class("Array", inherit = `arrow::Object`, public = list( IsNull = function(i) Array__IsNull(self, i), @@ -93,7 +94,7 @@ cast = function(target_type, safe = TRUE, options = cast_options(safe)) { assert_that(inherits(target_type, "arrow::DataType")) assert_that(inherits(options, "arrow::compute::CastOptions")) - `Array`$dispatch(Array__cast(self, target_type, options)) + Array$create(Array__cast(self, target_type, options)) } ), active = list( @@ -105,22 +106,22 @@ `arrow::DictionaryArray` <- R6Class("arrow::DictionaryArray", inherit = `Array`, public = list( - indices = function() `Array`$dispatch(DictionaryArray__indices(self)), - dictionary = function() `Array`$dispatch(DictionaryArray__dictionary(self)) + indices = function() Array$create(DictionaryArray__indices(self)), + dictionary = function() Array$create(DictionaryArray__dictionary(self)) ) ) `arrow::StructArray` <- R6Class("arrow::StructArray", inherit = `Array`, public = list( - field = function(i) `Array`$dispatch(StructArray__field(self, i)), - GetFieldByName = function(name) `Array`$dispatch(StructArray__GetFieldByName(self, name)), - Flatten = function() map(StructArray__Flatten(self), ~ `Array`$dispatch(.x)) + field = function(i) Array$create(StructArray__field(self, i)), + GetFieldByName = function(name) Array$create(StructArray__GetFieldByName(self, name)), + Flatten = function() map(StructArray__Flatten(self), ~ Array$create(.x)) ) ) `arrow::ListArray` <- R6Class("arrow::ListArray", inherit = `Array`, public = list( - values = function() `Array`$dispatch(ListArray__values(self)), + values = function() Array$create(ListArray__values(self)), value_length = function(i) ListArray__value_length(self, i), value_offset = function(i) ListArray__value_offset(self, i), raw_value_offsets = function() ListArray__raw_value_offsets(self) @@ -130,30 +131,24 @@ ) ) -`Array`$dispatch <- function(xp){ - a <- shared_ptr(`Array`, xp) +# Add a class method +Array$create <- function(x, type = NULL) { + if (!inherits(x, "externalptr")) { + x <- Array__from_vector(x, type) + } + a <- shared_ptr(Array, x) if (a$type_id() == Type$DICTIONARY){ - a <- shared_ptr(`arrow::DictionaryArray`, xp) + a <- shared_ptr(`arrow::DictionaryArray`, x) } else if (a$type_id() == Type$STRUCT) { - a <- shared_ptr(`arrow::StructArray`, xp) + a <- shared_ptr(`arrow::StructArray`, x) } else if (a$type_id() == Type$LIST) { - a <- shared_ptr(`arrow::ListArray`, xp) + a <- shared_ptr(`arrow::ListArray`, x) } a } #' @export -`length.Array` <- function(x) x$length() +length.Array <- function(x) x$length() #' @export `==.Array` <- function(x, y) x$Equals(y) - -#' create an [Array][arrow__Array] from an R vector -#' -#' @param x R object -#' @param type Explicit [type][arrow__DataType], or NULL (the default) to infer from the data -#' -#' @export -array <- function(x, type = NULL){ - `Array`$dispatch(Array__from_vector(x, type)) -} diff --git a/r/man/arrow__ArrayData.Rd b/r/man/ArrayData.Rd similarity index 78% rename from r/man/arrow__ArrayData.Rd rename to r/man/ArrayData.Rd index bd4e2363c9a44..2cca881da44f9 100644 --- a/r/man/arrow__ArrayData.Rd +++ b/r/man/ArrayData.Rd @@ -1,15 +1,14 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/ArrayData.R \docType{class} -\name{arrow__ArrayData} -\alias{arrow__ArrayData} +\name{ArrayData} \alias{ArrayData} \title{class ArrayData} \description{ class ArrayData } \section{Usage}{ -\preformatted{data <- array(x)$data() +\preformatted{data <- Array$create(x)$data() data$type() data$length() diff --git a/r/man/array.Rd b/r/man/array.Rd index 5920a3d83c935..8dd07a9bb742d 100644 --- a/r/man/array.Rd +++ b/r/man/array.Rd @@ -1,16 +1,56 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/array.R -\name{array} -\alias{array} -\title{create an \link[=arrow__Array]{Array} from an R vector} -\usage{ -array(x, type = NULL) +\docType{class} +\name{Array} +\alias{Array} +\title{class Array + +Array base type. Immutable data array with some logical type and some length.} +\description{ +class Array + +Array base type. Immutable data array with some logical type and some length. } -\arguments{ -\item{x}{R object} +\section{Usage}{ +\preformatted{a <- Array$create(x) -\item{type}{Explicit \link[=arrow__DataType]{type}, or NULL (the default) to infer from the data} +a$IsNull(i) +a$IsValid(i) +a$length() or length(a) +a$offset() +a$null_count() +a$type() +a$type_id() +a$Equals(b) +a$ApproxEquals(b) +a$as_vector() +a$ToString() +a$Slice(offset, length = NULL) +a$RangeEquals(other, start_idx, end_idx, other_start_idx) + +print(a) +a == a } -\description{ -create an \link[=arrow__Array]{Array} from an R vector } + +\section{Methods}{ + +\itemize{ +\item \code{$IsNull(i)}: Return true if value at index is null. Does not boundscheck +\item \code{$IsValid(i)}: Return true if value at index is valid. Does not boundscheck +\item \code{$length()}: Size in the number of elements this array contains +\item \code{$offset()}: A relative position into another array's data, to enable zero-copy slicing +\item \code{$null_count()}: The number of null entries in the array +\item \code{$type()}: logical type of data +\item \code{$type_id()}: type id +\item \code{$Equals(other)} : is this array equal to \code{other} +\item \code{$ApproxEquals(other)} : +\item \code{$data()}: return the underlying \link{ArrayData} +\item \code{$as_vector()}: convert to an R vector +\item \code{$ToString()}: string representation of the array +\item \code{$Slice(offset, length = NULL)} : Construct a zero-copy slice of the array with the indicated offset and length. If length is \code{NULL}, the slice goes until the end of the array. +\item \code{$RangeEquals(other, start_idx, end_idx, other_start_idx)} : +} +} + +\keyword{datasets} diff --git a/r/man/arrow__Array.Rd b/r/man/arrow__Array.Rd deleted file mode 100644 index 940524907f6b9..0000000000000 --- a/r/man/arrow__Array.Rd +++ /dev/null @@ -1,57 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/array.R -\docType{class} -\name{arrow__Array} -\alias{arrow__Array} -\alias{Array} -\title{class Array - -Array base type. Immutable data array with some logical type and some length.} -\description{ -class Array - -Array base type. Immutable data array with some logical type and some length. -} -\section{Usage}{ -\preformatted{a <- array(x) - -a$IsNull(i) -a$IsValid(i) -a$length() or length(a) -a$offset() -a$null_count() -a$type() -a$type_id() -a$Equals(b) -a$ApproxEquals(b) -a$as_vector() -a$ToString() -a$Slice(offset, length = NULL) -a$RangeEquals(other, start_idx, end_idx, other_start_idx) - -print(a) -a == a -} -} - -\section{Methods}{ - -\itemize{ -\item \code{$IsNull(i)}: Return true if value at index is null. Does not boundscheck -\item \code{$IsValid(i)}: Return true if value at index is valid. Does not boundscheck -\item \code{$length()}: Size in the number of elements this array contains -\item \code{$offset()}: A relative position into another array's data, to enable zero-copy slicing -\item \code{$null_count()}: The number of null entries in the array -\item \code{$type()}: logical type of data -\item \code{$type_id()}: type id -\item \code{$Equals(other)} : is this array equal to \code{other} -\item \code{$ApproxEquals(other)} : -\item \code{$data()}: return the underlying \link[=arrow__ArrayData]{ArrayData} -\item \code{$as_vector()}: convert to an R vector -\item \code{$ToString()}: string representation of the array -\item \code{$Slice(offset, length = NULL)} : Construct a zero-copy slice of the array with the indicated offset and length. If length is \code{NULL}, the slice goes until the end of the array. -\item \code{$RangeEquals(other, start_idx, end_idx, other_start_idx)} : -} -} - -\keyword{datasets} diff --git a/r/tests/testthat/test-Array.R b/r/tests/testthat/test-Array.R index 4a903fac923c0..b0bec91b2c8ab 100644 --- a/r/tests/testthat/test-Array.R +++ b/r/tests/testthat/test-Array.R @@ -18,14 +18,14 @@ context("arrow::Array") test_that("Array", { - x <- array(c(1:10, 1:10, 1:5)) + x <- Array$create(c(1:10, 1:10, 1:5)) expect_equal(x$type, int32()) - expect_equal(x$length(), 25L) + expect_equal(length(x), 25L) expect_equal(x$as_vector(), c(1:10, 1:10, 1:5)) y <- x$Slice(10) expect_equal(y$type, int32()) - expect_equal(y$length(), 15L) + expect_equal(length(y), 15L) expect_equal(y$as_vector(), c(1:10, 1:5)) expect_true(x$RangeEquals(y, 10, 24, 0)) @@ -35,7 +35,7 @@ test_that("Array", { expect_equal(z$as_vector(), c(1:5)) expect_true(x$RangeEquals(z, 10, 15, 0)) - x_dbl <- array(c(1,2,3,4,5,6)) + x_dbl <- Array$create(c(1,2,3,4,5,6)) expect_equal(x_dbl$type, float64()) expect_equal(x_dbl$length(), 6L) expect_equal(x_dbl$as_vector(), as.numeric(1:6)) @@ -53,8 +53,8 @@ test_that("Array", { }) test_that("Array supports NA", { - x_int <- array(as.integer(c(1:10, NA))) - x_dbl <- array(as.numeric(c(1:10, NA))) + x_int <- Array$create(as.integer(c(1:10, NA))) + x_dbl <- Array$create(as.numeric(c(1:10, NA))) expect_true(x_int$IsValid(0L)) expect_true(x_dbl$IsValid(0L)) expect_true(x_int$IsNull(10L)) @@ -68,19 +68,19 @@ test_that("Array supports NA", { test_that("Array supports logical vectors (ARROW-3341)", { # with NA x <- sample(c(TRUE, FALSE, NA), 1000, replace = TRUE) - arr_lgl <- array(x) + arr_lgl <- Array$create(x) expect_identical(x, arr_lgl$as_vector()) # without NA x <- sample(c(TRUE, FALSE), 1000, replace = TRUE) - arr_lgl <- array(x) + arr_lgl <- Array$create(x) expect_identical(x, arr_lgl$as_vector()) }) test_that("Array supports character vectors (ARROW-3339)", { # with NA x <- c("itsy", NA, "spider") - arr_chr <- array(x) + arr_chr <- Array$create(x) expect_equal(arr_chr$length(), 3L) expect_identical(arr_chr$as_vector(), x) expect_true(arr_chr$IsValid(0)) @@ -92,51 +92,51 @@ test_that("Array supports character vectors (ARROW-3339)", { # without NA x <- c("itsy", "bitsy", "spider") - arr_chr <- array(x) + arr_chr <- Array$create(x) expect_equal(arr_chr$length(), 3L) expect_identical(arr_chr$as_vector(), x) }) test_that("empty arrays are supported", { x <- character() - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- integer() - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- numeric() - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- factor(character()) - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- logical() - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) }) test_that("array with all nulls are supported", { nas <- c(NA, NA) x <- as.logical(nas) - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- as.integer(nas) - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- as.numeric(nas) - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- as.character(nas) - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) x <- as.factor(nas) - expect_equal(array(x)$as_vector(), x) + expect_equal(Array$create(x)$as_vector(), x) }) test_that("Array supports unordered factors (ARROW-3355)", { # without NA f <- factor(c("itsy", "bitsy", "spider", "spider")) - arr_fac <- array(f) + arr_fac <- Array$create(f) expect_equal(arr_fac$length(), 4L) expect_equal(arr_fac$type$index_type, int8()) expect_identical(arr_fac$as_vector(), f) @@ -152,7 +152,7 @@ test_that("Array supports unordered factors (ARROW-3355)", { # with NA f <- factor(c("itsy", "bitsy", NA, "spider", "spider")) - arr_fac <- array(f) + arr_fac <- Array$create(f) expect_equal(arr_fac$length(), 5L) expect_equal(arr_fac$type$index_type, int8()) expect_identical(arr_fac$as_vector(), f) @@ -171,7 +171,7 @@ test_that("Array supports unordered factors (ARROW-3355)", { test_that("Array supports ordered factors (ARROW-3355)", { # without NA f <- ordered(c("itsy", "bitsy", "spider", "spider")) - arr_fac <- array(f) + arr_fac <- Array$create(f) expect_equal(arr_fac$length(), 4L) expect_equal(arr_fac$type$index_type, int8()) expect_identical(arr_fac$as_vector(), f) @@ -187,7 +187,7 @@ test_that("Array supports ordered factors (ARROW-3355)", { # with NA f <- ordered(c("itsy", "bitsy", NA, "spider", "spider")) - arr_fac <- array(f) + arr_fac <- Array$create(f) expect_equal(arr_fac$length(), 5L) expect_equal(arr_fac$type$index_type, int8()) expect_identical(arr_fac$as_vector(), f) @@ -205,20 +205,20 @@ test_that("Array supports ordered factors (ARROW-3355)", { test_that("array supports Date (ARROW-3340)", { d <- Sys.Date() + 1:10 - a <- array(d) + a <- Array$create(d) expect_equal(a$type, date32()) expect_equal(a$length(), 10L) expect_equal(a$as_vector(), d) d[5] <- NA - a <- array(d) + a <- Array$create(d) expect_equal(a$type, date32()) expect_equal(a$length(), 10L) expect_equal(a$as_vector(), d) expect_true(a$IsNull(4)) d2 <- d + .5 - a <- array(d2) + a <- Array$create(d2) expect_equal(a$type, date32()) expect_equal(a$length(), 10L) expect_equal(a$as_vector(), d) @@ -227,14 +227,14 @@ test_that("array supports Date (ARROW-3340)", { test_that("array supports POSIXct (ARROW-3340)", { times <- lubridate::ymd_hms("2018-10-07 19:04:05") + 1:10 - a <- array(times) + a <- Array$create(times) expect_equal(a$type$name, "timestamp") expect_equal(a$type$unit(), unclass(TimeUnit$MICRO)) expect_equal(a$length(), 10L) expect_equal(as.numeric(a$as_vector()), as.numeric(times)) times[5] <- NA - a <- array(times) + a <- Array$create(times) expect_equal(a$type$name, "timestamp") expect_equal(a$type$unit(), unclass(TimeUnit$MICRO)) expect_equal(a$length(), 10L) @@ -244,13 +244,13 @@ test_that("array supports POSIXct (ARROW-3340)", { test_that("array supports integer64", { x <- bit64::as.integer64(1:10) - a <- array(x) + a <- Array$create(x) expect_equal(a$type, int64()) expect_equal(a$length(), 10L) expect_equal(a$as_vector(), x) x[4] <- NA - a <- array(x) + a <- Array$create(x) expect_equal(a$type, int64()) expect_equal(a$length(), 10L) expect_equal(a$as_vector(), x) @@ -259,18 +259,18 @@ test_that("array supports integer64", { test_that("array$as_vector() correctly handles all NA inte64 (ARROW-3795)", { x <- bit64::as.integer64(NA) - a <- array(x) + a <- Array$create(x) expect_true(is.na(a$as_vector())) }) test_that("array supports difftime", { time <- hms::hms(56, 34, 12) - a <- array(c(time, time)) + a <- Array$create(c(time, time)) expect_equal(a$type, time32(unit = TimeUnit$SECOND)) expect_equal(a$length(), 2L) expect_equal(a$as_vector(), c(time, time)) - a <- array(vctrs::vec_c(time, NA)) + a <- Array$create(vctrs::vec_c(time, NA)) expect_equal(a$type, time32(unit = TimeUnit$SECOND)) expect_equal(a$length(), 2L) expect_true(a$IsNull(1)) @@ -280,13 +280,13 @@ test_that("array supports difftime", { test_that("support for NaN (ARROW-3615)", { x <- c(1, NA, NaN, -1) - y <- array(x) + y <- Array$create(x) expect_true(y$IsValid(2)) expect_equal(y$null_count, 1L) }) test_that("integer types casts (ARROW-3741)", { - a <- array(c(1:10, NA)) + a <- Array$create(c(1:10, NA)) a_int8 <- a$cast(int8()) a_int16 <- a$cast(int16()) a_int32 <- a$cast(int32()) @@ -317,7 +317,7 @@ test_that("integer types casts (ARROW-3741)", { }) test_that("integer types cast safety (ARROW-3741, ARROW-5541)", { - a <- array(-(1:10)) + a <- Array$create(-(1:10)) expect_error(a$cast(uint8()), regexp = "Integer value out of bounds") expect_error(a$cast(uint16()), regexp = "Integer value out of bounds") expect_error(a$cast(uint32()), regexp = "Integer value out of bounds") @@ -331,7 +331,7 @@ test_that("integer types cast safety (ARROW-3741, ARROW-5541)", { test_that("float types casts (ARROW-3741)", { x <- c(1, 2, 3, NA) - a <- array(x) + a <- Array$create(x) a_f32 <- a$cast(float32()) a_f64 <- a$cast(float64()) @@ -347,12 +347,12 @@ test_that("float types casts (ARROW-3741)", { test_that("cast to half float works", { skip("until https://issues.apache.org/jira/browse/ARROW-3802") - a <- array(1:4) + a <- Array$create(1:4) a_f16 <- a$cast(float16()) expect_equal(a_16$type, float16()) }) -test_that("array() supports the type= argument. conversion from INTSXP and int64 to all int types", { +test_that("Array$create() supports the type= argument. conversion from INTSXP and int64 to all int types", { num_int32 <- 12L num_int64 <- bit64::as.integer64(10) @@ -362,38 +362,38 @@ test_that("array() supports the type= argument. conversion from INTSXP and int64 float32(), float64() ) for(type in types) { - expect_equal(array(num_int32, type = type)$type, type) - expect_equal(array(num_int64, type = type)$type, type) + expect_equal(Array$create(num_int32, type = type)$type, type) + expect_equal(Array$create(num_int64, type = type)$type, type) } }) -test_that("array() aborts on overflow", { - expect_error(array(128L, type = int8())$type, "Invalid.*downsize") - expect_error(array(-129L, type = int8())$type, "Invalid.*downsize") +test_that("Array$create() aborts on overflow", { + expect_error(Array$create(128L, type = int8())$type, "Invalid.*downsize") + expect_error(Array$create(-129L, type = int8())$type, "Invalid.*downsize") - expect_error(array(256L, type = uint8())$type, "Invalid.*downsize") - expect_error(array(-1L, type = uint8())$type, "Invalid.*downsize") + expect_error(Array$create(256L, type = uint8())$type, "Invalid.*downsize") + expect_error(Array$create(-1L, type = uint8())$type, "Invalid.*downsize") - expect_error(array(32768L, type = int16())$type, "Invalid.*downsize") - expect_error(array(-32769L, type = int16())$type, "Invalid.*downsize") + expect_error(Array$create(32768L, type = int16())$type, "Invalid.*downsize") + expect_error(Array$create(-32769L, type = int16())$type, "Invalid.*downsize") - expect_error(array(65536L, type = uint16())$type, "Invalid.*downsize") - expect_error(array(-1L, type = uint16())$type, "Invalid.*downsize") + expect_error(Array$create(65536L, type = uint16())$type, "Invalid.*downsize") + expect_error(Array$create(-1L, type = uint16())$type, "Invalid.*downsize") - expect_error(array(65536L, type = uint16())$type, "Invalid.*downsize") - expect_error(array(-1L, type = uint16())$type, "Invalid.*downsize") + expect_error(Array$create(65536L, type = uint16())$type, "Invalid.*downsize") + expect_error(Array$create(-1L, type = uint16())$type, "Invalid.*downsize") - expect_error(array(bit64::as.integer64(2^31), type = int32()), "Invalid.*downsize") - expect_error(array(bit64::as.integer64(2^32), type = uint32()), "Invalid.*downsize") + expect_error(Array$create(bit64::as.integer64(2^31), type = int32()), "Invalid.*downsize") + expect_error(Array$create(bit64::as.integer64(2^32), type = uint32()), "Invalid.*downsize") }) -test_that("array() can convert doubles to integer", { +test_that("Array$create() does not convert doubles to integer", { types <- list( int8(), int16(), int32(), int64(), uint8(), uint16(), uint32(), uint64() ) for(type in types) { - a <- array(10, type = type) + a <- Array$create(10, type = type) expect_equal(a$type, type) # exception for now because we cannot handle @@ -404,45 +404,44 @@ test_that("array() can 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$create() converts raw vectors to uint8 arrays (ARROW-3794)", { + expect_equal(Array$create(as.raw(1:10))$type, uint8()) }) test_that("Array$as_vector() converts to integer (ARROW-3794)", { - a <- array((-128):127)$cast(int8()) + a <- Array$create((-128):127)$cast(int8()) expect_equal(a$type, int8()) expect_equal(a$as_vector(), (-128):127) - a <- array(0:255)$cast(uint8()) + a <- Array$create(0:255)$cast(uint8()) expect_equal(a$type, uint8()) expect_equal(a$as_vector(), 0:255) }) -test_that("array() recognise arrow::Array (ARROW-3815)", { - a <- array(1:10) - expect_equal(a, array(a)) +test_that("Array$create() recognise arrow::Array (ARROW-3815)", { + a <- Array$create(1:10) + expect_equal(a, Array$create(a)) }) -test_that("array() handles data frame -> struct arrays (ARROW-3811)", { +test_that("Array$create() handles data frame -> struct arrays (ARROW-3811)", { df <- tibble::tibble(x = 1:10, y = x / 2, z = letters[1:10]) - a <- array(df) + a <- Array$create(df) expect_equal(a$type, struct(x = int32(), y = float64(), z = utf8())) expect_equivalent(a$as_vector(), df) }) -test_that("array() can handle data frame with custom struct type (not infered)", { +test_that("Array$create() can handle data frame with custom struct type (not infered)", { df <- tibble::tibble(x = 1:10, y = 1:10) type <- struct(x = float64(), y = int16()) - a <- array(df, type = type) + a <- Array$create(df, type = type) expect_equal(a$type, type) type <- struct(x = float64(), y = int16(), z = int32()) - expect_error(array(df, type = type), regexp = "Number of fields in struct.* incompatible with number of columns in the data frame") + expect_error(Array$create(df, type = type), regexp = "Number of fields in struct.* incompatible with number of columns in the data frame") type <- struct(y = int16(), x = float64()) - expect_error(array(df, type = type), regexp = "Field name in position.*does not match the name of the column of the data frame") + expect_error(Array$create(df, type = type), regexp = "Field name in position.*does not match the name of the column of the data frame") type <- struct(x = float64(), y = utf8()) - expect_error(array(df, type = type), regexp = "Cannot convert R object to string array") + expect_error(Array$create(df, type = type), regexp = "Cannot convert R object to string array") }) - diff --git a/r/tests/testthat/test-RecordBatch.R b/r/tests/testthat/test-RecordBatch.R index bf37ed7f1073f..d8ba8da0b5974 100644 --- a/r/tests/testthat/test-RecordBatch.R +++ b/r/tests/testthat/test-RecordBatch.R @@ -32,7 +32,7 @@ test_that("RecordBatch", { schema( int = int32(), dbl = float64(), lgl = boolean(), chr = utf8(), - fct = dictionary(int32(), array(letters[1:10])) + fct = dictionary(int32(), Array$create(letters[1:10])) ) ) expect_equal(batch$num_columns, 5L) @@ -67,12 +67,12 @@ test_that("RecordBatch", { col_fct <- batch$column(4) expect_true(inherits(col_fct, 'Array')) expect_equal(col_fct$as_vector(), tbl$fct) - expect_equal(col_fct$type, dictionary(int32(), array(letters[1:10]))) + expect_equal(col_fct$type, dictionary(int32(), Array$create(letters[1:10]))) batch2 <- batch$RemoveColumn(0) expect_equal( batch2$schema, - schema(dbl = float64(), lgl = boolean(), chr = utf8(), fct = dictionary(int32(), array(letters[1:10]))) + schema(dbl = float64(), lgl = boolean(), chr = utf8(), fct = dictionary(int32(), Array$create(letters[1:10]))) ) expect_equal(batch2$column(0), batch$column(1)) expect_identical(as.data.frame(batch2), tbl[,-1]) @@ -103,7 +103,7 @@ test_that("RecordBatch with 0 rows are supported", { dbl = float64(), lgl = boolean(), chr = utf8(), - fct = dictionary(int32(), array(c("a", "b"))) + fct = dictionary(int32(), Array$create(c("a", "b"))) ) ) }) @@ -147,7 +147,7 @@ test_that("RecordBatch dim() and nrow() (ARROW-3816)", { }) test_that("record_batch() handles Array", { - batch <- record_batch(x = 1:10, y = arrow::array(1:10)) + batch <- record_batch(x = 1:10, y = Array$create(1:10)) expect_equal(batch$schema, schema(x = int32(), y = int32())) }) diff --git a/r/tests/testthat/test-Table.R b/r/tests/testthat/test-Table.R index fb04bdefdfdbe..a22c992861592 100644 --- a/r/tests/testthat/test-Table.R +++ b/r/tests/testthat/test-Table.R @@ -103,7 +103,7 @@ test_that("table() handles record batches with splicing", { }) test_that("table() handles ... of arrays, chunked arrays, vectors", { - a <- array(1:10) + a <- Array$create(1:10) ca <- chunked_array(1:5, 6:10) v <- rnorm(10) tbl <- tibble::tibble(x = 1:10, y = letters[1:10]) diff --git a/r/tests/testthat/test-arraydata.R b/r/tests/testthat/test-arraydata.R index 02ca9b8562595..e05a83060056b 100644 --- a/r/tests/testthat/test-arraydata.R +++ b/r/tests/testthat/test-arraydata.R @@ -18,7 +18,7 @@ context("arrow::ArrayData") test_that("string vectors with only empty strings and nulls don't allocate a data buffer (ARROW-3693)", { - a <- array("") + a <- Array$create("") expect_equal(a$length(), 1L) buffers <- a$data()$buffers diff --git a/r/tests/testthat/test-chunkedarray.R b/r/tests/testthat/test-chunkedarray.R index 2e6b7306be1d8..fa0a45b245623 100644 --- a/r/tests/testthat/test-chunkedarray.R +++ b/r/tests/testthat/test-chunkedarray.R @@ -207,7 +207,7 @@ test_that("chunked_array() supports the type= argument. conversion from INTSXP a } }) -test_that("array() aborts on overflow", { +test_that("Array$create() aborts on overflow", { expect_error(chunked_array(128L, type = int8())$type, "Invalid.*downsize") expect_error(chunked_array(-129L, type = int8())$type, "Invalid.*downsize") @@ -276,7 +276,7 @@ test_that("chunked_array() handles 0 chunks if given a type", { test_that("chunked_array() can ingest arrays (ARROW-3815)", { expect_equal( - chunked_array(1:5, array(6:10))$as_vector(), + chunked_array(1:5, Array$create(6:10))$as_vector(), 1:10 ) }) diff --git a/r/tests/testthat/test-json.R b/r/tests/testthat/test-json.R index b3e7d5638f559..cea7b65cccb64 100644 --- a/r/tests/testthat/test-json.R +++ b/r/tests/testthat/test-json.R @@ -114,8 +114,8 @@ test_that("Can read json file with nested columns (ARROW-5503)", { ) struct_array <- tab1$column(1)$chunk(0) - ps <- array(c(NA, NA, 78, 90, NA, 19)) - hello <- array(c(NA, NA, "hi", "bonjour", "ciao", NA)) + ps <- Array$create(c(NA, NA, 78, 90, NA, 19)) + hello <- Array$create(c(NA, NA, "hi", "bonjour", "ciao", NA)) expect_equal(struct_array$field(0L), ps) expect_equal(struct_array$GetFieldByName("ps"), ps) expect_equal(struct_array$Flatten(), list(ps, hello)) diff --git a/r/tests/testthat/test-type.R b/r/tests/testthat/test-type.R index 70f8df6315956..9f60b5f0f8359 100644 --- a/r/tests/testthat/test-type.R +++ b/r/tests/testthat/test-type.R @@ -18,7 +18,7 @@ context("test-type") test_that("type() gets the right type for arrow::Array", { - a <- array(1:10) + a <- Array$create(1:10) expect_equal(type(a), a$type) }) @@ -35,7 +35,7 @@ test_that("type() infers from R type", { expect_equal(type(""), utf8()) expect_equal( type(iris$Species), - dictionary(int8(), array(levels(iris$Species)), FALSE) + dictionary(int8(), Array$create(levels(iris$Species)), FALSE) ) expect_equal( type(lubridate::ymd_hms("2019-02-14 13:55:05")),