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

Allow select to work on super-wide tables #22

Merged
merged 1 commit into from
Aug 18, 2021
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
14 changes: 14 additions & 0 deletions src/TableOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,22 @@ end

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

function typesubset(sch::Tables.Schema{nothing, nothing}, nms::NTuple{N, Symbol}) where {N}
names = sch.names
types = sch.types
return Tuple{types[indexin(collect(nms), names)]...}
end

function typesubset(sch::Tables.Schema{nothing, nothing}, inds::NTuple{N, Int}) where {N}
types = sch.types
return Tuple{Any[types[i] for i in inds]...}
end

typesubset(::Tables.Schema{nothing, nothing}, ::Tuple{}) = Tuple{}

namesubset(::Tables.Schema{names, types}, nms::NTuple{N, Symbol}) where {names, types, N} = nms
Base.@pure namesubset(::Tables.Schema{names, T}, inds::NTuple{N, Int}) where {names, T, N} = ntuple(i -> names[inds[i]], N)
Base.@pure namesubset(sch::Tables.Schema{nothing, nothing}, inds::NTuple{N, Int}) where {N} = (names = sch.names; ntuple(i -> names[inds[i]], N))
namesubset(::Tables.Schema{names, types}, ::Tuple{}) where {names, types} = ()
namesubset(names, nms::NTuple{N, Symbol}) where {N} = nms
namesubset(names, inds::NTuple{N, Int}) where {N} = ntuple(i -> names[inds[i]], N)
Expand Down
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ ctable = (A=[1, missing, 3], B=[1.0, 2.0, 3.0], C=["hey", "there", "sailor"])
rtable = Tables.rowtable(ctable)
rtable2 = Iterators.filter(i -> i.a % 2 == 0, [(a=x, b=y) for (x, y) in zip(1:20, 21:40)])

struct ReallyWideTable
end
Tables.istable(::Type{ReallyWideTable}) = true
Tables.columnaccess(::Type{ReallyWideTable}) = true
Tables.columns(x::ReallyWideTable) = x
Tables.columnnames(x::ReallyWideTable) = [Symbol(:x, i) for i = 1:100_000]
Tables.getcolumn(x::ReallyWideTable, i::Int) = rand(10)
Tables.getcolumn(x::ReallyWideTable, nm::Symbol) = rand(10)
Tables.schema(x::ReallyWideTable) = Tables.Schema(Tables.columnnames(x), [Float64 for _ = 1:100_000])

@testset "TableOperations.transform" begin

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

@testset "TableOperations.select" begin

# 20
x = ReallyWideTable()
sel = TableOperations.select(x, :x1, :x2)
sch = Tables.schema(sel)
@test sch.names == (:x1, :x2)
@test sch.types == (Float64, Float64)
tt = Tables.columntable(sel)
@test tt.x1 isa Vector{Float64}

sel = TableOperations.select(x, 1, 2)
sch = Tables.schema(sel)
@test sch.names == (:x1, :x2)
@test sch.types == (Float64, Float64)
tt = Tables.columntable(sel)
@test tt.x1 isa Vector{Float64}

# 117
sel = TableOperations.select(ctable)
@test Tables.istable(typeof(sel))
Expand Down