Skip to content

Commit 950712d

Browse files
authored
Various code cleanups; method reductions, removing unused code, etc. Mostly cherry-picked from #92 (#95)
1 parent 5eced11 commit 950712d

File tree

5 files changed

+21
-35
lines changed

5 files changed

+21
-35
lines changed

src/Tables.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ function __init__()
4141
allocatecolumn(::Type{WeakRefString{T}}, rows) where {T} = StringVector(undef, rows)
4242
allocatecolumn(::Type{Union{Missing, WeakRefString{T}}}, rows) where {T} =
4343
StringVector{Union{Missing, String}}(undef, rows)
44-
unweakref(wk::WeakRefString) = string(wk)
45-
unweakreftype(::Type{<:WeakRefString}) = String
4644
end
4745
end
4846

src/fallbacks.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,12 @@ function rows(x::T) where {T}
5959
cols = columns(x)
6060
return RowIterator(cols, rowcount(cols))
6161
elseif IteratorInterfaceExtensions.isiterable(x)
62-
return IteratorWrapper(IteratorInterfaceExtensions.getiterator(x))
62+
return nondatavaluerows(x)
6363
end
6464
throw(ArgumentError("no default `Tables.rows` implementation for type: $T"))
6565
end
6666

6767
# build columns from rows
68-
haslength(L) = L isa Union{Base.HasShape, Base.HasLength}
69-
7068
"""
7169
Tables.allocatecolumn(::Type{T}, len) => returns a column type (usually AbstractVector) w/ size to hold `len` elements
7270
@@ -89,7 +87,7 @@ end
8987

9088
@inline function buildcolumns(schema, rowitr::T) where {T}
9189
L = Base.IteratorSize(T)
92-
len = haslength(L) ? length(rowitr) : 0
90+
len = Base.haslength(L) ? length(rowitr) : 0
9391
nt = allocatecolumns(schema, len)
9492
for (i, row) in enumerate(rowitr)
9593
eachcolumn(add!, schema, row, L, nt, i)
@@ -125,7 +123,7 @@ function buildcolumns(::Nothing, rowitr::T) where {T}
125123
row, st = state
126124
names = Tuple(propertynames(row))
127125
L = Base.IteratorSize(T)
128-
len = haslength(L) ? length(rowitr) : 0
126+
len = Base.haslength(L) ? length(rowitr) : 0
129127
sch = Schema(names, nothing)
130128
columns = NamedTuple{names}(Tuple(Union{}[] for _ = 1:length(names)))
131129
return _buildcolumns(rowitr, row, st, sch, L, columns, 1, len, Ref{Any}(columns))
@@ -163,7 +161,7 @@ Base.getproperty(x::CopiedColumns, nm::Symbol) = getproperty(source(x), nm)
163161
elseif TableTraits.supports_get_columns_copy_using_missing(x)
164162
return CopiedColumns(TableTraits.get_columns_copy_using_missing(x))
165163
elseif IteratorInterfaceExtensions.isiterable(x)
166-
iw = IteratorWrapper(IteratorInterfaceExtensions.getiterator(x))
164+
iw = nondatavaluerows(x)
167165
return CopiedColumns(buildcolumns(schema(iw), iw))
168166
end
169167
throw(ArgumentError("no default `Tables.columns` implementation for type: $T"))

src/namedtuples.jl

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# Vector of NamedTuples
22
const RowTable{T} = Vector{T} where {T <: NamedTuple}
33

4-
# RowTables can't support WeakRefStrings
5-
unweakref(x) = x
6-
unweakreftype(T) = T
7-
Base.@pure unweakreftypes(::Type{T}) where {T <: Tuple} = Tuple{Any[unweakreftype(fieldtype(T, i)) for i = 1:fieldcount(T)]...}
8-
94
# interface implementation
105
istable(::Type{<:RowTable}) = true
116
rowaccess(::Type{<:RowTable}) = true
@@ -19,25 +14,25 @@ struct NamedTupleIterator{S, T}
1914
x::T
2015
end
2116
Base.IteratorEltype(::Type{<:NamedTupleIterator{S}}) where {S} = S === nothing ? Base.EltypeUnknown() : Base.HasEltype()
22-
Base.eltype(rows::NamedTupleIterator{Schema{names, T}}) where {names, T} = NamedTuple{names, unweakreftypes(T)}
17+
Base.eltype(rows::NamedTupleIterator{Schema{names, T}}) where {names, T} = NamedTuple{names, T}
2318
Base.IteratorSize(::Type{NamedTupleIterator{S, T}}) where {S, T} = Base.IteratorSize(T)
2419
Base.length(nt::NamedTupleIterator) = length(nt.x)
2520
Base.size(nt::NamedTupleIterator) = (length(nt.x),)
2621

2722
function Base.iterate(rows::NamedTupleIterator{Schema{names, T}}, st=()) where {names, T}
2823
if @generated
29-
vals = Tuple(:(unweakref(getproperty(row, $(fieldtype(T, i)), $i, $(Meta.QuoteNode(names[i]))))) for i = 1:fieldcount(T))
24+
vals = Tuple(:(getproperty(row, $(fieldtype(T, i)), $i, $(Meta.QuoteNode(names[i])))) for i = 1:fieldcount(T))
3025
return quote
3126
x = iterate(rows.x, st...)
3227
x === nothing && return nothing
3328
row, st = x
34-
return $(NamedTuple{names, unweakreftypes(T)})(($(vals...),)), (st,)
29+
return $(NamedTuple{names, T})(($(vals...),)), (st,)
3530
end
3631
else
3732
x = iterate(rows.x, st...)
3833
x === nothing && return nothing
3934
row, st = x
40-
return NamedTuple{names, unweakreftypes(T)}(Tuple(unweakref(getproperty(row, fieldtype(T, i), i, names[i])) for i = 1:fieldcount(T))), (st,)
35+
return NamedTuple{names, T}(Tuple(getproperty(row, fieldtype(T, i), i, names[i]) for i = 1:fieldcount(T))), (st,)
4136
end
4237
end
4338

@@ -47,7 +42,7 @@ function Base.iterate(rows::NamedTupleIterator{Nothing, T}, st=()) where {T}
4742
x === nothing && return nothing
4843
row, st = x
4944
names = Tuple(propertynames(row))
50-
return NamedTuple{names}(Tuple(unweakref(getproperty(row, nm)) for nm in names)), (st,)
45+
return NamedTuple{names}(Tuple(getproperty(row, nm) for nm in names)), (st,)
5146
end
5247

5348
namedtupleiterator(::Type{T}, rows::S) where {T <: NamedTuple, S} = rows
@@ -110,7 +105,7 @@ function ctappend(ct1::NamedTuple{N1, T1}, ct2::NamedTuple{N2, T2}) where {N1, T
110105
return ct1
111106
end
112107
else
113-
foreach(nm->append!(ct1[nm], ct2[nm]), nms)
108+
foreach(nm->append!(ct1[nm], ct2[nm]), N1)
114109
return ct1
115110
end
116111
end

src/tofromdatavalues.jl

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ nondatavaluetype(::Type{Union{}}) = Union{}
55
datavaluetype(::Type{T}) where {T} = T
66
datavaluetype(::Type{Union{}}) = Union{}
77

8-
Base.@pure function nondatavaluetype(::Type{NT}) where {NT <: NamedTuple{names}} where {names}
8+
Base.@pure function nondatavaluenamedtuple(::Type{NT}) where {NT <: NamedTuple{names}} where {names}
99
TT = Tuple{Any[ nondatavaluetype(fieldtype(NT, i)) for i = 1:fieldcount(NT) ]...}
1010
return NamedTuple{names, TT}
1111
end
1212

13-
Base.@pure function datavaluetype(::Tables.Schema{names, types}) where {names, types}
13+
Base.@pure function datavaluenamedtuple(::Tables.Schema{names, types}) where {names, types}
1414
TT = Tuple{Any[ datavaluetype(fieldtype(types, i)) for i = 1:fieldcount(types) ]...}
1515
return NamedTuple{names, TT}
1616
end
@@ -24,16 +24,18 @@ struct IteratorWrapper{S}
2424
x::S
2525
end
2626

27-
nondatavaluerows(x) = IteratorWrapper(x)
27+
nondatavaluerows(x) = IteratorWrapper(IteratorInterfaceExtensions.getiterator(x))
2828
Tables.istable(::Type{<:IteratorWrapper}) = true
2929
Tables.rowaccess(::Type{<:IteratorWrapper}) = true
3030
Tables.rows(x::IteratorWrapper) = x
3131

3232
function Tables.schema(dv::IteratorWrapper)
3333
eT = eltype(dv.x)
3434
(!(eT <: NamedTuple) || eT === Union{}) && return nothing
35-
return Tables.Schema(nondatavaluetype(eT))
35+
return Tables.Schema(nondatavaluenamedtuple(eT))
3636
end
37+
38+
Base.IteratorEltype(::Type{IteratorWrapper{S}}) where {S} = Base.IteratorEltype(S)
3739
Base.eltype(rows::IteratorWrapper) = IteratorRow{eltype(rows.x)}
3840
Base.IteratorSize(::Type{IteratorWrapper{S}}) where {S} = Base.IteratorSize(S)
3941
Base.length(rows::IteratorWrapper) = length(rows.x)
@@ -67,23 +69,16 @@ Base.propertynames(d::IteratorRow) = propertynames(getfield(d, 1))
6769
struct DataValueRowIterator{NT, S}
6870
x::S
6971
end
70-
DataValueRowIterator(::Type{NT}, x::S) where {NT <: NamedTuple, S} = DataValueRowIterator{NT, S}(x)
71-
72-
"Returns a DataValue-based NamedTuple-iterator"
73-
DataValueRowIterator(::Type{Schema{names, types}}, x::S) where {names, types, S} = DataValueRowIterator{datavaluetype(NamedTuple{names, types}), S}(x)
7472

7573
function datavaluerows(x)
7674
r = Tables.rows(x)
7775
s = Tables.schema(r)
7876
s === nothing && error("Schemaless sources cannot be passed to datavaluerows.")
79-
return DataValueRowIterator(datavaluetype(s), r)
77+
return DataValueRowIterator{datavaluenamedtuple(s), typeof(r)}(r)
8078
end
8179

82-
_iteratorsize(x) = x
83-
_iteratorsize(::Base.HasShape{1}) = Base.HasLength()
84-
8580
Base.eltype(rows::DataValueRowIterator{NT, S}) where {NT, S} = NT
86-
Base.IteratorSize(::Type{DataValueRowIterator{NT, S}}) where {NT, S} = _iteratorsize(Base.IteratorSize(S))
81+
Base.IteratorSize(::Type{DataValueRowIterator{NT, S}}) where {NT, S} = Base.IteratorSize(S)
8782
Base.length(rows::DataValueRowIterator) = length(rows.x)
8883

8984
function Base.iterate(rows::DataValueRowIterator{NT, S}, st=()) where {NT <: NamedTuple{names}, S} where {names}

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,19 @@ end
409409
rt2 = collect(dv)
410410
@test rt2[1] == (a = 1, b = DataValue{Float64}(4.0), c = "7")
411411

412-
ei = Tables.IteratorWrapper(QueryOperators.EnumerableIterable{eltype(dv), typeof(dv)}(dv))
412+
ei = Tables.nondatavaluerows(QueryOperators.EnumerableIterable{eltype(dv), typeof(dv)}(dv))
413413
nt = ei |> columntable
414414
@test isequal(rt, nt)
415415
rt3 = ei |> rowtable
416416
@test isequal(rt |> rowtable, rt3)
417417

418418
# rt = [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8"), (a=3, b=6.0, c="9")]
419-
mt = Tables.IteratorWrapper(ei.x |> y->QueryOperators.map(y, x->(a=x.b, c=x.c), Expr(:block)))
419+
mt = Tables.nondatavaluerows(ei.x |> y->QueryOperators.map(y, x->(a=x.b, c=x.c), Expr(:block)))
420420
@inferred (mt |> columntable)
421421
@inferred (mt |> rowtable)
422422

423423
# uninferrable case
424-
mt = Tables.IteratorWrapper(ei.x |> y->QueryOperators.map(y, x->(a=x.a, c=x.c), Expr(:block)))
424+
mt = Tables.nondatavaluerows(ei.x |> y->QueryOperators.map(y, x->(a=x.a, c=x.c), Expr(:block)))
425425
@test (mt |> columntable) == (a = Real[1, 2.0, 3], c = ["7", "8", "9"])
426426
@test length(mt |> rowtable) == 3
427427

0 commit comments

Comments
 (0)