Skip to content

Commit fabe199

Browse files
committed
Rearrange some definitions to make DataValues/Enumerables work better
1 parent a8060ec commit fabe199

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

src/Tables.jl

+23-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function __init__()
2222
allocatecolumn(::Type{WeakRefString{T}}, rows) where {T} = StringVector(rows)
2323
allocatecolumn(::Type{Union{Missing, WeakRefString{T}}}, rows) where {T} = StringVector{Union{Missing, String}}(rows)
2424
end
25+
@require IteratorInterfaceExtensions="82899510-4779-5014-852e-03e436cf321d" begin
26+
using .IteratorInterfaceExtensions
27+
IteratorInterfaceExtensions.getiterator(x::RowTable) = datavaluerows(x)
28+
IteratorInterfaceExtensions.isiterable(x::RowTable) = true
29+
IteratorInterfaceExtensions.getiterator(x::ColumnTable) = datavaluerows(x)
30+
IteratorInterfaceExtensions.isiterable(x::ColumnTable) = true
31+
end
2532
end
2633

2734
include("utils.jl")
@@ -89,11 +96,11 @@ struct ColumnAccess <: AccessStyle end
8996
"Tables.schema(s) => NamedTuple{names, types}"
9097
function schema end
9198

92-
function istable(x::T) where {T}
93-
hasmethod(AccessStyle, Tuple{T}) &&
94-
hasmethod(schema, Tuple{T}) &&
95-
AccessStyle(T) === RowAccess() ? hasmethod(rows, Tuple{T}) :
96-
AccessStyle(T) === ColumnAccess() ? hasmethod(columns, Tuple{T}) : false
99+
function istable(::Type{T}) where {T}
100+
hasmethod(Tables.AccessStyle, Tuple{T}) &&
101+
hasmethod(Tables.schema, Tuple{T}) &&
102+
(Tables.AccessStyle(T) === Tables.RowAccess() ? hasmethod(Tables.rows, Tuple{T}) :
103+
Tables.AccessStyle(T) === Tables.ColumnAccess() ? hasmethod(Tables.columns, Tuple{T}) : false)
97104
end
98105

99106
include("namedtuples.jl")
@@ -154,17 +161,19 @@ end
154161
@inline add!(val, col::Int, nm::Symbol, ::Base.HasLength, nt, row) = setindex!(nt[col], val, row)
155162
@inline add!(val, col::Int, nm::Symbol, T, nt, row) = push!(nt[col], val)
156163

164+
@inline function buildcolumns(sch, rowitr::T) where {T}
165+
L = Base.IteratorSize(T)
166+
len = L == Base.HasLength() ? length(rowitr) : 0
167+
nt = allocatecolumns(sch, len)
168+
for (i, row) in enumerate(rowitr)
169+
unroll(add!, sch, row, L, nt, i)
170+
end
171+
return nt
172+
end
173+
157174
@inline function columns(x::T) where {T}
158175
if AccessStyle(T) === RowAccess()
159-
sch = schema(x)
160-
rowitr = rows(x)
161-
L = Base.IteratorSize(typeof(rowitr))
162-
len = L == Base.HasLength() ? length(rowitr) : 0
163-
nt = allocatecolumns(sch, len)
164-
for (i, row) in enumerate(rowitr)
165-
unroll(add!, sch, row, L, nt, i)
166-
end
167-
return nt
176+
return buildcolumns(schema(x), rows(x))
168177
elseif AccessStyle(T) === ColumnAccess()
169178
return x
170179
else

src/datavalues.jl

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
using .DataValues
22

33
# DataValue-compatible row iteration for Data.Sources
4+
nondatavaluetype(::Type{DataValue{T}}) where {T} = Union{T, Missing}
5+
nondatavaluetype(::Type{T}) where {T} = T
6+
Base.@pure function nondatavaluetype(::Type{NT}) where {NT <: NamedTuple{names}} where {names}
7+
TT = Tuple{Any[ nondatavaluetype(fieldtype(NT, i)) for i = 1:fieldcount(NT) ]...}
8+
return NamedTuple{names, TT}
9+
end
10+
11+
unwrap(x) = x
12+
unwrap(x::DataValue) = isna(x) ? missing : DataValues.unsafe_get(x)
13+
414
datavaluetype(::Type{T}) where {T <: DataValue} = T
515
datavaluetype(::Type{T}) where {T} = DataValue{T}
616
datavaluetype(::Type{Union{T, Missing}}) where {T} = DataValue{T}

src/enumerable.jl

-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
using .QueryOperators
22

3-
nondatavaluetype(::Type{DataValue{T}}) where {T} = Union{T, Missing}
4-
nondatavaluetype(::Type{T}) where {T} = T
5-
6-
Base.@pure function nondatavaluetype(::Type{NT}) where {NT <: NamedTuple{names}} where {names}
7-
TT = Tuple{Any[ nondatavaluetype(fieldtype(NT, i)) for i = 1:fieldcount(NT) ]...}
8-
return NamedTuple{names, TT}
9-
end
10-
113
struct DataValueUnwrapRow{T}
124
row::T
135
end
146

15-
unwrap(x) = x
16-
unwrap(x::DataValue) = isna(x) ? missing : DataValues.unsafe_get(x)
177
Base.getproperty(d::DataValueUnwrapRow, ::Type{T}, col::Int, nm::Symbol) where {T} = unwrap(getproperty(getfield(d, 1), T, col, nm))
188
Base.getproperty(d::DataValueUnwrapRow, nm::Symbol) = unwrap(getproperty(getfield(d, 1), nm))
199
Base.propertynames(d::DataValueUnwrapRow) = propertynames(getfield(d, 1))
@@ -36,10 +26,3 @@ function Base.iterate(rows::DataValueUnwrapper{NT}, st=()) where {NT <: NamedTup
3626
row, st = x
3727
return DataValueUnwrapRow(row), (st,)
3828
end
39-
40-
using IteratorInterfaceExtensions
41-
42-
IteratorInterfaceExtensions.getiterator(x::RowTable) = datavaluerows(x)
43-
IteratorInterfaceExtensions.isiterable(x::RowTable) = true
44-
IteratorInterfaceExtensions.getiterator(x::ColumnTable) = datavaluerows(x)
45-
IteratorInterfaceExtensions.isiterable(x::ColumnTable) = true

0 commit comments

Comments
 (0)