Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move to validate
  • Loading branch information
nikolayk812 committed Apr 1, 2026
commit 1ed27efc715be49f70c6691c341ec885bf32dea8
94 changes: 2 additions & 92 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/named"
"github.com/sqlc-dev/sqlc/internal/sql/rewrite"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
"github.com/sqlc-dev/sqlc/internal/sql/validate"
)

Expand Down Expand Up @@ -143,11 +142,7 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
raw, namedParams, edits := rewrite.NamedParameters(c.conf.Engine, raw, numbers, dollar)

var table *ast.TableName
switch n := raw.Stmt.(type) {
case *ast.InsertStmt:
if err := check(validate.InsertStmt(n)); err != nil {
return nil, err
}
if n, ok := raw.Stmt.(*ast.InsertStmt); ok {
var err error
table, err = ParseTableName(n.Relation)
if err := check(err); err != nil {
Expand Down Expand Up @@ -187,7 +182,7 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
return nil, err
}
if n, ok := raw.Stmt.(*ast.InsertStmt); ok {
if err := check(c.validateOnConflictClause(n)); err != nil {
if err := check(validate.InsertStmt(n, table, c.catalog)); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -219,88 +214,3 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
Named: namedParams,
}, rerr
}

// validateOnConflictClause validates an ON CONFLICT DO UPDATE clause against
// the target table. It checks:
// - ON CONFLICT (col, ...) conflict target columns exist
// - DO UPDATE SET col = ... assignment target columns exist
// - EXCLUDED.col references exist
func (c *Compiler) validateOnConflictClause(n *ast.InsertStmt) error {
if n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate {
return nil
}

fqn, err := ParseTableName(n.Relation)
if err != nil {
return err
}

table, err := c.catalog.GetTable(fqn)
if err != nil {
return err
}

// Build set of column names for existence checks.
colNames := make(map[string]struct{}, len(table.Columns))
for _, col := range table.Columns {
colNames[col.Name] = struct{}{}
}

// Validate ON CONFLICT (col, ...) conflict target columns.
if n.OnConflictClause.Infer != nil && n.OnConflictClause.Infer.IndexElems != nil {
for _, item := range n.OnConflictClause.Infer.IndexElems.Items {
elem, ok := item.(*ast.IndexElem)
if !ok || elem.Name == nil {
continue
}
if _, exists := colNames[*elem.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *elem.Name)
e.Location = n.OnConflictClause.Infer.Location
return e
}
}
}

// Validate DO UPDATE SET col = ... assignment target columns and EXCLUDED.col references.
if n.OnConflictClause.TargetList == nil {
return nil
}
for _, item := range n.OnConflictClause.TargetList.Items {
target, ok := item.(*ast.ResTarget)
if !ok || target.Name == nil {
continue
}
if _, exists := colNames[*target.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *target.Name)
e.Location = target.Location
return e
}
if ref, ok := target.Val.(*ast.ColumnRef); ok {
if excludedCol, ok := excludedColumn(ref); ok {
if _, exists := colNames[excludedCol]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, excludedCol)
e.Location = ref.Location
return e
}
}
}
}
return nil
}

// excludedColumn returns the column name if the ColumnRef is an EXCLUDED.col
// reference, and ok=true. Returns "", false otherwise.
func excludedColumn(ref *ast.ColumnRef) (string, bool) {
if ref.Fields == nil || len(ref.Fields.Items) != 2 {
return "", false
}
first, ok := ref.Fields.Items[0].(*ast.String)
if !ok || first.Str != "excluded" {
return "", false
}
second, ok := ref.Fields.Items[1].(*ast.String)
if !ok {
return "", false
}
return second.Str, true
}
85 changes: 84 additions & 1 deletion internal/sql/validate/insert_stmt.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package validate

import (
"strings"

"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
)

func InsertStmt(stmt *ast.InsertStmt) error {
func InsertStmt(stmt *ast.InsertStmt, fqn *ast.TableName, c *catalog.Catalog) error {
sel, ok := stmt.SelectStmt.(*ast.SelectStmt)
if !ok {
return nil
Expand Down Expand Up @@ -35,5 +38,85 @@ func InsertStmt(stmt *ast.InsertStmt) error {
Message: "INSERT has more expressions than target columns",
}
}
return onConflictClause(stmt, fqn, c)
}

// onConflictClause validates an ON CONFLICT DO UPDATE clause against the target
// table. It checks:
// - ON CONFLICT (col, ...) conflict target columns exist
// - DO UPDATE SET col = ... assignment target columns exist
// - EXCLUDED.col references exist
func onConflictClause(n *ast.InsertStmt, fqn *ast.TableName, c *catalog.Catalog) error {
if n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate {
return nil
}

table, err := c.GetTable(fqn)
if err != nil {
return err
}

// Build set of column names for existence checks.
colNames := make(map[string]struct{}, len(table.Columns))
for _, col := range table.Columns {
colNames[col.Name] = struct{}{}
}

// Validate ON CONFLICT (col, ...) conflict target columns.
if n.OnConflictClause.Infer != nil && n.OnConflictClause.Infer.IndexElems != nil {
for _, item := range n.OnConflictClause.Infer.IndexElems.Items {
elem, ok := item.(*ast.IndexElem)
if !ok || elem.Name == nil {
continue
}
if _, exists := colNames[*elem.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *elem.Name)
e.Location = n.OnConflictClause.Infer.Location
return e
}
}
}

// Validate DO UPDATE SET col = ... assignment target columns and EXCLUDED.col references.
if n.OnConflictClause.TargetList == nil {
return nil
}
for _, item := range n.OnConflictClause.TargetList.Items {
target, ok := item.(*ast.ResTarget)
if !ok || target.Name == nil {
continue
}
if _, exists := colNames[*target.Name]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, *target.Name)
e.Location = target.Location
return e
}
if ref, ok := target.Val.(*ast.ColumnRef); ok {
if excludedCol, ok := excludedColumnRef(ref); ok {
if _, exists := colNames[excludedCol]; !exists {
e := sqlerr.ColumnNotFound(table.Rel.Name, excludedCol)
e.Location = ref.Location
return e
}
}
}
}
return nil
}

// excludedColumnRef returns the column name if the ColumnRef is an EXCLUDED.col
// reference, and ok=true. Returns "", false otherwise.
func excludedColumnRef(ref *ast.ColumnRef) (string, bool) {
if ref.Fields == nil || len(ref.Fields.Items) != 2 {
return "", false
}
first, ok := ref.Fields.Items[0].(*ast.String)
if !ok || !strings.EqualFold(first.Str, "excluded") {
return "", false
}
second, ok := ref.Fields.Items[1].(*ast.String)
if !ok {
return "", false
}
return second.Str, true
}