Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,28 @@ end
try_compatible_columns(rows::StructArray{T}, s::StructArray{T}) where {T} = Tables.columntable(rows)
try_compatible_columns(rows::StructArray{R}, s::StructArray{S}) where {R,S} = nothing

for (f, g) in zip((:append!, :prepend!), (:push!, :pushfirst!))
@eval function Base.$f(s::StructVector, rows)
table = try_compatible_columns(rows, s)
if table !== nothing
# Input `rows` is a container of rows _and_ satisfies column
# table interface. Thus, we can add the input column-by-column.
foreachfield($f, s, table)
return s
else
# Otherwise, fallback to a generic implementation expecting
# that `rows` is an iterator:
return foldl($g, rows; init = s)
end
function Base.append!(s::StructVector, rows)
table = try_compatible_columns(rows, s)
if table !== nothing
# Input `rows` is a container of rows _and_ satisfies column
# table interface. Thus, we can add the input column-by-column.
foreachfield(append!, s, table)
return s
else
# Otherwise, fallback to a generic implementation expecting
# that `rows` is an iterator:
return foldl(push!, rows; init = s)
end
end

function Base.prepend!(s::StructVector, rows)
table = try_compatible_columns(rows, s)
if table !== nothing
foreachfield(prepend!, s, table)
return s
else
# Pushing each row to the front would reverse their order, so collect
# the rows into a temporary `StructVector` and prepend it column-wise.
return prepend!(s, append!(similar(s, 0), rows))
end
end
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ end

@test e == c == StructArray(a=[10,1], b=[20,2], c=["A","a"])

# multiple rows keep their order (#371)
c = StructArray(a=[1], b=[2], c=["a"])
e = prepend!(c, [(a=10, b=20, c="A"), (a=11, b=21, c="B")])
@test e == c == StructArray(a=[10,11,1], b=[20,21,2], c=["A","B","a"])
c = StructArray(a=[1], b=[2.0])
prepend!(c, ((a=i, b=i) for i in -2:0))
@test c == StructArray(a=[-2,-1,0,1], b=[-2.0,-1.0,0.0,2.0])
@test eltype(c.b) == Float64
c = StructArray(a=[1], b=[2])
@test prepend!(c, ()) == StructArray(a=[1], b=[2])

c = StructArray(a=[1,2,3], b=[1,4,6], c=["a","b","c"])
d = filter!(c) do el
return isodd(el.a) && iseven(el.b)
Expand Down Expand Up @@ -321,6 +332,10 @@ end

@test e == c == StructArray{C}(a=[10,1], b=[20,2], c=["A","a"])

c = StructArray{C}(a=[1], b=[2], c=["a"])
e = prepend!(c, [C(10, 20, "A"), C(11, 21, "B")])
@test e == c == StructArray{C}(a=[10,11,1], b=[20,21,2], c=["A","B","a"])

c = StructArray{C}(a=[1,2,3], b=[1,4,6], c=["a","b","c"])
d = filter!(c) do el
return isodd(el.a) && iseven(el.b)
Expand Down
Loading