Skip to content

Commit

Permalink
add colmetadata support (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin authored Nov 17, 2024
1 parent 07e7462 commit ac5e719
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/StructArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ function refvalue(s::StructArray{T}, v::Tup) where {T}
createinstance(T, map(refvalue, components(s), v)...)
end

# implement colmetadata for StructArray based on metadata of individual columns
import DataAPI: metadata, metadatasupport, colmetadata, colmetadatasupport

colmetadatasupport(::Type{T}) where {T<:StructArray} = (
read=any(col -> metadatasupport(col).read, array_types(T).parameters),
write=false, # not implemented
)
colmetadata(sa::StructArray, col::Symbol) = metadata(getproperty(sa, col))
colmetadata(sa::StructArray) =
map(Tables.columns(sa)) do col
metadatasupport(typeof(col)).read ? metadata(col) : nothing
end

@static if !isdefined(Base, :get_extension)
include("../ext/StructArraysAdaptExt.jl")
include("../ext/StructArraysGPUArraysCoreExt.jl")
Expand Down
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,24 @@ end
end
end

@testset "metadata" begin
using DataAPI: colmetadatasupport, colmetadata, metadata, DataAPI

struct MyArray <: AbstractVector{Int} end
Base.size(::MyArray) = (2,)
DataAPI.metadatasupport(::Type{<:MyArray}) = (read=true, write=false)
DataAPI.metadata(::MyArray) = (x=1, y="2")

sa = StructArray(a=[1,2], b=[1,2])
@test colmetadatasupport(typeof(sa)) == (read=false, write=false)

sa = StructArray(a=MyArray(), b=[1,2])
@test metadata(sa.a) == (x=1, y="2")
@test colmetadatasupport(typeof(sa)) == (read=true, write=false)
@test colmetadata(sa, :a) == (x=1, y="2")
@test colmetadata(sa) == (a=(x=1, y="2"), b=nothing)
end

struct ArrayConverter end

Adapt.adapt_storage(::ArrayConverter, xs::AbstractArray) = convert(Array, xs)
Expand Down

0 comments on commit ac5e719

Please sign in to comment.