github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/rowexec/columnbackfiller.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package rowexec
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/kv"
    17  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/backfill"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
    21  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    22  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    23  )
    24  
    25  // columnBackfiller is a processor for backfilling columns.
    26  type columnBackfiller struct {
    27  	backfiller
    28  
    29  	backfill.ColumnBackfiller
    30  
    31  	desc        *sqlbase.ImmutableTableDescriptor
    32  	otherTables []*sqlbase.ImmutableTableDescriptor
    33  }
    34  
    35  var _ execinfra.Processor = &columnBackfiller{}
    36  var _ chunkBackfiller = &columnBackfiller{}
    37  
    38  func newColumnBackfiller(
    39  	ctx context.Context,
    40  	flowCtx *execinfra.FlowCtx,
    41  	processorID int32,
    42  	spec execinfrapb.BackfillerSpec,
    43  	post *execinfrapb.PostProcessSpec,
    44  	output execinfra.RowReceiver,
    45  ) (*columnBackfiller, error) {
    46  	otherTables := make([]*sqlbase.ImmutableTableDescriptor, len(spec.OtherTables))
    47  	for i, tbl := range spec.OtherTables {
    48  		otherTables[i] = sqlbase.NewImmutableTableDescriptor(tbl)
    49  	}
    50  	cb := &columnBackfiller{
    51  		desc:        sqlbase.NewImmutableTableDescriptor(spec.Table),
    52  		otherTables: otherTables,
    53  		backfiller: backfiller{
    54  			name:        "Column",
    55  			filter:      backfill.ColumnMutationFilter,
    56  			flowCtx:     flowCtx,
    57  			processorID: processorID,
    58  			output:      output,
    59  			spec:        spec,
    60  		},
    61  	}
    62  	cb.backfiller.chunks = cb
    63  
    64  	if err := cb.ColumnBackfiller.Init(ctx, cb.flowCtx.NewEvalCtx(), cb.desc); err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return cb, nil
    69  }
    70  
    71  func (cb *columnBackfiller) close(ctx context.Context) {}
    72  func (cb *columnBackfiller) prepare(ctx context.Context) error {
    73  	return nil
    74  }
    75  func (cb *columnBackfiller) flush(ctx context.Context) error {
    76  	return nil
    77  }
    78  func (cb *columnBackfiller) CurrentBufferFill() float32 {
    79  	return 0
    80  }
    81  
    82  // runChunk implements the chunkBackfiller interface.
    83  func (cb *columnBackfiller) runChunk(
    84  	ctx context.Context,
    85  	mutations []sqlbase.DescriptorMutation,
    86  	sp roachpb.Span,
    87  	chunkSize int64,
    88  	readAsOf hlc.Timestamp,
    89  ) (roachpb.Key, error) {
    90  	var key roachpb.Key
    91  	err := cb.flowCtx.Cfg.DB.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
    92  		if cb.flowCtx.Cfg.TestingKnobs.RunBeforeBackfillChunk != nil {
    93  			if err := cb.flowCtx.Cfg.TestingKnobs.RunBeforeBackfillChunk(sp); err != nil {
    94  				return err
    95  			}
    96  		}
    97  		if cb.flowCtx.Cfg.TestingKnobs.RunAfterBackfillChunk != nil {
    98  			defer cb.flowCtx.Cfg.TestingKnobs.RunAfterBackfillChunk()
    99  		}
   100  
   101  		// TODO(knz): do KV tracing in DistSQL processors.
   102  		var err error
   103  		key, err = cb.RunColumnBackfillChunk(
   104  			ctx,
   105  			txn,
   106  			cb.desc,
   107  			cb.otherTables,
   108  			sp,
   109  			chunkSize,
   110  			true,  /*alsoCommit*/
   111  			false, /*traceKV*/
   112  		)
   113  		return err
   114  	})
   115  	return key, err
   116  }