github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/compare/compare.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 compare
    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  	"golang.org/x/exp/constraints"
    33  )
    34  
    35  const (
    36  	LEFT_IS_SCALAR  = 1
    37  	RIGHT_IS_SCALAR = 2
    38  )
    39  
    40  func dec64PtrToC(p *types.Decimal64) *C.int64_t {
    41  	return (*C.int64_t)(unsafe.Pointer(p))
    42  }
    43  func dec128PtrToC(p *types.Decimal128) *C.int64_t {
    44  	return (*C.int64_t)(unsafe.Pointer(p))
    45  }
    46  
    47  func GetScalarFlag(xs, ys, rs *vector.Vector) int {
    48  	flag := 0
    49  	if xs.IsScalar() {
    50  		flag |= LEFT_IS_SCALAR
    51  	}
    52  	if ys.IsScalar() {
    53  		flag |= RIGHT_IS_SCALAR
    54  	}
    55  	return flag
    56  }
    57  
    58  func NumericEqual[T constraints.Integer | constraints.Float | bool](xs, ys, rs *vector.Vector) error {
    59  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[bool](rs)
    60  	flag := GetScalarFlag(xs, ys, rs)
    61  	rc := C.Numeric_VecEq(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
    62  		(*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(int32(xs.Typ.Oid)))
    63  	if rc != 0 {
    64  		return moerr.NewInvalidArgNoCtx("numeric equal", "")
    65  	}
    66  	return nil
    67  }
    68  
    69  func NumericNotEqual[T constraints.Integer | constraints.Float | bool](xs, ys, rs *vector.Vector) error {
    70  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[bool](rs)
    71  	flag := GetScalarFlag(xs, ys, rs)
    72  	rc := C.Numeric_VecNe(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
    73  		(*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(int32(xs.Typ.Oid)))
    74  	if rc != 0 {
    75  		return moerr.NewInvalidArgNoCtx("numeric not equal", "")
    76  	}
    77  	return nil
    78  }
    79  
    80  func NumericGreatThan[T constraints.Integer | constraints.Float | bool](xs, ys, rs *vector.Vector) error {
    81  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[bool](rs)
    82  	flag := GetScalarFlag(xs, ys, rs)
    83  	rc := C.Numeric_VecGt(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
    84  		(*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(int32(xs.Typ.Oid)))
    85  	if rc != 0 {
    86  		return moerr.NewInvalidArgNoCtx("numeric greater than", "")
    87  	}
    88  	return nil
    89  }
    90  
    91  func NumericGreatEqual[T constraints.Integer | constraints.Float | bool](xs, ys, rs *vector.Vector) error {
    92  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[bool](rs)
    93  	flag := GetScalarFlag(xs, ys, rs)
    94  	rc := C.Numeric_VecGe(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
    95  		(*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(int32(xs.Typ.Oid)))
    96  	if rc != 0 {
    97  		return moerr.NewInvalidArgNoCtx("numeric greater equal", "")
    98  	}
    99  	return nil
   100  }
   101  
   102  func NumericLessThan[T constraints.Integer | constraints.Float | bool](xs, ys, rs *vector.Vector) error {
   103  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[bool](rs)
   104  	flag := GetScalarFlag(xs, ys, rs)
   105  	rc := C.Numeric_VecLt(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
   106  		(*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(int32(xs.Typ.Oid)))
   107  	if rc != 0 {
   108  		return moerr.NewInvalidArgNoCtx("numeric less than", "")
   109  	}
   110  	return nil
   111  }
   112  
   113  func NumericLessEqual[T constraints.Integer | constraints.Float | bool](xs, ys, rs *vector.Vector) error {
   114  	xt, yt, rt := vector.MustTCols[T](xs), vector.MustTCols[T](ys), vector.MustTCols[bool](rs)
   115  	flag := GetScalarFlag(xs, ys, rs)
   116  	rc := C.Numeric_VecLe(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
   117  		(*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag), C.int32_t(int32(xs.Typ.Oid)))
   118  	if rc != 0 {
   119  		return moerr.NewInvalidArgNoCtx("numeric less equal", "")
   120  	}
   121  	return nil
   122  }
   123  
   124  func Decimal64VecEq(xs, ys, rs *vector.Vector) error {
   125  	xt := vector.MustTCols[types.Decimal64](xs)
   126  	yt := vector.MustTCols[types.Decimal64](ys)
   127  	rt := vector.MustTCols[bool](rs)
   128  	flag := GetScalarFlag(xs, ys, rs)
   129  	rc := C.Decimal64_VecEQ((*C.bool)(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
   130  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   131  	if rc != 0 {
   132  		return moerr.NewInvalidArgNoCtx("decimal64 equal", "")
   133  	}
   134  	return nil
   135  }
   136  
   137  func Decimal128VecEq(xs, ys, rs *vector.Vector) error {
   138  	xt := vector.MustTCols[types.Decimal128](xs)
   139  	yt := vector.MustTCols[types.Decimal128](ys)
   140  	rt := vector.MustTCols[bool](rs)
   141  	flag := GetScalarFlag(xs, ys, rs)
   142  	rc := C.Decimal128_VecEQ((*C.bool)(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
   143  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   144  	if rc != 0 {
   145  		return moerr.NewInvalidArgNoCtx("decimal128 equal", "")
   146  	}
   147  	return nil
   148  }
   149  
   150  func Decimal64VecNe(xs, ys, rs *vector.Vector) error {
   151  	xt := vector.MustTCols[types.Decimal64](xs)
   152  	yt := vector.MustTCols[types.Decimal64](ys)
   153  	rt := vector.MustTCols[bool](rs)
   154  	flag := GetScalarFlag(xs, ys, rs)
   155  	rc := C.Decimal64_VecNE((*C.bool)(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
   156  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   157  	if rc != 0 {
   158  		return moerr.NewInvalidArgNoCtx("decimal64 not equal", "")
   159  	}
   160  	return nil
   161  }
   162  
   163  func Decimal128VecNe(xs, ys, rs *vector.Vector) error {
   164  	xt := vector.MustTCols[types.Decimal128](xs)
   165  	yt := vector.MustTCols[types.Decimal128](ys)
   166  	rt := vector.MustTCols[bool](rs)
   167  	flag := GetScalarFlag(xs, ys, rs)
   168  	rc := C.Decimal128_VecNE((*C.bool)(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
   169  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   170  	if rc != 0 {
   171  		return moerr.NewInvalidArgNoCtx("decimal128 not equal", "")
   172  	}
   173  	return nil
   174  }
   175  
   176  func Decimal64VecGt(xs, ys, rs *vector.Vector) error {
   177  	xt := vector.MustTCols[types.Decimal64](xs)
   178  	yt := vector.MustTCols[types.Decimal64](ys)
   179  	rt := vector.MustTCols[bool](rs)
   180  	flag := GetScalarFlag(xs, ys, rs)
   181  	rc := C.Decimal64_VecGT((*C.bool)(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
   182  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   183  	if rc != 0 {
   184  		return moerr.NewInvalidArgNoCtx("decimal64 greater than", "")
   185  	}
   186  	return nil
   187  }
   188  
   189  func Decimal128VecGt(xs, ys, rs *vector.Vector) error {
   190  	xt := vector.MustTCols[types.Decimal128](xs)
   191  	yt := vector.MustTCols[types.Decimal128](ys)
   192  	rt := vector.MustTCols[bool](rs)
   193  	flag := GetScalarFlag(xs, ys, rs)
   194  	rc := C.Decimal128_VecGT((*C.bool)(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
   195  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   196  	if rc != 0 {
   197  		return moerr.NewInvalidArgNoCtx("decimal128 greater than", "")
   198  	}
   199  	return nil
   200  }
   201  
   202  func Decimal64VecGe(xs, ys, rs *vector.Vector) error {
   203  	xt := vector.MustTCols[types.Decimal64](xs)
   204  	yt := vector.MustTCols[types.Decimal64](ys)
   205  	rt := vector.MustTCols[bool](rs)
   206  	flag := GetScalarFlag(xs, ys, rs)
   207  	rc := C.Decimal64_VecGE((*C.bool)(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
   208  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   209  	if rc != 0 {
   210  		return moerr.NewInvalidArgNoCtx("decimal64 greater equal", "")
   211  	}
   212  	return nil
   213  }
   214  
   215  func Decimal128VecGe(xs, ys, rs *vector.Vector) error {
   216  	xt := vector.MustTCols[types.Decimal128](xs)
   217  	yt := vector.MustTCols[types.Decimal128](ys)
   218  	rt := vector.MustTCols[bool](rs)
   219  	flag := GetScalarFlag(xs, ys, rs)
   220  	rc := C.Decimal128_VecGE((*C.bool)(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
   221  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   222  	if rc != 0 {
   223  		return moerr.NewInvalidArgNoCtx("decimal128 greater equal", "")
   224  	}
   225  	return nil
   226  }
   227  
   228  func Decimal64VecLt(xs, ys, rs *vector.Vector) error {
   229  	xt := vector.MustTCols[types.Decimal64](xs)
   230  	yt := vector.MustTCols[types.Decimal64](ys)
   231  	rt := vector.MustTCols[bool](rs)
   232  	flag := GetScalarFlag(xs, ys, rs)
   233  	rc := C.Decimal64_VecLT((*C.bool)(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
   234  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   235  	if rc != 0 {
   236  		return moerr.NewInvalidArgNoCtx("decimal64 less than", "")
   237  	}
   238  	return nil
   239  }
   240  
   241  func Decimal128VecLt(xs, ys, rs *vector.Vector) error {
   242  	xt := vector.MustTCols[types.Decimal128](xs)
   243  	yt := vector.MustTCols[types.Decimal128](ys)
   244  	rt := vector.MustTCols[bool](rs)
   245  	flag := GetScalarFlag(xs, ys, rs)
   246  	rc := C.Decimal128_VecLT((*C.bool)(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
   247  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   248  	if rc != 0 {
   249  		return moerr.NewInvalidArgNoCtx("decimal128 less than", "")
   250  	}
   251  	return nil
   252  }
   253  
   254  func Decimal64VecLe(xs, ys, rs *vector.Vector) error {
   255  	xt := vector.MustTCols[types.Decimal64](xs)
   256  	yt := vector.MustTCols[types.Decimal64](ys)
   257  	rt := vector.MustTCols[bool](rs)
   258  	flag := GetScalarFlag(xs, ys, rs)
   259  	rc := C.Decimal64_VecLE((*C.bool)(&rt[0]), dec64PtrToC(&xt[0]), dec64PtrToC(&yt[0]),
   260  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   261  	if rc != 0 {
   262  		return moerr.NewInvalidArgNoCtx("decimal64 less equal", "")
   263  	}
   264  	return nil
   265  }
   266  
   267  func Decimal128VecLe(xs, ys, rs *vector.Vector) error {
   268  	xt := vector.MustTCols[types.Decimal128](xs)
   269  	yt := vector.MustTCols[types.Decimal128](ys)
   270  	rt := vector.MustTCols[bool](rs)
   271  	flag := GetScalarFlag(xs, ys, rs)
   272  	rc := C.Decimal128_VecLE((*C.bool)(&rt[0]), dec128PtrToC(&xt[0]), dec128PtrToC(&yt[0]),
   273  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
   274  	if rc != 0 {
   275  		return moerr.NewInvalidArgNoCtx("decimal128 less equal", "")
   276  	}
   277  	return nil
   278  }