github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/div/dec.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 div
    16  
    17  /*
    18  #include "mo.h"
    19  
    20  #cgo CFLAGS: -I../../../cgo
    21  #cgo LDFLAGS: -L../../../cgo -lmo -lm
    22  */
    23  import "C"
    24  import (
    25  	"unsafe"
    26  
    27  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    28  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    29  	"github.com/matrixorigin/matrixone/pkg/container/types"
    30  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    31  )
    32  
    33  const (
    34  	RC_DIVISION_BY_ZERO = 20200
    35  	RC_OUT_OF_RANGE     = 20201
    36  )
    37  
    38  func dec64PtrToC(p *types.Decimal64) *C.int64_t {
    39  	return (*C.int64_t)(unsafe.Pointer(p))
    40  }
    41  
    42  func dec128PtrToC(p *types.Decimal128) *C.int64_t {
    43  	return (*C.int64_t)(unsafe.Pointer(p))
    44  }
    45  
    46  func Decimal64VecDiv(xs, ys, rs *vector.Vector) error {
    47  	xt := vector.MustTCols[types.Decimal64](xs)
    48  	yt := vector.MustTCols[types.Decimal64](ys)
    49  	rt := vector.MustTCols[types.Decimal128](rs)
    50  	flag := 0
    51  	if xs.IsScalar() {
    52  		flag |= LEFT_IS_SCALAR
    53  	}
    54  	if ys.IsScalar() {
    55  		flag |= RIGHT_IS_SCALAR
    56  	}
    57  
    58  	rc := C.Decimal64_VecDiv(dec128PtrToC(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
    59  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    60  	if rc != 0 {
    61  		if rc == RC_DIVISION_BY_ZERO {
    62  			return moerr.NewDivByZeroNoCtx()
    63  		} else if rc == RC_OUT_OF_RANGE {
    64  			return moerr.NewOutOfRangeNoCtx("decimal64", "decimal div")
    65  		} else {
    66  			return moerr.NewInternalErrorNoCtx("decimal64 div internal error")
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  func Decimal128VecDiv(xs, ys, rs *vector.Vector) error {
    73  	xt := vector.MustTCols[types.Decimal128](xs)
    74  	yt := vector.MustTCols[types.Decimal128](ys)
    75  	rt := vector.MustTCols[types.Decimal128](rs)
    76  	flag := 0
    77  	if xs.IsScalar() {
    78  		flag |= LEFT_IS_SCALAR
    79  	}
    80  	if ys.IsScalar() {
    81  		flag |= RIGHT_IS_SCALAR
    82  	}
    83  
    84  	rc := C.Decimal128_VecDiv(dec128PtrToC(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
    85  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    86  	if rc != 0 {
    87  		if rc == RC_DIVISION_BY_ZERO {
    88  			return moerr.NewDivByZeroNoCtx()
    89  		} else if rc == RC_OUT_OF_RANGE {
    90  			return moerr.NewOutOfRangeNoCtx("decimal128", "decimal div")
    91  		} else {
    92  			return moerr.NewInternalErrorNoCtx("decimal128 div internal error")
    93  		}
    94  	}
    95  	return nil
    96  }