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

     1  // Copyright 2018 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 colexec
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/col/coldata"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/colexecbase"
    18  )
    19  
    20  // oneShotOp is an operator that does an arbitrary operation on the first batch
    21  // that it gets, then deletes itself from the operator tree. This is useful for
    22  // first-Next initialization that has to happen in an operator.
    23  type oneShotOp struct {
    24  	OneInputNode
    25  
    26  	outputSourceRef *colexecbase.Operator
    27  
    28  	fn func(batch coldata.Batch)
    29  }
    30  
    31  var _ colexecbase.Operator = &oneShotOp{}
    32  
    33  func (o *oneShotOp) Init() {
    34  	o.input.Init()
    35  }
    36  
    37  func (o *oneShotOp) Next(ctx context.Context) coldata.Batch {
    38  	batch := o.input.Next(ctx)
    39  
    40  	// Do our one-time work.
    41  	o.fn(batch)
    42  	// Swap out our output's input with our input, so we don't have to get called
    43  	// anymore.
    44  	*o.outputSourceRef = o.input
    45  
    46  	return batch
    47  }