Skip to content

Commit

Permalink
executor: fix point get -1 return max.uInt64 value (pingcap#10113)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored and db-storage committed May 29, 2019
1 parent 1d1ed4a commit 1f87106
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (b *executorBuilder) buildPointGet(p *plannercore.PointGetPlan) Executor {
idxVals: p.IndexValues,
handle: p.Handle,
startTS: startTS,
done: p.UnsignedHandle && p.Handle < 0,
}
}

Expand Down
14 changes: 8 additions & 6 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type PointGetPlan struct {
IndexInfo *model.IndexInfo
Handle int64
HandleParam *driver.ParamMarkerExpr
UnsignedHandle bool
IndexValues []types.Datum
IndexValueParams []*driver.ParamMarkerExpr
expr expression.Expression
Expand Down Expand Up @@ -185,7 +186,7 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP
if pairs == nil {
return nil
}
handlePair := findPKHandle(tbl, pairs)
handlePair, unsigned := findPKHandle(tbl, pairs)
if handlePair.value.Kind() != types.KindNull && len(pairs) == 1 {
schema := buildSchemaFromFields(ctx, tblName.Schema, tbl, selStmt.Fields.Fields)
if schema == nil {
Expand All @@ -197,6 +198,7 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP
if err != nil {
return nil
}
p.UnsignedHandle = unsigned
p.HandleParam = handlePair.param
return p
}
Expand Down Expand Up @@ -362,20 +364,20 @@ func getNameValuePairs(nvPairs []nameValuePair, expr ast.ExprNode) []nameValuePa
return nil
}

func findPKHandle(tblInfo *model.TableInfo, pairs []nameValuePair) (handlePair nameValuePair) {
func findPKHandle(tblInfo *model.TableInfo, pairs []nameValuePair) (handlePair nameValuePair, unsigned bool) {
if !tblInfo.PKIsHandle {
return handlePair
return handlePair, unsigned
}
for _, col := range tblInfo.Columns {
if mysql.HasPriKeyFlag(col.Flag) {
i := findInPairs(col.Name.L, pairs)
if i == -1 {
return handlePair
return handlePair, unsigned
}
return pairs[i]
return pairs[i], mysql.HasUnsignedFlag(col.Flag)
}
}
return handlePair
return handlePair, unsigned
}

func getIndexValues(idxInfo *model.IndexInfo, pairs []nameValuePair) ([]types.Datum, []*driver.ParamMarkerExpr) {
Expand Down
16 changes: 15 additions & 1 deletion planner/core/point_get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *testPointGetSuite) TestPointGetPlanCache(c *C) {
core.PreparedPlanCacheMaxMemory.Store(math.MaxUint64)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key, b int, c int, key idx_bc(b,c))")
tk.MustExec("create table t(a bigint unsigned primary key, b int, c int, key idx_bc(b,c))")
tk.MustExec("insert into t values(1, 1, 1), (2, 2, 2), (3, 3, 3)")
tk.MustQuery("explain select * from t where a = 1").Check(testkit.Rows(
"Point_Get_1 1.00 root table:t, handle:1",
Expand All @@ -68,6 +68,11 @@ func (s *testPointGetSuite) TestPointGetPlanCache(c *C) {
tk.MustQuery("explain delete from t where a = 1").Check(testkit.Rows(
"Point_Get_1 1.00 root table:t, handle:1",
))
tk.MustQuery("explain select a from t where a = -1").Check(testkit.Rows(
"TableDual_5 0.00 root rows:0"))
tk.MustExec(`prepare stmt0 from "select a from t where a = ?"`)
tk.MustExec("set @p0 = -1")
tk.MustQuery("execute stmt0 using @p0").Check(testkit.Rows())
metrics.ResettablePlanCacheCounterFortTest = true
metrics.PlanCacheCounter.Reset()
counter := metrics.PlanCacheCounter.WithLabelValues("prepare")
Expand Down Expand Up @@ -137,4 +142,13 @@ func (s *testPointGetSuite) TestPointGetPlanCache(c *C) {
counter.Write(pb)
hit = pb.GetCounter().GetValue()
c.Check(hit, Equals, float64(2))
tk.MustExec("insert into t (a, b, c) values (18446744073709551615, 4, 4)")
tk.MustExec("set @p1=-1")
tk.MustExec("set @p2=1")
tk.MustExec(`prepare stmt7 from "select a from t where a = ?"`)
tk.MustQuery("execute stmt7 using @p1").Check(testkit.Rows())
tk.MustQuery("execute stmt7 using @p2").Check(testkit.Rows("1"))
counter.Write(pb)
hit = pb.GetCounter().GetValue()
c.Check(hit, Equals, float64(3))
}

0 comments on commit 1f87106

Please sign in to comment.