github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/add/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 add
    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/types"
    31  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    32  )
    33  
    34  func dec64PtrToC(p *types.Decimal64) *C.int64_t {
    35  	return (*C.int64_t)(unsafe.Pointer(p))
    36  }
    37  func dec128PtrToC(p *types.Decimal128) *C.int64_t {
    38  	return (*C.int64_t)(unsafe.Pointer(p))
    39  }
    40  
    41  func Decimal64VecAdd(xs, ys, rs *vector.Vector) error {
    42  	xt := vector.MustTCols[types.Decimal64](xs)
    43  	yt := vector.MustTCols[types.Decimal64](ys)
    44  	rt := vector.MustTCols[types.Decimal64](rs)
    45  	flag := 0
    46  	if xs.IsScalar() {
    47  		flag |= LEFT_IS_SCALAR
    48  	}
    49  	if ys.IsScalar() {
    50  		flag |= RIGHT_IS_SCALAR
    51  	}
    52  
    53  	rc := C.Decimal64_VecAdd(dec64PtrToC(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
    54  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    55  	if rc != 0 {
    56  		return moerr.NewOutOfRangeNoCtx("decimal64", "decimal add overflow")
    57  	}
    58  	return nil
    59  }
    60  
    61  func Decimal128VecAdd(xs, ys, rs *vector.Vector) error {
    62  	xt := vector.MustTCols[types.Decimal128](xs)
    63  	yt := vector.MustTCols[types.Decimal128](ys)
    64  	rt := vector.MustTCols[types.Decimal128](rs)
    65  	flag := 0
    66  	if xs.IsScalar() {
    67  		flag |= LEFT_IS_SCALAR
    68  	}
    69  	if ys.IsScalar() {
    70  		flag |= RIGHT_IS_SCALAR
    71  	}
    72  	rc := C.Decimal128_VecAdd(dec128PtrToC(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
    73  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    74  	if rc != 0 {
    75  		return moerr.NewOutOfRangeNoCtx("decimal128", "decimal add overflow")
    76  	}
    77  	return nil
    78  }