Skip to content

Commit

Permalink
Switch to use promote_type instead of promote_typejoin (#18)
Browse files Browse the repository at this point in the history
* Switch to use promote_type instead of promote_typejoin

* Remove unneeded Base. prefix

* Adjust tests to ensure correct behavior
  • Loading branch information
quinnj authored Sep 22, 2018
1 parent 1f5f724 commit 1e2e6cd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ end
@inline add!(dest::AbstractVector, val, T, row) = push!(dest, val)

@inline function add_or_widen!(dest::AbstractVector{T}, val::S, nm::Symbol, L, row, len, updated) where {T, S}
if S === T || val isa T
if S === T || promote_type(S, T) <: T
add!(dest, val, L, row)
return dest
else
new = allocatecolumn(Base.promote_typejoin(T, S), max(len, length(dest)))
new = allocatecolumn(promote_type(T, S), max(len, length(dest)))
row > 1 && copyto!(new, 1, dest, 1, row - 1)
add!(new, val, L, row)
updated[] = merge(updated[], NamedTuple{(nm,)}((new,)))
Expand Down
2 changes: 1 addition & 1 deletion src/namedtuples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function Base.iterate(rows::NamedTupleIterator{Nothing, T}, st=()) where {T}
x = iterate(rows.x, st...)
x === nothing && return nothing
row, st = x
names = propertynames(row)
names = Tuple(propertynames(row))
return NamedTuple{names}(Tuple(getproperty(row, nm) for nm in names)), (st,)
end

Expand Down
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ end
@test length(rt2) == 9

rt = [(a=1, b=4.0, c="7"), (a=2.0, b=missing, c="8"), (a=3, b=6.0, c="9")]
@test isequal(Tables.buildcolumns(nothing, rt), (a = Real[1, 2.0, 3], b = Union{Missing, Float64}[4.0, missing, 6.0], c = ["7", "8", "9"]))
tt = Tables.buildcolumns(nothing, rt)
@test isequal(tt, (a = [1.0, 2.0, 3.0], b = Union{Missing, Float64}[4.0, missing, 6.0], c = ["7", "8", "9"]))
@test tt.a[1] === 1.0
@test tt.a[2] === 2.0
@test tt.a[3] === 3.0

nti = Tables.NamedTupleIterator{Nothing, typeof(rt)}(rt)
nti2 = collect(nti)
Expand Down

0 comments on commit 1e2e6cd

Please sign in to comment.