github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/is_true_false.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/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  func IsTrue(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    25  	return funcIs(vectors, proc, false, true)
    26  }
    27  
    28  func IsNotTrue(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    29  	return funcIs(vectors, proc, true, false)
    30  }
    31  
    32  func IsFalse(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    33  	return funcIs(vectors, proc, false, false)
    34  }
    35  
    36  func IsNotFalse(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    37  	return funcIs(vectors, proc, true, true)
    38  }
    39  
    40  func funcIs(vectors []*vector.Vector, proc *process.Process, nullValue bool, eqBool bool) (*vector.Vector, error) {
    41  	input := vectors[0]
    42  	retType := types.T_bool.ToType()
    43  	if input.IsScalar() {
    44  		if input.IsScalarNull() {
    45  			return vector.NewConstFixed(retType, input.Length(), nullValue, proc.Mp()), nil
    46  		} else {
    47  			col := vector.MustTCols[bool](input)
    48  			return vector.NewConstFixed(retType, input.Length(), col[0] == eqBool, proc.Mp()), nil
    49  		}
    50  	} else {
    51  		vlen := input.Length()
    52  		vec := vector.PreAllocType(retType, vlen, vlen, proc.Mp())
    53  		vals := vector.MustTCols[bool](vec)
    54  		olds := vector.MustTCols[bool](input)
    55  		for i := range vals {
    56  			if nulls.Contains(input.Nsp, uint64(i)) {
    57  				vals[i] = nullValue
    58  			} else {
    59  				vals[i] = olds[i] == eqBool
    60  			}
    61  		}
    62  		return vec, nil
    63  	}
    64  }