tcompare.c 41.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
 *
 */
H
Haojun Liao 已提交
1010
int32_t patternMatch(const char *pattern, size_t psize, const char *str, size_t ssize, const SPatternCompareInfo *pInfo) {
H
Hongze Cheng 已提交
1011 1012 1013 1014
  char c, c1;

  int32_t i = 0;
  int32_t j = 0;
H
Haojun Liao 已提交
1015
  int32_t nMatchChar = 0;
H
Hongze Cheng 已提交
1016

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

H
Haojun Liao 已提交
1020 1021 1022 1023 1024
      while ((c = pattern[i++]) == pInfo->matchAll || c == pInfo->matchOne) {
        if (i > psize) {  // overflow check
          break;
        }

H
Hongze Cheng 已提交
1025
        if (c == pInfo->matchOne) {
H
Haojun Liao 已提交
1026
          if (j > ssize || str[j++] == 0) { // empty string, return not match
H
Hongze Cheng 已提交
1027 1028
            return TSDB_PATTERN_NOWILDCARDMATCH;
          } else {
H
Haojun Liao 已提交
1029
            ++nMatchChar;
H
Hongze Cheng 已提交
1030 1031 1032 1033
          }
        }
      }

H
Haojun Liao 已提交
1034
      if (c == 0 || i > psize) {
H
Hongze Cheng 已提交
1035 1036 1037
        return TSDB_PATTERN_MATCH; /* "*" at the end of the pattern matches */
      }

H
Haojun Liao 已提交
1038 1039 1040 1041
      char acceptArray[3] = {toupper(c), tolower(c), 0};

      str += nMatchChar;
      int32_t remain = ssize - nMatchChar;
H
Hongze Cheng 已提交
1042
      while (1) {
H
Haojun Liao 已提交
1043
        size_t n = strcspn(str, acceptArray);
H
Hongze Cheng 已提交
1044

H
Haojun Liao 已提交
1045 1046 1047 1048
        str += n;
        remain -= n;

        if (str[0] == 0 || (remain <= 0)) {
H
Hongze Cheng 已提交
1049 1050 1051
          break;
        }

H
Haojun Liao 已提交
1052
        int32_t ret = patternMatch(&pattern[i], psize - i, ++str, --remain, pInfo);
H
Hongze Cheng 已提交
1053 1054 1055 1056
        if (ret != TSDB_PATTERN_NOMATCH) {
          return ret;
        }
      }
H
Haojun Liao 已提交
1057

H
Hongze Cheng 已提交
1058 1059 1060 1061
      return TSDB_PATTERN_NOWILDCARDMATCH;
    }

    c1 = str[j++];
H
Haojun Liao 已提交
1062
    ++nMatchChar;
S
Shengliang Guan 已提交
1063

H
Haojun Liao 已提交
1064 1065
    if (j <= ssize) {
      if (c == '\\' && pattern[i] == '_' && c1 == '_') {
S
Shengliang Guan 已提交
1066 1067 1068
        i++;
        continue;
      }
H
Haojun Liao 已提交
1069

H
Hongze Cheng 已提交
1070 1071 1072 1073 1074 1075 1076 1077
      if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) {
        continue;
      }
    }

    return TSDB_PATTERN_NOMATCH;
  }

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

H
Haojun Liao 已提交
1081
int32_t wcsPatternMatch(const TdUcs4 *pattern, size_t psize, const TdUcs4 *str, size_t ssize, const SPatternCompareInfo *pInfo) {
wafwerar's avatar
wafwerar 已提交
1082
  TdUcs4 c, c1;
H
Hongze Cheng 已提交
1083 1084 1085

  int32_t i = 0;
  int32_t j = 0;
H
Haojun Liao 已提交
1086
  int32_t nMatchChar = 0;
H
Hongze Cheng 已提交
1087

H
Haojun Liao 已提交
1088 1089 1090 1091 1092 1093 1094
  while ((c = pattern[i++]) != 0 && (i <= psize)) {
    /* Match "%" */
    if (c == pInfo->umatchAll) {
      while ((c = pattern[i++]) == pInfo->umatchAll || c == pInfo->umatchOne) {
        if (i > psize) {
          break;
        }
H
Hongze Cheng 已提交
1095

H
Haojun Liao 已提交
1096 1097 1098 1099 1100 1101
        if (c == pInfo->umatchOne) {
          if (j >= ssize || str[j++] == 0) {
            return TSDB_PATTERN_NOWILDCARDMATCH;
          } else {
            ++nMatchChar;
          }
H
Hongze Cheng 已提交
1102 1103
        }
      }
H
Haojun Liao 已提交
1104 1105

      if (c == 0 || i > psize) {
H
Hongze Cheng 已提交
1106 1107 1108
        return TSDB_PATTERN_MATCH;
      }

H
Haojun Liao 已提交
1109 1110 1111 1112
      TdUcs4  acceptArray[3] = {towupper(c), towlower(c), 0};

      str += nMatchChar;
      int32_t remain = ssize - nMatchChar;
H
Hongze Cheng 已提交
1113
      while (1) {
H
Haojun Liao 已提交
1114
        size_t n = wcscspn(str, acceptArray);
H
Hongze Cheng 已提交
1115 1116

        str += n;
H
Haojun Liao 已提交
1117 1118 1119
        remain -= n;

        if (str[0] == 0 || (remain <= 0)) {
H
Hongze Cheng 已提交
1120 1121 1122
          break;
        }

H
Haojun Liao 已提交
1123
        int32_t ret = wcsPatternMatch(&pattern[i], psize-i, ++str, --remain, pInfo);
H
Hongze Cheng 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132
        if (ret != TSDB_PATTERN_NOMATCH) {
          return ret;
        }
      }

      return TSDB_PATTERN_NOWILDCARDMATCH;
    }

    c1 = str[j++];
H
Haojun Liao 已提交
1133
    nMatchChar++;
H
Hongze Cheng 已提交
1134

H
Haojun Liao 已提交
1135 1136 1137 1138 1139 1140 1141
    if (j <= ssize) {
      if (c == L'\\' && pattern[i] == L'_' && c1 == L'_') {
        i++;
        continue;
      }

      if (c == c1 || towlower(c) == towlower(c1) || (c == pInfo->umatchOne && c1 != 0)) {
H
Hongze Cheng 已提交
1142 1143 1144 1145 1146 1147
        continue;
      }
    }

    return TSDB_PATTERN_NOMATCH;
  }
S
Shengliang Guan 已提交
1148

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

H
Haojun Liao 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight) {
  return comparestrRegexMatch(pLeft, pRight) ? 0 : 1;
}

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));

    uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf);
    regfree(&regex);
    return 1;
  }

  ret = regexec(&regex, pString, 0, NULL, 0);
  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 已提交
1175

H
Haojun Liao 已提交
1176 1177
  regfree(&regex);
  return (ret == 0) ? 0 : 1;
H
Hongze Cheng 已提交
1178 1179
}

H
Haojun Liao 已提交
1180
int32_t comparestrRegexMatch(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
1181
  size_t sz = varDataLen(pRight);
wafwerar's avatar
wafwerar 已提交
1182
  char  *pattern = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
1183 1184 1185 1186
  memcpy(pattern, varDataVal(pRight), varDataLen(pRight));
  pattern[sz] = 0;

  sz = varDataLen(pLeft);
wafwerar's avatar
wafwerar 已提交
1187
  char *str = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
1188 1189 1190
  memcpy(str, varDataVal(pLeft), sz);
  str[sz] = 0;

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

H
Haojun Liao 已提交
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
  taosMemoryFree(str);
  taosMemoryFree(pattern);

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

int32_t comparewcsRegexMatch(const void* pString, const void* pPattern) {
  size_t len = varDataLen(pPattern);
  char  *pattern = taosMemoryMalloc(len + 1);

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

H
Haojun Liao 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
  pattern[len] = 0;

  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 已提交
1219
  }
H
Haojun Liao 已提交
1220 1221 1222 1223 1224

  str[len] = 0;

  int32_t ret = doExecRegexMatch(str, pattern);

wafwerar's avatar
wafwerar 已提交
1225 1226
  taosMemoryFree(str);
  taosMemoryFree(pattern);
H
Haojun Liao 已提交
1227 1228 1229 1230 1231 1232

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

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

S
Shengliang Guan 已提交
1235 1236 1237
int32_t taosArrayCompareString(const void *a, const void *b) {
  const char *x = *(const char **)a;
  const char *y = *(const char **)b;
H
Hongze Cheng 已提交
1238 1239 1240

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

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

H
Haojun Liao 已提交
1245 1246
  ASSERT(varDataLen(pRight) <= TSDB_MAX_FIELD_LEN);
  size_t pLen = varDataLen(pRight);
D
dapan1121 已提交
1247 1248
  size_t sz = varDataLen(pLeft);

H
Haojun Liao 已提交
1249
  int32_t ret = patternMatch(varDataVal(pRight), pLen, varDataVal(pLeft), sz, &pInfo);
D
dapan1121 已提交
1250 1251 1252
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
}

H
Haojun Liao 已提交
1253 1254
int32_t comparestrPatternNMatch(const void *pLeft, const void *pRight) {
  return comparestrPatternMatch(pLeft, pRight) ? 0 : 1;
D
dapan1121 已提交
1255 1256
}

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

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

H
Haojun Liao 已提交
1262 1263
  int32_t ret = wcsPatternMatch((TdUcs4 *)varDataVal(pRight), psize / TSDB_NCHAR_SIZE, (TdUcs4 *)varDataVal(pLeft),
                                varDataLen(pLeft) / TSDB_NCHAR_SIZE, &pInfo);
D
dapan1121 已提交
1264 1265 1266
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
}

H
Haojun Liao 已提交
1267 1268
int32_t comparewcsPatternNMatch(const void *pLeft, const void *pRight) {
  return comparewcsPatternMatch(pLeft, pRight) ? 0 : 1;
D
dapan1121 已提交
1269
}
H
Haojun Liao 已提交
1270

D
dapan1121 已提交
1271 1272 1273
__compar_fn_t getComparFunc(int32_t type, int32_t optr) {
  __compar_fn_t comparFn = NULL;

D
dapan1121 已提交
1274
  if (optr == OP_TYPE_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
D
dapan1121 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
    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 已提交
1297
  if (optr == OP_TYPE_NOT_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
D
dapan1121 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
    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 已提交
1322 1323 1324 1325 1326 1327 1328 1329 1330
    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 已提交
1331
    case TSDB_DATA_TYPE_BIGINT:
S
Shengliang Guan 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340
    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 已提交
1341
    case TSDB_DATA_TYPE_BINARY: {
D
dapan1121 已提交
1342
      if (optr == OP_TYPE_MATCH) {
H
Haojun Liao 已提交
1343
        comparFn = comparestrRegexMatch;
D
dapan1121 已提交
1344
      } else if (optr == OP_TYPE_NMATCH) {
H
Haojun Liao 已提交
1345
        comparFn = comparestrRegexNMatch;
D
dapan1121 已提交
1346
      } else if (optr == OP_TYPE_LIKE) { /* wildcard query using like operator */
H
Haojun Liao 已提交
1347
        comparFn = comparestrPatternMatch;
D
dapan1121 已提交
1348
      } else if (optr == OP_TYPE_NOT_LIKE) { /* wildcard query using like operator */
H
Haojun Liao 已提交
1349
        comparFn = comparestrPatternNMatch;
D
dapan1121 已提交
1350
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
1351
        comparFn = compareChkInString;
D
dapan1121 已提交
1352
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
1353 1354 1355 1356 1357 1358 1359 1360 1361
        comparFn = compareChkNotInString;
      } else { /* normal relational comparFn */
        comparFn = compareLenPrefixedStr;
      }

      break;
    }

    case TSDB_DATA_TYPE_NCHAR: {
D
dapan1121 已提交
1362
      if (optr == OP_TYPE_MATCH) {
H
Haojun Liao 已提交
1363
        comparFn = comparewcsRegexMatch;
D
dapan1121 已提交
1364
      } else if (optr == OP_TYPE_NMATCH) {
H
Haojun Liao 已提交
1365
        comparFn = comparewcsRegexNMatch;
D
dapan1121 已提交
1366
      } else if (optr == OP_TYPE_LIKE) {
H
Haojun Liao 已提交
1367
        comparFn = comparewcsPatternMatch;
D
dapan1121 已提交
1368
      } else if (optr == OP_TYPE_NOT_LIKE) {
H
Haojun Liao 已提交
1369
        comparFn = comparewcsPatternNMatch;
D
dapan1121 已提交
1370
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
1371
        comparFn = compareChkInString;
D
dapan1121 已提交
1372
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
1373 1374 1375 1376 1377 1378 1379
        comparFn = compareChkNotInString;
      } else {
        comparFn = compareLenPrefixedWStr;
      }
      break;
    }

S
Shengliang Guan 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
    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 已提交
1392 1393 1394 1395 1396 1397 1398 1399 1400

    default:
      comparFn = compareInt32Val;
      break;
  }

  return comparFn;
}

S
compare  
Shengliang Guan 已提交
1401 1402 1403 1404
__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) {
  switch (keyType) {
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_BOOL:
1405
      return (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc;
S
compare  
Shengliang Guan 已提交
1406
    case TSDB_DATA_TYPE_SMALLINT:
1407
      return (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc;
S
compare  
Shengliang Guan 已提交
1408
    case TSDB_DATA_TYPE_INT:
1409
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
1410 1411
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
1412
      return (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc;
S
compare  
Shengliang Guan 已提交
1413
    case TSDB_DATA_TYPE_FLOAT:
1414
      return (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc;
S
compare  
Shengliang Guan 已提交
1415
    case TSDB_DATA_TYPE_DOUBLE:
1416
      return (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc;
S
compare  
Shengliang Guan 已提交
1417
    case TSDB_DATA_TYPE_UTINYINT:
1418
      return (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc;
S
compare  
Shengliang Guan 已提交
1419
    case TSDB_DATA_TYPE_USMALLINT:
1420
      return (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc;
S
compare  
Shengliang Guan 已提交
1421
    case TSDB_DATA_TYPE_UINT:
1422
      return (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc;
S
compare  
Shengliang Guan 已提交
1423
    case TSDB_DATA_TYPE_UBIGINT:
1424
      return (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc;
S
compare  
Shengliang Guan 已提交
1425
    case TSDB_DATA_TYPE_BINARY:
1426
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc;
S
compare  
Shengliang Guan 已提交
1427
    case TSDB_DATA_TYPE_NCHAR:
1428
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc;
wmmhello's avatar
wmmhello 已提交
1429 1430
    case TSDB_DATA_TYPE_JSON:
      return (order == TSDB_ORDER_ASC) ? compareJsonVal : compareJsonValDesc;
S
compare  
Shengliang Guan 已提交
1431
    default:
1432
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
1433 1434 1435
  }
}

S
Shengliang Guan 已提交
1436
int32_t doCompare(const char *f1, const char *f2, int32_t type, size_t size) {
S
compare  
Shengliang Guan 已提交
1437
  switch (type) {
S
Shengliang Guan 已提交
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    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 已提交
1448
    case TSDB_DATA_TYPE_TINYINT:
S
Shengliang Guan 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
    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 已提交
1459
    case TSDB_DATA_TYPE_NCHAR: {
S
Shengliang Guan 已提交
1460 1461
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
1462 1463

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
1464
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
1465
      }
wafwerar's avatar
wafwerar 已提交
1466
      int32_t ret = memcmp((TdUcs4 *)t1, (TdUcs4 *)t2, t2->len);
S
compare  
Shengliang Guan 已提交
1467 1468 1469 1470 1471 1472
      if (ret == 0) {
        return ret;
      }
      return (ret < 0) ? -1 : 1;
    }
    default: {  // todo refactor
S
Shengliang Guan 已提交
1473 1474
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
1475 1476

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
1477
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
1478 1479 1480 1481 1482
      } else {
        int32_t ret = strncmp(t1->data, t2->data, t1->len);
        if (ret == 0) {
          return 0;
        } else {
S
Shengliang Guan 已提交
1483
          return ret < 0 ? -1 : 1;
S
compare  
Shengliang Guan 已提交
1484 1485 1486 1487 1488
        }
      }
    }
  }
}