github.com/matrixorigin/matrixone@v1.2.0/cgo/logic.c (about)

     1  /*
     2   * Copyright 2021 Matrix Origin
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #include "mo_impl.h"
    18  
    19  /*
    20     Logical and operator truth table
    21      A     |  B     |  A AND B
    22      ------|--------|----------
    23      true  | true   |  true
    24      true  | false  |  false
    25      true  | null   |  null
    26      false | true   |  false
    27      false | false  |  false
    28      false | null   |  false
    29      null  | true   |  null
    30      null  | false  |  false
    31      null  | null   |  null
    32  */
    33  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) {
    34      bool *rt = (bool *) r;
    35      bool *at = (bool *) a;
    36      bool *bt = (bool *) b;
    37      if ((flag & LEFT_IS_SCALAR) != 0) {
    38          for (uint64_t i = 0; i < n; i++) {
    39              rt[i] = at[0] && bt[i];
    40          }
    41          if (rnulls != NULL && !at[0]) {
    42              for (uint64_t i = 0; i < n; i++) {
    43                  if (Bitmap_Contains(rnulls, i)) {
    44                      Bitmap_Remove(rnulls, i);
    45                  }
    46              }
    47          }
    48      } else if ((flag & RIGHT_IS_SCALAR) != 0) {
    49          for (uint64_t i = 0; i < n; i++) {
    50              rt[i] = at[i] && bt[0];
    51          }
    52          if (rnulls != NULL && !bt[0]) {
    53              for (uint64_t i = 0; i < n; i++) {
    54                  if (Bitmap_Contains(rnulls, i)) {
    55                      Bitmap_Remove(rnulls, i);
    56                  }
    57              }
    58          }
    59      } else {
    60          for (uint64_t i = 0; i < n; i++) {
    61              rt[i] = at[i] && bt[i];
    62          }
    63          if (anulls != NULL && bnulls != NULL) {
    64              for (uint64_t i = 0; i < n; i++) {
    65                  if (Bitmap_Contains(anulls, i) && !Bitmap_Contains(bnulls, i)) {
    66                      if (!bt[i]) {
    67                          Bitmap_Remove(rnulls, i);
    68                      }
    69                  }
    70  
    71                  if (Bitmap_Contains(bnulls, i) && !Bitmap_Contains(anulls, i)) {
    72                      if (!at[i]) {
    73                          Bitmap_Remove(rnulls, i);
    74                      }
    75                  }
    76              }
    77          } else if (anulls != NULL) {
    78              for (uint64_t i = 0; i < n; i++) {
    79                  if (!bt[i] && Bitmap_Contains(anulls, i)) {
    80                      Bitmap_Remove(rnulls, i);
    81                  }
    82              }
    83          } else if (bnulls != NULL) {
    84              for (uint64_t i = 0; i < n; i++) {
    85                  if (!at[i] && Bitmap_Contains(bnulls, i)) {
    86                      Bitmap_Remove(rnulls, i);
    87                  }
    88              }
    89          }
    90      }
    91      return RC_SUCCESS;
    92  }
    93  
    94  /*
    95   Logical or operator truth table
    96      A     |  B     |  A OR B
    97      ------|--------|----------
    98      true  | true   |  true
    99      true  | false  |  true
   100      true  | null   |  true
   101      false | true   |  true
   102      false | false  |  false
   103      false | null   |  null
   104      null  | true   |  true
   105      null  | false  |  null
   106      null  | null   |  null
   107  */
   108  int32_t Logic_VecOr(void *r, void *a, void  *b, uint64_t n, uint64_t *anulls, uint64_t *bnulls, uint64_t *rnulls, int32_t flag) {
   109      bool *rt = (bool *) r;
   110      bool *at = (bool *) a;
   111      bool *bt = (bool *) b;
   112      if ((flag & LEFT_IS_SCALAR) != 0) {
   113          for (uint64_t i = 0; i < n; i++) {
   114              rt[i] = at[0] || bt[i];
   115          }
   116          if (rnulls != NULL && at[0]) {
   117              for (uint64_t i = 0; i < n; i++) {
   118                  if (Bitmap_Contains(rnulls, i)) {
   119                      Bitmap_Remove(rnulls, i);
   120                  }
   121              }
   122          }
   123      } else if ((flag & RIGHT_IS_SCALAR) != 0) {
   124          for (uint64_t i = 0; i < n; i++) {
   125              rt[i] = at[i] || bt[0];
   126          }
   127          if (rnulls != NULL && bt[0]) {
   128              for (uint64_t i = 0; i < n; i++) {
   129                  if (Bitmap_Contains(rnulls, i)) {
   130                      Bitmap_Remove(rnulls, i);
   131                  }
   132              }
   133          }
   134      } else {
   135          for (uint64_t i = 0; i < n; i++) {
   136              rt[i] = at[i] || bt[i];
   137          }
   138          if (anulls != NULL && bnulls != NULL) {
   139              for (uint64_t i = 0; i < n; i++) {
   140                  if (Bitmap_Contains(anulls, i) &&  !Bitmap_Contains(bnulls, i)) {
   141                      if (bt[i]) {
   142                          Bitmap_Remove(rnulls, i);
   143                      }
   144                  }
   145  
   146                  if (Bitmap_Contains(bnulls, i) &&  !Bitmap_Contains(anulls, i)) {
   147                      if (at[i]) {
   148                          Bitmap_Remove(rnulls, i);
   149                      }
   150                  }
   151              }
   152          } else if (anulls != NULL) {
   153              for (uint64_t i = 0; i < n; i++) {
   154                  if (bt[i] && Bitmap_Contains(anulls, i)) {
   155                      Bitmap_Remove(rnulls, i);
   156                  }
   157              }
   158          } else if (bnulls != NULL) {
   159              for (uint64_t i = 0; i < n; i++) {
   160                  if (at[i] && Bitmap_Contains(bnulls, i)) {
   161                      Bitmap_Remove(rnulls, i);
   162                  }
   163              }
   164          }
   165      }
   166      return RC_SUCCESS;
   167  }
   168  
   169  /*
   170   Logical exclusive or truth table
   171      A     |  B     |  A XOR B
   172      ------|--------|----------
   173      true  | true   |  false
   174      true  | false  |  true
   175      true  | null   |  null
   176      false | true   |  true
   177      false | false  |  false
   178      false | null   |  null
   179      null  | true   |  true
   180      null  | false  |  null
   181      null  | null   |  null
   182  */
   183  int32_t Logic_VecXor(void *r, void *a, void  *b, uint64_t n, uint64_t *nulls, int32_t flag) {
   184      bool *rt = (bool *) r;
   185      bool *at = (bool *) a;
   186      bool *bt = (bool *) b;
   187      if ((flag & LEFT_IS_SCALAR) != 0) {
   188          for (uint64_t i = 0; i < n; i++) {
   189              rt[i] = (at[0] || bt[i]) && !(at[0] && bt[i]);
   190          }
   191      } else if ((flag & RIGHT_IS_SCALAR) != 0) {
   192          for (uint64_t i = 0; i < n; i++) {
   193              rt[i] = (at[i] || bt[0]) && !(at[i] && bt[0]);
   194          }
   195      } else {
   196          for (uint64_t i = 0; i < n; i++) {
   197              rt[i] = (at[i] || bt[i]) && !(at[i] && bt[i]);
   198          }
   199      }
   200      return RC_SUCCESS;
   201  }
   202  
   203  /*
   204      Logical not truth table
   205      P     |   NOT P
   206      ----------------
   207      true  |   false
   208      false |   true
   209      null  |   null
   210  */
   211  int32_t Logic_VecNot(void *r, void *a, uint64_t n, uint64_t *nulls, int32_t flag) {
   212      bool *rt = (bool *) r;
   213      bool *at = (bool *) a;
   214      if ((flag & LEFT_IS_SCALAR) != 0) {
   215          rt[0] = !at[0];
   216      } else {
   217          for (uint64_t i = 0; i < n; i++) {
   218              rt[i] = !at[i];
   219          }
   220      }
   221      return RC_SUCCESS;
   222  }