Skip to content

Commit 568ebe5

Browse files
committed
fix(sqlite): Support multiple ON CONFLICT clauses
1 parent 3ad8b20 commit 568ebe5

10 files changed

Lines changed: 97 additions & 23 deletions

File tree

‎internal/core/analyzer/dml.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,56 @@ func (a *analyzer) analyzeInsert(s *ast.InsertStmt) error {
2424
if err := a.bindInsertValues(s.SelectStmt, rel, targets); err != nil {
2525
return err
2626
}
27+
for _, clause := range s.OnConflictClauses {
28+
if err := a.bindOnConflict(rel, clause); err != nil {
29+
return err
30+
}
31+
}
32+
a.scope = &scope{rels: []scopeRel{rel}}
2733
return a.projectReturning(s.ReturningList)
2834
}
2935

36+
// bindOnConflict binds the placeholders of an ON CONFLICT clause: those in the
37+
// WHERE of its conflict target, and those in the SET and WHERE of a DO UPDATE.
38+
// Inside the clause the row that failed to insert is visible as EXCLUDED.
39+
func (a *analyzer) bindOnConflict(target scopeRel, clause *ast.OnConflictClause) error {
40+
if clause == nil {
41+
return nil
42+
}
43+
excluded := target
44+
excluded.alias = "excluded"
45+
excluded.qualifiedOnly = true
46+
a.scope = &scope{rels: []scopeRel{target, excluded}}
47+
48+
if clause.Infer != nil && clause.Infer.WhereClause != nil {
49+
if _, err := a.typeExpr(clause.Infer.WhereClause); err != nil {
50+
return fmt.Errorf("on conflict where: %w", err)
51+
}
52+
}
53+
if clause.Action != ast.OnConflictActionUpdate {
54+
return nil
55+
}
56+
for _, item := range listItems(clause.TargetList) {
57+
rt, ok := item.(*ast.ResTarget)
58+
if !ok || rt.Name == nil {
59+
continue
60+
}
61+
col, ok := findColumn(target, *rt.Name)
62+
if !ok {
63+
return fmt.Errorf("unknown column %q", *rt.Name)
64+
}
65+
if err := a.bindValue(target, &col, rt.Val); err != nil {
66+
return fmt.Errorf("on conflict set %s: %w", *rt.Name, err)
67+
}
68+
}
69+
if clause.WhereClause != nil {
70+
if _, err := a.typeExpr(clause.WhereClause); err != nil {
71+
return fmt.Errorf("on conflict where: %w", err)
72+
}
73+
}
74+
return nil
75+
}
76+
3077
func (a *analyzer) analyzeUpdate(s *ast.UpdateStmt) error {
3178
if err := a.bindCTEs(s.WithClause); err != nil {
3279
return err

‎internal/core/analyzer/scope.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type scope struct {
1414
type scopeRel struct {
1515
alias string
1616
classOID int64
17+
// qualifiedOnly hides the relation from unqualified column references.
18+
// The EXCLUDED row of an ON CONFLICT DO UPDATE shares every column name
19+
// with the target table, and a bare name there means the target's.
20+
qualifiedOnly bool
1721
// cols is the catalog's column list, held as-is rather than copied into a
1822
// scope-local column type.
1923
cols []core.ClassColumn
@@ -234,6 +238,9 @@ func (s *scope) resolveColumn(relation, column string) (rel scopeRel, col core.C
234238
if relation != "" && r.alias != relation {
235239
continue
236240
}
241+
if relation == "" && r.qualifiedOnly {
242+
continue
243+
}
237244
for _, c := range r.cols {
238245
if c.Name != column {
239246
continue

‎internal/engine/duckdb/convert.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ func (c *cc) convertInsertStatement(n *dw.InsertStatement) ast.Node {
910910
}
911911

912912
if n.OnConflict != nil {
913-
stmt.OnConflictClause = c.convertOnConflict(n.OnConflict)
913+
stmt.OnConflictClauses = []*ast.OnConflictClause{c.convertOnConflict(n.OnConflict)}
914914
}
915915

916916
stmt.ReturningList = c.convertReturning(n.Returning)

‎internal/engine/postgresql/convert.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,7 +1827,7 @@ func convertInsertStmt(n *pg.InsertStmt) *ast.InsertStmt {
18271827
Relation: convertRangeVar(n.Relation),
18281828
Cols: convertSlice(n.Cols),
18291829
SelectStmt: convertNode(n.SelectStmt),
1830-
OnConflictClause: convertOnConflictClause(n.OnConflictClause),
1830+
OnConflictClauses: convertOnConflictClauses(n.OnConflictClause),
18311831
ReturningList: convertSlice(n.ReturningClause.GetExprs()),
18321832
ReturningOldAlias: oldAlias,
18331833
ReturningNewAlias: newAlias,
@@ -2028,6 +2028,15 @@ func convertObjectWithArgs(n *pg.ObjectWithArgs) *ast.ObjectWithArgs {
20282028
}
20292029
}
20302030

2031+
// convertOnConflictClauses wraps PostgreSQL's single ON CONFLICT clause in the
2032+
// slice ast.InsertStmt keeps for SQLite's sake.
2033+
func convertOnConflictClauses(n *pg.OnConflictClause) []*ast.OnConflictClause {
2034+
if n == nil {
2035+
return nil
2036+
}
2037+
return []*ast.OnConflictClause{convertOnConflictClause(n)}
2038+
}
2039+
20312040
func convertOnConflictClause(n *pg.OnConflictClause) *ast.OnConflictClause {
20322041
if n == nil {
20332042
return nil

‎internal/engine/sqlite/convert.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ func (c *cc) convertInsertStmt(n *meyer.InsertStmt) ast.Node {
463463
insert.SelectStmt = sel
464464
}
465465
}
466-
if len(n.Upserts) > 0 {
467-
insert.OnConflictClause = c.convertUpsert(n.Upserts[0])
466+
for _, u := range n.Upserts {
467+
insert.OnConflictClauses = append(insert.OnConflictClauses, c.convertUpsert(u))
468468
}
469469
return insert
470470
}

‎internal/sql/ast/insert_stmt.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ import "github.com/sqlc-dev/sqlc/internal/sql/format"
55
type InsertStmt struct {
66
Tag NodeTag[InsertStmt] `json:"tag"`
77

8-
Relation *RangeVar `json:"relation,omitempty"`
9-
Cols *List `json:"cols,omitempty"`
10-
SelectStmt Node `json:"select_stmt,omitempty"`
11-
OnConflictClause *OnConflictClause `json:"on_conflict_clause,omitempty"`
8+
Relation *RangeVar `json:"relation,omitempty"`
9+
Cols *List `json:"cols,omitempty"`
10+
SelectStmt Node `json:"select_stmt,omitempty"`
11+
// OnConflictClauses holds the upsert clauses in the order they appear.
12+
// SQLite (3.35+) allows several ON CONFLICT clauses on one INSERT; every
13+
// other engine allows at most one, so for them the slice has zero or one
14+
// element.
15+
OnConflictClauses []*OnConflictClause `json:"on_conflict_clauses,omitempty"`
1216
OnDuplicateKeyUpdate *OnDuplicateKeyUpdate `json:"on_duplicate_key_update,omitempty"` // MySQL-specific
1317
ReturningList *List `json:"returning_list,omitempty"`
1418
WithClause *WithClause `json:"with_clause,omitempty"`
@@ -66,9 +70,9 @@ func (n *InsertStmt) Format(buf *TrackedBuffer, d format.Dialect) {
6670
buf.astFormat(n.SelectStmt, d)
6771
}
6872

69-
if n.OnConflictClause != nil {
73+
for _, clause := range n.OnConflictClauses {
7074
buf.Line()
71-
buf.astFormat(n.OnConflictClause, d)
75+
buf.astFormat(clause, d)
7276
}
7377

7478
if n.OnDuplicateKeyUpdate != nil {

‎internal/sql/astutils/rewrite.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.
813813
a.apply(n, "Relation", nil, n.Relation)
814814
a.apply(n, "Cols", nil, n.Cols)
815815
a.apply(n, "SelectStmt", nil, n.SelectStmt)
816-
a.apply(n, "OnConflictClause", nil, n.OnConflictClause)
816+
a.applyList(n, "OnConflictClauses")
817817
a.apply(n, "OnDuplicateKeyUpdate", nil, n.OnDuplicateKeyUpdate)
818818
a.apply(n, "ReturningList", nil, n.ReturningList)
819819
a.apply(n, "WithClause", nil, n.WithClause)

‎internal/sql/astutils/walk.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,8 +1315,10 @@ func Walk(f Visitor, node ast.Node) {
13151315
if n.SelectStmt != nil {
13161316
Walk(f, n.SelectStmt)
13171317
}
1318-
if n.OnConflictClause != nil {
1319-
Walk(f, n.OnConflictClause)
1318+
for _, clause := range n.OnConflictClauses {
1319+
if clause != nil {
1320+
Walk(f, clause)
1321+
}
13201322
}
13211323
if n.OnDuplicateKeyUpdate != nil {
13221324
Walk(f, n.OnDuplicateKeyUpdate)

‎internal/sql/validate/cmd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func validateCopyfrom(n ast.Node) error {
1414
if !ok {
1515
return errors.New(":copyfrom requires an INSERT INTO statement")
1616
}
17-
if stmt.OnConflictClause != nil {
17+
if len(stmt.OnConflictClauses) > 0 {
1818
return errors.New(":copyfrom is not compatible with ON CONFLICT")
1919
}
2020
if stmt.WithClause != nil {

‎internal/sql/validate/insert_stmt.go‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,21 @@ func InsertStmt(c *catalog.Catalog, fqn *ast.TableName, stmt *ast.InsertStmt) er
4141
}
4242
}
4343

44-
return onConflictClause(c, fqn, stmt)
44+
for _, clause := range stmt.OnConflictClauses {
45+
if err := onConflictClause(c, fqn, clause); err != nil {
46+
return err
47+
}
48+
}
49+
return nil
4550
}
4651

4752
// onConflictClause validates an ON CONFLICT DO UPDATE clause against the target
4853
// table. It checks:
4954
// - ON CONFLICT (col, ...) conflict target columns exist
5055
// - DO UPDATE SET col = ... assignment target columns exist
5156
// - EXCLUDED.col references exist
52-
func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.InsertStmt) error {
53-
if fqn == nil || n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate {
57+
func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.OnConflictClause) error {
58+
if fqn == nil || n == nil || n.Action != ast.OnConflictActionUpdate {
5459
return nil
5560
}
5661

@@ -66,35 +71,35 @@ func onConflictClause(c *catalog.Catalog, fqn *ast.TableName, n *ast.InsertStmt)
6671
}
6772

6873
// DO UPDATE requires a conflict target: ON CONFLICT (col) or ON CONFLICT ON CONSTRAINT name.
69-
if n.OnConflictClause.Infer == nil {
74+
if n.Infer == nil {
7075
return &sqlerr.Error{
7176
Code: "42601",
7277
Message: "ON CONFLICT DO UPDATE requires inference specification or constraint name",
7378
}
7479
}
7580

7681
// Validate ON CONFLICT (col, ...) conflict target columns.
77-
if n.OnConflictClause.Infer.IndexElems != nil {
78-
for _, item := range n.OnConflictClause.Infer.IndexElems.Items {
82+
if n.Infer.IndexElems != nil {
83+
for _, item := range n.Infer.IndexElems.Items {
7984
elem, ok := item.(*ast.IndexElem)
8085
if !ok || elem.Name == nil {
8186
continue
8287
}
8388

8489
if _, exists := colNames[*elem.Name]; !exists {
8590
e := sqlerr.ColumnNotFound(table.Rel.Name, *elem.Name)
86-
e.Location = n.OnConflictClause.Infer.Location
91+
e.Location = n.Infer.Location
8792
return e
8893
}
8994
}
9095
}
9196

9297
// Validate DO UPDATE SET col = ... assignment target columns and EXCLUDED.col references.
93-
if n.OnConflictClause.TargetList == nil {
98+
if n.TargetList == nil {
9499
return nil
95100
}
96101

97-
for _, item := range n.OnConflictClause.TargetList.Items {
102+
for _, item := range n.TargetList.Items {
98103
target, ok := item.(*ast.ResTarget)
99104
if !ok || target.Name == nil {
100105
continue

0 commit comments

Comments
 (0)