github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/sub/sub.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 sub
    16  
    17  /*
    18  #include "mo.h"
    19  
    20  #cgo CFLAGS: -I../../../cgo
    21  #cgo LDFLAGS: -L../../../cgo -lmo -lm
    22  */
    23  import "C"
    24  
    25  import (
    26  	"unsafe"
    27  
    28  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    29  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    30  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    31  	"github.com/matrixorigin/matrixone/pkg/vectorize/overflow"
    32  	"golang.org/x/exp/constraints"
    33  )
    34  
    35  const (
    36  	LEFT_IS_SCALAR  = 1
    37  	RIGHT_IS_SCALAR = 2
    38  )
    39  
    40  func goNumericSubUnsigned[T constraints.Unsigned](xs, ys, rs *vector.Vector) error {
    41  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
    42  	if xs.IsScalar() {
    43  		for i, y := range yt {
    44  			if !nulls.Contains(rs.Nsp, uint64(i)) {
    45  				rt[i] = xt[0] - y
    46  				if overflow.OverflowUIntSub(xt[i], y, rt[i]) {
    47  					return moerr.NewOutOfRangeNoCtx("unsigned int", "unsigned int SUB")
    48  				}
    49  			}
    50  		}
    51  		return nil
    52  	} else if ys.IsScalar() {
    53  		for i, x := range xt {
    54  			if !nulls.Contains(rs.Nsp, uint64(i)) {
    55  				rt[i] = x - yt[0]
    56  				if overflow.OverflowUIntSub(x, yt[0], rt[0]) {
    57  					return moerr.NewOutOfRangeNoCtx("unsigned int", "unsigned int SUB")
    58  				}
    59  			}
    60  		}
    61  		return nil
    62  	} else {
    63  		for i, x := range xt {
    64  			if !nulls.Contains(rs.Nsp, uint64(i)) {
    65  				rt[i] = x - yt[i]
    66  				if overflow.OverflowUIntSub(x, yt[i], rt[i]) {
    67  					return moerr.NewOutOfRangeNoCtx("unsigned int", "unsigned int SUB")
    68  				}
    69  			}
    70  		}
    71  		return nil
    72  	}
    73  }
    74  
    75  func goNumericSubSigned[T constraints.Signed](xs, ys, rs *vector.Vector) error {
    76  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
    77  	if xs.IsScalar() {
    78  		for i, y := range yt {
    79  			if !nulls.Contains(rs.Nsp, uint64(i)) {
    80  				rt[i] = xt[0] - y
    81  				if overflow.OverflowIntSub(xt[0], y, rt[i]) {
    82  					return moerr.NewOutOfRangeNoCtx("int", "int SUB")
    83  				}
    84  			}
    85  		}
    86  		return nil
    87  	} else if ys.IsScalar() {
    88  		for i, x := range xt {
    89  			if !nulls.Contains(rs.Nsp, uint64(i)) {
    90  				rt[i] = x - yt[0]
    91  				if overflow.OverflowIntSub(x, yt[0], rt[i]) {
    92  					return moerr.NewOutOfRangeNoCtx("int", "int SUB")
    93  				}
    94  			}
    95  		}
    96  		return nil
    97  	} else {
    98  		for i, x := range xt {
    99  			if !nulls.Contains(rs.Nsp, uint64(i)) {
   100  				rt[i] = x - yt[i]
   101  				if overflow.OverflowIntSub(x, yt[i], rt[i]) {
   102  					return moerr.NewOutOfRangeNoCtx("int", "int SUB")
   103  				}
   104  			}
   105  		}
   106  		return nil
   107  	}
   108  }
   109  
   110  func goNumericSubFloat[T constraints.Float](xs, ys, rs *vector.Vector) error {
   111  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
   112  	if xs.IsScalar() {
   113  		for i, y := range yt {
   114  			if !nulls.Contains(rs.Nsp, uint64(i)) {
   115  				rt[i] = xt[0] - y
   116  			}
   117  		}
   118  		return nil
   119  	} else if ys.IsScalar() {
   120  		for i, x := range xt {
   121  			if !nulls.Contains(rs.Nsp, uint64(i)) {
   122  				rt[i] = x - yt[0]
   123  			}
   124  		}
   125  		return nil
   126  	} else {
   127  		for i, x := range xt {
   128  			if !nulls.Contains(rs.Nsp, uint64(i)) {
   129  				rt[i] = x - yt[i]
   130  			}
   131  		}
   132  		return nil
   133  	}
   134  }
   135  
   136  func NumericSubSigned[T constraints.Signed](xs, ys, rs *vector.Vector) error {
   137  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
   138  	flag := 0
   139  	if xs.IsScalar() {
   140  		flag |= LEFT_IS_SCALAR
   141  	}
   142  	if ys.IsScalar() {
   143  		flag |= RIGHT_IS_SCALAR
   144  	}
   145  
   146  	rc := C.SignedInt_VecSub(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
   147  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
   148  	if rc != 0 {
   149  		return moerr.NewOutOfRangeNoCtx("int", "int SUB")
   150  	}
   151  	return nil
   152  }
   153  
   154  func NumericSubUnsigned[T constraints.Unsigned](xs, ys, rs *vector.Vector) error {
   155  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
   156  	flag := 0
   157  	if xs.IsScalar() {
   158  		flag |= LEFT_IS_SCALAR
   159  	}
   160  	if ys.IsScalar() {
   161  		flag |= RIGHT_IS_SCALAR
   162  	}
   163  	rc := C.UnsignedInt_VecSub(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
   164  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
   165  	if rc != 0 {
   166  		return moerr.NewOutOfRangeNoCtx("unsigned int", "unsigned int SUB")
   167  	}
   168  	return nil
   169  }
   170  
   171  func NumericSubFloat[T constraints.Float](xs, ys, rs *vector.Vector) error {
   172  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[T](rs)
   173  	flag := 0
   174  	if xs.IsScalar() {
   175  		flag |= LEFT_IS_SCALAR
   176  	}
   177  	if ys.IsScalar() {
   178  		flag |= RIGHT_IS_SCALAR
   179  	}
   180  
   181  	rc := C.Float_VecSub(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
   182  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(rs.Typ.TypeSize()))
   183  	if rc != 0 {
   184  		return moerr.NewOutOfRangeNoCtx("float", "float SUB")
   185  	}
   186  	return nil
   187  }