forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fk.go
426 lines (387 loc) · 12.5 KB
/
fk.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2016 The Cockroach 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 sqlbase
import (
"fmt"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
// TableLookupsByID maps table IDs to looked up descriptors or, for tables that
// exist but are not yet public/leasable, entries with just the IsAdding flag.
type TableLookupsByID map[ID]TableLookup
// TableLookup is the value type of TableLookupsByID: An optional table
// descriptor, populated when the table is public/leasable, and an IsAdding
// flag.
type TableLookup struct {
Table *TableDescriptor
IsAdding bool
}
// FKCheck indicates a kind of FK check (delete, insert, or both).
type FKCheck int
const (
// CheckDeletes checks if rows reference a changed value.
CheckDeletes FKCheck = iota
// CheckInserts checks if a new/changed value references an existing row.
CheckInserts
// CheckUpdates checks all references (CheckDeletes+CheckInserts).
CheckUpdates
)
// TablesNeededForFKs calculates the IDs of the additional TableDescriptors that
// will be needed for FK checking delete and/or insert operations on `table`.
//
// NB: the returned map's values are *not* set -- higher level calling code, eg
// in planner, should fill the map's values by acquiring leases. This function
// is essentially just returning a slice of IDs, but the empty map can be filled
// in place and reused, avoiding a second allocation.
func TablesNeededForFKs(table TableDescriptor, usage FKCheck) TableLookupsByID {
var ret TableLookupsByID
for _, idx := range table.AllNonDropIndexes() {
if usage != CheckDeletes && idx.ForeignKey.IsSet() {
if ret == nil {
ret = make(TableLookupsByID)
}
ret[idx.ForeignKey.Table] = TableLookup{}
}
if usage != CheckInserts {
for _, idx := range table.AllNonDropIndexes() {
for _, ref := range idx.ReferencedBy {
if ret == nil {
ret = make(TableLookupsByID)
}
ret[ref.Table] = TableLookup{}
}
}
}
}
return ret
}
type fkInsertHelper map[IndexID][]baseFKHelper
var errSkipUnusedFK = errors.New("no columns involved in FK included in writer")
func makeFKInsertHelper(
txn *client.Txn,
table TableDescriptor,
otherTables TableLookupsByID,
colMap map[ColumnID]int,
alloc *DatumAlloc,
) (fkInsertHelper, error) {
var fks fkInsertHelper
for _, idx := range table.AllNonDropIndexes() {
if idx.ForeignKey.IsSet() {
fk, err := makeBaseFKHelper(txn, otherTables, idx, idx.ForeignKey, colMap, alloc)
if err == errSkipUnusedFK {
continue
}
if err != nil {
return fks, err
}
if fks == nil {
fks = make(fkInsertHelper)
}
fks[idx.ID] = append(fks[idx.ID], fk)
}
}
return fks, nil
}
func (fks fkInsertHelper) checkAll(ctx context.Context, row parser.Datums, traceKV bool) error {
for idx := range fks {
if err := fks.checkIdx(ctx, idx, row, traceKV); err != nil {
return err
}
}
return nil
}
func (fks fkInsertHelper) checkIdx(
ctx context.Context, idx IndexID, row parser.Datums, traceKV bool,
) error {
for _, fk := range fks[idx] {
nulls := true
for _, colID := range fk.searchIdx.ColumnIDs[:fk.prefixLen] {
found, ok := fk.ids[colID]
if !ok {
panic(fmt.Sprintf("fk ids (%v) missing column id %d", fk.ids, colID))
}
nulls = nulls && row[found] == parser.DNull
}
if nulls {
continue
}
found, err := fk.check(ctx, row, traceKV)
if err != nil {
return err
}
if !found {
fkValues := make(parser.Datums, fk.prefixLen)
for i, colID := range fk.searchIdx.ColumnIDs[:fk.prefixLen] {
fkValues[i] = row[fk.ids[colID]]
}
return pgerror.NewErrorf(pgerror.CodeForeignKeyViolationError,
"foreign key violation: value %s not found in %s@%s %s",
fkValues, fk.searchTable.Name, fk.searchIdx.Name, fk.searchIdx.ColumnNames[:fk.prefixLen])
}
}
return nil
}
// CollectSpans implements the FkSpanCollector interface.
func (fks fkInsertHelper) CollectSpans() roachpb.Spans {
return collectSpansWithFKMap(fks)
}
// CollectSpansForValues implements the FkSpanCollector interface.
func (fks fkInsertHelper) CollectSpansForValues(values parser.Datums) (roachpb.Spans, error) {
return collectSpansForValuesWithFKMap(fks, values)
}
type fkDeleteHelper map[IndexID][]baseFKHelper
func makeFKDeleteHelper(
txn *client.Txn,
table TableDescriptor,
otherTables TableLookupsByID,
colMap map[ColumnID]int,
alloc *DatumAlloc,
) (fkDeleteHelper, error) {
var fks fkDeleteHelper
for _, idx := range table.AllNonDropIndexes() {
for _, ref := range idx.ReferencedBy {
if otherTables[ref.Table].IsAdding {
// We can assume that a table being added but not yet public is empty,
// and thus does not need to be checked for FK violations.
continue
}
fk, err := makeBaseFKHelper(txn, otherTables, idx, ref, colMap, alloc)
if err == errSkipUnusedFK {
continue
}
if err != nil {
return fks, err
}
if fks == nil {
fks = make(fkDeleteHelper)
}
fks[idx.ID] = append(fks[idx.ID], fk)
}
}
return fks, nil
}
func (fks fkDeleteHelper) checkAll(ctx context.Context, row parser.Datums, traceKV bool) error {
for idx := range fks {
if err := fks.checkIdx(ctx, idx, row, traceKV); err != nil {
return err
}
}
return nil
}
func (fks fkDeleteHelper) checkIdx(
ctx context.Context, idx IndexID, row parser.Datums, traceKV bool,
) error {
for _, fk := range fks[idx] {
found, err := fk.check(ctx, row, traceKV)
if err != nil {
return err
}
if found {
if row == nil {
return pgerror.NewErrorf(pgerror.CodeForeignKeyViolationError,
"foreign key violation: non-empty columns %s referenced in table %q",
fk.writeIdx.ColumnNames[:fk.prefixLen], fk.searchTable.Name)
}
fkValues := make(parser.Datums, fk.prefixLen)
for i, colID := range fk.searchIdx.ColumnIDs[:fk.prefixLen] {
fkValues[i] = row[fk.ids[colID]]
}
return pgerror.NewErrorf(pgerror.CodeForeignKeyViolationError,
"foreign key violation: values %v in columns %s referenced in table %q",
fkValues, fk.writeIdx.ColumnNames[:fk.prefixLen], fk.searchTable.Name)
}
}
return nil
}
// CollectSpans implements the FkSpanCollector interface.
func (fks fkDeleteHelper) CollectSpans() roachpb.Spans {
return collectSpansWithFKMap(fks)
}
// CollectSpansForValues implements the FkSpanCollector interface.
func (fks fkDeleteHelper) CollectSpansForValues(values parser.Datums) (roachpb.Spans, error) {
return collectSpansForValuesWithFKMap(fks, values)
}
type fkUpdateHelper struct {
inbound fkDeleteHelper // Check old values are not referenced.
outbound fkInsertHelper // Check rows referenced by new values still exist.
}
func makeFKUpdateHelper(
txn *client.Txn,
table TableDescriptor,
otherTables TableLookupsByID,
colMap map[ColumnID]int,
alloc *DatumAlloc,
) (fkUpdateHelper, error) {
ret := fkUpdateHelper{}
var err error
if ret.inbound, err = makeFKDeleteHelper(txn, table, otherTables, colMap, alloc); err != nil {
return ret, err
}
ret.outbound, err = makeFKInsertHelper(txn, table, otherTables, colMap, alloc)
return ret, err
}
func (fks fkUpdateHelper) checkIdx(
ctx context.Context, idx IndexID, oldValues, newValues parser.Datums, traceKV bool,
) error {
if err := fks.inbound.checkIdx(ctx, idx, oldValues, traceKV); err != nil {
return err
}
return fks.outbound.checkIdx(ctx, idx, newValues, traceKV)
}
// CollectSpans implements the FkSpanCollector interface.
func (fks fkUpdateHelper) CollectSpans() roachpb.Spans {
inboundReads := fks.inbound.CollectSpans()
outboundReads := fks.outbound.CollectSpans()
return append(inboundReads, outboundReads...)
}
// CollectSpansForValues implements the FkSpanCollector interface.
func (fks fkUpdateHelper) CollectSpansForValues(values parser.Datums) (roachpb.Spans, error) {
inboundReads, err := fks.inbound.CollectSpansForValues(values)
if err != nil {
return nil, err
}
outboundReads, err := fks.outbound.CollectSpansForValues(values)
if err != nil {
return nil, err
}
return append(inboundReads, outboundReads...), nil
}
type baseFKHelper struct {
txn *client.Txn
rf RowFetcher
searchTable *TableDescriptor // the table being searched (for err msg)
searchIdx *IndexDescriptor // the index that must (not) contain a value
prefixLen int
writeIdx IndexDescriptor // the index we want to modify
searchPrefix []byte // prefix of keys in searchIdx
ids map[ColumnID]int // col IDs
}
func makeBaseFKHelper(
txn *client.Txn,
otherTables TableLookupsByID,
writeIdx IndexDescriptor,
ref ForeignKeyReference,
colMap map[ColumnID]int,
alloc *DatumAlloc,
) (baseFKHelper, error) {
b := baseFKHelper{txn: txn, writeIdx: writeIdx, searchTable: otherTables[ref.Table].Table}
if b.searchTable == nil {
return b, errors.Errorf("referenced table %d not in provided table map %+v", ref.Table, otherTables)
}
b.searchPrefix = MakeIndexKeyPrefix(b.searchTable, ref.Index)
searchIdx, err := b.searchTable.FindIndexByID(ref.Index)
if err != nil {
return b, err
}
b.prefixLen = len(searchIdx.ColumnIDs)
if len(writeIdx.ColumnIDs) < b.prefixLen {
b.prefixLen = len(writeIdx.ColumnIDs)
}
b.searchIdx = searchIdx
ids := ColIDtoRowIndexFromCols(b.searchTable.Columns)
isSecondary := b.searchTable.PrimaryIndex.ID != searchIdx.ID
err = b.rf.Init(b.searchTable, ids, searchIdx, false, /* reverse */
isSecondary, b.searchTable.Columns, nil,
false /* returnRangeInfo */, alloc)
if err != nil {
return b, err
}
b.ids = make(map[ColumnID]int, len(writeIdx.ColumnIDs))
nulls := true
for i, writeColID := range writeIdx.ColumnIDs[:b.prefixLen] {
if found, ok := colMap[writeColID]; ok {
b.ids[searchIdx.ColumnIDs[i]] = found
nulls = false
} else if !nulls {
return b, errors.Errorf("missing value for column %q in multi-part foreign key", writeIdx.ColumnNames[i])
}
}
if nulls {
return b, errSkipUnusedFK
}
return b, nil
}
// check performs the foreign key constraint check that this baseFKHelper was
// set up to perform, against the input datums. It returns true if a matching
// foreign key was found.
// TODO(dt): Batch checks of many rows.
func (f baseFKHelper) check(ctx context.Context, values parser.Datums, traceKV bool) (bool, error) {
span, err := f.spanForValues(values)
if err != nil {
return false, err
}
if traceKV {
log.VEventf(ctx, 2, "Scan %s -> %s", span.Key, span.EndKey)
}
err = f.rf.StartScan(ctx, f.txn, roachpb.Spans{span}, true /* limit batches */, 1)
if err != nil {
return false, err
}
return !f.rf.kvEnd, nil
}
func (f baseFKHelper) spanForValues(values parser.Datums) (roachpb.Span, error) {
var key roachpb.Key
if values != nil {
keyBytes, _, err := EncodePartialIndexKey(
f.searchTable, f.searchIdx, f.prefixLen, f.ids, values, f.searchPrefix)
if err != nil {
return roachpb.Span{}, err
}
key = roachpb.Key(keyBytes)
} else {
key = roachpb.Key(f.searchPrefix)
}
return roachpb.Span{Key: key, EndKey: key.PrefixEnd()}, nil
}
func (f baseFKHelper) span() roachpb.Span {
key := roachpb.Key(f.searchPrefix)
return roachpb.Span{Key: key, EndKey: key.PrefixEnd()}
}
// FkSpanCollector can collect the spans that foreign key validation will touch.
type FkSpanCollector interface {
CollectSpans() roachpb.Spans
CollectSpansForValues(values parser.Datums) (roachpb.Spans, error)
}
var _ FkSpanCollector = fkInsertHelper{}
var _ FkSpanCollector = fkDeleteHelper{}
var _ FkSpanCollector = fkUpdateHelper{}
func collectSpansWithFKMap(fks map[IndexID][]baseFKHelper) roachpb.Spans {
var reads roachpb.Spans
for idx := range fks {
for _, fk := range fks[idx] {
reads = append(reads, fk.span())
}
}
return reads
}
func collectSpansForValuesWithFKMap(
fks map[IndexID][]baseFKHelper, values parser.Datums,
) (roachpb.Spans, error) {
var reads roachpb.Spans
for idx := range fks {
for _, fk := range fks[idx] {
read, err := fk.spanForValues(values)
if err != nil {
return nil, err
}
reads = append(reads, read)
}
}
return reads, nil
}