Skip to content

Preserve row order in prepend! with row iterators - #372

Merged
aplavin merged 1 commit into
masterfrom
kc/prepend-order
Nov 12, 2026
Merged

aplavin merged 1 commit into
masterfrom
kc/prepend-order

Conversation

@KristofferC

Copy link
Copy Markdown
Collaborator

Fixes #371.

If rows can't be added column by column, prepend!(s::StructVector, rows) falls back to foldl(pushfirst!, rows; init = s). That puts each row at the front, so multiple rows come out in reverse order:

s = StructArray(a=[1], b=[1.0])
prepend!(s, [(a=-1, b=-1.0), (a=0, b=0.0)])
s.a  # was [0, -1, 1], now [-1, 0, 1]

The fallback now collects the rows into similar(s, 0) with append! (same element conversion as push!) and prepends that column by column. append! itself is unchanged; it is just written out instead of being generated by the @eval loop shared with prepend!.

The existing tests only prepended one row. I added multi-row cases for NamedTuple and struct eltypes, a generator with element conversion, and an empty iterator. The new tests fail without the fix.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@aplavin

aplavin commented Nov 12, 2026

Copy link
Copy Markdown
Member

Looks like a clear bug fix, thank you!

@aplavin
aplavin merged commit 64f6748 into master Nov 12, 2026
8 of 10 checks passed
@aplavin
aplavin deleted the kc/prepend-order branch November 12, 2026 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prepend! with a row iterator inserts rows in reverse order

2 participants