feat(builder): union columns + DEFAULT for mixed-shape InsertRecords#54
Open
klaidliadon wants to merge 1 commit into
Open
feat(builder): union columns + DEFAULT for mixed-shape InsertRecords#54klaidliadon wants to merge 1 commit into
klaidliadon wants to merge 1 commit into
Conversation
Closes #53. Follow-up to #50. #50 rejected any batch whose rows produced different Map column sets, with a build-time "record N columns differ from record 0" error. That was the conservative answer: it stopped squirrel from emitting malformed multi-row SQL where row widths didn't match. But it's restrictive. Realistic ,omitzero (#50) and legacy ,omitempty on map fields produce heterogeneous batches constantly. Forcing callers to split into per-row InsertRecord calls kills the point of batching. Replace the drift check with union-by-name: walk rows once to compute the column union and a per-row map[string]any of present columns, then emit Columns(allCols...) with each row padded to the union via sq.Expr("DEFAULT") in any slot the row skipped. PostgreSQL accepts DEFAULT in any VALUES position, so the resulting SQL is valid for every shape the union covers. The per-row "empty record" rejection from #50 also goes away — a row with no own columns can be all-DEFAULT *precisely because* another row contributes the union. Only the whole-batch empty case still errors, with a hint pointing at sq.Expr (matches the existing #50 hint shape on the InsertRecord single-row path). Tests: rewrote the drift-rejection tests to assert union+DEFAULT SQL including args; added empty-row-mixed-with-non-empty, heterogeneous map records, and a real PG round-trip against a new mixed_shape table that proves each row lands with the right mix of caller values, DB defaults, and NULLs. Planned via three Codex review rounds (per-row vs whole-batch reject flipped, map-padding by name vs positional, test plan gaps, sq.Expr hint vs unmerged #52 reference). Plan at tmp/insertrecords-mixed-shape/2026-06-02-plan.md.
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.