Skip to content
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

improve tables support #276

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions src/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Loading