Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner, executor: return TableDual when tryFastPlan is promised to be false (#10676) #10699

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3799,6 +3799,47 @@ func (s *testSuite) TestSplitIndexRegion(c *C) {
c.Assert(terr.Code(), Equals, terror.ErrCode(mysql.WarnDataTruncated))
}

func (s *testSuite) TestIssue10448(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(pk int1 primary key)")
tk.MustExec("insert into t values(125)")
tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 128").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(pk int8 primary key)")
tk.MustExec("insert into t values(9223372036854775807)")
tk.MustQuery("select * from t where pk = 9223372036854775807").Check(testkit.Rows("9223372036854775807"))
tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Rows("Point_Get_1 1.00 root table:t, handle:9223372036854775807"))
tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(pk int1 unsigned primary key)")
tk.MustExec("insert into t values(255)")
tk.MustQuery("select * from t where pk = 255").Check(testkit.Rows("255"))
tk.MustQuery("desc select * from t where pk = 256").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(pk int8 unsigned primary key)")
tk.MustExec("insert into t value(18446744073709551615)")
tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Rows("Point_Get_1 1.00 root table:t, handle:18446744073709551615"))
tk.MustQuery("select * from t where pk = 18446744073709551615").Check(testkit.Rows("18446744073709551615"))
tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Rows("Point_Get_1 1.00 root table:t, handle:9223372036854775807"))
tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Rows("TableDual_2 0.00 root rows:0"))
tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Rows("Point_Get_1 1.00 root table:t, handle:9223372036854775808"))
}

func (s *testSuite) TestUnsignedFeedback(c *C) {
tk := testkit.NewTestKit(c, s.store)
oriProbability := statistics.FeedbackProbability.Load()
Expand Down
1 change: 0 additions & 1 deletion executor/point_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (b *executorBuilder) buildPointGet(p *plannercore.PointGetPlan) Executor {
idxVals: p.IndexValues,
handle: p.Handle,
startTS: startTS,
done: p.UnsignedHandle && p.Handle < 0,
}
}

Expand Down
3 changes: 2 additions & 1 deletion planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ func (e *Execute) getPhysicalPlan(ctx sessionctx.Context, is infoschema.InfoSche
if err != nil {
return nil, err
}
if prepared.UseCache {
_, isTableDual := p.(*PhysicalTableDual)
if !isTableDual && prepared.UseCache {
ctx.PreparedPlanCache().Put(cacheKey, NewPSTMTPlanCacheValue(p))
}
return p, err
Expand Down
41 changes: 31 additions & 10 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/opcode"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/privilege"
Expand All @@ -46,6 +47,7 @@ type PointGetPlan struct {
IndexValueParams []*driver.ParamMarkerExpr
expr expression.Expression
ctx sessionctx.Context
IsTableDual bool
}

type nameValuePair struct {
Expand Down Expand Up @@ -84,7 +86,11 @@ func (p *PointGetPlan) ExplainInfo() string {
}
}
} else {
fmt.Fprintf(buffer, ", handle:%d", p.Handle)
if p.UnsignedHandle {
fmt.Fprintf(buffer, ", handle:%d", uint64(p.Handle))
} else {
fmt.Fprintf(buffer, ", handle:%d", p.Handle)
}
}
return buffer.String()
}
Expand Down Expand Up @@ -130,6 +136,11 @@ func TryFastPlan(ctx sessionctx.Context, node ast.Node) Plan {
if checkFastPlanPrivilege(ctx, fp, mysql.SelectPriv) != nil {
return nil
}
if fp.IsTableDual {
tableDual := PhysicalTableDual{}
tableDual.SetSchema(fp.Schema())
return tableDual.Init(ctx, &property.StatsInfo{})
}
return fp
}
case *ast.UpdateStmt:
Expand Down Expand Up @@ -186,19 +197,23 @@ func tryPointGetPlan(ctx sessionctx.Context, selStmt *ast.SelectStmt) *PointGetP
if pairs == nil {
return nil
}
handlePair, unsigned := findPKHandle(tbl, pairs)
handlePair, fieldType := findPKHandle(tbl, pairs)
if handlePair.value.Kind() != types.KindNull && len(pairs) == 1 {
schema := buildSchemaFromFields(ctx, tblName.Schema, tbl, selStmt.Fields.Fields)
if schema == nil {
return nil
}
p := newPointGetPlan(ctx, schema, tbl)
var err error
p.Handle, err = handlePair.value.ToInt64(ctx.GetSessionVars().StmtCtx)
intDatum, err := handlePair.value.ConvertTo(ctx.GetSessionVars().StmtCtx, fieldType)
if err != nil {
if terror.ErrorEqual(types.ErrOverflow, err) {
p.IsTableDual = true
return p
}
return nil
}
p.UnsignedHandle = unsigned
p.Handle = intDatum.GetInt64()
p.UnsignedHandle = mysql.HasUnsignedFlag(fieldType.Flag)
p.HandleParam = handlePair.param
return p
}
Expand Down Expand Up @@ -364,20 +379,20 @@ func getNameValuePairs(nvPairs []nameValuePair, expr ast.ExprNode) []nameValuePa
return nil
}

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

func getIndexValues(idxInfo *model.IndexInfo, pairs []nameValuePair) ([]types.Datum, []*driver.ParamMarkerExpr) {
Expand Down Expand Up @@ -427,6 +442,9 @@ func tryUpdatePointPlan(ctx sessionctx.Context, updateStmt *ast.UpdateStmt) Plan
if checkFastPlanPrivilege(ctx, fastSelect, mysql.SelectPriv, mysql.UpdatePriv) != nil {
return nil
}
if fastSelect.IsTableDual {
return PhysicalTableDual{}.Init(ctx, &property.StatsInfo{})
}
orderedList := buildOrderedList(ctx, fastSelect, updateStmt.List)
if orderedList == nil {
return nil
Expand Down Expand Up @@ -484,6 +502,9 @@ func tryDeletePointPlan(ctx sessionctx.Context, delStmt *ast.DeleteStmt) Plan {
if checkFastPlanPrivilege(ctx, fastSelect, mysql.SelectPriv, mysql.DeletePriv) != nil {
return nil
}
if fastSelect.IsTableDual {
return PhysicalTableDual{}.Init(ctx, &property.StatsInfo{})
}
delPlan := Delete{
SelectPlan: fastSelect,
}.Init(ctx)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/point_get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,5 @@ func (s *testPointGetSuite) TestPointGetPlanCache(c *C) {
tk.MustQuery("execute stmt7 using @p2").Check(testkit.Rows("1"))
counter.Write(pb)
hit = pb.GetCounter().GetValue()
c.Check(hit, Equals, float64(3))
c.Check(hit, Equals, float64(2))
}