github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/logical.go (about) 1 // Copyright 2021 - 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/vectorize/logical" 21 "github.com/matrixorigin/matrixone/pkg/vm/process" 22 ) 23 24 type logicFn func(v1, v2, r *vector.Vector) error 25 26 type logicType int8 27 28 const ( 29 AND logicType = 0 30 OR logicType = 1 31 XOR logicType = 2 32 ) 33 34 func Logic(vectors []*vector.Vector, proc *process.Process, cfn logicFn, op logicType) (*vector.Vector, error) { 35 left, right := vectors[0], vectors[1] 36 if left.IsScalarNull() || right.IsScalarNull() { 37 if op == AND { 38 return HandleAndNullCol(vectors, proc) 39 } 40 41 if op == OR { 42 return HandleOrNullCol(vectors, proc) 43 } 44 45 if op == XOR { 46 if left.IsScalarNull() { 47 return proc.AllocConstNullVector(boolType, vector.Length(right)), nil 48 } else { 49 return proc.AllocConstNullVector(boolType, vector.Length(left)), nil 50 } 51 } 52 } 53 54 if left.IsScalar() && right.IsScalar() { 55 vec := proc.AllocScalarVector(boolType) 56 if err := cfn(left, right, vec); err != nil { 57 return nil, err 58 } 59 return vec, nil 60 } 61 62 length := vector.Length(left) 63 if left.IsScalar() { 64 length = vector.Length(right) 65 } 66 resultVector := allocateBoolVector(length, proc) 67 nulls.Or(left.Nsp, right.Nsp, resultVector.Nsp) 68 69 if err := cfn(left, right, resultVector); err != nil { 70 return nil, err 71 } 72 return resultVector, nil 73 } 74 75 func LogicAnd(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 76 return Logic(args, proc, logical.And, AND) 77 } 78 79 func LogicOr(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 80 return Logic(args, proc, logical.Or, OR) 81 } 82 83 func LogicXor(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 84 return Logic(args, proc, logical.Xor, XOR) 85 } 86 87 func LogicNot(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 88 v1 := vs[0] 89 if v1.IsScalarNull() { 90 return proc.AllocScalarNullVector(boolType), nil 91 } 92 if v1.IsScalar() { 93 vec := proc.AllocScalarVector(boolType) 94 if err := logical.Not(v1, vec); err != nil { 95 return nil, err 96 } 97 return vec, nil 98 } 99 length := vector.Length(v1) 100 vec := allocateBoolVector(length, proc) 101 nulls.Or(v1.Nsp, nil, vec.Nsp) 102 if err := logical.Not(v1, vec); err != nil { 103 return nil, err 104 } 105 return vec, nil 106 }