github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/logical/logical.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 logical
    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  
    33  const (
    34  	LEFT_IS_SCALAR  = 1
    35  	RIGHT_IS_SCALAR = 2
    36  )
    37  
    38  func And(xs, ys, rs *vector.Vector) error {
    39  	xt, yt, rt := vector.MustTCols[bool](xs), vector.MustTCols[bool](ys), vector.MustTCols[bool](rs)
    40  	flag := 0
    41  	if xs.IsScalar() {
    42  		flag |= LEFT_IS_SCALAR
    43  	}
    44  	if ys.IsScalar() {
    45  		flag |= RIGHT_IS_SCALAR
    46  	}
    47  	//int32_t Logic_VecAnd(void *r, void *a, void  *b, uint64_t n, uint64_t *anulls, uint64_t *bnulls, uint64_t *rnulls, int32_t flag)
    48  	rc := C.Logic_VecAnd(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
    49  		(*C.uint64_t)(nulls.Ptr(xs.Nsp)), (*C.uint64_t)(nulls.Ptr(ys.Nsp)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    50  	if rc != 0 {
    51  		return moerr.NewInternalErrorNoCtx("logical AND")
    52  	}
    53  	return nil
    54  }
    55  
    56  func Or(xs, ys, rs *vector.Vector) error {
    57  	xt, yt, rt := vector.MustTCols[bool](xs), vector.MustTCols[bool](ys), vector.MustTCols[bool](rs)
    58  	flag := 0
    59  	if xs.IsScalar() {
    60  		flag |= LEFT_IS_SCALAR
    61  	}
    62  	if ys.IsScalar() {
    63  		flag |= RIGHT_IS_SCALAR
    64  	}
    65  	rc := C.Logic_VecOr(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]), C.uint64_t(len(rt)),
    66  		(*C.uint64_t)(nulls.Ptr(xs.Nsp)), (*C.uint64_t)(nulls.Ptr(ys.Nsp)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    67  	if rc != 0 {
    68  		return moerr.NewInternalErrorNoCtx("logic OR")
    69  	}
    70  	return nil
    71  }
    72  
    73  func Xor(xs, ys, rs *vector.Vector) error {
    74  	xt, yt, rt := vector.MustTCols[bool](xs), vector.MustTCols[bool](ys), vector.MustTCols[bool](rs)
    75  	flag := 0
    76  	if xs.IsScalar() {
    77  		flag |= LEFT_IS_SCALAR
    78  	}
    79  	if ys.IsScalar() {
    80  		flag |= RIGHT_IS_SCALAR
    81  	}
    82  	// int32_t Logic_VecXor(void *r, void *a, void  *b, uint64_t n, uint64_t *nulls, int32_t flag)
    83  	rc := C.Logic_VecXor(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]), unsafe.Pointer(&yt[0]),
    84  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    85  	if rc != 0 {
    86  		return moerr.NewInternalErrorNoCtx("logic XOR")
    87  	}
    88  	return nil
    89  }
    90  
    91  func Not(xs, rs *vector.Vector) error {
    92  	xt, rt := vector.MustTCols[bool](xs), vector.MustTCols[bool](rs)
    93  	flag := 0
    94  	if xs.IsScalar() {
    95  		flag |= LEFT_IS_SCALAR
    96  	}
    97  	rc := C.Logic_VecNot(unsafe.Pointer(&rt[0]), unsafe.Pointer(&xt[0]),
    98  		C.uint64_t(len(rt)), (*C.uint64_t)(nulls.Ptr(rs.Nsp)), C.int32_t(flag))
    99  	if rc != 0 {
   100  		return moerr.NewInternalErrorNoCtx("logic NOT")
   101  	}
   102  	return nil
   103  }