github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/mergelimit/limit.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 mergelimit
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"reflect"
    21  	"time"
    22  
    23  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    24  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  )
    27  
    28  func String(arg any, buf *bytes.Buffer) {
    29  	ap := arg.(*Argument)
    30  	buf.WriteString(fmt.Sprintf("mergeLimit(%d)", ap.Limit))
    31  }
    32  
    33  func Prepare(proc *process.Process, arg any) error {
    34  	ap := arg.(*Argument)
    35  	ap.ctr = new(container)
    36  	ap.ctr.seen = 0
    37  
    38  	ap.ctr.receiverListener = make([]reflect.SelectCase, len(proc.Reg.MergeReceivers))
    39  	for i, mr := range proc.Reg.MergeReceivers {
    40  		ap.ctr.receiverListener[i] = reflect.SelectCase{
    41  			Dir:  reflect.SelectRecv,
    42  			Chan: reflect.ValueOf(mr.Ch),
    43  		}
    44  	}
    45  	ap.ctr.aliveMergeReceiver = len(proc.Reg.MergeReceivers)
    46  	return nil
    47  }
    48  
    49  func Call(idx int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) {
    50  	ap := arg.(*Argument)
    51  	anal := proc.GetAnalyze(idx)
    52  	anal.Start()
    53  	defer anal.Stop()
    54  	ctr := ap.ctr
    55  
    56  	for {
    57  		if ctr.aliveMergeReceiver == 0 {
    58  			proc.SetInputBatch(nil)
    59  			ap.Free(proc, false)
    60  			return true, nil
    61  		}
    62  
    63  		start := time.Now()
    64  		chosen, value, ok := reflect.Select(ctr.receiverListener)
    65  		if !ok {
    66  			return false, moerr.NewInternalError(proc.Ctx, "pipeline closed unexpectedly")
    67  		}
    68  		anal.WaitStop(start)
    69  		pointer := value.UnsafePointer()
    70  		bat := (*batch.Batch)(pointer)
    71  		if bat == nil {
    72  			ctr.receiverListener = append(ctr.receiverListener[:chosen], ctr.receiverListener[chosen+1:]...)
    73  			ctr.aliveMergeReceiver--
    74  			continue
    75  		}
    76  
    77  		if bat.Length() == 0 {
    78  			continue
    79  		}
    80  
    81  		anal.Input(bat, isFirst)
    82  		if ap.ctr.seen >= ap.Limit {
    83  			bat.Clean(proc.Mp())
    84  			continue
    85  		}
    86  		newSeen := ap.ctr.seen + uint64(bat.Length())
    87  		if newSeen < ap.Limit {
    88  			ap.ctr.seen = newSeen
    89  			anal.Output(bat, isLast)
    90  			proc.SetInputBatch(bat)
    91  			return false, nil
    92  		} else {
    93  			num := int(newSeen - ap.Limit)
    94  			batch.SetLength(bat, bat.Length()-num)
    95  			ap.ctr.seen = newSeen
    96  			anal.Output(bat, isLast)
    97  			proc.SetInputBatch(bat)
    98  			return false, nil
    99  		}
   100  	}
   101  }