indexComm.c 15.1 KB
Newer Older
dengyihao's avatar
add UT  
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16
#include "indexComm.h"
dengyihao's avatar
add UT  
dengyihao 已提交
17 18
#include "index.h"
#include "indexInt.h"
dengyihao's avatar
dengyihao 已提交
19
#include "tcoding.h"
20
#include "tcompare.h"
dengyihao's avatar
dengyihao 已提交
21
#include "tdataformat.h"
22
#include "ttypes.h"
23
#include "tvariant.h"
dengyihao's avatar
add UT  
dengyihao 已提交
24

dengyihao's avatar
dengyihao 已提交
25 26 27
#define INDEX_DATA_BOOL_NULL      0x02
#define INDEX_DATA_TINYINT_NULL   0x80
#define INDEX_DATA_SMALLINT_NULL  0x8000
wafwerar's avatar
wafwerar 已提交
28 29
#define INDEX_DATA_INT_NULL       0x80000000LL
#define INDEX_DATA_BIGINT_NULL    0x8000000000000000LL
dengyihao's avatar
dengyihao 已提交
30 31
#define INDEX_DATA_TIMESTAMP_NULL TSDB_DATA_BIGINT_NULL

dengyihao's avatar
dengyihao 已提交
32
#define INDEX_DATA_FLOAT_NULL    0x7FF00000            // it is an NAN
wafwerar's avatar
wafwerar 已提交
33
#define INDEX_DATA_DOUBLE_NULL   0x7FFFFF0000000000LL  // an NAN
dengyihao's avatar
dengyihao 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47
#define INDEX_DATA_NCHAR_NULL    0xFFFFFFFF
#define INDEX_DATA_BINARY_NULL   0xFF
#define INDEX_DATA_JSON_NULL     0xFFFFFFFF
#define INDEX_DATA_JSON_null     0xFFFFFFFE
#define INDEX_DATA_JSON_NOT_NULL 0x01

#define INDEX_DATA_UTINYINT_NULL  0xFF
#define INDEX_DATA_USMALLINT_NULL 0xFFFF
#define INDEX_DATA_UINT_NULL      0xFFFFFFFF
#define INDEX_DATA_UBIGINT_NULL   0xFFFFFFFFFFFFFFFFL

#define INDEX_DATA_NULL_STR   "NULL"
#define INDEX_DATA_NULL_STR_L "null"

dengyihao's avatar
add UT  
dengyihao 已提交
48 49 50
char JSON_COLUMN[] = "JSON";
char JSON_VALUE_DELIM = '&';

dengyihao's avatar
dengyihao 已提交
51
char* idxInt2str(int64_t val, char* dst, int radix) {
52
  char     buffer[65] = {0};
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  char*    p;
  int64_t  new_val;
  uint64_t uval = (uint64_t)val;

  if (radix < 0) {
    if (val < 0) {
      *dst++ = '-';
      uval = (uint64_t)0 - uval; /* Avoid integer overflow in (-val) for LLONG_MIN (BUG#31799). */
    }
  }
  p = &buffer[sizeof(buffer) - 1];
  *p = '\0';
  new_val = (int64_t)(uval / 10);
  *--p = '0' + (char)(uval - (uint64_t)new_val * 10);
  val = new_val;

  while (val != 0) {
    new_val = val / 10;
    *--p = '0' + (char)(val - new_val * 10);
    val = new_val;
  }
  while ((*dst++ = *p++) != 0)
    ;
  return dst - 1;
}
dengyihao's avatar
dengyihao 已提交
78
__compar_fn_t idxGetCompar(int8_t type) {
dengyihao's avatar
dengyihao 已提交
79 80 81 82 83
  if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) {
    return (__compar_fn_t)strcmp;
  }
  return getComparFunc(type, 0);
}
dengyihao's avatar
dengyihao 已提交
84
static FORCE_INLINE TExeCond tCompareLessThan(void* a, void* b, int8_t type) {
dengyihao's avatar
dengyihao 已提交
85
  __compar_fn_t func = idxGetCompar(type);
86
  return tCompare(func, QUERY_LESS_THAN, a, b, type);
87
}
dengyihao's avatar
dengyihao 已提交
88
static FORCE_INLINE TExeCond tCompareLessEqual(void* a, void* b, int8_t type) {
dengyihao's avatar
dengyihao 已提交
89
  __compar_fn_t func = idxGetCompar(type);
90
  return tCompare(func, QUERY_LESS_EQUAL, a, b, type);
91
}
dengyihao's avatar
dengyihao 已提交
92
static FORCE_INLINE TExeCond tCompareGreaterThan(void* a, void* b, int8_t type) {
dengyihao's avatar
dengyihao 已提交
93
  __compar_fn_t func = idxGetCompar(type);
94
  return tCompare(func, QUERY_GREATER_THAN, a, b, type);
95
}
dengyihao's avatar
dengyihao 已提交
96
static FORCE_INLINE TExeCond tCompareGreaterEqual(void* a, void* b, int8_t type) {
dengyihao's avatar
dengyihao 已提交
97
  __compar_fn_t func = idxGetCompar(type);
98
  return tCompare(func, QUERY_GREATER_EQUAL, a, b, type);
99
}
dengyihao's avatar
dengyihao 已提交
100

dengyihao's avatar
dengyihao 已提交
101
static FORCE_INLINE TExeCond tCompareContains(void* a, void* b, int8_t type) {
dengyihao's avatar
dengyihao 已提交
102
  __compar_fn_t func = idxGetCompar(type);
dengyihao's avatar
dengyihao 已提交
103 104
  return tCompare(func, QUERY_TERM, a, b, type);
}
dengyihao's avatar
dengyihao 已提交
105
static FORCE_INLINE TExeCond tCompareEqual(void* a, void* b, int8_t type) {
dengyihao's avatar
dengyihao 已提交
106
  __compar_fn_t func = idxGetCompar(type);
dengyihao's avatar
dengyihao 已提交
107 108
  return tCompare(func, QUERY_TERM, a, b, type);
}
109
TExeCond tCompare(__compar_fn_t func, int8_t cmptype, void* a, void* b, int8_t dtype) {
110
  if (dtype == TSDB_DATA_TYPE_BINARY || dtype == TSDB_DATA_TYPE_NCHAR || dtype == TSDB_DATA_TYPE_VARBINARY) {
111 112 113
    return tDoCompare(func, cmptype, a, b);
  }
#if 1
114 115 116 117 118 119 120 121 122
  if (dtype == TSDB_DATA_TYPE_TIMESTAMP) {
    int64_t va = taosStr2int64(a);
    int64_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
  } else if (dtype == TSDB_DATA_TYPE_BOOL || dtype == TSDB_DATA_TYPE_UTINYINT) {
    uint8_t va = taosStr2int64(a);
    uint8_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
  } else if (dtype == TSDB_DATA_TYPE_TINYINT) {
123 124 125
    int8_t va = taosStr2int64(a);
    int8_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
126
  } else if (dtype == TSDB_DATA_TYPE_SMALLINT) {
127 128 129
    int16_t va = taosStr2int64(a);
    int16_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
130 131 132 133 134
  } else if (dtype == TSDB_DATA_TYPE_USMALLINT) {
    uint16_t va = taosStr2int64(a);
    uint16_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
  } else if (dtype == TSDB_DATA_TYPE_INT) {
135 136 137
    int32_t va = taosStr2int64(a);
    int32_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
138 139 140 141 142
  } else if (dtype == TSDB_DATA_TYPE_UINT) {
    uint32_t va = taosStr2int64(a);
    uint32_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
  } else if (dtype == TSDB_DATA_TYPE_BIGINT) {
143 144 145
    int64_t va = taosStr2int64(a);
    int64_t vb = taosStr2int64(b);
    return tDoCompare(func, cmptype, &va, &vb);
146 147 148 149 150 151 152
  } else if (dtype == TSDB_DATA_TYPE_UBIGINT) {
    uint64_t va, vb;
    if (0 != toUInteger(a, strlen(a), 10, &va) || 0 != toUInteger(b, strlen(b), 10, &vb)) {
      return CONTINUE;
    }
    return tDoCompare(func, cmptype, &va, &vb);
  } else if (dtype == TSDB_DATA_TYPE_FLOAT) {
wafwerar's avatar
wafwerar 已提交
153
    float va = taosStr2Float(a, NULL);
154 155 156
    if (errno == ERANGE && va == -1) {
      return CONTINUE;
    }
wafwerar's avatar
wafwerar 已提交
157
    float vb = taosStr2Float(b, NULL);
158 159 160 161 162
    if (errno == ERANGE && va == -1) {
      return CONTINUE;
    }
    return tDoCompare(func, cmptype, &va, &vb);
  } else if (dtype == TSDB_DATA_TYPE_DOUBLE) {
wafwerar's avatar
wafwerar 已提交
163
    double va = taosStr2Double(a, NULL);
164 165 166
    if (errno == ERANGE && va == -1) {
      return CONTINUE;
    }
wafwerar's avatar
wafwerar 已提交
167
    double vb = taosStr2Double(b, NULL);
168 169 170 171
    if (errno == ERANGE && va == -1) {
      return CONTINUE;
    }
    return tDoCompare(func, cmptype, &va, &vb);
172
  }
173
  assert(0);
174
  return BREAK;
175 176 177
#endif
}
TExeCond tDoCompare(__compar_fn_t func, int8_t comparType, void* a, void* b) {
178 179
  // optime later
  int32_t ret = func(a, b);
180
  switch (comparType) {
dengyihao's avatar
dengyihao 已提交
181
    case QUERY_LESS_THAN:
182
      if (ret < 0) return MATCH;
dengyihao's avatar
dengyihao 已提交
183
      break;
184 185 186 187 188 189 190 191 192 193
    case QUERY_LESS_EQUAL: {
      if (ret <= 0) return MATCH;
      break;
    }
    case QUERY_GREATER_THAN: {
      if (ret > 0) return MATCH;
      break;
    }
    case QUERY_GREATER_EQUAL: {
      if (ret >= 0) return MATCH;
dengyihao's avatar
dengyihao 已提交
194
      break;
195
    }
dengyihao's avatar
dengyihao 已提交
196 197
    case QUERY_TERM: {
      if (ret == 0) return MATCH;
dengyihao's avatar
dengyihao 已提交
198
      break;
dengyihao's avatar
dengyihao 已提交
199
    }
dengyihao's avatar
dengyihao 已提交
200 201
    default:
      return BREAK;
202 203 204 205
  }
  return CONTINUE;
}

dengyihao's avatar
dengyihao 已提交
206
static TExeCond (*rangeCompare[])(void* a, void* b, int8_t type) = {
dengyihao's avatar
dengyihao 已提交
207
    tCompareLessThan, tCompareLessEqual, tCompareGreaterThan, tCompareGreaterEqual, tCompareContains, tCompareEqual};
208

dengyihao's avatar
dengyihao 已提交
209
_cache_range_compare idxGetCompare(RangeType ty) { return rangeCompare[ty]; }
210

dengyihao's avatar
dengyihao 已提交
211
char* idxPackJsonData(SIndexTerm* itm) {
dengyihao's avatar
add UT  
dengyihao 已提交
212 213 214 215
  /*
   * |<-----colname---->|<-----dataType---->|<--------colVal---------->|
   * |<-----string----->|<-----uint8_t----->|<----depend on dataType-->|
   */
dengyihao's avatar
dengyihao 已提交
216
  uint8_t ty = IDX_TYPE_GET_TYPE(itm->colType);
dengyihao's avatar
add UT  
dengyihao 已提交
217 218

  int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1;
wafwerar's avatar
wafwerar 已提交
219
  char*   buf = (char*)taosMemoryCalloc(1, sz);
dengyihao's avatar
add UT  
dengyihao 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  char*   p = buf;

  memcpy(p, itm->colName, itm->nColName);
  p += itm->nColName;

  memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM));
  p += sizeof(JSON_VALUE_DELIM);

  memcpy(p, &ty, sizeof(ty));
  p += sizeof(ty);

  memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM));
  p += sizeof(JSON_VALUE_DELIM);

  memcpy(p, itm->colVal, itm->nColVal);

  return buf;
}
238

dengyihao's avatar
dengyihao 已提交
239
char* idxPackJsonDataPrefix(SIndexTerm* itm, int32_t* skip) {
dengyihao's avatar
dengyihao 已提交
240 241 242 243
  /*
   * |<-----colname---->|<-----dataType---->|<--------colVal---------->|
   * |<-----string----->|<-----uint8_t----->|<----depend on dataType-->|
   */
dengyihao's avatar
dengyihao 已提交
244
  uint8_t ty = IDX_TYPE_GET_TYPE(itm->colType);
dengyihao's avatar
dengyihao 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

  int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1;
  char*   buf = (char*)taosMemoryCalloc(1, sz);
  char*   p = buf;

  memcpy(p, itm->colName, itm->nColName);
  p += itm->nColName;

  memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM));
  p += sizeof(JSON_VALUE_DELIM);

  memcpy(p, &ty, sizeof(ty));
  p += sizeof(ty);

  memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM));
  p += sizeof(JSON_VALUE_DELIM);

  *skip = p - buf;

  return buf;
}
dengyihao's avatar
dengyihao 已提交
266
char* idxPackJsonDataPrefixNoType(SIndexTerm* itm, int32_t* skip) {
dengyihao's avatar
dengyihao 已提交
267 268 269 270
  /*
   * |<-----colname---->|<-----dataType---->|<--------colVal---------->|
   * |<-----string----->|<-----uint8_t----->|<----depend on dataType-->|
   */
dengyihao's avatar
dengyihao 已提交
271
  uint8_t ty = IDX_TYPE_GET_TYPE(itm->colType);
dengyihao's avatar
dengyihao 已提交
272 273 274 275 276 277 278 279 280 281 282 283

  int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1;
  char*   buf = (char*)taosMemoryCalloc(1, sz);
  char*   p = buf;

  memcpy(p, itm->colName, itm->nColName);
  p += itm->nColName;

  memcpy(p, &JSON_VALUE_DELIM, sizeof(JSON_VALUE_DELIM));
  p += sizeof(JSON_VALUE_DELIM);
  *skip = p - buf;

dengyihao's avatar
dengyihao 已提交
284 285
  return buf;
}
dengyihao's avatar
dengyihao 已提交
286

dengyihao's avatar
dengyihao 已提交
287 288 289 290 291 292
int idxUidCompare(const void* a, const void* b) {
  uint64_t l = *(uint64_t*)a;
  uint64_t r = *(uint64_t*)b;
  return l - r;
}
int32_t idxConvertData(void* src, int8_t type, void** dst) {
dengyihao's avatar
dengyihao 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  int tlen = -1;
  switch (type) {
    case TSDB_DATA_TYPE_TIMESTAMP:
      tlen = taosEncodeFixedI64(NULL, *(int64_t*)src);
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeFixedI64(dst, *(int64_t*)src);
      break;
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_UTINYINT:
      tlen = taosEncodeFixedU8(NULL, *(uint8_t*)src);
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeFixedU8(dst, *(uint8_t*)src);
      break;
    case TSDB_DATA_TYPE_TINYINT:
      tlen = taosEncodeFixedI8(NULL, *(uint8_t*)src);
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeFixedI8(dst, *(uint8_t*)src);
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      tlen = taosEncodeFixedI16(NULL, *(int16_t*)src);
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeFixedI16(dst, *(int16_t*)src);
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      tlen = taosEncodeFixedU16(NULL, *(uint16_t*)src);
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeFixedU16(dst, *(uint16_t*)src);
      break;
    case TSDB_DATA_TYPE_INT:
      tlen = taosEncodeFixedI32(NULL, *(int32_t*)src);
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeFixedI32(dst, *(int32_t*)src);
      break;
    case TSDB_DATA_TYPE_FLOAT:
      tlen = taosEncodeBinary(NULL, src, sizeof(float));
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeBinary(dst, src, sizeof(float));
      break;
    case TSDB_DATA_TYPE_UINT:
      tlen = taosEncodeFixedU32(NULL, *(uint32_t*)src);
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeFixedU32(dst, *(uint32_t*)src);
      break;
    case TSDB_DATA_TYPE_BIGINT:
dengyihao's avatar
dengyihao 已提交
337
      tlen = taosEncodeFixedI64(NULL, *(int64_t*)src);
dengyihao's avatar
dengyihao 已提交
338
      *dst = taosMemoryCalloc(1, tlen + 1);
dengyihao's avatar
dengyihao 已提交
339
      tlen = taosEncodeFixedI64(dst, *(int64_t*)src);
dengyihao's avatar
dengyihao 已提交
340 341 342 343 344 345 346
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      tlen = taosEncodeBinary(NULL, src, sizeof(double));
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeBinary(dst, src, sizeof(double));
      break;
    case TSDB_DATA_TYPE_UBIGINT:
dengyihao's avatar
dengyihao 已提交
347
      tlen = taosEncodeFixedU64(NULL, *(uint64_t*)src);
dengyihao's avatar
dengyihao 已提交
348
      *dst = taosMemoryCalloc(1, tlen + 1);
dengyihao's avatar
dengyihao 已提交
349
      tlen = taosEncodeFixedU64(dst, *(uint64_t*)src);
dengyihao's avatar
dengyihao 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
      break;
    case TSDB_DATA_TYPE_NCHAR: {
      tlen = taosEncodeBinary(NULL, varDataVal(src), varDataLen(src));
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeBinary(dst, varDataVal(src), varDataLen(src));

      break;
    }
    case TSDB_DATA_TYPE_VARCHAR: {  // TSDB_DATA_TYPE_BINARY
      tlen = taosEncodeBinary(NULL, src, strlen(src));
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeBinary(dst, src, strlen(src));
      break;
    }
    case TSDB_DATA_TYPE_VARBINARY:
      tlen = taosEncodeBinary(NULL, src, strlen(src));
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeBinary(dst, src, strlen(src));
      break;
    default:
      TASSERT(0);
      break;
  }
wafwerar's avatar
wafwerar 已提交
373
  *dst = (char*)*dst - tlen;
374
  // indexMayFillNumbericData(*dst, tlen);
dengyihao's avatar
dengyihao 已提交
375 376
  return tlen;
}
dengyihao's avatar
dengyihao 已提交
377
int32_t idxConvertDataToStr(void* src, int8_t type, void** dst) {
dengyihao's avatar
dengyihao 已提交
378 379 380 381
  if (src == NULL) {
    *dst = strndup(INDEX_DATA_NULL_STR, (int)strlen(INDEX_DATA_NULL_STR));
    return (int32_t)strlen(INDEX_DATA_NULL_STR);
  }
382 383
  int     tlen = tDataTypes[type].bytes;
  int32_t bufSize = 64;
dengyihao's avatar
dengyihao 已提交
384 385
  switch (type) {
    case TSDB_DATA_TYPE_TIMESTAMP:
386
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
387
      idxInt2str(*(int64_t*)src, *dst, -1);
dengyihao's avatar
dengyihao 已提交
388
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
389 390 391
      break;
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_UTINYINT:
392
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
393
      idxInt2str(*(uint8_t*)src, *dst, 1);
dengyihao's avatar
dengyihao 已提交
394
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
395 396
      break;
    case TSDB_DATA_TYPE_TINYINT:
397
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
398
      idxInt2str(*(int8_t*)src, *dst, 1);
dengyihao's avatar
dengyihao 已提交
399
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
400 401
      break;
    case TSDB_DATA_TYPE_SMALLINT:
402
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
403
      idxInt2str(*(int16_t*)src, *dst, -1);
dengyihao's avatar
dengyihao 已提交
404
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
405 406
      break;
    case TSDB_DATA_TYPE_USMALLINT:
407
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
408
      idxInt2str(*(uint16_t*)src, *dst, -1);
dengyihao's avatar
dengyihao 已提交
409
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
410 411
      break;
    case TSDB_DATA_TYPE_INT:
412
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
413
      idxInt2str(*(int32_t*)src, *dst, -1);
dengyihao's avatar
dengyihao 已提交
414
      tlen = strlen(*dst);
415
      break;
dengyihao's avatar
dengyihao 已提交
416
    case TSDB_DATA_TYPE_UINT:
417
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
418
      idxInt2str(*(uint32_t*)src, *dst, 1);
dengyihao's avatar
dengyihao 已提交
419
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
420 421
      break;
    case TSDB_DATA_TYPE_BIGINT:
422 423
      *dst = taosMemoryCalloc(1, bufSize + 1);
      sprintf(*dst, "%" PRIu64, *(uint64_t*)src);
dengyihao's avatar
dengyihao 已提交
424
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
425 426
      break;
    case TSDB_DATA_TYPE_UBIGINT:
427
      *dst = taosMemoryCalloc(1, bufSize + 1);
dengyihao's avatar
dengyihao 已提交
428
      idxInt2str(*(uint64_t*)src, *dst, 1);
dengyihao's avatar
dengyihao 已提交
429
      tlen = strlen(*dst);
430 431 432
    case TSDB_DATA_TYPE_FLOAT:
      *dst = taosMemoryCalloc(1, bufSize + 1);
      sprintf(*dst, "%.9lf", *(float*)src);
dengyihao's avatar
dengyihao 已提交
433
      tlen = strlen(*dst);
434 435 436 437
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      *dst = taosMemoryCalloc(1, bufSize + 1);
      sprintf(*dst, "%.9lf", *(double*)src);
dengyihao's avatar
dengyihao 已提交
438
      tlen = strlen(*dst);
dengyihao's avatar
dengyihao 已提交
439
      break;
440 441 442 443
    case TSDB_DATA_TYPE_NCHAR: {
      tlen = taosEncodeBinary(NULL, varDataVal(src), varDataLen(src));
      *dst = taosMemoryCalloc(1, tlen + 1);
      tlen = taosEncodeBinary(dst, varDataVal(src), varDataLen(src));
dengyihao's avatar
dengyihao 已提交
444
      *dst = (char*)*dst - tlen;
dengyihao's avatar
dengyihao 已提交
445
      break;
446 447
    }
    case TSDB_DATA_TYPE_VARCHAR: {  // TSDB_DATA_TYPE_BINARY
dengyihao's avatar
dengyihao 已提交
448
      tlen = taosEncodeBinary(NULL, varDataVal(src), varDataLen(src));
449
      *dst = taosMemoryCalloc(1, tlen + 1);
dengyihao's avatar
dengyihao 已提交
450
      tlen = taosEncodeBinary(dst, varDataVal(src), varDataLen(src));
dengyihao's avatar
dengyihao 已提交
451
      *dst = (char*)*dst - tlen;
dengyihao's avatar
dengyihao 已提交
452
      break;
453 454
    }
    case TSDB_DATA_TYPE_VARBINARY:
dengyihao's avatar
dengyihao 已提交
455
      tlen = taosEncodeBinary(NULL, varDataVal(src), varDataLen(src));
456
      *dst = taosMemoryCalloc(1, tlen + 1);
dengyihao's avatar
dengyihao 已提交
457
      tlen = taosEncodeBinary(dst, varDataVal(src), varDataLen(src));
dengyihao's avatar
dengyihao 已提交
458
      *dst = (char*)*dst - tlen;
459
      break;
dengyihao's avatar
dengyihao 已提交
460
    default:
461
      TASSERT(0);
dengyihao's avatar
dengyihao 已提交
462
      break;
dengyihao's avatar
dengyihao 已提交
463 464 465
  }
  return tlen;
}