tvariant.c 29.2 KB
Newer Older
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/>.
 */

S
common  
Shengliang Guan 已提交
16 17
#define _DEFAULT_SOURCE
#include "tvariant.h"
18
#include "ttime.h"
19
#include "ttokendef.h"
20
#include "tvariant.h"
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#define SET_EXT_INFO(converted, res, minv, maxv, exti)              \
  do {                                                              \
    if (converted == NULL || exti == NULL || *converted == false) { \
      break;                                                        \
    }                                                               \
    if ((res) < (minv)) {                                           \
      *exti = -1;                                                   \
      break;                                                        \
    }                                                               \
    if ((res) > (maxv)) {                                           \
      *exti = 1;                                                    \
      break;                                                        \
    }                                                               \
    assert(0);                                                      \
  } while (0)
37

X
Xiaoyu Wang 已提交
38
int32_t toInteger(const char *z, int32_t n, int32_t base, int64_t *value) {
39
  errno = 0;
X
Xiaoyu Wang 已提交
40
  char *endPtr = NULL;
41

wafwerar's avatar
wafwerar 已提交
42
  *value = taosStr2Int64(z, &endPtr, base);
X
Xiaoyu Wang 已提交
43
  if (errno == ERANGE || errno == EINVAL || endPtr - z != n) {
44 45 46 47 48 49 50
    errno = 0;
    return -1;
  }

  return 0;
}

X
Xiaoyu Wang 已提交
51 52 53
int32_t toUInteger(const char *z, int32_t n, int32_t base, uint64_t *value) {
  errno = 0;
  char *endPtr = NULL;
54

X
Xiaoyu Wang 已提交
55 56 57 58 59
  const char *p = z;
  while (*p != 0 && *p == ' ') p++;
  if (*p != 0 && *p == '-') {
    return -1;
  }
X
Xiaoyu Wang 已提交
60

wafwerar's avatar
wafwerar 已提交
61
  *value = taosStr2UInt64(z, &endPtr, base);
X
Xiaoyu Wang 已提交
62 63 64
  if (errno == ERANGE || errno == EINVAL || endPtr - z != n) {
    errno = 0;
    return -1;
65
  }
X
Xiaoyu Wang 已提交
66

X
Xiaoyu Wang 已提交
67
  return 0;
68
}
H
Haojun Liao 已提交
69

70
/**
H
Haojun Liao 已提交
71
 * create SVariant from binary string, not ascii data
72 73 74 75 76
 * @param pVar
 * @param pz
 * @param len
 * @param type
 */
H
Haojun Liao 已提交
77
void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type) {
78 79 80
  switch (type) {
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT: {
81
      pVar->nLen = tDataTypes[type].bytes;
82
      pVar->i = GET_INT8_VAL(pz);
83 84 85
      break;
    }
    case TSDB_DATA_TYPE_UTINYINT: {
86
      pVar->nLen = tDataTypes[type].bytes;
87
      pVar->u = GET_UINT8_VAL(pz);
88 89 90
      break;
    }
    case TSDB_DATA_TYPE_SMALLINT: {
91
      pVar->nLen = tDataTypes[type].bytes;
92
      pVar->i = GET_INT16_VAL(pz);
93 94 95
      break;
    }
    case TSDB_DATA_TYPE_USMALLINT: {
96
      pVar->nLen = tDataTypes[type].bytes;
97
      pVar->u = GET_UINT16_VAL(pz);
98 99 100
      break;
    }
    case TSDB_DATA_TYPE_INT: {
101
      pVar->nLen = tDataTypes[type].bytes;
102
      pVar->i = GET_INT32_VAL(pz);
103 104 105
      break;
    }
    case TSDB_DATA_TYPE_UINT: {
106
      pVar->nLen = tDataTypes[type].bytes;
107
      pVar->u = GET_UINT32_VAL(pz);
108 109 110 111
      break;
    }
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP: {
112
      pVar->nLen = tDataTypes[type].bytes;
113
      pVar->i = GET_INT64_VAL(pz);
114 115 116
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
117
      pVar->nLen = tDataTypes[type].bytes;
118
      pVar->u = GET_UINT64_VAL(pz);
119 120 121
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
122
      pVar->nLen = tDataTypes[type].bytes;
H
Haojun Liao 已提交
123
      pVar->d = GET_DOUBLE_VAL(pz);
124 125 126
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
127
      pVar->nLen = tDataTypes[type].bytes;
H
Haojun Liao 已提交
128
      pVar->d = GET_FLOAT_VAL(pz);
129 130
      break;
    }
X
Xiaoyu Wang 已提交
131
    case TSDB_DATA_TYPE_NCHAR: {  // here we get the nchar length from raw binary bits length
132
      size_t lenInwchar = len / TSDB_NCHAR_SIZE;
133

wafwerar's avatar
wafwerar 已提交
134
      pVar->ucs4 = taosMemoryCalloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE);
wafwerar's avatar
wafwerar 已提交
135
      memcpy(pVar->ucs4, pz, lenInwchar * TSDB_NCHAR_SIZE);
136
      pVar->nLen = (int32_t)len;
X
Xiaoyu Wang 已提交
137

138 139
      break;
    }
140
    case TSDB_DATA_TYPE_BINARY: {  // todo refactor, extract a method
wafwerar's avatar
wafwerar 已提交
141
      pVar->pz = taosMemoryCalloc(len + 1, sizeof(char));
142
      memcpy(pVar->pz, pz, len);
143
      pVar->nLen = (int32_t)len;
144 145
      break;
    }
X
Xiaoyu Wang 已提交
146

147
    default:
148
      pVar->i = GET_INT32_VAL(pz);
149
      pVar->nLen = tDataTypes[TSDB_DATA_TYPE_INT].bytes;
150
  }
X
Xiaoyu Wang 已提交
151

152 153 154
  pVar->nType = type;
}

H
Haojun Liao 已提交
155
void taosVariantDestroy(SVariant *pVar) {
156
  if (pVar == NULL) return;
X
Xiaoyu Wang 已提交
157

wmmhello's avatar
wmmhello 已提交
158 159
  if (pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR
     || pVar->nType == TSDB_DATA_TYPE_JSON) {
wafwerar's avatar
wafwerar 已提交
160
    taosMemoryFreeClear(pVar->pz);
161 162
    pVar->nLen = 0;
  }
weixin_48148422's avatar
weixin_48148422 已提交
163 164

  // NOTE: this is only for string array
W
wpan 已提交
165
  if (pVar->nType == TSDB_DATA_TYPE_POINTER_ARRAY) {
weixin_48148422's avatar
weixin_48148422 已提交
166
    size_t num = taosArrayGetSize(pVar->arr);
X
Xiaoyu Wang 已提交
167 168
    for (size_t i = 0; i < num; i++) {
      void *p = taosArrayGetP(pVar->arr, i);
wafwerar's avatar
wafwerar 已提交
169
      taosMemoryFree(p);
weixin_48148422's avatar
weixin_48148422 已提交
170 171 172
    }
    taosArrayDestroy(pVar->arr);
    pVar->arr = NULL;
W
wpan 已提交
173 174 175
  } else if (pVar->nType == TSDB_DATA_TYPE_VALUE_ARRAY) {
    taosArrayDestroy(pVar->arr);
    pVar->arr = NULL;
weixin_48148422's avatar
weixin_48148422 已提交
176
  }
177 178
}

H
Haojun Liao 已提交
179
bool taosVariantIsValid(SVariant *pVar) {
180 181 182 183
  assert(pVar != NULL);
  return isValidDataType(pVar->nType);
}

H
Haojun Liao 已提交
184
void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) {
185
  if (pSrc == NULL || pDst == NULL) return;
X
Xiaoyu Wang 已提交
186

H
Haojun Liao 已提交
187
  pDst->nType = pSrc->nType;
wmmhello's avatar
wmmhello 已提交
188 189
  if (pSrc->nType == TSDB_DATA_TYPE_BINARY || pSrc->nType == TSDB_DATA_TYPE_NCHAR
      || pSrc->nType == TSDB_DATA_TYPE_JSON) {
H
Haojun Liao 已提交
190
    int32_t len = pSrc->nLen + TSDB_NCHAR_SIZE;
wafwerar's avatar
wafwerar 已提交
191
    char   *p = taosMemoryRealloc(pDst->pz, len);
H
Haojun Liao 已提交
192 193
    assert(p);

H
Haojun Liao 已提交
194
    memset(p, 0, len);
H
Haojun Liao 已提交
195 196
    pDst->pz = p;

H
Haojun Liao 已提交
197 198
    memcpy(pDst->pz, pSrc->pz, pSrc->nLen);
    pDst->nLen = pSrc->nLen;
H
Haojun Liao 已提交
199 200 201
    return;
  }

202
  if (IS_NUMERIC_TYPE(pSrc->nType) || (pSrc->nType == TSDB_DATA_TYPE_BOOL)) {
203
    pDst->i = pSrc->i;
W
wpan 已提交
204
  } else if (pSrc->nType == TSDB_DATA_TYPE_POINTER_ARRAY) {  // this is only for string array
weixin_48148422's avatar
weixin_48148422 已提交
205
    size_t num = taosArrayGetSize(pSrc->arr);
X
Xiaoyu Wang 已提交
206 207 208 209
    pDst->arr = taosArrayInit(num, sizeof(char *));
    for (size_t i = 0; i < num; i++) {
      char *p = (char *)taosArrayGetP(pSrc->arr, i);
      char *n = strdup(p);
weixin_48148422's avatar
weixin_48148422 已提交
210 211
      taosArrayPush(pDst->arr, &n);
    }
W
wpan 已提交
212
  } else if (pSrc->nType == TSDB_DATA_TYPE_VALUE_ARRAY) {
X
Xiaoyu Wang 已提交
213 214 215 216 217 218 219 220
    size_t num = taosArrayGetSize(pSrc->arr);
    pDst->arr = taosArrayInit(num, sizeof(int64_t));
    pDst->nLen = pSrc->nLen;
    assert(pSrc->nLen == num);
    for (size_t i = 0; i < num; i++) {
      int64_t *p = taosArrayGet(pSrc->arr, i);
      taosArrayPush(pDst->arr, p);
    }
221
  }
H
Haojun Liao 已提交
222

W
wpan 已提交
223
  if (pDst->nType != TSDB_DATA_TYPE_POINTER_ARRAY && pDst->nType != TSDB_DATA_TYPE_VALUE_ARRAY) {
H
Haojun Liao 已提交
224
    pDst->nLen = tDataTypes[pDst->nType].bytes;
H
Haojun Liao 已提交
225
  }
226 227
}

X
Xiaoyu Wang 已提交
228
int32_t taosVariantCompare(const SVariant *p1, const SVariant *p2) {
H
Haojun Liao 已提交
229 230 231 232 233 234 235 236 237 238 239
  if (p1->nType == TSDB_DATA_TYPE_NULL && p2->nType == TSDB_DATA_TYPE_NULL) {
    return 0;
  }

  if (p1->nType == TSDB_DATA_TYPE_NULL) {
    return -1;
  }

  if (p2->nType == TSDB_DATA_TYPE_NULL) {
    return 1;
  }
240

241 242 243 244
  if (p1->nType == TSDB_DATA_TYPE_BINARY || p1->nType == TSDB_DATA_TYPE_NCHAR) {
    if (p1->nLen == p2->nLen) {
      return memcmp(p1->pz, p2->pz, p1->nLen);
    } else {
X
Xiaoyu Wang 已提交
245
      return p1->nLen > p2->nLen ? 1 : -1;
246 247
    }
  } else if (p1->nType == TSDB_DATA_TYPE_FLOAT || p1->nType == TSDB_DATA_TYPE_DOUBLE) {
H
Haojun Liao 已提交
248
    if (p1->d == p2->d) {
249 250
      return 0;
    } else {
X
Xiaoyu Wang 已提交
251
      return p1->d > p2->d ? 1 : -1;
252 253
    }
  } else if (IS_UNSIGNED_NUMERIC_TYPE(p1->nType)) {
254
    if (p1->u == p2->u) {
255 256
      return 0;
    } else {
X
Xiaoyu Wang 已提交
257
      return p1->u > p2->u ? 1 : -1;
258 259
    }
  } else {
260
    if (p1->i == p2->i) {
261 262
      return 0;
    } else {
X
Xiaoyu Wang 已提交
263
      return p1->i > p2->i ? 1 : -1;
264
    }
265 266 267
  }
}

H
Haojun Liao 已提交
268
int32_t taosVariantToString(SVariant *pVar, char *dst) {
269
  if (pVar == NULL || dst == NULL) return 0;
X
Xiaoyu Wang 已提交
270

271 272 273 274 275 276
  switch (pVar->nType) {
    case TSDB_DATA_TYPE_BINARY: {
      int32_t len = sprintf(dst, "\'%s\'", pVar->pz);
      assert(len <= pVar->nLen + sizeof("\'") * 2);  // two more chars
      return len;
    }
X
Xiaoyu Wang 已提交
277

278 279
    case TSDB_DATA_TYPE_NCHAR: {
      dst[0] = '\'';
wafwerar's avatar
wafwerar 已提交
280
      taosUcs4ToMbs(pVar->ucs4, (taosUcs4len(pVar->ucs4) + 1) * TSDB_NCHAR_SIZE, dst + 1);
281
      int32_t len = (int32_t)strlen(dst);
282 283 284 285
      dst[len] = '\'';
      dst[len + 1] = 0;
      return len + 1;
    }
X
Xiaoyu Wang 已提交
286

287 288 289 290
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
291 292 293
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
294
      return sprintf(dst, "%d", (int32_t)pVar->i);
X
Xiaoyu Wang 已提交
295

296
    case TSDB_DATA_TYPE_BIGINT:
297
      return sprintf(dst, "%" PRId64, pVar->i);
298
    case TSDB_DATA_TYPE_UBIGINT:
299
      return sprintf(dst, "%" PRIu64, pVar->u);
300 301
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
H
Haojun Liao 已提交
302
      return sprintf(dst, "%.9lf", pVar->d);
X
Xiaoyu Wang 已提交
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    default:
      return 0;
  }
}

static FORCE_INLINE int32_t convertToBoolImpl(char *pStr, int32_t len) {
  if ((strncasecmp(pStr, "true", len) == 0) && (len == 4)) {
    return TSDB_TRUE;
  } else if ((strncasecmp(pStr, "false", len) == 0) && (len == 5)) {
    return TSDB_FALSE;
  } else if (strcasecmp(pStr, TSDB_DATA_NULL_STR_L) == 0) {
    return TSDB_DATA_BOOL_NULL;
  } else {
    return -1;
  }
}

wafwerar's avatar
wafwerar 已提交
321
static FORCE_INLINE int32_t wcsconvertToBoolImpl(TdUcs4 *pstr, int32_t len) {
322 323 324 325
  if ((wcsncasecmp(pstr, L"true", len) == 0) && (len == 4)) {
    return TSDB_TRUE;
  } else if (wcsncasecmp(pstr, L"false", len) == 0 && (len == 5)) {
    return TSDB_FALSE;
326 327
  } else if (memcmp(pstr, L"null", wcslen(L"null")) == 0) {
    return TSDB_DATA_BOOL_NULL;
328 329 330 331 332
  } else {
    return -1;
  }
}

H
Haojun Liao 已提交
333
static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
334
  const int32_t INITIAL_ALLOC_SIZE = 40;
X
Xiaoyu Wang 已提交
335
  char         *pBuf = NULL;
336

H
Haojun Liao 已提交
337
  // it is a in-place convert type for SVariant, local buffer is needed
338
  if (*pDest == pVariant->pz) {
wafwerar's avatar
wafwerar 已提交
339
    pBuf = taosMemoryCalloc(1, INITIAL_ALLOC_SIZE);
340
  }
X
Xiaoyu Wang 已提交
341

342 343 344
  if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
    size_t newSize = pVariant->nLen * TSDB_NCHAR_SIZE;
    if (pBuf != NULL) {
dengyihao's avatar
dengyihao 已提交
345
      if (newSize >= INITIAL_ALLOC_SIZE) {
wafwerar's avatar
wafwerar 已提交
346
        pBuf = taosMemoryRealloc(pBuf, newSize + 1);
347
      }
X
Xiaoyu Wang 已提交
348

wafwerar's avatar
wafwerar 已提交
349
      taosUcs4ToMbs(pVariant->ucs4, (int32_t)newSize, pBuf);
wafwerar's avatar
wafwerar 已提交
350
      taosMemoryFree(pVariant->ucs4);
351 352
      pBuf[newSize] = 0;
    } else {
wafwerar's avatar
wafwerar 已提交
353
      taosUcs4ToMbs(pVariant->ucs4, (int32_t)newSize, *pDest);
354
    }
X
Xiaoyu Wang 已提交
355

356
  } else {
357
    if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) {
358
      sprintf(pBuf == NULL ? *pDest : pBuf, "%" PRId64, pVariant->i);
359
    } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) {
H
Haojun Liao 已提交
360
      sprintf(pBuf == NULL ? *pDest : pBuf, "%lf", pVariant->d);
361
    } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) {
362
      sprintf(pBuf == NULL ? *pDest : pBuf, "%s", (pVariant->i == TSDB_TRUE) ? "TRUE" : "FALSE");
363 364 365 366
    } else if (pVariant->nType == 0) {  // null data
      setNull(pBuf == NULL ? *pDest : pBuf, TSDB_DATA_TYPE_BINARY, 0);
    }
  }
X
Xiaoyu Wang 已提交
367

368 369 370
  if (pBuf != NULL) {
    *pDest = pBuf;
  }
X
Xiaoyu Wang 已提交
371

372
  *pDestSize = (int32_t)strlen(*pDest);
373 374 375
  return 0;
}

H
Haojun Liao 已提交
376
static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
377
  char tmpBuf[40] = {0};
X
Xiaoyu Wang 已提交
378 379

  char   *pDst = tmpBuf;
380
  int32_t nLen = 0;
381 382 383

  // convert the number to string, than convert it to wchar string.
  if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) {
384
    nLen = sprintf(pDst, "%" PRId64, pVariant->i);
385
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
X
Xiaoyu Wang 已提交
386
    nLen = sprintf(pDst, "%" PRIu64, pVariant->u);
387
  } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) {
H
Haojun Liao 已提交
388
    nLen = sprintf(pDst, "%lf", pVariant->d);
389 390 391 392
  } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
    pDst = pVariant->pz;
    nLen = pVariant->nLen;
  } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) {
393
    nLen = sprintf(pDst, "%s", (pVariant->i == TSDB_TRUE) ? "TRUE" : "FALSE");
394
  }
X
Xiaoyu Wang 已提交
395

396
  if (*pDest == pVariant->pz) {
wafwerar's avatar
wafwerar 已提交
397
    TdUcs4 *pWStr = taosMemoryCalloc(1, (nLen + 1) * TSDB_NCHAR_SIZE);
X
Xiaoyu Wang 已提交
398
    bool    ret = taosMbsToUcs4(pDst, nLen, pWStr, (nLen + 1) * TSDB_NCHAR_SIZE, NULL);
399
    if (!ret) {
wafwerar's avatar
wafwerar 已提交
400
      taosMemoryFreeClear(pWStr);
401 402 403
      return -1;
    }

404 405
    // free the binary buffer in the first place
    if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
wafwerar's avatar
wafwerar 已提交
406
      taosMemoryFree(pVariant->ucs4);
407
    }
X
Xiaoyu Wang 已提交
408

wafwerar's avatar
wafwerar 已提交
409 410
    pVariant->ucs4 = pWStr;
    *pDestSize = taosUcs4len(pVariant->ucs4);
X
Xiaoyu Wang 已提交
411

412
    // shrink the allocate memory, no need to check here.
wafwerar's avatar
wafwerar 已提交
413
    char *tmp = taosMemoryRealloc(pVariant->ucs4, (*pDestSize + 1) * TSDB_NCHAR_SIZE);
414
    assert(tmp != NULL);
X
Xiaoyu Wang 已提交
415

wafwerar's avatar
wafwerar 已提交
416
    pVariant->ucs4 = (TdUcs4 *)tmp;
417
  } else {
418
    int32_t output = 0;
419

X
Xiaoyu Wang 已提交
420
    bool ret = taosMbsToUcs4(pDst, nLen, (TdUcs4 *)*pDest, (nLen + 1) * TSDB_NCHAR_SIZE, &output);
421 422 423 424
    if (!ret) {
      return -1;
    }

H
hjxilinx 已提交
425
    if (pDestSize != NULL) {
426
      *pDestSize = output;
H
hjxilinx 已提交
427
    }
428
  }
X
Xiaoyu Wang 已提交
429

430 431 432 433
  return 0;
}

static FORCE_INLINE int32_t convertToDouble(char *pStr, int32_t len, double *value) {
X
Xiaoyu Wang 已提交
434 435 436 437 438
  //  SToken stoken = {.z = pStr, .n = len};
  //  if (TK_ILLEGAL == tGetNumericStringType(&stoken)) {
  //    return -1;
  //  }
  //
wafwerar's avatar
wafwerar 已提交
439
  //  *value = taosStr2Double(pStr, NULL);
440 441 442
  return 0;
}

X
Xiaoyu Wang 已提交
443 444
static FORCE_INLINE int32_t convertToInteger(SVariant *pVariant, int64_t *result, int32_t type, bool issigned,
                                             bool releaseVariantPtr, bool *converted) {
445
  if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
H
Haojun Liao 已提交
446
    setNull((char *)result, type, tDataTypes[type].bytes);
447 448
    return 0;
  }
449 450 451 452 453 454

  if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || (pVariant->nType == TSDB_DATA_TYPE_BOOL)) {
    *result = pVariant->i;
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
    *result = pVariant->u;
  } else if (IS_FLOAT_TYPE(pVariant->nType)) {
X
Xiaoyu Wang 已提交
455
    *result = (int64_t)pVariant->d;
456
  } else {
X
Xiaoyu Wang 已提交
457
    // TODO: handling var types
458
  }
H
Haojun Liao 已提交
459
#if 0
460
  errno = 0;
D
fix bug  
dapan1121 已提交
461
  if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || (pVariant->nType == TSDB_DATA_TYPE_BOOL)) {
462
    *result = pVariant->i;
463
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
464
    *result = pVariant->u;
465
  } else if (IS_FLOAT_TYPE(pVariant->nType)) {
H
Haojun Liao 已提交
466
    *result = (int64_t) pVariant->d;
467
  } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
H
Haojun Liao 已提交
468
    SToken token = {.z = pVariant->pz, .n = pVariant->nLen};
469
    /*int32_t n = */tGetToken(pVariant->pz, &token.type);
470

471 472
    if (token.type == TK_NULL) {
      if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
473
        taosMemoryFree(pVariant->pz);
474 475
        pVariant->nLen = 0;
      }
476

H
Haojun Liao 已提交
477
      setNull((char *)result, type, tDataTypes[type].bytes);
478 479
      return 0;
    }
480 481 482 483

    // decide if it is a valid number
    token.type = tGetNumericStringType(&token);
    if (token.type == TK_ILLEGAL) {
484 485
      return -1;
    }
486 487

    int64_t res = 0;
488 489
    int32_t t = tStrToInteger(token.z, token.type, token.n, &res, issigned);
    if (t != 0) {
490 491
      return -1;
    }
492 493

    if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
494
      taosMemoryFree(pVariant->pz);
495 496 497 498
      pVariant->nLen = 0;
    }

    *result = res;
499 500
  } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
    errno = 0;
wafwerar's avatar
wafwerar 已提交
501
    TdUcs4 *endPtr = NULL;
502
    
H
Haojun Liao 已提交
503
    SToken token = {0};
504
    token.n = tGetToken(pVariant->pz, &token.type);
505 506
    
    if (token.type == TK_MINUS || token.type == TK_PLUS) {
507
      token.n = tGetToken(pVariant->pz + token.n, &token.type);
508 509 510
    }
    
    if (token.type == TK_FLOAT) {
wafwerar's avatar
wafwerar 已提交
511
      double v = wcstod(pVariant->ucs4, &endPtr);
512
      if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
513
        taosMemoryFree(pVariant->pz);
514 515 516 517 518 519 520 521 522 523
        pVariant->nLen = 0;
      }
      
      if ((errno == ERANGE && v == -1) || (isinf(v) || isnan(v))) {
        return -1;
      }
      
      *result = (int64_t)v;
    } else if (token.type == TK_NULL) {
      if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
524
        taosMemoryFree(pVariant->pz);
525 526
        pVariant->nLen = 0;
      }
H
Haojun Liao 已提交
527
      setNull((char *)result, type, tDataTypes[type].bytes);
528 529
      return 0;
    } else {
wafwerar's avatar
wafwerar 已提交
530
      int64_t val = wcstoll(pVariant->ucs4, &endPtr, 10);
531
      if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
532
        taosMemoryFree(pVariant->pz);
533 534 535 536 537 538 539 540 541 542
        pVariant->nLen = 0;
      }
      
      if (errno == ERANGE) {
        return -1;  // data overflow
      }
      
      *result = val;
    }
  }
543

W
wpan 已提交
544 545 546 547
  if (converted) {
    *converted = true;
  }

548
  bool code = false;
549 550

  uint64_t ui = 0;
551 552 553 554 555 556 557 558 559 560
  switch(type) {
    case TSDB_DATA_TYPE_TINYINT:
      code = IS_VALID_TINYINT(*result); break;
    case TSDB_DATA_TYPE_SMALLINT:
      code = IS_VALID_SMALLINT(*result); break;
    case TSDB_DATA_TYPE_INT:
      code = IS_VALID_INT(*result); break;
    case TSDB_DATA_TYPE_BIGINT:
      code = IS_VALID_BIGINT(*result); break;
    case TSDB_DATA_TYPE_UTINYINT:
561 562
      ui = *result;
      code = IS_VALID_UTINYINT(ui); break;
563
    case TSDB_DATA_TYPE_USMALLINT:
564 565
      ui = *result;
      code = IS_VALID_USMALLINT(ui); break;
566
    case TSDB_DATA_TYPE_UINT:
567 568
      ui = *result;
      code = IS_VALID_UINT(ui); break;
569
    case TSDB_DATA_TYPE_UBIGINT:
570 571
      ui = *result;
      code = IS_VALID_UBIGINT(ui); break;
572
  }
573

H
Haojun Liao 已提交
574

575
  return code? 0:-1;
H
Haojun Liao 已提交
576 577
#endif
  return 0;
578 579
}

H
Haojun Liao 已提交
580
static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) {
581
  if (pVariant->nType == TSDB_DATA_TYPE_BOOL) {
582
    *pDest = pVariant->i;  // in order to be compatible to null of bool
583
  } else if (IS_NUMERIC_TYPE(pVariant->nType)) {
584
    *pDest = ((pVariant->i != 0) ? TSDB_TRUE : TSDB_FALSE);
585
  } else if (pVariant->nType == TSDB_DATA_TYPE_FLOAT || pVariant->nType == TSDB_DATA_TYPE_DOUBLE) {
H
Haojun Liao 已提交
586
    *pDest = ((pVariant->d != 0) ? TSDB_TRUE : TSDB_FALSE);
587 588 589 590 591
  } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
    int32_t ret = 0;
    if ((ret = convertToBoolImpl(pVariant->pz, pVariant->nLen)) < 0) {
      return ret;
    }
X
Xiaoyu Wang 已提交
592

593 594 595
    *pDest = ret;
  } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
    int32_t ret = 0;
wafwerar's avatar
wafwerar 已提交
596
    if ((ret = wcsconvertToBoolImpl(pVariant->ucs4, pVariant->nLen)) < 0) {
597 598 599 600 601 602
      return ret;
    }
    *pDest = ret;
  } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
    *pDest = TSDB_DATA_BOOL_NULL;
  }
X
Xiaoyu Wang 已提交
603

604 605 606 607 608 609 610 611
  assert(*pDest == TSDB_TRUE || *pDest == TSDB_FALSE || *pDest == TSDB_DATA_BOOL_NULL);
  return 0;
}

/*
 * transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType
 * to column type defined in schema
 */
X
Xiaoyu Wang 已提交
612 613
int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted,
                       char *extInfo) {
W
wpan 已提交
614 615 616
  if (converted) {
    *converted = false;
  }
X
Xiaoyu Wang 已提交
617

618
  if (pVariant == NULL || (pVariant->nType != 0 && !isValidDataType(pVariant->nType))) {
619 620
    return -1;
  }
621

622
  errno = 0;  // reset global error code
H
Haojun Liao 已提交
623
  int64_t result = 0;
624

625 626
  switch (type) {
    case TSDB_DATA_TYPE_BOOL: {
627
      if (convertToBool(pVariant, &result) < 0) {
628 629
        return -1;
      }
630 631

      *(int8_t *)payload = (int8_t)result;
632 633
      break;
    }
X
Xiaoyu Wang 已提交
634

635
    case TSDB_DATA_TYPE_TINYINT: {
W
wpan 已提交
636 637
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, result, INT8_MIN + 1, INT8_MAX, extInfo);
638 639
        return -1;
      }
X
Xiaoyu Wang 已提交
640
      *((int8_t *)payload) = (int8_t)result;
641 642 643 644
      break;
    }

    case TSDB_DATA_TYPE_UTINYINT: {
X
Xiaoyu Wang 已提交
645
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
W
wpan 已提交
646
        SET_EXT_INFO(converted, result, 0, UINT8_MAX - 1, extInfo);
647 648
        return -1;
      }
X
Xiaoyu Wang 已提交
649
      *((uint8_t *)payload) = (uint8_t)result;
650 651
      break;
    }
X
Xiaoyu Wang 已提交
652

653
    case TSDB_DATA_TYPE_SMALLINT: {
W
wpan 已提交
654 655
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, result, INT16_MIN + 1, INT16_MAX, extInfo);
656 657 658 659 660
        return -1;
      }
      *((int16_t *)payload) = (int16_t)result;
      break;
    }
661 662

    case TSDB_DATA_TYPE_USMALLINT: {
W
wpan 已提交
663 664
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
        SET_EXT_INFO(converted, result, 0, UINT16_MAX - 1, extInfo);
665 666 667 668 669
        return -1;
      }
      *((uint16_t *)payload) = (uint16_t)result;
      break;
    }
X
Xiaoyu Wang 已提交
670

671
    case TSDB_DATA_TYPE_INT: {
W
wpan 已提交
672 673
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, result, INT32_MIN + 1, INT32_MAX, extInfo);
674 675 676 677 678
        return -1;
      }
      *((int32_t *)payload) = (int32_t)result;
      break;
    }
679 680

    case TSDB_DATA_TYPE_UINT: {
W
wpan 已提交
681 682
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
        SET_EXT_INFO(converted, result, 0, UINT32_MAX - 1, extInfo);
683 684 685 686 687
        return -1;
      }
      *((uint32_t *)payload) = (uint32_t)result;
      break;
    }
X
Xiaoyu Wang 已提交
688

689
    case TSDB_DATA_TYPE_BIGINT: {
W
wpan 已提交
690 691
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, (int64_t)result, INT64_MIN + 1, INT64_MAX, extInfo);
692 693 694 695 696
        return -1;
      }
      *((int64_t *)payload) = (int64_t)result;
      break;
    }
697 698

    case TSDB_DATA_TYPE_UBIGINT: {
W
wpan 已提交
699 700
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
        SET_EXT_INFO(converted, (uint64_t)result, 0, UINT64_MAX - 1, extInfo);
701 702 703 704 705 706
        return -1;
      }
      *((uint64_t *)payload) = (uint64_t)result;
      break;
    }

707 708 709 710 711 712 713
    case TSDB_DATA_TYPE_FLOAT: {
      if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
        if (strncasecmp(TSDB_DATA_NULL_STR_L, pVariant->pz, pVariant->nLen) == 0 &&
            strlen(TSDB_DATA_NULL_STR_L) == pVariant->nLen) {
          *((int32_t *)payload) = TSDB_DATA_FLOAT_NULL;
          return 0;
        } else {
S
Shengliang Guan 已提交
714
          double  value = -1;
715
          int32_t ret = convertToDouble(pVariant->pz, pVariant->nLen, &value);
716 717 718 719
          if ((errno == ERANGE && (float)value == -1) || (ret != 0)) {
            return -1;
          }

W
wpan 已提交
720 721 722
          if (converted) {
            *converted = true;
          }
X
Xiaoyu Wang 已提交
723

W
wpan 已提交
724 725 726 727
          if (value > FLT_MAX || value < -FLT_MAX) {
            SET_EXT_INFO(converted, value, -FLT_MAX, FLT_MAX, extInfo);
            return -1;
          }
S
TD-1530  
Shengliang Guan 已提交
728
          SET_FLOAT_VAL(payload, value);
729
        }
X
Xiaoyu Wang 已提交
730 731
      } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) ||
                 IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
W
wpan 已提交
732 733 734
        if (converted) {
          *converted = true;
        }
X
Xiaoyu Wang 已提交
735 736

        if (pVariant->i > FLT_MAX || pVariant->i < -FLT_MAX) {
737
          SET_EXT_INFO(converted, pVariant->i, -FLT_MAX, FLT_MAX, extInfo);
W
wpan 已提交
738 739 740
          return -1;
        }

741
        SET_FLOAT_VAL(payload, pVariant->i);
742
      } else if (IS_FLOAT_TYPE(pVariant->nType)) {
W
wpan 已提交
743 744 745
        if (converted) {
          *converted = true;
        }
X
Xiaoyu Wang 已提交
746 747

        if (pVariant->d > FLT_MAX || pVariant->d < -FLT_MAX) {
H
Haojun Liao 已提交
748
          SET_EXT_INFO(converted, pVariant->d, -FLT_MAX, FLT_MAX, extInfo);
W
wpan 已提交
749 750
          return -1;
        }
X
Xiaoyu Wang 已提交
751

H
Haojun Liao 已提交
752
        SET_FLOAT_VAL(payload, pVariant->d);
753
      } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
754
        *((uint32_t *)payload) = TSDB_DATA_FLOAT_NULL;
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
        return 0;
      }

      float fv = GET_FLOAT_VAL(payload);
      if (isinf(fv) || isnan(fv) || fv > FLT_MAX || fv < -FLT_MAX) {
        return -1;
      }
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
      if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
        if (strncasecmp(TSDB_DATA_NULL_STR_L, pVariant->pz, pVariant->nLen) == 0 &&
            strlen(TSDB_DATA_NULL_STR_L) == pVariant->nLen) {
          *((int64_t *)payload) = TSDB_DATA_DOUBLE_NULL;
          return 0;
        } else {
          double  value = 0;
          int32_t ret;
          ret = convertToDouble(pVariant->pz, pVariant->nLen, &value);
          if ((errno == ERANGE && value == -1) || (ret != 0)) {
            return -1;
          }

S
TD-1530  
Shengliang Guan 已提交
778
          SET_DOUBLE_VAL(payload, value);
779
        }
X
Xiaoyu Wang 已提交
780 781
      } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) ||
                 IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
782
        SET_DOUBLE_VAL(payload, pVariant->i);
783
      } else if (IS_FLOAT_TYPE(pVariant->nType)) {
H
Haojun Liao 已提交
784
        SET_DOUBLE_VAL(payload, pVariant->d);
785 786 787 788 789 790
      } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
        *((int64_t *)payload) = TSDB_DATA_DOUBLE_NULL;
        return 0;
      }

      double dv = GET_DOUBLE_VAL(payload);
791
      if (errno == ERANGE || isinf(dv) || isnan(dv)) {
792 793
        return -1;
      }
794

795 796
      break;
    }
X
Xiaoyu Wang 已提交
797

798
    case TSDB_DATA_TYPE_BINARY: {
799 800
      if (!includeLengthPrefix) {
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
X
Xiaoyu Wang 已提交
801
          *(uint8_t *)payload = TSDB_DATA_BINARY_NULL;
802 803 804 805 806 807 808
        } else {
          if (pVariant->nType != TSDB_DATA_TYPE_BINARY) {
            toBinary(pVariant, &payload, &pVariant->nLen);
          } else {
            strncpy(payload, pVariant->pz, pVariant->nLen);
          }
        }
809
      } else {
810 811
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
          setVardataNull(payload, TSDB_DATA_TYPE_BINARY);
812
        } else {
813 814 815 816 817 818 819 820 821 822
          char *p = varDataVal(payload);

          if (pVariant->nType != TSDB_DATA_TYPE_BINARY) {
            toBinary(pVariant, &p, &pVariant->nLen);
          } else {
            strncpy(p, pVariant->pz, pVariant->nLen);
          }

          varDataSetLen(payload, pVariant->nLen);
          assert(p == varDataVal(payload));
823 824 825 826 827 828 829 830
        }
      }
      break;
    }
    case TSDB_DATA_TYPE_TIMESTAMP: {
      if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
        *((int64_t *)payload) = TSDB_DATA_BIGINT_NULL;
      } else {
831
        *((int64_t *)payload) = pVariant->i;
832 833 834 835
      }
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
L
liu0x54 已提交
836
      int32_t newlen = 0;
837 838 839 840 841
      if (!includeLengthPrefix) {
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
          *(uint32_t *)payload = TSDB_DATA_NCHAR_NULL;
        } else {
          if (pVariant->nType != TSDB_DATA_TYPE_NCHAR) {
842 843 844
            if (toNchar(pVariant, &payload, &newlen) != 0) {
              return -1;
            }
845
          } else {
X
Xiaoyu Wang 已提交
846
            tasoUcs4Copy((TdUcs4 *)payload, pVariant->ucs4, pVariant->nLen);
847 848
          }
        }
849
      } else {
850 851
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
          setVardataNull(payload, TSDB_DATA_TYPE_NCHAR);
852
        } else {
853 854 855
          char *p = varDataVal(payload);

          if (pVariant->nType != TSDB_DATA_TYPE_NCHAR) {
856 857 858
            if (toNchar(pVariant, &p, &newlen) != 0) {
              return -1;
            }
859
          } else {
wafwerar's avatar
wafwerar 已提交
860
            memcpy(p, pVariant->ucs4, pVariant->nLen);
L
liu0x54 已提交
861
            newlen = pVariant->nLen;
862 863
          }

L
liu0x54 已提交
864
          varDataSetLen(payload, newlen);  // the length may be changed after toNchar function called
865
          assert(p == varDataVal(payload));
866 867
        }
      }
X
Xiaoyu Wang 已提交
868

869 870 871
      break;
    }
  }
X
Xiaoyu Wang 已提交
872

873 874 875
  return 0;
}

H
Haojun Liao 已提交
876 877 878 879
/*
 * transfer data from variant serve as the implicit data conversion: from input sql string pVariant->nType
 * to column type defined in schema
 */
H
Haojun Liao 已提交
880
int32_t taosVariantDump(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix) {
H
Haojun Liao 已提交
881 882
  return tVariantDumpEx(pVariant, payload, type, includeLengthPrefix, NULL, NULL);
}
W
wpan 已提交
883

884 885 886 887 888 889
/*
 * In variant, bool/smallint/tinyint/int/bigint share the same attribution of
 * structure, also ignore the convert the type required
 *
 * It is actually the bigint/binary/bool/nchar type transfer
 */
890
int32_t taosVariantTypeSetType(SVariant *pVariant, char type) {
891 892 893
  if (pVariant == NULL || pVariant->nType == 0) {  // value is not set
    return 0;
  }
X
Xiaoyu Wang 已提交
894

895 896
  switch (type) {
    case TSDB_DATA_TYPE_BOOL: {  // bool
897
      if (convertToBool(pVariant, &pVariant->i) < 0) {
898 899
        return -1;
      }
X
Xiaoyu Wang 已提交
900

901 902 903 904 905 906 907
      pVariant->nType = type;
      break;
    }
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT: {
908
      convertToInteger(pVariant, &(pVariant->i), type, true, true, NULL);
909 910 911 912 913 914 915
      pVariant->nType = TSDB_DATA_TYPE_BIGINT;
      break;
    }
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE: {
      if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
        errno = 0;
wafwerar's avatar
wafwerar 已提交
916
        double v = taosStr2Double(pVariant->pz, NULL);
917
        if ((errno == ERANGE && v == -1) || (isinf(v) || isnan(v))) {
wafwerar's avatar
wafwerar 已提交
918
          taosMemoryFree(pVariant->pz);
919 920
          return -1;
        }
X
Xiaoyu Wang 已提交
921

wafwerar's avatar
wafwerar 已提交
922
        taosMemoryFree(pVariant->pz);
H
Haojun Liao 已提交
923
        pVariant->d = v;
924 925
      } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
        errno = 0;
wafwerar's avatar
wafwerar 已提交
926
        double v = wcstod(pVariant->ucs4, NULL);
927
        if ((errno == ERANGE && v == -1) || (isinf(v) || isnan(v))) {
wafwerar's avatar
wafwerar 已提交
928
          taosMemoryFree(pVariant->pz);
929 930
          return -1;
        }
X
Xiaoyu Wang 已提交
931

wafwerar's avatar
wafwerar 已提交
932
        taosMemoryFree(pVariant->pz);
H
Haojun Liao 已提交
933
        pVariant->d = v;
934
      } else if (pVariant->nType >= TSDB_DATA_TYPE_BOOL && pVariant->nType <= TSDB_DATA_TYPE_BIGINT) {
X
Xiaoyu Wang 已提交
935
        double tmp = (double)pVariant->i;
H
Haojun Liao 已提交
936
        pVariant->d = tmp;
937
      }
X
Xiaoyu Wang 已提交
938

939 940 941 942 943 944 945 946 947 948 949 950
      pVariant->nType = TSDB_DATA_TYPE_DOUBLE;
      break;
    }
    case TSDB_DATA_TYPE_BINARY: {
      if (pVariant->nType != TSDB_DATA_TYPE_BINARY) {
        toBinary(pVariant, &pVariant->pz, &pVariant->nLen);
      }
      pVariant->nType = type;
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
      if (pVariant->nType != TSDB_DATA_TYPE_NCHAR) {
951
        if (toNchar(pVariant, &pVariant->pz, &pVariant->nLen) != 0) {
952 953
          return -1;
        }
954 955 956 957 958
      }
      pVariant->nType = type;
      break;
    }
  }
X
Xiaoyu Wang 已提交
959

960
  return 0;
961
}
D
dapan1121 已提交
962

X
Xiaoyu Wang 已提交
963
char *taosVariantGet(SVariant *pVar, int32_t type) {
D
dapan1121 已提交
964
  switch (type) {
G
Ganlin Zhao 已提交
965
    case TSDB_DATA_TYPE_BOOL:
D
dapan1121 已提交
966 967
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
G
Ganlin Zhao 已提交
968
    case TSDB_DATA_TYPE_INT:
D
dapan1121 已提交
969 970 971
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      return (char *)&pVar->i;
G
Ganlin Zhao 已提交
972 973 974 975 976
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      return (char *)&pVar->u;
D
dapan1121 已提交
977 978 979 980
    case TSDB_DATA_TYPE_DOUBLE:
    case TSDB_DATA_TYPE_FLOAT:
      return (char *)&pVar->d;
    case TSDB_DATA_TYPE_BINARY:
wmmhello's avatar
wmmhello 已提交
981
    case TSDB_DATA_TYPE_JSON:
D
dapan1121 已提交
982 983 984
      return (char *)pVar->pz;
    case TSDB_DATA_TYPE_NCHAR:
      return (char *)pVar->ucs4;
G
Ganlin Zhao 已提交
985
    default:
D
dapan1121 已提交
986 987 988 989 990
      return NULL;
  }

  return NULL;
}