diff --git a/src/tables.jl b/src/tables.jl index d6ac2248..432a75b2 100644 --- a/src/tables.jl +++ b/src/tables.jl @@ -12,6 +12,14 @@ function _schema(::Type{T}) where {T<:NTuple{N, Any}} where N return Tables.Schema{ntuple(identity, N), T} end +StructArray(cols::Tables.AbstractColumns) = StructArray(Tables.columntable(cols)) +StructArray{T}(cols::Tables.AbstractColumns) where {T} = StructArray{T}(Tables.columntable(cols)) + +# convert from any Tables-compliant object +fromtable(cols) = StructArray(Tables.columntable(cols)) +Tables.materializer(::Type{<:StructArray}) = fromtable +Tables.materializer(::StructArray) = fromtable # Tables documentation says it's not needed, but actually it is + function try_compatible_columns(rows::R, s::StructArray) where {R} Tables.isrowtable(rows) && Tables.columnaccess(rows) || return nothing T = eltype(rows) diff --git a/test/runtests.jl b/test/runtests.jl index c1443111..bddea661 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,7 +12,7 @@ using Test using SparseArrays using Documenter: doctest -if Base.VERSION >= v"1.6" && Int === Int64 +if Base.VERSION == v"1.6" && Int === Int64 doctest(StructArrays) end @@ -714,6 +714,22 @@ end # Testing integer column "names": @test invoke(append!, Tuple{StructVector,Any}, StructArray(([0],)), StructArray(([1],))) == StructArray(([0, 1],)) + + dtab = (a=[1,2],) |> Tables.dictcolumntable + @test StructArray(dtab) == [(a=1,), (a=2,)] + @test StructArray{NamedTuple{(:a,), Tuple{Float64}}}(dtab) == [(a=1.,), (a=2.,)] + @test StructVector{NamedTuple{(:a,), Tuple{Float64}}}(dtab) == [(a=1,), (a=2,)] + + tblbase = (a=[1,2], b=["3", "4"]) + @testset for tblfunc in [Tables.columntable, Tables.rowtable, Tables.dictcolumntable, Tables.dictrowtable] + tbl = tblfunc(tblbase) + sa = StructArrays.fromtable(tbl) + @test sa::StructArray == [(a=1, b="3"), (a=2, b="4")] + sa = Tables.materializer(StructArray)(tbl) + @test sa::StructArray == [(a=1, b="3"), (a=2, b="4")] + sa = Tables.materializer(sa)(tbl) + @test sa::StructArray == [(a=1, b="3"), (a=2, b="4")] + end end struct S