-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
dml_planning.go
164 lines (148 loc) · 5.36 KB
/
dml_planning.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Copyright 2022 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 operators
import (
"fmt"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
"vitess.io/vitess/go/vt/vtgate/semantics"
"vitess.io/vitess/go/vt/vtgate/vindexes"
)
// getVindexInformation returns the vindex and VindexPlusPredicates for the DML,
// If it cannot find a unique vindex match, it returns an error.
func getVindexInformation(id semantics.TableSet, table *vindexes.Table) (
*vindexes.ColumnVindex,
[]*VindexPlusPredicates) {
// Check that we have a primary vindex which is valid
if len(table.ColumnVindexes) == 0 || !table.ColumnVindexes[0].IsUnique() {
panic(vterrors.VT09001(table.Name))
}
primaryVindex := table.ColumnVindexes[0]
var vindexesAndPredicates []*VindexPlusPredicates
for _, colVindex := range table.Ordered {
if lu, isLu := colVindex.Vindex.(vindexes.LookupBackfill); isLu && lu.IsBackfilling() {
// Checking if the Vindex is currently backfilling or not, if it isn't we can read from the vindex table,
// and we will be able to do a delete equal. Otherwise, we continue to look for next best vindex.
continue
}
vindexesAndPredicates = append(vindexesAndPredicates, &VindexPlusPredicates{
ColVindex: colVindex,
TableID: id,
})
}
return primaryVindex, vindexesAndPredicates
}
func buildChangedVindexesValues(
ctx *plancontext.PlanningContext,
update *sqlparser.Update,
table *vindexes.Table,
ksidCols []sqlparser.IdentifierCI,
assignments []SetExpr,
) (vv map[string]*engine.VindexValues, ownedVindexQuery string, subQueriesArgOnChangedVindex []string) {
changedVindexes := make(map[string]*engine.VindexValues)
buf, offset := initialQuery(ksidCols, table)
for i, vindex := range table.ColumnVindexes {
vindexValueMap := make(map[string]evalengine.Expr)
first := true
for _, vcol := range vindex.Columns {
// Searching in order of columns in colvindex.
found := false
for _, assignment := range assignments {
if !vcol.Equal(assignment.Name.Name) {
continue
}
if found {
panic(vterrors.VT03015(assignment.Name.Name))
}
found = true
pv, err := evalengine.Translate(assignment.Expr.EvalExpr, &evalengine.Config{
ResolveType: ctx.SemTable.TypeForExpr,
Collation: ctx.SemTable.Collation,
Environment: ctx.VSchema.Environment(),
})
if err != nil {
panic(invalidUpdateExpr(assignment.Name.Name.String(), assignment.Expr.EvalExpr))
}
if assignment.Expr.Info != nil {
sqe, ok := assignment.Expr.Info.(SubQueryExpression)
if ok {
for _, sq := range sqe {
subQueriesArgOnChangedVindex = append(subQueriesArgOnChangedVindex, sq.ArgName)
}
}
}
vindexValueMap[vcol.String()] = pv
if first {
buf.Myprintf(", %s", assignment.String())
first = false
} else {
buf.Myprintf(" and %s", assignment.String())
}
}
}
if len(vindexValueMap) == 0 {
// Vindex not changing, continue
continue
}
if update.Limit != nil && len(update.OrderBy) == 0 {
panic(vterrors.VT12001(fmt.Sprintf("you need to provide the ORDER BY clause when using LIMIT; invalid update on vindex: %v", vindex.Name)))
}
if i == 0 {
panic(vterrors.VT12001(fmt.Sprintf("you cannot UPDATE primary vindex columns; invalid update on vindex: %v", vindex.Name)))
}
if _, ok := vindex.Vindex.(vindexes.Lookup); !ok {
panic(vterrors.VT12001(fmt.Sprintf("you can only UPDATE lookup vindexes; invalid update on vindex: %v", vindex.Name)))
}
changedVindexes[vindex.Name] = &engine.VindexValues{
EvalExprMap: vindexValueMap,
Offset: offset,
}
offset++
}
if len(changedVindexes) == 0 {
return nil, "", nil
}
// generate rest of the owned vindex query.
aTblExpr, ok := update.TableExprs[0].(*sqlparser.AliasedTableExpr)
if !ok {
panic(vterrors.VT12001("UPDATE on complex table expression"))
}
tblExpr := &sqlparser.AliasedTableExpr{Expr: sqlparser.TableName{Name: table.Name}, As: aTblExpr.As}
buf.Myprintf(" from %v%v%v%v for update", tblExpr, update.Where, update.OrderBy, update.Limit)
return changedVindexes, buf.String(), subQueriesArgOnChangedVindex
}
func initialQuery(ksidCols []sqlparser.IdentifierCI, table *vindexes.Table) (*sqlparser.TrackedBuffer, int) {
buf := sqlparser.NewTrackedBuffer(nil)
offset := 0
for _, col := range ksidCols {
if offset == 0 {
buf.Myprintf("select %v", col)
} else {
buf.Myprintf(", %v", col)
}
offset++
}
for _, cv := range table.Owned {
for _, column := range cv.Columns {
buf.Myprintf(", %v", column)
offset++
}
}
return buf, offset
}
func invalidUpdateExpr(upd string, expr sqlparser.Expr) error {
return vterrors.VT12001(fmt.Sprintf("only values are supported; invalid update on column: `%s` with expr: [%s]", upd, sqlparser.String(expr)))
}