github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/op_bit.go (about) 1 // Copyright 2022 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 operator 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/container/nulls" 19 "github.com/matrixorigin/matrixone/pkg/container/vector" 20 "github.com/matrixorigin/matrixone/pkg/vm/process" 21 "golang.org/x/exp/constraints" 22 ) 23 24 type opBitT interface { 25 constraints.Integer 26 } 27 28 type opBitFun[T opBitT] func(v1, v2 T) T 29 30 func opBitXor[T opBitT](v1, v2 T) T { 31 return v1 ^ v2 32 } 33 34 func opBitOr[T opBitT](v1, v2 T) T { 35 return v1 | v2 36 } 37 38 func opBitAnd[T opBitT](v1, v2 T) T { 39 return v1 & v2 40 } 41 42 func opBitRightShift[T opBitT](v1, v2 T) T { 43 if v2 < 0 { 44 return 0 45 } 46 return v1 >> v2 47 } 48 49 func opBitLeftShift[T opBitT](v1, v2 T) T { 50 if v2 < 0 { 51 return 0 52 } 53 return v1 << v2 54 } 55 56 func OpBitAndFun[T opBitT](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 57 return Arith[T, T](args, proc, args[0].GetType(), func(xs, ys, rs *vector.Vector) error { 58 return goOpBitGeneral(xs, ys, rs, opBitAnd[T]) 59 }) 60 } 61 62 func OpBitOrFun[T opBitT](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 63 return Arith[T, T](args, proc, args[0].GetType(), func(xs, ys, rs *vector.Vector) error { 64 return goOpBitGeneral(xs, ys, rs, opBitOr[T]) 65 }) 66 } 67 68 func OpBitXorFun[T opBitT](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 69 return Arith[T, T](args, proc, args[0].GetType(), func(xs, ys, rs *vector.Vector) error { 70 return goOpBitGeneral(xs, ys, rs, opBitXor[T]) 71 }) 72 } 73 74 func OpBitRightShiftFun[T opBitT](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 75 return Arith[T, T](args, proc, args[0].GetType(), func(xs, ys, rs *vector.Vector) error { 76 return goOpBitGeneral(xs, ys, rs, opBitRightShift[T]) 77 }) 78 } 79 80 func OpBitLeftShiftFun[T opBitT](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 81 return Arith[T, T](args, proc, args[0].GetType(), func(xs, ys, rs *vector.Vector) error { 82 return goOpBitGeneral(xs, ys, rs, opBitLeftShift[T]) 83 }) 84 } 85 86 func goOpBitGeneral[T opBitT](xs, ys, rs *vector.Vector, bfn opBitFun[T]) error { 87 xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs) 88 if xs.IsScalar() { 89 if nulls.Any(ys.Nsp) { 90 for i, y := range yt { 91 if !nulls.Contains(rs.Nsp, uint64(i)) { 92 rt[i] = bfn(xt[0], y) 93 } 94 } 95 } else { 96 for i, y := range yt { 97 rt[i] = bfn(xt[0], y) 98 } 99 } 100 return nil 101 } else if ys.IsScalar() { 102 if nulls.Any(xs.Nsp) { 103 for i, x := range xt { 104 if !nulls.Contains(rs.Nsp, uint64(i)) { 105 rt[i] = bfn(x, yt[0]) 106 } 107 } 108 } else { 109 for i, x := range xt { 110 rt[i] = bfn(x, yt[0]) 111 } 112 } 113 return nil 114 } else { 115 if nulls.Any(rs.Nsp) { 116 for i, x := range xt { 117 if !nulls.Contains(rs.Nsp, uint64(i)) { 118 rt[i] = bfn(x, yt[i]) 119 } 120 } 121 } else { 122 for i, x := range xt { 123 rt[i] = bfn(x, yt[i]) 124 } 125 } 126 return nil 127 } 128 }