github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/xor.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  )
    22  
    23  func ScalarXorNotScalar(_, nsv *vector.Vector, col1, col2 []bool, proc *process.Process) (*vector.Vector, error) {
    24  	length := vector.Length(nsv)
    25  	vec := allocateBoolVector(length, proc)
    26  	vcols := vec.Col.([]bool)
    27  	value := col1[0]
    28  	for i := range vcols {
    29  		vcols[i] = (col2[i] || value) && !(col2[i] && value)
    30  	}
    31  	nulls.Or(nsv.Nsp, nil, vec.Nsp)
    32  	return vec, nil
    33  }
    34  
    35  func Xor(vs []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    36  	v1, v2 := vs[0], vs[1]
    37  	col1, col2 := vector.MustTCols[bool](v1), vector.MustTCols[bool](v2)
    38  	if v1.IsScalarNull() || v2.IsScalarNull() {
    39  		return handleScalarNull(v1, v2, proc)
    40  	}
    41  
    42  	c1, c2 := v1.IsScalar(), v2.IsScalar()
    43  	switch {
    44  	case c1 && c2:
    45  		vec := proc.AllocScalarVector(boolType)
    46  		vec.Col = make([]bool, 1)
    47  		vec.Col.([]bool)[0] = (col1[0] || col2[0]) && !(col1[0] && col2[0])
    48  		return vec, nil
    49  	case c1 && !c2:
    50  		return ScalarXorNotScalar(v1, v2, col1, col2, proc)
    51  	case !c1 && c2:
    52  		return ScalarXorNotScalar(v2, v1, col2, col1, proc)
    53  	}
    54  	// case !c1 && !c2
    55  	length := vector.Length(v1)
    56  	vec := allocateBoolVector(length, proc)
    57  	vcols := vec.Col.([]bool)
    58  	for i := range vcols {
    59  		vcols[i] = (col1[i] || col2[i]) && !(col1[i] && col2[i])
    60  	}
    61  	nulls.Or(v1.Nsp, v2.Nsp, vec.Nsp)
    62  	return vec, nil
    63  }