-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
row_converter.go
550 lines (503 loc) · 18.5 KB
/
row_converter.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package row
import (
"context"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/transform"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/errors"
)
// KVInserter implements the putter interface.
type KVInserter func(roachpb.KeyValue)
// CPut is not implmented.
func (i KVInserter) CPut(key, value interface{}, expValue []byte) {
panic("unimplemented")
}
// Del is not implemented.
func (i KVInserter) Del(key ...interface{}) {
// This is called when there are multiple column families to ensure that
// existing data is cleared. With the exception of IMPORT INTO, the entire
// existing keyspace in any IMPORT is guaranteed to be empty, so we don't have
// to worry about it.
//
// IMPORT INTO disallows overwriting an existing row, so we're also okay here.
// The reason this works is that row existence is precisely defined as whether
// column family 0 exists, meaning that we write column family 0 even if all
// the non-pk columns in it are NULL. It follows that either the row does
// exist and the imported column family 0 will conflict (and the IMPORT INTO
// will fail) or the row does not exist (and thus the column families are all
// empty).
}
// Put method of the putter interface.
func (i KVInserter) Put(key, value interface{}) {
i(roachpb.KeyValue{
Key: *key.(*roachpb.Key),
Value: *value.(*roachpb.Value),
})
}
// InitPut method of the putter interface.
func (i KVInserter) InitPut(key, value interface{}, failOnTombstones bool) {
i(roachpb.KeyValue{
Key: *key.(*roachpb.Key),
Value: *value.(*roachpb.Value),
})
}
// GenerateInsertRow prepares a row tuple for insertion. It fills in default
// expressions, verifies non-nullable columns, and checks column widths.
//
// The result is a row tuple providing values for every column in insertCols.
// This results contains:
//
// - the values provided by rowVals, the tuple of source values. The
// caller ensures this provides values 1-to-1 to the prefix of
// insertCols that was specified explicitly in the INSERT statement.
// - the default values for any additional columns in insertCols that
// have default values in defaultExprs.
// - the computed values for any additional columns in insertCols
// that are computed. The mapping in rowContainerForComputedCols
// maps the indexes of the comptuedCols/computeExpr slices
// back into indexes in the result row tuple.
//
func GenerateInsertRow(
defaultExprs []tree.TypedExpr,
computeExprs []tree.TypedExpr,
insertCols []descpb.ColumnDescriptor,
computedColsLookup []descpb.ColumnDescriptor,
evalCtx *tree.EvalContext,
tableDesc catalog.TableDescriptor,
rowVals tree.Datums,
rowContainerForComputedVals *schemaexpr.RowIndexedVarContainer,
) (tree.Datums, error) {
// The values for the row may be shorter than the number of columns being
// inserted into. Generate default values for those columns using the
// default expressions. This will not happen if the row tuple was produced
// by a ValuesClause, because all default expressions will have been populated
// already by fillDefaults.
if len(rowVals) < len(insertCols) {
// It's not cool to append to the slice returned by a node; make a copy.
oldVals := rowVals
rowVals = make(tree.Datums, len(insertCols))
copy(rowVals, oldVals)
for i := len(oldVals); i < len(insertCols); i++ {
if defaultExprs == nil {
rowVals[i] = tree.DNull
continue
}
d, err := defaultExprs[i].Eval(evalCtx)
if err != nil {
return nil, err
}
rowVals[i] = d
}
}
// Generate the computed values, if needed.
if len(computeExprs) > 0 {
rowContainerForComputedVals.CurSourceRow = rowVals
evalCtx.PushIVarContainer(rowContainerForComputedVals)
for i := range computedColsLookup {
// Note that even though the row is not fully constructed at this point,
// since we disallow computed columns from referencing other computed
// columns, all the columns which could possibly be referenced *are*
// available.
col := computedColsLookup[i]
computeIdx := rowContainerForComputedVals.Mapping.GetDefault(col.ID)
if !col.IsComputed() {
continue
}
d, err := computeExprs[computeIdx].Eval(evalCtx)
if err != nil {
return nil, errors.Wrapf(err,
"computed column %s",
tree.ErrString((*tree.Name)(&col.Name)))
}
rowVals[computeIdx] = d
}
evalCtx.PopIVarContainer()
}
// Verify the column constraints.
//
// We would really like to use enforceLocalColumnConstraints() here,
// but this is not possible because of some brain damage in the
// Insert() constructor, which causes insertCols to contain
// duplicate columns descriptors: computed columns are listed twice,
// one will receive a NULL value and one will receive a comptued
// value during execution. It "works out in the end" because the
// latter (non-NULL) value overwrites the earlier, but
// enforceLocalColumnConstraints() does not know how to reason about
// this.
//
// In the end it does not matter much, this code is going away in
// favor of the (simpler, correct) code in the CBO.
// Check to see if NULL is being inserted into any non-nullable column.
for _, col := range tableDesc.WritableColumns() {
if !col.IsNullable() {
if i, ok := rowContainerForComputedVals.Mapping.Get(col.GetID()); !ok || rowVals[i] == tree.DNull {
return nil, sqlerrors.NewNonNullViolationError(col.GetName())
}
}
}
// Ensure that the values honor the specified column widths.
for i := 0; i < len(insertCols); i++ {
outVal, err := tree.AdjustValueToType(insertCols[i].Type, rowVals[i])
if err != nil {
return nil, err
}
rowVals[i] = outVal
}
return rowVals, nil
}
// KVBatch represents a batch of KVs generated from converted rows.
type KVBatch struct {
// Source is where the row data in the batch came from.
Source int32
// LastRow is the index of the last converted row in source in this batch.
LastRow int64
// Progress represents the fraction of the input that generated this row.
Progress float32
// KVs is the actual converted KV data.
KVs []roachpb.KeyValue
}
// DatumRowConverter converts Datums into kvs and streams it to the destination
// channel.
type DatumRowConverter struct {
// current row buf
Datums []tree.Datum
// kv destination and current batch
KvCh chan<- KVBatch
KvBatch KVBatch
BatchCap int
tableDesc catalog.TableDescriptor
// Tracks which column indices in the set of visible columns are part of the
// user specified target columns. This can be used before populating Datums
// to filter out unwanted column data.
TargetColOrds util.FastIntSet
// The rest of these are derived from tableDesc, just cached here.
ri Inserter
EvalCtx *tree.EvalContext
cols []descpb.ColumnDescriptor
VisibleCols []descpb.ColumnDescriptor
VisibleColTypes []*types.T
computedExprs []tree.TypedExpr
defaultCache []tree.TypedExpr
computedIVarContainer schemaexpr.RowIndexedVarContainer
// FractionFn is used to set the progress header in KVBatches.
CompletedRowFn func() int64
FractionFn func() float32
}
var kvDatumRowConverterBatchSize = util.ConstantWithMetamorphicTestValue(
"datum-row-converter-batch-size",
5000, /* defaultValue */
1, /* metamorphicValue */
)
// TestingSetDatumRowConverterBatchSize sets kvDatumRowConverterBatchSize and
// returns function to reset this setting back to its old value.
func TestingSetDatumRowConverterBatchSize(newSize int) func() {
oldSize := kvDatumRowConverterBatchSize
kvDatumRowConverterBatchSize = newSize
return func() {
kvDatumRowConverterBatchSize = oldSize
}
}
// getSequenceAnnotation returns a mapping from sequence name to metadata
// related to the sequence which will be used when evaluating the default
// expression using the sequence.
func (c *DatumRowConverter) getSequenceAnnotation(
evalCtx *tree.EvalContext, cols []descpb.ColumnDescriptor,
) (map[string]*SequenceMetadata, map[descpb.ID]*SequenceMetadata, error) {
// Identify the sequences used in all the columns.
sequenceIDs := make(map[descpb.ID]struct{})
for _, col := range cols {
for _, id := range col.UsesSequenceIds {
sequenceIDs[id] = struct{}{}
}
}
if len(sequenceIDs) == 0 {
return nil, nil, nil
}
var seqNameToMetadata map[string]*SequenceMetadata
var seqIDToMetadata map[descpb.ID]*SequenceMetadata
err := evalCtx.DB.Txn(evalCtx.Context, func(ctx context.Context, txn *kv.Txn) error {
seqNameToMetadata = make(map[string]*SequenceMetadata)
seqIDToMetadata = make(map[descpb.ID]*SequenceMetadata)
txn.SetFixedTimestamp(ctx, hlc.Timestamp{WallTime: evalCtx.TxnTimestamp.UnixNano()})
for seqID := range sequenceIDs {
seqDesc, err := catalogkv.MustGetTableDescByID(ctx, txn, evalCtx.Codec, seqID)
if err != nil {
return err
}
seqOpts := seqDesc.GetSequenceOpts()
if seqOpts == nil {
return errors.Newf("descriptor %s is not a sequence", seqDesc.GetName())
}
seqMetadata := &SequenceMetadata{
id: seqID,
seqDesc: seqDesc,
}
seqNameToMetadata[seqDesc.GetName()] = seqMetadata
seqIDToMetadata[seqDesc.GetID()] = seqMetadata
}
return nil
})
return seqNameToMetadata, seqIDToMetadata, err
}
// NewDatumRowConverter returns an instance of a DatumRowConverter.
func NewDatumRowConverter(
ctx context.Context,
tableDesc catalog.TableDescriptor,
targetColNames tree.NameList,
evalCtx *tree.EvalContext,
kvCh chan<- KVBatch,
seqChunkProvider *SeqChunkProvider,
metrics *Metrics,
) (*DatumRowConverter, error) {
c := &DatumRowConverter{
tableDesc: tableDesc,
KvCh: kvCh,
EvalCtx: evalCtx.Copy(),
}
var targetCols []catalog.Column
var err error
// IMPORT INTO allows specifying target columns which could be a subset of
// immutDesc.VisibleColumns. If no target columns are specified we assume all
// columns of the table descriptor are to be inserted into.
if len(targetColNames) != 0 {
if targetCols, err = colinfo.ProcessTargetColumns(tableDesc, targetColNames,
true /* ensureColumns */, false /* allowMutations */); err != nil {
return nil, err
}
} else {
targetCols = tableDesc.VisibleColumns()
}
targetColDescriptors := make([]descpb.ColumnDescriptor, len(targetCols))
var targetColIDs catalog.TableColSet
for i, col := range targetCols {
c.TargetColOrds.Add(i)
targetColIDs.Add(col.GetID())
targetColDescriptors[i] = *col.ColumnDesc()
}
var txCtx transform.ExprTransformContext
semaCtx := tree.MakeSemaContext()
relevantColumns := func(col *descpb.ColumnDescriptor) bool {
return col.HasDefault() || col.IsComputed()
}
cols := schemaexpr.ProcessColumnSet(
targetColDescriptors, tableDesc, relevantColumns)
defaultExprs, err := schemaexpr.MakeDefaultExprs(ctx, cols, &txCtx, c.EvalCtx, &semaCtx)
if err != nil {
return nil, errors.Wrap(err, "process default and computed columns")
}
ri, err := MakeInserter(
ctx,
nil, /* txn */
evalCtx.Codec,
tableDesc,
cols,
&rowenc.DatumAlloc{},
&evalCtx.Settings.SV,
evalCtx.SessionData.Internal,
metrics,
)
if err != nil {
return nil, errors.Wrap(err, "make row inserter")
}
c.ri = ri
c.cols = cols
c.VisibleCols = targetColDescriptors
c.VisibleColTypes = make([]*types.T, len(c.VisibleCols))
for i := range c.VisibleCols {
c.VisibleColTypes[i] = c.VisibleCols[i].Type
}
c.Datums = make([]tree.Datum, len(targetColDescriptors), len(cols))
c.defaultCache = make([]tree.TypedExpr, len(cols))
annot := make(tree.Annotations, 1)
var cellInfoAnnot CellInfoAnnotation
// Currently, this is only true for an IMPORT INTO CSV.
if seqChunkProvider != nil {
seqNameToMetadata, seqIDToMetadata, err := c.getSequenceAnnotation(evalCtx, c.cols)
if err != nil {
return nil, err
}
cellInfoAnnot.seqNameToMetadata = seqNameToMetadata
cellInfoAnnot.seqIDToMetadata = seqIDToMetadata
cellInfoAnnot.seqChunkProvider = seqChunkProvider
}
cellInfoAnnot.uniqueRowIDInstance = 0
annot.Set(cellInfoAddr, &cellInfoAnnot)
c.EvalCtx.Annotations = &annot
// Check for a hidden column. This should be the unique_rowid PK if present.
// In addition, check for non-targeted columns with non-null DEFAULT expressions.
// If the DEFAULT expression is immutable, we can store it in the cache so that it
// doesn't have to be reevaluated for every row.
for i := range cols {
col := &cols[i]
if col.DefaultExpr != nil {
// Placeholder for columns with default values that will be evaluated when
// each import row is being created.
typedExpr, volatile, err := sanitizeExprsForImport(ctx, c.EvalCtx, defaultExprs[i], col.Type)
if err != nil {
// This expression may not be safe for import but we don't want to
// call the user out at this stage: targeted columns may not have
// been identified now (e.g. "IMPORT PGDUMP...") and we want to
// throw an error only at the "Row" stage when the targeted columns
// have been identified.
c.defaultCache[i] = &unsafeErrExpr{
err: errors.Wrapf(err, "default expression %s unsafe for import", defaultExprs[i].String()),
}
} else {
c.defaultCache[i] = typedExpr
if volatile == overrideImmutable {
// This default expression isn't volatile, so we can evaluate once
// here and memoize it.
c.defaultCache[i], err = c.defaultCache[i].Eval(c.EvalCtx)
if err != nil {
return nil, errors.Wrapf(err, "error evaluating default expression")
}
}
}
if !targetColIDs.Contains(col.ID) {
c.Datums = append(c.Datums, nil)
}
}
if col.IsComputed() && !targetColIDs.Contains(col.ID) {
c.Datums = append(c.Datums, nil)
}
}
if len(c.Datums) != len(cols) {
return nil, errors.New("unexpected hidden column")
}
padding := 2 * (len(tableDesc.PublicNonPrimaryIndexes()) + len(tableDesc.GetFamilies()))
c.BatchCap = kvDatumRowConverterBatchSize + padding
c.KvBatch.KVs = make([]roachpb.KeyValue, 0, c.BatchCap)
colDescs := make([]descpb.ColumnDescriptor, len(c.tableDesc.PublicColumns()))
colsOrdered := make([]descpb.ColumnDescriptor, len(cols))
for i, col := range c.tableDesc.PublicColumns() {
colDescs[i] = *col.ColumnDesc()
// We prefer to have the order of columns that will be sent into
// MakeComputedExprs to map that of Datums.
colsOrdered[ri.InsertColIDtoRowIndex.GetDefault(col.GetID())] = colDescs[i]
}
// Here, computeExprs will be nil if there's no computed column, or
// the list of computed expressions (including nil, for those columns
// that are not computed) otherwise, according to colsOrdered.
c.computedExprs, _, err = schemaexpr.MakeComputedExprs(
ctx,
colsOrdered,
colDescs,
c.tableDesc,
tree.NewUnqualifiedTableName(tree.Name(c.tableDesc.GetName())),
c.EvalCtx,
&semaCtx)
if err != nil {
return nil, errors.Wrapf(err, "error evaluating computed expression for IMPORT INTO")
}
c.computedIVarContainer = schemaexpr.RowIndexedVarContainer{
Mapping: ri.InsertColIDtoRowIndex,
Cols: make([]descpb.ColumnDescriptor, len(tableDesc.PublicColumns())),
}
for i, col := range tableDesc.PublicColumns() {
c.computedIVarContainer.Cols[i] = *col.ColumnDesc()
}
return c, nil
}
const rowIDBits = 64 - builtins.NodeIDBits
// Row inserts kv operations into the current kv batch, and triggers a SendBatch
// if necessary.
func (c *DatumRowConverter) Row(ctx context.Context, sourceID int32, rowIndex int64) error {
getCellInfoAnnotation(c.EvalCtx.Annotations).reset(sourceID, rowIndex)
for i := range c.cols {
col := &c.cols[i]
if col.DefaultExpr != nil {
// If this column is targeted, then the evaluation is a no-op except to
// make one evaluation just in case we have random() default expression
// to ensure that the positions we advance in a row is the same as the
// number of instances the function random() appears in a row.
// TODO (anzoteh96): Optimize this part of code when there's no expression
// involving random(), gen_random_uuid(), or anything like that.
datum, err := c.defaultCache[i].Eval(c.EvalCtx)
if !c.TargetColOrds.Contains(i) {
if err != nil {
return errors.Wrapf(
err, "error evaluating default expression %q", *col.DefaultExpr)
}
c.Datums[i] = datum
}
}
}
var computedColsLookup []descpb.ColumnDescriptor
if len(c.computedExprs) > 0 {
cols := c.tableDesc.PublicColumns()
computedColsLookup = make([]descpb.ColumnDescriptor, len(cols))
for i, col := range cols {
computedColsLookup[i] = *col.ColumnDesc()
}
}
insertRow, err := GenerateInsertRow(
c.defaultCache, c.computedExprs, c.cols, computedColsLookup, c.EvalCtx,
c.tableDesc, c.Datums, &c.computedIVarContainer)
if err != nil {
return errors.Wrap(err, "generate insert row")
}
// TODO(mgartner): Add partial index IDs to ignoreIndexes that we should
// not delete entries from.
var pm PartialIndexUpdateHelper
if err := c.ri.InsertRow(
ctx,
KVInserter(func(kv roachpb.KeyValue) {
kv.Value.InitChecksum(kv.Key)
c.KvBatch.KVs = append(c.KvBatch.KVs, kv)
}),
insertRow,
pm,
true, /* ignoreConflicts */
false, /* traceKV */
); err != nil {
return errors.Wrap(err, "insert row")
}
// If our batch is full, flush it and start a new one.
if len(c.KvBatch.KVs) >= kvDatumRowConverterBatchSize {
if err := c.SendBatch(ctx); err != nil {
return err
}
}
return nil
}
// SendBatch streams kv operations from the current KvBatch to the destination
// channel, and resets the KvBatch to empty.
func (c *DatumRowConverter) SendBatch(ctx context.Context) error {
if len(c.KvBatch.KVs) == 0 {
return nil
}
if c.FractionFn != nil {
c.KvBatch.Progress = c.FractionFn()
}
if c.CompletedRowFn != nil {
c.KvBatch.LastRow = c.CompletedRowFn()
}
select {
case c.KvCh <- c.KvBatch:
case <-ctx.Done():
return ctx.Err()
}
c.KvBatch.KVs = make([]roachpb.KeyValue, 0, c.BatchCap)
return nil
}