feat(builder): union columns + DEFAULT for mixed-shape InsertRecords#54
Merged
Conversation
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.
8fba979 to
bdaa64b
Compare
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.