github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/logicalOperator.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 function
    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  )
    22  
    23  var andFn = generalFunctionTemplateFactor[bool, bool](
    24  	func(v1 bool) (bool, bool) { return false, v1 }, false,
    25  	func(v2 bool) (bool, bool) { return false, v2 }, false,
    26  	func(v1, v2 bool) (bool, bool) { return v1 && v2, false }, true,
    27  	nil, true, true,
    28  )
    29  
    30  var orFn = generalFunctionTemplateFactor[bool, bool](
    31  	func(v1 bool) (bool, bool) { return v1, !v1 }, false,
    32  	func(v2 bool) (bool, bool) { return v2, !v2 }, false,
    33  	func(v1, v2 bool) (bool, bool) { return v1 || v2, false }, true,
    34  	nil, true, true,
    35  )
    36  
    37  var xorFn = generalFunctionTemplateFactor[bool, bool](
    38  	nil, true,
    39  	nil, true,
    40  	func(v1, v2 bool) (bool, bool) { return v1 != v2, false }, true,
    41  	nil, true, true,
    42  )
    43  
    44  func notFn(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
    45  	return opUnaryFixedToFixed[bool, bool](parameters, result, proc, length, func(v bool) bool {
    46  		return !v
    47  	})
    48  }
    49  
    50  func opMultiAnd(params []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
    51  	rsVec := vector.MustFunctionResult[bool](result).GetResultVector()
    52  	rsArr := vector.MustFixedCol[bool](rsVec)
    53  
    54  	arr0 := vector.MustFixedCol[bool](params[0])
    55  	if params[0].IsConstNull() {
    56  		for i := 0; i < length; i++ {
    57  			rsArr[i] = false
    58  		}
    59  		nulls.AddRange(rsVec.GetNulls(), 0, uint64(length))
    60  	} else if params[0].IsConst() {
    61  		for i := 0; i < length; i++ {
    62  			rsArr[i] = arr0[0]
    63  		}
    64  	} else {
    65  		copy(rsArr, arr0)
    66  		nulls.Set(rsVec.GetNulls(), params[0].GetNulls())
    67  	}
    68  
    69  	for idx := 1; idx < len(params); idx++ {
    70  		arr1 := vector.MustFixedCol[bool](params[idx])
    71  
    72  		if params[idx].IsConstNull() {
    73  			for i := 0; i < length; i++ {
    74  				if rsArr[i] {
    75  					rsArr[i] = false
    76  					rsVec.GetNulls().Add(uint64(i))
    77  				}
    78  			}
    79  		} else if params[idx].IsConst() {
    80  			if !arr1[0] {
    81  				for i := 0; i < length; i++ {
    82  					rsArr[i] = false
    83  				}
    84  				nulls.RemoveRange(rsVec.GetNulls(), 0, uint64(length))
    85  			}
    86  		} else {
    87  			if rsVec.GetNulls().Any() || params[idx].GetNulls().Any() {
    88  				rowCount := uint64(length)
    89  				for i := uint64(0); i < rowCount; i++ {
    90  					null1 := rsVec.GetNulls().Contains(i)
    91  					null2 := params[idx].GetNulls().Contains(i)
    92  
    93  					if null1 && !null2 {
    94  						if !arr1[i] {
    95  							rsVec.GetNulls().Del(i)
    96  							rsArr[i] = false
    97  						}
    98  					} else if !null1 && null2 {
    99  						if rsArr[i] {
   100  							rsVec.GetNulls().Add(i)
   101  							rsArr[i] = false
   102  						}
   103  					} else if !null1 && !null2 {
   104  						rsArr[i] = rsArr[i] && arr1[i]
   105  					}
   106  				}
   107  			} else {
   108  				rowCount := uint64(length)
   109  				for i := uint64(0); i < rowCount; i++ {
   110  					rsArr[i] = rsArr[i] && arr1[i]
   111  				}
   112  			}
   113  		}
   114  	}
   115  	return nil
   116  }
   117  
   118  func opMultiOr(params []*vector.Vector, result vector.FunctionResultWrapper, _ *process.Process, length int) error {
   119  	rsVec := vector.MustFunctionResult[bool](result).GetResultVector()
   120  	rsArr := vector.MustFixedCol[bool](rsVec)
   121  
   122  	arr0 := vector.MustFixedCol[bool](params[0])
   123  	if params[0].IsConstNull() {
   124  		for i := 0; i < length; i++ {
   125  			rsArr[i] = false
   126  		}
   127  		nulls.AddRange(rsVec.GetNulls(), 0, uint64(length))
   128  	} else if params[0].IsConst() {
   129  		for i := 0; i < length; i++ {
   130  			rsArr[i] = arr0[0]
   131  		}
   132  	} else {
   133  		copy(rsArr, arr0)
   134  		nulls.Set(rsVec.GetNulls(), params[0].GetNulls())
   135  	}
   136  
   137  	for idx := 1; idx < len(params); idx++ {
   138  		arr1 := vector.MustFixedCol[bool](params[idx])
   139  
   140  		if params[idx].IsConstNull() {
   141  			for i := 0; i < length; i++ {
   142  				if !rsArr[i] {
   143  					rsVec.GetNulls().Add(uint64(i))
   144  				}
   145  			}
   146  		} else if params[idx].IsConst() {
   147  			if arr1[0] {
   148  				for i := 0; i < length; i++ {
   149  					rsArr[i] = true
   150  				}
   151  				nulls.RemoveRange(rsVec.GetNulls(), 0, uint64(length))
   152  			}
   153  		} else {
   154  			if rsVec.GetNulls().Any() || params[idx].GetNulls().Any() {
   155  				rowCount := uint64(length)
   156  				for i := uint64(0); i < rowCount; i++ {
   157  					null1 := rsVec.GetNulls().Contains(i)
   158  					null2 := params[idx].GetNulls().Contains(i)
   159  
   160  					if null1 && !null2 {
   161  						if arr1[i] {
   162  							rsVec.GetNulls().Del(i)
   163  							rsArr[i] = true
   164  						}
   165  					} else if !null1 && null2 {
   166  						if !rsArr[i] {
   167  							rsVec.GetNulls().Add(i)
   168  						}
   169  					} else if !null1 && !null2 {
   170  						rsArr[i] = rsArr[i] || arr1[i]
   171  					}
   172  				}
   173  			} else {
   174  				rowCount := uint64(length)
   175  				for i := uint64(0); i < rowCount; i++ {
   176  					rsArr[i] = rsArr[i] || arr1[i]
   177  				}
   178  			}
   179  		}
   180  	}
   181  	return nil
   182  }