• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3911

24 Apr 2025 11:36PM UTC coverage: 53.735% (-1.6%) from 55.295%
#3911

push

travis-ci

happyguoxy
Sync branches at 2025-04-25 07:35

170049 of 316459 relevant lines covered (53.73%)

1192430.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

30
int32_t setChkInBytes1(const void *pLeft, const void *pRight) {
×
31
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0;
×
32
}
33

34
int32_t setChkInBytes2(const void *pLeft, const void *pRight) {
×
35
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 2) ? 1 : 0;
×
36
}
37

38
int32_t setChkInBytes4(const void *pLeft, const void *pRight) {
62✔
39
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 4) ? 1 : 0;
62✔
40
}
41

42
int32_t setChkInBytes8(const void *pLeft, const void *pRight) {
131✔
43
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, 8) ? 1 : 0;
131✔
44
}
45

46
int32_t setChkNotInBytes1(const void *pLeft, const void *pRight) {
×
47
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, 1) ? 1 : 0;
×
48
}
49

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

54
int32_t setChkNotInBytes4(const void *pLeft, const void *pRight) {
2✔
55
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, 4) ? 1 : 0;
2✔
56
}
57

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

62
int32_t setChkInDecimalHash(const void* pLeft, const void* pRight) {
×
63
  const SDecimalCompareCtx *pCtxL = pLeft, *pCtxR = pRight;
×
64
  return NULL != taosHashGet((SHashObj *)(pCtxR->pData), pCtxL->pData, tDataTypes[pCtxL->type].bytes) ? 1 : 0;
×
65
}
66

67
int32_t setChkNotInDecimalHash(const void* pLeft, const void* pRight) {
×
68
  const SDecimalCompareCtx *pCtxL = pLeft, *pCtxR = pRight;
×
69
  return NULL == taosHashGet((SHashObj *)(pCtxR->pData), pCtxL->pData, tDataTypes[pCtxL->type].bytes) ? 1 : 0;
×
70
}
71

72
int32_t compareChkInString(const void *pLeft, const void *pRight) {
5✔
73
  return NULL != taosHashGet((SHashObj *)pRight, pLeft, varDataTLen(pLeft)) ? 1 : 0;
5✔
74
}
75

76
int32_t compareChkNotInString(const void *pLeft, const void *pRight) {
×
77
  return NULL == taosHashGet((SHashObj *)pRight, pLeft, varDataTLen(pLeft)) ? 1 : 0;
×
78
}
79

80
int32_t compareInt8Val(const void *pLeft, const void *pRight) {
22,955✔
81
  int8_t left = GET_INT8_VAL(pLeft), right = GET_INT8_VAL(pRight);
22,955✔
82
  if (left > right) return 1;
22,955✔
83
  if (left < right) return -1;
16,032✔
84
  return 0;
9,377✔
85
}
86

87
int32_t compareInt8ValDesc(const void *pLeft, const void *pRight) { return compareInt8Val(pRight, pLeft); }
534✔
88

89
int32_t compareInt16Val(const void *pLeft, const void *pRight) {
18,333✔
90
  int16_t left = GET_INT16_VAL(pLeft), right = GET_INT16_VAL(pRight);
18,333✔
91
  if (left > right) return 1;
18,333✔
92
  if (left < right) return -1;
10,912✔
93
  return 0;
3,210✔
94
}
95

96
int32_t compareInt16ValDesc(const void *pLeft, const void *pRight) { return compareInt16Val(pRight, pLeft); }
30✔
97

98
int32_t compareInt32Val(const void *pLeft, const void *pRight) {
134,634✔
99
  int32_t left = GET_INT32_VAL(pLeft), right = GET_INT32_VAL(pRight);
134,634✔
100
  if (left > right) return 1;
134,634✔
101
  if (left < right) return -1;
21,505✔
102
  return 0;
7,664✔
103
}
104

105
int32_t compareInt32ValDesc(const void *pLeft, const void *pRight) { return compareInt32Val(pRight, pLeft); }
30✔
106

107
int32_t compareInt64Val(const void *pLeft, const void *pRight) {
35,992✔
108
  int64_t left = GET_INT64_VAL(pLeft), right = GET_INT64_VAL(pRight);
35,992✔
109
  if (left > right) return 1;
35,992✔
110
  if (left < right) return -1;
15,494✔
111
  return 0;
3,333✔
112
}
113

114
int32_t compareInt64ValDesc(const void *pLeft, const void *pRight) { return compareInt64Val(pRight, pLeft); }
100✔
115

116
int32_t compareUint32Val(const void *pLeft, const void *pRight) {
8,289✔
117
  uint32_t left = GET_UINT32_VAL(pLeft), right = GET_UINT32_VAL(pRight);
8,289✔
118
  if (left > right) return 1;
8,289✔
119
  if (left < right) return -1;
3,903✔
120
  return 0;
1,075✔
121
}
122

123
int32_t compareUint32ValDesc(const void *pLeft, const void *pRight) { return compareUint32Val(pRight, pLeft); }
32✔
124

125
int32_t compareUint64Val(const void *pLeft, const void *pRight) {
8,322✔
126
  uint64_t left = GET_UINT64_VAL(pLeft), right = GET_UINT64_VAL(pRight);
8,322✔
127
  if (left > right) return 1;
8,322✔
128
  if (left < right) return -1;
4,205✔
129
  return 0;
1,073✔
130
}
131

132
int32_t compareUint64ValDesc(const void *pLeft, const void *pRight) { return compareUint64Val(pRight, pLeft); }
30✔
133

134
int32_t compareUint16Val(const void *pLeft, const void *pRight) {
17,320✔
135
  uint16_t left = GET_UINT16_VAL(pLeft), right = GET_UINT16_VAL(pRight);
17,320✔
136
  if (left > right) return 1;
17,320✔
137
  if (left < right) return -1;
10,259✔
138
  return 0;
2,856✔
139
}
140

141
int32_t compareUint16ValDesc(const void *pLeft, const void *pRight) { return compareUint16Val(pRight, pLeft); }
30✔
142

143
int32_t compareUint8Val(const void *pLeft, const void *pRight) {
18,112✔
144
  uint8_t left = GET_UINT8_VAL(pLeft), right = GET_UINT8_VAL(pRight);
18,112✔
145
  if (left > right) return 1;
18,112✔
146
  if (left < right) return -1;
10,644✔
147
  return 0;
2,863✔
148
}
149

150
int32_t compareUint8ValDesc(const void *pLeft, const void *pRight) { return compareUint8Val(pRight, pLeft); }
30✔
151

152
int32_t compareFloatVal(const void *pLeft, const void *pRight) {
26,191,334✔
153
  float p1 = GET_FLOAT_VAL(pLeft);
26,191,334✔
154
  float p2 = GET_FLOAT_VAL(pRight);
26,191,334✔
155

156
  if (isnan(p1) && isnan(p2)) {
26,191,334✔
157
    return 0;
×
158
  }
159

160
  if (isnan(p1)) {
26,191,334✔
161
    return -1;
×
162
  }
163

164
  if (isnan(p2)) {
26,191,334✔
165
    return 1;
×
166
  }
167
  if (FLT_EQUAL(p1, p2)) {
26,191,334✔
168
    return 0;
1,204✔
169
  }
170
  return FLT_GREATER(p1, p2) ? 1 : -1;
26,190,130✔
171
}
172

173
int32_t compareFloatValDesc(const void *pLeft, const void *pRight) { return compareFloatVal(pRight, pLeft); }
30✔
174

175
int32_t compareDoubleVal(const void *pLeft, const void *pRight) {
44,334✔
176
  double p1 = GET_DOUBLE_VAL(pLeft);
44,334✔
177
  double p2 = GET_DOUBLE_VAL(pRight);
44,334✔
178

179
  if (isnan(p1) && isnan(p2)) {
44,334✔
180
    return 0;
×
181
  }
182

183
  if (isnan(p1)) {
44,334✔
184
    return -1;
×
185
  }
186

187
  if (isnan(p2)) {
44,334✔
188
    return 1;
×
189
  }
190

191
  if (DBL_EQUAL(p1, p2)) {
44,334✔
192
    return 0;
14,760✔
193
  }
194
  return FLT_GREATER(p1, p2) ? 1 : -1;
29,574✔
195
}
196

197
int32_t compareDoubleValDesc(const void *pLeft, const void *pRight) { return compareDoubleVal(pRight, pLeft); }
30✔
198

199
int32_t compareLenPrefixedStr(const void *pLeft, const void *pRight) {
22,937✔
200
  int32_t len1 = varDataLen(pLeft);
22,937✔
201
  int32_t len2 = varDataLen(pRight);
22,937✔
202

203
  int32_t minLen = TMIN(len1, len2);
22,937✔
204
  int32_t ret = strncmp(varDataVal(pLeft), varDataVal(pRight), minLen);
22,937✔
205
  if (ret == 0) {
22,937✔
206
    if (len1 == len2) {
4,076✔
207
      return 0;
3,394✔
208
    } else {
209
      return len1 > len2 ? 1 : -1;
682✔
210
    }
211
  } else {
212
    return ret > 0 ? 1 : -1;
18,861✔
213
  }
214
}
215

216
int32_t compareLenPrefixedStrDesc(const void *pLeft, const void *pRight) {
30✔
217
  return compareLenPrefixedStr(pRight, pLeft);
30✔
218
}
219

220
int32_t compareLenPrefixedWStr(const void *pLeft, const void *pRight) {
19,022✔
221
  int32_t len1 = varDataLen(pLeft);
19,022✔
222
  int32_t len2 = varDataLen(pRight);
19,022✔
223

224
  int32_t ret = taosUcs4Compare((TdUcs4 *)varDataVal(pLeft), (TdUcs4 *)varDataVal(pRight), len1>len2 ? len2:len1);
19,022✔
225
  if (ret == 0) {
19,022✔
226
    if (len1 > len2)
1,708✔
227
      return 1;
557✔
228
    else if(len1 < len2)
1,151✔
229
      return -1;
57✔
230
    else
231
      return 0;
1,094✔
232
  }
233
  return (ret < 0) ? -1 : 1;
17,314✔
234
}
235

236
int32_t compareLenPrefixedWStrDesc(const void *pLeft, const void *pRight) {
32✔
237
  return compareLenPrefixedWStr(pRight, pLeft);
32✔
238
}
239

240
int32_t compareLenBinaryVal(const void *pLeft, const void *pRight) {
2✔
241
  int32_t len1 = varDataLen(pLeft);
2✔
242
  int32_t len2 = varDataLen(pRight);
2✔
243

244
  int32_t minLen = TMIN(len1, len2);
2✔
245
  int32_t ret = memcmp(varDataVal(pLeft), varDataVal(pRight), minLen);
2✔
246
  if (ret == 0) {
2✔
247
    if (len1 == len2) {
1✔
248
      return 0;
1✔
249
    } else {
250
      return len1 > len2 ? 1 : -1;
×
251
    }
252
  } else {
253
    return ret > 0 ? 1 : -1;
1✔
254
  }
255
}
256

257
int32_t compareLenBinaryValDesc(const void *pLeft, const void *pRight) {
×
258
  return compareLenBinaryVal(pRight, pLeft);
×
259
}
260

261
// string > number > bool > null
262
// ref: https://dev.mysql.com/doc/refman/8.0/en/json.html#json-comparison
263
int32_t compareJsonVal(const void *pLeft, const void *pRight) {
×
264
  char leftType = *(char *)pLeft;
×
265
  char rightType = *(char *)pRight;
×
266
  if (leftType != rightType) {
×
267
    return leftType > rightType ? 1 : -1;
×
268
  }
269

270
  char *realDataLeft = POINTER_SHIFT(pLeft, CHAR_BYTES);
×
271
  char *realDataRight = POINTER_SHIFT(pRight, CHAR_BYTES);
×
272
  if (leftType == TSDB_DATA_TYPE_BOOL) {
×
273
    DEFAULT_COMP(GET_INT8_VAL(realDataLeft), GET_INT8_VAL(realDataRight));
×
274
  } else if (leftType == TSDB_DATA_TYPE_DOUBLE) {
×
275
    DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(realDataLeft), GET_DOUBLE_VAL(realDataRight));
×
276
  } else if (leftType == TSDB_DATA_TYPE_NCHAR) {
×
277
    return compareLenPrefixedWStr(realDataLeft, realDataRight);
×
278
  } else if (leftType == TSDB_DATA_TYPE_NULL) {
×
279
    return 0;
×
280
  } else {
281
    uError("data type unexpected leftType:%d rightType:%d", leftType, rightType);
×
282
    return 0;
×
283
  }
284
}
285

286
int32_t compareInt8Int16(const void *pLeft, const void *pRight) {
×
287
  int8_t  left = GET_INT8_VAL(pLeft);
×
288
  int16_t right = GET_INT16_VAL(pRight);
×
289
  if (left > right) return 1;
×
290
  if (left < right) return -1;
×
291
  return 0;
×
292
}
293

294
int32_t compareInt8Int32(const void *pLeft, const void *pRight) {
×
295
  int8_t  left = GET_INT8_VAL(pLeft);
×
296
  int32_t right = GET_INT32_VAL(pRight);
×
297
  if (left > right) return 1;
×
298
  if (left < right) return -1;
×
299
  return 0;
×
300
}
301

302
int32_t compareInt8Int64(const void *pLeft, const void *pRight) {
×
303
  int8_t  left = GET_INT8_VAL(pLeft);
×
304
  int64_t right = GET_INT64_VAL(pRight);
×
305
  if (left > right) return 1;
×
306
  if (left < right) return -1;
×
307
  return 0;
×
308
}
309

310
int32_t compareInt8Float(const void *pLeft, const void *pRight) {
×
311
  int8_t left = GET_INT8_VAL(pLeft);
×
312
  float  right = GET_FLOAT_VAL(pRight);
×
313
  if (left > right) return 1;
×
314
  if (left < right) return -1;
×
315
  return 0;
×
316
}
317

318
int32_t compareInt8Double(const void *pLeft, const void *pRight) {
×
319
  int8_t left = GET_INT8_VAL(pLeft);
×
320
  double right = GET_DOUBLE_VAL(pRight);
×
321
  if (left > right) return 1;
×
322
  if (left < right) return -1;
×
323
  return 0;
×
324
}
325

326
int32_t compareInt8Uint8(const void *pLeft, const void *pRight) {
×
327
  int8_t  left = GET_INT8_VAL(pLeft);
×
328
  uint8_t right = GET_UINT8_VAL(pRight);
×
329
  if (left > right) return 1;
×
330
  if (left < right) return -1;
×
331
  return 0;
×
332
}
333

334
int32_t compareInt8Uint16(const void *pLeft, const void *pRight) {
×
335
  int8_t   left = GET_INT8_VAL(pLeft);
×
336
  uint16_t right = GET_UINT16_VAL(pRight);
×
337
  if (left > right) return 1;
×
338
  if (left < right) return -1;
×
339
  return 0;
×
340
}
341

342
int32_t compareInt8Uint32(const void *pLeft, const void *pRight) {
×
343
  int8_t   left = GET_INT8_VAL(pLeft);
×
344
  if (left < 0) return -1;
×
345
  uint32_t right = GET_UINT32_VAL(pRight);
×
346
  if ((uint32_t)left > right) return 1;
×
347
  if ((uint32_t)left < right) return -1;
×
348
  return 0;
×
349
}
350

351
int32_t compareInt8Uint64(const void *pLeft, const void *pRight) {
1✔
352
  int8_t   left = GET_INT8_VAL(pLeft);
1✔
353
  if (left < 0) return -1;
1✔
354
  uint64_t right = GET_UINT64_VAL(pRight);
1✔
355
  if ((uint64_t)left > right) return 1;
1✔
356
  if ((uint64_t)left < right) return -1;
×
357
  return 0;
×
358
}
359

360
int32_t compareInt16Int8(const void *pLeft, const void *pRight) {
2,696✔
361
  int16_t left = GET_INT16_VAL(pLeft);
2,696✔
362
  int8_t  right = GET_INT8_VAL(pRight);
2,696✔
363
  if (left > right) return 1;
2,696✔
364
  if (left < right) return -1;
242✔
365
  return 0;
22✔
366
}
367

368
int32_t compareInt16Int32(const void *pLeft, const void *pRight) {
5✔
369
  int16_t left = GET_INT16_VAL(pLeft);
5✔
370
  int32_t right = GET_INT32_VAL(pRight);
5✔
371
  if (left > right) return 1;
5✔
372
  if (left < right) return -1;
3✔
373
  return 0;
×
374
}
375

376
int32_t compareInt16Int64(const void *pLeft, const void *pRight) {
×
377
  int16_t left = GET_INT16_VAL(pLeft);
×
378
  int64_t right = GET_INT64_VAL(pRight);
×
379
  if (left > right) return 1;
×
380
  if (left < right) return -1;
×
381
  return 0;
×
382
}
383

384
int32_t compareInt16Float(const void *pLeft, const void *pRight) {
×
385
  int16_t left = GET_INT16_VAL(pLeft);
×
386
  float   right = GET_FLOAT_VAL(pRight);
×
387
  if (left > right) return 1;
×
388
  if (left < right) return -1;
×
389
  return 0;
×
390
}
391

392
int32_t compareInt16Double(const void *pLeft, const void *pRight) {
5✔
393
  int16_t left = GET_INT16_VAL(pLeft);
5✔
394
  double  right = GET_DOUBLE_VAL(pRight);
5✔
395
  if (left > right) return 1;
5✔
396
  if (left < right) return -1;
2✔
397
  return 0;
×
398
}
399

400
int32_t compareInt16Uint8(const void *pLeft, const void *pRight) {
×
401
  int16_t left = GET_INT16_VAL(pLeft);
×
402
  uint8_t right = GET_UINT8_VAL(pRight);
×
403
  if (left > right) return 1;
×
404
  if (left < right) return -1;
×
405
  return 0;
×
406
}
407

408
int32_t compareInt16Uint16(const void *pLeft, const void *pRight) {
×
409
  int16_t  left = GET_INT16_VAL(pLeft);
×
410
  uint16_t right = GET_UINT16_VAL(pRight);
×
411
  if (left > right) return 1;
×
412
  if (left < right) return -1;
×
413
  return 0;
×
414
}
415

416
int32_t compareInt16Uint32(const void *pLeft, const void *pRight) {
×
417
  int16_t  left = GET_INT16_VAL(pLeft);
×
418
  if (left < 0) return -1;
×
419
  uint32_t right = GET_UINT32_VAL(pRight);
×
420
  if ((uint32_t)left > right) return 1;
×
421
  if ((uint32_t)left < right) return -1;
×
422
  return 0;
×
423
}
424

425
int32_t compareInt16Uint64(const void *pLeft, const void *pRight) {
×
426
  int16_t  left = GET_INT16_VAL(pLeft);
×
427
  if (left < 0) return -1;
×
428
  uint64_t right = GET_UINT64_VAL(pRight);
×
429
  if ((uint64_t)left > right) return 1;
×
430
  if ((uint64_t)left < right) return -1;
×
431
  return 0;
×
432
}
433

434
int32_t compareInt32Int8(const void *pLeft, const void *pRight) {
1,348✔
435
  int32_t left = GET_INT32_VAL(pLeft);
1,348✔
436
  int8_t  right = GET_INT8_VAL(pRight);
1,348✔
437
  if (left > right) return 1;
1,348✔
438
  if (left < right) return -1;
62✔
439
  return 0;
2✔
440
}
441

442
int32_t compareInt32Int16(const void *pLeft, const void *pRight) {
4✔
443
  int32_t left = GET_INT32_VAL(pLeft);
4✔
444
  int16_t right = GET_INT16_VAL(pRight);
4✔
445
  if (left > right) return 1;
4✔
446
  if (left < right) return -1;
4✔
447
  return 0;
2✔
448
}
449

450
int32_t compareInt32Int64(const void *pLeft, const void *pRight) {
94,030✔
451
  int32_t left = GET_INT32_VAL(pLeft);
94,030✔
452
  int64_t right = GET_INT64_VAL(pRight);
94,030✔
453
  if (left > right) return 1;
94,030✔
454
  if (left < right) return -1;
6✔
455
  return 0;
×
456
}
457

458
int32_t compareInt32Float(const void *pLeft, const void *pRight) {
×
459
  int32_t left = GET_INT32_VAL(pLeft);
×
460
  float   right = GET_FLOAT_VAL(pRight);
×
461
  if (left > right) return 1;
×
462
  if (left < right) return -1;
×
463
  return 0;
×
464
}
465

466
int32_t compareInt32Double(const void *pLeft, const void *pRight) {
1✔
467
  int32_t left = GET_INT32_VAL(pLeft);
1✔
468
  double  right = GET_DOUBLE_VAL(pRight);
1✔
469
  if (left > right) return 1;
1✔
470
  if (left < right) return -1;
×
471
  return 0;
×
472
}
473

474
int32_t compareInt32Uint8(const void *pLeft, const void *pRight) {
×
475
  int32_t left = GET_INT32_VAL(pLeft);
×
476
  uint8_t right = GET_UINT8_VAL(pRight);
×
477
  if (left > right) return 1;
×
478
  if (left < right) return -1;
×
479
  return 0;
×
480
}
481

482
int32_t compareInt32Uint16(const void *pLeft, const void *pRight) {
×
483
  int32_t  left = GET_INT32_VAL(pLeft);
×
484
  uint16_t right = GET_UINT16_VAL(pRight);
×
485
  if (left > right) return 1;
×
486
  if (left < right) return -1;
×
487
  return 0;
×
488
}
489

490
int32_t compareInt32Uint32(const void *pLeft, const void *pRight) {
×
491
  int32_t  left = GET_INT32_VAL(pLeft);
×
492
  if (left < 0) return -1;
×
493
  uint32_t right = GET_UINT32_VAL(pRight);
×
494
  if ((uint32_t)left > right) return 1;
×
495
  if ((uint32_t)left < right) return -1;
×
496
  return 0;
×
497
}
498

499
int32_t compareInt32Uint64(const void *pLeft, const void *pRight) {
×
500
  int32_t  left = GET_INT32_VAL(pLeft);
×
501
  if (left < 0) return -1;
×
502
  uint64_t right = GET_UINT64_VAL(pRight);
×
503
  if ((uint64_t)left > right) return 1;
×
504
  if ((uint64_t)left < right) return -1;
×
505
  return 0;
×
506
}
507

508
int32_t compareInt64Int8(const void *pLeft, const void *pRight) {
2,696✔
509
  int64_t left = GET_INT64_VAL(pLeft);
2,696✔
510
  int8_t  right = GET_INT8_VAL(pRight);
2,696✔
511
  if (left > right) return 1;
2,696✔
512
  if (left < right) return -1;
64✔
513
  return 0;
4✔
514
}
515

516
int32_t compareInt64Int16(const void *pLeft, const void *pRight) {
×
517
  int64_t left = GET_INT64_VAL(pLeft);
×
518
  int16_t right = GET_INT16_VAL(pRight);
×
519
  if (left > right) return 1;
×
520
  if (left < right) return -1;
×
521
  return 0;
×
522
}
523

524
int32_t compareInt64Int32(const void *pLeft, const void *pRight) {
5✔
525
  int64_t left = GET_INT64_VAL(pLeft);
5✔
526
  int32_t right = GET_INT32_VAL(pRight);
5✔
527
  if (left > right) return 1;
5✔
528
  if (left < right) return -1;
4✔
529
  return 0;
×
530
}
531

532
int32_t compareInt64Float(const void *pLeft, const void *pRight) {
×
533
  int64_t left = GET_INT64_VAL(pLeft);
×
534
  float   right = GET_FLOAT_VAL(pRight);
×
535
  if (left > right) return 1;
×
536
  if (left < right) return -1;
×
537
  return 0;
×
538
}
539

540
int32_t compareInt64Double(const void *pLeft, const void *pRight) {
×
541
  int64_t left = GET_INT64_VAL(pLeft);
×
542
  double  right = GET_DOUBLE_VAL(pRight);
×
543
  if (left > right) return 1;
×
544
  if (left < right) return -1;
×
545
  return 0;
×
546
}
547

548
int32_t compareInt64Uint8(const void *pLeft, const void *pRight) {
×
549
  int64_t left = GET_INT64_VAL(pLeft);
×
550
  uint8_t right = GET_UINT8_VAL(pRight);
×
551
  if (left > right) return 1;
×
552
  if (left < right) return -1;
×
553
  return 0;
×
554
}
555

556
int32_t compareInt64Uint16(const void *pLeft, const void *pRight) {
×
557
  int64_t  left = GET_INT64_VAL(pLeft);
×
558
  uint16_t right = GET_UINT16_VAL(pRight);
×
559
  if (left > right) return 1;
×
560
  if (left < right) return -1;
×
561
  return 0;
×
562
}
563

564
int32_t compareInt64Uint32(const void *pLeft, const void *pRight) {
×
565
  int64_t  left = GET_INT64_VAL(pLeft);
×
566
  uint32_t right = GET_UINT32_VAL(pRight);
×
567
  if (left > right) return 1;
×
568
  if (left < right) return -1;
×
569
  return 0;
×
570
}
571

572
int32_t compareInt64Uint64(const void *pLeft, const void *pRight) {
×
573
  int64_t  left = GET_INT64_VAL(pLeft);
×
574
  if (left < 0) return -1;
×
575
  uint64_t right = GET_UINT64_VAL(pRight);
×
576
  if ((uint64_t)left > right) return 1;
×
577
  if ((uint64_t)left < right) return -1;
×
578
  return 0;
×
579
}
580

581
int32_t compareFloatInt8(const void *pLeft, const void *pRight) {
×
582
  float  left = GET_FLOAT_VAL(pLeft);
×
583
  int8_t right = GET_INT8_VAL(pRight);
×
584
  if (left > right) return 1;
×
585
  if (left < right) return -1;
×
586
  return 0;
×
587
}
588

589
int32_t compareFloatInt16(const void *pLeft, const void *pRight) {
×
590
  float   left = GET_FLOAT_VAL(pLeft);
×
591
  int16_t right = GET_INT16_VAL(pRight);
×
592
  if (left > right) return 1;
×
593
  if (left < right) return -1;
×
594
  return 0;
×
595
}
596

597
int32_t compareFloatInt32(const void *pLeft, const void *pRight) {
×
598
  float   left = GET_FLOAT_VAL(pLeft);
×
599
  int32_t right = GET_INT32_VAL(pRight);
×
600
  if (left > right) return 1;
×
601
  if (left < right) return -1;
×
602
  return 0;
×
603
}
604

605
int32_t compareFloatInt64(const void *pLeft, const void *pRight) {
×
606
  float   left = GET_FLOAT_VAL(pLeft);
×
607
  int64_t right = GET_INT64_VAL(pRight);
×
608
  if (left > right) return 1;
×
609
  if (left < right) return -1;
×
610
  return 0;
×
611
}
612

613
int32_t compareFloatDouble(const void *pLeft, const void *pRight) {
×
614
  float  left = GET_FLOAT_VAL(pLeft);
×
615
  double right = GET_DOUBLE_VAL(pRight);
×
616

617
  if (isnan(left) && isnan(right)) {
×
618
    return 0;
×
619
  }
620

621
  if (isnan(left)) {
×
622
    return -1;
×
623
  }
624

625
  if (isnan(right)) {
×
626
    return 1;
×
627
  }
628

629
  if (FLT_EQUAL(left, right)) {
×
630
    return 0;
×
631
  }
632
  return FLT_GREATER(left, right) ? 1 : -1;
×
633
}
634

635
int32_t compareFloatUint8(const void *pLeft, const void *pRight) {
×
636
  float   left = GET_FLOAT_VAL(pLeft);
×
637
  uint8_t right = GET_UINT8_VAL(pRight);
×
638
  if (left > right) return 1;
×
639
  if (left < right) return -1;
×
640
  return 0;
×
641
}
642

643
int32_t compareFloatUint16(const void *pLeft, const void *pRight) {
×
644
  float    left = GET_FLOAT_VAL(pLeft);
×
645
  uint16_t right = GET_UINT16_VAL(pRight);
×
646
  if (left > right) return 1;
×
647
  if (left < right) return -1;
×
648
  return 0;
×
649
}
650

651
int32_t compareFloatUint32(const void *pLeft, const void *pRight) {
×
652
  float    left = GET_FLOAT_VAL(pLeft);
×
653
  uint32_t right = GET_UINT32_VAL(pRight);
×
654
  if (left > right) return 1;
×
655
  if (left < right) return -1;
×
656
  return 0;
×
657
}
658

659
int32_t compareFloatUint64(const void *pLeft, const void *pRight) {
×
660
  float    left = GET_FLOAT_VAL(pLeft);
×
661
  uint64_t right = GET_UINT64_VAL(pRight);
×
662
  if (left > right) return 1;
×
663
  if (left < right) return -1;
×
664
  return 0;
×
665
}
666

667
int32_t compareDoubleInt8(const void *pLeft, const void *pRight) {
×
668
  double left = GET_DOUBLE_VAL(pLeft);
×
669
  int8_t right = GET_INT8_VAL(pRight);
×
670
  if (left > right) return 1;
×
671
  if (left < right) return -1;
×
672
  return 0;
×
673
}
674

675
int32_t compareDoubleInt16(const void *pLeft, const void *pRight) {
×
676
  double  left = GET_DOUBLE_VAL(pLeft);
×
677
  int16_t right = GET_INT16_VAL(pRight);
×
678
  if (left > right) return 1;
×
679
  if (left < right) return -1;
×
680
  return 0;
×
681
}
682

683
int32_t compareDoubleInt32(const void *pLeft, const void *pRight) {
×
684
  double  left = GET_DOUBLE_VAL(pLeft);
×
685
  int32_t right = GET_INT32_VAL(pRight);
×
686
  if (left > right) return 1;
×
687
  if (left < right) return -1;
×
688
  return 0;
×
689
}
690

691
int32_t compareDoubleInt64(const void *pLeft, const void *pRight) {
×
692
  double  left = GET_DOUBLE_VAL(pLeft);
×
693
  int64_t right = GET_INT64_VAL(pRight);
×
694
  if (left > right) return 1;
×
695
  if (left < right) return -1;
×
696
  return 0;
×
697
}
698

699
int32_t compareDoubleFloat(const void *pLeft, const void *pRight) {
×
700
  double left = GET_DOUBLE_VAL(pLeft);
×
701
  float  right = GET_FLOAT_VAL(pRight);
×
702

703
  if (isnan(left) && isnan(right)) {
×
704
    return 0;
×
705
  }
706

707
  if (isnan(left)) {
×
708
    return -1;
×
709
  }
710

711
  if (isnan(right)) {
×
712
    return 1;
×
713
  }
714

715
  if (FLT_EQUAL(left, right)) {
×
716
    return 0;
×
717
  }
718
  return FLT_GREATER(left, right) ? 1 : -1;
×
719
}
720

721
int32_t compareDoubleUint8(const void *pLeft, const void *pRight) {
×
722
  double  left = GET_DOUBLE_VAL(pLeft);
×
723
  uint8_t right = GET_UINT8_VAL(pRight);
×
724
  if (left > right) return 1;
×
725
  if (left < right) return -1;
×
726
  return 0;
×
727
}
728

729
int32_t compareDoubleUint16(const void *pLeft, const void *pRight) {
×
730
  double   left = GET_DOUBLE_VAL(pLeft);
×
731
  uint16_t right = GET_UINT16_VAL(pRight);
×
732
  if (left > right) return 1;
×
733
  if (left < right) return -1;
×
734
  return 0;
×
735
}
736

737
int32_t compareDoubleUint32(const void *pLeft, const void *pRight) {
×
738
  double   left = GET_DOUBLE_VAL(pLeft);
×
739
  uint32_t right = GET_UINT32_VAL(pRight);
×
740
  if (left > right) return 1;
×
741
  if (left < right) return -1;
×
742
  return 0;
×
743
}
744

745
int32_t compareDoubleUint64(const void *pLeft, const void *pRight) {
×
746
  double   left = GET_DOUBLE_VAL(pLeft);
×
747
  uint64_t right = GET_UINT64_VAL(pRight);
×
748
  if (left > right) return 1;
×
749
  if (left < right) return -1;
×
750
  return 0;
×
751
}
752

753
int32_t compareUint8Int8(const void *pLeft, const void *pRight) {
1,348✔
754
  uint8_t left = GET_UINT8_VAL(pLeft);
1,348✔
755
  int8_t  right = GET_INT8_VAL(pRight);
1,348✔
756
  if (left > right) return 1;
1,348✔
757
  if (left < right) return -1;
231✔
758
  return 0;
11✔
759
}
760

761
int32_t compareUint8Int16(const void *pLeft, const void *pRight) {
×
762
  uint8_t left = GET_UINT8_VAL(pLeft);
×
763
  int16_t right = GET_INT16_VAL(pRight);
×
764
  if (left > right) return 1;
×
765
  if (left < right) return -1;
×
766
  return 0;
×
767
}
768

769
int32_t compareUint8Int32(const void *pLeft, const void *pRight) {
×
770
  uint8_t left = GET_UINT8_VAL(pLeft);
×
771
  int32_t right = GET_INT32_VAL(pRight);
×
772
  if (left > right) return 1;
×
773
  if (left < right) return -1;
×
774
  return 0;
×
775
}
776

777
int32_t compareUint8Int64(const void *pLeft, const void *pRight) {
×
778
  uint8_t left = GET_UINT8_VAL(pLeft);
×
779
  int64_t right = GET_INT64_VAL(pRight);
×
780
  if (left > right) return 1;
×
781
  if (left < right) return -1;
×
782
  return 0;
×
783
}
784

785
int32_t compareUint8Float(const void *pLeft, const void *pRight) {
×
786
  uint8_t left = GET_UINT8_VAL(pLeft);
×
787
  float   right = GET_FLOAT_VAL(pRight);
×
788
  if (left > right) return 1;
×
789
  if (left < right) return -1;
×
790
  return 0;
×
791
}
792

793
int32_t compareUint8Double(const void *pLeft, const void *pRight) {
×
794
  uint8_t left = GET_UINT8_VAL(pLeft);
×
795
  double  right = GET_DOUBLE_VAL(pRight);
×
796
  if (left > right) return 1;
×
797
  if (left < right) return -1;
×
798
  return 0;
×
799
}
800

801
int32_t compareUint8Uint16(const void *pLeft, const void *pRight) {
×
802
  uint8_t  left = GET_UINT8_VAL(pLeft);
×
803
  uint16_t right = GET_UINT16_VAL(pRight);
×
804
  if (left > right) return 1;
×
805
  if (left < right) return -1;
×
806
  return 0;
×
807
}
808

809
int32_t compareUint8Uint32(const void *pLeft, const void *pRight) {
×
810
  uint8_t  left = GET_UINT8_VAL(pLeft);
×
811
  uint32_t right = GET_UINT32_VAL(pRight);
×
812
  if (left > right) return 1;
×
813
  if (left < right) return -1;
×
814
  return 0;
×
815
}
816

817
int32_t compareUint8Uint64(const void *pLeft, const void *pRight) {
×
818
  uint8_t  left = GET_UINT8_VAL(pLeft);
×
819
  uint64_t right = GET_UINT64_VAL(pRight);
×
820
  if (left > right) return 1;
×
821
  if (left < right) return -1;
×
822
  return 0;
×
823
}
824

825
int32_t compareUint16Int8(const void *pLeft, const void *pRight) {
1,348✔
826
  uint16_t left = GET_UINT16_VAL(pLeft);
1,348✔
827
  int8_t   right = GET_INT8_VAL(pRight);
1,348✔
828
  if (left > right) return 1;
1,348✔
829
  if (left < right) return -1;
341✔
830
  return 0;
11✔
831
}
832

833
int32_t compareUint16Int16(const void *pLeft, const void *pRight) {
×
834
  uint16_t left = GET_UINT16_VAL(pLeft);
×
835
  int16_t  right = GET_INT16_VAL(pRight);
×
836
  if (left > right) return 1;
×
837
  if (left < right) return -1;
×
838
  return 0;
×
839
}
840

841
int32_t compareUint16Int32(const void *pLeft, const void *pRight) {
×
842
  uint16_t left = GET_UINT16_VAL(pLeft);
×
843
  int32_t  right = GET_INT32_VAL(pRight);
×
844
  if (left > right) return 1;
×
845
  if (left < right) return -1;
×
846
  return 0;
×
847
}
848

849
int32_t compareUint16Int64(const void *pLeft, const void *pRight) {
×
850
  uint16_t left = GET_UINT16_VAL(pLeft);
×
851
  int64_t  right = GET_INT64_VAL(pRight);
×
852
  if (left > right) return 1;
×
853
  if (left < right) return -1;
×
854
  return 0;
×
855
}
856

857
int32_t compareUint16Float(const void *pLeft, const void *pRight) {
×
858
  uint16_t left = GET_UINT16_VAL(pLeft);
×
859
  float    right = GET_FLOAT_VAL(pRight);
×
860
  if (left > right) return 1;
×
861
  if (left < right) return -1;
×
862
  return 0;
×
863
}
864

865
int32_t compareUint16Double(const void *pLeft, const void *pRight) {
×
866
  uint16_t left = GET_UINT16_VAL(pLeft);
×
867
  double   right = GET_DOUBLE_VAL(pRight);
×
868
  if (left > right) return 1;
×
869
  if (left < right) return -1;
×
870
  return 0;
×
871
}
872

873
int32_t compareUint16Uint8(const void *pLeft, const void *pRight) {
×
874
  uint16_t left = GET_UINT16_VAL(pLeft);
×
875
  uint8_t  right = GET_UINT8_VAL(pRight);
×
876
  if (left > right) return 1;
×
877
  if (left < right) return -1;
×
878
  return 0;
×
879
}
880

881
int32_t compareUint16Uint32(const void *pLeft, const void *pRight) {
×
882
  uint16_t left = GET_UINT16_VAL(pLeft);
×
883
  uint32_t right = GET_UINT32_VAL(pRight);
×
884
  if (left > right) return 1;
×
885
  if (left < right) return -1;
×
886
  return 0;
×
887
}
888

889
int32_t compareUint16Uint64(const void *pLeft, const void *pRight) {
1✔
890
  uint16_t left = GET_UINT16_VAL(pLeft);
1✔
891
  uint64_t right = GET_UINT64_VAL(pRight);
1✔
892
  if (left > right) return 1;
1✔
893
  if (left < right) return -1;
1✔
894
  return 0;
1✔
895
}
896

897
int32_t compareUint32Int8(const void *pLeft, const void *pRight) {
1,363✔
898
  uint32_t left = GET_UINT32_VAL(pLeft);
1,363✔
899
  int8_t   right = GET_INT8_VAL(pRight);
1,363✔
900
  if (right < 0) return 1;
1,363✔
901
  if (left > (uint32_t)right) return 1;
1,363✔
902
  if (left < (uint32_t)right) return -1;
54✔
903
  return 0;
3✔
904
}
905

906
int32_t compareUint32Int16(const void *pLeft, const void *pRight) {
×
907
  uint32_t left = GET_UINT32_VAL(pLeft);
×
908
  int16_t  right = GET_INT16_VAL(pRight);
×
909
  if (right < 0) return 1;
×
910
  if (left > (uint32_t)right) return 1;
×
911
  if (left < (uint32_t)right) return -1;
×
912
  return 0;
×
913
}
914

915
int32_t compareUint32Int32(const void *pLeft, const void *pRight) {
×
916
  uint32_t left = GET_UINT32_VAL(pLeft);
×
917
  int32_t  right = GET_INT32_VAL(pRight);
×
918
  if (right < 0) return 1;
×
919
  if (left > (uint32_t)right) return 1;
×
920
  if (left < (uint32_t)right) return -1;
×
921
  return 0;
×
922
}
923

924
int32_t compareUint32Int64(const void *pLeft, const void *pRight) {
×
925
  uint32_t left = GET_UINT32_VAL(pLeft);
×
926
  int64_t  right = GET_INT64_VAL(pRight);
×
927
  if (left > right) return 1;
×
928
  if (left < right) return -1;
×
929
  return 0;
×
930
}
931

932
int32_t compareUint32Float(const void *pLeft, const void *pRight) {
×
933
  uint32_t left = GET_UINT32_VAL(pLeft);
×
934
  float    right = GET_FLOAT_VAL(pRight);
×
935
  if (left > right) return 1;
×
936
  if (left < right) return -1;
×
937
  return 0;
×
938
}
939

940
int32_t compareUint32Double(const void *pLeft, const void *pRight) {
×
941
  uint32_t left = GET_UINT32_VAL(pLeft);
×
942
  double   right = GET_DOUBLE_VAL(pRight);
×
943
  if (left > right) return 1;
×
944
  if (left < right) return -1;
×
945
  return 0;
×
946
}
947

948
int32_t compareUint32Uint8(const void *pLeft, const void *pRight) {
×
949
  uint32_t left = GET_UINT32_VAL(pLeft);
×
950
  uint8_t  right = GET_UINT8_VAL(pRight);
×
951
  if (left > right) return 1;
×
952
  if (left < right) return -1;
×
953
  return 0;
×
954
}
955

956
int32_t compareUint32Uint16(const void *pLeft, const void *pRight) {
×
957
  uint32_t left = GET_UINT32_VAL(pLeft);
×
958
  uint16_t right = GET_UINT16_VAL(pRight);
×
959
  if (left > right) return 1;
×
960
  if (left < right) return -1;
×
961
  return 0;
×
962
}
963

964
int32_t compareUint32Uint64(const void *pLeft, const void *pRight) {
×
965
  uint32_t left = GET_UINT32_VAL(pLeft);
×
966
  uint64_t right = GET_UINT64_VAL(pRight);
×
967
  if (left > right) return 1;
×
968
  if (left < right) return -1;
×
969
  return 0;
×
970
}
971

972
int32_t compareUint64Int8(const void *pLeft, const void *pRight) {
1,348✔
973
  uint64_t left = GET_UINT64_VAL(pLeft);
1,348✔
974
  int8_t   right = GET_INT8_VAL(pRight);
1,348✔
975
  if (right < 0) return 1;
1,348✔
976
  if (left > (uint64_t)right) return 1;
1,348✔
977
  if (left < (uint64_t)right) return -1;
42✔
978
  return 0;
2✔
979
}
980

981
int32_t compareUint64Int16(const void *pLeft, const void *pRight) {
×
982
  uint64_t left = GET_UINT64_VAL(pLeft);
×
983
  int16_t  right = GET_INT16_VAL(pRight);
×
984
  if (right < 0) return 1;
×
985
  if (left > (uint64_t)right) return 1;
×
986
  if (left < (uint64_t)right) return -1;
×
987
  return 0;
×
988
}
989

990
int32_t compareUint64Int32(const void *pLeft, const void *pRight) {
×
991
  uint64_t left = GET_UINT64_VAL(pLeft);
×
992
  int32_t  right = GET_INT32_VAL(pRight);
×
993
  if (right < 0) return 1;
×
994
  if (left > (uint64_t)right) return 1;
×
995
  if (left < (uint64_t)right) return -1;
×
996
  return 0;
×
997
}
998

999
int32_t compareUint64Int64(const void *pLeft, const void *pRight) {
×
1000
  uint64_t left = GET_UINT64_VAL(pLeft);
×
1001
  int64_t  right = GET_INT64_VAL(pRight);
×
1002
  if (right < 0) return 1;
×
1003
  if (left > (uint64_t)right) return 1;
×
1004
  if (left < (uint64_t)right) return -1;
×
1005
  return 0;
×
1006
}
1007

1008
int32_t compareUint64Float(const void *pLeft, const void *pRight) {
×
1009
  uint64_t left = GET_UINT64_VAL(pLeft);
×
1010
  float    right = GET_FLOAT_VAL(pRight);
×
1011
  if (left > right) return 1;
×
1012
  if (left < right) return -1;
×
1013
  return 0;
×
1014
}
1015

1016
int32_t compareUint64Double(const void *pLeft, const void *pRight) {
×
1017
  uint64_t left = GET_UINT64_VAL(pLeft);
×
1018
  double   right = GET_DOUBLE_VAL(pRight);
×
1019
  if (left > right) return 1;
×
1020
  if (left < right) return -1;
×
1021
  return 0;
×
1022
}
1023

1024
int32_t compareUint64Uint8(const void *pLeft, const void *pRight) {
×
1025
  uint64_t left = GET_UINT64_VAL(pLeft);
×
1026
  uint8_t  right = GET_UINT8_VAL(pRight);
×
1027
  if (left > right) return 1;
×
1028
  if (left < right) return -1;
×
1029
  return 0;
×
1030
}
1031

1032
int32_t compareUint64Uint16(const void *pLeft, const void *pRight) {
×
1033
  uint64_t left = GET_UINT64_VAL(pLeft);
×
1034
  uint16_t right = GET_UINT16_VAL(pRight);
×
1035
  if (left > right) return 1;
×
1036
  if (left < right) return -1;
×
1037
  return 0;
×
1038
}
1039

1040
int32_t compareUint64Uint32(const void *pLeft, const void *pRight) {
×
1041
  uint64_t left = GET_UINT64_VAL(pLeft);
×
1042
  uint32_t right = GET_UINT32_VAL(pRight);
×
1043
  if (left > right) return 1;
×
1044
  if (left < right) return -1;
×
1045
  return 0;
×
1046
}
1047

1048
int32_t compareDecimal64(const void* pleft, const void* pright) {
×
1049
  if (decimal64Compare(OP_TYPE_GREATER_THAN, pleft, pright)) return 1;
×
1050
  if (decimal64Compare(OP_TYPE_LOWER_THAN, pleft, pright)) return -1;
×
1051
  return 0;
×
1052
}
1053

1054
int32_t compareDecimal128(const void* pleft, const void* pright) {
×
1055
  if (decimalCompare(OP_TYPE_GREATER_THAN, pleft, pright)) return 1;
×
1056
  if (decimalCompare(OP_TYPE_LOWER_THAN, pleft, pright)) return -1;
×
1057
  return 0;
×
1058
}
1059

1060
int32_t compareDecimal64SameScale(const void* pleft, const void* pright) {
×
1061
  const SDecimalOps* pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
×
1062
  if (pOps->gt(pleft, pright, DECIMAL_WORD_NUM(Decimal64))) return 1;
×
1063
  if (pOps->lt(pleft, pright, DECIMAL_WORD_NUM(Decimal64))) return -1;
×
1064
  return 0;
×
1065
}
1066

1067
int32_t compareDecimal64SameScaleDesc(const void* pLeft, const void* pRight) {
×
1068
  const SDecimalOps* pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL64);
×
1069
  if (pOps->lt(pLeft, pRight, DECIMAL_WORD_NUM(Decimal64))) return 1;
×
1070
  if (pOps->gt(pLeft, pRight, DECIMAL_WORD_NUM(Decimal64))) return -1;
×
1071
  return 0;
×
1072
}
1073

1074
int32_t compareDecimal128SameScale(const void* pleft, const void* pright) {
×
1075
  const SDecimalOps* pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
×
1076
  if (pOps->gt(pleft, pright, DECIMAL_WORD_NUM(Decimal))) return 1;
×
1077
  if (pOps->lt(pleft, pright, DECIMAL_WORD_NUM(Decimal))) return -1;
×
1078
  return 0;
×
1079
}
1080

1081
int32_t compareDecimal128SameScaleDesc(const void* pLeft, const void* pRight) {
×
1082
  const SDecimalOps* pOps = getDecimalOps(TSDB_DATA_TYPE_DECIMAL);
×
1083
  if (pOps->lt(pLeft, pRight, DECIMAL_WORD_NUM(Decimal))) return 1;
×
1084
  if (pOps->gt(pLeft, pRight, DECIMAL_WORD_NUM(Decimal))) return -1;
×
1085
  return 0;
×
1086
}
1087

1088
int32_t compareJsonValDesc(const void *pLeft, const void *pRight) { return compareJsonVal(pRight, pLeft); }
×
1089

1090
/*
1091
 * Compare two strings
1092
 *    TSDB_MATCH:            Match
1093
 *    TSDB_NOMATCH:          No match
1094
 *    TSDB_NOWILDCARDMATCH:  No match in spite of having * or % wildcards.
1095
 * Like matching rules:
1096
 *      '%': Matches zero or more characters
1097
 *      '_': Matches one character
1098
 *
1099
 */
1100
int32_t patternMatch(const char *pattern, size_t psize, const char *str, size_t ssize,
2,791✔
1101
                     const SPatternCompareInfo *pInfo) {
1102
  char c, c1;
1103

1104
  int32_t i = 0;
2,791✔
1105
  int32_t j = 0;
2,791✔
1106
  int32_t nMatchChar = 0;
2,791✔
1107

1108
  while ((i < psize) && ((c = pattern[i++]) != 0)) {
2,888✔
1109
    if (c == pInfo->matchAll) { /* Match "*" */
2,852✔
1110

1111
      while ((i < psize) && ((c = pattern[i++]) == pInfo->matchAll || c == pInfo->matchOne)) {
1,406✔
1112
        if (c == pInfo->matchOne) {
36✔
1113
          if (j >= ssize || str[j++] == 0) {  // empty string, return not match
36✔
1114
            return TSDB_PATTERN_NOWILDCARDMATCH;
2✔
1115
          } else {
1116
            ++nMatchChar;
34✔
1117
          }
1118
        }
1119
      }
1120

1121
      if (i >= psize && (c == pInfo->umatchOne || c == pInfo->umatchAll)) {
1,370✔
1122
        return TSDB_PATTERN_MATCH; /* "*" at the end of the pattern matches */
6✔
1123
      }
1124

1125
      if (c == '\\' && (pattern[i] == '_' || pattern[i] == '%')) {
1,364✔
1126
        c = pattern[i];
2✔
1127
        i++;
2✔
1128
      }
1129

1130
      char rejectList[2] = {toupper(c), tolower(c)};
1,364✔
1131

1132
      str += nMatchChar;
1,364✔
1133
      int32_t remain = ssize - nMatchChar;
1,364✔
1134
      while (1) {
1,356✔
1135
        size_t n = tstrncspn(str, remain, rejectList, 2);
2,720✔
1136

1137
        str += n;
2,720✔
1138
        remain -= n;
2,720✔
1139

1140
        if ((remain <= 0) || str[0] == 0) {
2,720✔
1141
          break;
1142
        }
1143

1144
        int32_t ret = patternMatch(&pattern[i], psize - i, ++str, --remain, pInfo);
1,364✔
1145
        if (ret != TSDB_PATTERN_NOMATCH) {
1,364✔
1146
          return ret;
8✔
1147
        }
1148
      }
1149

1150
      return TSDB_PATTERN_NOWILDCARDMATCH;
1,356✔
1151
    }
1152

1153
    if (j < ssize) {
1,480✔
1154
      c1 = str[j++];
1,475✔
1155
      ++nMatchChar;
1,475✔
1156

1157
      if (c == '\\' && (pattern[i] == '_' || pattern[i] == '%')) {
1,475✔
1158
        if (c1 != pattern[i]) {
7✔
1159
          return TSDB_PATTERN_NOMATCH;
1✔
1160
        } else {
1161
          i++;
6✔
1162
          continue;
6✔
1163
        }
1164
      }
1165

1166
      if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) {
1,468✔
1167
        continue;
91✔
1168
      }
1169
    }
1170

1171
    return TSDB_PATTERN_NOMATCH;
1,382✔
1172
  }
1173

1174
  return (j >= ssize || str[j] == 0) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH;
36✔
1175
}
1176

1177
int32_t rawStrPatternMatch(const char *str, const char *pattern) {
×
1178
  SPatternCompareInfo pInfo = PATTERN_COMPARE_INFO_INITIALIZER;
×
1179

1180
  size_t pLen = strlen(pattern);
×
1181
  size_t sz = strlen(str);
×
1182
  if (pLen > TSDB_MAX_FIELD_LEN) {
×
1183
    return 1;
×
1184
  }
1185

1186
  int32_t ret = patternMatch(pattern, pLen, str, sz, &pInfo);
×
1187
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
×
1188
}
1189

1190
int32_t wcsPatternMatch(const TdUcs4 *pattern, size_t psize, const TdUcs4 *str, size_t ssize,
49✔
1191
                        const SPatternCompareInfo *pInfo) {
1192
  TdUcs4 c, c1;
1193

1194
  int32_t i = 0;
49✔
1195
  int32_t j = 0;
49✔
1196
  int32_t nMatchChar = 0;
49✔
1197

1198
  while ((i < psize) && ((c = pattern[i++]) != 0)) {
76✔
1199
    if (c == pInfo->umatchAll) { /* Match "%" */
60✔
1200

1201
      while ((i < psize) && ((c = pattern[i++]) == pInfo->umatchAll || c == pInfo->umatchOne)) {
59✔
1202
        if (c == pInfo->umatchOne) {
36✔
1203
          if (j >= ssize || str[j++] == 0) {
36✔
1204
            return TSDB_PATTERN_NOWILDCARDMATCH;
2✔
1205
          } else {
1206
            ++nMatchChar;
34✔
1207
          }
1208
        }
1209
      }
1210

1211
      if (i >= psize && (c == pInfo->umatchOne || c == pInfo->umatchAll)) {
23✔
1212
        return TSDB_PATTERN_MATCH;
6✔
1213
      }
1214

1215
      if (c == '\\' && (pattern[i] == '_' || pattern[i] == '%')) {
17✔
1216
        c = pattern[i];
4✔
1217
        i++;
4✔
1218
      }
1219

1220
      TdUcs4 rejectList[2] = {towupper(c), towlower(c)};
17✔
1221

1222
      str += nMatchChar;
17✔
1223
      int32_t remain = ssize - nMatchChar;
17✔
1224
      while (1) {
8✔
1225
        size_t n = twcsncspn(str, remain, rejectList, 2);
25✔
1226

1227
        str += n;
25✔
1228
        remain -= n;
25✔
1229

1230
        if ((remain <= 0) || str[0] == 0) {
25✔
1231
          break;
1232
        }
1233

1234
        int32_t ret = wcsPatternMatch(&pattern[i], psize - i, ++str, --remain, pInfo);
17✔
1235
        if (ret != TSDB_PATTERN_NOMATCH) {
17✔
1236
          return ret;
9✔
1237
        }
1238
      }
1239

1240
      return TSDB_PATTERN_NOWILDCARDMATCH;
8✔
1241
    }
1242

1243
    if (j < ssize) {
35✔
1244
      c1 = str[j++];
29✔
1245
      nMatchChar++;
29✔
1246

1247
      if (c == '\\' && (pattern[i] == '_' || pattern[i] == '%')) {
29✔
1248
        if (c1 != pattern[i]) {
2✔
1249
          return TSDB_PATTERN_NOMATCH;
×
1250
        } else {
1251
          i++;
2✔
1252
          continue;
2✔
1253
        }
1254
      }
1255

1256
      if (c == c1 || towlower(c) == towlower(c1) || (c == pInfo->umatchOne && c1 != 0)) {
27✔
1257
        continue;
25✔
1258
      }
1259
    }
1260

1261
    return TSDB_PATTERN_NOMATCH;
8✔
1262
  }
1263

1264
  return (j >= ssize || str[j] == 0) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH;
16✔
1265
}
1266

1267
int32_t comparestrRegexNMatch(const void *pLeft, const void *pRight) {
2✔
1268
  return comparestrRegexMatch(pLeft, pRight) ? 0 : 1;
2✔
1269
}
1270

1271
typedef struct UsingRegex {
1272
  regex_t pRegex;
1273
  int32_t lastUsedTime;
1274
} UsingRegex;
1275
typedef UsingRegex* HashRegexPtr;
1276

1277
typedef struct RegexCache {
1278
  SHashObj *regexHash;
1279
  void     *regexCacheTmr;
1280
  void     *timer;
1281
  SRWLatch  mutex;
1282
  bool      exit;
1283
  int8_t    inited;
1284
} RegexCache;
1285
static RegexCache sRegexCache;
1286
#define MAX_REGEX_CACHE_SIZE   20
1287
#define REGEX_CACHE_CLEAR_TIME 30
1288

1289
static void checkRegexCache(void* param, void* tmrId) {
245✔
1290
  int32_t  code = 0;
245✔
1291
  taosRLockLatch(&sRegexCache.mutex);
245✔
1292
  if(sRegexCache.exit) {
245✔
1293
    goto _exit;
×
1294
  }
1295
  bool stopped = taosTmrReset(checkRegexCache, REGEX_CACHE_CLEAR_TIME * 1000, param, sRegexCache.regexCacheTmr, &tmrId);
245✔
1296
  if (stopped) {
245✔
1297
    uError("failed to reset regex cache timer");
×
1298
    goto _exit;
×
1299
  }
1300
  if (taosHashGetSize(sRegexCache.regexHash) < MAX_REGEX_CACHE_SIZE) {
245✔
1301
    goto _exit;
245✔
1302
  }
1303

1304
  if (taosHashGetSize(sRegexCache.regexHash) >= MAX_REGEX_CACHE_SIZE) {
×
1305
    UsingRegex **ppUsingRegex = taosHashIterate(sRegexCache.regexHash, NULL);
×
1306
    while ((ppUsingRegex != NULL)) {
×
1307
      if (taosGetTimestampSec() - (*ppUsingRegex)->lastUsedTime > REGEX_CACHE_CLEAR_TIME) {
×
1308
        size_t len = 0;
×
1309
        char* key = (char*)taosHashGetKey(ppUsingRegex, &len);
×
1310
        if (TSDB_CODE_SUCCESS != taosHashRemove(sRegexCache.regexHash, key, len)) {
×
1311
          uError("failed to remove regex pattern %s from cache", key);
×
1312
          goto _exit;
×
1313
        }
1314
      }
1315
      ppUsingRegex = taosHashIterate(sRegexCache.regexHash, ppUsingRegex);
×
1316
    }
1317
  }
1318
_exit:
×
1319
  taosRUnLockLatch(&sRegexCache.mutex);
245✔
1320
}
245✔
1321

1322
void regexCacheFree(void *ppUsingRegex) {
8✔
1323
  regfree(&(*(UsingRegex **)ppUsingRegex)->pRegex);
8✔
1324
  taosMemoryFree(*(UsingRegex **)ppUsingRegex);
8✔
1325
}
8✔
1326

1327
int32_t InitRegexCache() {
476✔
1328
  #ifdef WINDOWS
1329
    return 0;
1330
  #endif
1331
  if (atomic_val_compare_exchange_8(&sRegexCache.inited, 0, 1) != 0) return TSDB_CODE_SUCCESS;
476✔
1332
  sRegexCache.regexHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
476✔
1333
  if (sRegexCache.regexHash == NULL) {
476✔
1334
    uError("failed to create RegexCache");
×
1335
    return terrno;
×
1336
  }
1337
  taosHashSetFreeFp(sRegexCache.regexHash, regexCacheFree);
476✔
1338
  sRegexCache.regexCacheTmr = taosTmrInit(0, 0, 0, "REGEXCACHE");
476✔
1339
  if (sRegexCache.regexCacheTmr == NULL) {
476✔
1340
    uError("failed to create regex cache check timer");
×
1341
    return terrno;
×
1342
  }
1343

1344
  sRegexCache.exit = false;
476✔
1345
  taosInitRWLatch(&sRegexCache.mutex);
476✔
1346
  sRegexCache.timer = taosTmrStart(checkRegexCache, REGEX_CACHE_CLEAR_TIME * 1000, NULL, sRegexCache.regexCacheTmr);
476✔
1347
  if (sRegexCache.timer == NULL) {
476✔
1348
    uError("failed to start regex cache timer");
×
1349
    return terrno;
×
1350
  }
1351

1352
  return TSDB_CODE_SUCCESS;
476✔
1353
}
1354

1355
void DestroyRegexCache(){
476✔
1356
  #ifdef WINDOWS
1357
    return;
1358
  #endif
1359
  int32_t code = 0;
476✔
1360
  uInfo("[regex cache] destory regex cache");
476✔
1361
  bool ret = taosTmrStopA(&sRegexCache.timer);
476✔
1362
  if (!ret) {
476✔
1363
    uInfo("stop regex cache timer may be failed");
66✔
1364
  }
1365
  taosWLockLatch(&sRegexCache.mutex);
476✔
1366
  sRegexCache.exit = true;
476✔
1367
  taosWUnLockLatch(&sRegexCache.mutex);
476✔
1368
  taosHashCleanup(sRegexCache.regexHash);
476✔
1369
  sRegexCache.regexHash = NULL;
476✔
1370
  taosTmrCleanUp(sRegexCache.regexCacheTmr);
476✔
1371
  sRegexCache.regexCacheTmr = NULL;
476✔
1372
}
476✔
1373

1374
int32_t checkRegexPattern(const char *pPattern) {
×
1375
  if (pPattern == NULL) {
×
1376
    return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
×
1377
  }
1378

1379
  regex_t regex;
1380
  int32_t cflags = REG_EXTENDED;
×
1381
  int32_t ret = regcomp(&regex, pPattern, cflags);
×
1382
  if (ret != 0) {
×
1383
    char msgbuf[256] = {0};
×
1384
    (void)regerror(ret, &regex, msgbuf, tListLen(msgbuf));
×
1385
    uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf);
×
1386
    return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
×
1387
  }
1388
  regfree(&regex);
×
1389
  return TSDB_CODE_SUCCESS;
×
1390
}
1391

1392
int32_t getRegComp(const char *pPattern, HashRegexPtr **regexRet) {
700,209✔
1393
  HashRegexPtr* ppUsingRegex = (HashRegexPtr*)taosHashAcquire(sRegexCache.regexHash, pPattern, strlen(pPattern));
700,209✔
1394
  if (ppUsingRegex != NULL) {
700,209✔
1395
    (*ppUsingRegex)->lastUsedTime = taosGetTimestampSec();
700,201✔
1396
    *regexRet = ppUsingRegex;
700,201✔
1397
    return TSDB_CODE_SUCCESS;
700,201✔
1398
  }
1399
  UsingRegex *pUsingRegex = taosMemoryMalloc(sizeof(UsingRegex));
8✔
1400
  if (pUsingRegex == NULL) {
8✔
1401
    uError("Failed to Malloc when compile regex pattern %s.", pPattern);
×
1402
    return terrno;
×
1403
  }
1404
  int32_t cflags = REG_EXTENDED;
8✔
1405
  int32_t ret = regcomp(&pUsingRegex->pRegex, pPattern, cflags);
8✔
1406
  if (ret != 0) {
8✔
1407
    char msgbuf[256] = {0};
×
1408
    (void)regerror(ret, &pUsingRegex->pRegex, msgbuf, tListLen(msgbuf));
×
1409
    uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf);
×
1410
    taosMemoryFree(pUsingRegex);
×
1411
    return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
×
1412
  }
1413

1414
  while (true) {
×
1415
    int code = taosHashPut(sRegexCache.regexHash, pPattern, strlen(pPattern), &pUsingRegex, sizeof(UsingRegex *));
8✔
1416
    if (code != 0 && code != TSDB_CODE_DUP_KEY) {
8✔
1417
      regexCacheFree(&pUsingRegex);
×
1418
      uError("Failed to put regex pattern %s into cache, exception internal error.", pPattern);
×
1419
      return code;
×
1420
    } else if (code == TSDB_CODE_DUP_KEY) {
8✔
1421
      terrno = 0;
×
1422
    }
1423
    ppUsingRegex = (UsingRegex **)taosHashAcquire(sRegexCache.regexHash, pPattern, strlen(pPattern));
8✔
1424
    if (ppUsingRegex) {
8✔
1425
      if (*ppUsingRegex != pUsingRegex) {
8✔
1426
        regexCacheFree(&pUsingRegex);
×
1427
      }
1428
      pUsingRegex = (*ppUsingRegex);
8✔
1429
      break;
8✔
1430
    } else {
1431
      continue;
×
1432
    }
1433
  }
1434
  pUsingRegex->lastUsedTime = taosGetTimestampSec();
8✔
1435
  *regexRet = ppUsingRegex;
8✔
1436
  return TSDB_CODE_SUCCESS;
8✔
1437
}
1438

1439
void releaseRegComp(UsingRegex  **regex){
200,207✔
1440
  taosHashRelease(sRegexCache.regexHash, regex);
200,207✔
1441
}
200,207✔
1442

1443
static threadlocal UsingRegex ** ppUsingRegex;
1444
static threadlocal regex_t * pRegex;
1445
static threadlocal char    *pOldPattern = NULL;
1446

1447
#ifdef WINDOWS
1448
static threadlocal regex_t gRegex;
1449

1450
void DestoryThreadLocalRegComp() {
1451
  if (NULL != pOldPattern) {
1452
    regfree(&gRegex);
1453
    taosMemoryFree(pOldPattern);
1454
    pOldPattern = NULL;
1455
  }
1456
}
1457

1458
int32_t threadGetRegComp(regex_t **regex, const char *pPattern) {
1459
  if (NULL != pOldPattern) {
1460
    if (strcmp(pOldPattern, pPattern) == 0) {
1461
      *regex = &gRegex;
1462
      return 0;
1463
    } else {
1464
      DestoryThreadLocalRegComp();
1465
    }
1466
  }
1467
  pOldPattern = taosStrdup(pPattern);
1468
  if (NULL == pOldPattern) {
1469
    uError("Failed to Malloc when compile regex pattern %s.", pPattern);
1470
    return terrno;
1471
  }
1472
  int32_t cflags = REG_EXTENDED;
1473
  int32_t ret = regcomp(&gRegex, pPattern, cflags);
1474
  if (ret != 0) {
1475
    char msgbuf[256] = {0};
1476
    (void)regerror(ret, &gRegex, msgbuf, tListLen(msgbuf));
1477
    uError("Failed to compile regex pattern %s. reason %s", pPattern, msgbuf);
1478
    taosMemoryFree(pOldPattern);
1479
    pOldPattern = NULL;
1480
    return TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR;
1481
  }
1482
  *regex = &gRegex;
1483
  return 0;
1484
}
1485
#else
1486
void DestoryThreadLocalRegComp() {
241,880✔
1487
  if (NULL != pOldPattern) {
241,880✔
1488
    releaseRegComp(ppUsingRegex);
200,207✔
1489
    taosMemoryFree(pOldPattern);
200,207✔
1490
    ppUsingRegex = NULL;
200,207✔
1491
    pRegex = NULL;
200,207✔
1492
    pOldPattern = NULL;
200,207✔
1493
  }
1494
}
241,880✔
1495

1496
int32_t threadGetRegComp(regex_t **regex, const char *pPattern) {
600,008✔
1497
  if (NULL != pOldPattern) {
600,008✔
1498
    if (strcmp(pOldPattern, pPattern) == 0) {
600,006✔
1499
      *regex = pRegex;
399,799✔
1500
      return 0;
399,799✔
1501
    } else {
1502
      DestoryThreadLocalRegComp();
200,207✔
1503
    }
1504
  }
1505

1506
  HashRegexPtr *ppRegex = NULL;
200,209✔
1507
  int32_t code = getRegComp(pPattern, &ppRegex);
200,209✔
1508
  if (code != TSDB_CODE_SUCCESS) {
200,209✔
1509
    return code;
×
1510
  }
1511
  pOldPattern = taosStrdup(pPattern);
200,209✔
1512
  if (NULL == pOldPattern) {
200,209✔
1513
    uError("Failed to Malloc when compile regex pattern %s.", pPattern);
×
1514
    return terrno;
×
1515
  }
1516
  ppUsingRegex = ppRegex;
200,209✔
1517
  pRegex = &((*ppUsingRegex)->pRegex);
200,209✔
1518
  *regex = &(*ppRegex)->pRegex;
200,209✔
1519
  return 0;
200,209✔
1520
}
1521
#endif
1522

1523
static int32_t doExecRegexMatch(const char *pString, const char *pPattern) {
8✔
1524
  int32_t ret = 0;
8✔
1525
  char    msgbuf[256] = {0};
8✔
1526

1527
  regex_t *regex = NULL;
8✔
1528
  ret = threadGetRegComp(&regex, pPattern);
8✔
1529
  if (ret != 0) {
8✔
1530
    return ret;
×
1531
  }
1532

1533
  regmatch_t pmatch[1];
1534
  ret = regexec(regex, pString, 1, pmatch, 0);
8✔
1535
  if (ret != 0 && ret != REG_NOMATCH) {
8✔
1536
    terrno =  TSDB_CODE_PAR_REGULAR_EXPRESSION_ERROR; 
×
1537
    (void)regerror(ret, regex, msgbuf, sizeof(msgbuf));
×
1538
    uDebug("Failed to match %s with pattern %s, reason %s", pString, pPattern, msgbuf)
×
1539
  }
1540

1541
  return (ret == 0) ? 0 : 1;
8✔
1542
}
1543

1544
int32_t comparestrRegexMatch(const void *pLeft, const void *pRight) {
4✔
1545
  size_t sz = varDataLen(pRight);
4✔
1546
  char  *pattern = taosMemoryMalloc(sz + 1);
4✔
1547
  if (NULL == pattern) {
4✔
1548
    return 1;  // terrno has been set
×
1549
  }
1550

1551
  (void)memcpy(pattern, varDataVal(pRight), varDataLen(pRight));
4✔
1552
  pattern[sz] = 0;
4✔
1553

1554
  sz = varDataLen(pLeft);
4✔
1555
  char *str = taosMemoryMalloc(sz + 1);
4✔
1556
  if (NULL == str) {
4✔
1557
    taosMemoryFree(pattern);
×
1558
    return 1;  // terrno has been set
×
1559
  }
1560

1561
  (void)memcpy(str, varDataVal(pLeft), sz);
4✔
1562
  str[sz] = 0;
4✔
1563

1564
  int32_t ret = doExecRegexMatch(str, pattern);
4✔
1565

1566
  taosMemoryFree(str);
4✔
1567
  taosMemoryFree(pattern);
4✔
1568

1569
  return (ret == 0) ? 0 : 1;
4✔
1570
}
1571

1572
int32_t comparewcsRegexMatch(const void *pString, const void *pPattern) {
4✔
1573
  size_t len = varDataLen(pPattern);
4✔
1574
  char  *pattern = taosMemoryMalloc(len + 1);
4✔
1575
  if (NULL == pattern) {
4✔
1576
    return 1;  // terrno has been set
×
1577
  }
1578

1579
  int convertLen = taosUcs4ToMbs((TdUcs4 *)varDataVal(pPattern), len, pattern, NULL);
4✔
1580
  if (convertLen < 0) {
4✔
1581
    taosMemoryFree(pattern);
×
1582
    return 1; // terrno has been set
×
1583
  }
1584

1585
  pattern[convertLen] = 0;
4✔
1586

1587
  len = varDataLen(pString);
4✔
1588
  char *str = taosMemoryMalloc(len + 1);
4✔
1589
  if (NULL == str) {
4✔
1590
    taosMemoryFree(pattern);
×
1591
    return 1; // terrno has been set
×
1592
  }
1593

1594
  convertLen = taosUcs4ToMbs((TdUcs4 *)varDataVal(pString), len, str, NULL);
4✔
1595
  if (convertLen < 0) {
4✔
1596
    taosMemoryFree(str);
×
1597
    taosMemoryFree(pattern);
×
1598
    return 1; // terrno has been set
×
1599
  }
1600

1601
  str[convertLen] = 0;
4✔
1602

1603
  int32_t ret = doExecRegexMatch(str, pattern);
4✔
1604

1605
  taosMemoryFree(str);
4✔
1606
  taosMemoryFree(pattern);
4✔
1607

1608
  return (ret == 0) ? 0 : 1;
4✔
1609
}
1610

1611
int32_t comparewcsRegexNMatch(const void *pLeft, const void *pRight) {
2✔
1612
  return comparewcsRegexMatch(pLeft, pRight) ? 0 : 1;
2✔
1613
}
1614

1615
int32_t taosArrayCompareString(const void *a, const void *b) {
18✔
1616
  const char *x = *(const char **)a;
18✔
1617
  const char *y = *(const char **)b;
18✔
1618

1619
  return strcmp(x, y);
18✔
1620
}
1621

1622
int32_t comparestrPatternMatch(const void *pLeft, const void *pRight) {
1,396✔
1623
  SPatternCompareInfo pInfo = PATTERN_COMPARE_INFO_INITIALIZER;
1,396✔
1624

1625
  if (varDataTLen(pRight) > TSDB_MAX_FIELD_LEN) {
1,396✔
1626
    return 1;
×
1627
  }
1628
  size_t pLen = varDataLen(pRight);
1,396✔
1629
  size_t sz = varDataLen(pLeft);
1,396✔
1630

1631
  int32_t ret = patternMatch(varDataVal(pRight), pLen, varDataVal(pLeft), sz, &pInfo);
1,396✔
1632
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
1,396✔
1633
}
1634

1635
int32_t comparestrPatternNMatch(const void *pLeft, const void *pRight) {
2✔
1636
  return comparestrPatternMatch(pLeft, pRight) ? 0 : 1;
2✔
1637
}
1638

1639
int32_t comparewcsPatternMatch(const void *pLeft, const void *pRight) {
4✔
1640
  SPatternCompareInfo pInfo = PATTERN_COMPARE_INFO_INITIALIZER;
4✔
1641

1642
  size_t psize = varDataLen(pRight);
4✔
1643

1644
  int32_t ret = wcsPatternMatch((TdUcs4 *)varDataVal(pRight), psize / TSDB_NCHAR_SIZE, (TdUcs4 *)varDataVal(pLeft),
4✔
1645
                                varDataLen(pLeft) / TSDB_NCHAR_SIZE, &pInfo);
4✔
1646
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
4✔
1647
}
1648

1649
int32_t comparewcsPatternNMatch(const void *pLeft, const void *pRight) {
2✔
1650
  return comparewcsPatternMatch(pLeft, pRight) ? 0 : 1;
2✔
1651
}
1652

1653
__compar_fn_t getComparFunc(int32_t type, int32_t optr) {
208,240✔
1654
  __compar_fn_t comparFn = NULL;
208,240✔
1655

1656
  if (optr == OP_TYPE_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_VARBINARY &&
208,240✔
1657
                             type != TSDB_DATA_TYPE_NCHAR && type != TSDB_DATA_TYPE_GEOMETRY)) {
×
1658
    switch (type) {
×
1659
      case TSDB_DATA_TYPE_BOOL:
×
1660
      case TSDB_DATA_TYPE_TINYINT:
1661
      case TSDB_DATA_TYPE_UTINYINT:
1662
        return setChkInBytes1;
×
1663
      case TSDB_DATA_TYPE_SMALLINT:
×
1664
      case TSDB_DATA_TYPE_USMALLINT:
1665
        return setChkInBytes2;
×
1666
      case TSDB_DATA_TYPE_INT:
×
1667
      case TSDB_DATA_TYPE_UINT:
1668
      case TSDB_DATA_TYPE_FLOAT:
1669
        return setChkInBytes4;
×
1670
      case TSDB_DATA_TYPE_BIGINT:
×
1671
      case TSDB_DATA_TYPE_UBIGINT:
1672
      case TSDB_DATA_TYPE_DOUBLE:
1673
      case TSDB_DATA_TYPE_TIMESTAMP:
1674
        return setChkInBytes8;
×
1675
      default:
×
1676
        uError("getComparFunc data type unexpected type:%d, optr:%d", type, optr);
×
1677
        terrno = TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
1678
        return NULL;
×
1679
    }
1680
  }
1681

1682
  if (optr == OP_TYPE_NOT_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_VARBINARY &&
208,240✔
1683
                                 type != TSDB_DATA_TYPE_NCHAR && type != TSDB_DATA_TYPE_GEOMETRY)) {
×
1684
    switch (type) {
×
1685
      case TSDB_DATA_TYPE_BOOL:
×
1686
      case TSDB_DATA_TYPE_TINYINT:
1687
      case TSDB_DATA_TYPE_UTINYINT:
1688
        return setChkNotInBytes1;
×
1689
      case TSDB_DATA_TYPE_SMALLINT:
×
1690
      case TSDB_DATA_TYPE_USMALLINT:
1691
        return setChkNotInBytes2;
×
1692
      case TSDB_DATA_TYPE_INT:
×
1693
      case TSDB_DATA_TYPE_UINT:
1694
      case TSDB_DATA_TYPE_FLOAT:
1695
        return setChkNotInBytes4;
×
1696
      case TSDB_DATA_TYPE_BIGINT:
×
1697
      case TSDB_DATA_TYPE_UBIGINT:
1698
      case TSDB_DATA_TYPE_DOUBLE:
1699
      case TSDB_DATA_TYPE_TIMESTAMP:
1700
        return setChkNotInBytes8;
×
1701
      default:
×
1702
        uError("getComparFunc data type unexpected type:%d, optr:%d", type, optr);
×
1703
        terrno = TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
×
1704
        return NULL;
×
1705
    }
1706
  }
1707

1708
  switch (type) {
208,240✔
1709
    case TSDB_DATA_TYPE_BOOL:
21,410✔
1710
    case TSDB_DATA_TYPE_TINYINT:
1711
      comparFn = compareInt8Val;
21,410✔
1712
      break;
21,410✔
1713
    case TSDB_DATA_TYPE_SMALLINT:
17,242✔
1714
      comparFn = compareInt16Val;
17,242✔
1715
      break;
17,242✔
1716
    case TSDB_DATA_TYPE_INT:
20,703✔
1717
      comparFn = compareInt32Val;
20,703✔
1718
      break;
20,703✔
1719
    case TSDB_DATA_TYPE_BIGINT:
16,737✔
1720
    case TSDB_DATA_TYPE_TIMESTAMP:
1721
      comparFn = compareInt64Val;
16,737✔
1722
      break;
16,737✔
1723
    case TSDB_DATA_TYPE_FLOAT:
7,495✔
1724
      comparFn = compareFloatVal;
7,495✔
1725
      break;
7,495✔
1726
    case TSDB_DATA_TYPE_DOUBLE:
43,039✔
1727
      comparFn = compareDoubleVal;
43,039✔
1728
      break;
43,039✔
1729
    case TSDB_DATA_TYPE_VARBINARY:
×
1730
      if (optr == OP_TYPE_IN) {
×
1731
        comparFn = compareChkInString;
×
1732
      } else if (optr == OP_TYPE_NOT_IN) {
×
1733
        comparFn = compareChkNotInString;
×
1734
      } else { /* normal relational comparFn */
1735
        comparFn = compareLenBinaryVal;
×
1736
      }
1737
      break;
×
1738
    case TSDB_DATA_TYPE_BINARY:
16,176✔
1739
    case TSDB_DATA_TYPE_GEOMETRY: {
1740
      if (optr == OP_TYPE_MATCH) {
16,176✔
1741
        comparFn = comparestrRegexMatch;
×
1742
      } else if (optr == OP_TYPE_NMATCH) {
16,176✔
1743
        comparFn = comparestrRegexNMatch;
×
1744
      } else if (optr == OP_TYPE_LIKE) { /* wildcard query using like operator */
16,176✔
1745
        comparFn = comparestrPatternMatch;
×
1746
      } else if (optr == OP_TYPE_NOT_LIKE) { /* wildcard query using like operator */
16,176✔
1747
        comparFn = comparestrPatternNMatch;
×
1748
      } else if (optr == OP_TYPE_IN) {
16,176✔
1749
        comparFn = compareChkInString;
×
1750
      } else if (optr == OP_TYPE_NOT_IN) {
16,176✔
1751
        comparFn = compareChkNotInString;
×
1752
      } else { /* normal relational comparFn */
1753
        comparFn = compareLenPrefixedStr;
16,176✔
1754
      }
1755

1756
      break;
16,176✔
1757
    }
1758

1759
    case TSDB_DATA_TYPE_NCHAR: {
15,508✔
1760
      if (optr == OP_TYPE_MATCH) {
15,508✔
1761
        comparFn = comparewcsRegexMatch;
×
1762
      } else if (optr == OP_TYPE_NMATCH) {
15,508✔
1763
        comparFn = comparewcsRegexNMatch;
×
1764
      } else if (optr == OP_TYPE_LIKE) {
15,508✔
1765
        comparFn = comparewcsPatternMatch;
×
1766
      } else if (optr == OP_TYPE_NOT_LIKE) {
15,508✔
1767
        comparFn = comparewcsPatternNMatch;
×
1768
      } else if (optr == OP_TYPE_IN) {
15,508✔
1769
        comparFn = compareChkInString;
×
1770
      } else if (optr == OP_TYPE_NOT_IN) {
15,508✔
1771
        comparFn = compareChkNotInString;
×
1772
      } else {
1773
        comparFn = compareLenPrefixedWStr;
15,508✔
1774
      }
1775
      break;
15,508✔
1776
    }
1777

1778
    case TSDB_DATA_TYPE_UTINYINT:
17,580✔
1779
      comparFn = compareUint8Val;
17,580✔
1780
      break;
17,580✔
1781
    case TSDB_DATA_TYPE_USMALLINT:
16,767✔
1782
      comparFn = compareUint16Val;
16,767✔
1783
      break;
16,767✔
1784
    case TSDB_DATA_TYPE_UINT:
7,755✔
1785
      comparFn = compareUint32Val;
7,755✔
1786
      break;
7,755✔
1787
    case TSDB_DATA_TYPE_UBIGINT:
7,790✔
1788
      comparFn = compareUint64Val;
7,790✔
1789
      break;
7,790✔
1790

1791
    default:
38✔
1792
      comparFn = compareInt32Val;
38✔
1793
      break;
38✔
1794
  }
1795

1796
  return comparFn;
208,240✔
1797
}
1798

1799
__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) {
151✔
1800
  switch (keyType) {
151✔
1801
    case TSDB_DATA_TYPE_TINYINT:
4✔
1802
    case TSDB_DATA_TYPE_BOOL:
1803
      return (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc;
4✔
1804
    case TSDB_DATA_TYPE_SMALLINT:
2✔
1805
      return (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc;
2✔
1806
    case TSDB_DATA_TYPE_INT:
29✔
1807
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
29✔
1808
    case TSDB_DATA_TYPE_BIGINT:
81✔
1809
    case TSDB_DATA_TYPE_TIMESTAMP:
1810
      return (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc;
81✔
1811
    case TSDB_DATA_TYPE_FLOAT:
2✔
1812
      return (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc;
2✔
1813
    case TSDB_DATA_TYPE_DOUBLE:
18✔
1814
      return (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc;
18✔
1815
    case TSDB_DATA_TYPE_UTINYINT:
2✔
1816
      return (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc;
2✔
1817
    case TSDB_DATA_TYPE_USMALLINT:
2✔
1818
      return (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc;
2✔
1819
    case TSDB_DATA_TYPE_UINT:
4✔
1820
      return (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc;
4✔
1821
    case TSDB_DATA_TYPE_UBIGINT:
2✔
1822
      return (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc;
2✔
1823
    case TSDB_DATA_TYPE_VARBINARY:
×
1824
      return (order == TSDB_ORDER_ASC) ? compareLenBinaryVal : compareLenBinaryValDesc;
×
1825
    case TSDB_DATA_TYPE_BINARY:
3✔
1826
    case TSDB_DATA_TYPE_GEOMETRY:
1827
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc;
3✔
1828
    case TSDB_DATA_TYPE_NCHAR:
2✔
1829
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc;
2✔
1830
    case TSDB_DATA_TYPE_JSON:
×
1831
      return (order == TSDB_ORDER_ASC) ? compareJsonVal : compareJsonValDesc;
×
1832
    case TSDB_DATA_TYPE_DECIMAL64:
×
1833
      return (order == TSDB_ORDER_ASC) ? compareDecimal64SameScale : compareDecimal64SameScaleDesc;
×
1834
    case TSDB_DATA_TYPE_DECIMAL:
×
1835
      return (order == TSDB_ORDER_ASC) ? compareDecimal128SameScale : compareDecimal128SameScaleDesc;
×
1836
    default:
×
1837
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
×
1838
  }
1839
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc