/
from-array.R
203 lines (168 loc) · 5.31 KB
/
from-array.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#' Convert Arrow vectors to R objects
#'
#' Note that [from_narrow_array()] dispatches on `ptype`
#'
#' @param x An [narrow_array()]
#' @param ptype An R object to use as a prototype
#' @param ... Passed to S3 methods
#'
#' @return An object with the same class as `ptype`
#' @export
#'
#' @examples
#' from_narrow_array(as_narrow_array(c(TRUE, FALSE, NA)), logical())
#'
from_narrow_array <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
UseMethod("from_narrow_array", ptype)
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.default <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
assert_x_narrow_array(x)
stop_cant_convert(x, ptype)
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.NULL <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
if (!inherits(x, "narrow_array")) {
NextMethod()
}
is_null_type <- x$schema$format == "n"
if (!is_null_type) {
NextMethod()
}
stopifnot(is.null(x$schema$dictionary))
NULL
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.logical <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
if (!is.null(x$schema$dictionary)) {
return(convert_arrow_fallback(x, ptype))
}
with_arrow_fallback(.Call(narrow_c_logical_from_array, x), x, ptype)
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.integer <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
if (!is.null(x$schema$dictionary)) {
return(convert_arrow_fallback(x, ptype))
}
with_arrow_fallback(.Call(narrow_c_integer_from_array, x), x, ptype)
}
from_narrow_array_integer <- function(x) {
with_arrow_fallback(.Call(narrow_c_integer_from_array, x), x, integer())
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.double <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
if (!is.null(x$schema$dictionary)) {
return(convert_arrow_fallback(x, ptype))
}
with_arrow_fallback(.Call(narrow_c_double_from_array, x), x, ptype)
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.raw <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
if (!is.null(x$schema$dictionary)) {
return(convert_arrow_fallback(x, ptype))
}
with_arrow_fallback(.Call(narrow_c_raw_from_array, x), ptype)
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.character <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
if (is.null(x$schema$dictionary)) {
with_arrow_fallback(.Call(narrow_c_character_from_array, x), x, ptype)
} else {
indices <- from_narrow_array_integer(x) + 1L
dictionary <- narrow_array(x$schema$dictionary, x$array_data$dictionary)
from_narrow_array(dictionary, character())[indices]
}
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.factor <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
assert_x_narrow_array(x)
stopifnot(!is.null(x$schema$dictionary))
# because of weirdness with UseMethod()
if (missing(ptype)) {
ptype <- narrow_default_ptype(x$schema)
}
# get indices
indices <- from_narrow_array_integer(x) + 1L
# try to detect levels if none were given
levels <- levels(ptype)
if (identical(levels, character())) {
dictionary <- narrow_array(x$schema$dictionary, x$array_data$dictionary)
levels <- from_narrow_array(dictionary, character())
}
class(indices) <- "factor"
attr(indices, "levels") <- levels
indices
}
#' @rdname from_narrow_array
#' @export
from_narrow_array.data.frame <- function(x, ptype = narrow_default_ptype(x$schema), ...) {
assert_x_narrow_array(x)
if (!is.null(x$schema$dictionary)) {
return(convert_arrow_fallback(x, ptype))
}
# because of weirdness with UseMethod()
if (missing(ptype)) {
ptype <- narrow_default_ptype(x$schema)
}
child_schemas <- x$schema$children
if (ncol(ptype) == 0) {
ptype <- narrow_default_ptype(x$schema)
} else {
stopifnot(identical(ncol(ptype), length(child_schemas)))
}
child_arrays <- x$array_data$children
child_arrays <- Map(narrow_array, child_schemas, child_arrays)
result <- Map(from_narrow_array, child_arrays, ptype)
names(result) <- names(ptype)
new_data_frame(result, nrow = as.integer(as.numeric(x$array_data$length)))
}
with_arrow_fallback <- function(expr, x, ptype) {
tryCatch(
force(expr),
error = function(e) {
err <- paste0(conditionMessage(e), collapse = "\n")
convert_arrow_fallback(x, ptype)
})
}
convert_arrow_fallback <- function(x, ptype) {
assert_arrow("fallback conversion")
if (!requireNamespace("vctrs", quietly = TRUE)) {
stop("Package 'vctrs' required for fallback conversion", call. = FALSE)
}
x_arrow <- from_narrow_array(x, arrow::Array)
# support dictionary encoding for any type
if (x_arrow$type_id == arrow::Type$DICTIONARY) {
x_arrow <- x_arrow$cast(x_arrow$dictionary()$type)
}
result <- x_arrow$as_vector()
# because vctrs won't numeric convert to character
if (is.character(ptype)) {
as.character(result)
} else {
vctrs::vec_cast(result, ptype)
}
}
assert_x_narrow_array <- function(x) {
if (!inherits(x, "narrow_array")) {
stop("`x` is not an `narrow_array()`", call. = FALSE)
}
}
stop_cant_convert <- function(x, ptype) {
ptype_label <- class(ptype)[1]
stop(
sprintf(
"Can't convert schema format '%s' to '%s'",
x$schema$format,
ptype_label
),
call. = FALSE
)
}