github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/unaryops.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/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/neg"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  	"golang.org/x/exp/constraints"
    24  )
    25  
    26  func UnaryTilde[T constraints.Integer](vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    27  	srcVector := vectors[0]
    28  	srcValues := vector.MustTCols[T](srcVector)
    29  	returnType := types.Type{
    30  		Oid:  types.T_uint64,
    31  		Size: types.T_uint64.ToType().Size,
    32  	}
    33  
    34  	if srcVector.IsScalar() {
    35  		if srcVector.IsScalarNull() {
    36  			return proc.AllocScalarNullVector(returnType), nil
    37  		}
    38  		resVector := proc.AllocScalarVector(returnType)
    39  		resValues := make([]uint64, 1)
    40  		nulls.Set(resVector.Nsp, srcVector.Nsp)
    41  		resValues[0] = funcBitInversion(srcValues[0])
    42  		vector.SetCol(resVector, resValues)
    43  		return resVector, nil
    44  	} else {
    45  		resVector, err := proc.AllocVectorOfRows(returnType, int64(len(srcValues)), srcVector.Nsp)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  		resValues := vector.MustTCols[uint64](resVector)
    50  
    51  		var i uint64
    52  		if nulls.Any(resVector.Nsp) {
    53  			for i = 0; i < uint64(len(resValues)); i++ {
    54  				if !nulls.Contains(resVector.Nsp, i) {
    55  					resValues[i] = funcBitInversion(srcValues[i])
    56  				} else {
    57  					resValues[i] = 0
    58  				}
    59  			}
    60  		} else {
    61  			for i = 0; i < uint64(len(resValues)); i++ {
    62  				resValues[i] = funcBitInversion(srcValues[i])
    63  			}
    64  		}
    65  		return resVector, nil
    66  	}
    67  }
    68  
    69  func funcBitInversion[T constraints.Integer](x T) uint64 {
    70  	if x > 0 {
    71  		n := uint64(x)
    72  		return ^n
    73  	} else {
    74  		return uint64(^x)
    75  	}
    76  }
    77  
    78  func UnaryMinus[T constraints.Signed | constraints.Float](vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    79  	srcVector := vectors[0]
    80  	srcValues := vector.MustTCols[T](srcVector)
    81  
    82  	if srcVector.IsScalar() {
    83  		if srcVector.IsScalarNull() {
    84  			return proc.AllocScalarNullVector(srcVector.Typ), nil
    85  		}
    86  		resVector := proc.AllocScalarVector(srcVector.Typ)
    87  		resValues := make([]T, 1)
    88  		nulls.Set(resVector.Nsp, srcVector.Nsp)
    89  		vector.SetCol(resVector, neg.NumericNeg(srcValues, resValues))
    90  		return resVector, nil
    91  	} else {
    92  		resVector, err := proc.AllocVectorOfRows(srcVector.Typ, int64(len(srcValues)), srcVector.Nsp)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		resValues := vector.MustTCols[T](resVector)
    97  		neg.NumericNeg(srcValues, resValues)
    98  		return resVector, nil
    99  	}
   100  }
   101  
   102  func UnaryMinusDecimal64(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   103  	srcVector := vectors[0]
   104  	srcValues := vector.MustTCols[types.Decimal64](srcVector)
   105  
   106  	if srcVector.IsScalar() {
   107  		if srcVector.ConstVectorIsNull() {
   108  			return proc.AllocScalarNullVector(srcVector.Typ), nil
   109  		}
   110  		resVector := proc.AllocScalarVector(srcVector.Typ)
   111  		resValues := make([]types.Decimal64, 1)
   112  		nulls.Set(resVector.Nsp, srcVector.Nsp)
   113  		vector.SetCol(resVector, neg.Decimal64Neg(srcValues, resValues))
   114  		return resVector, nil
   115  	} else {
   116  		resVector, err := proc.AllocVectorOfRows(srcVector.Typ, int64(len(srcValues)), srcVector.Nsp)
   117  		if err != nil {
   118  			return nil, err
   119  		}
   120  		resValues := vector.MustTCols[types.Decimal64](resVector)
   121  		neg.Decimal64Neg(srcValues, resValues)
   122  		return resVector, nil
   123  	}
   124  }
   125  
   126  func UnaryMinusDecimal128(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   127  	srcVector := vectors[0]
   128  	srcValues := vector.MustTCols[types.Decimal128](srcVector)
   129  
   130  	if srcVector.IsScalar() {
   131  		if srcVector.ConstVectorIsNull() {
   132  			return proc.AllocScalarNullVector(srcVector.Typ), nil
   133  		}
   134  		resVector := proc.AllocScalarVector(srcVector.Typ)
   135  		resValues := make([]types.Decimal128, 1)
   136  		vector.SetCol(resVector, neg.Decimal128Neg(srcValues, resValues))
   137  		return resVector, nil
   138  	} else {
   139  		resVector, err := proc.AllocVectorOfRows(srcVector.Typ, int64(len(srcValues)), srcVector.Nsp)
   140  		if err != nil {
   141  			return nil, err
   142  		}
   143  		resValues := vector.MustTCols[types.Decimal128](resVector)
   144  		// XXX should pass in nulls
   145  		neg.Decimal128Neg(srcValues, resValues)
   146  		return resVector, nil
   147  	}
   148  }