github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/mergerecursive/mergerecursive.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 mergerecursive
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  const argName = "merge_recursive"
    25  
    26  func (arg *Argument) String(buf *bytes.Buffer) {
    27  	buf.WriteString(argName)
    28  	buf.WriteString(": merge recursive ")
    29  }
    30  
    31  func (arg *Argument) Prepare(proc *process.Process) error {
    32  	ap := arg
    33  	ap.ctr = new(container)
    34  	ap.ctr.InitReceiver(proc, true)
    35  	return nil
    36  }
    37  
    38  func (arg *Argument) Call(proc *process.Process) (vm.CallResult, error) {
    39  	if err, isCancel := vm.CancelCheck(proc); isCancel {
    40  		return vm.CancelResult, err
    41  	}
    42  
    43  	anal := proc.GetAnalyze(arg.GetIdx(), arg.GetParallelIdx(), arg.GetParallelMajor())
    44  	anal.Start()
    45  	defer anal.Stop()
    46  
    47  	result := vm.NewCallResult()
    48  	for !arg.ctr.last {
    49  		bat, _, err := arg.ctr.ReceiveFromSingleReg(0, anal)
    50  		if err != nil {
    51  			result.Status = vm.ExecStop
    52  			return result, err
    53  		}
    54  		if bat == nil || bat.End() {
    55  			result.Batch = nil
    56  			result.Status = vm.ExecStop
    57  			return result, nil
    58  		}
    59  		if bat.Last() {
    60  			arg.ctr.last = true
    61  		}
    62  		arg.ctr.bats = append(arg.ctr.bats, bat)
    63  	}
    64  	arg.buf = arg.ctr.bats[0]
    65  	arg.ctr.bats = arg.ctr.bats[1:]
    66  
    67  	if arg.buf.Last() {
    68  		arg.ctr.last = false
    69  	}
    70  
    71  	if arg.buf.End() {
    72  		arg.buf.Clean(proc.Mp())
    73  		result.Batch = nil
    74  		result.Status = vm.ExecStop
    75  		return result, nil
    76  	}
    77  
    78  	anal.Input(arg.buf, arg.GetIsFirst())
    79  	anal.Output(arg.buf, arg.GetIsLast())
    80  	result.Batch = arg.buf
    81  	return result, nil
    82  }