Skip to content

Commit 1f87106

Browse files
lysudb-storage
authored andcommitted
executor: fix point get -1 return max.uInt64 value (pingcap#10113)
1 parent 1d1ed4a commit 1f87106

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

executor/point_get.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (b *executorBuilder) buildPointGet(p *plannercore.PointGetPlan) Executor {
4646
idxVals: p.IndexValues,
4747
handle: p.Handle,
4848
startTS: startTS,
49+
done: p.UnsignedHandle && p.Handle < 0,
4950
}
5051
}
5152

planner/core/point_get_plan.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type PointGetPlan struct {
4141
IndexInfo *model.IndexInfo
4242
Handle int64
4343
HandleParam *driver.ParamMarkerExpr
44+
UnsignedHandle bool
4445
IndexValues []types.Datum
4546
IndexValueParams []*driver.ParamMarkerExpr
4647
expr expression.Expression
@@ -185,7 +186,7 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP
185186
if pairs == nil {
186187
return nil
187188
}
188-
handlePair := findPKHandle(tbl, pairs)
189+
handlePair, unsigned := findPKHandle(tbl, pairs)
189190
if handlePair.value.Kind() != types.KindNull && len(pairs) == 1 {
190191
schema := buildSchemaFromFields(ctx, tblName.Schema, tbl, selStmt.Fields.Fields)
191192
if schema == nil {
@@ -197,6 +198,7 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP
197198
if err != nil {
198199
return nil
199200
}
201+
p.UnsignedHandle = unsigned
200202
p.HandleParam = handlePair.param
201203
return p
202204
}
@@ -362,20 +364,20 @@ func getNameValuePairs(nvPairs []nameValuePair, expr ast.ExprNode) []nameValuePa
362364
return nil
363365
}
364366

365-
func findPKHandle(tblInfo *model.TableInfo, pairs []nameValuePair) (handlePair nameValuePair) {
367+
func findPKHandle(tblInfo *model.TableInfo, pairs []nameValuePair) (handlePair nameValuePair, unsigned bool) {
366368
if !tblInfo.PKIsHandle {
367-
return handlePair
369+
return handlePair, unsigned
368370
}
369371
for _, col := range tblInfo.Columns {
370372
if mysql.HasPriKeyFlag(col.Flag) {
371373
i := findInPairs(col.Name.L, pairs)
372374
if i == -1 {
373-
return handlePair
375+
return handlePair, unsigned
374376
}
375-
return pairs[i]
377+
return pairs[i], mysql.HasUnsignedFlag(col.Flag)
376378
}
377379
}
378-
return handlePair
380+
return handlePair, unsigned
379381
}
380382

381383
func getIndexValues(idxInfo *model.IndexInfo, pairs []nameValuePair) ([]types.Datum, []*driver.ParamMarkerExpr) {

planner/core/point_get_plan_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (s *testPointGetSuite) TestPointGetPlanCache(c *C) {
5454
core.PreparedPlanCacheMaxMemory.Store(math.MaxUint64)
5555
tk.MustExec("use test")
5656
tk.MustExec("drop table if exists t")
57-
tk.MustExec("create table t(a int primary key, b int, c int, key idx_bc(b,c))")
57+
tk.MustExec("create table t(a bigint unsigned primary key, b int, c int, key idx_bc(b,c))")
5858
tk.MustExec("insert into t values(1, 1, 1), (2, 2, 2), (3, 3, 3)")
5959
tk.MustQuery("explain select * from t where a = 1").Check(testkit.Rows(
6060
"Point_Get_1 1.00 root table:t, handle:1",
@@ -68,6 +68,11 @@ func (s *testPointGetSuite) TestPointGetPlanCache(c *C) {
6868
tk.MustQuery("explain delete from t where a = 1").Check(testkit.Rows(
6969
"Point_Get_1 1.00 root table:t, handle:1",
7070
))
71+
tk.MustQuery("explain select a from t where a = -1").Check(testkit.Rows(
72+
"TableDual_5 0.00 root rows:0"))
73+
tk.MustExec(`prepare stmt0 from "select a from t where a = ?"`)
74+
tk.MustExec("set @p0 = -1")
75+
tk.MustQuery("execute stmt0 using @p0").Check(testkit.Rows())
7176
metrics.ResettablePlanCacheCounterFortTest = true
7277
metrics.PlanCacheCounter.Reset()
7378
counter := metrics.PlanCacheCounter.WithLabelValues("prepare")
@@ -137,4 +142,13 @@ func (s *testPointGetSuite) TestPointGetPlanCache(c *C) {
137142
counter.Write(pb)
138143
hit = pb.GetCounter().GetValue()
139144
c.Check(hit, Equals, float64(2))
145+
tk.MustExec("insert into t (a, b, c) values (18446744073709551615, 4, 4)")
146+
tk.MustExec("set @p1=-1")
147+
tk.MustExec("set @p2=1")
148+
tk.MustExec(`prepare stmt7 from "select a from t where a = ?"`)
149+
tk.MustQuery("execute stmt7 using @p1").Check(testkit.Rows())
150+
tk.MustQuery("execute stmt7 using @p2").Check(testkit.Rows("1"))
151+
counter.Write(pb)
152+
hit = pb.GetCounter().GetValue()
153+
c.Check(hit, Equals, float64(3))
140154
}

0 commit comments

Comments
 (0)