Skip to content

Commit

Permalink
feat: add +1 to rowaffected in upsert engine only if update returns a…
Browse files Browse the repository at this point in the history
… rowaffected value, added e2e test for multiple rows

Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Dec 5, 2023
1 parent 213e423 commit bc51d7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions go/test/endtoend/vtgate/foreignkey/fk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1232,4 +1232,9 @@ func TestInsertWithFKOnDup(t *testing.T) {
mcmp.Exec(`insert into u_t1(id, col1) values (100, 75) on duplicate key update col1 = values(col1)`)
mcmp.AssertMatches(`select * from u_t1 order by id`, `[[INT64(100) INT64(75)] [INT64(200) INT64(2)] [INT64(300) INT64(3)] [INT64(400) INT64(50)]]`)
mcmp.AssertMatches(`select * from u_t2 order by id`, `[[INT64(1000) NULL] [INT64(2000) INT64(2)] [INT64(3000) INT64(3)] [INT64(4000) NULL]]`)

// inserting multiple rows in parent, some child rows updated to null.
mcmp.Exec(`insert into u_t1(id, col1) values (100, 42),(600, 2),(300, 24),(200, 2) on duplicate key update col1 = values(col1)`)
mcmp.AssertMatches(`select * from u_t1 order by id`, `[[INT64(100) INT64(42)] [INT64(200) INT64(2)] [INT64(300) INT64(24)] [INT64(400) INT64(50)] [INT64(600) INT64(2)]]`)
mcmp.AssertMatches(`select * from u_t2 order by id`, `[[INT64(1000) NULL] [INT64(2000) INT64(2)] [INT64(3000) NULL] [INT64(4000) NULL]]`)
}
6 changes: 4 additions & 2 deletions go/vt/vtgate/engine/upsert.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ func execOne(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.
if err != nil {
return nil, err
}
// To match mysql, need to report +1 on rows affected.
updQr.RowsAffected += 1
// To match mysql, need to report +1 on rows affected if there is any change.
if updQr.RowsAffected > 0 {
updQr.RowsAffected += 1
}
return updQr, nil
}

Expand Down

0 comments on commit bc51d7e

Please sign in to comment.