Skip to content

Commit

Permalink
cherry pick pingcap#33055 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
guo-shaoge authored and ti-srebot committed Mar 16, 2022
1 parent 75f81d2 commit aacab7e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions executor/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func (e *DeleteExec) deleteSingleTableByChunk(ctx context.Context) error {
func (e *DeleteExec) composeTblRowMap(tblRowMap tableRowMapType, colPosInfos []plannercore.TblColPosInfo, joinedRow []types.Datum) {
// iterate all the joined tables, and got the copresonding rows in joinedRow.
for _, info := range colPosInfos {
if unmatchedOuterRow(info, joinedRow) {
continue
}
if tblRowMap[info.TblID] == nil {
tblRowMap[info.TblID] = make(map[int64][]types.Datum)
}
Expand Down
30 changes: 30 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7140,3 +7140,33 @@ func (s *testSuite) TestIssue26532(c *C) {
tk.MustQuery("select greatest(\"2020-01-01 01:01:01\" ,\"2019-01-01 01:01:01\" )union select null;").Sort().Check(testkit.Rows("2020-01-01 01:01:01", "<nil>"))
tk.MustQuery("select least(\"2020-01-01 01:01:01\" , \"2019-01-01 01:01:01\" )union select null;").Sort().Check(testkit.Rows("2019-01-01 01:01:01", "<nil>"))
}

func (s *testSuite) TestDeleteWithMulTbl(c *C) {
tk := testkit.NewTestKit(c, s.store)

// Delete multiple tables from left joined table.
// The result of left join is (3, null, null).
// Because rows in t2 are not matched, so no row will be deleted in t2.
// But row in t1 is matched, so it should be deleted.
tk.MustExec("use test;")
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (c1 int);")
tk.MustExec("create table t2 (c1 int primary key, c2 int);")
tk.MustExec("insert into t1 values(3);")
tk.MustExec("insert into t2 values(2, 2);")
tk.MustExec("insert into t2 values(0, 0);")
tk.MustExec("delete from t1, t2 using t1 left join t2 on t1.c1 = t2.c2;")
tk.MustQuery("select * from t1 order by c1;").Check(testkit.Rows())
tk.MustQuery("select * from t2 order by c1;").Check(testkit.Rows("0 0", "2 2"))

// Rows in both t1 and t2 are matched, so will be deleted even if it's null.
// NOTE: The null values are not generated by join.
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (c1 int);")
tk.MustExec("create table t2 (c2 int);")
tk.MustExec("insert into t1 values(null);")
tk.MustExec("insert into t2 values(null);")
tk.MustExec("delete from t1, t2 using t1 join t2 where t1.c1 is null;")
tk.MustQuery("select * from t1;").Check(testkit.Rows())
tk.MustQuery("select * from t2;").Check(testkit.Rows())
}
38 changes: 38 additions & 0 deletions executor/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,39 @@ func (e *UpdateExec) exec(ctx context.Context, schema *expression.Schema, row, n
break
}
}
<<<<<<< HEAD
if !updatable {
=======
if unmatchedOuterRow(content, row) {
updatable = false
}
e.tableUpdatable = append(e.tableUpdatable, updatable)

changed, ok := e.updatedRowKeys[content.Start].Get(handle)
if ok {
e.changed = append(e.changed, changed.(bool))
e.matches = append(e.matches, false)
} else {
e.changed = append(e.changed, false)
e.matches = append(e.matches, true)
}
}
return nil
}

func (e *UpdateExec) merge(row, newData []types.Datum, mergeGenerated bool) error {
if e.mergedRowData == nil {
e.mergedRowData = make(map[int64]*kv.HandleMap)
}
var mergedData []types.Datum
// merge updates from and into mergedRowData
for i, content := range e.tblColPosInfos {
if !e.multiUpdateOnSameTable[content.TblID] {
// No need to merge if not multi-updated
continue
}
if !e.tableUpdatable[i] {
>>>>>>> 2dd0074e4... executor: fix wrong result of delete multiple tables using left join (#33055)
// If there's nothing to update, we can just skip current row
continue
}
Expand Down Expand Up @@ -119,8 +151,14 @@ func (e *UpdateExec) exec(ctx context.Context, schema *expression.Schema, row, n
// the inner handle field is filled with a NULL value.
//
// This fixes: https://github.com/pingcap/tidb/issues/7176.
<<<<<<< HEAD
func (e *UpdateExec) canNotUpdate(handle types.Datum) bool {
return handle.IsNull()
=======
func unmatchedOuterRow(tblPos plannercore.TblColPosInfo, waitUpdateRow []types.Datum) bool {
firstHandleIdx := tblPos.HandleCols.GetCol(0)
return waitUpdateRow[firstHandleIdx.Index].IsNull()
>>>>>>> 2dd0074e4... executor: fix wrong result of delete multiple tables using left join (#33055)
}

// Next implements the Executor Next interface.
Expand Down

0 comments on commit aacab7e

Please sign in to comment.