Skip to content

Add mockchunks function #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DiskArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include("mapreduce.jl")
include("permute.jl")
include("reshape.jl")
include("subarray.jl")
include("rechunk.jl")
include("mockchunks.jl")
include("cat.jl")
include("generator.jl")
include("zip.jl")
Expand Down
53 changes: 53 additions & 0 deletions src/mockchunks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
MockChunkedDiskArray <: AbstractDiskArray

MockChunkedDiskArray(parent::AbstractArray, chunks::GridChunks)

A disk array that pretends to have a specific chunk pattern,
regardless of the true chunk pattern of the parent array.

This is useful in `zip` and other operations that can iterate
over multiple arrays with different patterns.
"""
struct MockChunkedDiskArray{T,N,A<:AbstractArray{T,N},C<:GridChunks} <: AbstractDiskArray{T,N}
parent::A
chunks::C
end

"""
mockchunks(data::AbstractArray,chunks)

Change the chunk pattern of the underlying DiskArray according to `chunks`.

Note that this will not change the chunking of the underlying data itself, it will just make the data
"look" like it had a different chunking. If you need a persistent on-disk representation of this chunking, save the resulting array.

The chunks argument can take one of the following forms:

- a [`DiskArrays.GridChunks`](@ref) object
- a tuple specifying the chunk size along each dimension, like `(10, 10, 1)` for a 3-D array
"""
mockchunks(data::AbstractDiskArray, chunks::GridChunks) = MockChunkedDiskArray(data, chunks)

Check warning on line 30 in src/mockchunks.jl

View check run for this annotation

Codecov / codecov/patch

src/mockchunks.jl#L30

Added line #L30 was not covered by tests

Base.parent(A::MockChunkedDiskArray) = A.parent
Base.size(A::MockChunkedDiskArray) = size(parent(A))

# DiskArrays interface

haschunks(::MockChunkedDiskArray) = Chunked()
eachchunk(A::MockChunkedDiskArray) = A.chunks

# These could be more efficient with memory in some cases, but this is simple
readblock!(A::MockChunkedDiskArray, data, I...) = _readblock_mockchunked(A, data, I...)

Check warning on line 41 in src/mockchunks.jl

View check run for this annotation

Codecov / codecov/patch

src/mockchunks.jl#L41

Added line #L41 was not covered by tests
readblock!(A::MockChunkedDiskArray, data, I::AbstractVector...) =
_readblock_mockchunked(A, data, I...)
writeblock!(A::MockChunkedDiskArray, data, I...) = writeblock!(parent(A), data, I...)

Check warning on line 44 in src/mockchunks.jl

View check run for this annotation

Codecov / codecov/patch

src/mockchunks.jl#L44

Added line #L44 was not covered by tests

function _readblock_mockchunked(A, data, I...)
if haschunks(parent(A)) isa Chunked
readblock!(parent(A), data, I...)
else
# Handle non disk arrays that may be chunked for e.g. chunked `zip`
copyto!(data, view(parent(A), I...))
end
end
38 changes: 0 additions & 38 deletions src/rechunk.jl

This file was deleted.

2 changes: 1 addition & 1 deletion src/zip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function DiskZip(As::AbstractArray...)
return DiskZip(As)
else
rechunked = map(As) do A
RechunkedDiskArray(A, chunks)
MockChunkedDiskArray(A, chunks)
end
return DiskZip(rechunked)
end
Expand Down
Loading