This repository has been archived by the owner on May 7, 2024. It is now read-only.
/
array-stream.R
134 lines (115 loc) · 3.3 KB
/
array-stream.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
#' Stream objects as Arrow arrays
#'
#' @param list_of_array A `list()` of [narrow_array()]s.
#' @param schema The schema for all the arrays in `list_of_array`.
#' @param validate Use `FALSE` to skip validation of arrays in
#' `list_of_array`.
#' @param x An R object to convert to an Arrow Stream
#' @param array_stream An object of class "narrow_array_stream"
#' @param ... Passed to S3 methods
#' @inheritParams from_narrow_array
#'
#' @return An object of class "narrow_array_stream"
#' @export
#'
narrow_array_stream <- function(list_of_array = list(), schema = NULL, validate = TRUE) {
if (!is.list(list_of_array)) {
list_of_array <- list(list_of_array)
}
list_of_array <- lapply(list_of_array, as_narrow_array)
if (is.null(schema) && length(list_of_array) == 0) {
schema <- narrow_schema("n")
}
schema <- schema %||% list_of_array[[1]]$schema
schema <- as_narrow_schema(schema)
if (validate) {
for (i in seq_along(list_of_array)) {
tryCatch(
narrow_array(schema, list_of_array[[i]]$array_data, validate = TRUE),
error = function(e) {
msg <- conditionMessage(e)
stop(
sprintf("Validation of `list_of_array[[%d]]` failed:\n %s", i, msg),
call. = FALSE
)
}
)
}
}
.Call(narrow_c_narrow_array_stream, list_of_array, schema)
}
#' @rdname narrow_array_stream
#' @export
narrow_array_stream_collect <- function(array_stream, ptype = NULL) {
array_stream <- as_narrow_array_stream(array_stream)
if (is.null(ptype)) {
ptype <- narrow_default_ptype(
narrow_array_stream_get_schema(
array_stream
)
)
}
batches <- vector("list", 1024)
i <- 0
while (!is.null(batch <- narrow_array_stream_get_next(array_stream))) {
i <- i + 1L
batches[[i]] <- from_narrow_array(batch, ptype = ptype)
}
if (length(batches) > i) {
batches <- batches[seq_len(i)]
}
if (is.data.frame(ptype)) {
do.call(rbind, batches)
} else {
do.call(c, batches)
}
}
#' @rdname narrow_array_stream
#' @export
narrow_array_stream_get_schema <- function(array_stream) {
.Call(narrow_c_narrow_array_stream_get_schema, array_stream)
}
#' @rdname narrow_array_stream
#' @export
narrow_array_stream_get_next <- function(array_stream, validate = TRUE) {
array_data <- .Call(narrow_c_narrow_array_stream_get_next, array_stream)
if (is.null(array_data)) {
NULL
} else {
narrow_array(
narrow_array_stream_get_schema(array_stream),
array_data,
validate = validate
)
}
}
#' @rdname narrow_array_stream
#' @export
as_narrow_array_stream <- function(x, ...) {
UseMethod("as_narrow_array_stream")
}
#' @rdname narrow_array_stream
#' @export
as_narrow_array_stream.narrow_array_stream <- function(x, ...) {
x
}
#' @rdname narrow_array_stream
#' @export
as_narrow_array_stream.list <- function(x, ...) {
narrow_array_stream(x)
}
#' @rdname narrow_array_stream
#' @export
as_narrow_array_stream.function <- function(x, ...) {
as_narrow_array_stream(x(...))
}
#' @rdname narrow_array_stream
#' @export
as_narrow_array_stream.default <- function(x, ...) {
as_narrow_array_stream.narrow_array(as_narrow_array(x))
}
#' @rdname narrow_array_stream
#' @export
as_narrow_array_stream.narrow_array <- function(x, ...) {
.Call(narrow_c_narrow_array_stream, list(x), x$schema)
}