-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtablewriter.go
446 lines (385 loc) · 11.8 KB
/
tablewriter.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// 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.
//
// Author: Daniel Harrison ([email protected])
package sql
import (
"bytes"
"fmt"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/cockroachdb/cockroach/sql/sqlbase"
"github.com/cockroachdb/cockroach/util/log"
)
// tableWriter handles writing kvs and forming table rows.
//
// Usage:
// err := tw.init(txn)
// // Handle err.
// for {
// values := ...
// row, err := tw.row(values)
// // Handle err.
// }
// err := tw.finalize()
// // Handle err.
type tableWriter interface {
// init provides the tableWriter with a Txn to write to and returns an error
// if it was misconfigured.
init(*client.Txn) error
// row performs a sql row modification (tableInserter performs an insert,
// etc). It batches up writes to the init'd txn and periodically sends them.
// The returned DTuple is suitable for use with returningHelper.
row(parser.DTuple) (parser.DTuple, error)
// finalize flushes out any remaining writes. It is called after all calls to
// row.
finalize() error
}
var _ tableWriter = (*tableInserter)(nil)
var _ tableWriter = (*tableUpdater)(nil)
var _ tableWriter = (*tableUpserter)(nil)
var _ tableWriter = (*tableDeleter)(nil)
// tableInserter handles writing kvs and forming table rows for inserts.
type tableInserter struct {
ri rowInserter
autoCommit bool
// Set by init.
txn *client.Txn
b *client.Batch
}
func (ti *tableInserter) init(txn *client.Txn) error {
ti.txn = txn
ti.b = txn.NewBatch()
return nil
}
func (ti *tableInserter) row(values parser.DTuple) (parser.DTuple, error) {
return nil, ti.ri.insertRow(ti.b, values)
}
func (ti *tableInserter) finalize() error {
var err error
if ti.autoCommit {
// An auto-txn can commit the transaction with the batch. This is an
// optimization to avoid an extra round-trip to the transaction
// coordinator.
err = ti.txn.CommitInBatch(ti.b)
} else {
err = ti.txn.Run(ti.b)
}
if err != nil {
return convertBatchError(ti.ri.helper.tableDesc, ti.b)
}
return nil
}
// tableUpdater handles writing kvs and forming table rows for updates.
type tableUpdater struct {
ru rowUpdater
autoCommit bool
// Set by init.
txn *client.Txn
b *client.Batch
}
func (tu *tableUpdater) init(txn *client.Txn) error {
tu.txn = txn
tu.b = txn.NewBatch()
return nil
}
func (tu *tableUpdater) row(values parser.DTuple) (parser.DTuple, error) {
oldValues := values[:len(tu.ru.fetchCols)]
updateValues := values[len(tu.ru.fetchCols):]
return tu.ru.updateRow(tu.b, oldValues, updateValues)
}
func (tu *tableUpdater) finalize() error {
var err error
if tu.autoCommit {
// An auto-txn can commit the transaction with the batch. This is an
// optimization to avoid an extra round-trip to the transaction
// coordinator.
err = tu.txn.CommitInBatch(tu.b)
} else {
err = tu.txn.Run(tu.b)
}
if err != nil {
return convertBatchError(tu.ru.helper.tableDesc, tu.b)
}
return nil
}
type tableUpsertEvaler interface {
// eval returns the values for the update case of an upsert, given the row
// that would have been inserted and the existing (conflicting) values.
eval(insertRow parser.DTuple, existingRow parser.DTuple) (parser.DTuple, error)
}
// tableUpserter handles writing kvs and forming table rows for upserts.
type tableUpserter struct {
ri rowInserter
updateCols []sqlbase.ColumnDescriptor
conflictIndex sqlbase.IndexDescriptor
evaler tableUpsertEvaler
// Set by init.
txn *client.Txn
tableDesc *sqlbase.TableDescriptor
ru rowUpdater
updateColIDtoRowIndex map[sqlbase.ColumnID]int
a sqlbase.DatumAlloc
fetcher sqlbase.RowFetcher
// Batched up in run/flush.
insertRows []parser.DTuple
// For allocation avoidance.
indexKeyPrefix []byte
}
func (tu *tableUpserter) init(txn *client.Txn) error {
tu.txn = txn
tu.tableDesc = tu.ri.helper.tableDesc
tu.indexKeyPrefix = sqlbase.MakeIndexKeyPrefix(tu.tableDesc.ID, tu.tableDesc.PrimaryIndex.ID)
var err error
tu.ru, err = makeRowUpdater(tu.tableDesc, tu.updateCols)
if err != nil {
return err
}
// TODO(dan): Use ru.fetchCols to compute the fetch selectors.
tu.updateColIDtoRowIndex = make(map[sqlbase.ColumnID]int)
for i, updateCol := range tu.ru.updateCols {
tu.updateColIDtoRowIndex[updateCol.ID] = i
}
valNeededForCol := make([]bool, len(tu.ru.fetchCols))
for i := range valNeededForCol {
// TODO(dan): We only need the primary key columns, the update columns, and
// anything referenced by an UpdateExpr.
valNeededForCol[i] = true
}
err = tu.fetcher.Init(
tu.tableDesc, tu.ru.fetchColIDtoRowIndex, &tu.tableDesc.PrimaryIndex, false, false,
valNeededForCol)
if err != nil {
return err
}
return nil
}
func (tu *tableUpserter) row(row parser.DTuple) (parser.DTuple, error) {
// TODO(dan): If a table has one index and every column is being upserted,
// then it can be done entirely with Puts. This would greatly help the
// key/value table case.
tu.insertRows = append(tu.insertRows, row)
// TODO(dan): If len(tu.insertRows) > some threshold, call flush().
return nil, nil
}
// flush commits to tu.txn any rows batched up in tu.insertRows.
func (tu *tableUpserter) flush() error {
defer func() {
tu.insertRows = nil
}()
existingRows, err := tu.fetchExisting()
if err != nil {
return err
}
b := tu.txn.NewBatch()
for i, insertRow := range tu.insertRows {
existingRow := existingRows[i]
if existingRow == nil {
err := tu.ri.insertRow(b, insertRow)
if err != nil {
return err
}
} else {
existingValues := existingRow[:len(tu.ru.fetchCols)]
updateValues, err := tu.evaler.eval(insertRow, existingValues)
if err != nil {
return err
}
_, err = tu.ru.updateRow(b, existingValues, updateValues)
if err != nil {
return err
}
}
}
if err := tu.txn.Run(b); err != nil {
return convertBatchError(tu.tableDesc, b)
}
return nil
}
// upsertRowPKs returns the primary keys of any rows with potential upsert
// conflicts.
//
// If the conflict index is the primary index, we can compute them directly.
// In this case, the slice will be filled, but not all rows will have
// conflicts.
//
// Otherwise, compute the keys for the conflict index and look them up. The
// primary keys can be constructed from the entries that come back. In this
// case, some spots in the slice will be nil (indicating no conflict) and the
// others will be conflicting rows.
func (tu *tableUpserter) upsertRowPKs() ([]roachpb.Key, error) {
upsertRowPKs := make([]roachpb.Key, len(tu.insertRows))
if tu.conflictIndex.ID == tu.tableDesc.PrimaryIndex.ID {
for i, insertRow := range tu.insertRows {
upsertRowPK, _, err := sqlbase.EncodeIndexKey(
&tu.conflictIndex, tu.ri.insertColIDtoRowIndex, insertRow, tu.indexKeyPrefix)
if err != nil {
return nil, err
}
upsertRowPKs[i] = upsertRowPK
}
} else {
b := tu.txn.NewBatch()
for _, insertRow := range tu.insertRows {
entry, err := sqlbase.EncodeSecondaryIndex(
tu.tableDesc.ID, tu.conflictIndex, tu.ri.insertColIDtoRowIndex, insertRow)
if err != nil {
return nil, err
}
if log.V(2) {
log.Infof("Get %s\n", entry.Key)
}
b.Get(entry.Key)
}
if err := tu.txn.Run(b); err != nil {
return nil, err
}
for i, result := range b.Results {
if len(result.Rows) == 0 {
// No conflict for this row, so leave upsertRowPKs[i] as nil.
} else if len(result.Rows) == 1 {
if result.Rows[0].Value == nil {
upsertRowPKs[i] = nil
} else {
upsertRowPK, err := sqlbase.ExtractIndexKey(&tu.a, tu.tableDesc, result.Rows[0])
if err != nil {
return nil, err
}
upsertRowPKs[i] = upsertRowPK
}
}
}
}
return upsertRowPKs, nil
}
// fetchExisting returns any existing rows in the table that conflict with the
// ones in tu.insertRows. The returned slice is the same length as tu.insertRows
// and a nil entry indicates no conflict.
func (tu *tableUpserter) fetchExisting() ([]parser.DTuple, error) {
primaryKeys, err := tu.upsertRowPKs()
if err != nil {
return nil, err
}
pkSpans := make(sqlbase.Spans, 0, len(primaryKeys))
rowIdxForPrimaryKey := make(map[string]int, len(primaryKeys))
for i, primaryKey := range primaryKeys {
if primaryKey != nil {
pkSpans = append(pkSpans, sqlbase.Span{Start: primaryKey, End: primaryKey.PrefixEnd()})
if _, ok := rowIdxForPrimaryKey[string(primaryKey)]; ok {
return nil, fmt.Errorf("UPSERT/ON CONFLICT DO UPDATE command cannot affect row a second time")
}
rowIdxForPrimaryKey[string(primaryKey)] = i
}
}
if len(pkSpans) == 0 {
// Every key was empty, so there's nothing to fetch.
return make([]parser.DTuple, len(primaryKeys)), nil
}
if err := tu.fetcher.StartScan(tu.txn, pkSpans, int64(len(pkSpans))); err != nil {
return nil, err
}
rows := make([]parser.DTuple, len(primaryKeys))
for {
row, err := tu.fetcher.NextRow()
if err != nil {
return nil, err
}
if row == nil {
break // Done
}
rowPrimaryKey, _, err := sqlbase.EncodeIndexKey(
&tu.tableDesc.PrimaryIndex, tu.ru.fetchColIDtoRowIndex, row, tu.indexKeyPrefix)
if err != nil {
return nil, err
}
rows[rowIdxForPrimaryKey[string(rowPrimaryKey)]] = row
}
return rows, nil
}
func (tu *tableUpserter) finalize() error {
return tu.flush()
}
// tableDeleter handles writing kvs and forming table rows for deletes.
type tableDeleter struct {
rd rowDeleter
autoCommit bool
// Set by init.
txn *client.Txn
b *client.Batch
}
func (td *tableDeleter) init(txn *client.Txn) error {
td.txn = txn
td.b = txn.NewBatch()
return nil
}
func (td *tableDeleter) row(values parser.DTuple) (parser.DTuple, error) {
return nil, td.rd.deleteRow(td.b, values)
}
func (td *tableDeleter) finalize() error {
if td.autoCommit {
// An auto-txn can commit the transaction with the batch. This is an
// optimization to avoid an extra round-trip to the transaction
// coordinator.
return td.txn.CommitInBatch(td.b)
}
return td.txn.Run(td.b)
}
// fastPathAvailable returns true if the fastDelete optimization can be used.
func (td *tableDeleter) fastPathAvailable() bool {
if len(td.rd.helper.indexes) != 0 {
if log.V(2) {
log.Infof("delete forced to scan: values required to update %d secondary indexes", len(td.rd.helper.indexes))
}
return false
}
return true
}
// fastDelete adds to the batch the kv operations necessary to delete sql rows
// without knowing the values that are currently present. fastDelete calls
// finalize, so it should not be called after.
func (td *tableDeleter) fastDelete(
scan *scanNode,
) (rowCount int, err error) {
for _, span := range scan.spans {
if log.V(2) {
log.Infof("Skipping scan and just deleting %s - %s", span.Start, span.End)
}
td.b.DelRange(span.Start, span.End, true)
}
err = td.finalize()
if err != nil {
return 0, err
}
for _, r := range td.b.Results {
var prev []byte
for _, i := range r.Keys {
// If prefix is same, don't bother decoding key.
if len(prev) > 0 && bytes.HasPrefix(i, prev) {
continue
}
after, err := scan.fetcher.ReadIndexKey(i)
if err != nil {
return 0, err
}
k := i[:len(i)-len(after)]
if !bytes.Equal(k, prev) {
prev = k
rowCount++
}
}
}
td.b = nil
return rowCount, nil
}