github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/mergeoffset/offset.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 mergeoffset 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 interface{}, buf *bytes.Buffer) { 29 ap := arg.(*Argument) 30 buf.WriteString(fmt.Sprintf("mergeOffset(%d)", ap.Offset)) 31 } 32 33 func Prepare(proc *process.Process, arg interface{}) 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 interface{}, 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 70 pointer := value.UnsafePointer() 71 bat := (*batch.Batch)(pointer) 72 if bat == nil { 73 ctr.receiverListener = append(ctr.receiverListener[:chosen], ctr.receiverListener[chosen+1:]...) 74 ctr.aliveMergeReceiver-- 75 continue 76 } 77 78 if bat.Length() == 0 { 79 continue 80 } 81 82 anal.Input(bat, isFirst) 83 if ap.ctr.seen > ap.Offset { 84 anal.Output(bat, isLast) 85 proc.SetInputBatch(bat) 86 return false, nil 87 } 88 length := len(bat.Zs) 89 // bat = PartOne + PartTwo, and PartTwo is required. 90 if ap.ctr.seen+uint64(length) > ap.Offset { 91 sels := newSels(int64(ap.Offset-ap.ctr.seen), int64(length)-int64(ap.Offset-ap.ctr.seen), proc) 92 ap.ctr.seen += uint64(length) 93 bat.Shrink(sels) 94 proc.Mp().PutSels(sels) 95 anal.Output(bat, isLast) 96 proc.SetInputBatch(bat) 97 return false, nil 98 } 99 ap.ctr.seen += uint64(length) 100 bat.Clean(proc.Mp()) 101 } 102 } 103 104 func newSels(start, count int64, proc *process.Process) []int64 { 105 sels := proc.Mp().GetSels() 106 for i := int64(0); i < count; i++ { 107 sels = append(sels, start+i) 108 } 109 return sels[:count] 110 }