From 74c5f836adda6c9888c1ab114d77885467861e43 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Tue, 10 Nov 2026 17:58:37 +0100 Subject: [PATCH] Preserve row order in `prepend!` with row iterators The generic fallback built the result with `foldl(pushfirst!, rows)`, which put each new row at the front, so rows came out in reverse order. Instead, collect the rows into an empty `similar(s, 0)` with `append!` and prepend that column by column. This keeps the same element conversion as `push!`. Fixes #371 Co-Authored-By: Claude Opus 5.5 --- src/tables.jl | 36 +++++++++++++++++++++++------------- test/runtests.jl | 15 +++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/tables.jl b/src/tables.jl index 432a75b2..416696a5 100644 --- a/src/tables.jl +++ b/src/tables.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 9ec78d51..bad13d78 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -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)