Skip to content

Commit

Permalink
add more tests for dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed Aug 22, 2023
1 parent bed2f5c commit fa80a63
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions r/R/convert-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ convert_array.vctrs_partial_factor <- function(array, to, ...) {
stop_cant_convert_array(array, to)
}

browser()

levels <- convert_array(array$dictionary, character())
array$dictionary <- NULL
indices <- convert_array(array, integer()) + 1L
Expand Down
60 changes: 60 additions & 0 deletions r/tests/testthat/test-convert-array.R
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ test_that("convert to vector works for null -> logical()", {
)
})

test_that("convert to vector works for dictionary<boolean> -> logical()", {
array <- as_nanoarrow_array(c(0L, 1L, 2L, 1L, 0L))
array$dictionary <- as_nanoarrow_array(c(TRUE, FALSE, NA))

expect_identical(
convert_array(array, logical()),
c(TRUE, FALSE, NA, FALSE, TRUE)
)
})

test_that("convert to vector errors for bad array to logical()", {
expect_error(
convert_array(as_nanoarrow_array(letters), logical()),
Expand Down Expand Up @@ -328,6 +338,16 @@ test_that("convert to vector works for null -> logical()", {
)
})

test_that("convert to vector works for dictionary<integer> -> integer()", {
array <- as_nanoarrow_array(c(0L, 1L, 2L, 1L, 0L))
array$dictionary <- as_nanoarrow_array(c(123L, 0L, NA_integer_))

expect_identical(
convert_array(array, integer()),
c(123L, 0L, NA_integer_, 0L, 123L)
)
})

test_that("convert to vector warns for invalid integer()", {
array <- as_nanoarrow_array(.Machine$integer.max + 1)
expect_warning(
Expand Down Expand Up @@ -434,6 +454,16 @@ test_that("convert to vector works for null -> double()", {
)
})

test_that("convert to vector works for dictionary<double> -> double()", {
array <- as_nanoarrow_array(c(0L, 1L, 2L, 1L, 0L))
array$dictionary <- as_nanoarrow_array(c(123, 0, NA_real_))

expect_identical(
convert_array(array, double()),
c(123, 0, NA_real_, 0, 123)
)
})

test_that("convert to vector errors for bad array to double()", {
expect_error(
convert_array(as_nanoarrow_array(letters), double()),
Expand Down Expand Up @@ -508,6 +538,20 @@ test_that("convert to vector works for dictionary<string> -> factor()", {
)
})

test_that("batched convert to vector works for dictionary<string> -> factor()", {
# A slightly different path: convert_array.factor() called from C multiple
# times with different dictionaries each time.
array1 <- as_nanoarrow_array(factor(letters[1:5]))
array2 <- as_nanoarrow_array(factor(letters[6:10]))
array3 <- as_nanoarrow_array(factor(letters[11:15]))

stream <- basic_array_stream(list(array1, array2, array3))
expect_identical(
convert_array_stream(stream, factor(levels = letters)),
factor(letters[1:15], levels = letters)
)
})

test_that("convert to vector works for dictionary<string> -> partial_factor()", {
skip_if_not_installed("vctrs")

Expand All @@ -523,6 +567,22 @@ test_that("convert to vector works for dictionary<string> -> partial_factor()",
)
})

test_that("batched convert to vector works for dictionary<string> -> partial_factor()", {
skip_if_not_installed("vctrs")

# A slightly different path: convert_array.factor() called from C multiple
# times with different dictionaries each time.
array1 <- as_nanoarrow_array(factor(letters[1:5]))
array2 <- as_nanoarrow_array(factor(letters[6:10]))
array3 <- as_nanoarrow_array(factor(letters[11:15]))

stream <- basic_array_stream(list(array1, array2, array3))
expect_identical(
convert_array_stream(stream, vctrs::partial_factor()),
factor(letters[1:15], levels = letters)
)
})

test_that("convert to vector works for blob::blob()", {
skip_if_not_installed("blob")

Expand Down

0 comments on commit fa80a63

Please sign in to comment.