|
9 | 9 | "github.com/sqlc-dev/sqlc/internal/sql/ast" |
10 | 10 | "github.com/sqlc-dev/sqlc/internal/sql/named" |
11 | 11 | "github.com/sqlc-dev/sqlc/internal/sql/rewrite" |
| 12 | + "github.com/sqlc-dev/sqlc/internal/sql/sqlerr" |
12 | 13 | "github.com/sqlc-dev/sqlc/internal/sql/validate" |
13 | 14 | ) |
14 | 15 |
|
@@ -152,6 +153,9 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool) |
152 | 153 | if err := check(err); err != nil { |
153 | 154 | return nil, err |
154 | 155 | } |
| 156 | + if err := check(c.validateOnConflictColumns(n)); err != nil { |
| 157 | + return nil, err |
| 158 | + } |
155 | 159 | } |
156 | 160 |
|
157 | 161 | if err := check(validate.FuncCall(c.catalog, c.combo, raw)); err != nil { |
@@ -213,3 +217,83 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool) |
213 | 217 | Named: namedParams, |
214 | 218 | }, rerr |
215 | 219 | } |
| 220 | + |
| 221 | +// validateOnConflictColumns checks column names in an ON CONFLICT DO UPDATE |
| 222 | +// clause against the target table: |
| 223 | +// - ON CONFLICT (col, ...) conflict target columns |
| 224 | +// - DO UPDATE SET col = ... assignment target columns |
| 225 | +// - EXCLUDED.col references in assignment values |
| 226 | +func (c *Compiler) validateOnConflictColumns(n *ast.InsertStmt) error { |
| 227 | + if n.OnConflictClause == nil || n.OnConflictClause.Action != ast.OnConflictActionUpdate { |
| 228 | + return nil |
| 229 | + } |
| 230 | + fqn, err := ParseTableName(n.Relation) |
| 231 | + if err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + table, err := c.catalog.GetTable(fqn) |
| 235 | + if err != nil { |
| 236 | + return err |
| 237 | + } |
| 238 | + colSet := make(map[string]struct{}, len(table.Columns)) |
| 239 | + for _, col := range table.Columns { |
| 240 | + colSet[col.Name] = struct{}{} |
| 241 | + } |
| 242 | + |
| 243 | + // Validate ON CONFLICT (col, ...) conflict target columns. |
| 244 | + if n.OnConflictClause.Infer != nil { |
| 245 | + for _, item := range n.OnConflictClause.Infer.IndexElems.Items { |
| 246 | + elem, ok := item.(*ast.IndexElem) |
| 247 | + if !ok || elem.Name == nil { |
| 248 | + continue |
| 249 | + } |
| 250 | + if _, exists := colSet[*elem.Name]; !exists { |
| 251 | + e := sqlerr.ColumnNotFound(table.Rel.Name, *elem.Name) |
| 252 | + e.Location = n.OnConflictClause.Infer.Location |
| 253 | + return e |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + // Validate DO UPDATE SET col = ... and EXCLUDED.col references. |
| 259 | + for _, item := range n.OnConflictClause.TargetList.Items { |
| 260 | + target, ok := item.(*ast.ResTarget) |
| 261 | + if !ok || target.Name == nil { |
| 262 | + continue |
| 263 | + } |
| 264 | + // Validate the assignment target column. |
| 265 | + if _, exists := colSet[*target.Name]; !exists { |
| 266 | + e := sqlerr.ColumnNotFound(table.Rel.Name, *target.Name) |
| 267 | + e.Location = target.Location |
| 268 | + return e |
| 269 | + } |
| 270 | + // Validate EXCLUDED.col references in the assigned value. |
| 271 | + if ref, ok := target.Val.(*ast.ColumnRef); ok { |
| 272 | + if col, ok := excludedColumn(ref); ok { |
| 273 | + if _, exists := colSet[col]; !exists { |
| 274 | + e := sqlerr.ColumnNotFound(table.Rel.Name, col) |
| 275 | + e.Location = ref.Location |
| 276 | + return e |
| 277 | + } |
| 278 | + } |
| 279 | + } |
| 280 | + } |
| 281 | + return nil |
| 282 | +} |
| 283 | + |
| 284 | +// excludedColumn returns the column name if the ColumnRef is an EXCLUDED.col |
| 285 | +// reference, and ok=true. Returns "", false otherwise. |
| 286 | +func excludedColumn(ref *ast.ColumnRef) (string, bool) { |
| 287 | + if ref.Fields == nil || len(ref.Fields.Items) != 2 { |
| 288 | + return "", false |
| 289 | + } |
| 290 | + first, ok := ref.Fields.Items[0].(*ast.String) |
| 291 | + if !ok || first.Str != "excluded" { |
| 292 | + return "", false |
| 293 | + } |
| 294 | + second, ok := ref.Fields.Items[1].(*ast.String) |
| 295 | + if !ok { |
| 296 | + return "", false |
| 297 | + } |
| 298 | + return second.Str, true |
| 299 | +} |
0 commit comments