Skip to content

Commit

Permalink
sql: fix UPSERT fast path check
Browse files Browse the repository at this point in the history
The fast path was incorrectly taken when all columns in the DO UPDATE
clause were of the form `x = excluded.x` but not every column was in the
clause. This led to a correctness issue.

Closes cockroachdb#13962.
  • Loading branch information
danhhz committed Mar 9, 2017
1 parent f22562c commit 2910264
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/tablewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ func (tu *tableUpserter) init(txn *client.Txn) error {
tu.tableDesc = tu.ri.helper.tableDesc
tu.indexKeyPrefix = sqlbase.MakeIndexKeyPrefix(tu.tableDesc, tu.tableDesc.PrimaryIndex.ID)

// For the fast path, all columns must be specified in the insert.
allColsIdentityExpr := len(tu.ri.insertCols) == len(tu.tableDesc.Columns) &&
// Plus, all columns not in the conflict index must be specified in the
// DO UPDATE clause and be of the form `x = excluded.x`.
len(tu.updateCols) == len(tu.tableDesc.Columns)-len(tu.conflictIndex.ColumnIDs) &&
tu.evaler != nil && tu.evaler.isIdentityEvaler()
// When adding or removing a column in a schema change (mutation), the user
// can't specify it, which means we need to do a lookup and so we can't use
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/testdata/upsert
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,17 @@ SELECT a, b from issue_6710
----
1 test1
2 test2

statement ok
CREATE TABLE issue_13962 (a INT PRIMARY KEY, b INT, c INT)

statement ok
INSERT INTO issue_13962 VALUES (1, 1, 1)

statement ok
INSERT INTO issue_13962 VALUES (1, 2, 2) ON CONFLICT (a) DO UPDATE SET b = excluded.b

query III
SELECT * FROM issue_13962
----
1 2 1

0 comments on commit 2910264

Please sign in to comment.