-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add TableTraits.jl integration #76
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ julia 0.6 | |
Compat 0.19 | ||
NamedTuples 2.1.0 | ||
PooledArrays | ||
TableTraits 0.0.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using TableTraits | ||
|
||
TableTraits.isiterable(x::IndexedTable) = true | ||
TableTraits.isiterabletable(x::IndexedTable) = true | ||
|
||
function TableTraits.getiterator{S<:IndexedTable}(source::S) | ||
return rows(source) | ||
end | ||
|
||
# Sink | ||
|
||
@generated function _fillIndexedTable{idx_indices,data_indices}(iter,idx_storage,data_storage,::Type{idx_indices},::Type{data_indices}) | ||
push_exprs = Expr(:block) | ||
for (i,idx) in enumerate(map(i->i.parameters[1],idx_indices.parameters)) | ||
ex = :( push!(idx_storage.columns[$i], row[$idx]) ) | ||
push!(push_exprs.args, ex) | ||
end | ||
|
||
for (i,idx) in enumerate(map(i->i.parameters[1],data_indices.parameters)) | ||
ex = :( push!(data_storage.columns[$i], row[$idx]) ) | ||
push!(push_exprs.args, ex) | ||
end | ||
|
||
quote | ||
for row in iter | ||
$push_exprs | ||
end | ||
end | ||
end | ||
|
||
function IndexedTable(x; idxcols::Union{Void,Vector{Symbol}}=nothing, datacols::Union{Void,Vector{Symbol}}=nothing) | ||
if isiterabletable(x) | ||
iter = getiterator(x) | ||
|
||
source_colnames = TableTraits.column_names(iter) | ||
source_coltypes = TableTraits.column_types(iter) | ||
|
||
if idxcols==nothing && datacols==nothing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This case should probably result in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But then that's not what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a natural conversion of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Index columns need unique values, right? So one could also say that in this case it should create an |
||
idxcols = source_colnames[1:end-1] | ||
datacols = [source_colnames[end]] | ||
elseif idxcols==nothing | ||
idxcols = setdiff(source_colnames,datacols) | ||
elseif datacols==nothing | ||
datacols = setdiff(source_colnames, idxcols) | ||
end | ||
|
||
if length(setdiff(idxcols, source_colnames))>0 | ||
error("Unknown idxcol") | ||
end | ||
|
||
if length(setdiff(datacols, source_colnames))>0 | ||
error("Unknown datacol") | ||
end | ||
|
||
idxcols_indices = [findfirst(source_colnames,i) for i in idxcols] | ||
datacols_indices = [findfirst(source_colnames,i) for i in datacols] | ||
|
||
idx_storage = Columns([Array{source_coltypes[i],1}(0) for i in idxcols_indices]..., names=[source_colnames[i] for i in idxcols_indices]) | ||
data_storage = Columns([Array{source_coltypes[i],1}(0) for i in datacols_indices]..., names=[source_colnames[i] for i in datacols_indices]) | ||
|
||
tuple_type_idx = eval(Expr(:curly, :Tuple, [Expr(:curly, :Val, i) for i in idxcols_indices]...)) | ||
tuple_type_data = eval(Expr(:curly, :Tuple, [Expr(:curly, :Val, i) for i in datacols_indices]...)) | ||
|
||
_fillIndexedTable(iter, idx_storage, data_storage, tuple_type_idx, tuple_type_data) | ||
|
||
return IndexedTable(idx_storage, data_storage) | ||
elseif idxcols==nothing && datacols==nothing | ||
return convert(IndexedTable, x) | ||
else | ||
throw(ArgumentError("x cannot be turned into an IndexedTable.")) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
include("test_core.jl") | ||
include("test_query.jl") | ||
include("test_utils.jl") | ||
include("test_tabletraits.jl") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using IndexedTables | ||
using TableTraits | ||
using NamedTuples | ||
using Base.Test | ||
|
||
@testset "TableTraits" begin | ||
|
||
source_it = IndexedTable(Columns(a=[1,2,3]), Columns(b=[1.,2.,3.], c=["A","B","C"])) | ||
|
||
@test isiterable(source_it) == true | ||
|
||
target_array = collect(getiterator(source_it)) | ||
|
||
@test length(target_array) == 3 | ||
@test target_array[1] == @NT(a=1, b=1., c="A") | ||
@test target_array[2] == @NT(a=2, b=2., c="B") | ||
@test target_array[3] == @NT(a=3, b=3., c="C") | ||
|
||
source_array = [@NT(a=1,b=1.,c="A"), @NT(a=2,b=2.,c="B"), @NT(a=3,b=3.,c="C")] | ||
|
||
it1 = IndexedTable(source_array) | ||
@test length(it1) == 3 | ||
@test it1[1,1.].c == "A" | ||
@test it1[2,2.].c == "B" | ||
@test it1[3,3.].c == "C" | ||
|
||
it2 = IndexedTable(source_array, idxcols=[:a]) | ||
@test length(it2) == 3 | ||
@test it2[1] == @NT(b=1., c="A") | ||
@test it2[2] == @NT(b=2., c="B") | ||
@test it2[3] == @NT(b=3., c="C") | ||
|
||
it3 = IndexedTable(source_array, datacols=[:b, :c]) | ||
@test length(it3) == 3 | ||
@test it3[1] == @NT(b=1., c="A") | ||
@test it3[2] == @NT(b=2., c="B") | ||
@test it3[3] == @NT(b=3., c="C") | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! This could also use an optimized method when
x
isColumns
!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you should just be able to add another method that handles that case, right? It would be good if the named arguments had the same semantics, of course.
I'm also not sure this is the right API, I just was loosely inspired by this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, one more thing: we should also add a code path to this method that deals with an iterator where the element type is
Pair{X,S}
. If it is just anyPair
, it would create an unnamed index and data column. If eitherX
orX
are aNamedTuple
, it would create named columns for the index and data columns. At that point the following would automatically work:Not in this PR, but could be added later.