github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/limit/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 limit 16 17 import ( 18 "bytes" 19 "fmt" 20 21 "github.com/matrixorigin/matrixone/pkg/container/batch" 22 "github.com/matrixorigin/matrixone/pkg/vm/process" 23 ) 24 25 func String(arg any, buf *bytes.Buffer) { 26 n := arg.(*Argument) 27 buf.WriteString(fmt.Sprintf("limit(%v)", n.Limit)) 28 } 29 30 func Prepare(_ *process.Process, _ any) error { 31 return nil 32 } 33 34 // Call returning only the first n tuples from its input 35 func Call(idx int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) { 36 bat := proc.InputBatch() 37 if bat == nil { 38 return true, nil 39 } 40 if bat.Length() == 0 { 41 return false, nil 42 } 43 ap := arg.(*Argument) 44 anal := proc.GetAnalyze(idx) 45 anal.Start() 46 defer anal.Stop() 47 anal.Input(bat, isFirst) 48 if ap.Seen >= ap.Limit { 49 proc.Reg.InputBatch = nil 50 bat.Clean(proc.Mp()) 51 return true, nil 52 } 53 length := bat.Length() 54 newSeen := ap.Seen + uint64(length) 55 if newSeen >= ap.Limit { // limit - seen 56 batch.SetLength(bat, int(ap.Limit-ap.Seen)) 57 ap.Seen = newSeen 58 anal.Output(bat, isLast) 59 proc.SetInputBatch(bat) 60 return true, nil 61 } 62 anal.Output(bat, isLast) 63 ap.Seen = newSeen 64 return false, nil 65 }