Skip to content

Commit a6a1f3f

Browse files
committed
Workaround to enable writing of high-dimensional dense arrays.
Seems to be an issue with the underlying tiledb package, but whatever.
1 parent 4e1f73b commit a6a1f3f

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: TileDBArray
2-
Version: 1.21.0
3-
Date: 2025-06-19
2+
Version: 1.21.1
3+
Date: 2026-04-04
44
Title: Using TileDB as a DelayedArray Backend
55
Description: Implements a DelayedArray backend for reading and
66
writing dense or sparse arrays in the TileDB format. The

R/TileDBRealizationSink.R

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ setValidity2("TileDBRealizationSink", function(object) {
210210
#' @importFrom SparseArray nzwhich nzvals
211211
#' @importFrom DelayedArray start width
212212
setMethod("write_block", "TileDBRealizationSink", function(sink, viewport, block) {
213-
starts <- start(viewport) - 1L
214213
obj <- tiledb_array(sink@path, attrs=sink@attr, query_type="WRITE")
215214
on.exit(tiledb_array_close(obj))
216215

@@ -220,24 +219,27 @@ setMethod("write_block", "TileDBRealizationSink", function(sink, viewport, block
220219

221220
ndim <- ncol(idx)
222221
store <- vector("list", ndim + 1L)
222+
starts <- start(viewport)
223223
for (i in seq_len(ndim)) {
224-
store[[i]] <- idx[,i] + starts[i] + sink@offset[i] - 1L
224+
store[[i]] <- idx[,i] + (starts[i] - 1L) + (sink@offset[i] - 1L)
225225
}
226226
store[[ndim + 1]] <- vals
227227

228228
names(store) <- c(sprintf("d%i", seq_len(ndim)), sink@attr)
229229
obj[] <- data.frame(store)
230230

231231
} else {
232-
args <- lapply(width(viewport), seq_len)
233-
for (i in seq_along(args)) {
234-
args[[i]] <- args[[i]] + starts[i] + sink@offset[i] - 1L
235-
}
232+
starts <- start(viewport)
233+
widths <- width(viewport)
234+
ndim <- length(starts)
236235

237-
# Need to coerce the block, because it could be a SparseArray
238-
# derivative.
239-
args <- c(list(sink=obj), args, list(value=as.array(block)))
240-
do.call("[<-", args)
236+
ranges <- vector("list", ndim)
237+
for (i in seq_len(ndim)) {
238+
actual.start <- starts[i] + (sink@offset[i] - 1L)
239+
ranges[[i]] <- cbind(actual.start, actual.start + widths[i] - 1L)
240+
}
241+
selected_ranges(obj) <- ranges
242+
obj[] <- as.array(block) # Need to coerce the block, because it could be a SparseArray derivative.
241243
}
242244

243245
sink

inst/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
\title{TileDBArray News}
33
\encoding{UTF-8}
44

5+
\section{Version 1.22.0}{\itemize{
6+
\item Workaround for \code{TileDBRealizationSink} to properly write higher-dimensional dense arrays.
7+
}}
8+
59
\section{Version 1.16.0}{\itemize{
610
\item Minor fix for \code{as.data.frame=} deprecation in \code{tiledb_array()}.
711

tests/testthat/test-write.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ test_that("can shunt between sparse and non-sparse, regardless of the format", {
5454
}
5555
})
5656

57+
test_that("works with high-dimensional arrays", {
58+
path <- tempfile()
59+
arr <- array(runif(1000), dim=c(10, 5, 20))
60+
out <- writeTileDBArray(arr, path=path)
61+
expect_identical(unname(as.array(out)), arr)
62+
63+
path <- tempfile()
64+
arr[] <- 0
65+
arr[sample(length(arr), 100)] <- rpois(100, lambda=10)
66+
arr <- as(arr, "SVT_SparseArray")
67+
out <- writeTileDBArray(arr, path=path, sparse=TRUE)
68+
expect_identical(as(out, "SVT_SparseArray"), arr)
69+
})
70+
5771
test_that("responds to the path", {
5872
path <- tempfile()
5973
expect_false(file.exists(path))

0 commit comments

Comments
 (0)