github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/merge/merge.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package merge
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  const argName = "merge"
    26  
    27  func (arg *Argument) String(buf *bytes.Buffer) {
    28  	buf.WriteString(argName)
    29  	buf.WriteString(": union all ")
    30  }
    31  
    32  func (arg *Argument) Prepare(proc *process.Process) error {
    33  	ap := arg
    34  	ap.ctr = new(container)
    35  	ap.ctr.InitReceiver(proc, true)
    36  	return nil
    37  }
    38  
    39  func (arg *Argument) Call(proc *process.Process) (vm.CallResult, error) {
    40  	if err, isCancel := vm.CancelCheck(proc); isCancel {
    41  		return vm.CancelResult, err
    42  	}
    43  
    44  	anal := proc.GetAnalyze(arg.GetIdx(), arg.GetParallelIdx(), arg.GetParallelMajor())
    45  	anal.Start()
    46  	defer anal.Stop()
    47  	var end bool
    48  	result := vm.NewCallResult()
    49  	if arg.buf != nil {
    50  		proc.PutBatch(arg.buf)
    51  		arg.buf = nil
    52  	}
    53  
    54  	for {
    55  		arg.buf, end, _ = arg.ctr.ReceiveFromAllRegs(anal)
    56  		if end {
    57  			result.Status = vm.ExecStop
    58  			return result, nil
    59  		}
    60  
    61  		if arg.buf.Last() && arg.SinkScan {
    62  			proc.PutBatch(arg.buf)
    63  			continue
    64  		}
    65  		break
    66  	}
    67  
    68  	anal.Input(arg.buf, arg.GetIsFirst())
    69  	anal.Output(arg.buf, arg.GetIsLast())
    70  	result.Batch = arg.buf
    71  	return result, nil
    72  }