forked from TileDB-Inc/TileDB-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_arrayschemaevolution.R
More file actions
346 lines (275 loc) · 13.3 KB
/
Copy pathtest_arrayschemaevolution.R
File metadata and controls
346 lines (275 loc) · 13.3 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
library(tinytest)
library(tiledb)
if (Sys.info()[["sysname"]] == "Windows") exit_file("Skip on Windows")
ctx <- tiledb_ctx(limitTileDBCores())
if (tiledb_version(TRUE) < "2.11.0") exit_file("Needs TileDB 2.11.* or later")
df <- data.frame(key=letters[1:10],
val=1:10)
uri <- tempfile()
arr <- fromDataFrame(df, uri)
ase <- tiledb_array_schema_evolution()
attr <- tiledb_attr("foo", "INT32")
ase <- tiledb_array_schema_evolution_add_attribute(ase, attr)
ase <- tiledb_array_schema_evolution_array_evolve(ase, uri)
arr <- tiledb_array(uri, return_as="data.frame", extended=FALSE)
res <- arr[]
expect_equal(dim(res), c(10,3))
expect_equal(colnames(res), c("key", "val", "foo"))
ase <- tiledb_array_schema_evolution()
ase <- tiledb_array_schema_evolution_drop_attribute(ase, "val")
ase <- tiledb_array_schema_evolution_array_evolve(ase, uri)
arr <- tiledb_array(uri, return_as="data.frame", extended=FALSE)
res <- arr[]
expect_equal(dim(res), c(10,2))
expect_equal(colnames(res), c("key", "foo"))
if (tiledb_version(TRUE) < "2.17.0") exit_file("Needs TileDB 2.17.* or later")
df <- data.frame(key=letters[1:10], val=c(1:5,5:1))
uri <- tempfile()
arr <- fromDataFrame(df, uri)
sch <- schema(uri)
attrs <- attrs(sch)
attr <- attrs$val # copy of attribute
## First drop existing attribute
ase <- tiledb_array_schema_evolution()
ase <- tiledb_array_schema_evolution_drop_attribute(ase, "val")
tiledb_array_schema_evolution_array_evolve(ase, uri)
## Second add enumeration under a name
ase <- tiledb_array_schema_evolution()
enums <- c("blue", "green", "orange", "pink", "red")
ase <- tiledb_array_schema_evolution_add_enumeration(ase, "frobo", enums)
## Third connect the attribute to the enum and add it back in
attr <- tiledb_attribute_set_enumeration_name(attr, "frobo")
ase <- tiledb_array_schema_evolution_add_attribute(ase, attr)
tiledb_array_schema_evolution_array_evolve(ase, uri)
## check as data.frame
arr <- tiledb_array(uri, return_as="data.frame")
res <- arr[]
expect_true(is.factor(res$val))
expect_equal(levels(res$val), enums)
expect_equal(as.integer(res$val), c(1:5,5:1))
## -- testing 'create empty following by extending'
if (tiledb_version(TRUE) < "2.17.3") exit_file("Needs TileDB 2.17.3 or later")
uri <- tempfile()
dom <- tiledb_domain(dims = tiledb_dim("rows", c(1L, 20L), 10L, "INT32"))
attrs <- c(tiledb_attr("a", type = "INT32"), tiledb_attr("b", type = "INT32"))
schema <- tiledb_array_schema(dom, attrs = attrs, sparse = TRUE)
schema <- tiledb_array_schema_set_enumeration_empty(schema, attrs[[2]], "an_enum")
tiledb_array_create(uri, schema)
df <- data.frame(rows = 1:10, a = 100 + 0:9, b = rep(c(1L, 2L), each=5))
A <- tiledb_array(uri)
A[] <- df
arr <- tiledb_array(uri, return_as="data.frame")[]
expect_true(is.factor(arr[, "b"]))
expect_equal(levels(arr[, "b"]), character()) # empty levels vector
## now alter array
ase <- tiledb_array_schema_evolution()
arr <- tiledb_array(uri)
arr <- tiledb_array_open(arr, "READ")
ase <- tiledb_array_schema_evolution_extend_enumeration(ase, arr, "an_enum", c("red", "green"))
tiledb_array_schema_evolution_array_evolve(ase, uri)
arr <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(levels(arr[, "b"]), c("red", "green"))
## append to array
df <- data.frame(rows = 11:20, a = 100 + 10:19, b = rep(c(3L, 4L), each=5))
A <- tiledb_array(uri)
A[] <- df
## and alter again
ase <- tiledb_array_schema_evolution()
arr <- tiledb_array(uri)
arr <- tiledb_array_open(arr, "READ")
ase <- tiledb_array_schema_evolution_extend_enumeration(ase, arr, "an_enum", c("blue", "orange"))
tiledb_array_schema_evolution_array_evolve(ase, uri)
arr <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(levels(arr[, "b"]), c("red", "green", "blue", "orange"))
## -- testing query condition on non int32 columns
run_int_col_test <- function(coltype) {
uri <- tempfile()
enums <- c("blue", "green", "red")
dom <- tiledb_domain(dims = tiledb_dim(name="dim", domain=c(0L,100L), tile=10L, type="INT32"))
attrs <- c(tiledb_attr(name="fct", type = coltype, enumeration=enums),
tiledb_attr(name="dbl", type = "FLOAT64"))
schema <- tiledb_array_schema(domain=dom, attrs=attrs, sparse=TRUE, enumerations=list(fct=enums))
tiledb_array_create(uri, schema)
set.seed(42)
df <- data.frame(dim = 1:10, fct = sample(length(enums), 10, replace=TRUE) - 1, dbl = rnorm(10))
arr <- tiledb_array(uri)
arr[] <- df
res <- tiledb_array(uri, return_as="data.frame", query_condition = parse_query_condition(fct == blue, arr))[]
expect_equal(nrow(res), 5)
res <- tiledb_array(uri, return_as="data.frame", query_condition = parse_query_condition(fct == green, arr))[]
expect_equal(nrow(res), 3)
res <- tiledb_array(uri, return_as="data.frame", query_condition = parse_query_condition(fct == red, arr))[]
expect_equal(nrow(res), 2)
res <- tiledb_array(uri, return_as="data.frame", query_condition = parse_query_condition(fct != blue, arr))[]
expect_equal(nrow(res), 5)
expect_error(tiledb_array(uri, return_as="data.frame", query_condition = parse_query_condition(fct > blue, arr))[])
unlink(uri)
}
sapply(c("INT8", "INT16", "INT32", "UINT8", "UINT16", "UINT32"), run_int_col_test)
## test that factor levels can grow without overlap
uri <- tempfile()
df1 <- data.frame(id = 1:3, obs = factor(c("A", "B", "A")))
fromDataFrame(df1, uri, col_index=1, tile_domain=c(1L, 6L))
## write with a factor with two elements but without one of the initial ones
## while factor(c("B", "C", "B")) gets encoded as c(1,2,1) it should really
## encoded as c(2,3,2) under levels that are c("A", "B", "C") -- and the
## write method now does that
df2 <- data.frame(id = 4:6, obs = factor(c("B", "C", "B")))
fromDataFrame(df2, uri, col_index=1, mode="append")
res <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(nrow(res), 6)
expect_equal(nlevels(res[["obs"]]), 3)
expect_equal(levels(res[["obs"]]), c("A", "B", "C"))
expect_equal(as.integer(res[["obs"]]), c(1L, 2L, 1L, 2L, 3L, 2L))
ref <- rbind(df1, df2)
expect_equivalent(res, ref) # equivalent because of query status attribute
## test that ordered factor levels can grow without overlap
uri <- tempfile()
df1 <- data.frame(id = 1:3, obs = ordered(c("A", "B", "A")))
fromDataFrame(df1, uri, col_index=1, tile_domain=c(1L, 6L))
## write with a factor with two elements but without one of the initial ones
## while factor(c("B", "C", "B")) gets encoded as c(1,2,1) it should really
## encoded as c(2,3,2) under levels that are c("A", "B", "C") -- and the
## write method now does that
df2 <- data.frame(id = 4:6, obs = ordered(c("B", "C", "B")))
fromDataFrame(df2, uri, col_index=1, mode="append")
res <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(nrow(res), 6)
expect_equal(nlevels(res[["obs"]]), 3)
expect_equal(levels(res[["obs"]]), c("A", "B", "C"))
expect_equal(as.integer(res[["obs"]]), c(1L, 2L, 1L, 2L, 3L, 2L))
ref <- rbind(df1, df2)
expect_equivalent(res, ref) # equivalent because of query status attribute
## test that factor levels are re-leveled with new updates
## but existing levels (case with subset of current levels fixing issue 843)
uri <- tempfile()
df1 <- data.frame(id = 1:3, obs = factor(c("A", "B", "C")))
fromDataFrame(df1, uri, col_index=1, tile_domain=c(1L, 5L))
df2 <- data.frame(id = 4:5, obs = factor(c("B", "C")))
fromDataFrame(df2, uri, col_index=1, mode="append")
res <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(nrow(res), 5)
expect_equal(nlevels(res[["obs"]]), 3)
expect_equal(levels(res[["obs"]]), c("A", "B", "C"))
expect_equal(as.integer(res[["obs"]]), c(1L, 2L, 3L, 2L, 3L))
ref <- rbind(df1, df2)
expect_equivalent(res, ref) # equivalent because of query status attribute
## test that factor levels are re-leveled with new updates
## but existing levels (case with all current levels)
uri <- tempfile()
df1 <- data.frame(id = 1:3, obs = factor(c("A", "B", "C")))
fromDataFrame(df1, uri, col_index=1, tile_domain=c(1L, 6L))
df2 <- data.frame(id = 4:6, obs = factor(c("B", "C", "A")))
fromDataFrame(df2, uri, col_index=1, mode="append")
res <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(nrow(res), 6)
expect_equal(nlevels(res[["obs"]]), 3)
expect_equal(levels(res[["obs"]]), c("A", "B", "C"))
expect_equal(as.integer(res[["obs"]]), c(1L, 2L, 3L, 2L, 3L, 1L))
ref <- rbind(df1, df2)
expect_equivalent(res, ref) # equivalent because of query status attribute
## test that ordered factor levels are re-leveled with new updates
## but existing levels (case with subset of current levels fixing issue 843)
uri <- tempfile()
df1 <- data.frame(id = 1:3, obs = ordered(c("A", "B", "C")))
fromDataFrame(df1, uri, col_index=1, tile_domain=c(1L, 5L))
df2 <- data.frame(id = 4:5, obs = ordered(c("B", "C")))
fromDataFrame(df2, uri, col_index=1, mode="append")
res <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(nrow(res), 5)
expect_equal(nlevels(res[["obs"]]), 3)
expect_equal(levels(res[["obs"]]), c("A", "B", "C"))
expect_equal(as.integer(res[["obs"]]), c(1L, 2L, 3L, 2L, 3L))
ref <- rbind(df1, df2)
expect_equivalent(res, ref) # equivalent because of query status attribute
## test that ordered factor levels are re-leveled with new updates
## but existing levels (case with all current levels)
uri <- tempfile()
df1 <- data.frame(id = 1:3, obs = ordered(c("A", "B", "C")))
fromDataFrame(df1, uri, col_index=1, tile_domain=c(1L, 6L))
df2 <- data.frame(id = 4:6, obs = ordered(c("B", "C", "A")))
fromDataFrame(df2, uri, col_index=1, mode="append")
res <- tiledb_array(uri, return_as="data.frame")[]
expect_equal(nrow(res), 6)
expect_equal(nlevels(res[["obs"]]), 3)
expect_equal(levels(res[["obs"]]), c("A", "B", "C"))
expect_equal(as.integer(res[["obs"]]), c(1L, 2L, 3L, 2L, 3L, 1L))
ref <- rbind(df1, df2)
expect_equivalent(res, ref) # equivalent because of query status attribute
## another test for growing
uri <- tempfile()
df1 <- data.frame(rows=11:14, a=200+0:3, b=factor(rep(c("blue", "ornage"), each=2)))
expect_silent(fromDataFrame(df1, uri, col_index=1, tile_domain=c(1L, 30L)))
df2 <- data.frame(rows=21:24, a=300+0:3, b=factor(rep(c("brown", "yellow"), each=2)))
expect_silent(fromDataFrame(df2, uri, mode="append", col_index=1))
res <- tiledb_array(uri, return_as="data.frame")[]
expect_equivalent(res, rbind(df1, df2)) # factors in data.frame get releveled too
## check factor additions do not overflow
for (tp in c("INT8", "UINT8")) {
uri <- tempfile()
dom <- tiledb_domain(dims = tiledb_dim("rows", c(1L, 300L), 10L, "INT32"))
attrs <- c(tiledb_attr("a", type = "INT32"),
tiledb_attr("b", type = tp))
schema <- tiledb_array_schema(dom, attrs = attrs, sparse = TRUE)
schema <- tiledb_array_schema_set_enumeration_empty(schema, attrs[[2]], "b")
invisible(tiledb_array_create(uri, schema))
df <- data.frame(rows = 1:50, a = 100 + 0:49, b = factor(paste0("f", 101:150)))
expect_silent(fromDataFrame(df, uri, mode="append", col_index=1))
df <- data.frame(rows = 51:130, a = 200 + 0:79, b = factor(paste0("f", 151:230)))
if (tp == "INT8") {
expect_error(fromDataFrame(df, uri, mode="append", col_index=1)) # errors for INT8
} else {
expect_silent(fromDataFrame(df, uri, mode="append", col_index=1)) # passes for UINT8
}
}
## tests formerly in file 'test_arrayschemaevolution_arrow.R'
if (Sys.info()[["sysname"]] == "Windows") exit_file("Skip on Windows")
if (Sys.getenv("CI", "") == "") exit_file("Skip unextended test run")
if (tiledb_version(TRUE) < "2.17.0") exit_file("Needs TileDB 2.17.* or later")
df <- data.frame(key=letters[1:10], val=c(1:5,5:1))
uri <- tempfile()
arr <- fromDataFrame(df, uri)
sch <- schema(uri)
attrs <- attrs(sch)
attr <- attrs$val # copy of attribute
## First drop existing attribute
ase <- tiledb_array_schema_evolution()
ase <- tiledb_array_schema_evolution_drop_attribute(ase, "val")
tiledb_array_schema_evolution_array_evolve(ase, uri)
## Second add enumeration under a name
ase <- tiledb_array_schema_evolution()
enums <- c("blue", "green", "orange", "pink", "red")
ase <- tiledb_array_schema_evolution_add_enumeration(ase, "frobo", enums)
## Third connect the attribute to the enum and add it back in
attr <- tiledb_attribute_set_enumeration_name(attr, "frobo")
ase <- tiledb_array_schema_evolution_add_attribute(ase, attr)
tiledb_array_schema_evolution_array_evolve(ase, uri)
## check as data.frame
arr <- tiledb_array(uri, return_as="data.frame")
res <- arr[]
expect_true(is.factor(res$val))
expect_equal(levels(res$val), enums)
expect_equal(as.integer(res$val), c(1:5,5:1))
## check as arrow
if (!requireNamespace("arrow", quietly=TRUE)) exit_file("No 'arrow' package.")
arr <- tiledb_array(uri, return_as="arrow")
res <- arr[]
v <- res[["val"]]$as_vector()
expect_true(is.factor(v))
expect_equal(levels(v), enums)
expect_equal(as.integer(v), c(1:5,5:1))
## current domain
if (tiledb_version(TRUE) < "2.26.0") exit_file("Needs TileDB 2.26.* or later")
uri <- tempfile()
dim <- tiledb_dim("dim", c(1L, 1000L), 50L, type = "INT32")
dom <- tiledb_domain(dim = dim)
attr <- tiledb_attr("a", type = "INT32")
sch <- tiledb_array_schema(dom, attrs = attr, sparse = TRUE)
arr <- tiledb_array_create(uri, sch)
cd <- tiledb_current_domain()
ndr <- tiledb_ndrectangle(dom)
tiledb_ndrectangle_set_range(ndr, "dim", 1L, 100L)
cd <- tiledb_current_domain_set_ndrectangle(cd, ndr)
ase <- tiledb_array_schema_evolution()
expect_silent(tiledb_array_schema_evolution_expand_current_domain(ase, cd))
expect_silent(tiledb_array_schema_evolution_array_evolve(ase, uri))