github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/indexjoin/join.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 indexjoin
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/vm"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  )
    25  
    26  const argName = "index"
    27  
    28  func (arg *Argument) String(buf *bytes.Buffer) {
    29  	buf.WriteString(argName)
    30  	buf.WriteString(": index join ")
    31  }
    32  
    33  func (arg *Argument) Prepare(proc *process.Process) (err error) {
    34  	ap := arg
    35  	ap.ctr = new(container)
    36  	ap.ctr.InitReceiver(proc, false)
    37  	return err
    38  }
    39  
    40  func (arg *Argument) Call(proc *process.Process) (vm.CallResult, error) {
    41  	if err, isCancel := vm.CancelCheck(proc); isCancel {
    42  		return vm.CancelResult, err
    43  	}
    44  
    45  	anal := proc.GetAnalyze(arg.GetIdx(), arg.GetParallelIdx(), arg.GetParallelMajor())
    46  	anal.Start()
    47  	defer anal.Stop()
    48  	ap := arg
    49  	ctr := ap.ctr
    50  	result := vm.NewCallResult()
    51  	for {
    52  		switch ctr.state {
    53  
    54  		case Probe:
    55  			bat, _, err := ctr.ReceiveFromSingleReg(0, anal)
    56  			if err != nil {
    57  				return result, err
    58  			}
    59  			if bat == nil {
    60  				ctr.state = End
    61  				continue
    62  			}
    63  			if bat.IsEmpty() {
    64  				proc.PutBatch(bat)
    65  				continue
    66  			}
    67  
    68  			if arg.buf != nil {
    69  				proc.PutBatch(arg.buf)
    70  				arg.buf = nil
    71  			}
    72  			arg.buf = batch.NewWithSize(len(ap.Result))
    73  			for i, pos := range ap.Result {
    74  				srcVec := bat.Vecs[pos]
    75  				vec := proc.GetVector(*srcVec.GetType())
    76  				if err := vector.GetUnionAllFunction(*srcVec.GetType(), proc.Mp())(vec, srcVec); err != nil {
    77  					vec.Free(proc.Mp())
    78  					return result, err
    79  				}
    80  				arg.buf.SetVector(int32(i), vec)
    81  			}
    82  			arg.buf.AddRowCount(bat.RowCount())
    83  			proc.PutBatch(bat)
    84  			result.Batch = arg.buf
    85  			anal.Output(arg.buf, arg.GetIsLast())
    86  			return result, nil
    87  
    88  		default:
    89  			result.Batch = nil
    90  			result.Status = vm.ExecStop
    91  			return result, nil
    92  		}
    93  	}
    94  }