github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/sub/dec.go (about)

     1  // Copyright 2021 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  
    32  	"github.com/matrixorigin/matrixone/pkg/container/types"
    33  )
    34  
    35  func dec64PtrToC(p *types.Decimal64) *C.int64_t {
    36  	return (*C.int64_t)(unsafe.Pointer(p))
    37  }
    38  func dec128PtrToC(p *types.Decimal128) *C.int64_t {
    39  	return (*C.int64_t)(unsafe.Pointer(p))
    40  }
    41  
    42  func Decimal64VecSub(xs, ys, rs *vector.Vector) error {
    43  	xt := vector.MustTCols[types.Decimal64](xs)
    44  	yt := vector.MustTCols[types.Decimal64](ys)
    45  	rt := vector.MustTCols[types.Decimal64](rs)
    46  
    47  	flag := 0
    48  	if xs.IsScalar() {
    49  		flag |= LEFT_IS_SCALAR
    50  	}
    51  	if ys.IsScalar() {
    52  		flag |= RIGHT_IS_SCALAR
    53  	}
    54  
    55  	rc := C.Decimal64_VecSub(dec64PtrToC(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
    56  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    57  	if rc != 0 {
    58  		return moerr.NewOutOfRangeNoCtx("decimal64", "decimal SUB")
    59  	}
    60  	return nil
    61  }
    62  
    63  func Decimal128VecSub(xs, ys, rs *vector.Vector) error {
    64  	xt := vector.MustTCols[types.Decimal128](xs)
    65  	yt := vector.MustTCols[types.Decimal128](ys)
    66  	rt := vector.MustTCols[types.Decimal128](rs)
    67  
    68  	flag := 0
    69  	if xs.IsScalar() {
    70  		flag |= LEFT_IS_SCALAR
    71  	}
    72  	if ys.IsScalar() {
    73  		flag |= RIGHT_IS_SCALAR
    74  	}
    75  	rc := C.Decimal128_VecSub(dec128PtrToC(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
    76  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    77  	if rc != 0 {
    78  		return moerr.NewOutOfRangeNoCtx("decimal128", "decimal SUB")
    79  	}
    80  	return nil
    81  }
    82  
    83  func DatetimeSub(xs, ys, rs *vector.Vector) error {
    84  	xt := vector.MustTCols[types.Datetime](xs)
    85  	yt := vector.MustTCols[types.Datetime](ys)
    86  	rt := vector.MustTCols[int64](rs)
    87  
    88  	if len(xt) == 1 && len(yt) == 1 {
    89  		res := xt[0].DatetimeMinusWithSecond(yt[0])
    90  		rt[0] = res
    91  	} else if len(xt) == 1 {
    92  		for i := range yt {
    93  			res := xt[0].DatetimeMinusWithSecond(yt[i])
    94  			rt[i] = res
    95  		}
    96  	} else if len(yt) == 1 {
    97  		for i := range xt {
    98  			res := xt[i].DatetimeMinusWithSecond(yt[0])
    99  			rt[i] = res
   100  		}
   101  	} else {
   102  		for i := range xt {
   103  			res := xt[i].DatetimeMinusWithSecond(yt[i])
   104  			rt[i] = res
   105  		}
   106  	}
   107  	return nil
   108  }