forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codex.go
243 lines (227 loc) · 7.06 KB
/
codex.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"bytes"
"encoding/base64"
"fmt"
"strings"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/schema"
)
// buildValueList builds the set of PK reference rows used to drive the next query.
// It uses the PK values supplied in the original query and bind variables.
// The generated reference rows are validated for type match against the PK of the table.
func buildValueList(tableInfo *TableInfo, pkValues []interface{}, bindVars map[string]interface{}) [][]sqltypes.Value {
length := -1
for _, pkValue := range pkValues {
if list, ok := pkValue.([]interface{}); ok {
if length == -1 {
if length = len(list); length == 0 {
panic(NewTabletError(FAIL, "empty list for values %v", pkValues))
}
} else if length != len(list) {
panic(NewTabletError(FAIL, "mismatched lengths for values %v", pkValues))
}
}
}
if length == -1 {
length = 1
}
valueList := make([][]sqltypes.Value, length)
for i := 0; i < length; i++ {
valueList[i] = make([]sqltypes.Value, len(pkValues))
for j, pkValue := range pkValues {
if list, ok := pkValue.([]interface{}); ok {
valueList[i][j] = resolveValue(tableInfo.GetPKColumn(j), list[i], bindVars)
} else {
valueList[i][j] = resolveValue(tableInfo.GetPKColumn(j), pkValue, bindVars)
}
}
}
return valueList
}
// buildINValueList builds the set of PK reference rows used to drive the next query
// using an IN clause. This works only for tables with no composite PK columns.
// The generated reference rows are validated for type match against the PK of the table.
func buildINValueList(tableInfo *TableInfo, pkValues []interface{}, bindVars map[string]interface{}) [][]sqltypes.Value {
if len(tableInfo.PKColumns) != 1 {
panic("unexpected")
}
valueList := make([][]sqltypes.Value, len(pkValues))
for i, pkValue := range pkValues {
valueList[i] = make([]sqltypes.Value, 1)
valueList[i][0] = resolveValue(tableInfo.GetPKColumn(0), pkValue, bindVars)
}
return valueList
}
// buildSecondaryList is used for handling ON DUPLICATE DMLs, or those that change the PK.
func buildSecondaryList(tableInfo *TableInfo, pkList [][]sqltypes.Value, secondaryList []interface{}, bindVars map[string]interface{}) [][]sqltypes.Value {
if secondaryList == nil {
return nil
}
valueList := make([][]sqltypes.Value, len(pkList))
for i, row := range pkList {
valueList[i] = make([]sqltypes.Value, len(row))
for j, cell := range row {
if secondaryList[j] == nil {
valueList[i][j] = cell
} else {
valueList[i][j] = resolveValue(tableInfo.GetPKColumn(j), secondaryList[j], bindVars)
}
}
}
return valueList
}
func resolveValue(col *schema.TableColumn, value interface{}, bindVars map[string]interface{}) (result sqltypes.Value) {
switch v := value.(type) {
case string:
lookup, ok := bindVars[v[1:]]
if !ok {
panic(NewTabletError(FAIL, "Missing bind var %s", v))
}
sqlval, err := sqltypes.BuildValue(lookup)
if err != nil {
panic(NewTabletError(FAIL, "%v", err))
}
result = sqlval
case sqltypes.Value:
result = v
case nil:
// no op
default:
panic("unreachable")
}
validateValue(col, result)
return result
}
func validateRow(tableInfo *TableInfo, columnNumbers []int, row []sqltypes.Value) {
if len(row) != len(columnNumbers) {
panic(NewTabletError(FAIL, "data inconsistency %d vs %d", len(row), len(columnNumbers)))
}
for j, value := range row {
validateValue(&tableInfo.Columns[columnNumbers[j]], value)
}
}
func validateValue(col *schema.TableColumn, value sqltypes.Value) {
if value.IsNull() {
return
}
switch col.Category {
case schema.CAT_NUMBER:
if !value.IsNumeric() {
panic(NewTabletError(FAIL, "Type mismatch, expecting numeric type for %v", value))
}
case schema.CAT_VARBINARY:
if !value.IsString() {
panic(NewTabletError(FAIL, "Type mismatch, expecting string type for %v", value))
}
}
}
func buildKey(row []sqltypes.Value) (key string) {
buf := bytes.NewBuffer(make([]byte, 0, 32))
for i, pkValue := range row {
if pkValue.IsNull() {
return ""
}
pkValue.EncodeAscii(buf)
if i != len(row)-1 {
buf.WriteByte('.')
}
}
return buf.String()
}
func buildStreamComment(tableInfo *TableInfo, pkValueList [][]sqltypes.Value, secondaryList [][]sqltypes.Value) []byte {
buf := bytes.NewBuffer(make([]byte, 0, 256))
fmt.Fprintf(buf, " /* _stream %s (", tableInfo.Name)
// We assume the first index exists, and is the pk
for _, pkName := range tableInfo.Indexes[0].Columns {
buf.WriteString(pkName)
buf.WriteString(" ")
}
buf.WriteString(")")
buildPKValueList(buf, tableInfo, pkValueList)
buildPKValueList(buf, tableInfo, secondaryList)
buf.WriteString("; */")
return buf.Bytes()
}
func buildPKValueList(buf *bytes.Buffer, tableInfo *TableInfo, pkValueList [][]sqltypes.Value) {
for _, pkValues := range pkValueList {
buf.WriteString(" (")
for _, pkValue := range pkValues {
pkValue.EncodeAscii(buf)
buf.WriteString(" ")
}
buf.WriteString(")")
}
}
func applyFilter(columnNumbers []int, input []sqltypes.Value) (output []sqltypes.Value) {
output = make([]sqltypes.Value, len(columnNumbers))
for colIndex, colPointer := range columnNumbers {
if colPointer >= 0 {
output[colIndex] = input[colPointer]
}
}
return output
}
func applyFilterWithPKDefaults(tableInfo *TableInfo, columnNumbers []int, input []sqltypes.Value) (output []sqltypes.Value) {
output = make([]sqltypes.Value, len(columnNumbers))
for colIndex, colPointer := range columnNumbers {
if colPointer >= 0 {
output[colIndex] = input[colPointer]
} else {
output[colIndex] = tableInfo.GetPKColumn(colIndex).Default
}
}
return output
}
func validateKey(tableInfo *TableInfo, key string) (newKey string) {
if key == "" {
// TODO: Verify auto-increment table
return
}
pieces := strings.Split(key, ".")
if len(pieces) != len(tableInfo.PKColumns) {
// TODO: Verify auto-increment table
return ""
}
pkValues := make([]sqltypes.Value, len(tableInfo.PKColumns))
for i, piece := range pieces {
if piece[0] == '\'' {
s, err := base64.StdEncoding.DecodeString(piece[1 : len(piece)-1])
if err != nil {
log.Warningf("Error decoding key %s for table %s: %v", key, tableInfo.Name, err)
internalErrors.Add("Mismatch", 1)
return
}
pkValues[i] = sqltypes.MakeString(s)
} else if piece == "null" {
// TODO: Verify auto-increment table
return ""
} else {
n, err := sqltypes.BuildNumeric(piece)
if err != nil {
log.Warningf("Error decoding key %s for table %s: %v", key, tableInfo.Name, err)
internalErrors.Add("Mismatch", 1)
return
}
pkValues[i] = n
}
}
if newKey = buildKey(pkValues); newKey != key {
log.Warningf("Error: Key mismatch, received: %s, computed: %s", key, newKey)
internalErrors.Add("Mismatch", 1)
}
return newKey
}
// unicoded returns a valid UTF-8 string that json won't reject
func unicoded(in string) (out string) {
for i, v := range in {
if v == 0xFFFD {
return in[:i]
}
}
return in
}