tcompare.c 43.2 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"
X
Xiaoyu Wang 已提交
24
#include "tutil.h"
S
Shengliang Guan 已提交
25
#include "types.h"
26
#include "osString.h"
H
Hongze Cheng 已提交
27

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

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

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

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

D
dapan1121 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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 已提交
60
int32_t compareChkInString(const void *pLeft, const void *pRight) {
61
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, varDataTLen(pLeft)) ? 1 : 0;
D
dapan1121 已提交
62 63
}

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

H
Hongze Cheng 已提交
68 69 70 71 72 73 74
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 已提交
75
int32_t compareInt8ValDesc(const void *pLeft, const void *pRight) { return compareInt8Val(pRight, pLeft); }
H
Hongze Cheng 已提交
76 77 78 79 80 81 82 83

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 已提交
84
int32_t compareInt16ValDesc(const void *pLeft, const void *pRight) { return compareInt16Val(pRight, pLeft); }
H
Hongze Cheng 已提交
85 86 87 88 89 90 91 92

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 已提交
93
int32_t compareInt32ValDesc(const void *pLeft, const void *pRight) { return compareInt32Val(pRight, pLeft); }
H
Hongze Cheng 已提交
94 95 96 97 98 99 100 101

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 已提交
102
int32_t compareInt64ValDesc(const void *pLeft, const void *pRight) { return compareInt64Val(pRight, pLeft); }
H
Hongze Cheng 已提交
103 104 105 106 107 108 109 110

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 已提交
111
int32_t compareUint32ValDesc(const void *pLeft, const void *pRight) { return compareUint32Val(pRight, pLeft); }
H
Hongze Cheng 已提交
112 113 114 115 116 117 118 119

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 已提交
120
int32_t compareUint64ValDesc(const void *pLeft, const void *pRight) { return compareUint64Val(pRight, pLeft); }
H
Hongze Cheng 已提交
121 122 123 124 125 126 127 128

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 已提交
129
int32_t compareUint16ValDesc(const void *pLeft, const void *pRight) { return compareUint16Val(pRight, pLeft); }
H
Hongze Cheng 已提交
130

S
Shengliang Guan 已提交
131
int32_t compareUint8Val(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
132 133 134 135 136 137
  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 已提交
138
int32_t compareUint8ValDesc(const void *pLeft, const void *pRight) { return compareUint8Val(pRight, pLeft); }
H
Hongze Cheng 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

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 已提交
158
  return FLT_GREATER(p1, p2) ? 1 : -1;
H
Hongze Cheng 已提交
159 160
}

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

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 已提交
178

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

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

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

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

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

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

A
Alex Duan 已提交
212
  int32_t ret = tasoUcs4Compare((TdUcs4 *)varDataVal(pLeft), (TdUcs4 *)varDataVal(pRight), len1>len2 ? len2:len1);
213 214 215 216 217 218
  if (ret == 0) {
    if (len1 > len2)
      return 1;
    else if(len1 < len2)
      return -1;
    else
H
Hongze Cheng 已提交
219 220
      return 0;
  }
221
  return (ret < 0) ? -1 : 1;
H
Hongze Cheng 已提交
222 223
}

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

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
int32_t compareLenBinaryVal(const void *pLeft, const void *pRight) {
  int32_t len1 = varDataLen(pLeft);
  int32_t len2 = varDataLen(pRight);

  int32_t minLen = TMIN(len1, len2);
  int32_t ret = memcmp(varDataVal(pLeft), varDataVal(pRight), minLen);
  if (ret == 0) {
    if (len1 == len2) {
      return 0;
    } else {
      return len1 > len2 ? 1 : -1;
    }
  } else {
    return ret > 0 ? 1 : -1;
  }
}

wmmhello's avatar
wmmhello 已提交
245 246 247
// 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 已提交
248 249 250
  char leftType = *(char *)pLeft;
  char rightType = *(char *)pRight;
  if (leftType != rightType) {
wmmhello's avatar
wmmhello 已提交
251 252 253
    return leftType > rightType ? 1 : -1;
  }

H
Hongze Cheng 已提交
254 255 256
  char *realDataLeft = POINTER_SHIFT(pLeft, CHAR_BYTES);
  char *realDataRight = POINTER_SHIFT(pRight, CHAR_BYTES);
  if (leftType == TSDB_DATA_TYPE_BOOL) {
wmmhello's avatar
wmmhello 已提交
257
    DEFAULT_COMP(GET_INT8_VAL(realDataLeft), GET_INT8_VAL(realDataRight));
H
Hongze Cheng 已提交
258
  } else if (leftType == TSDB_DATA_TYPE_DOUBLE) {
wmmhello's avatar
wmmhello 已提交
259
    DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(realDataLeft), GET_DOUBLE_VAL(realDataRight));
H
Hongze Cheng 已提交
260
  } else if (leftType == TSDB_DATA_TYPE_NCHAR) {
wmmhello's avatar
wmmhello 已提交
261
    return compareLenPrefixedWStr(realDataLeft, realDataRight);
H
Hongze Cheng 已提交
262
  } else if (leftType == TSDB_DATA_TYPE_NULL) {
wmmhello's avatar
wmmhello 已提交
263
    return 0;
H
Hongze Cheng 已提交
264
  } else {
A
Alex Duan 已提交
265
    ASSERTS(0, "data type unexpected");
266
    return 0;
wmmhello's avatar
wmmhello 已提交
267 268 269
  }
}

D
dapan1121 已提交
270
int32_t compareInt8Int16(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
271
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
272 273 274 275 276 277 278
  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 已提交
279
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
280 281 282 283 284 285 286
  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 已提交
287
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
288 289 290 291 292 293 294
  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 已提交
295
  int8_t left = GET_INT8_VAL(pLeft);
H
Hongze Cheng 已提交
296
  float  right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
297 298 299 300 301 302
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt8Double(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
303
  int8_t left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
304 305 306 307 308 309 310
  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 已提交
311
  int8_t  left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
312 313 314 315 316 317 318
  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 已提交
319
  int8_t   left = GET_INT8_VAL(pLeft);
D
dapan1121 已提交
320 321 322 323 324 325 326
  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 已提交
327
  int8_t   left = GET_INT8_VAL(pLeft);
328
  if (left < 0) return -1;
D
dapan1121 已提交
329
  uint32_t right = GET_UINT32_VAL(pRight);
330 331
  if ((uint32_t)left > right) return 1;
  if ((uint32_t)left < right) return -1;
D
dapan1121 已提交
332 333 334 335
  return 0;
}

int32_t compareInt8Uint64(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
336
  int8_t   left = GET_INT8_VAL(pLeft);
337
  if (left < 0) return -1;
D
dapan1121 已提交
338
  uint64_t right = GET_UINT64_VAL(pRight);
339 340
  if ((uint64_t)left > right) return 1;
  if ((uint64_t)left < right) return -1;
D
dapan1121 已提交
341 342 343 344
  return 0;
}

int32_t compareInt16Int8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
345
  int16_t left = GET_INT16_VAL(pLeft);
H
Hongze Cheng 已提交
346
  int8_t  right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
347 348 349 350 351 352
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Int32(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
353
  int16_t left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
354 355 356 357 358 359 360
  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 已提交
361
  int16_t left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
362 363 364 365 366 367 368
  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 已提交
369
  int16_t left = GET_INT16_VAL(pLeft);
H
Hongze Cheng 已提交
370
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
371 372 373 374 375 376
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Double(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
377
  int16_t left = GET_INT16_VAL(pLeft);
H
Hongze Cheng 已提交
378
  double  right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
379 380 381 382 383 384
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareInt16Uint8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
385
  int16_t left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
386 387 388 389 390 391 392
  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 已提交
393
  int16_t  left = GET_INT16_VAL(pLeft);
D
dapan1121 已提交
394 395 396 397 398 399 400
  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 已提交
401
  int16_t  left = GET_INT16_VAL(pLeft);
402
  if (left < 0) return -1;
D
dapan1121 已提交
403
  uint32_t right = GET_UINT32_VAL(pRight);
404 405
  if ((uint32_t)left > right) return 1;
  if ((uint32_t)left < right) return -1;
D
dapan1121 已提交
406 407 408 409
  return 0;
}

int32_t compareInt16Uint64(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
410
  int16_t  left = GET_INT16_VAL(pLeft);
411
  if (left < 0) return -1;
D
dapan1121 已提交
412
  uint64_t right = GET_UINT64_VAL(pRight);
413 414
  if ((uint64_t)left > right) return 1;
  if ((uint64_t)left < right) return -1;
D
dapan1121 已提交
415 416 417 418 419
  return 0;
}

int32_t compareInt32Int8(const void *pLeft, const void *pRight) {
  int32_t left = GET_INT32_VAL(pLeft);
H
Hongze Cheng 已提交
420
  int8_t  right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
421 422 423 424 425 426 427 428 429 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 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 已提交
444
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
445 446 447 448 449 450 451
  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 已提交
452
  double  right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466
  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 已提交
467
  int32_t  left = GET_INT32_VAL(pLeft);
D
dapan1121 已提交
468 469 470 471 472 473 474
  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 已提交
475
  int32_t  left = GET_INT32_VAL(pLeft);
476
  if (left < 0) return -1;
D
dapan1121 已提交
477
  uint32_t right = GET_UINT32_VAL(pRight);
478 479
  if ((uint32_t)left > right) return 1;
  if ((uint32_t)left < right) return -1;
D
dapan1121 已提交
480 481 482 483
  return 0;
}

int32_t compareInt32Uint64(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
484
  int32_t  left = GET_INT32_VAL(pLeft);
485
  if (left < 0) return -1;
D
dapan1121 已提交
486
  uint64_t right = GET_UINT64_VAL(pRight);
487 488
  if ((uint64_t)left > right) return 1;
  if ((uint64_t)left < right) return -1;
D
dapan1121 已提交
489 490 491 492
  return 0;
}

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

int32_t compareInt64Int16(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
501
  int64_t left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
502 503 504 505 506 507 508
  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 已提交
509
  int64_t left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
510 511 512 513 514 515 516
  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 已提交
517
  int64_t left = GET_INT64_VAL(pLeft);
H
Hongze Cheng 已提交
518
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
519 520 521 522 523 524
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

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

int32_t compareInt64Uint8(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
533
  int64_t left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
534 535 536 537 538 539 540
  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 已提交
541
  int64_t  left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
542 543 544 545 546 547 548
  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 已提交
549
  int64_t  left = GET_INT64_VAL(pLeft);
D
dapan1121 已提交
550 551 552 553 554 555 556
  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 已提交
557
  int64_t  left = GET_INT64_VAL(pLeft);
558
  if (left < 0) return -1;
D
dapan1121 已提交
559
  uint64_t right = GET_UINT64_VAL(pRight);
560 561
  if ((uint64_t)left > right) return 1;
  if ((uint64_t)left < right) return -1;
D
dapan1121 已提交
562 563 564 565
  return 0;
}

int32_t compareFloatInt8(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
566
  float  left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
567 568 569 570 571 572 573
  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 已提交
574
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
575 576 577 578 579 580 581
  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 已提交
582
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
583 584 585 586 587 588 589
  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 已提交
590
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
591 592 593 594 595 596 597
  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 已提交
598
  float  left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
599
  double right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

  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 已提交
617 618 619
}

int32_t compareFloatUint8(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
620
  float   left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
621 622 623 624 625 626 627
  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 已提交
628
  float    left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
629 630 631 632 633 634 635
  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 已提交
636
  float    left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
637 638 639 640 641 642 643
  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 已提交
644
  float    left = GET_FLOAT_VAL(pLeft);
D
dapan1121 已提交
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  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 已提交
660
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
661 662 663 664 665 666 667
  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 已提交
668
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
669 670 671 672 673 674 675
  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 已提交
676
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
677 678 679 680 681 682 683 684
  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 已提交
685
  float  right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702

  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 已提交
703 704 705
}

int32_t compareDoubleUint8(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
706
  double  left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
707 708 709 710 711 712 713
  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 已提交
714
  double   left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
715 716 717 718 719 720 721
  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 已提交
722
  double   left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
723 724 725 726 727 728 729
  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 已提交
730
  double   left = GET_DOUBLE_VAL(pLeft);
D
dapan1121 已提交
731 732 733 734 735 736 737 738
  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 已提交
739
  int8_t  right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
  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 已提交
771
  float   right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
772 773 774 775 776 777 778
  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 已提交
779
  double  right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
780 781 782 783 784 785
  if (left > right) return 1;
  if (left < right) return -1;
  return 0;
}

int32_t compareUint8Uint16(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
786
  uint8_t  left = GET_UINT8_VAL(pLeft);
D
dapan1121 已提交
787 788 789 790 791 792 793
  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 已提交
794
  uint8_t  left = GET_UINT8_VAL(pLeft);
D
dapan1121 已提交
795 796 797 798 799 800 801
  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 已提交
802
  uint8_t  left = GET_UINT8_VAL(pLeft);
D
dapan1121 已提交
803 804 805 806 807 808 809 810
  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 已提交
811
  int8_t   right = GET_INT8_VAL(pRight);
D
dapan1121 已提交
812 813 814 815 816 817 818
  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 已提交
819
  int16_t  right = GET_INT16_VAL(pRight);
D
dapan1121 已提交
820 821 822 823 824 825 826
  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 已提交
827
  int32_t  right = GET_INT32_VAL(pRight);
D
dapan1121 已提交
828 829 830 831 832 833 834
  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 已提交
835
  int64_t  right = GET_INT64_VAL(pRight);
D
dapan1121 已提交
836 837 838 839 840 841 842
  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 已提交
843
  float    right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
844 845 846 847 848 849 850
  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 已提交
851
  double   right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
852 853 854 855 856 857 858
  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 已提交
859
  uint8_t  right = GET_UINT8_VAL(pRight);
D
dapan1121 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
  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 已提交
883
  int8_t   right = GET_INT8_VAL(pRight);
884 885 886
  if (right < 0) return 1;
  if (left > (uint32_t)right) return 1;
  if (left < (uint32_t)right) return -1;
D
dapan1121 已提交
887 888 889 890 891
  return 0;
}

int32_t compareUint32Int16(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
892
  int16_t  right = GET_INT16_VAL(pRight);
893 894 895
  if (right < 0) return 1;
  if (left > (uint32_t)right) return 1;
  if (left < (uint32_t)right) return -1;
D
dapan1121 已提交
896 897 898 899 900
  return 0;
}

int32_t compareUint32Int32(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
901
  int32_t  right = GET_INT32_VAL(pRight);
902 903 904
  if (right < 0) return 1;
  if (left > (uint32_t)right) return 1;
  if (left < (uint32_t)right) return -1;
D
dapan1121 已提交
905 906 907 908 909
  return 0;
}

int32_t compareUint32Int64(const void *pLeft, const void *pRight) {
  uint32_t left = GET_UINT32_VAL(pLeft);
H
Hongze Cheng 已提交
910
  int64_t  right = GET_INT64_VAL(pRight);
D
dapan1121 已提交
911 912 913 914 915 916 917
  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 已提交
918
  float    right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
919 920 921 922 923 924 925
  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 已提交
926
  double   right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
927 928 929 930 931 932 933
  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 已提交
934
  uint8_t  right = GET_UINT8_VAL(pRight);
D
dapan1121 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
  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 已提交
958
  int8_t   right = GET_INT8_VAL(pRight);
959 960 961
  if (right < 0) return 1;
  if (left > (uint64_t)right) return 1;
  if (left < (uint64_t)right) return -1;
D
dapan1121 已提交
962 963 964 965 966
  return 0;
}

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

int32_t compareUint64Int32(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
976
  int32_t  right = GET_INT32_VAL(pRight);
977 978 979
  if (right < 0) return 1;
  if (left > (uint64_t)right) return 1;
  if (left < (uint64_t)right) return -1;
D
dapan1121 已提交
980 981 982 983 984
  return 0;
}

int32_t compareUint64Int64(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
985
  int64_t  right = GET_INT64_VAL(pRight);
986 987 988
  if (right < 0) return 1;
  if (left > (uint64_t)right) return 1;
  if (left < (uint64_t)right) return -1;
D
dapan1121 已提交
989 990 991 992 993
  return 0;
}

int32_t compareUint64Float(const void *pLeft, const void *pRight) {
  uint64_t left = GET_UINT64_VAL(pLeft);
H
Hongze Cheng 已提交
994
  float    right = GET_FLOAT_VAL(pRight);
D
dapan1121 已提交
995 996 997 998 999 1000 1001
  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 已提交
1002
  double   right = GET_DOUBLE_VAL(pRight);
D
dapan1121 已提交
1003 1004 1005 1006 1007 1008 1009
  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 已提交
1010
  uint8_t  right = GET_UINT8_VAL(pRight);
D
dapan1121 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  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 已提交
1032
int32_t compareJsonValDesc(const void *pLeft, const void *pRight) { return compareJsonVal(pRight, pLeft); }
H
Haojun Liao 已提交
1033

H
Hongze Cheng 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
/*
 * 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
 *
 */
X
Xiaoyu Wang 已提交
1044 1045
int32_t patternMatch(const char *pattern, size_t psize, const char *str, size_t ssize,
                     const SPatternCompareInfo *pInfo) {
H
Hongze Cheng 已提交
1046 1047 1048 1049
  char c, c1;

  int32_t i = 0;
  int32_t j = 0;
H
Haojun Liao 已提交
1050
  int32_t nMatchChar = 0;
H
Hongze Cheng 已提交
1051

H
Haojun Liao 已提交
1052
  while ((i < psize) && ((c = pattern[i++]) != 0)) {
H
Hongze Cheng 已提交
1053 1054
    if (c == pInfo->matchAll) { /* Match "*" */

X
Xiaoyu Wang 已提交
1055
      while ((i < psize) && ((c = pattern[i++]) == pInfo->matchAll || c == pInfo->matchOne)) {
H
Hongze Cheng 已提交
1056
        if (c == pInfo->matchOne) {
X
Xiaoyu Wang 已提交
1057
          if (j >= ssize || str[j++] == 0) {  // empty string, return not match
H
Hongze Cheng 已提交
1058 1059
            return TSDB_PATTERN_NOWILDCARDMATCH;
          } else {
H
Haojun Liao 已提交
1060
            ++nMatchChar;
H
Hongze Cheng 已提交
1061 1062 1063 1064
          }
        }
      }

H
Haojun Liao 已提交
1065
      if (i >= psize && (c == pInfo->umatchOne || c == pInfo->umatchAll)) {
H
Hongze Cheng 已提交
1066 1067 1068
        return TSDB_PATTERN_MATCH; /* "*" at the end of the pattern matches */
      }

H
Haojun Liao 已提交
1069
      char rejectList[2] = {toupper(c), tolower(c)};
H
Haojun Liao 已提交
1070 1071 1072

      str += nMatchChar;
      int32_t remain = ssize - nMatchChar;
H
Hongze Cheng 已提交
1073
      while (1) {
H
Haojun Liao 已提交
1074
        size_t n = tstrncspn(str, remain, rejectList, 2);
H
Hongze Cheng 已提交
1075

H
Haojun Liao 已提交
1076 1077
        str += n;
        remain -= n;
H
Hongze Cheng 已提交
1078

H
Haojun Liao 已提交
1079
        if ((remain <= 0) || str[0] == 0) {
H
Hongze Cheng 已提交
1080 1081 1082
          break;
        }

H
Haojun Liao 已提交
1083
        int32_t ret = patternMatch(&pattern[i], psize - i, ++str, --remain, pInfo);
H
Hongze Cheng 已提交
1084 1085 1086 1087
        if (ret != TSDB_PATTERN_NOMATCH) {
          return ret;
        }
      }
H
Haojun Liao 已提交
1088

H
Hongze Cheng 已提交
1089 1090 1091
      return TSDB_PATTERN_NOWILDCARDMATCH;
    }

H
Haojun Liao 已提交
1092 1093 1094
    if (j < ssize) {
      c1 = str[j++];
      ++nMatchChar;
S
Shengliang Guan 已提交
1095

H
Haojun Liao 已提交
1096
      if (c == '\\' && pattern[i] == '_' && c1 == '_') {
S
Shengliang Guan 已提交
1097 1098 1099
        i++;
        continue;
      }
H
Haojun Liao 已提交
1100

H
Hongze Cheng 已提交
1101 1102 1103 1104 1105 1106 1107 1108
      if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) {
        continue;
      }
    }

    return TSDB_PATTERN_NOMATCH;
  }

H
Haojun Liao 已提交
1109
  return (j >= ssize || str[j] == 0) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH;
H
Hongze Cheng 已提交
1110 1111
}

X
Xiaoyu Wang 已提交
1112 1113
int32_t wcsPatternMatch(const TdUcs4 *pattern, size_t psize, const TdUcs4 *str, size_t ssize,
                        const SPatternCompareInfo *pInfo) {
wafwerar's avatar
wafwerar 已提交
1114
  TdUcs4 c, c1;
H
Hongze Cheng 已提交
1115 1116 1117

  int32_t i = 0;
  int32_t j = 0;
H
Haojun Liao 已提交
1118
  int32_t nMatchChar = 0;
H
Hongze Cheng 已提交
1119

H
Haojun Liao 已提交
1120 1121
  while ((i < psize) && ((c = pattern[i++]) != 0)) {
    if (c == pInfo->umatchAll) { /* Match "%" */
H
Hongze Cheng 已提交
1122

H
Haojun Liao 已提交
1123
      while ((i < psize) && ((c = pattern[i++]) == pInfo->umatchAll || c == pInfo->umatchOne)) {
H
Haojun Liao 已提交
1124 1125 1126 1127 1128 1129
        if (c == pInfo->umatchOne) {
          if (j >= ssize || str[j++] == 0) {
            return TSDB_PATTERN_NOWILDCARDMATCH;
          } else {
            ++nMatchChar;
          }
H
Hongze Cheng 已提交
1130 1131
        }
      }
H
Haojun Liao 已提交
1132

H
Haojun Liao 已提交
1133
      if (i >= psize && (c == pInfo->umatchOne || c == pInfo->umatchAll)) {
H
Hongze Cheng 已提交
1134 1135 1136
        return TSDB_PATTERN_MATCH;
      }

H
Haojun Liao 已提交
1137
      TdUcs4 rejectList[2] = {towupper(c), towlower(c)};
H
Haojun Liao 已提交
1138 1139 1140

      str += nMatchChar;
      int32_t remain = ssize - nMatchChar;
H
Hongze Cheng 已提交
1141
      while (1) {
H
Haojun Liao 已提交
1142
        size_t n = twcsncspn(str, remain, rejectList, 2);
H
Hongze Cheng 已提交
1143 1144

        str += n;
H
Haojun Liao 已提交
1145 1146
        remain -= n;

H
Haojun Liao 已提交
1147
        if ((remain <= 0) || str[0] == 0) {
H
Hongze Cheng 已提交
1148 1149 1150
          break;
        }

H
Haojun Liao 已提交
1151
        int32_t ret = wcsPatternMatch(&pattern[i], psize - i, ++str, --remain, pInfo);
H
Hongze Cheng 已提交
1152 1153 1154 1155 1156 1157 1158 1159
        if (ret != TSDB_PATTERN_NOMATCH) {
          return ret;
        }
      }

      return TSDB_PATTERN_NOWILDCARDMATCH;
    }

H
Haojun Liao 已提交
1160 1161 1162
    if (j < ssize) {
      c1 = str[j++];
      nMatchChar++;
H
Hongze Cheng 已提交
1163

H
Haojun Liao 已提交
1164 1165 1166 1167
      if (c == L'\\' && pattern[i] == L'_' && c1 == L'_') {
        i++;
        continue;
      }
H
Hongze Cheng 已提交
1168

H
Haojun Liao 已提交
1169
      if (c == c1 || towlower(c) == towlower(c1) || (c == pInfo->umatchOne && c1 != 0)) {
H
Hongze Cheng 已提交
1170 1171 1172 1173 1174 1175
        continue;
      }
    }

    return TSDB_PATTERN_NOMATCH;
  }
S
Shengliang Guan 已提交
1176

H
Haojun Liao 已提交
1177
  return (j >= ssize || str[j] == 0) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH;
H
Hongze Cheng 已提交
1178 1179
}

H
Haojun Liao 已提交
1180 1181
int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight) {
  return comparestrRegexMatch(pLeft, pRight) ? 0 : 1;
H
Hongze Cheng 已提交
1182 1183
}

H
Haojun Liao 已提交
1184 1185 1186 1187 1188 1189 1190 1191
static int32_t doExecRegexMatch(const char *pString, const char *pPattern) {
  int32_t ret = 0;
  regex_t regex;
  char    msgbuf[256] = {0};

  int32_t cflags = REG_EXTENDED;
  if ((ret = regcomp(&regex, pPattern, cflags)) != 0) {
    regerror(ret, &regex, msgbuf, tListLen(msgbuf));
H
Hongze Cheng 已提交
1192

H
Haojun Liao 已提交
1193 1194 1195 1196 1197
    uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf);
    regfree(&regex);
    return 1;
  }

1198 1199
  regmatch_t pmatch[1];
  ret = regexec(&regex, pString, 1, pmatch, 0);
H
Haojun Liao 已提交
1200 1201 1202 1203
  if (ret != 0 && ret != REG_NOMATCH) {
    regerror(ret, &regex, msgbuf, sizeof(msgbuf));
    uDebug("Failed to match %s with pattern %s, reason %s", pString, pPattern, msgbuf)
  }
H
Hongze Cheng 已提交
1204

H
Haojun Liao 已提交
1205 1206
  regfree(&regex);
  return (ret == 0) ? 0 : 1;
H
Hongze Cheng 已提交
1207 1208
}

H
Haojun Liao 已提交
1209
int32_t comparestrRegexMatch(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
1210
  size_t sz = varDataLen(pRight);
wafwerar's avatar
wafwerar 已提交
1211
  char  *pattern = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
1212 1213 1214 1215
  memcpy(pattern, varDataVal(pRight), varDataLen(pRight));
  pattern[sz] = 0;

  sz = varDataLen(pLeft);
wafwerar's avatar
wafwerar 已提交
1216
  char *str = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
1217 1218 1219
  memcpy(str, varDataVal(pLeft), sz);
  str[sz] = 0;

H
Haojun Liao 已提交
1220
  int32_t ret = doExecRegexMatch(str, pattern);
H
Hongze Cheng 已提交
1221

H
Haojun Liao 已提交
1222 1223 1224
  taosMemoryFree(str);
  taosMemoryFree(pattern);

X
Xiaoyu Wang 已提交
1225 1226
  return (ret == 0) ? 0 : 1;
  ;
H
Haojun Liao 已提交
1227 1228
}

X
Xiaoyu Wang 已提交
1229
int32_t comparewcsRegexMatch(const void *pString, const void *pPattern) {
H
Haojun Liao 已提交
1230
  size_t len = varDataLen(pPattern);
1231
  char  *pattern = taosMemoryMalloc(len + 1);
H
Haojun Liao 已提交
1232 1233 1234

  int convertLen = taosUcs4ToMbs((TdUcs4 *)varDataVal(pPattern), len, pattern);
  if (convertLen < 0) {
wafwerar's avatar
wafwerar 已提交
1235
    taosMemoryFree(pattern);
H
Haojun Liao 已提交
1236
    return TSDB_CODE_APP_ERROR;
H
Hongze Cheng 已提交
1237 1238
  }

H
Haojun Liao 已提交
1239
  pattern[convertLen] = 0;
H
Haojun Liao 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248

  len = varDataLen(pString);
  char *str = taosMemoryMalloc(len + 1);
  convertLen = taosUcs4ToMbs((TdUcs4 *)varDataVal(pString), len, str);
  if (convertLen < 0) {
    taosMemoryFree(str);
    taosMemoryFree(pattern);

    return TSDB_CODE_APP_ERROR;
H
Hongze Cheng 已提交
1249
  }
H
Haojun Liao 已提交
1250

H
Haojun Liao 已提交
1251
  str[convertLen] = 0;
H
Haojun Liao 已提交
1252 1253 1254

  int32_t ret = doExecRegexMatch(str, pattern);

wafwerar's avatar
wafwerar 已提交
1255 1256
  taosMemoryFree(str);
  taosMemoryFree(pattern);
H
Haojun Liao 已提交
1257 1258 1259 1260 1261 1262

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

int32_t comparewcsRegexNMatch(const void *pLeft, const void *pRight) {
  return comparewcsRegexMatch(pLeft, pRight) ? 0 : 1;
H
Hongze Cheng 已提交
1263 1264
}

S
Shengliang Guan 已提交
1265 1266 1267
int32_t taosArrayCompareString(const void *a, const void *b) {
  const char *x = *(const char **)a;
  const char *y = *(const char **)b;
H
Hongze Cheng 已提交
1268

1269
  return strcmp(x, y);
H
Hongze Cheng 已提交
1270
}
D
dapan1121 已提交
1271

H
Haojun Liao 已提交
1272 1273
int32_t comparestrPatternMatch(const void *pLeft, const void *pRight) {
  SPatternCompareInfo pInfo = PATTERN_COMPARE_INFO_INITIALIZER;
D
dapan1121 已提交
1274

1275
  ASSERT(varDataTLen(pRight) <= TSDB_MAX_FIELD_LEN);
H
Haojun Liao 已提交
1276
  size_t pLen = varDataLen(pRight);
D
dapan1121 已提交
1277 1278
  size_t sz = varDataLen(pLeft);

H
Haojun Liao 已提交
1279
  int32_t ret = patternMatch(varDataVal(pRight), pLen, varDataVal(pLeft), sz, &pInfo);
D
dapan1121 已提交
1280 1281 1282
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
}

H
Haojun Liao 已提交
1283 1284
int32_t comparestrPatternNMatch(const void *pLeft, const void *pRight) {
  return comparestrPatternMatch(pLeft, pRight) ? 0 : 1;
D
dapan1121 已提交
1285 1286
}

H
Haojun Liao 已提交
1287 1288
int32_t comparewcsPatternMatch(const void *pLeft, const void *pRight) {
  SPatternCompareInfo pInfo = PATTERN_COMPARE_INFO_INITIALIZER;
D
dapan1121 已提交
1289

H
Haojun Liao 已提交
1290
  size_t psize = varDataLen(pRight);
D
dapan1121 已提交
1291

H
Haojun Liao 已提交
1292 1293
  int32_t ret = wcsPatternMatch((TdUcs4 *)varDataVal(pRight), psize / TSDB_NCHAR_SIZE, (TdUcs4 *)varDataVal(pLeft),
                                varDataLen(pLeft) / TSDB_NCHAR_SIZE, &pInfo);
D
dapan1121 已提交
1294 1295 1296
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
}

H
Haojun Liao 已提交
1297 1298
int32_t comparewcsPatternNMatch(const void *pLeft, const void *pRight) {
  return comparewcsPatternMatch(pLeft, pRight) ? 0 : 1;
D
dapan1121 已提交
1299
}
H
Haojun Liao 已提交
1300

D
dapan1121 已提交
1301 1302 1303
__compar_fn_t getComparFunc(int32_t type, int32_t optr) {
  __compar_fn_t comparFn = NULL;

D
Dingle Zhang 已提交
1304
  if (optr == OP_TYPE_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR && type != TSDB_DATA_TYPE_GEOMETRY)) {
D
dapan1121 已提交
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    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:
A
Alex Duan 已提交
1323
        ASSERTS(0, "data type unexpected");
D
dapan1121 已提交
1324 1325 1326
    }
  }

D
Dingle Zhang 已提交
1327
  if (optr == OP_TYPE_NOT_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR && type != TSDB_DATA_TYPE_GEOMETRY)) {
D
dapan1121 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
    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:
A
Alex Duan 已提交
1346
        ASSERTS(0, "data type unexpected");
D
dapan1121 已提交
1347 1348 1349 1350 1351
    }
  }

  switch (type) {
    case TSDB_DATA_TYPE_BOOL:
S
Shengliang Guan 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360
    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 已提交
1361
    case TSDB_DATA_TYPE_BIGINT:
S
Shengliang Guan 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370
    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
Dingle Zhang 已提交
1371 1372
    case TSDB_DATA_TYPE_BINARY:
    case TSDB_DATA_TYPE_GEOMETRY: {
D
dapan1121 已提交
1373
      if (optr == OP_TYPE_MATCH) {
H
Haojun Liao 已提交
1374
        comparFn = comparestrRegexMatch;
D
dapan1121 已提交
1375
      } else if (optr == OP_TYPE_NMATCH) {
H
Haojun Liao 已提交
1376
        comparFn = comparestrRegexNMatch;
D
dapan1121 已提交
1377
      } else if (optr == OP_TYPE_LIKE) { /* wildcard query using like operator */
H
Haojun Liao 已提交
1378
        comparFn = comparestrPatternMatch;
D
dapan1121 已提交
1379
      } else if (optr == OP_TYPE_NOT_LIKE) { /* wildcard query using like operator */
H
Haojun Liao 已提交
1380
        comparFn = comparestrPatternNMatch;
D
dapan1121 已提交
1381
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
1382
        comparFn = compareChkInString;
D
dapan1121 已提交
1383
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392
        comparFn = compareChkNotInString;
      } else { /* normal relational comparFn */
        comparFn = compareLenPrefixedStr;
      }

      break;
    }

    case TSDB_DATA_TYPE_NCHAR: {
D
dapan1121 已提交
1393
      if (optr == OP_TYPE_MATCH) {
H
Haojun Liao 已提交
1394
        comparFn = comparewcsRegexMatch;
D
dapan1121 已提交
1395
      } else if (optr == OP_TYPE_NMATCH) {
H
Haojun Liao 已提交
1396
        comparFn = comparewcsRegexNMatch;
D
dapan1121 已提交
1397
      } else if (optr == OP_TYPE_LIKE) {
H
Haojun Liao 已提交
1398
        comparFn = comparewcsPatternMatch;
D
dapan1121 已提交
1399
      } else if (optr == OP_TYPE_NOT_LIKE) {
H
Haojun Liao 已提交
1400
        comparFn = comparewcsPatternNMatch;
D
dapan1121 已提交
1401
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
1402
        comparFn = compareChkInString;
D
dapan1121 已提交
1403
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
1404 1405 1406 1407 1408 1409 1410
        comparFn = compareChkNotInString;
      } else {
        comparFn = compareLenPrefixedWStr;
      }
      break;
    }

S
Shengliang Guan 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
    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 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431

    default:
      comparFn = compareInt32Val;
      break;
  }

  return comparFn;
}

S
compare  
Shengliang Guan 已提交
1432 1433 1434 1435
__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) {
  switch (keyType) {
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_BOOL:
1436
      return (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc;
S
compare  
Shengliang Guan 已提交
1437
    case TSDB_DATA_TYPE_SMALLINT:
1438
      return (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc;
S
compare  
Shengliang Guan 已提交
1439
    case TSDB_DATA_TYPE_INT:
1440
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
1441 1442
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
1443
      return (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc;
S
compare  
Shengliang Guan 已提交
1444
    case TSDB_DATA_TYPE_FLOAT:
1445
      return (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc;
S
compare  
Shengliang Guan 已提交
1446
    case TSDB_DATA_TYPE_DOUBLE:
1447
      return (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc;
S
compare  
Shengliang Guan 已提交
1448
    case TSDB_DATA_TYPE_UTINYINT:
1449
      return (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc;
S
compare  
Shengliang Guan 已提交
1450
    case TSDB_DATA_TYPE_USMALLINT:
1451
      return (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc;
S
compare  
Shengliang Guan 已提交
1452
    case TSDB_DATA_TYPE_UINT:
1453
      return (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc;
S
compare  
Shengliang Guan 已提交
1454
    case TSDB_DATA_TYPE_UBIGINT:
1455
      return (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc;
S
compare  
Shengliang Guan 已提交
1456
    case TSDB_DATA_TYPE_BINARY:
D
Dingle Zhang 已提交
1457
    case TSDB_DATA_TYPE_GEOMETRY:
1458
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc;
S
compare  
Shengliang Guan 已提交
1459
    case TSDB_DATA_TYPE_NCHAR:
1460
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc;
wmmhello's avatar
wmmhello 已提交
1461 1462
    case TSDB_DATA_TYPE_JSON:
      return (order == TSDB_ORDER_ASC) ? compareJsonVal : compareJsonValDesc;
S
compare  
Shengliang Guan 已提交
1463
    default:
1464
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
1465 1466 1467
  }
}

S
Shengliang Guan 已提交
1468
int32_t doCompare(const char *f1, const char *f2, int32_t type, size_t size) {
S
compare  
Shengliang Guan 已提交
1469
  switch (type) {
S
Shengliang Guan 已提交
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
    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 已提交
1480
    case TSDB_DATA_TYPE_TINYINT:
S
Shengliang Guan 已提交
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
    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 已提交
1491
    case TSDB_DATA_TYPE_NCHAR: {
S
Shengliang Guan 已提交
1492 1493
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
1494 1495

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
1496
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
1497
      }
wafwerar's avatar
wafwerar 已提交
1498
      int32_t ret = memcmp((TdUcs4 *)t1, (TdUcs4 *)t2, t2->len);
S
compare  
Shengliang Guan 已提交
1499 1500 1501 1502 1503 1504
      if (ret == 0) {
        return ret;
      }
      return (ret < 0) ? -1 : 1;
    }
    default: {  // todo refactor
S
Shengliang Guan 已提交
1505 1506
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
1507 1508

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
1509
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
1510 1511 1512 1513 1514
      } else {
        int32_t ret = strncmp(t1->data, t2->data, t1->len);
        if (ret == 0) {
          return 0;
        } else {
S
Shengliang Guan 已提交
1515
          return ret < 0 ? -1 : 1;
S
compare  
Shengliang Guan 已提交
1516 1517 1518 1519 1520
        }
      }
    }
  }
}