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

plan: use point get plan for UPDATE statement #7586

Merged
merged 6 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 1 addition & 2 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ IndexJoin_11 4166.67 root left outer join, inner:IndexLookUp_10, outer key:test.
└─TableScan_9 10.00 cop table:t2, keep order:false, stats:pseudo
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
id count task operator info
TableReader_5 1.00 root data:TableScan_4
└─TableScan_4 1.00 cop table:t1, range:[1,1], keep order:false, stats:pseudo
Point_Get_1 1.00 root table:t1, handle:1
explain delete from t1 where t1.c2 = 1;
id count task operator info
IndexLookUp_9 10.00 root
Expand Down
3 changes: 1 addition & 2 deletions cmd/explaintest/r/explain_easy_stats.result
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ Projection_6 2481.25 root test.t1.c1, test.t1.c2, test.t1.c3, test.t2.c1, test.t
└─TableScan_20 1985.00 cop table:t2, keep order:false
explain update t1 set t1.c2 = 2 where t1.c1 = 1;
id count task operator info
TableReader_5 1.00 root data:TableScan_4
└─TableScan_4 1.00 cop table:t1, range:[1,1], keep order:false
Point_Get_1 1.00 root table:t1, handle:1
explain delete from t1 where t1.c2 = 1;
id count task operator info
IndexLookUp_9 0.00 root
Expand Down
1 change: 1 addition & 0 deletions domain/schema_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (*testSuite) TestSchemaValidator(c *C) {
c.Assert(valid, Equals, ResultUnknown)

close(exit)
time.Sleep(time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait for the goroutine to quit to pass leak test.

}

func reload(validator SchemaValidator, leaseGrantCh chan leaseGrantItem, ids ...int64) int64 {
Expand Down
5 changes: 5 additions & 0 deletions expression/simple_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func (sr *simpleRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok boo
if v.Sel == nil {
sr.inToExpression(len(v.List), v.Not, &v.Type)
}
case *ast.ParamMarkerExpr:
tp := types.NewFieldType(mysql.TypeUnspecified)
types.DefaultParamTypeForValue(v.GetValue(), tp)
value := &Constant{Value: v.Datum, RetType: tp}
sr.push(value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused about this newly added modification. Since we convert ParamMakerExpr to Constant here, what if we execute the prepared statement with another parameter? will it still use the previous constant instead of the parameter we provide?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We rebuild the plan for every execution.
And each execution updates the value in the ParamMakerExpr.

case *ast.RowExpr:
sr.rowToScalarFunc(v)
case *ast.ParenthesesExpr:
Expand Down
53 changes: 53 additions & 0 deletions plan/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func tryFastPlan(ctx sessionctx.Context, node ast.Node) Plan {
}
return fp
}
case *ast.UpdateStmt:
return tryUpdatePointPlan(ctx, x)
case *ast.DeleteStmt:
return tryDeletePointPlan(ctx, x)
}
Expand Down Expand Up @@ -382,6 +384,57 @@ func findInPairs(colName string, pairs []nameValuePair) int {
return -1
}

func tryUpdatePointPlan(ctx sessionctx.Context, updateStmt *ast.UpdateStmt) Plan {
selStmt := &ast.SelectStmt{
Fields: &ast.FieldList{},
From: updateStmt.TableRefs,
Where: updateStmt.Where,
OrderBy: updateStmt.Order,
Limit: updateStmt.Limit,
}
fastSelect := tryPointGetPlan(ctx, selStmt)
if fastSelect == nil {
return nil
}
if checkFastPlanPrivilege(ctx, fastSelect, mysql.SelectPriv, mysql.UpdatePriv) != nil {
return nil
}
orderedList := buildOrderedList(ctx, fastSelect, updateStmt.List)
if orderedList == nil {
return nil
}
updatePlan := &Update{
SelectPlan: fastSelect,
OrderedList: orderedList,
}
updatePlan.SetSchema(fastSelect.schema)
return updatePlan
}

func buildOrderedList(ctx sessionctx.Context, fastSelect *PointGetPlan, list []*ast.Assignment) []*expression.Assignment {
orderedList := make([]*expression.Assignment, 0, len(list))
for _, assign := range list {
col, err := fastSelect.schema.FindColumn(assign.Column)
if err != nil {
return nil
}
if col == nil {
return nil
}
newAssign := &expression.Assignment{
Col: col,
}
expr, err := expression.RewriteSimpleExprWithSchema(ctx, assign.Expr, fastSelect.schema)
if err != nil {
return nil
}
expr = expression.BuildCastFunction(ctx, expr, col.GetType())
newAssign.Expr = expr.ResolveIndices(fastSelect.schema)
orderedList = append(orderedList, newAssign)
}
return orderedList
}

func tryDeletePointPlan(ctx sessionctx.Context, delStmt *ast.DeleteStmt) Plan {
if delStmt.IsMultiTable {
return nil
Expand Down