feat(builder): union columns + DEFAULT for mixed-shape InsertRecords - #54
Merged
Conversation
klaidliadon
force-pushed
the
fix-insertrecords-mixed-shape
branch
from
June 10, 2026 12:31
006a90f to
8fba979
Compare
Closes #53. Follow-up to #50. Replace the InsertRecords column-drift check with union-by-name: walk rows once to compute the column union and a per-row map of present columns, then emit Columns(allCols...) with each row padded to the union via DEFAULT where the row skipped a column. The per-row empty-record rejection from #50 goes away because an empty row can be all-DEFAULT when another row contributes the union. Only the whole-batch empty case still errors, now pointing callers at SQL.InsertDefaults in a loop after rebasing over the dedicated DEFAULT VALUES builder. Tests cover uniform and mixed shapes, omitzero slices, mixed maps, empty rows with a non-empty union, all-empty rejection, map records, InsertDefaults SQL construction, and PG roundtrips for both InsertDefaults and mixed-shape inserts.
klaidliadon
force-pushed
the
fix-insertrecords-mixed-shape
branch
from
June 10, 2026 12:32
8fba979 to
bdaa64b
Compare
VojtechVitek
added a commit
that referenced
this pull request
Aug 2, 2026
insertAll relied on GetAll rewriting the slice backing array, so DB-generated ids/defaults reached the caller only when they spread a slice (Insert(ctx, recs...)); passing individual records (Insert(ctx, a, b)) left them at their zero values, unlike a single Insert and unlike Save. Preserve the original pointers and copy the DB-returned values back into them after GetAll (RETURNING *), matching insertOne/GetOne and saveAll. Fixes the populate-back half of #24; the SQLSTATE 42601 half was already fixed by PR #54 (union + DEFAULT). Verified by TestInsertMultiPopulatesRecords_Issue24 (red in the previous commit, green now) and the full Postgres-backed suite. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Aug 2, 2026
klaidliadon
pushed a commit
that referenced
this pull request
Aug 4, 2026
* test(table): failing regression for issue #24 batch insert populate-back Passing records to Table.Insert as individual variadic args (not a spread slice) does not populate the caller's records with DB-generated ids/defaults on a multi-record insert, unlike a single Insert and unlike Save. insertAll relies on GetAll rewriting the slice backing array, which only reflects back to the caller when they spread a slice. This test reproduces #24 end to end against Postgres. The SQLSTATE 42601 part is already fixed (PR #54 union + DEFAULT); the populate-back assertions fail. The fix follows next commit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(table): populate caller records on multi-record Insert insertAll relied on GetAll rewriting the slice backing array, so DB-generated ids/defaults reached the caller only when they spread a slice (Insert(ctx, recs...)); passing individual records (Insert(ctx, a, b)) left them at their zero values, unlike a single Insert and unlike Save. Preserve the original pointers and copy the DB-returned values back into them after GetAll (RETURNING *), matching insertOne/GetOne and saveAll. Fixes the populate-back half of #24; the SQLSTATE 42601 half was already fixed by PR #54 (union + DEFAULT). Verified by TestInsertMultiPopulatesRecords_Issue24 (red in the previous commit, green now) and the full Postgres-backed suite. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#50 rejected any batch whose rows produced different
Mapcolumn sets —,omitzeroslice /,omitemptymap records that mixniland non-nilempty values hit this constantly, forcing callers to split into per-rowInsertRecordcalls. Closes #53.PostgreSQL accepts
DEFAULTin anyVALUESposition. The fix: union the cols across rows, emitsq.Expr("DEFAULT")for any slot a row skipped.Design
Drafted via three Codex review rounds. Plan at
tmp/insertrecords-mixed-shape/2026-06-02-plan.md(branch-only).Key inflection points the reviews forced:
(DEFAULT, DEFAULT, ...). Only the whole-batch empty union should error.map[string]anyper row.SQL.InsertDefaultsin the whole-batch-empty error — but that API is on the unmerged feat(builder): InsertDefaults for INSERT ... DEFAULT VALUES #52 branch. v3 hints atsq.Expr(matches feat(mapper): support ,omitzero tag option #50's existing single-row hint) so InsertRecords: union columns + emit DEFAULT for missing, instead of rejecting mixed-shape batches #53 stays independent of feat(builder): InsertDefaults for INSERT ... DEFAULT VALUES #52. Whichever lands second updates its own hint.Reuses
sqlDefault = sq.Expr("DEFAULT")frommapper.go:26.slices.Sorted(maps.Keys(colSet))matchesMap's lexical column order atmapper.go:161, so generated SQL lines up with what callers see fromMap(record)directly.Behavior table
Test plan
make db-reset test-allagainst PostgreSQL 18.3 — all 6 packages green.argsassertion), mixed shape withDEFAULTslots in the right places,,omitzeromixed slices,,omitemptymixed maps, empty row mixed with non-empty, all-rows-empty rejection, heterogeneousmap[string]anyrecords.TestInsertRecordsMixedShapeRoundTrip: exec a three-row heterogeneous batch against a new dedicatedmixed_shapetable (nullable + defaulted columns), verify each row landed with the right mix of caller values / DB defaults / NULLs.go vet ./...clean.Notes
,omitemptysemantics preserved: rows where,omitemptyskipped a column (e.g.[]string{}on a slice field) getDEFAULTin the union slot, not the empty literal. The user tagged the column for "use DB default when missing" — that contract still holds, just now inside a batch.SQL.InsertDefaultsinstead ofsq.Expr. Independent edit, no merge ordering required.