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

     1  // Copyright 2016 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 sql
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    20  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    21  	"github.com/cockroachdb/errors"
    22  )
    23  
    24  func initBackfillerSpec(
    25  	backfillType backfillType,
    26  	desc sqlbase.TableDescriptor,
    27  	duration time.Duration,
    28  	chunkSize int64,
    29  	otherTables []sqlbase.TableDescriptor,
    30  	readAsOf hlc.Timestamp,
    31  ) (execinfrapb.BackfillerSpec, error) {
    32  	ret := execinfrapb.BackfillerSpec{
    33  		Table:       desc,
    34  		Duration:    duration,
    35  		ChunkSize:   chunkSize,
    36  		OtherTables: otherTables,
    37  		ReadAsOf:    readAsOf,
    38  	}
    39  	switch backfillType {
    40  	case indexBackfill:
    41  		ret.Type = execinfrapb.BackfillerSpec_Index
    42  	case columnBackfill:
    43  		ret.Type = execinfrapb.BackfillerSpec_Column
    44  	default:
    45  		return execinfrapb.BackfillerSpec{}, errors.Errorf("bad backfill type %d", backfillType)
    46  	}
    47  	return ret, nil
    48  }
    49  
    50  // createBackfiller generates a plan consisting of index/column backfiller
    51  // processors, one for each node that has spans that we are reading. The plan is
    52  // finalized.
    53  func (dsp *DistSQLPlanner) createBackfiller(
    54  	planCtx *PlanningCtx,
    55  	backfillType backfillType,
    56  	desc sqlbase.TableDescriptor,
    57  	duration time.Duration,
    58  	chunkSize int64,
    59  	spans []roachpb.Span,
    60  	otherTables []sqlbase.TableDescriptor,
    61  	readAsOf hlc.Timestamp,
    62  ) (PhysicalPlan, error) {
    63  	spec, err := initBackfillerSpec(backfillType, desc, duration, chunkSize, otherTables, readAsOf)
    64  	if err != nil {
    65  		return PhysicalPlan{}, err
    66  	}
    67  
    68  	spanPartitions, err := dsp.PartitionSpans(planCtx, spans)
    69  	if err != nil {
    70  		return PhysicalPlan{}, err
    71  	}
    72  
    73  	var p PhysicalPlan
    74  	p.ResultRouters = make([]physicalplan.ProcessorIdx, len(spanPartitions))
    75  	for i, sp := range spanPartitions {
    76  		ib := &execinfrapb.BackfillerSpec{}
    77  		*ib = spec
    78  		ib.Spans = make([]execinfrapb.TableReaderSpan, len(sp.Spans))
    79  		for j := range sp.Spans {
    80  			ib.Spans[j].Span = sp.Spans[j]
    81  		}
    82  
    83  		proc := physicalplan.Processor{
    84  			Node: sp.Node,
    85  			Spec: execinfrapb.ProcessorSpec{
    86  				Core:   execinfrapb.ProcessorCoreUnion{Backfiller: ib},
    87  				Output: []execinfrapb.OutputRouterSpec{{Type: execinfrapb.OutputRouterSpec_PASS_THROUGH}},
    88  			},
    89  		}
    90  
    91  		pIdx := p.AddProcessor(proc)
    92  		p.ResultRouters[i] = pIdx
    93  	}
    94  	dsp.FinalizePlan(planCtx, &p)
    95  	return p, nil
    96  }