Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion server/analyzer/assign_insert_casts.go
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ func AssignInsertCasts(ctx *sql.Context, a *analyzer.Analyzer, node sql.Node, sc
if err != nil {
return nil, false, err
}
exprs := append(newDupExprs, insertInto.Checks().ToExpressions()...)
if insertInto.OnDupWhere != nil {
exprs = append(exprs, insertInto.OnDupWhere)
}
exprs = append(exprs, insertInto.Returning...)
// TODO: this relies on a particular implementation detail InsertInto.WithExpressions
newInsertInto, err := insertInto.WithExpressions(ctx, append(newDupExprs, insertInto.Checks().ToExpressions()...)...)
newInsertInto, err := insertInto.WithExpressions(ctx, exprs...)
if err != nil {
return nil, false, err
}
Expand Down
36 changes: 21 additions & 15 deletions server/ast/insert.go
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func nodeInsert(ctx *Context, node *tree.Insert) (insert *vitess.Insert, err err
}
var ignore string
var onDuplicate vitess.OnDup
var onDuplicateWhere vitess.Expr
Comment thread
fulghum marked this conversation as resolved.

if node.OnConflict != nil {
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
if isIgnore(node.OnConflict) {
Expand All @@ -54,6 +55,12 @@ func nodeInsert(ctx *Context, node *tree.Insert) (insert *vitess.Insert, err err
for _, updateExpr := range updateExprs {
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
onDuplicate = append(onDuplicate, updateExpr)
}
if node.OnConflict.Where != nil {
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
onDuplicateWhere, err = nodeExpr(ctx, node.OnConflict.Where.Expr)
if err != nil {
return nil, err
}
}
} else {
return nil, errors.Errorf("the ON CONFLICT clause provided is not yet supported")
}
Expand Down Expand Up @@ -107,14 +114,19 @@ func nodeInsert(ctx *Context, node *tree.Insert) (insert *vitess.Insert, err err
}
}
return &vitess.Insert{
Comment thread
fulghum marked this conversation as resolved.
Comment thread
fulghum marked this conversation as resolved.
Action: vitess.InsertStr,
Ignore: ignore,
Table: tableName,
Returning: returningExprs,
With: with,
Columns: columns,
Rows: rows,
OnDup: onDuplicate,
Action: vitess.InsertStr,
Ignore: ignore,
Table: tableName,
Returning: returningExprs,
With: with,
Columns: columns,
Rows: rows,
OnDup: onDuplicate,
OnDupValuesAlias: "excluded",
OnDupWhere: onDuplicateWhere,
// TODO: Apply PostgreSQL's single-row count to unconditional conflict updates once
// enginetests support dialect-specific affected-row expectations.
CountOnDuplicateUpdateAsOneRow: node.OnConflict != nil && node.OnConflict.Where != nil,
Auth: vitess.AuthInformation{
AuthType: auth.AuthType_INSERT,
TargetType: auth.AuthTargetType_TableIdentifiers,
Expand All @@ -134,11 +146,5 @@ func isIgnore(conflict *tree.OnConflict) bool {
// supportedOnConflictClause returns true if the ON CONFLICT clause given can be represented as
// an ON DUPLICATE KEY UPDATE clause in GMS
func supportedOnConflictClause(conflict *tree.OnConflict) bool {
if conflict.ArbiterPredicate != nil {
return false
}
if conflict.Where != nil {
return false
}
return true
return conflict.ArbiterPredicate == nil
}
Loading
Loading