Skip to content

Commit

Permalink
Add tests, fix and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Jan 1, 2021
1 parent d2bef41 commit 35eacfa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/groupeddataframe/complextransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,30 @@ function _combine_rows_with_first_task!(tid::Integer,
lock(widen_type_lock)
try
newoutcols = outcolsref[]
# If another thread widened outcols, try again
# to see whether new eltype is wide enough
if newoutcols !== outcols
j = fill_row!(row, newoutcols, i, j, colnames)
end
while j !== nothing
# Workaround for julia#15276
newoutcols = let i=i, j=j, outcols=outcols, row=row, idx=idx
ntuple(length(outcols)) do k
S = typeof(row[k])
T = eltype(outcols[k])
U = promote_type(S, T)
if S <: T || U <: T
outcols[k]
else
copyto!(Tables.allocatecolumn(U, length(outcols[k])),
idx[1], outcols[k], idx[1], i - idx[1] + (k < j))
end
# Workaround for julia#15276
newoutcols = let i=i, j=j, newoutcols=newoutcols, row=row, idx=idx
ntuple(length(newoutcols)) do k
S = typeof(row[k])
T = eltype(newoutcols[k])
U = promote_type(S, T)
if S <: T || U <: T
newoutcols[k]
else
Tables.allocatecolumn(U, length(newoutcols[k]))
end
end
outcolsref[] = newoutcols
j = fill_row!(row, newoutcols, i, j, colnames)
type_widened .= true
type_widened[tid] = false
end
j = fill_row!(row, newoutcols, i, j, colnames)
@assert j === nothing # eltype is guaranteed to match
for k in 1:length(outcols)
if outcols[k] !== newoutcols[k]
copyto!(newoutcols[k], idx[1],
outcols[k], idx[1], i - idx[1] + 1)
end
end
outcolsref[] = newoutcols
type_widened .= true
type_widened[tid] = false
finally
unlock(widen_type_lock)
end
Expand All @@ -159,12 +158,15 @@ function _combine_rows_with_first_task!(tid::Integer,
type_widened[tid] = false
newoutcols = outcolsref[]
for k in 1:length(outcols)
copyto!(newoutcols[k], idx[1], outcols[k], idx[1], i - idx[1] + 1)
if outcols[k] !== newoutcols[k]
copyto!(newoutcols[k], idx[1],
outcols[k], idx[1], i - idx[1] + 1)
end
end
end
return _combine_rows_with_first_task!(tid, idx, newoutcols, outcolsref,
type_widened, widen_type_lock,
i, f, gd, starts, ends,
i+1, f, gd, starts, ends,
incols, colnames, firstmulticol)
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/grouping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3293,4 +3293,16 @@ end
end
end

@testset "result eltype widening from different tasks" begin
for df in (DataFrame(x=1:5, y=Any[1, missing, nothing, 2.0, 'a']),
DataFrame(x=1:9, y=Any[1, 1, missing, 1, nothing, 1, 2.0, 1, 'a']),
DataFrame(x=1:9, y=Any[1, 2, 3, 4, 5, 6, 2.0, missing, 'a']))
gd = groupby(df, :x)
@test combine(gd, :y => (y -> y[1]) => :y) df
# sleep ensures one task will widen the result after the other is done,
# so that data has to be copied at the end
@test combine(gd, [:x, :y] => ((x, y) -> (sleep(x == [5]); y[1])) => :y) df
end
end

end # module

0 comments on commit 35eacfa

Please sign in to comment.