Skip to content

Commit 104aed6

Browse files
authored
Allow select to work on super-wide tables (#22)
Started with code suggested by @OkonSamuel in issue #20. Made some tweaks and added some tests and this seems to work now.
1 parent e945919 commit 104aed6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/TableOperations.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,22 @@ end
106106

107107
typesubset(::Tables.Schema{names, types}, ::Tuple{}) where {names, types} = Tuple{}
108108

109+
function typesubset(sch::Tables.Schema{nothing, nothing}, nms::NTuple{N, Symbol}) where {N}
110+
names = sch.names
111+
types = sch.types
112+
return Tuple{types[indexin(collect(nms), names)]...}
113+
end
114+
115+
function typesubset(sch::Tables.Schema{nothing, nothing}, inds::NTuple{N, Int}) where {N}
116+
types = sch.types
117+
return Tuple{Any[types[i] for i in inds]...}
118+
end
119+
120+
typesubset(::Tables.Schema{nothing, nothing}, ::Tuple{}) = Tuple{}
121+
109122
namesubset(::Tables.Schema{names, types}, nms::NTuple{N, Symbol}) where {names, types, N} = nms
110123
Base.@pure namesubset(::Tables.Schema{names, T}, inds::NTuple{N, Int}) where {names, T, N} = ntuple(i -> names[inds[i]], N)
124+
Base.@pure namesubset(sch::Tables.Schema{nothing, nothing}, inds::NTuple{N, Int}) where {N} = (names = sch.names; ntuple(i -> names[inds[i]], N))
111125
namesubset(::Tables.Schema{names, types}, ::Tuple{}) where {names, types} = ()
112126
namesubset(names, nms::NTuple{N, Symbol}) where {N} = nms
113127
namesubset(names, inds::NTuple{N, Int}) where {N} = ntuple(i -> names[inds[i]], N)

test/runtests.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ ctable = (A=[1, missing, 3], B=[1.0, 2.0, 3.0], C=["hey", "there", "sailor"])
44
rtable = Tables.rowtable(ctable)
55
rtable2 = Iterators.filter(i -> i.a % 2 == 0, [(a=x, b=y) for (x, y) in zip(1:20, 21:40)])
66

7+
struct ReallyWideTable
8+
end
9+
Tables.istable(::Type{ReallyWideTable}) = true
10+
Tables.columnaccess(::Type{ReallyWideTable}) = true
11+
Tables.columns(x::ReallyWideTable) = x
12+
Tables.columnnames(x::ReallyWideTable) = [Symbol(:x, i) for i = 1:100_000]
13+
Tables.getcolumn(x::ReallyWideTable, i::Int) = rand(10)
14+
Tables.getcolumn(x::ReallyWideTable, nm::Symbol) = rand(10)
15+
Tables.schema(x::ReallyWideTable) = Tables.Schema(Tables.columnnames(x), [Float64 for _ = 1:100_000])
16+
717
@testset "TableOperations.transform" begin
818

919
tran = ctable |> TableOperations.transform(C=Symbol)
@@ -93,6 +103,22 @@ end
93103

94104
@testset "TableOperations.select" begin
95105

106+
# 20
107+
x = ReallyWideTable()
108+
sel = TableOperations.select(x, :x1, :x2)
109+
sch = Tables.schema(sel)
110+
@test sch.names == (:x1, :x2)
111+
@test sch.types == (Float64, Float64)
112+
tt = Tables.columntable(sel)
113+
@test tt.x1 isa Vector{Float64}
114+
115+
sel = TableOperations.select(x, 1, 2)
116+
sch = Tables.schema(sel)
117+
@test sch.names == (:x1, :x2)
118+
@test sch.types == (Float64, Float64)
119+
tt = Tables.columntable(sel)
120+
@test tt.x1 isa Vector{Float64}
121+
96122
# 117
97123
sel = TableOperations.select(ctable)
98124
@test Tables.istable(typeof(sel))

0 commit comments

Comments
 (0)