From bc51d7e10c80ad0c38bffaa92ebb977a7b2b2729 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Tue, 5 Dec 2023 20:45:25 +0530 Subject: [PATCH] feat: add +1 to rowaffected in upsert engine only if update returns a rowaffected value, added e2e test for multiple rows Signed-off-by: Harshit Gangal --- go/test/endtoend/vtgate/foreignkey/fk_test.go | 5 +++++ go/vt/vtgate/engine/upsert.go | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/go/test/endtoend/vtgate/foreignkey/fk_test.go b/go/test/endtoend/vtgate/foreignkey/fk_test.go index 8c9e7ac30c7..721bb4f9862 100644 --- a/go/test/endtoend/vtgate/foreignkey/fk_test.go +++ b/go/test/endtoend/vtgate/foreignkey/fk_test.go @@ -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]]`) } diff --git a/go/vt/vtgate/engine/upsert.go b/go/vt/vtgate/engine/upsert.go index 6e155741b85..8542892ada4 100644 --- a/go/vt/vtgate/engine/upsert.go +++ b/go/vt/vtgate/engine/upsert.go @@ -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 }