github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/product/product.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 product 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/process" 23 ) 24 25 func String(_ any, buf *bytes.Buffer) { 26 buf.WriteString(" cross join ") 27 } 28 29 func Prepare(proc *process.Process, arg any) error { 30 ap := arg.(*Argument) 31 ap.ctr = new(container) 32 return nil 33 } 34 35 func Call(idx int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) { 36 anal := proc.GetAnalyze(idx) 37 anal.Start() 38 defer anal.Stop() 39 ap := arg.(*Argument) 40 ctr := ap.ctr 41 for { 42 switch ctr.state { 43 case Build: 44 if err := ctr.build(ap, proc, anal); err != nil { 45 ap.Free(proc, true) 46 return false, err 47 } 48 ctr.state = Probe 49 50 case Probe: 51 bat := <-proc.Reg.MergeReceivers[0].Ch 52 if bat == nil { 53 ctr.state = End 54 continue 55 } 56 if len(bat.Zs) == 0 { 57 continue 58 } 59 if ctr.bat == nil { 60 bat.Clean(proc.Mp()) 61 continue 62 } 63 if err := ctr.probe(bat, ap, proc, anal, isFirst, isLast); err != nil { 64 bat.Clean(proc.Mp()) 65 ap.Free(proc, true) 66 return false, err 67 } 68 return false, nil 69 70 default: 71 ap.Free(proc, false) 72 proc.SetInputBatch(nil) 73 return true, nil 74 } 75 } 76 } 77 78 func (ctr *container) build(ap *Argument, proc *process.Process, anal process.Analyze) error { 79 bat := <-proc.Reg.MergeReceivers[1].Ch 80 if bat != nil { 81 ctr.bat = bat 82 } 83 return nil 84 } 85 86 func (ctr *container) probe(bat *batch.Batch, ap *Argument, proc *process.Process, anal process.Analyze, isFirst bool, isLast bool) error { 87 defer bat.Clean(proc.Mp()) 88 anal.Input(bat, isFirst) 89 rbat := batch.NewWithSize(len(ap.Result)) 90 rbat.Zs = proc.Mp().GetSels() 91 for i, rp := range ap.Result { 92 if rp.Rel == 0 { 93 rbat.Vecs[i] = vector.New(bat.Vecs[rp.Pos].Typ) 94 } else { 95 rbat.Vecs[i] = vector.New(ctr.bat.Vecs[rp.Pos].Typ) 96 } 97 } 98 count := bat.Length() 99 for i := 0; i < count; i++ { 100 for j := 0; j < len(ctr.bat.Zs); j++ { 101 for k, rp := range ap.Result { 102 if rp.Rel == 0 { 103 if err := vector.UnionOne(rbat.Vecs[k], bat.Vecs[rp.Pos], int64(i), proc.Mp()); err != nil { 104 rbat.Clean(proc.Mp()) 105 return err 106 } 107 } else { 108 if err := vector.UnionOne(rbat.Vecs[k], ctr.bat.Vecs[rp.Pos], int64(j), proc.Mp()); err != nil { 109 rbat.Clean(proc.Mp()) 110 return err 111 } 112 } 113 } 114 rbat.Zs = append(rbat.Zs, ctr.bat.Zs[j]) 115 } 116 } 117 rbat.ExpandNulls() 118 anal.Output(rbat, isLast) 119 proc.SetInputBatch(rbat) 120 return nil 121 }