github.com/matrixorigin/matrixone@v1.2.0/cgo/compare.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 #define Type_BOOL 10 21 #define Type_INT8 20 22 #define Type_INT16 21 23 #define Type_INT32 22 24 #define Type_INT64 23 25 #define Type_INT128 24 26 #define Type_UINT8 25 27 #define Type_UINT16 26 28 #define Type_UINT32 27 29 #define Type_UINT64 28 30 #define Type_UINT128 29 31 #define Type_FLOAT32 30 32 #define Type_FLOAT64 31 33 34 // Time 35 #define Type_DATE 50 36 #define Type_TIME 51 37 #define Type_DATETIME 52 38 #define Type_TIMESTAMP 53 39 40 41 42 /* 43 * Equal operator (=) 44 */ 45 #define COMPARE_EQ(TGT, A, B) \ 46 TGT = ((A) == (B)) 47 48 49 /* 50 * Not Equal operator (<>) 51 */ 52 #define COMPARE_NE(TGT, A, B) \ 53 TGT = ((A) != (B)) 54 55 56 /* 57 * great than operator (>) 58 */ 59 #define COMPARE_GT(TGT, A, B) \ 60 TGT = ((A) > (B)) 61 62 63 /* 64 * great equal operator (>=) 65 */ 66 #define COMPARE_GE(TGT, A, B) \ 67 TGT = ((A) >= (B)) 68 69 70 /* 71 * less than operator (<) 72 */ 73 #define COMPARE_LT(TGT, A, B) \ 74 TGT = ((A) < (B)) 75 76 77 /* 78 * less equal operator (<=) 79 */ 80 #define COMPARE_LE(TGT, A, B) \ 81 TGT = ((A) <= (B)) 82 83 84 /* 85 * bool compare operator 86 */ 87 #define COMPARE_BOOL_EQ(TGT, A, B) \ 88 TGT = (((A) && (B)) || (!(A) && !(B))) 89 90 #define COMPARE_BOOL_NE(TGT, A, B) \ 91 TGT = ((!(A) && (B)) || ((A) && !(B))) 92 93 #define COMPARE_BOOL_LE(TGT, A, B) \ 94 TGT = (!(A) || (B)) 95 96 #define COMPARE_BOOL_LT(TGT, A, B) \ 97 TGT = (!(A) && (B)) 98 99 #define COMPARE_BOOL_GE(TGT, A, B) \ 100 TGT = ((A) || !(B)) 101 102 #define COMPARE_BOOL_GT(TGT, A, B) \ 103 TGT = ((A) && !(B)) 104 105 106 107 108 109 #define MO_COMPARE_T(OP, ZT) \ 110 bool *rt = (bool *) r; \ 111 ZT *at = (ZT *) a; \ 112 ZT *bt = (ZT *) b; \ 113 if ((flag & LEFT_IS_SCALAR) != 0) { \ 114 if (nulls != NULL) { \ 115 for (uint64_t i = 0; i < n; i++) { \ 116 if (!bitmap_test(nulls, i)) { \ 117 OP(rt[i], at[0], bt[i]); \ 118 } \ 119 } \ 120 } else { \ 121 for (uint64_t i = 0; i < n; i++) { \ 122 OP(rt[i], at[0], bt[i]); \ 123 } \ 124 } \ 125 } else if ((flag & RIGHT_IS_SCALAR) != 0) { \ 126 if (nulls != NULL) { \ 127 for (uint64_t i = 0; i < n; i++) { \ 128 if (!bitmap_test(nulls, i)) { \ 129 OP(rt[i], at[i], bt[0]); \ 130 } \ 131 } \ 132 } else { \ 133 for (uint64_t i = 0; i < n; i++) { \ 134 OP(rt[i], at[i], bt[0]); \ 135 } \ 136 } \ 137 } else { \ 138 if (nulls != NULL) { \ 139 for (uint64_t i = 0; i < n; i++) { \ 140 if (!bitmap_test(nulls, i)) { \ 141 OP(rt[i], at[i], bt[i]); \ 142 } \ 143 } \ 144 } else { \ 145 for (uint64_t i = 0; i < n; i++) { \ 146 OP(rt[i], at[i], bt[i]); \ 147 } \ 148 } \ 149 } 150 151 152 153 int32_t Numeric_VecEq(void *r, void *a, void *b, uint64_t n, uint64_t *nulls, int32_t flag, int32_t type) 154 { 155 if (type == Type_INT8) { 156 MO_COMPARE_T(COMPARE_EQ, int8_t); 157 } else if (type == Type_INT16) { 158 MO_COMPARE_T(COMPARE_EQ, int16_t); 159 } else if (type == Type_INT32) { 160 MO_COMPARE_T(COMPARE_EQ, int32_t); 161 } else if (type == Type_INT64) { 162 MO_COMPARE_T(COMPARE_EQ, int64_t); 163 } else if (type == Type_UINT8) { 164 MO_COMPARE_T(COMPARE_EQ, uint8_t); 165 } else if (type == Type_UINT16) { 166 MO_COMPARE_T(COMPARE_EQ, uint16_t); 167 } else if (type == Type_UINT32) { 168 MO_COMPARE_T(COMPARE_EQ, uint32_t); 169 } else if (type == Type_UINT64) { 170 MO_COMPARE_T(COMPARE_EQ, uint64_t); 171 } else if (type == Type_FLOAT32) { 172 MO_COMPARE_T(COMPARE_EQ, float); 173 } else if (type == Type_FLOAT64) { 174 MO_COMPARE_T(COMPARE_EQ, double); 175 } else if (type == Type_DATE) { 176 MO_COMPARE_T(COMPARE_EQ, int32_t); 177 } else if (type == Type_TIME) { 178 MO_COMPARE_T(COMPARE_EQ, int64_t); 179 } else if (type == Type_DATETIME) { 180 MO_COMPARE_T(COMPARE_EQ, int64_t); 181 } else if (type == Type_TIMESTAMP) { 182 MO_COMPARE_T(COMPARE_EQ, int64_t); 183 } else if (type == Type_BOOL) { 184 MO_COMPARE_T(COMPARE_BOOL_EQ, bool); 185 } else { 186 return RC_INVALID_ARGUMENT; 187 } 188 return RC_SUCCESS; 189 } 190 191 192 int32_t Numeric_VecNe(void *r, void *a, void *b, uint64_t n, uint64_t *nulls, int32_t flag, int32_t type) 193 { 194 if (type == Type_INT8) { 195 MO_COMPARE_T(COMPARE_NE, int8_t); 196 } else if (type == Type_INT16) { 197 MO_COMPARE_T(COMPARE_NE, int16_t); 198 } else if (type == Type_INT32) { 199 MO_COMPARE_T(COMPARE_NE, int32_t); 200 } else if (type == Type_INT64) { 201 MO_COMPARE_T(COMPARE_NE, int64_t); 202 } else if (type == Type_UINT8) { 203 MO_COMPARE_T(COMPARE_NE, uint8_t); 204 } else if (type == Type_UINT16) { 205 MO_COMPARE_T(COMPARE_NE, uint16_t); 206 } else if (type == Type_UINT32) { 207 MO_COMPARE_T(COMPARE_NE, uint32_t); 208 } else if (type == Type_UINT64) { 209 MO_COMPARE_T(COMPARE_NE, uint64_t); 210 } else if (type == Type_FLOAT32) { 211 MO_COMPARE_T(COMPARE_NE, float); 212 } else if (type == Type_FLOAT64) { 213 MO_COMPARE_T(COMPARE_NE, double); 214 } else if (type == Type_DATE) { 215 MO_COMPARE_T(COMPARE_NE, int32_t); 216 } else if (type == Type_TIME){ 217 MO_COMPARE_T(COMPARE_NE, int64_t); 218 } else if (type == Type_DATETIME) { 219 MO_COMPARE_T(COMPARE_NE, int64_t); 220 } else if (type == Type_TIMESTAMP) { 221 MO_COMPARE_T(COMPARE_NE, int64_t); 222 } else if (type == Type_BOOL) { 223 MO_COMPARE_T(COMPARE_BOOL_NE, bool); 224 } else { 225 return RC_INVALID_ARGUMENT; 226 } 227 return RC_SUCCESS; 228 } 229 230 231 int32_t Numeric_VecGt(void *r, void *a, void *b, uint64_t n, uint64_t *nulls, int32_t flag, int32_t type) 232 { 233 if (type == Type_INT8) { 234 MO_COMPARE_T(COMPARE_GT, int8_t); 235 } else if (type == Type_INT16) { 236 MO_COMPARE_T(COMPARE_GT, int16_t); 237 } else if (type == Type_INT32) { 238 MO_COMPARE_T(COMPARE_GT, int32_t); 239 } else if (type == Type_INT64) { 240 MO_COMPARE_T(COMPARE_GT, int64_t); 241 } else if (type == Type_UINT8) { 242 MO_COMPARE_T(COMPARE_GT, uint8_t); 243 } else if (type == Type_UINT16) { 244 MO_COMPARE_T(COMPARE_GT, uint16_t); 245 } else if (type == Type_UINT32) { 246 MO_COMPARE_T(COMPARE_GT, uint32_t); 247 } else if (type == Type_UINT64) { 248 MO_COMPARE_T(COMPARE_GT, uint64_t); 249 } else if (type == Type_FLOAT32) { 250 MO_COMPARE_T(COMPARE_GT, float); 251 } else if (type == Type_FLOAT64) { 252 MO_COMPARE_T(COMPARE_GT, double); 253 } else if (type == Type_DATE) { 254 MO_COMPARE_T(COMPARE_GT, int32_t); 255 } else if (type == Type_TIME) { 256 MO_COMPARE_T(COMPARE_GT, int64_t); 257 } else if (type == Type_DATETIME) { 258 MO_COMPARE_T(COMPARE_GT, int64_t); 259 } else if (type == Type_TIMESTAMP) { 260 MO_COMPARE_T(COMPARE_GT, int64_t); 261 } else if (type == Type_BOOL) { 262 MO_COMPARE_T(COMPARE_BOOL_GT, bool); 263 } else { 264 return RC_INVALID_ARGUMENT; 265 } 266 return RC_SUCCESS; 267 } 268 269 int32_t Numeric_VecGe(void *r, void *a, void *b, uint64_t n, uint64_t *nulls, int32_t flag, int32_t type) 270 { 271 if (type == Type_INT8) { 272 MO_COMPARE_T(COMPARE_GE, int8_t); 273 } else if (type == Type_INT16) { 274 MO_COMPARE_T(COMPARE_GE, int16_t); 275 } else if (type == Type_INT32) { 276 MO_COMPARE_T(COMPARE_GE, int32_t); 277 } else if (type == Type_INT64) { 278 MO_COMPARE_T(COMPARE_GE, int64_t); 279 } else if (type == Type_UINT8) { 280 MO_COMPARE_T(COMPARE_GE, uint8_t); 281 } else if (type == Type_UINT16) { 282 MO_COMPARE_T(COMPARE_GE, uint16_t); 283 } else if (type == Type_UINT32) { 284 MO_COMPARE_T(COMPARE_GE, uint32_t); 285 } else if (type == Type_UINT64) { 286 MO_COMPARE_T(COMPARE_GE, uint64_t); 287 } else if (type == Type_FLOAT32) { 288 MO_COMPARE_T(COMPARE_GE, float); 289 } else if (type == Type_FLOAT64) { 290 MO_COMPARE_T(COMPARE_GE, double); 291 } else if (type == Type_DATE) { 292 MO_COMPARE_T(COMPARE_GE, int32_t); 293 } else if (type == Type_TIME) { 294 MO_COMPARE_T(COMPARE_GE, int64_t); 295 } else if (type == Type_DATETIME) { 296 MO_COMPARE_T(COMPARE_GE, int64_t); 297 } else if (type == Type_TIMESTAMP) { 298 MO_COMPARE_T(COMPARE_GE, int64_t); 299 } else if (type == Type_BOOL) { 300 MO_COMPARE_T(COMPARE_BOOL_GE, bool); 301 } else { 302 return RC_INVALID_ARGUMENT; 303 } 304 305 return RC_SUCCESS; 306 } 307 308 309 int32_t Numeric_VecLt(void *r, void *a, void *b, uint64_t n, uint64_t *nulls, int32_t flag, int32_t type) 310 { 311 if (type == Type_INT8) { 312 MO_COMPARE_T(COMPARE_LT, int8_t); 313 } else if (type == Type_INT16) { 314 MO_COMPARE_T(COMPARE_LT, int16_t); 315 } else if (type == Type_INT32) { 316 MO_COMPARE_T(COMPARE_LT, int32_t); 317 } else if (type == Type_INT64) { 318 MO_COMPARE_T(COMPARE_LT, int64_t); 319 } else if (type == Type_UINT8) { 320 MO_COMPARE_T(COMPARE_LT, uint8_t); 321 } else if (type == Type_UINT16) { 322 MO_COMPARE_T(COMPARE_LT, uint16_t); 323 } else if (type == Type_UINT32) { 324 MO_COMPARE_T(COMPARE_LT, uint32_t); 325 } else if (type == Type_UINT64) { 326 MO_COMPARE_T(COMPARE_LT, uint64_t); 327 } else if (type == Type_FLOAT32) { 328 MO_COMPARE_T(COMPARE_LT, float); 329 } else if (type == Type_FLOAT64) { 330 MO_COMPARE_T(COMPARE_LT, double); 331 } else if (type == Type_DATE) { 332 MO_COMPARE_T(COMPARE_LT, int32_t); 333 } else if (type == Type_TIME) { 334 MO_COMPARE_T(COMPARE_LT, int64_t); 335 } else if (type == Type_DATETIME) { 336 MO_COMPARE_T(COMPARE_LT, int64_t); 337 } else if (type == Type_TIMESTAMP) { 338 MO_COMPARE_T(COMPARE_LT, int64_t); 339 } else if (type == Type_BOOL) { 340 MO_COMPARE_T(COMPARE_BOOL_LT, bool); 341 } else { 342 return RC_INVALID_ARGUMENT; 343 } 344 return RC_SUCCESS; 345 } 346 347 348 int32_t Numeric_VecLe(void *r, void *a, void *b, uint64_t n, uint64_t *nulls, int32_t flag, int32_t type) 349 { 350 if (type == Type_INT8) { 351 MO_COMPARE_T(COMPARE_LE, int8_t); 352 } else if (type == Type_INT16) { 353 MO_COMPARE_T(COMPARE_LE, int16_t); 354 } else if (type == Type_INT32) { 355 MO_COMPARE_T(COMPARE_LE, int32_t); 356 } else if (type == Type_INT64) { 357 MO_COMPARE_T(COMPARE_LE, int64_t); 358 } else if (type == Type_UINT8) { 359 MO_COMPARE_T(COMPARE_LE, uint8_t); 360 } else if (type == Type_UINT16) { 361 MO_COMPARE_T(COMPARE_LE, uint16_t); 362 } else if (type == Type_UINT32) { 363 MO_COMPARE_T(COMPARE_LE, uint32_t); 364 } else if (type == Type_UINT64) { 365 MO_COMPARE_T(COMPARE_LE, uint64_t); 366 } else if (type == Type_FLOAT32) { 367 MO_COMPARE_T(COMPARE_LE, float); 368 } else if (type == Type_FLOAT64) { 369 MO_COMPARE_T(COMPARE_LE, double); 370 } else if (type == Type_DATE) { 371 MO_COMPARE_T(COMPARE_LE, int32_t); 372 } else if (type == Type_TIME) { 373 MO_COMPARE_T(COMPARE_LE, int64_t); 374 } else if (type == Type_DATETIME) { 375 MO_COMPARE_T(COMPARE_LE, int64_t); 376 } else if (type == Type_TIMESTAMP) { 377 MO_COMPARE_T(COMPARE_LE, int64_t); 378 } else if (type == Type_BOOL) { 379 MO_COMPARE_T(COMPARE_BOOL_LE, bool); 380 } else { 381 return RC_INVALID_ARGUMENT; 382 } 383 return RC_SUCCESS; 384 }