tvariant.c 30.6 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, bool *isSigned) {
39
  errno = 0;
X
Xiaoyu Wang 已提交
40
  char *endPtr = NULL;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

  int32_t index = 0;

  bool specifiedSign = (z[0] == '+' || z[0] == '-');
  if (specifiedSign) {
    *isSigned = true;
    index = 1;
  }

  uint64_t val = strtoull(&z[index], &endPtr, base);
  if (errno == ERANGE || errno == EINVAL) {
    errno = 0;
    return -1;
  }

  if (specifiedSign && val > INT64_MAX) {
    return -1;
  }

  if (endPtr - &z[index] != n - index) {
    return -1;
  }

  *isSigned = specifiedSign || (val <= INT64_MAX);
  if (*isSigned) {
X
Xiaoyu Wang 已提交
66
    *value = (z[0] == '-') ? -val : val;
67
  } else {
X
Xiaoyu Wang 已提交
68
    *(uint64_t *)value = val;
69 70 71 72 73
  }

  return 0;
}

X
Xiaoyu Wang 已提交
74
void taosVariantCreate(SVariant *pVar, const char *z, int32_t n, int32_t type) {
H
Haojun Liao 已提交
75
  int32_t ret = 0;
H
Haojun Liao 已提交
76
  memset(pVar, 0, sizeof(SVariant));
77

H
Haojun Liao 已提交
78
  switch (type) {
79
    case TSDB_DATA_TYPE_BOOL: {
H
Haojun Liao 已提交
80
      if (strncasecmp(z, "true", 4) == 0) {
81
        pVar->i = TSDB_TRUE;
H
Haojun Liao 已提交
82
      } else if (strncasecmp(z, "false", 5) == 0) {
83
        pVar->i = TSDB_FALSE;
84 85
      } else {
        return;
86 87 88
      }
      break;
    }
89

90 91 92
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_BIGINT:
X
Xiaoyu Wang 已提交
93
    case TSDB_DATA_TYPE_INT: {
94 95 96
      bool sign = true;

      int32_t base = 10;
97
      if (type == TK_NK_HEX) {
98
        base = 16;
99
      } else if (type == TK_NK_OCT) {
100
        base = 8;
101
      } else if (type == TK_NK_BIN) {
102 103 104
        base = 2;
      }

105
      ret = toInteger(z, n, base, &pVar->i, &sign);
106
      if (ret != 0) {
X
Xiaoyu Wang 已提交
107
        pVar->nType = -1;  // -1 means error type
108 109
        return;
      }
110

X
Xiaoyu Wang 已提交
111
      pVar->nType = (sign) ? TSDB_DATA_TYPE_BIGINT : TSDB_DATA_TYPE_UBIGINT;
112
      break;
113
    }
114
    case TSDB_DATA_TYPE_DOUBLE:
115
    case TSDB_DATA_TYPE_FLOAT: {
H
Haojun Liao 已提交
116
      pVar->d = strtod(z, NULL);
117
      break;
118
    }
119
    case TSDB_DATA_TYPE_BINARY: {
H
Haojun Liao 已提交
120
      pVar->pz = strndup(z, n);
121
      //pVar->nLen = strRmquote(pVar->pz, n);
122 123
      break;
    }
124
    case TSDB_DATA_TYPE_TIMESTAMP: {
125
      assert(0);
X
Xiaoyu Wang 已提交
126 127 128 129
      pVar->i = taosGetTimestamp(TSDB_TIME_PRECISION_NANO);
      break;
    }

130 131 132 133
    default: {  // nType == 0 means the null value
      type = TSDB_DATA_TYPE_NULL;
    }
  }
X
Xiaoyu Wang 已提交
134

135 136
  pVar->nType = type;
}
H
Haojun Liao 已提交
137

138
/**
H
Haojun Liao 已提交
139
 * create SVariant from binary string, not ascii data
140 141 142 143 144
 * @param pVar
 * @param pz
 * @param len
 * @param type
 */
H
Haojun Liao 已提交
145
void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uint32_t type) {
146 147 148
  switch (type) {
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT: {
149
      pVar->nLen = tDataTypes[type].bytes;
150
      pVar->i = GET_INT8_VAL(pz);
151 152 153
      break;
    }
    case TSDB_DATA_TYPE_UTINYINT: {
154
      pVar->nLen = tDataTypes[type].bytes;
155
      pVar->u = GET_UINT8_VAL(pz);
156 157 158
      break;
    }
    case TSDB_DATA_TYPE_SMALLINT: {
159
      pVar->nLen = tDataTypes[type].bytes;
160
      pVar->i = GET_INT16_VAL(pz);
161 162 163
      break;
    }
    case TSDB_DATA_TYPE_USMALLINT: {
164
      pVar->nLen = tDataTypes[type].bytes;
165
      pVar->u = GET_UINT16_VAL(pz);
166 167 168
      break;
    }
    case TSDB_DATA_TYPE_INT: {
169
      pVar->nLen = tDataTypes[type].bytes;
170
      pVar->i = GET_INT32_VAL(pz);
171 172 173
      break;
    }
    case TSDB_DATA_TYPE_UINT: {
174
      pVar->nLen = tDataTypes[type].bytes;
175
      pVar->u = GET_UINT32_VAL(pz);
176 177 178 179
      break;
    }
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP: {
180
      pVar->nLen = tDataTypes[type].bytes;
181
      pVar->i = GET_INT64_VAL(pz);
182 183 184
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
185
      pVar->nLen = tDataTypes[type].bytes;
186
      pVar->u = GET_UINT64_VAL(pz);
187 188 189
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
190
      pVar->nLen = tDataTypes[type].bytes;
H
Haojun Liao 已提交
191
      pVar->d = GET_DOUBLE_VAL(pz);
192 193 194
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
195
      pVar->nLen = tDataTypes[type].bytes;
H
Haojun Liao 已提交
196
      pVar->d = GET_FLOAT_VAL(pz);
197 198
      break;
    }
X
Xiaoyu Wang 已提交
199
    case TSDB_DATA_TYPE_NCHAR: {  // here we get the nchar length from raw binary bits length
200
      size_t lenInwchar = len / TSDB_NCHAR_SIZE;
201

wafwerar's avatar
wafwerar 已提交
202
      pVar->ucs4 = taosMemoryCalloc(1, (lenInwchar + 1) * TSDB_NCHAR_SIZE);
wafwerar's avatar
wafwerar 已提交
203
      memcpy(pVar->ucs4, pz, lenInwchar * TSDB_NCHAR_SIZE);
204
      pVar->nLen = (int32_t)len;
X
Xiaoyu Wang 已提交
205

206 207
      break;
    }
208
    case TSDB_DATA_TYPE_BINARY: {  // todo refactor, extract a method
wafwerar's avatar
wafwerar 已提交
209
      pVar->pz = taosMemoryCalloc(len + 1, sizeof(char));
210
      memcpy(pVar->pz, pz, len);
211
      pVar->nLen = (int32_t)len;
212 213
      break;
    }
X
Xiaoyu Wang 已提交
214

215
    default:
216
      pVar->i = GET_INT32_VAL(pz);
217
      pVar->nLen = tDataTypes[TSDB_DATA_TYPE_INT].bytes;
218
  }
X
Xiaoyu Wang 已提交
219

220 221 222
  pVar->nType = type;
}

H
Haojun Liao 已提交
223
void taosVariantDestroy(SVariant *pVar) {
224
  if (pVar == NULL) return;
X
Xiaoyu Wang 已提交
225

226
  if (pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR) {
wafwerar's avatar
wafwerar 已提交
227
    taosMemoryFreeClear(pVar->pz);
228 229
    pVar->nLen = 0;
  }
weixin_48148422's avatar
weixin_48148422 已提交
230 231

  // NOTE: this is only for string array
W
wpan 已提交
232
  if (pVar->nType == TSDB_DATA_TYPE_POINTER_ARRAY) {
weixin_48148422's avatar
weixin_48148422 已提交
233
    size_t num = taosArrayGetSize(pVar->arr);
X
Xiaoyu Wang 已提交
234 235
    for (size_t i = 0; i < num; i++) {
      void *p = taosArrayGetP(pVar->arr, i);
wafwerar's avatar
wafwerar 已提交
236
      taosMemoryFree(p);
weixin_48148422's avatar
weixin_48148422 已提交
237 238 239
    }
    taosArrayDestroy(pVar->arr);
    pVar->arr = NULL;
W
wpan 已提交
240 241 242
  } else if (pVar->nType == TSDB_DATA_TYPE_VALUE_ARRAY) {
    taosArrayDestroy(pVar->arr);
    pVar->arr = NULL;
weixin_48148422's avatar
weixin_48148422 已提交
243
  }
244 245
}

H
Haojun Liao 已提交
246
bool taosVariantIsValid(SVariant *pVar) {
247 248 249 250
  assert(pVar != NULL);
  return isValidDataType(pVar->nType);
}

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

H
Haojun Liao 已提交
254
  pDst->nType = pSrc->nType;
H
Haojun Liao 已提交
255
  if (pSrc->nType == TSDB_DATA_TYPE_BINARY || pSrc->nType == TSDB_DATA_TYPE_NCHAR) {
H
Haojun Liao 已提交
256
    int32_t len = pSrc->nLen + TSDB_NCHAR_SIZE;
wafwerar's avatar
wafwerar 已提交
257
    char   *p = taosMemoryRealloc(pDst->pz, len);
H
Haojun Liao 已提交
258 259
    assert(p);

H
Haojun Liao 已提交
260
    memset(p, 0, len);
H
Haojun Liao 已提交
261 262
    pDst->pz = p;

H
Haojun Liao 已提交
263 264
    memcpy(pDst->pz, pSrc->pz, pSrc->nLen);
    pDst->nLen = pSrc->nLen;
H
Haojun Liao 已提交
265 266 267
    return;
  }

268
  if (IS_NUMERIC_TYPE(pSrc->nType) || (pSrc->nType == TSDB_DATA_TYPE_BOOL)) {
269
    pDst->i = pSrc->i;
W
wpan 已提交
270
  } else if (pSrc->nType == TSDB_DATA_TYPE_POINTER_ARRAY) {  // this is only for string array
weixin_48148422's avatar
weixin_48148422 已提交
271
    size_t num = taosArrayGetSize(pSrc->arr);
X
Xiaoyu Wang 已提交
272 273 274 275
    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 已提交
276 277
      taosArrayPush(pDst->arr, &n);
    }
W
wpan 已提交
278
  } else if (pSrc->nType == TSDB_DATA_TYPE_VALUE_ARRAY) {
X
Xiaoyu Wang 已提交
279 280 281 282 283 284 285 286
    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);
    }
287
  }
H
Haojun Liao 已提交
288

W
wpan 已提交
289
  if (pDst->nType != TSDB_DATA_TYPE_POINTER_ARRAY && pDst->nType != TSDB_DATA_TYPE_VALUE_ARRAY) {
H
Haojun Liao 已提交
290
    pDst->nLen = tDataTypes[pDst->nType].bytes;
H
Haojun Liao 已提交
291
  }
292 293
}

X
Xiaoyu Wang 已提交
294
int32_t taosVariantCompare(const SVariant *p1, const SVariant *p2) {
H
Haojun Liao 已提交
295 296 297 298 299 300 301 302 303 304 305
  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;
  }
306

307 308 309 310
  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 已提交
311
      return p1->nLen > p2->nLen ? 1 : -1;
312 313
    }
  } else if (p1->nType == TSDB_DATA_TYPE_FLOAT || p1->nType == TSDB_DATA_TYPE_DOUBLE) {
H
Haojun Liao 已提交
314
    if (p1->d == p2->d) {
315 316
      return 0;
    } else {
X
Xiaoyu Wang 已提交
317
      return p1->d > p2->d ? 1 : -1;
318 319
    }
  } else if (IS_UNSIGNED_NUMERIC_TYPE(p1->nType)) {
320
    if (p1->u == p2->u) {
321 322
      return 0;
    } else {
X
Xiaoyu Wang 已提交
323
      return p1->u > p2->u ? 1 : -1;
324 325
    }
  } else {
326
    if (p1->i == p2->i) {
327 328
      return 0;
    } else {
X
Xiaoyu Wang 已提交
329
      return p1->i > p2->i ? 1 : -1;
330
    }
331 332 333
  }
}

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

337 338 339 340 341 342
  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 已提交
343

344 345
    case TSDB_DATA_TYPE_NCHAR: {
      dst[0] = '\'';
wafwerar's avatar
wafwerar 已提交
346
      taosUcs4ToMbs(pVar->ucs4, (taosUcs4len(pVar->ucs4) + 1) * TSDB_NCHAR_SIZE, dst + 1);
347
      int32_t len = (int32_t)strlen(dst);
348 349 350 351
      dst[len] = '\'';
      dst[len + 1] = 0;
      return len + 1;
    }
X
Xiaoyu Wang 已提交
352

353 354 355 356
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
357 358 359
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
360
      return sprintf(dst, "%d", (int32_t)pVar->i);
X
Xiaoyu Wang 已提交
361

362
    case TSDB_DATA_TYPE_BIGINT:
363
      return sprintf(dst, "%" PRId64, pVar->i);
364
    case TSDB_DATA_TYPE_UBIGINT:
365
      return sprintf(dst, "%" PRIu64, pVar->u);
366 367
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
H
Haojun Liao 已提交
368
      return sprintf(dst, "%.9lf", pVar->d);
X
Xiaoyu Wang 已提交
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    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 已提交
387
static FORCE_INLINE int32_t wcsconvertToBoolImpl(TdUcs4 *pstr, int32_t len) {
388 389 390 391
  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;
392 393
  } else if (memcmp(pstr, L"null", wcslen(L"null")) == 0) {
    return TSDB_DATA_BOOL_NULL;
394 395 396 397 398
  } else {
    return -1;
  }
}

H
Haojun Liao 已提交
399
static int32_t toBinary(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
400
  const int32_t INITIAL_ALLOC_SIZE = 40;
X
Xiaoyu Wang 已提交
401
  char         *pBuf = NULL;
402

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

408 409 410
  if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
    size_t newSize = pVariant->nLen * TSDB_NCHAR_SIZE;
    if (pBuf != NULL) {
dengyihao's avatar
dengyihao 已提交
411
      if (newSize >= INITIAL_ALLOC_SIZE) {
wafwerar's avatar
wafwerar 已提交
412
        pBuf = taosMemoryRealloc(pBuf, newSize + 1);
413
      }
X
Xiaoyu Wang 已提交
414

wafwerar's avatar
wafwerar 已提交
415
      taosUcs4ToMbs(pVariant->ucs4, (int32_t)newSize, pBuf);
wafwerar's avatar
wafwerar 已提交
416
      taosMemoryFree(pVariant->ucs4);
417 418
      pBuf[newSize] = 0;
    } else {
wafwerar's avatar
wafwerar 已提交
419
      taosUcs4ToMbs(pVariant->ucs4, (int32_t)newSize, *pDest);
420
    }
X
Xiaoyu Wang 已提交
421

422
  } else {
423
    if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) {
424
      sprintf(pBuf == NULL ? *pDest : pBuf, "%" PRId64, pVariant->i);
425
    } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) {
H
Haojun Liao 已提交
426
      sprintf(pBuf == NULL ? *pDest : pBuf, "%lf", pVariant->d);
427
    } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) {
428
      sprintf(pBuf == NULL ? *pDest : pBuf, "%s", (pVariant->i == TSDB_TRUE) ? "TRUE" : "FALSE");
429 430 431 432
    } else if (pVariant->nType == 0) {  // null data
      setNull(pBuf == NULL ? *pDest : pBuf, TSDB_DATA_TYPE_BINARY, 0);
    }
  }
X
Xiaoyu Wang 已提交
433

434 435 436
  if (pBuf != NULL) {
    *pDest = pBuf;
  }
X
Xiaoyu Wang 已提交
437

438
  *pDestSize = (int32_t)strlen(*pDest);
439 440 441
  return 0;
}

H
Haojun Liao 已提交
442
static int32_t toNchar(SVariant *pVariant, char **pDest, int32_t *pDestSize) {
443
  char tmpBuf[40] = {0};
X
Xiaoyu Wang 已提交
444 445

  char   *pDst = tmpBuf;
446
  int32_t nLen = 0;
447 448 449

  // convert the number to string, than convert it to wchar string.
  if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType)) {
450
    nLen = sprintf(pDst, "%" PRId64, pVariant->i);
451
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
X
Xiaoyu Wang 已提交
452
    nLen = sprintf(pDst, "%" PRIu64, pVariant->u);
453
  } else if (pVariant->nType == TSDB_DATA_TYPE_DOUBLE || pVariant->nType == TSDB_DATA_TYPE_FLOAT) {
H
Haojun Liao 已提交
454
    nLen = sprintf(pDst, "%lf", pVariant->d);
455 456 457 458
  } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
    pDst = pVariant->pz;
    nLen = pVariant->nLen;
  } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL) {
459
    nLen = sprintf(pDst, "%s", (pVariant->i == TSDB_TRUE) ? "TRUE" : "FALSE");
460
  }
X
Xiaoyu Wang 已提交
461

462
  if (*pDest == pVariant->pz) {
wafwerar's avatar
wafwerar 已提交
463
    TdUcs4 *pWStr = taosMemoryCalloc(1, (nLen + 1) * TSDB_NCHAR_SIZE);
wafwerar's avatar
wafwerar 已提交
464
    bool     ret = taosMbsToUcs4(pDst, nLen, pWStr, (nLen + 1) * TSDB_NCHAR_SIZE, NULL);
465
    if (!ret) {
wafwerar's avatar
wafwerar 已提交
466
      taosMemoryFreeClear(pWStr);
467 468 469
      return -1;
    }

470 471
    // free the binary buffer in the first place
    if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
wafwerar's avatar
wafwerar 已提交
472
      taosMemoryFree(pVariant->ucs4);
473
    }
X
Xiaoyu Wang 已提交
474

wafwerar's avatar
wafwerar 已提交
475 476
    pVariant->ucs4 = pWStr;
    *pDestSize = taosUcs4len(pVariant->ucs4);
X
Xiaoyu Wang 已提交
477

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

wafwerar's avatar
wafwerar 已提交
482
    pVariant->ucs4 = (TdUcs4 *)tmp;
483
  } else {
484
    int32_t output = 0;
485

wafwerar's avatar
wafwerar 已提交
486
    bool ret = taosMbsToUcs4(pDst, nLen, (TdUcs4*)*pDest, (nLen + 1) * TSDB_NCHAR_SIZE, &output);
487 488 489 490
    if (!ret) {
      return -1;
    }

H
hjxilinx 已提交
491
    if (pDestSize != NULL) {
492
      *pDestSize = output;
H
hjxilinx 已提交
493
    }
494
  }
X
Xiaoyu Wang 已提交
495

496 497 498 499
  return 0;
}

static FORCE_INLINE int32_t convertToDouble(char *pStr, int32_t len, double *value) {
X
Xiaoyu Wang 已提交
500 501 502 503 504 505
  //  SToken stoken = {.z = pStr, .n = len};
  //  if (TK_ILLEGAL == tGetNumericStringType(&stoken)) {
  //    return -1;
  //  }
  //
  //  *value = strtod(pStr, NULL);
506 507 508
  return 0;
}

X
Xiaoyu Wang 已提交
509 510
static FORCE_INLINE int32_t convertToInteger(SVariant *pVariant, int64_t *result, int32_t type, bool issigned,
                                             bool releaseVariantPtr, bool *converted) {
511
  if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
H
Haojun Liao 已提交
512
    setNull((char *)result, type, tDataTypes[type].bytes);
513 514
    return 0;
  }
515 516 517 518 519 520 521 522 523 524

  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)) {
    *result = (int64_t) pVariant->d;
  } else {
    //TODO: handling var types
  }
H
Haojun Liao 已提交
525
#if 0
526
  errno = 0;
D
fix bug  
dapan1121 已提交
527
  if (IS_SIGNED_NUMERIC_TYPE(pVariant->nType) || (pVariant->nType == TSDB_DATA_TYPE_BOOL)) {
528
    *result = pVariant->i;
529
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
530
    *result = pVariant->u;
531
  } else if (IS_FLOAT_TYPE(pVariant->nType)) {
H
Haojun Liao 已提交
532
    *result = (int64_t) pVariant->d;
533
  } else if (pVariant->nType == TSDB_DATA_TYPE_BINARY) {
H
Haojun Liao 已提交
534
    SToken token = {.z = pVariant->pz, .n = pVariant->nLen};
535
    /*int32_t n = */tGetToken(pVariant->pz, &token.type);
536

537 538
    if (token.type == TK_NULL) {
      if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
539
        taosMemoryFree(pVariant->pz);
540 541
        pVariant->nLen = 0;
      }
542

H
Haojun Liao 已提交
543
      setNull((char *)result, type, tDataTypes[type].bytes);
544 545
      return 0;
    }
546 547 548 549

    // decide if it is a valid number
    token.type = tGetNumericStringType(&token);
    if (token.type == TK_ILLEGAL) {
550 551
      return -1;
    }
552 553

    int64_t res = 0;
554 555
    int32_t t = tStrToInteger(token.z, token.type, token.n, &res, issigned);
    if (t != 0) {
556 557
      return -1;
    }
558 559

    if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
560
      taosMemoryFree(pVariant->pz);
561 562 563 564
      pVariant->nLen = 0;
    }

    *result = res;
565 566
  } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
    errno = 0;
wafwerar's avatar
wafwerar 已提交
567
    TdUcs4 *endPtr = NULL;
568
    
H
Haojun Liao 已提交
569
    SToken token = {0};
570
    token.n = tGetToken(pVariant->pz, &token.type);
571 572
    
    if (token.type == TK_MINUS || token.type == TK_PLUS) {
573
      token.n = tGetToken(pVariant->pz + token.n, &token.type);
574 575 576
    }
    
    if (token.type == TK_FLOAT) {
wafwerar's avatar
wafwerar 已提交
577
      double v = wcstod(pVariant->ucs4, &endPtr);
578
      if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
579
        taosMemoryFree(pVariant->pz);
580 581 582 583 584 585 586 587 588 589
        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 已提交
590
        taosMemoryFree(pVariant->pz);
591 592
        pVariant->nLen = 0;
      }
H
Haojun Liao 已提交
593
      setNull((char *)result, type, tDataTypes[type].bytes);
594 595
      return 0;
    } else {
wafwerar's avatar
wafwerar 已提交
596
      int64_t val = wcstoll(pVariant->ucs4, &endPtr, 10);
597
      if (releaseVariantPtr) {
wafwerar's avatar
wafwerar 已提交
598
        taosMemoryFree(pVariant->pz);
599 600 601 602 603 604 605 606 607 608
        pVariant->nLen = 0;
      }
      
      if (errno == ERANGE) {
        return -1;  // data overflow
      }
      
      *result = val;
    }
  }
609

W
wpan 已提交
610 611 612 613
  if (converted) {
    *converted = true;
  }

614
  bool code = false;
615 616

  uint64_t ui = 0;
617 618 619 620 621 622 623 624 625 626
  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:
627 628
      ui = *result;
      code = IS_VALID_UTINYINT(ui); break;
629
    case TSDB_DATA_TYPE_USMALLINT:
630 631
      ui = *result;
      code = IS_VALID_USMALLINT(ui); break;
632
    case TSDB_DATA_TYPE_UINT:
633 634
      ui = *result;
      code = IS_VALID_UINT(ui); break;
635
    case TSDB_DATA_TYPE_UBIGINT:
636 637
      ui = *result;
      code = IS_VALID_UBIGINT(ui); break;
638
  }
639

H
Haojun Liao 已提交
640

641
  return code? 0:-1;
H
Haojun Liao 已提交
642 643
#endif
  return 0;
644 645
}

H
Haojun Liao 已提交
646
static int32_t convertToBool(SVariant *pVariant, int64_t *pDest) {
647
  if (pVariant->nType == TSDB_DATA_TYPE_BOOL) {
648
    *pDest = pVariant->i;  // in order to be compatible to null of bool
649
  } else if (IS_NUMERIC_TYPE(pVariant->nType)) {
650
    *pDest = ((pVariant->i != 0) ? TSDB_TRUE : TSDB_FALSE);
651
  } else if (pVariant->nType == TSDB_DATA_TYPE_FLOAT || pVariant->nType == TSDB_DATA_TYPE_DOUBLE) {
H
Haojun Liao 已提交
652
    *pDest = ((pVariant->d != 0) ? TSDB_TRUE : TSDB_FALSE);
653 654 655 656 657
  } 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 已提交
658

659 660 661
    *pDest = ret;
  } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
    int32_t ret = 0;
wafwerar's avatar
wafwerar 已提交
662
    if ((ret = wcsconvertToBoolImpl(pVariant->ucs4, pVariant->nLen)) < 0) {
663 664 665 666 667 668
      return ret;
    }
    *pDest = ret;
  } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
    *pDest = TSDB_DATA_BOOL_NULL;
  }
X
Xiaoyu Wang 已提交
669

670 671 672 673 674 675 676 677
  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 已提交
678 679
int32_t tVariantDumpEx(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix, bool *converted,
                       char *extInfo) {
W
wpan 已提交
680 681 682
  if (converted) {
    *converted = false;
  }
X
Xiaoyu Wang 已提交
683

684
  if (pVariant == NULL || (pVariant->nType != 0 && !isValidDataType(pVariant->nType))) {
685 686
    return -1;
  }
687

688
  errno = 0;  // reset global error code
H
Haojun Liao 已提交
689
  int64_t result = 0;
690

691 692
  switch (type) {
    case TSDB_DATA_TYPE_BOOL: {
693
      if (convertToBool(pVariant, &result) < 0) {
694 695
        return -1;
      }
696 697

      *(int8_t *)payload = (int8_t)result;
698 699
      break;
    }
X
Xiaoyu Wang 已提交
700

701
    case TSDB_DATA_TYPE_TINYINT: {
W
wpan 已提交
702 703
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, result, INT8_MIN + 1, INT8_MAX, extInfo);
704 705
        return -1;
      }
X
Xiaoyu Wang 已提交
706
      *((int8_t *)payload) = (int8_t)result;
707 708 709 710
      break;
    }

    case TSDB_DATA_TYPE_UTINYINT: {
X
Xiaoyu Wang 已提交
711
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
W
wpan 已提交
712
        SET_EXT_INFO(converted, result, 0, UINT8_MAX - 1, extInfo);
713 714
        return -1;
      }
X
Xiaoyu Wang 已提交
715
      *((uint8_t *)payload) = (uint8_t)result;
716 717
      break;
    }
X
Xiaoyu Wang 已提交
718

719
    case TSDB_DATA_TYPE_SMALLINT: {
W
wpan 已提交
720 721
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, result, INT16_MIN + 1, INT16_MAX, extInfo);
722 723 724 725 726
        return -1;
      }
      *((int16_t *)payload) = (int16_t)result;
      break;
    }
727 728

    case TSDB_DATA_TYPE_USMALLINT: {
W
wpan 已提交
729 730
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
        SET_EXT_INFO(converted, result, 0, UINT16_MAX - 1, extInfo);
731 732 733 734 735
        return -1;
      }
      *((uint16_t *)payload) = (uint16_t)result;
      break;
    }
X
Xiaoyu Wang 已提交
736

737
    case TSDB_DATA_TYPE_INT: {
W
wpan 已提交
738 739
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, result, INT32_MIN + 1, INT32_MAX, extInfo);
740 741 742 743 744
        return -1;
      }
      *((int32_t *)payload) = (int32_t)result;
      break;
    }
745 746

    case TSDB_DATA_TYPE_UINT: {
W
wpan 已提交
747 748
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
        SET_EXT_INFO(converted, result, 0, UINT32_MAX - 1, extInfo);
749 750 751 752 753
        return -1;
      }
      *((uint32_t *)payload) = (uint32_t)result;
      break;
    }
X
Xiaoyu Wang 已提交
754

755
    case TSDB_DATA_TYPE_BIGINT: {
W
wpan 已提交
756 757
      if (convertToInteger(pVariant, &result, type, true, false, converted) < 0) {
        SET_EXT_INFO(converted, (int64_t)result, INT64_MIN + 1, INT64_MAX, extInfo);
758 759 760 761 762
        return -1;
      }
      *((int64_t *)payload) = (int64_t)result;
      break;
    }
763 764

    case TSDB_DATA_TYPE_UBIGINT: {
W
wpan 已提交
765 766
      if (convertToInteger(pVariant, &result, type, false, false, converted) < 0) {
        SET_EXT_INFO(converted, (uint64_t)result, 0, UINT64_MAX - 1, extInfo);
767 768 769 770 771 772
        return -1;
      }
      *((uint64_t *)payload) = (uint64_t)result;
      break;
    }

773 774 775 776 777 778 779
    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 已提交
780
          double  value = -1;
781
          int32_t ret = convertToDouble(pVariant->pz, pVariant->nLen, &value);
782 783 784 785
          if ((errno == ERANGE && (float)value == -1) || (ret != 0)) {
            return -1;
          }

W
wpan 已提交
786 787 788
          if (converted) {
            *converted = true;
          }
X
Xiaoyu Wang 已提交
789

W
wpan 已提交
790 791 792 793
          if (value > FLT_MAX || value < -FLT_MAX) {
            SET_EXT_INFO(converted, value, -FLT_MAX, FLT_MAX, extInfo);
            return -1;
          }
S
TD-1530  
Shengliang Guan 已提交
794
          SET_FLOAT_VAL(payload, value);
795
        }
X
Xiaoyu Wang 已提交
796 797
      } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) ||
                 IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
W
wpan 已提交
798 799 800
        if (converted) {
          *converted = true;
        }
X
Xiaoyu Wang 已提交
801 802

        if (pVariant->i > FLT_MAX || pVariant->i < -FLT_MAX) {
803
          SET_EXT_INFO(converted, pVariant->i, -FLT_MAX, FLT_MAX, extInfo);
W
wpan 已提交
804 805 806
          return -1;
        }

807
        SET_FLOAT_VAL(payload, pVariant->i);
808
      } else if (IS_FLOAT_TYPE(pVariant->nType)) {
W
wpan 已提交
809 810 811
        if (converted) {
          *converted = true;
        }
X
Xiaoyu Wang 已提交
812 813

        if (pVariant->d > FLT_MAX || pVariant->d < -FLT_MAX) {
H
Haojun Liao 已提交
814
          SET_EXT_INFO(converted, pVariant->d, -FLT_MAX, FLT_MAX, extInfo);
W
wpan 已提交
815 816
          return -1;
        }
X
Xiaoyu Wang 已提交
817

H
Haojun Liao 已提交
818
        SET_FLOAT_VAL(payload, pVariant->d);
819
      } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
820
        *((uint32_t *)payload) = TSDB_DATA_FLOAT_NULL;
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
        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 已提交
844
          SET_DOUBLE_VAL(payload, value);
845
        }
X
Xiaoyu Wang 已提交
846 847
      } else if (pVariant->nType == TSDB_DATA_TYPE_BOOL || IS_SIGNED_NUMERIC_TYPE(pVariant->nType) ||
                 IS_UNSIGNED_NUMERIC_TYPE(pVariant->nType)) {
848
        SET_DOUBLE_VAL(payload, pVariant->i);
849
      } else if (IS_FLOAT_TYPE(pVariant->nType)) {
H
Haojun Liao 已提交
850
        SET_DOUBLE_VAL(payload, pVariant->d);
851 852 853 854 855 856
      } else if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
        *((int64_t *)payload) = TSDB_DATA_DOUBLE_NULL;
        return 0;
      }

      double dv = GET_DOUBLE_VAL(payload);
857
      if (errno == ERANGE || isinf(dv) || isnan(dv)) {
858 859
        return -1;
      }
860

861 862
      break;
    }
X
Xiaoyu Wang 已提交
863

864
    case TSDB_DATA_TYPE_BINARY: {
865 866
      if (!includeLengthPrefix) {
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
X
Xiaoyu Wang 已提交
867
          *(uint8_t *)payload = TSDB_DATA_BINARY_NULL;
868 869 870 871 872 873 874
        } else {
          if (pVariant->nType != TSDB_DATA_TYPE_BINARY) {
            toBinary(pVariant, &payload, &pVariant->nLen);
          } else {
            strncpy(payload, pVariant->pz, pVariant->nLen);
          }
        }
875
      } else {
876 877
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
          setVardataNull(payload, TSDB_DATA_TYPE_BINARY);
878
        } else {
879 880 881 882 883 884 885 886 887 888
          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));
889 890 891 892 893 894 895 896
        }
      }
      break;
    }
    case TSDB_DATA_TYPE_TIMESTAMP: {
      if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
        *((int64_t *)payload) = TSDB_DATA_BIGINT_NULL;
      } else {
897
        *((int64_t *)payload) = pVariant->i;
898 899 900 901
      }
      break;
    }
    case TSDB_DATA_TYPE_NCHAR: {
L
liu0x54 已提交
902
      int32_t newlen = 0;
903 904 905 906 907
      if (!includeLengthPrefix) {
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
          *(uint32_t *)payload = TSDB_DATA_NCHAR_NULL;
        } else {
          if (pVariant->nType != TSDB_DATA_TYPE_NCHAR) {
908 909 910
            if (toNchar(pVariant, &payload, &newlen) != 0) {
              return -1;
            }
911
          } else {
wafwerar's avatar
wafwerar 已提交
912
            tasoUcs4Copy((TdUcs4*)payload, pVariant->ucs4, pVariant->nLen);
913 914
          }
        }
915
      } else {
916 917
        if (pVariant->nType == TSDB_DATA_TYPE_NULL) {
          setVardataNull(payload, TSDB_DATA_TYPE_NCHAR);
918
        } else {
919 920 921
          char *p = varDataVal(payload);

          if (pVariant->nType != TSDB_DATA_TYPE_NCHAR) {
922 923 924
            if (toNchar(pVariant, &p, &newlen) != 0) {
              return -1;
            }
925
          } else {
wafwerar's avatar
wafwerar 已提交
926
            memcpy(p, pVariant->ucs4, pVariant->nLen);
L
liu0x54 已提交
927
            newlen = pVariant->nLen;
928 929
          }

L
liu0x54 已提交
930
          varDataSetLen(payload, newlen);  // the length may be changed after toNchar function called
931
          assert(p == varDataVal(payload));
932 933
        }
      }
X
Xiaoyu Wang 已提交
934

935 936 937
      break;
    }
  }
X
Xiaoyu Wang 已提交
938

939 940 941
  return 0;
}

H
Haojun Liao 已提交
942 943 944 945
/*
 * 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 已提交
946
int32_t taosVariantDump(SVariant *pVariant, char *payload, int16_t type, bool includeLengthPrefix) {
H
Haojun Liao 已提交
947 948
  return tVariantDumpEx(pVariant, payload, type, includeLengthPrefix, NULL, NULL);
}
W
wpan 已提交
949

950 951 952 953 954 955
/*
 * 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
 */
956
int32_t taosVariantTypeSetType(SVariant *pVariant, char type) {
957 958 959
  if (pVariant == NULL || pVariant->nType == 0) {  // value is not set
    return 0;
  }
X
Xiaoyu Wang 已提交
960

961 962
  switch (type) {
    case TSDB_DATA_TYPE_BOOL: {  // bool
963
      if (convertToBool(pVariant, &pVariant->i) < 0) {
964 965
        return -1;
      }
X
Xiaoyu Wang 已提交
966

967 968 969 970 971 972 973
      pVariant->nType = type;
      break;
    }
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT: {
974
      convertToInteger(pVariant, &(pVariant->i), type, true, true, NULL);
975 976 977 978 979 980 981 982 983
      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;
        double v = strtod(pVariant->pz, NULL);
        if ((errno == ERANGE && v == -1) || (isinf(v) || isnan(v))) {
wafwerar's avatar
wafwerar 已提交
984
          taosMemoryFree(pVariant->pz);
985 986
          return -1;
        }
X
Xiaoyu Wang 已提交
987

wafwerar's avatar
wafwerar 已提交
988
        taosMemoryFree(pVariant->pz);
H
Haojun Liao 已提交
989
        pVariant->d = v;
990 991
      } else if (pVariant->nType == TSDB_DATA_TYPE_NCHAR) {
        errno = 0;
wafwerar's avatar
wafwerar 已提交
992
        double v = wcstod(pVariant->ucs4, NULL);
993
        if ((errno == ERANGE && v == -1) || (isinf(v) || isnan(v))) {
wafwerar's avatar
wafwerar 已提交
994
          taosMemoryFree(pVariant->pz);
995 996
          return -1;
        }
X
Xiaoyu Wang 已提交
997

wafwerar's avatar
wafwerar 已提交
998
        taosMemoryFree(pVariant->pz);
H
Haojun Liao 已提交
999
        pVariant->d = v;
1000
      } else if (pVariant->nType >= TSDB_DATA_TYPE_BOOL && pVariant->nType <= TSDB_DATA_TYPE_BIGINT) {
X
Xiaoyu Wang 已提交
1001
        double tmp = (double)pVariant->i;
H
Haojun Liao 已提交
1002
        pVariant->d = tmp;
1003
      }
X
Xiaoyu Wang 已提交
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
      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) {
1017
        if (toNchar(pVariant, &pVariant->pz, &pVariant->nLen) != 0) {
1018 1019
          return -1;
        }
1020 1021 1022 1023 1024
      }
      pVariant->nType = type;
      break;
    }
  }
X
Xiaoyu Wang 已提交
1025

1026
  return 0;
1027
}
D
dapan1121 已提交
1028 1029 1030

char * taosVariantGet(SVariant *pVar, int32_t type) {
  switch (type) {
G
Ganlin Zhao 已提交
1031
    case TSDB_DATA_TYPE_BOOL:
D
dapan1121 已提交
1032 1033
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
G
Ganlin Zhao 已提交
1034
    case TSDB_DATA_TYPE_INT:
D
dapan1121 已提交
1035 1036 1037
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      return (char *)&pVar->i;
G
Ganlin Zhao 已提交
1038 1039 1040 1041 1042
    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 已提交
1043 1044 1045 1046 1047 1048 1049
    case TSDB_DATA_TYPE_DOUBLE:
    case TSDB_DATA_TYPE_FLOAT:
      return (char *)&pVar->d;
    case TSDB_DATA_TYPE_BINARY:
      return (char *)pVar->pz;
    case TSDB_DATA_TYPE_NCHAR:
      return (char *)pVar->ucs4;
G
Ganlin Zhao 已提交
1050
    default:
D
dapan1121 已提交
1051 1052 1053 1054 1055
      return NULL;
  }

  return NULL;
}