Skip to content

Commit

Permalink
Allow any TableTraits source to satisfy the Tables interface (#45)
Browse files Browse the repository at this point in the history
* Allow any TableTraits source to satisfy the Tables interface

* Remove Manifest

* Add tests for new TableTraits integration
  • Loading branch information
quinnj authored Jan 8, 2019
1 parent 0d0ca87 commit 00f34cd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 47 deletions.
44 changes: 0 additions & 44 deletions Manifest.toml

This file was deleted.

2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ version = "0.1.9"

[deps]
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
IteratorInterfaceExtensions = "82899510-4779-5014-852e-03e436cf321d"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5 changes: 4 additions & 1 deletion src/Tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Tables

using Requires

using TableTraits, IteratorInterfaceExtensions

export rowtable, columntable

function __init__()
Expand Down Expand Up @@ -116,7 +118,8 @@ Obviously every table type is different, but via a combination of `Tables.rows`
abstract type Table end

# default definitions
istable(x::T) where {T} = istable(T)
istable(x::T) where {T} = istable(T) || TableTraits.isiterabletable(x) === true ||
TableTraits.isiterabletable(x) === missing
istable(::Type{T}) where {T} = false
rowaccess(x::T) where {T} = rowaccess(T)
rowaccess(::Type{T}) where {T} = false
Expand Down
14 changes: 14 additions & 0 deletions src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function rows(x::T) where {T}
cols = columns(x)
return RowIterator(cols, rowcount(cols))
else
it = TableTraits.isiterabletable(x)
if it === true || it === missing
return DataValueUnwrapper(IteratorInterfaceExtensions.getiterator(x))
end
throw(ArgumentError("no default `Tables.rows` implementation for type: $T"))
end
end
Expand Down Expand Up @@ -146,7 +150,17 @@ end
if rowaccess(T)
r = rows(x)
return buildcolumns(schema(r), r)
elseif TableTraits.supports_get_columns_copy_using_missing(x)
return TableTraits.get_columns_copy_using_missing(x)
else
it = TableTraits.isiterabletable(x)
y = IteratorInterfaceExtensions.getiterator(x)
if it === true
return columns(DataValueUnwrapper(y))
elseif it === missing
# non-NamedTuple or EltypeUnknown
return buildcolumns(nothing, DataValueUnwrapper(y))
end
throw(ArgumentError("no default `Tables.columns` implementation for type: $T"))
end
end
17 changes: 15 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Test, Tables
using Test, Tables, TableTraits

@testset "utils.jl" begin

Expand Down Expand Up @@ -223,6 +223,19 @@ end
@test sortperm([a, b, c, d]) == [3, 1, 2, 4]
end

struct ColumnSource
end

TableTraits.supports_get_columns_copy_using_missing(::ColumnSource) = true

function TableTraits.get_columns_copy_using_missing(x::ColumnSource)
return (a=[1,2,3], b=[4.,5.,6.], c=["A", "B", "C"])
end

let x=ColumnSource()
@test Tables.columns(x) == TableTraits.get_columns_copy_using_missing(x)
end

@static if :Query in Symbol.(Base.loaded_modules_array())
rt = (a = Real[1, 2.0, 3], b = Union{Missing, Float64}[4.0, missing, 6.0], c = ["7", "8", "9"])

Expand Down Expand Up @@ -250,4 +263,4 @@ end
rt = (a = Missing[missing, missing], b=[1,2])
dv = Tables.datavaluerows(rt)
@test eltype(dv) == NamedTuple{(:a, :b), Tuple{DataValue{Union{}}, Int}}
end
end

0 comments on commit 00f34cd

Please sign in to comment.