/
update.go
96 lines (81 loc) · 3.04 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package planbuilder
import (
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/sysvars"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
func gen4UpdateStmtPlanner(
version querypb.ExecuteOptions_PlannerVersion,
updStmt *sqlparser.Update,
reservedVars *sqlparser.ReservedVars,
vschema plancontext.VSchema,
) (*planResult, error) {
if updStmt.With != nil {
return nil, vterrors.VT12001("WITH expression in UPDATE statement")
}
ctx, err := plancontext.CreatePlanningContext(updStmt, reservedVars, vschema, version)
if err != nil {
return nil, err
}
err = queryRewrite(ctx.SemTable, reservedVars, updStmt)
if err != nil {
return nil, err
}
// If there are non-literal foreign key updates, we have to run the query with foreign key checks off.
if ctx.SemTable.HasNonLiteralForeignKeyUpdate(updStmt.Exprs) {
// Since we are running the query with foreign key checks off, we have to verify all the foreign keys validity on vtgate.
ctx.VerifyAllFKs = true
updStmt.SetComments(updStmt.GetParsedComments().SetMySQLSetVarValue(sysvars.ForeignKeyChecks, "OFF"))
}
// Remove all the foreign keys that don't require any handling.
err = ctx.SemTable.RemoveNonRequiredForeignKeys(ctx.VerifyAllFKs, vindexes.UpdateAction)
if err != nil {
return nil, err
}
if ks, tables := ctx.SemTable.SingleUnshardedKeyspace(); ks != nil {
if !ctx.SemTable.ForeignKeysPresent() {
plan := updateUnshardedShortcut(updStmt, ks, tables)
setCommentDirectivesOnPlan(plan, updStmt)
return newPlanResult(plan.Primitive(), operators.QualifiedTables(ks, tables)...), nil
}
}
if ctx.SemTable.NotUnshardedErr != nil {
return nil, ctx.SemTable.NotUnshardedErr
}
op, err := operators.PlanQuery(ctx, updStmt)
if err != nil {
return nil, err
}
plan, err := transformToLogicalPlan(ctx, op)
if err != nil {
return nil, err
}
return newPlanResult(plan.Primitive(), operators.TablesUsed(op)...), nil
}
func updateUnshardedShortcut(stmt *sqlparser.Update, ks *vindexes.Keyspace, tables []*vindexes.Table) logicalPlan {
edml := engine.NewDML()
edml.Keyspace = ks
edml.Opcode = engine.Unsharded
edml.Query = generateQuery(stmt)
for _, tbl := range tables {
edml.TableNames = append(edml.TableNames, tbl.Name.String())
}
return &primitiveWrapper{prim: &engine.Update{DML: edml}}
}