Repository navigation
feat(analysis): analyze multi-row and SELECT-sourced inserts - #81
Merged
Merged
Conversation
An INSERT now accepts several VALUES rows, written inside parentheses at any depth, or a SELECT, with or without FROM, and pairs each value or item with the column list by position. A placeholder is typed and named by its target column, so INSERT ... SELECT :projectId, unnest(:userIds::bigint[]) compiles. The SELECT is analyzed as a nested read that sees no target, WITH clauses after the column list are analyzed, and both sources accept ON CONFLICT and RETURNING. Rows that are not parenthesized, DEFAULT VALUES, count mismatches, and set operations are rejected with diagnostics.
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
An
INSERTcan now write severalVALUESrows or the rows of aSELECT, with every placeholder typed from the column it is written into. Bulk inserts such asINSERT INTO memberships (project_id, user_id) SELECT :projectId, unnest(:userIds::bigint[])and multi-row inserts now compile without extra casts, where previously they were rejected.Changes
VALUESrows pair with the column list by position; a named placeholder repeated across rows is one parameter bound at each position.VALUESrows written inside parentheses at any depth are accepted.INSERT ... SELECT, parenthesized or not and with or withoutFROM, pairs its items with the column list by position. A placeholder item is typed and named by its target column, a cast item is named by its target column, direct columns resolve against theSELECT's own sources, and*/t.*count one item per source column.SELECTis analyzed like any other analyzed nested read: its joinON,WHERE,LIMIT, andOFFSETplaceholders are typed by their clause rules, CTEs and subquery sources are read, and the insert target is not in scope.WITHclauses written after the column list are analyzed, on aSELECTand on aVALUESsource, as well as before theINSERT.ON CONFLICTandRETURNINGare accepted on every accepted row source.INSERT column and value counts must match.for a row or aSELECTof the wrong width, andINSERT requires parenthesized VALUES rows or a SELECT.forVALUES ROW(...), a bare value element, andDEFAULT VALUES; a set operation as the row source keeps the set-operation diagnostic.Scope and non-goals
UPDATE ... FROMandDELETE ... USINGremain rejected.LATERAL, JDBC batch execution, andCOPYremain unsupported.RETURNINGitem rules, conflict-clause scopes, and the rules of other nestedSELECTs are unchanged; a scalar subquery in an insertSELECTitem is still typed only by casts.