Skip to content

Commit 7522dfd

Browse files
aplavinpiever
andauthored
improve tables support (#276)
* support more table types in the constructor * add fromtable() and Tables.materializer() * only run doctests on a single Julia version --------- Co-authored-by: Pietro Vertechi <[email protected]>
1 parent f6b194b commit 7522dfd

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/tables.jl

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ function _schema(::Type{T}) where {T<:NTuple{N, Any}} where N
1212
return Tables.Schema{ntuple(identity, N), T}
1313
end
1414

15+
StructArray(cols::Tables.AbstractColumns) = StructArray(Tables.columntable(cols))
16+
StructArray{T}(cols::Tables.AbstractColumns) where {T} = StructArray{T}(Tables.columntable(cols))
17+
18+
# convert from any Tables-compliant object
19+
fromtable(cols) = StructArray(Tables.columntable(cols))
20+
Tables.materializer(::Type{<:StructArray}) = fromtable
21+
Tables.materializer(::StructArray) = fromtable # Tables documentation says it's not needed, but actually it is
22+
1523
function try_compatible_columns(rows::R, s::StructArray) where {R}
1624
Tables.isrowtable(rows) && Tables.columnaccess(rows) || return nothing
1725
T = eltype(rows)

test/runtests.jl

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using SparseArrays
1414
using InfiniteArrays
1515

1616
using Documenter: doctest
17-
if Base.VERSION >= v"1.6" && Int === Int64
17+
if Base.VERSION == v"1.6" && Int === Int64
1818
doctest(StructArrays)
1919
end
2020

@@ -717,6 +717,22 @@ end
717717
# Testing integer column "names":
718718
@test invoke(append!, Tuple{StructVector,Any}, StructArray(([0],)), StructArray(([1],))) ==
719719
StructArray(([0, 1],))
720+
721+
dtab = (a=[1,2],) |> Tables.dictcolumntable
722+
@test StructArray(dtab) == [(a=1,), (a=2,)]
723+
@test StructArray{NamedTuple{(:a,), Tuple{Float64}}}(dtab) == [(a=1.,), (a=2.,)]
724+
@test StructVector{NamedTuple{(:a,), Tuple{Float64}}}(dtab) == [(a=1,), (a=2,)]
725+
726+
tblbase = (a=[1,2], b=["3", "4"])
727+
@testset for tblfunc in [Tables.columntable, Tables.rowtable, Tables.dictcolumntable, Tables.dictrowtable]
728+
tbl = tblfunc(tblbase)
729+
sa = StructArrays.fromtable(tbl)
730+
@test sa::StructArray == [(a=1, b="3"), (a=2, b="4")]
731+
sa = Tables.materializer(StructArray)(tbl)
732+
@test sa::StructArray == [(a=1, b="3"), (a=2, b="4")]
733+
sa = Tables.materializer(sa)(tbl)
734+
@test sa::StructArray == [(a=1, b="3"), (a=2, b="4")]
735+
end
720736
end
721737

722738
struct S

0 commit comments

Comments
 (0)