From a93a97a9bdd648919c51b7837e79c265e78a8f12 Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Tue, 30 Jul 2019 20:28:36 +0800 Subject: [PATCH] planner: add table info into context in fast plan case (#11446) (#11457) --- planner/core/logical_plan_test.go | 42 +++++++++++++++++++++++++++++++ planner/core/point_get_plan.go | 2 ++ 2 files changed, 44 insertions(+) diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index 564cdd58e4826..152c869954f67 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -2461,3 +2461,45 @@ func (s *testPlanSuite) TestSkylinePruning(c *C) { c.Assert(pathsName(paths), Equals, tt.result) } } + +func (s *testPlanSuite) TestFastPlanContextTables(c *C) { + defer testleak.AfterTest(c)() + tests := []struct { + sql string + fastPlan bool + }{ + { + "select * from t where a=1", + true, + }, + { + + "update t set f=0 where a=43215", + true, + }, + { + "delete from t where a =43215", + true, + }, + { + "select * from t where a>1", + false, + }, + } + for _, tt := range tests { + stmt, err := s.ParseOneStmt(tt.sql, "", "") + c.Assert(err, IsNil) + Preprocess(s.ctx, stmt, s.is) + s.ctx.GetSessionVars().StmtCtx.Tables = nil + p := TryFastPlan(s.ctx, stmt) + if tt.fastPlan { + c.Assert(p, NotNil) + c.Assert(len(s.ctx.GetSessionVars().StmtCtx.Tables), Equals, 1) + c.Assert(s.ctx.GetSessionVars().StmtCtx.Tables[0].Table, Equals, "t") + c.Assert(s.ctx.GetSessionVars().StmtCtx.Tables[0].DB, Equals, "test") + } else { + c.Assert(p, IsNil) + c.Assert(len(s.ctx.GetSessionVars().StmtCtx.Tables), Equals, 0) + } + } +} diff --git a/planner/core/point_get_plan.go b/planner/core/point_get_plan.go index ae2cfbb733be2..83810ce96a10c 100644 --- a/planner/core/point_get_plan.go +++ b/planner/core/point_get_plan.go @@ -27,6 +27,7 @@ import ( "github.com/pingcap/tidb/planner/property" "github.com/pingcap/tidb/privilege" "github.com/pingcap/tidb/sessionctx" + "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/types/parser_driver" "github.com/pingcap/tipb/go-tipb" @@ -267,6 +268,7 @@ func newPointGetPlan(ctx sessionctx.Context, schema *expression.Schema, tbl *mod schema: schema, TblInfo: tbl, } + ctx.GetSessionVars().StmtCtx.Tables = []stmtctx.TableEntry{{DB: ctx.GetSessionVars().CurrentDB, Table: tbl.Name.L}} return p }