github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/abs.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 unary
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/vectorize/abs"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  // abs function's evaluation for arguments: [uint64]
    25  func AbsUInt64(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    26  	inputVector := vectors[0]
    27  	resultType := types.Type{Oid: types.T_uint64, Size: 8}
    28  	inputValues := vector.MustTCols[uint64](inputVector)
    29  	if inputVector.IsScalar() {
    30  		if inputVector.ConstVectorIsNull() {
    31  			return proc.AllocScalarNullVector(resultType), nil
    32  		}
    33  		resultVector := vector.NewConst(resultType, 1)
    34  		resultValues := make([]uint64, 1)
    35  		vector.SetCol(resultVector, abs.AbsUint64(inputValues, resultValues))
    36  		return resultVector, nil
    37  	} else {
    38  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    39  		if err != nil {
    40  			return nil, err
    41  		}
    42  		resultValues := vector.GetFixedVectorValues[uint64](resultVector)
    43  		abs.AbsUint64(inputValues, resultValues)
    44  		return resultVector, nil
    45  	}
    46  }
    47  
    48  // abs function's evaluation for arguments: [int64]
    49  func AbsInt64(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    50  	inputVector := vectors[0]
    51  	resultType := types.Type{Oid: types.T_int64, Size: 8}
    52  	inputValues := vector.MustTCols[int64](inputVector)
    53  	if inputVector.IsScalar() {
    54  		if inputVector.ConstVectorIsNull() {
    55  			return proc.AllocScalarNullVector(resultType), nil
    56  		}
    57  		resultVector := vector.NewConst(resultType, 1)
    58  		resultValues := make([]int64, 1)
    59  		vector.SetCol(resultVector, abs.AbsInt64(inputValues, resultValues))
    60  		return resultVector, nil
    61  	} else {
    62  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  		resultValues := vector.MustTCols[int64](resultVector)
    67  		abs.AbsInt64(inputValues, resultValues)
    68  		return resultVector, nil
    69  	}
    70  }
    71  
    72  // abs function's evaluation for arguments: [float64]
    73  func AbsFloat64(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    74  	inputVector := vectors[0]
    75  	resultType := types.Type{Oid: types.T_float64, Size: 8}
    76  	inputValues := vector.MustTCols[float64](inputVector)
    77  	if inputVector.IsScalar() {
    78  		if inputVector.ConstVectorIsNull() {
    79  			return proc.AllocScalarNullVector(resultType), nil
    80  		}
    81  		resultVector := vector.NewConst(resultType, 1)
    82  		resultValues := make([]float64, 1)
    83  		vector.SetCol(resultVector, abs.AbsFloat64(inputValues, resultValues))
    84  		return resultVector, nil
    85  	} else {
    86  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		resultValues := vector.MustTCols[float64](resultVector)
    91  		abs.AbsFloat64(inputValues, resultValues)
    92  		return resultVector, nil
    93  	}
    94  }
    95  
    96  func AbsDecimal128(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    97  	inputVector := vectors[0]
    98  	resultType := types.Type{Oid: types.T_decimal128, Size: 16, Scale: inputVector.Typ.Scale}
    99  	inputValues := vector.MustTCols[types.Decimal128](inputVector)
   100  	if inputVector.IsScalar() {
   101  		if inputVector.ConstVectorIsNull() {
   102  			return proc.AllocScalarNullVector(resultType), nil
   103  		}
   104  		resultVector := vector.NewConst(resultType, 1)
   105  		resultValues := make([]types.Decimal128, 1)
   106  		vector.SetCol(resultVector, abs.AbsDecimal128(inputValues, resultValues))
   107  		return resultVector, nil
   108  	} else {
   109  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(inputValues)), inputVector.Nsp)
   110  		if err != nil {
   111  			return nil, err
   112  		}
   113  		resultValues := vector.MustTCols[types.Decimal128](resultVector)
   114  		abs.AbsDecimal128(inputValues, resultValues)
   115  		return resultVector, nil
   116  	}
   117  }