tcompare.c 19.6 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) {
D
dapan1121 已提交
59 60 61
  return NULL != taosHashGet((SHashObj *)pRight, varDataVal(pLeft), varDataLen(pLeft)) ? 1 : 0;
}

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

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 189

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

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

S
Shengliang Guan 已提交
201
int32_t compareLenPrefixedStrDesc(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
202 203 204 205 206 207 208 209
  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 已提交
210
    return len1 > len2 ? 1 : -1;
H
Hongze Cheng 已提交
211
  } else {
wafwerar's avatar
wafwerar 已提交
212
    int32_t ret = memcmp((TdUcs4 *)pLeft, (TdUcs4 *)pRight, len1);
H
Hongze Cheng 已提交
213 214 215 216 217 218 219 220
    if (ret == 0) {
      return 0;
    } else {
      return ret > 0 ? 1 : -1;
    }
  }
}

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

225 226 227 228 229
int32_t compareJsonContainsKey(const void* pLeft, const void* pRight) {
  if(pLeft) return 0;
  return 1;
}

H
Hongze Cheng 已提交
230 231 232 233 234 235 236 237 238 239
/*
 * 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
 *
 */
240
int32_t patternMatch(const char *patterStr, const char *str, size_t size, const SPatternCompareInfo *pInfo) {
H
Hongze Cheng 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
  char c, c1;

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

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

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

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

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

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

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

    c1 = str[j++];
S
Shengliang Guan 已提交
286 287
    ++o;

H
Hongze Cheng 已提交
288
    if (j <= size) {
S
Shengliang Guan 已提交
289 290 291 292
      if (c == '\\' && patterStr[i] == '_' && c1 == '_') {
        i++;
        continue;
      }
H
Hongze Cheng 已提交
293 294 295 296 297 298 299 300 301 302 303
      if (c == c1 || tolower(c) == tolower(c1) || (c == pInfo->matchOne && c1 != 0)) {
        continue;
      }
    }

    return TSDB_PATTERN_NOMATCH;
  }

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

wafwerar's avatar
wafwerar 已提交
304 305 306 307
int32_t WCSPatternMatch(const TdUcs4 *patterStr, const TdUcs4 *str, size_t size, const SPatternCompareInfo *pInfo) {
  TdUcs4 c, c1;
  TdUcs4 matchOne = L'_';  // "_"
  TdUcs4 matchAll = L'%';  // "%"
H
Hongze Cheng 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

  int32_t i = 0;
  int32_t j = 0;

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

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

wafwerar's avatar
wafwerar 已提交
324
      TdUcs4 accept[3] = {towupper(c), towlower(c), 0};
H
Hongze Cheng 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
      while (1) {
        size_t n = wcscspn(str, accept);

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

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

      return TSDB_PATTERN_NOWILDCARDMATCH;
    }

    c1 = str[j++];

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

    return TSDB_PATTERN_NOMATCH;
  }
S
Shengliang Guan 已提交
352

H
Hongze Cheng 已提交
353 354 355
  return (str[j] == 0 || j >= size) ? TSDB_PATTERN_MATCH : TSDB_PATTERN_NOMATCH;
}

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

S
Shengliang Guan 已提交
358
int32_t compareStrRegexCompNMatch(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
359 360 361
  return compareStrRegexComp(pLeft, pRight) ? 0 : 1;
}

S
Shengliang Guan 已提交
362
int32_t compareStrRegexComp(const void *pLeft, const void *pRight) {
H
Hongze Cheng 已提交
363
  size_t sz = varDataLen(pRight);
wafwerar's avatar
wafwerar 已提交
364
  char  *pattern = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
365 366 367 368
  memcpy(pattern, varDataVal(pRight), varDataLen(pRight));
  pattern[sz] = 0;

  sz = varDataLen(pLeft);
wafwerar's avatar
wafwerar 已提交
369
  char *str = taosMemoryMalloc(sz + 1);
H
Hongze Cheng 已提交
370 371 372
  memcpy(str, varDataVal(pLeft), sz);
  str[sz] = 0;

373
  int32_t errCode = 0;
H
Hongze Cheng 已提交
374 375 376
  regex_t regex;
  char    msgbuf[256] = {0};

377
  int32_t cflags = REG_EXTENDED;
H
Hongze Cheng 已提交
378 379 380 381
  if ((errCode = regcomp(&regex, pattern, cflags)) != 0) {
    regerror(errCode, &regex, msgbuf, sizeof(msgbuf));
    uError("Failed to compile regex pattern %s. reason %s", pattern, msgbuf);
    regfree(&regex);
wafwerar's avatar
wafwerar 已提交
382 383
    taosMemoryFree(str);
    taosMemoryFree(pattern);
H
Hongze Cheng 已提交
384 385 386 387 388 389 390 391 392 393
    return 1;
  }

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

S
Shengliang Guan 已提交
399 400 401
int32_t taosArrayCompareString(const void *a, const void *b) {
  const char *x = *(const char **)a;
  const char *y = *(const char **)b;
H
Hongze Cheng 已提交
402 403 404

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

S
Shengliang Guan 已提交
406
int32_t compareStrPatternMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
407 408 409
  SPatternCompareInfo pInfo = {'%', '_'};

  assert(varDataLen(pRight) <= TSDB_MAX_FIELD_LEN);
wafwerar's avatar
wafwerar 已提交
410
  char *pattern = taosMemoryCalloc(varDataLen(pRight) + 1, sizeof(char));
D
dapan1121 已提交
411 412 413
  memcpy(pattern, varDataVal(pRight), varDataLen(pRight));

  size_t sz = varDataLen(pLeft);
wafwerar's avatar
wafwerar 已提交
414
  char  *buf = taosMemoryMalloc(sz + 1);
D
dapan1121 已提交
415 416 417 418
  memcpy(buf, varDataVal(pLeft), sz);
  buf[sz] = 0;

  int32_t ret = patternMatch(pattern, buf, sz, &pInfo);
wafwerar's avatar
wafwerar 已提交
419 420
  taosMemoryFree(buf);
  taosMemoryFree(pattern);
D
dapan1121 已提交
421 422 423
  return (ret == TSDB_PATTERN_MATCH) ? 0 : 1;
}

S
Shengliang Guan 已提交
424
int32_t compareStrPatternNotMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
425 426 427
  return compareStrPatternMatch(pLeft, pRight) ? 0 : 1;
}

S
Shengliang Guan 已提交
428
int32_t compareWStrPatternMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
429 430 431 432
  SPatternCompareInfo pInfo = {'%', '_'};

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

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

wafwerar's avatar
wafwerar 已提交
436
  int32_t ret = WCSPatternMatch((TdUcs4*)pattern, (TdUcs4*)varDataVal(pLeft), varDataLen(pLeft) / TSDB_NCHAR_SIZE, &pInfo);
wafwerar's avatar
wafwerar 已提交
437
  taosMemoryFree(pattern);
D
dapan1121 已提交
438 439 440 441

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

S
Shengliang Guan 已提交
442
int32_t compareWStrPatternNotMatch(const void *pLeft, const void *pRight) {
D
dapan1121 已提交
443 444 445 446 447
  return compareWStrPatternMatch(pLeft, pRight) ? 0 : 1;
}
__compar_fn_t getComparFunc(int32_t type, int32_t optr) {
  __compar_fn_t comparFn = NULL;

D
dapan1121 已提交
448
  if (optr == OP_TYPE_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
D
dapan1121 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    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 已提交
471
  if (optr == OP_TYPE_NOT_IN && (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR)) {
D
dapan1121 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    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 已提交
496 497 498 499 500 501 502 503 504
    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 已提交
505
    case TSDB_DATA_TYPE_BIGINT:
S
Shengliang Guan 已提交
506 507 508 509 510 511 512 513 514
    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 已提交
515
    case TSDB_DATA_TYPE_BINARY: {
D
dapan1121 已提交
516
      if (optr == OP_TYPE_MATCH) {
D
dapan1121 已提交
517
        comparFn = compareStrRegexCompMatch;
D
dapan1121 已提交
518
      } else if (optr == OP_TYPE_NMATCH) {
D
dapan1121 已提交
519
        comparFn = compareStrRegexCompNMatch;
D
dapan1121 已提交
520
      } else if (optr == OP_TYPE_LIKE) { /* wildcard query using like operator */
D
dapan1121 已提交
521
        comparFn = compareStrPatternMatch;
D
dapan1121 已提交
522
      } else if (optr == OP_TYPE_NOT_LIKE) { /* wildcard query using like operator */
D
dapan1121 已提交
523
        comparFn = compareStrPatternNotMatch;
D
dapan1121 已提交
524
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
525
        comparFn = compareChkInString;
D
dapan1121 已提交
526
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
527 528 529 530 531 532 533 534 535
        comparFn = compareChkNotInString;
      } else { /* normal relational comparFn */
        comparFn = compareLenPrefixedStr;
      }

      break;
    }

    case TSDB_DATA_TYPE_NCHAR: {
D
dapan1121 已提交
536
      if (optr == OP_TYPE_MATCH) {
D
dapan1121 已提交
537
        comparFn = compareStrRegexCompMatch;
D
dapan1121 已提交
538
      } else if (optr == OP_TYPE_NMATCH) {
D
dapan1121 已提交
539
        comparFn = compareStrRegexCompNMatch;
D
dapan1121 已提交
540
      } else if (optr == OP_TYPE_LIKE) {
D
dapan1121 已提交
541
        comparFn = compareWStrPatternMatch;
D
dapan1121 已提交
542
      } else if (optr == OP_TYPE_NOT_LIKE) {
D
dapan1121 已提交
543
        comparFn = compareWStrPatternNotMatch;
D
dapan1121 已提交
544
      } else if (optr == OP_TYPE_IN) {
D
dapan1121 已提交
545
        comparFn = compareChkInString;
D
dapan1121 已提交
546
      } else if (optr == OP_TYPE_NOT_IN) {
D
dapan1121 已提交
547 548 549 550 551 552 553
        comparFn = compareChkNotInString;
      } else {
        comparFn = compareLenPrefixedWStr;
      }
      break;
    }

S
Shengliang Guan 已提交
554 555 556 557 558 559 560 561 562 563 564 565
    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 已提交
566 567 568 569 570 571 572 573 574

    default:
      comparFn = compareInt32Val;
      break;
  }

  return comparFn;
}

S
compare  
Shengliang Guan 已提交
575 576 577 578
__compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) {
  switch (keyType) {
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_BOOL:
579
      return (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc;
S
compare  
Shengliang Guan 已提交
580
    case TSDB_DATA_TYPE_SMALLINT:
581
      return (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc;
S
compare  
Shengliang Guan 已提交
582
    case TSDB_DATA_TYPE_INT:
583
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
584 585
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
586
      return (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc;
S
compare  
Shengliang Guan 已提交
587
    case TSDB_DATA_TYPE_FLOAT:
588
      return (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc;
S
compare  
Shengliang Guan 已提交
589
    case TSDB_DATA_TYPE_DOUBLE:
590
      return (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc;
S
compare  
Shengliang Guan 已提交
591
    case TSDB_DATA_TYPE_UTINYINT:
592
      return (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc;
S
compare  
Shengliang Guan 已提交
593
    case TSDB_DATA_TYPE_USMALLINT:
594
      return (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc;
S
compare  
Shengliang Guan 已提交
595
    case TSDB_DATA_TYPE_UINT:
596
      return (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc;
S
compare  
Shengliang Guan 已提交
597
    case TSDB_DATA_TYPE_UBIGINT:
598
      return (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc;
S
compare  
Shengliang Guan 已提交
599
    case TSDB_DATA_TYPE_BINARY:
600
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc;
S
compare  
Shengliang Guan 已提交
601
    case TSDB_DATA_TYPE_NCHAR:
602
      return (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc;
S
compare  
Shengliang Guan 已提交
603
    default:
604
      return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc;
S
compare  
Shengliang Guan 已提交
605 606 607
  }
}

S
Shengliang Guan 已提交
608
int32_t doCompare(const char *f1, const char *f2, int32_t type, size_t size) {
S
compare  
Shengliang Guan 已提交
609
  switch (type) {
S
Shengliang Guan 已提交
610 611 612 613 614 615 616 617 618 619
    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 已提交
620
    case TSDB_DATA_TYPE_TINYINT:
S
Shengliang Guan 已提交
621 622 623 624 625 626 627 628 629 630
    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 已提交
631
    case TSDB_DATA_TYPE_NCHAR: {
S
Shengliang Guan 已提交
632 633
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
634 635

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
636
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
637
      }
wafwerar's avatar
wafwerar 已提交
638
      int32_t ret = memcmp((TdUcs4 *)t1, (TdUcs4 *)t2, t2->len);
S
compare  
Shengliang Guan 已提交
639 640 641 642 643 644
      if (ret == 0) {
        return ret;
      }
      return (ret < 0) ? -1 : 1;
    }
    default: {  // todo refactor
S
Shengliang Guan 已提交
645 646
      tstr *t1 = (tstr *)f1;
      tstr *t2 = (tstr *)f2;
S
compare  
Shengliang Guan 已提交
647 648

      if (t1->len != t2->len) {
S
Shengliang Guan 已提交
649
        return t1->len > t2->len ? 1 : -1;
S
compare  
Shengliang Guan 已提交
650 651 652 653 654
      } else {
        int32_t ret = strncmp(t1->data, t2->data, t1->len);
        if (ret == 0) {
          return 0;
        } else {
S
Shengliang Guan 已提交
655
          return ret < 0 ? -1 : 1;
S
compare  
Shengliang Guan 已提交
656 657 658 659 660
        }
      }
    }
  }
}