github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/arith.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/vectorize/div"
    19  	"github.com/matrixorigin/matrixone/pkg/vectorize/mod"
    20  	"github.com/matrixorigin/matrixone/pkg/vectorize/mult"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/sub"
    22  	"golang.org/x/exp/constraints"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    25  	"github.com/matrixorigin/matrixone/pkg/container/types"
    26  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    27  	"github.com/matrixorigin/matrixone/pkg/vectorize/add"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    29  )
    30  
    31  //
    32  // This file contains some vectorized arithmatic operators.
    33  //
    34  
    35  type arithT interface {
    36  	constraints.Integer | constraints.Float | bool |
    37  		types.Decimal64 | types.Decimal128
    38  }
    39  
    40  type arithFn func(v1, v2, r *vector.Vector) error
    41  
    42  // Generic T1 is the operand type and generic T2 is the return value type
    43  func Arith[T1 arithT, T2 arithT](vectors []*vector.Vector, proc *process.Process, typ types.Type, afn arithFn) (*vector.Vector, error) {
    44  	left, right := vectors[0], vectors[1]
    45  	leftValues, rightValues := vector.MustTCols[T1](left), vector.MustTCols[T1](right)
    46  
    47  	if left.IsScalarNull() || right.IsScalarNull() {
    48  		return proc.AllocScalarNullVector(typ), nil
    49  	}
    50  
    51  	if left.IsScalar() && right.IsScalar() {
    52  		resultVector := proc.AllocScalarVector(typ)
    53  		if err := afn(left, right, resultVector); err != nil {
    54  			return nil, err
    55  		}
    56  		return resultVector, nil
    57  	}
    58  
    59  	nEle := len(leftValues)
    60  	if left.IsScalar() {
    61  		nEle = len(rightValues)
    62  	}
    63  
    64  	resultVector, err := proc.AllocVectorOfRows(typ, int64(nEle), nil)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	nulls.Or(left.Nsp, right.Nsp, resultVector.Nsp)
    69  	if err = afn(left, right, resultVector); err != nil {
    70  		return nil, err
    71  	}
    72  	return resultVector, nil
    73  }
    74  
    75  // Addition operation
    76  func PlusUint[T constraints.Unsigned](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    77  	return Arith[T, T](args, proc, args[0].GetType(), add.NumericAddUnsigned[T])
    78  }
    79  func PlusInt[T constraints.Signed](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    80  	return Arith[T, T](args, proc, args[0].GetType(), add.NumericAddSigned[T])
    81  }
    82  func PlusFloat[T constraints.Float](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    83  	return Arith[T, T](args, proc, args[0].GetType(), add.NumericAddFloat[T])
    84  }
    85  func PlusDecimal64(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    86  	lv, rv := args[0], args[1]
    87  	lvScale, rvScale := lv.Typ.Scale, rv.Typ.Scale
    88  	resultScale := lvScale
    89  	if lvScale < rvScale {
    90  		resultScale = rvScale
    91  	}
    92  	resultTyp := types.Type{Oid: types.T_decimal64, Size: types.DECIMAL64_NBYTES, Width: types.DECIMAL128_WIDTH, Scale: resultScale}
    93  	return Arith[types.Decimal64, types.Decimal64](args, proc, resultTyp, add.Decimal64VecAdd)
    94  }
    95  func PlusDecimal128(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    96  	lv, rv := args[0], args[1]
    97  	lvScale, rvScale := lv.Typ.Scale, rv.Typ.Scale
    98  	resultScale := lvScale
    99  	if lvScale < rvScale {
   100  		resultScale = rvScale
   101  	}
   102  	resultTyp := types.Type{Oid: types.T_decimal128, Size: types.DECIMAL128_NBYTES, Width: types.DECIMAL128_WIDTH, Scale: resultScale}
   103  	return Arith[types.Decimal128, types.Decimal128](args, proc, resultTyp, add.Decimal128VecAdd)
   104  }
   105  
   106  // Subtraction operation
   107  func MinusUint[T constraints.Unsigned](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   108  	return Arith[T, T](args, proc, args[0].GetType(), sub.NumericSubUnsigned[T])
   109  }
   110  func MinusInt[T constraints.Signed](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   111  	return Arith[T, T](args, proc, args[0].GetType(), sub.NumericSubSigned[T])
   112  }
   113  func MinusFloat[T constraints.Float](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   114  	return Arith[T, T](args, proc, args[0].GetType(), sub.NumericSubFloat[T])
   115  }
   116  func MinusDecimal64(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   117  	lv, rv := args[0], args[1]
   118  	lvScale, rvScale := lv.Typ.Scale, rv.Typ.Scale
   119  	resultScale := lvScale
   120  	if lvScale < rvScale {
   121  		resultScale = rvScale
   122  	}
   123  	resultTyp := types.Type{Oid: types.T_decimal64, Size: types.DECIMAL64_NBYTES, Width: types.DECIMAL64_WIDTH, Scale: resultScale}
   124  	return Arith[types.Decimal64, types.Decimal64](args, proc, resultTyp, sub.Decimal64VecSub)
   125  }
   126  func MinusDecimal128(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   127  	lv, rv := args[0], args[1]
   128  	lvScale := lv.Typ.Scale
   129  	rvScale := rv.Typ.Scale
   130  	resultScale := lvScale
   131  	if lvScale < rvScale {
   132  		resultScale = rvScale
   133  	}
   134  	resultTyp := types.Type{Oid: types.T_decimal128, Size: types.DECIMAL128_NBYTES, Width: types.DECIMAL128_WIDTH, Scale: resultScale}
   135  	return Arith[types.Decimal128, types.Decimal128](args, proc, resultTyp, sub.Decimal128VecSub)
   136  }
   137  
   138  func MinusDatetime(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   139  	resultType := types.T_int64.ToType()
   140  	return Arith[types.Datetime, types.Datetime](args, proc, resultType, sub.DatetimeSub)
   141  }
   142  
   143  // Multiplication operation
   144  func MultUint[T constraints.Unsigned](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   145  	return Arith[T, T](args, proc, args[0].GetType(), mult.NumericMultUnsigned[T])
   146  }
   147  func MultInt[T constraints.Signed](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   148  	return Arith[T, T](args, proc, args[0].GetType(), mult.NumericMultSigned[T])
   149  }
   150  func MultFloat[T constraints.Float](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   151  	return Arith[T, T](args, proc, args[0].GetType(), mult.NumericMultFloat[T])
   152  }
   153  func MultDecimal64(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   154  	lv, rv := args[0], args[1]
   155  	resultScale := lv.Typ.Scale + rv.Typ.Scale
   156  	resultTyp := types.Type{Oid: types.T_decimal128, Size: types.DECIMAL128_NBYTES, Width: types.DECIMAL128_WIDTH, Scale: resultScale}
   157  	return Arith[types.Decimal64, types.Decimal128](args, proc, resultTyp, mult.Decimal64VecMult)
   158  }
   159  func MultDecimal128(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   160  	lv, rv := args[0], args[1]
   161  	resultScale := lv.Typ.Scale + rv.Typ.Scale
   162  	resultTyp := types.Type{Oid: types.T_decimal128, Size: types.DECIMAL128_NBYTES, Width: types.DECIMAL128_WIDTH, Scale: resultScale}
   163  	return Arith[types.Decimal128, types.Decimal128](args, proc, resultTyp, mult.Decimal128VecMult)
   164  }
   165  
   166  // Division operation
   167  func DivFloat[T constraints.Float](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   168  	return Arith[T, T](args, proc, args[0].GetType(), div.NumericDivFloat[T])
   169  }
   170  func DivDecimal64(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   171  	var scale int32
   172  	if args[0].Typ.Scale == 0 {
   173  		scale = types.MYSQL_DEFAULT_SCALE
   174  	} else {
   175  		scale = types.MYSQL_DEFAULT_SCALE + args[0].Typ.Scale
   176  	}
   177  	resultTyp := types.Type{Oid: types.T_decimal128, Size: types.DECIMAL128_NBYTES, Width: types.DECIMAL128_WIDTH, Scale: scale}
   178  	return Arith[types.Decimal64, types.Decimal128](args, proc, resultTyp, div.Decimal64VecDiv)
   179  }
   180  func DivDecimal128(args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   181  	var scale int32
   182  	if args[0].Typ.Scale == 0 {
   183  		scale = types.MYSQL_DEFAULT_SCALE
   184  	} else {
   185  		scale = types.MYSQL_DEFAULT_SCALE + args[0].Typ.Scale
   186  	}
   187  	resultTyp := types.Type{Oid: types.T_decimal128, Size: types.DECIMAL128_NBYTES, Width: types.DECIMAL128_WIDTH, Scale: scale}
   188  	return Arith[types.Decimal128, types.Decimal128](args, proc, resultTyp, div.Decimal128VecDiv)
   189  }
   190  
   191  // Integer division operation
   192  func IntegerDivFloat[T constraints.Float](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   193  	resultTyp := types.T_int64.ToType()
   194  	return Arith[T, int64](args, proc, resultTyp, div.NumericIntegerDivFloat[T])
   195  }
   196  
   197  // mod operation
   198  func ModUint[T constraints.Unsigned](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   199  	return Arith[T, T](args, proc, args[0].GetType(), mod.NumericModUnsigned[T])
   200  }
   201  func ModInt[T constraints.Signed](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   202  	return Arith[T, T](args, proc, args[0].GetType(), mod.NumericModSigned[T])
   203  }
   204  func ModFloat[T constraints.Float](args []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
   205  	return Arith[T, T](args, proc, args[0].GetType(), mod.NumericModFloat[T])
   206  }