feat(merge): MSSQL OUTPUT clause on MERGE / INSERT / UPDATE / DELETE - #157
Merged
Conversation
Wires up SQL Server's OUTPUT clause through the same MergeNode.returning
slot the PG path uses, so the typed MERGE builder works on MSSQL without
a separate surface. Bare column refs default to INSERTED.<col> (the
post-action row); pass an explicit col("name", "DELETED") for the
pre-action row. Adds mergeActionMssql() helper for $action — kept
distinct from PG's mergeAction() because the two compile to different
syntax (function call vs. pseudo-column).
Co-Authored-By: Claude Opus 4.7 (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.
Summary
OUTPUTclause through the sameMergeNode.returningslot the PG path uses (PR feat(merge): RETURNING on MERGE (PG 17+) #148 set upRETURNINGon MERGE for PG 17+; MSSQL previously threw viaMERGE_RETURNINGfeature flag). The MSSQL printer now rewrites the tail toOUTPUT <projections>instead.mergeActionMssql()builder helper for SQL Server's$actionpseudo-column — kept distinct from PG's existingmergeAction()(which compiles toMERGE_ACTION()) because the two emit different SQL shapes (function call vs. pseudo-column). Documented the dialect-divergence in JSDoc on both helpers.MERGE_RETURNINGfeature flag now includesmssql; the existing INSERT / UPDATE / DELETE OUTPUT paths got a small extension to respect explicitINSERTED/DELETEDtable qualifiers on column refs (soUPDATE OUTPUT INSERTED.id, DELETED.idworks without the auto-prefix double-wrapping the user's choice).Pseudo-table conventions on MERGE
col("id")→INSERTED.[id](post-action row — the common case).col("email", "DELETED")→[DELETED].[email](pre-action row).star()→INSERTED.*; passstar("DELETED")for the deleted set.mergeActionMssql()→ bare$actiontoken (no parens — it's a pseudo-column).Implementation notes
$actiontoken can't be a function call (validateFunctionNamerejects$prefixes) and arawnode would be flagged by thefindRawNodesaudit. We use a sentinel function name (__SUMAK_MSSQL_ACTION__) that the MSSQL printer recognizes and rewrites to$action; on other dialects the sentinel falls through and the engine errors at parse pointing at the offending call site (the desired "wrong but visible" behavior).printMergere-uses the base printer's MERGE skeleton (target / source / ON / WHENs / CTEs) and only adds theOUTPUT <cols>tail — no duplication of MERGE-statement logic.OUTPUT … INTO @var(writing to a table variable) — sumak doesn't model variable declarations.Test plan
MERGE … OUTPUT $action, INSERTED.[id]for the printer surface (both untyped and typed builder).RETURNING MERGE_ACTION(), "id", "name"still emits the same way; existing PGlite roundtrip passes.INSERT / UPDATE / DELETE OUTPUTpaths cover the mixedINSERTED + DELETEDqualifier case (UPDATE projecting both pre- and post-image).pnpm fmt && pnpm lint && pnpm typecheck && pnpm vitest runall green (2076 tests pass, no new lint warnings).🤖 Generated with Claude Code