tcompare.c 40.8 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#define _GNU_SOURCE
#define _XOPEN_SOURCE
#define _DEFAULT_SOURCE
S
compare  
Shengliang Guan 已提交
19
#include "tcompare.h"
H
Hongze Cheng 已提交
20
#include "regex.h"
S
Shengliang Guan 已提交
21
#include "tdef.h"
22
#include "thash.h"
S
log  
Shengliang Guan 已提交
23
#include "tlog.h"
S
Shengliang Guan 已提交
24
#include "types.h"
H
Hongze Cheng 已提交
25

D
dapan1121 已提交
26
int32_t setChkInBytes1(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
27 28 29
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0;
}

D
dapan1121 已提交
30
int32_t setChkInBytes2(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
31 32 33
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 2) ? 1 : 0;
}

D
dapan1121 已提交
34
int32_t setChkInBytes4(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
35 36 37
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 4) ? 1 : 0;
}

D
dapan1121 已提交
38
int32_t setChkInBytes8(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
39 40 41
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 8) ? 1 : 0;
}

D
dapan1121 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
int32_t setChkNotInBytes1(const void *pLeft, const void *pRight) {
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0;
}

int32_t setChkNotInBytes2(const void *pLeft, const void *pRight) {
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, 2) ? 1 : 0;
}

int32_t setChkNotInBytes4(const void *pLeft, const void *pRight) {
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, 4) ? 1 : 0;
}

int32_t setChkNotInBytes8(const void *pLeft, const void *pRight) {
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, 8) ? 1 : 0;
}

S
Shengliang Guan 已提交
58
int32_t compareChkInString(const void *pLeft, const void *pRight) {
59
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, varDataTLen(pLeft)) ? 1 : 0;
D
dapan1121 已提交
60 61
}

S
Shengliang Guan 已提交
62
int32_t compareChkNotInString(const void *pLeft, const void *pRight) {
63
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, varDataTLen(pLeft)) ? 1 : 0;
D
dapan1121 已提交
64 65
}

H
Hongze Cheng 已提交
66 67 68 69 70 71 72
int32_t compareInt8Val(const void *pLeft, const void *pRight) {
  int8_t left = GET_INT8_VAL(pLeft), right = GET_INT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
73
int32_t compareInt8ValDesc(const void *pLeft, const void *pRight) { return compareInt8Val(pRight, pLeft); }
H
Hongze Cheng 已提交
74 75 76 77 78 79 80 81

int32_t compareInt16Val(const void *pLeft, const void *pRight) {
  int16_t left = GET_INT16_VAL(pLeft), right = GET_INT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
82
int32_t compareInt16ValDesc(const void *pLeft, const void *pRight) { return compareInt16Val(pRight, pLeft); }
H
Hongze Cheng 已提交
83 84 85 86 87 88 89 90

int32_t compareInt32Val(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft), right = GET_INT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
91
int32_t compareInt32ValDesc(const void *pLeft, const void *pRight) { return compareInt32Val(pRight, pLeft); }
H
Hongze Cheng 已提交
92 93 94 95 96 97 98 99

int32_t compareInt64Val(const void *pLeft, const void *pRight) {
  int64_t left = GET_INT64_VAL(pLeft), right = GET_INT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
100
int32_t compareInt64ValDesc(const void *pLeft, const void *pRight) { return compareInt64Val(pRight, pLeft); }
H
Hongze Cheng 已提交
101 102 103 104 105 106 107 108

int32_t compareUint32Val(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft), right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
109
int32_t compareUint32ValDesc(const void *pLeft, const void *pRight) { return compareUint32Val(pRight, pLeft); }
H
Hongze Cheng 已提交
110 111 112 113 114 115 116 117

int32_t compareUint64Val(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft), right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
118
int32_t compareUint64ValDesc(const void *pLeft, const void *pRight) { return compareUint64Val(pRight, pLeft); }
H
Hongze Cheng 已提交
119 120 121 122 123 124 125 126

int32_t compareUint16Val(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft), right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
127
int32_t compareUint16ValDesc(const void *pLeft, const void *pRight) { return compareUint16Val(pRight, pLeft); }
H
Hongze Cheng 已提交
128

S
Shengliang Guan 已提交
129
int32_t compareUint8Val(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
130 131 132 133 134 135
  uint8_t left = GET_UINT8_VAL(pLeft), right = GET_UINT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

S
Shengliang Guan 已提交
136
int32_t compareUint8ValDesc(const void *pLeft, const void *pRight) { return compareUint8Val(pRight, pLeft); }
H
Hongze Cheng 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

int32_t compareFloatVal(const void *pLeft, const void *pRight) {
  float p1 = GET_FLOAT_VAL(pLeft);
  float p2 = GET_FLOAT_VAL(pRight);

  if (isnan(p1) && isnan(p2)) {
    return 0;
  }

  if (isnan(p1)) {
    return -1;
  }

  if (isnan(p2)) {
    return 1;
  }
  if (FLT_EQUAL(p1, p2)) {
    return 0;
  }
S
Shengliang Guan 已提交
156
  return FLT_GREATER(p1, p2) ? 1 : -1;
H
Hongze Cheng 已提交
157 158
}

S
Shengliang Guan 已提交
159
int32_t compareFloatValDesc(const void *pLeft, const void *pRight) { return compareFloatVal(pRight, pLeft); }
H
Hongze Cheng 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

int32_t compareDoubleVal(const void *pLeft, const void *pRight) {
  double p1 = GET_DOUBLE_VAL(pLeft);
  double p2 = GET_DOUBLE_VAL(pRight);

  if (isnan(p1) && isnan(p2)) {
    return 0;
  }

  if (isnan(p1)) {
    return -1;
  }

  if (isnan(p2)) {
    return 1;
  }
H
Haojun Liao 已提交
176

H
Hongze Cheng 已提交
177 178 179
  if (FLT_EQUAL(p1, p2)) {
    return 0;
  }
S
Shengliang Guan 已提交
180
  return FLT_GREATER(p1, p2) ? 1 : -1;
H
Hongze Cheng 已提交
181 182
}

S
Shengliang Guan 已提交
183
int32_t compareDoubleValDesc(const void *pLeft, const void *pRight) { return compareDoubleVal(pRight, pLeft); }
H
Hongze Cheng 已提交
184 185 186 187 188

int32_t compareLenPrefixedStr(const void *pLeft, const void *pRight) {
  int32_t len1 = varDataLen(pLeft);
  int32_t len2 = varDataLen(pRight);

H
Haojun Liao 已提交
189
  int32_t minLen = TMIN(len1, len2);
190 191
  int32_t ret = strncmp(varDataVal(pLeft), varDataVal(pRight), minLen);
  if (ret == 0) {
192
    if (len1 == len2) {
H
Hongze Cheng 已提交
193 194
      return 0;
    } else {
195
      return len1 > len2 ? 1 : -1;
H
Hongze Cheng 已提交
196 197
    }
  } else {
198
    return ret > 0 ? 1 : -1;
H
Hongze Cheng 已提交
199 200 201
  }
}

S
Shengliang Guan 已提交
202
int32_t compareLenPrefixedStrDesc(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
203 204 205 206 207 208 209 210
  return compareLenPrefixedStr(pRight, pLeft);
}

int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight) {
  int32_t len1 = varDataLen(pLeft);
  int32_t len2 = varDataLen(pRight);

  if (len1 != len2) {
S
Shengliang Guan 已提交
211
    return len1 > len2 ? 1 : -1;
H
Hongze Cheng 已提交
212
  } else {
wafwerar's avatar
wafwerar 已提交
213
    int32_t ret = memcmp((TdUcs4 *)pLeft, (TdUcs4 *)pRight, len1);
H
Hongze Cheng 已提交
214 215 216 217 218 219 220 221
    if (ret == 0) {
      return 0;
    } else {
      return ret > 0 ? 1 : -1;
    }
  }
}

S
Shengliang Guan 已提交
222
int32_t compareLenPrefixedWStrDesc(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
223
  return compareLenPrefixedWStr(pRight, pLeft);
224 225
}

wmmhello's avatar
wmmhello 已提交
226 227 228
// string > number > bool > null
// ref: https://dev.mysql.com/doc/refman/8.0/en/json.html#json-comparison
int32_t compareJsonVal(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
229 230 231
  char leftType = *(char *)pLeft;
  char rightType = *(char *)pRight;
  if (leftType != rightType) {
wmmhello's avatar
wmmhello 已提交
232 233 234
    return leftType > rightType ? 1 : -1;
  }

H
Hongze Cheng 已提交
235 236 237
  char *realDataLeft = POINTER_SHIFT(pLeft, CHAR_BYTES);
  char *realDataRight = POINTER_SHIFT(pRight, CHAR_BYTES);
  if (leftType == TSDB_DATA_TYPE_BOOL) {
wmmhello's avatar
wmmhello 已提交
238
    DEFAULT_COMP(GET_INT8_VAL(realDataLeft), GET_INT8_VAL(realDataRight));
H
Hongze Cheng 已提交
239
  } else if (leftType == TSDB_DATA_TYPE_DOUBLE) {
wmmhello's avatar
wmmhello 已提交
240
    DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(realDataLeft), GET_DOUBLE_VAL(realDataRight));
H
Hongze Cheng 已提交
241
  } else if (leftType == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
242
    return compareLenPrefixedWStr(realDataLeft, realDataRight);
H
Hongze Cheng 已提交
243
  } else if (leftType == TSDB_DATA_TYPE_NULL) {
wmmhello's avatar
wmmhello 已提交
244
    return 0;
H
Hongze Cheng 已提交
245
  } else {
wmmhello's avatar
wmmhello 已提交
246
    assert(0);
247
    return 0;
wmmhello's avatar
wmmhello 已提交
248 249 250
  }
}

D
dapan1121 已提交
251
int32_t compareInt8Int16(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
252
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
253 254 255 256 257 258 259
  int16_t right = GET_INT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Int32(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
260
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
261 262 263 264 265 266 267
  int32_t right = GET_INT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Int64(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
268
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
269 270 271 272 273 274 275
  int64_t right = GET_INT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Float(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
276
  int8_t left = GET_INT8_VAL(pLeft);
H
Hongze Cheng 已提交
277
  float  right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
278 279 280 281 282 283
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Double(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
284
  int8_t left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
285 286 287 288 289 290 291
  double right = GET_DOUBLE_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Uint8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
292
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
293 294 295 296 297 298 299
  uint8_t right = GET_UINT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Uint16(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
300
  int8_t   left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
301 302 303 304 305 306 307
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Uint32(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
308
  int8_t   left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
309 310 311 312 313 314 315
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Uint64(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
316
  int8_t   left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
317 318 319 320 321 322 323
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Int8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
324
  int16_t left = GET_INT16_VAL(pLeft);
H
Hongze Cheng 已提交
325
  int8_t  right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
326 327 328 329 330 331
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Int32(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
332
  int16_t left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
333 334 335 336 337 338 339
  int32_t right = GET_INT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Int64(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
340
  int16_t left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
341 342 343 344 345 346 347
  int64_t right = GET_INT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Float(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
348
  int16_t left = GET_INT16_VAL(pLeft);
H
Hongze Cheng 已提交
349
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
350 351 352 353 354 355
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Double(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
356
  int16_t left = GET_INT16_VAL(pLeft);
H
Hongze Cheng 已提交
357
  double  right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
358 359 360 361 362 363
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Uint8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
364
  int16_t left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
365 366 367 368 369 370 371
  uint8_t right = GET_UINT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Uint16(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
372
  int16_t  left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
373 374 375 376 377 378 379
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Uint32(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
380
  int16_t  left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
381 382 383 384 385 386 387
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Uint64(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
388
  int16_t  left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
389 390 391 392 393 394 395 396
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Int8(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft);
H
Hongze Cheng 已提交
397
  int8_t  right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Int16(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft);
  int16_t right = GET_INT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Int64(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft);
  int64_t right = GET_INT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Float(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft);
H
Hongze Cheng 已提交
421
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
422 423 424 425 426 427 428
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Double(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft);
H
Hongze Cheng 已提交
429
  double  right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Uint8(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft);
  uint8_t right = GET_UINT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Uint16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
444
  int32_t  left = GET_INT32_VAL(pLeft);
D
dapan1121 已提交
445 446 447 448 449 450 451
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Uint32(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
452
  int32_t  left = GET_INT32_VAL(pLeft);
D
dapan1121 已提交
453 454 455 456 457 458 459
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt32Uint64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
460
  int32_t  left = GET_INT32_VAL(pLeft);
D
dapan1121 已提交
461 462 463 464 465 466 467
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Int8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
468
  int64_t left = GET_INT64_VAL(pLeft);
H
Hongze Cheng 已提交
469
  int8_t  right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
470 471 472 473 474 475
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Int16(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
476
  int64_t left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
477 478 479 480 481 482 483
  int16_t right = GET_INT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Int32(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
484
  int64_t left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
485 486 487 488 489 490 491
  int32_t right = GET_INT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Float(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
492
  int64_t left = GET_INT64_VAL(pLeft);
H
Hongze Cheng 已提交
493
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
494 495 496 497 498 499
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Double(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
500
  int64_t left = GET_INT64_VAL(pLeft);
H
Hongze Cheng 已提交
501
  double  right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
502 503 504 505 506 507
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Uint8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
508
  int64_t left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
509 510 511 512 513 514 515
  uint8_t right = GET_UINT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Uint16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
516
  int64_t  left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
517 518 519 520 521 522 523
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Uint32(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
524
  int64_t  left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
525 526 527 528 529 530 531
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt64Uint64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
532
  int64_t  left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
533 534 535 536 537 538 539
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatInt8(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
540
  float  left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
541 542 543 544 545 546 547
  int8_t right = GET_INT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatInt16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
548
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
549 550 551 552 553 554 555
  int16_t right = GET_INT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatInt32(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
556
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
557 558 559 560 561 562 563
  int32_t right = GET_INT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatInt64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
564
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
565 566 567 568 569 570 571
  int64_t right = GET_INT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatDouble(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
572
  float  left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
573
  double right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590

  if (isnan(left) && isnan(right)) {
    return 0;
  }

  if (isnan(left)) {
    return -1;
  }

  if (isnan(right)) {
    return 1;
  }

  if (FLT_EQUAL(left, right)) {
    return 0;
  }
  return FLT_GREATER(left, right) ? 1 : -1;
D
dapan1121 已提交
591 592 593
}

int32_t compareFloatUint8(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
594
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
595 596 597 598 599 600 601
  uint8_t right = GET_UINT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatUint16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
602
  float    left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
603 604 605 606 607 608 609
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatUint32(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
610
  float    left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
611 612 613 614 615 616 617
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareFloatUint64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
618
  float    left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleInt8(const void *pLeft, const void *pRight) {
  double left = GET_DOUBLE_VAL(pLeft);
  int8_t right = GET_INT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleInt16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
634
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
635 636 637 638 639 640 641
  int16_t right = GET_INT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleInt32(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
642
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
643 644 645 646 647 648 649
  int32_t right = GET_INT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleInt64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
650
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
651 652 653 654 655 656 657 658
  int64_t right = GET_INT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleFloat(const void *pLeft, const void *pRight) {
  double left = GET_DOUBLE_VAL(pLeft);
H
Hongze Cheng 已提交
659
  float  right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

  if (isnan(left) && isnan(right)) {
    return 0;
  }

  if (isnan(left)) {
    return -1;
  }

  if (isnan(right)) {
    return 1;
  }

  if (FLT_EQUAL(left, right)) {
    return 0;
  }
  return FLT_GREATER(left, right) ? 1 : -1;
D
dapan1121 已提交
677 678 679
}

int32_t compareDoubleUint8(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
680
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
681 682 683 684 685 686 687
  uint8_t right = GET_UINT8_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleUint16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
688
  double   left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
689 690 691 692 693 694 695
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleUint32(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
696
  double   left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
697 698 699 700 701 702 703
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareDoubleUint64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
704
  double   left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
705 706 707 708 709 710 711 712
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Int8(const void *pLeft, const void *pRight) {
  uint8_t left = GET_UINT8_VAL(pLeft);
H
Hongze Cheng 已提交
713
  int8_t  right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Int16(const void *pLeft, const void *pRight) {
  uint8_t left = GET_UINT8_VAL(pLeft);
  int16_t right = GET_INT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Int32(const void *pLeft, const void *pRight) {
  uint8_t left = GET_UINT8_VAL(pLeft);
  int32_t right = GET_INT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Int64(const void *pLeft, const void *pRight) {
  uint8_t left = GET_UINT8_VAL(pLeft);
  int64_t right = GET_INT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Float(const void *pLeft, const void *pRight) {
  uint8_t left = GET_UINT8_VAL(pLeft);
H
Hongze Cheng 已提交
745
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
746 747 748 749 750 751 752
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Double(const void *pLeft, const void *pRight) {
  uint8_t left = GET_UINT8_VAL(pLeft);
H
Hongze Cheng 已提交
753
  double  right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
754 755 756 757 758 759
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Uint16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
760
  uint8_t  left = GET_UINT8_VAL(pLeft);
D
dapan1121 已提交
761 762 763 764 765 766 767
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Uint32(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
768
  uint8_t  left = GET_UINT8_VAL(pLeft);
D
dapan1121 已提交
769 770 771 772 773 774 775
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Uint64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
776
  uint8_t  left = GET_UINT8_VAL(pLeft);
D
dapan1121 已提交
777 778 779 780 781 782 783 784
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Int8(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
H
Hongze Cheng 已提交
785
  int8_t   right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
786 787 788 789 790 791 792
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Int16(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
H
Hongze Cheng 已提交
793
  int16_t  right = GET_INT16_VAL(pRight);
D
dapan1121 已提交
794 795 796 797 798 799 800
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Int32(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
H
Hongze Cheng 已提交
801
  int32_t  right = GET_INT32_VAL(pRight);
D
dapan1121 已提交
802 803 804 805 806 807 808
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Int64(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
H
Hongze Cheng 已提交
809
  int64_t  right = GET_INT64_VAL(pRight);
D
dapan1121 已提交
810 811 812 813 814 815 816
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Float(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
H
Hongze Cheng 已提交
817
  float    right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
818 819 820 821 822 823 824
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Double(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
H
Hongze Cheng 已提交
825
  double   right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
826 827 828 829 830 831 832
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Uint8(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
H
Hongze Cheng 已提交
833
  uint8_t  right = GET_UINT8_VAL(pRight);
D
dapan1121 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Uint32(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint16Uint64(const void *pLeft, const void *pRight) {
  uint16_t left = GET_UINT16_VAL(pLeft);
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Int8(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
857
  int8_t   right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
858 859 860 861 862 863 864
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Int16(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
865
  int16_t  right = GET_INT16_VAL(pRight);
D
dapan1121 已提交
866 867 868 869 870 871 872
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Int32(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
873
  int32_t  right = GET_INT32_VAL(pRight);
D
dapan1121 已提交
874 875 876 877 878 879 880
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Int64(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
881
  int64_t  right = GET_INT64_VAL(pRight);
D
dapan1121 已提交
882 883 884 885 886 887 888
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Float(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
889
  float    right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
890 891 892 893 894 895 896
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Double(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
897
  double   right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
898 899 900 901 902 903 904
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Uint8(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
905
  uint8_t  right = GET_UINT8_VAL(pRight);
D
dapan1121 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Uint16(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint32Uint64(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
  uint64_t right = GET_UINT64_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Int8(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
929
  int8_t   right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
930 931 932 933 934 935 936
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Int16(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
937
  int16_t  right = GET_INT16_VAL(pRight);
D
dapan1121 已提交
938 939 940 941 942 943 944
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Int32(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
945
  int32_t  right = GET_INT32_VAL(pRight);
D
dapan1121 已提交
946 947 948 949 950 951 952
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Int64(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
953
  int64_t  right = GET_INT64_VAL(pRight);
D
dapan1121 已提交
954 955 956 957 958 959 960
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Float(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
961
  float    right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
962 963 964 965 966 967 968
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Double(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
969
  double   right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
970 971 972 973 974 975 976
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Uint8(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
977
  uint8_t  right = GET_UINT8_VAL(pRight);
D
dapan1121 已提交
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Uint16(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
  uint16_t right = GET_UINT16_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint64Uint32(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
  uint32_t right = GET_UINT32_VAL(pRight);
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

H
Hongze Cheng 已提交
999
int32_t compareJsonValDesc(const void *pLeft, const void *pRight) { return compareJsonVal(pRight, pLeft); }
H
Hongze Cheng 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
/*
 * Compare two strings
 *    TSDB_MATCH:            Match
 *    TSDB_NOMATCH:          No match
 *    TSDB_NOWILDCARDMATCH:  No match in spite of having * or % wildcards.
 * Like matching rules:
 *      '%': Matches zero or more characters
 *      '_': Matches one character
 *
 */
1010
int32_t patternMatch(const char *patterStr, const char *str, size_t size, const SPatternCompareInfo *pInfo) {
H
Hongze Cheng 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
  char c, c1;

  int32_t i = 0;
  int32_t j = 0;
  int32_t o = 0;
  int32_t m = 0;

  while ((c = patterStr[i++]) != 0) {
    if (c == pInfo->matchAll) { /* Match "*" */

      while ((c = patterStr[i++]) == pInfo->matchAll || c == pInfo->matchOne) {
        if (c == pInfo->matchOne) {
          if (j > size || str[j++] == 0) {
            // empty string, return not match
            return TSDB_PATTERN_NOWILDCARDMATCH;
          } else {
            ++o;
          }
        }
      }

      if (c == 0) {
        return TSDB_PATTERN_MATCH; /* "*" at the end of the pattern matches */
      }

      char next[3] = {toupper(c), tolower(c), 0};
      m = o;
      while (1) {
        size_t n = strcspn(str + m, next);
        str += m + n;

        if (str[0] == 0 || (n >= size)) {
          break;
        }

        int32_t ret = patternMatch(&patterStr[i], ++str, size - n - 1, pInfo);
        if (ret != TSDB_PATTERN_NOMATCH) {
          return ret;
        }
        m = 0;
      }
      return TSDB_PATTERN_NOWILDCARDMATCH;
    }

    c1 = str[j++];
S
Shengliang Guan 已提交
1056 1057
    ++o;

H
Hongze Cheng 已提交
1058
    if (j <= size) {
S
Shengliang Guan 已提交
1059 1060 1061 1062
      if (c == '\\' && patterStr[i] == '_' && c1 == '_') {
        i++;
        continue;
      }
H
Hongze Cheng 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
      if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) {
        continue;
      }
    }

    return TSDB_PATTERN_NOMATCH;
  }

  return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH;
}

wafwerar's avatar
wafwerar 已提交
1074 1075 1076 1077
int32_t WCSPatternMatch(const TdUcs4 *patterStr, const TdUcs4 *str, size_t size, const SPatternCompareInfo *pInfo) {
  TdUcs4 c, c1;
  TdUcs4 matchOne = L'_';  // "_"
  TdUcs4 matchAll = L'%';  // "%"
H
Hongze Cheng 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

  int32_t i = 0;
  int32_t j = 0;

  while ((c = patterStr[i++]) != 0) {
    if (c == matchAll) { /* Match "%" */

      while ((c = patterStr[i++]) == matchAll || c == matchOne) {
        if (c == matchOne && (j >= size || str[j++] == 0)) {
          return TSDB_PATTERN_NOWILDCARDMATCH;
        }
      }
      if (c == 0) {
        return TSDB_PATTERN_MATCH;
      }

wafwerar's avatar
wafwerar 已提交
1094
      TdUcs4 accept[3] = {towupper(c), towlower(c), 0};
H
Hongze Cheng 已提交
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
      while (1) {
        size_t n = wcscspn(str, accept);

        str += n;
        if (str[0] == 0 || (n >= size)) {
          break;
        }

        int32_t ret = WCSPatternMatch(&patterStr[i], ++str, size - n - 1, pInfo);
        if (ret != TSDB_PATTERN_NOMATCH) {
          return ret;
        }
      }

      return TSDB_PATTERN_NOWILDCARDMATCH;
    }

    c1 = str[j++];

    if (j <= size) {
      if (c == c1 || towlower(c) == towlower(c1) || (c == matchOne && c1 != 0)) {
        continue;
      }
    }

    return TSDB_PATTERN_NOMATCH;
  }
S
Shengliang Guan 已提交
1122

D
dapan1121 已提交
1123
  return (j >= size || str[j] == 0) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH;
H
Hongze Cheng 已提交
1124 1125
}

S
Shengliang Guan 已提交
1126
int32_t compareStrRegexCompMatch(const void *pLeft, const void *pRight) { return compareStrRegexComp(pLeft, pRight); }
H
Hongze Cheng 已提交
1127

S
Shengliang Guan 已提交
1128
int32_t compareStrRegexCompNMatch(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
1129 1130 1131
  return compareStrRegexComp(pLeft, pRight) ? 0 : 1;
}

S
Shengliang Guan 已提交
1132
int32_t compareStrRegexComp(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
1133
  size_t sz = varDataLen(pRight);
wafwerar's avatar
wafwerar 已提交
1134
  char  *pattern = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
1135 1136 1137 1138
  memcpy(pattern, varDataVal(pRight), varDataLen(pRight));
  pattern[sz] = 0;

  sz = varDataLen(pLeft);
wafwerar's avatar
wafwerar 已提交
1139
  char *str = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
1140 1141 1142
  memcpy(str, varDataVal(pLeft), sz);
  str[sz] = 0;

1143
  int32_t errCode = 0;
H
Hongze Cheng 已提交
1144 1145 1146
  regex_t regex;
  char    msgbuf[256] = {0};

1147
  int32_t cflags = REG_EXTENDED;
H
Hongze Cheng 已提交
1148 1149 1150 1151
  if ((errCode = regcomp(&regex, pattern, cflags)) != 0) {
    regerror(errCode, &regex, msgbuf, sizeof(msgbuf));
    uError("Failed to compile regex pattern %s. reason %s", pattern, msgbuf);
    regfree(&regex);
wafwerar's avatar
wafwerar 已提交
1152 1153
    taosMemoryFree(str);
    taosMemoryFree(pattern);
H
Hongze Cheng 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
    return 1;
  }

  errCode = regexec(&regex, str, 0, NULL, 0);
  if (errCode != 0 && errCode != REG_NOMATCH) {
    regerror(errCode, &regex, msgbuf, sizeof(msgbuf));
    uDebug("Failed to match %s with pattern %s, reason %s", str, pattern, msgbuf)
  }
  int32_t result = (errCode == 0) ? 0 : 1;
  regfree(&regex);
wafwerar's avatar
wafwerar 已提交
1164 1165
  taosMemoryFree(str);
  taosMemoryFree(pattern);
H
Hongze Cheng 已提交
1166 1167 1168
  return result;
}

S
Shengliang Guan 已提交
1169 1170 1171
int32_t taosArrayCompareString(const void *a, const void *b) {
  const char *x = *(const char **)a;
  const char *y = *(const char **)b;
H
Hongze Cheng 已提交
1172 1173 1174

  return compareLenPrefixedStr(x, y);
}
D
dapan1121 已提交
1175

S
Shengliang Guan 已提交
1176
int32_t compareStrPatternMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
1177 1178 1179
  SPatternCompareInfo pInfo = {'%', '_'};

  assert(varDataLen(pRight) <= TSDB_MAX_FIELD_LEN);
wafwerar's avatar
wafwerar 已提交
1180
  char *pattern = taosMemoryCalloc(varDataLen(pRight) + 1, sizeof(char));
D
dapan1121 已提交
1181 1182 1183
  memcpy(pattern, varDataVal(pRight), varDataLen(pRight));

  size_t sz = varDataLen(pLeft);
wafwerar's avatar
wafwerar 已提交
1184
  char  *buf = taosMemoryMalloc(sz + 1);
D
dapan1121 已提交
1185 1186 1187 1188
  memcpy(buf, varDataVal(pLeft), sz);
  buf[sz] = 0;

  int32_t ret = patternMatch(pattern, buf, sz, &pInfo);
wafwerar's avatar
wafwerar 已提交
1189 1190
  taosMemoryFree(buf);
  taosMemoryFree(pattern);
D
dapan1121 已提交
1191 1192 1193
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
}

S
Shengliang Guan 已提交
1194
int32_t compareStrPatternNotMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
1195 1196 1197
  return compareStrPatternMatch(pLeft, pRight) ? 0 : 1;
}

S
Shengliang Guan 已提交
1198
int32_t compareWStrPatternMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
1199 1200 1201 1202
  SPatternCompareInfo pInfo = {'%', '_'};

  assert(varDataLen(pRight) <= TSDB_MAX_FIELD_LEN * TSDB_NCHAR_SIZE);

wafwerar's avatar
wafwerar 已提交
1203
  char *pattern = taosMemoryCalloc(varDataLen(pRight) + TSDB_NCHAR_SIZE, 1);
D
dapan1121 已提交
1204 1205
  memcpy(pattern, varDataVal(pRight), varDataLen(pRight));

H
Hongze Cheng 已提交
1206 1207
  int32_t ret =
      WCSPatternMatch((TdUcs4 *)pattern, (TdUcs4 *)varDataVal(pLeft), varDataLen(pLeft) / TSDB_NCHAR_SIZE, &pInfo);
wafwerar's avatar
wafwerar 已提交
1208
  taosMemoryFree(pattern);
D
dapan1121 已提交
1209 1210 1211 1212

  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
}

S
Shengliang Guan 已提交
1213
int32_t compareWStrPatternNotMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
1214 1215 1216 1217 1218
  return compareWStrPatternMatch(pLeft, pRight) ? 0 : 1;
}
__compar_fn_t getComparFunc(int32_t type, int32_t optr) {
  __compar_fn_t comparFn = NULL;

D
dapan1121 已提交
1219
  if (optr == OP_TYPE_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
D
dapan1121 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
    switch (type) {
      case TSDB_DATA_TYPE_BOOL:
      case TSDB_DATA_TYPE_TINYINT:
      case TSDB_DATA_TYPE_UTINYINT:
        return setChkInBytes1;
      case TSDB_DATA_TYPE_SMALLINT:
      case TSDB_DATA_TYPE_USMALLINT:
        return setChkInBytes2;
      case TSDB_DATA_TYPE_INT:
      case TSDB_DATA_TYPE_UINT:
      case TSDB_DATA_TYPE_FLOAT:
        return setChkInBytes4;
      case TSDB_DATA_TYPE_BIGINT:
      case TSDB_DATA_TYPE_UBIGINT:
      case TSDB_DATA_TYPE_DOUBLE:
      case TSDB_DATA_TYPE_TIMESTAMP:
        return setChkInBytes8;
      default:
        assert(0);
    }
  }

D
dapan1121 已提交
1242
  if (optr == OP_TYPE_NOT_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
D
dapan1121 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
    switch (type) {
      case TSDB_DATA_TYPE_BOOL:
      case TSDB_DATA_TYPE_TINYINT:
      case TSDB_DATA_TYPE_UTINYINT:
        return setChkNotInBytes1;
      case TSDB_DATA_TYPE_SMALLINT:
      case TSDB_DATA_TYPE_USMALLINT:
        return setChkNotInBytes2;
      case TSDB_DATA_TYPE_INT:
      case TSDB_DATA_TYPE_UINT:
      case TSDB_DATA_TYPE_FLOAT:
        return setChkNotInBytes4;
      case TSDB_DATA_TYPE_BIGINT:
      case TSDB_DATA_TYPE_UBIGINT:
      case TSDB_DATA_TYPE_DOUBLE:
      case TSDB_DATA_TYPE_TIMESTAMP:
        return setChkNotInBytes8;
      default:
        assert(0);
    }
  }

  switch (type) {
    case TSDB_DATA_TYPE_BOOL:
S
Shengliang Guan 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275
    case TSDB_DATA_TYPE_TINYINT:
      comparFn = compareInt8Val;
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      comparFn = compareInt16Val;
      break;
    case TSDB_DATA_TYPE_INT:
      comparFn = compareInt32Val;
      break;
D
dapan1121 已提交
1276
    case TSDB_DATA_TYPE_BIGINT:
S
Shengliang Guan 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285
    case TSDB_DATA_TYPE_TIMESTAMP:
      comparFn = compareInt64Val;
      break;
    case TSDB_DATA_TYPE_FLOAT:
      comparFn = compareFloatVal;
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      comparFn = compareDoubleVal;
      break;
D
dapan1121 已提交
1286
    case TSDB_DATA_TYPE_BINARY: {
D
dapan1121 已提交
1287
      if (optr == OP_TYPE_MATCH) {
D
dapan1121 已提交
1288
        comparFn = compareStrRegexCompMatch;
D
dapan1121 已提交
1289
      } else if (optr == OP_TYPE_NMATCH) {
D
dapan1121 已提交
1290
        comparFn = compareStrRegexCompNMatch;
D
dapan1121 已提交
1291
      } else if (optr == OP_TYPE_LIKE) { /* wildcard query using like operator */
D
dapan1121 已提交
1292
        comparFn = compareStrPatternMatch;
D
dapan1121 已提交
1293
      } else if (optr == OP_TYPE_NOT_LIKE) { /* wildcard query using like operator */
D
dapan1121 已提交
1294
        comparFn = compareStrPatternNotMatch;
D
dapan1121 已提交
1295
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
1296
        comparFn = compareChkInString;
D
dapan1121 已提交
1297
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306
        comparFn = compareChkNotInString;
      } else { /* normal relational comparFn */
        comparFn = compareLenPrefixedStr;
      }

      break;
    }

    case TSDB_DATA_TYPE_NCHAR: {
D
dapan1121 已提交
1307
      if (optr == OP_TYPE_MATCH) {
D
dapan1121 已提交
1308
        comparFn = compareStrRegexCompMatch;
D
dapan1121 已提交
1309
      } else if (optr == OP_TYPE_NMATCH) {
D
dapan1121 已提交
1310
        comparFn = compareStrRegexCompNMatch;
D
dapan1121 已提交
1311
      } else if (optr == OP_TYPE_LIKE) {
D
dapan1121 已提交
1312
        comparFn = compareWStrPatternMatch;
D
dapan1121 已提交
1313
      } else if (optr == OP_TYPE_NOT_LIKE) {
D
dapan1121 已提交
1314
        comparFn = compareWStrPatternNotMatch;
D
dapan1121 已提交
1315
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
1316
        comparFn = compareChkInString;
D
dapan1121 已提交
1317
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
1318 1319 1320 1321 1322 1323 1324
        comparFn = compareChkNotInString;
      } else {
        comparFn = compareLenPrefixedWStr;
      }
      break;
    }

S
Shengliang Guan 已提交
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    case TSDB_DATA_TYPE_UTINYINT:
      comparFn = compareUint8Val;
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      comparFn = compareUint16Val;
      break;
    case TSDB_DATA_TYPE_UINT:
      comparFn = compareUint32Val;
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      comparFn = compareUint64Val;
      break;
D
dapan1121 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345

    default:
      comparFn = compareInt32Val;
      break;
  }

  return comparFn;
}

S
compare  
Shengliang Guan 已提交
1346 1347 1348 1349
__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) {
  switch (keyType) {
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_BOOL:
1350
      return (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc;
S
compare  
Shengliang Guan 已提交
1351
    case TSDB_DATA_TYPE_SMALLINT:
1352
      return (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc;
S
compare  
Shengliang Guan 已提交
1353
    case TSDB_DATA_TYPE_INT:
1354
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
1355 1356
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
1357
      return (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc;
S
compare  
Shengliang Guan 已提交
1358
    case TSDB_DATA_TYPE_FLOAT:
1359
      return (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc;
S
compare  
Shengliang Guan 已提交
1360
    case TSDB_DATA_TYPE_DOUBLE:
1361
      return (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc;
S
compare  
Shengliang Guan 已提交
1362
    case TSDB_DATA_TYPE_UTINYINT:
1363
      return (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc;
S
compare  
Shengliang Guan 已提交
1364
    case TSDB_DATA_TYPE_USMALLINT:
1365
      return (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc;
S
compare  
Shengliang Guan 已提交
1366
    case TSDB_DATA_TYPE_UINT:
1367
      return (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc;
S
compare  
Shengliang Guan 已提交
1368
    case TSDB_DATA_TYPE_UBIGINT:
1369
      return (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc;
S
compare  
Shengliang Guan 已提交
1370
    case TSDB_DATA_TYPE_BINARY:
1371
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc;
S
compare  
Shengliang Guan 已提交
1372
    case TSDB_DATA_TYPE_NCHAR:
1373
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc;
wmmhello's avatar
wmmhello 已提交
1374 1375
    case TSDB_DATA_TYPE_JSON:
      return (order == TSDB_ORDER_ASC) ? compareJsonVal : compareJsonValDesc;
S
compare  
Shengliang Guan 已提交
1376
    default:
1377
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
1378 1379 1380
  }
}

S
Shengliang Guan 已提交
1381
int32_t doCompare(const char *f1, const char *f2, int32_t type, size_t size) {
S
compare  
Shengliang Guan 已提交
1382
  switch (type) {
S
Shengliang Guan 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
    case TSDB_DATA_TYPE_INT:
      DEFAULT_COMP(GET_INT32_VAL(f1), GET_INT32_VAL(f2));
    case TSDB_DATA_TYPE_DOUBLE:
      DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(f1), GET_DOUBLE_VAL(f2));
    case TSDB_DATA_TYPE_FLOAT:
      DEFAULT_FLOAT_COMP(GET_FLOAT_VAL(f1), GET_FLOAT_VAL(f2));
    case TSDB_DATA_TYPE_BIGINT:
      DEFAULT_COMP(GET_INT64_VAL(f1), GET_INT64_VAL(f2));
    case TSDB_DATA_TYPE_SMALLINT:
      DEFAULT_COMP(GET_INT16_VAL(f1), GET_INT16_VAL(f2));
S
compare  
Shengliang Guan 已提交
1393
    case TSDB_DATA_TYPE_TINYINT:
S
Shengliang Guan 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
    case TSDB_DATA_TYPE_BOOL:
      DEFAULT_COMP(GET_INT8_VAL(f1), GET_INT8_VAL(f2));
    case TSDB_DATA_TYPE_UTINYINT:
      DEFAULT_COMP(GET_UINT8_VAL(f1), GET_UINT8_VAL(f2));
    case TSDB_DATA_TYPE_USMALLINT:
      DEFAULT_COMP(GET_UINT16_VAL(f1), GET_UINT16_VAL(f2));
    case TSDB_DATA_TYPE_UINT:
      DEFAULT_COMP(GET_UINT32_VAL(f1), GET_UINT32_VAL(f2));
    case TSDB_DATA_TYPE_UBIGINT:
      DEFAULT_COMP(GET_UINT64_VAL(f1), GET_UINT64_VAL(f2));
S
compare  
Shengliang Guan 已提交
1404
    case TSDB_DATA_TYPE_NCHAR: {
S
Shengliang Guan 已提交
1405 1406
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
1407 1408

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
1409
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
1410
      }
wafwerar's avatar
wafwerar 已提交
1411
      int32_t ret = memcmp((TdUcs4 *)t1, (TdUcs4 *)t2, t2->len);
S
compare  
Shengliang Guan 已提交
1412 1413 1414 1415 1416 1417
      if (ret == 0) {
        return ret;
      }
      return (ret < 0) ? -1 : 1;
    }
    default: {  // todo refactor
S
Shengliang Guan 已提交
1418 1419
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
1420 1421

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
1422
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
1423 1424 1425 1426 1427
      } else {
        int32_t ret = strncmp(t1->data, t2->data, t1->len);
        if (ret == 0) {
          return 0;
        } else {
S
Shengliang Guan 已提交
1428
          return ret < 0 ? -1 : 1;
S
compare  
Shengliang Guan 已提交
1429 1430 1431 1432 1433
        }
      }
    }
  }
}