From 5e9787075240e6627c9bddbd26278725d3c5e995 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 11 Jan 2022 15:54:16 +0800 Subject: [PATCH 1/4] fixup --- planner/core/find_best_task.go | 4 ++++ planner/core/point_get_plan.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index acee57c988c4a..82ff0228988bb 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -699,6 +699,10 @@ func (ds *DataSource) getPruningInfo(candidates []*candidatePath, prop *property func (ds *DataSource) isPointGetConvertableSchema() bool { for _, col := range ds.Columns { + if col.Name.L == model.ExtraHandleName.L { + continue + } + // Only handle tables that all columns are public. if col.State != model.StatePublic { return false diff --git a/planner/core/point_get_plan.go b/planner/core/point_get_plan.go index c9beca4d054ec..12ed544805b00 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -1476,6 +1476,11 @@ func buildPointDeletePlan(ctx sessionctx.Context, pointPlan PhysicalPlan, dbName func findCol(tbl *model.TableInfo, colName *ast.ColumnName) *model.ColumnInfo { for _, col := range tbl.Columns { + if colName.Name.L == model.ExtraHandleName.L && !tbl.PKIsHandle { + colInfo := model.NewExtraHandleColInfo() + colInfo.Offset = len(tbl.Columns) - 1 + return colInfo + } if col.Name.L == colName.Name.L { return col } From 1ae886ec58bc1ccd6625bef7fdad6e58aeb23b91 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 11 Jan 2022 15:59:58 +0800 Subject: [PATCH 2/4] fixup --- planner/core/point_get_plan_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/planner/core/point_get_plan_test.go b/planner/core/point_get_plan_test.go index 9d426ed719cfc..1d666a7dab9b5 100644 --- a/planner/core/point_get_plan_test.go +++ b/planner/core/point_get_plan_test.go @@ -207,6 +207,19 @@ func (s *testPointGetSuite) TestPointGetForUpdate(c *C) { tk.MustExec("rollback") } +func (s *testPointGetSuite) TestGetExtraColumn(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec(`CREATE TABLE t ( + a int(11) DEFAULT NULL, + b int(11) DEFAULT NULL, + UNIQUE KEY idx (a))`) + tk.MustQuery(`explain format='brief' select t.*, _tidb_rowid from t where a = 1`).Check(testkit.Rows("Point_Get 1.00 root table:t, index:idx(a) ")) + tk.MustQuery(`explain format='brief' select t.*, _tidb_rowid, date_format(a, "") from t where a = 1`).Check(testkit.Rows( + `Projection 1.00 root test.t.a, test.t.b, test.t._tidb_rowid, date_format(cast(test.t.a, datetime BINARY), )->Column#4`, + `└─Point_Get 1.00 root table:t, index:idx(a) `)) +} + func (s *testPointGetSuite) TestPointGetForUpdateWithSubquery(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") From 0c011fd257acd3c66dc2006ca4b7316cd0810b74 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 11 Jan 2022 16:14:25 +0800 Subject: [PATCH 3/4] fixup --- planner/core/point_get_plan_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/planner/core/point_get_plan_test.go b/planner/core/point_get_plan_test.go index 1d666a7dab9b5..02754f9dc7c39 100644 --- a/planner/core/point_get_plan_test.go +++ b/planner/core/point_get_plan_test.go @@ -218,6 +218,21 @@ func (s *testPointGetSuite) TestGetExtraColumn(c *C) { tk.MustQuery(`explain format='brief' select t.*, _tidb_rowid, date_format(a, "") from t where a = 1`).Check(testkit.Rows( `Projection 1.00 root test.t.a, test.t.b, test.t._tidb_rowid, date_format(cast(test.t.a, datetime BINARY), )->Column#4`, `└─Point_Get 1.00 root table:t, index:idx(a) `)) + tk.MustExec(`begin`) // in transaction + tk.MustExec(`insert into t values (1, 1)`) + tk.MustQuery(`explain format='brief' select t.*, _tidb_rowid from t where a = 1`).Check(testkit.Rows(`Point_Get 1.00 root table:t, index:idx(a) `)) + tk.MustExec(`commit`) + tk.MustQuery(`explain format='brief' select count(_tidb_rowid) from t where a=1`).Check(testkit.Rows( + `StreamAgg 1.00 root funcs:count(test.t._tidb_rowid)->Column#4`, + `└─Point_Get 1.00 root table:t, index:idx(a) `)) + tk.MustQuery(`explain format='brief' select *, date_format(b, "") from t where a =1 for update`).Check(testkit.Rows( + `Projection 1.00 root test.t.a, test.t.b, date_format(cast(test.t.b, datetime BINARY), )->Column#4`, + `└─SelectLock 1.00 root for update 0`, + ` └─Point_Get 1.00 root table:t, index:idx(a) `)) + + // if the PK is handled + tk.MustExec(`create table t1 (pk int, a int, b int, primary key(pk), unique key(a))`) + c.Check(tk.ExecToErr(`explain format='brief' select t1.*, _tidb_rowid from t1 where a = 1`), ErrorMatches, ".*Unknown column \\'_tidb_rowid\\'.*") } func (s *testPointGetSuite) TestPointGetForUpdateWithSubquery(c *C) { From 1028cbebe1afe645c7282355c064ea3d906cb3da Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Tue, 11 Jan 2022 16:51:44 +0800 Subject: [PATCH 4/4] fixup --- planner/core/point_get_plan.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/planner/core/point_get_plan.go b/planner/core/point_get_plan.go index 12ed544805b00..c3d57954f93e3 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -1475,12 +1475,12 @@ func buildPointDeletePlan(ctx sessionctx.Context, pointPlan PhysicalPlan, dbName } func findCol(tbl *model.TableInfo, colName *ast.ColumnName) *model.ColumnInfo { + if colName.Name.L == model.ExtraHandleName.L && !tbl.PKIsHandle { + colInfo := model.NewExtraHandleColInfo() + colInfo.Offset = len(tbl.Columns) - 1 + return colInfo + } for _, col := range tbl.Columns { - if colName.Name.L == model.ExtraHandleName.L && !tbl.PKIsHandle { - colInfo := model.NewExtraHandleColInfo() - colInfo.Offset = len(tbl.Columns) - 1 - return colInfo - } if col.Name.L == colName.Name.L { return col }