sclfunc.c 65.1 KB
Newer Older
H
Haojun Liao 已提交
1 2
#include "function.h"
#include "scalar.h"
H
Haojun Liao 已提交
3
#include "sclInt.h"
D
dapan1121 已提交
4
#include "sclvector.h"
5
#include "tdatablock.h"
wmmhello's avatar
wmmhello 已提交
6
#include "tjson.h"
7 8
#include "ttime.h"
#include "vnode.h"
9

G
Ganlin Zhao 已提交
10 11 12 13 14
typedef float (*_float_fn)(float);
typedef double (*_double_fn)(double);
typedef double (*_double_fn_2)(double, double);
typedef int (*_conv_fn)(int);
typedef void (*_trim_fn)(char *, char*, int32_t, int32_t);
G
Ganlin Zhao 已提交
15
typedef int16_t (*_len_fn)(char *, int32_t);
G
Ganlin Zhao 已提交
16

17
/** Math functions **/
18 19 20 21 22
static double tlog(double v) {
  return log(v);
}

static double tlog2(double v, double base) {
23 24 25 26 27 28
  double a = log(v);
  double b = log(base);
  if (isnan(a) || isinf(a)) {
    return a;
  } else if (isnan(b) || isinf(b)) {
    return b;
29 30
  } else {
    return a / b;
31
  }
32 33
}

34
int32_t absFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Haojun Liao 已提交
35 36 37 38
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
39

H
Haojun Liao 已提交
40
  switch (type) {
41
    case TSDB_DATA_TYPE_FLOAT: {
H
Haojun Liao 已提交
42 43
      float *in  = (float *)pInputData->pData;
      float *out = (float *)pOutputData->pData;
44
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
45 46
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
47 48
          continue;
        }
49
        out[i] = (in[i] >= 0)? in[i] : -in[i];
50
      }
H
Haojun Liao 已提交
51
      break;
52 53 54
    }

    case TSDB_DATA_TYPE_DOUBLE: {
H
Haojun Liao 已提交
55 56
      double *in  = (double *)pInputData->pData;
      double *out = (double *)pOutputData->pData;
57
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
58 59
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
60 61
          continue;
        }
62
        out[i] = (in[i] >= 0)? in[i] : -in[i];
H
Haojun Liao 已提交
63 64
      }
      break;
65 66
    }

H
Haojun Liao 已提交
67
    case TSDB_DATA_TYPE_TINYINT: {
H
Haojun Liao 已提交
68 69
      int8_t *in  = (int8_t *)pInputData->pData;
      int8_t *out = (int8_t *)pOutputData->pData;
70
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
71 72
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
73 74
          continue;
        }
75
        out[i] = (in[i] >= 0)? in[i] : -in[i];
76
      }
H
Haojun Liao 已提交
77 78
      break;
    }
79

H
Haojun Liao 已提交
80
    case TSDB_DATA_TYPE_SMALLINT: {
H
Haojun Liao 已提交
81 82
      int16_t *in  = (int16_t *)pInputData->pData;
      int16_t *out = (int16_t *)pOutputData->pData;
83
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
84 85
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
86 87
          continue;
        }
88
        out[i] = (in[i] >= 0)? in[i] : -in[i];
89
      }
H
Haojun Liao 已提交
90 91
      break;
    }
92

H
Haojun Liao 已提交
93
    case TSDB_DATA_TYPE_INT: {
H
Haojun Liao 已提交
94 95
      int32_t *in  = (int32_t *)pInputData->pData;
      int32_t *out = (int32_t *)pOutputData->pData;
96
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
97 98
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
99 100
          continue;
        }
101
        out[i] = (in[i] >= 0)? in[i] : -in[i];
102
      }
H
Haojun Liao 已提交
103 104
      break;
    }
105

H
Haojun Liao 已提交
106
    case TSDB_DATA_TYPE_BIGINT: {
H
Haojun Liao 已提交
107 108
      int64_t *in  = (int64_t *)pInputData->pData;
      int64_t *out = (int64_t *)pOutputData->pData;
109
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
110 111
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
112 113
          continue;
        }
114
        out[i] = (in[i] >= 0)? in[i] : -in[i];
115
      }
H
Haojun Liao 已提交
116
      break;
117 118
    }

119 120 121 122 123 124 125
    case TSDB_DATA_TYPE_NULL: {
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        colDataAppendNULL(pOutputData, i);
      }
      break;
    }

H
Haojun Liao 已提交
126
    default: {
127
      colDataAssign(pOutputData, pInputData, pInput->numOfRows, NULL);
128 129
    }
  }
130

H
Haojun Liao 已提交
131
  pOutput->numOfRows = pInput->numOfRows;
132
  return TSDB_CODE_SUCCESS;
133 134
}

135
static int32_t doScalarFunctionUnique(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _double_fn valFn) {
H
Haojun Liao 已提交
136
  int32_t type = GET_PARAM_TYPE(pInput);
137

138
  SColumnInfoData *pInputData = pInput->columnData;
H
Haojun Liao 已提交
139
  SColumnInfoData *pOutputData = pOutput->columnData;
140

141
  _getDoubleValue_fn_t getValueFn = getVectorDoubleValueFn(type);
142

143
  double *out = (double *)pOutputData->pData;
144

145
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
146
    if (colDataIsNull_s(pInputData, i) || IS_NULL_TYPE(type)) {
147
      colDataAppendNULL(pOutputData, i);
148
      continue;
149
    }
150 151 152 153 154 155
    double result = valFn(getValueFn(pInputData->pData, i));
    if (isinf(result) || isnan(result)) {
      colDataAppendNULL(pOutputData, i);
    } else {
      out[i] = result;
    }
156
  }
157

H
Haojun Liao 已提交
158
  pOutput->numOfRows = pInput->numOfRows;
159
  return TSDB_CODE_SUCCESS;
160 161
}

162
static int32_t doScalarFunctionUnique2(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _double_fn_2 valFn) {
G
Ganlin Zhao 已提交
163
  SColumnInfoData *pInputData[2];
H
Haojun Liao 已提交
164
  SColumnInfoData *pOutputData = pOutput->columnData;
G
Ganlin Zhao 已提交
165
  _getDoubleValue_fn_t getValueFn[2];
166

G
Ganlin Zhao 已提交
167 168 169 170
  for (int32_t i = 0; i < inputNum; ++i) {
    pInputData[i] = pInput[i].columnData;
    getValueFn[i]= getVectorDoubleValueFn(GET_PARAM_TYPE(&pInput[i]));
  }
171

172
  double *out = (double *)pOutputData->pData;
173
  double result;
174

175 176 177
  bool hasNullType = (IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0])) ||
                      IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[1])));

wafwerar's avatar
wafwerar 已提交
178
  int32_t numOfRows = TMAX(pInput[0].numOfRows, pInput[1].numOfRows);
179 180 181
  if (pInput[0].numOfRows == pInput[1].numOfRows) {
    for (int32_t i = 0; i < numOfRows; ++i) {
      if (colDataIsNull_s(pInputData[0], i) ||
182 183
          colDataIsNull_s(pInputData[1], i) ||
          hasNullType) {
184 185 186 187 188 189 190 191 192
        colDataAppendNULL(pOutputData, i);
        continue;
      }
      result = valFn(getValueFn[0](pInputData[0]->pData, i), getValueFn[1](pInputData[1]->pData, i));
      if (isinf(result) || isnan(result)) {
        colDataAppendNULL(pOutputData, i);
      } else {
        out[i] = result;
      }
193
    }
194
  } else if (pInput[0].numOfRows == 1) { //left operand is constant
195
    if (colDataIsNull_s(pInputData[0], 0) || hasNullType) {
196
      colDataAppendNNULL(pOutputData, 0, pInput[1].numOfRows);
197
    } else {
198 199 200 201 202
      for (int32_t i = 0; i < numOfRows; ++i) {
        if (colDataIsNull_s(pInputData[1], i)) {
          colDataAppendNULL(pOutputData, i);
          continue;
        }
203

204 205 206 207 208 209 210 211
        result = valFn(getValueFn[0](pInputData[0]->pData, 0), getValueFn[1](pInputData[1]->pData, i));
        if (isinf(result) || isnan(result)) {
          colDataAppendNULL(pOutputData, i);
          continue;
        }

        out[i] = result;
      }
212
    }
213
  } else if (pInput[1].numOfRows == 1) {
214
    if (colDataIsNull_s(pInputData[1], 0) || hasNullType) {
215
      colDataAppendNNULL(pOutputData, 0, pInput[0].numOfRows);
216
    } else {
217 218 219 220 221 222 223 224 225 226 227 228 229 230
      for (int32_t i = 0; i < numOfRows; ++i) {
        if (colDataIsNull_s(pInputData[0], i)) {
          colDataAppendNULL(pOutputData, i);
          continue;
        }

        result = valFn(getValueFn[0](pInputData[0]->pData, i), getValueFn[1](pInputData[1]->pData, 0));
        if (isinf(result) || isnan(result)) {
          colDataAppendNULL(pOutputData, i);
          continue;
        }

        out[i] = result;
      }
231
    }
232
  }
233

234
  pOutput->numOfRows = numOfRows;
235 236 237
  return TSDB_CODE_SUCCESS;
}

238
static int32_t doScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _float_fn f1, _double_fn d1) {
H
Haojun Liao 已提交
239
  int32_t type = GET_PARAM_TYPE(pInput);
240

H
Haojun Liao 已提交
241 242
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;
243

H
Haojun Liao 已提交
244 245 246 247
  switch (type) {
    case TSDB_DATA_TYPE_FLOAT: {
      float *in  = (float *)pInputData->pData;
      float *out = (float *)pOutputData->pData;
248

249
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
250 251
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
252 253
          continue;
        }
254
        out[i] = f1(in[i]);
255
      }
H
Haojun Liao 已提交
256 257
      break;
    }
258

H
Haojun Liao 已提交
259 260 261
    case TSDB_DATA_TYPE_DOUBLE: {
      double *in  = (double *)pInputData->pData;
      double *out = (double *)pOutputData->pData;
262

263
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
264 265
        if (colDataIsNull_s(pInputData, i)) {
          colDataAppendNULL(pOutputData, i);
H
Haojun Liao 已提交
266 267
          continue;
        }
268
        out[i] = d1(in[i]);
269
      }
H
Haojun Liao 已提交
270 271 272
      break;
    }

273 274 275 276 277 278 279
    case TSDB_DATA_TYPE_NULL: {
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        colDataAppendNULL(pOutputData, i);
      }
      break;
    }

H
Haojun Liao 已提交
280
    default: {
281
      colDataAssign(pOutputData, pInputData, pInput->numOfRows, NULL);
282
    }
283 284
  }

H
Haojun Liao 已提交
285
  pOutput->numOfRows = pInput->numOfRows;
286 287
  return TSDB_CODE_SUCCESS;
}
288

G
Ganlin Zhao 已提交
289
/** String functions **/
290
static int16_t tlength(char *input, int32_t type) {
G
Ganlin Zhao 已提交
291 292 293
  return varDataLen(input);
}

294
static int16_t tcharlength(char *input, int32_t type) {
G
Ganlin Zhao 已提交
295 296 297 298
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    return varDataLen(input);
  } else { //NCHAR
    return varDataLen(input) / TSDB_NCHAR_SIZE;
G
Ganlin Zhao 已提交
299
  }
G
Ganlin Zhao 已提交
300
}
G
Ganlin Zhao 已提交
301

302
static void tltrim(char *input, char *output, int32_t type, int32_t charLen) {
G
Ganlin Zhao 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  int32_t numOfSpaces = 0;
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    for (int32_t i = 0; i < charLen; ++i) {
      if (!isspace(*(varDataVal(input) + i))) {
        break;
      }
      numOfSpaces++;
    }
  } else { //NCHAR
    for (int32_t i = 0; i < charLen; ++i) {
      if (!iswspace(*((uint32_t *)varDataVal(input) + i))) {
        break;
      }
      numOfSpaces++;
    }
  }
G
Ganlin Zhao 已提交
319

G
Ganlin Zhao 已提交
320 321 322 323 324 325 326 327
  int32_t resLen;
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    resLen = charLen - numOfSpaces;
    memcpy(varDataVal(output), varDataVal(input) + numOfSpaces, resLen);
  } else {
    resLen = (charLen - numOfSpaces) * TSDB_NCHAR_SIZE;
    memcpy(varDataVal(output), varDataVal(input) + numOfSpaces * TSDB_NCHAR_SIZE, resLen);
  }
G
Ganlin Zhao 已提交
328

G
Ganlin Zhao 已提交
329 330 331
  varDataSetLen(output, resLen);
}

332
static void trtrim(char *input, char *output, int32_t type, int32_t charLen) {
G
Ganlin Zhao 已提交
333 334 335 336 337 338 339 340 341
  int32_t numOfSpaces = 0;
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    for (int32_t i = charLen - 1; i >= 0; --i) {
      if (!isspace(*(varDataVal(input) + i))) {
        break;
      }
      numOfSpaces++;
    }
  } else { //NCHAR
342
    for (int32_t i = charLen - 1; i >= 0; --i) {
G
Ganlin Zhao 已提交
343 344 345 346
      if (!iswspace(*((uint32_t *)varDataVal(input) + i))) {
        break;
      }
      numOfSpaces++;
G
Ganlin Zhao 已提交
347
    }
G
Ganlin Zhao 已提交
348
  }
G
Ganlin Zhao 已提交
349

G
Ganlin Zhao 已提交
350 351 352 353 354
  int32_t resLen;
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    resLen = charLen - numOfSpaces;
  } else {
    resLen = (charLen - numOfSpaces) * TSDB_NCHAR_SIZE;
G
Ganlin Zhao 已提交
355
  }
G
Ganlin Zhao 已提交
356
  memcpy(varDataVal(output), varDataVal(input), resLen);
G
Ganlin Zhao 已提交
357

G
Ganlin Zhao 已提交
358
  varDataSetLen(output, resLen);
G
Ganlin Zhao 已提交
359 360
}

361
static int32_t doLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _len_fn lenFn) {
G
Ganlin Zhao 已提交
362 363 364 365 366
  int32_t type = GET_PARAM_TYPE(pInput);

  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

367 368
  ASSERT(pOutputData->info.type == TSDB_DATA_TYPE_BIGINT);
  int64_t *out = (int64_t *)pOutputData->pData;
G
Ganlin Zhao 已提交
369 370

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
371
    if (colDataIsNull_s(pInputData, i)) {
372
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
373 374 375
      continue;
    }

376
    char *in = colDataGetData(pInputData, i);
G
Ganlin Zhao 已提交
377
    out[i] = lenFn(in, type);
G
Ganlin Zhao 已提交
378 379 380 381 382
  }

  pOutput->numOfRows = pInput->numOfRows;
  return TSDB_CODE_SUCCESS;
}
383

384 385
static int32_t concatCopyHelper(const char *input, char *output, bool hasNchar, int32_t type, int16_t *dataLen) {
  if (hasNchar && type == TSDB_DATA_TYPE_VARCHAR) {
386
    TdUcs4 *newBuf = taosMemoryCalloc((varDataLen(input) + 1) * TSDB_NCHAR_SIZE, 1);
387 388
    int32_t len = varDataLen(input);
    bool ret = taosMbsToUcs4(varDataVal(input), len, newBuf, (varDataLen(input) + 1) * TSDB_NCHAR_SIZE, &len);
389 390 391 392 393 394 395 396 397 398 399 400
    if (!ret) {
      taosMemoryFree(newBuf);
      return TSDB_CODE_FAILED;
    }
    memcpy(varDataVal(output) + *dataLen, newBuf, varDataLen(input) * TSDB_NCHAR_SIZE);
    *dataLen += varDataLen(input) * TSDB_NCHAR_SIZE;
    taosMemoryFree(newBuf);
  } else {
    memcpy(varDataVal(output) + *dataLen, varDataVal(input), varDataLen(input));
    *dataLen += varDataLen(input);
  }
  return TSDB_CODE_SUCCESS;
401
}
G
Ganlin Zhao 已提交
402

403 404 405 406 407 408 409 410 411 412 413 414 415
static int32_t getNumOfNullEntries(SColumnInfoData *pColumnInfoData, int32_t numOfRows) {
  int32_t numOfNulls = 0;
  if (!pColumnInfoData->hasNull) {
    return numOfNulls;
  }
  for (int i = 0; i < numOfRows; ++i) {
    if (pColumnInfoData->varmeta.offset[i] == -1) {
      numOfNulls++;
    }
  }
  return numOfNulls;
}

G
Ganlin Zhao 已提交
416
int32_t concatFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
417
  int32_t ret = TSDB_CODE_SUCCESS;
G
Ganlin Zhao 已提交
418 419
  SColumnInfoData **pInputData = taosMemoryCalloc(inputNum, sizeof(SColumnInfoData *));
  SColumnInfoData *pOutputData = pOutput->columnData;
420
  char **input = taosMemoryCalloc(inputNum, POINTER_BYTES);
421
  char *outputBuf = NULL;
G
Ganlin Zhao 已提交
422

423
  int32_t inputLen = 0;
424
  int32_t numOfRows = 0;
425
  bool hasNchar = (GET_PARAM_TYPE(pOutput) == TSDB_DATA_TYPE_NCHAR) ? true : false;
426 427 428 429 430
  for (int32_t i = 0; i < inputNum; ++i) {
    if (pInput[i].numOfRows > numOfRows) {
      numOfRows = pInput[i].numOfRows;
    }
  }
G
Ganlin Zhao 已提交
431 432
  for (int32_t i = 0; i < inputNum; ++i) {
    pInputData[i] = pInput[i].columnData;
433
    int32_t factor = 1;
434
    if (hasNchar && (GET_PARAM_TYPE(&pInput[i]) == TSDB_DATA_TYPE_VARCHAR)) {
435 436
      factor = TSDB_NCHAR_SIZE;
    }
437 438

    int32_t numOfNulls = getNumOfNullEntries(pInputData[i], pInput[i].numOfRows);
439
    if (pInput[i].numOfRows == 1) {
440
      inputLen += (pInputData[i]->varmeta.length - VARSTR_HEADER_SIZE) * factor * (numOfRows - numOfNulls);
441
    } else {
442
      inputLen += (pInputData[i]->varmeta.length - (numOfRows - numOfNulls) * VARSTR_HEADER_SIZE) * factor;
443
    }
444 445
  }

446 447 448
  int32_t outputLen = inputLen + numOfRows * VARSTR_HEADER_SIZE;
  outputBuf = taosMemoryCalloc(outputLen, 1);
  char *output = outputBuf;
G
Ganlin Zhao 已提交
449

450
  for (int32_t k = 0; k < numOfRows; ++k) {
451
    bool hasNull = false;
G
Ganlin Zhao 已提交
452
    for (int32_t i = 0; i < inputNum; ++i) {
453
      if (colDataIsNull_s(pInputData[i], k) ||
454
          IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[i]))) {
455
        colDataAppendNULL(pOutputData, k);
G
Ganlin Zhao 已提交
456 457 458 459 460 461 462 463 464
        hasNull = true;
        break;
      }
    }

    if (hasNull) {
      continue;
    }

465

G
Ganlin Zhao 已提交
466 467
    int16_t dataLen = 0;
    for (int32_t i = 0; i < inputNum; ++i) {
468 469
      int32_t rowIdx = (pInput[i].numOfRows == 1) ? 0 : k;
      input[i] = colDataGetData(pInputData[i], rowIdx);
470 471 472 473

      ret = concatCopyHelper(input[i], output, hasNchar, GET_PARAM_TYPE(&pInput[i]), &dataLen);
      if (ret != TSDB_CODE_SUCCESS) {
        goto DONE;
474
      }
G
Ganlin Zhao 已提交
475
    }
476
    varDataSetLen(output, dataLen);
477 478
    colDataAppend(pOutputData, k, output, false);
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
479 480
  }

481
  pOutput->numOfRows = numOfRows;
482 483

DONE:
484
  taosMemoryFree(input);
485
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
486 487
  taosMemoryFree(pInputData);

488
  return ret;
G
Ganlin Zhao 已提交
489 490
}

491

G
Ganlin Zhao 已提交
492
int32_t concatWsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
493
  int32_t ret = TSDB_CODE_SUCCESS;
G
Ganlin Zhao 已提交
494 495
  SColumnInfoData **pInputData = taosMemoryCalloc(inputNum, sizeof(SColumnInfoData *));
  SColumnInfoData *pOutputData = pOutput->columnData;
496
  char **input = taosMemoryCalloc(inputNum, POINTER_BYTES);
497
  char *outputBuf = NULL;
G
Ganlin Zhao 已提交
498

499
  int32_t inputLen = 0;
500
  int32_t numOfRows = 0;
501
  bool hasNchar = (GET_PARAM_TYPE(pOutput) == TSDB_DATA_TYPE_NCHAR) ? true : false;
502
  for (int32_t i = 1; i < inputNum; ++i) {
503 504 505 506
    if (pInput[i].numOfRows > numOfRows) {
      numOfRows = pInput[i].numOfRows;
    }
  }
G
Ganlin Zhao 已提交
507 508
  for (int32_t i = 0; i < inputNum; ++i) {
    pInputData[i] = pInput[i].columnData;
509
    int32_t factor = 1;
510
    if (hasNchar && (GET_PARAM_TYPE(&pInput[i]) == TSDB_DATA_TYPE_VARCHAR)) {
511 512
      factor = TSDB_NCHAR_SIZE;
    }
513 514

    int32_t numOfNulls = getNumOfNullEntries(pInputData[i], pInput[i].numOfRows);
515 516
    if (i == 0) {
      // calculate required separator space
517
      inputLen += (pInputData[0]->varmeta.length - VARSTR_HEADER_SIZE) * (numOfRows - numOfNulls) * (inputNum - 2) * factor;
518
    } else if (pInput[i].numOfRows == 1) {
519
      inputLen += (pInputData[i]->varmeta.length - VARSTR_HEADER_SIZE) * (numOfRows - numOfNulls) * factor;
520
    } else {
521
      inputLen += (pInputData[i]->varmeta.length - (numOfRows - numOfNulls) * VARSTR_HEADER_SIZE) * factor;
522 523 524
    }
  }

525 526 527
  int32_t outputLen = inputLen + numOfRows * VARSTR_HEADER_SIZE;
  outputBuf = taosMemoryCalloc(outputLen, 1);
  char *output = outputBuf;
G
Ganlin Zhao 已提交
528

529
  for (int32_t k = 0; k < numOfRows; ++k) {
530
    if (colDataIsNull_s(pInputData[0], k) ||
531
        IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0]))) {
532
      colDataAppendNULL(pOutputData, k);
G
Ganlin Zhao 已提交
533 534 535 536
      continue;
    }

    int16_t dataLen = 0;
537
    bool hasNull = false;
G
Ganlin Zhao 已提交
538
    for (int32_t i = 1; i < inputNum; ++i) {
539
      if (colDataIsNull_s(pInputData[i], k) ||
540
          IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[i]))) {
541 542
        hasNull = true;
        break;
G
Ganlin Zhao 已提交
543 544
      }

545 546
      int32_t rowIdx = (pInput[i].numOfRows == 1) ? 0 : k;
      input[i] = colDataGetData(pInputData[i], rowIdx);
547

548 549 550
      ret = concatCopyHelper(input[i], output, hasNchar, GET_PARAM_TYPE(&pInput[i]), &dataLen);
      if (ret != TSDB_CODE_SUCCESS) {
        goto DONE;
551
      }
G
Ganlin Zhao 已提交
552

553

G
Ganlin Zhao 已提交
554 555
      if (i < inputNum - 1) {
        //insert the separator
556 557
        char *sep = (pInput[0].numOfRows == 1) ? colDataGetData(pInputData[0], 0) : colDataGetData(pInputData[0], k);
         ret = concatCopyHelper(sep, output, hasNchar, GET_PARAM_TYPE(&pInput[0]), &dataLen);
558
        if (ret != TSDB_CODE_SUCCESS) {
559
          goto DONE;
560
        }
G
Ganlin Zhao 已提交
561 562
      }
    }
563 564 565 566 567 568 569 570 571

    if (hasNull) {
      colDataAppendNULL(pOutputData, k);
      memset(output, 0, dataLen);
    } else {
      varDataSetLen(output, dataLen);
      colDataAppend(pOutputData, k, output, false);
      output += varDataTLen(output);
    }
G
Ganlin Zhao 已提交
572 573
  }

574
  pOutput->numOfRows = numOfRows;
575 576

DONE:
577
  taosMemoryFree(input);
578
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
579 580
  taosMemoryFree(pInputData);

581
  return ret;
G
Ganlin Zhao 已提交
582 583
}

584
static int32_t doCaseConvFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _conv_fn convFn) {
G
Ganlin Zhao 已提交
585 586 587 588 589
  int32_t type = GET_PARAM_TYPE(pInput);

  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

590 591
  int32_t outputLen = pInputData->varmeta.length;
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
592
  char *output = outputBuf;
593

G
Ganlin Zhao 已提交
594
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
595 596
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
597 598 599
      continue;
    }

600
    char *input = colDataGetData(pInput[0].columnData, i);
601
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
602 603
    if (type == TSDB_DATA_TYPE_VARCHAR) {
      for (int32_t j = 0; j < len; ++j) {
604
        *(varDataVal(output) + j) = convFn(*(varDataVal(input) + j));
G
Ganlin Zhao 已提交
605 606 607
      }
    } else { //NCHAR
      for (int32_t j = 0; j < len / TSDB_NCHAR_SIZE; ++j) {
608
        *((uint32_t *)varDataVal(output) + j) = convFn(*((uint32_t *)varDataVal(input) + j));
G
Ganlin Zhao 已提交
609 610
      }
    }
611
    varDataSetLen(output, len);
612
    colDataAppend(pOutputData, i, output, false);
613
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
614 615 616
  }

  pOutput->numOfRows = pInput->numOfRows;
617
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
618 619 620 621 622

  return TSDB_CODE_SUCCESS;
}


623
static int32_t doTrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _trim_fn trimFn) {
G
Ganlin Zhao 已提交
624 625 626 627 628
  int32_t type = GET_PARAM_TYPE(pInput);

  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

629 630
  int32_t outputLen = pInputData->varmeta.length;
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
631
  char *output = outputBuf;
632

G
Ganlin Zhao 已提交
633
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
634 635
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
636 637 638
      continue;
    }

639
    char *input = colDataGetData(pInputData, i);
640
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
641
    int32_t charLen = (type == TSDB_DATA_TYPE_VARCHAR) ? len : len / TSDB_NCHAR_SIZE;
642 643 644
    trimFn(input, output, type, charLen);

    varDataSetLen(output, len);
645
    colDataAppend(pOutputData, i, output, false);
646
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
647 648 649
  }

  pOutput->numOfRows = pInput->numOfRows;
650
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667

  return TSDB_CODE_SUCCESS;
}

int32_t substrFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t subPos = 0;
  GET_TYPED_DATA(subPos, int32_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);

  int32_t subLen = INT16_MAX;
  if (inputNum == 3) {
    GET_TYPED_DATA(subLen, int32_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
    subLen = (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? subLen : subLen * TSDB_NCHAR_SIZE;
  }

  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

668
  int32_t outputLen = pInputData->varmeta.length * pInput->numOfRows;
669
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
670
  char *output = outputBuf;
671 672

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
673 674
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
675 676
      continue;
    }
677
    char *input = colDataGetData(pInput[0].columnData, i);
678
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
679 680 681 682
    int32_t startPosBytes;

    if (subPos > 0) {
      startPosBytes = (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? subPos - 1 : (subPos - 1) * TSDB_NCHAR_SIZE;
wafwerar's avatar
wafwerar 已提交
683
      startPosBytes = TMIN(startPosBytes, len);
G
Ganlin Zhao 已提交
684 685
    } else {
      startPosBytes = (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? len + subPos : len + subPos * TSDB_NCHAR_SIZE;
wafwerar's avatar
wafwerar 已提交
686
      startPosBytes = TMAX(startPosBytes, 0);
G
Ganlin Zhao 已提交
687 688
    }

wafwerar's avatar
wafwerar 已提交
689
    int32_t resLen = TMIN(subLen, len - startPosBytes);
690 691
    if (resLen > 0) {
      memcpy(varDataVal(output), varDataVal(input) + startPosBytes, resLen);
G
Ganlin Zhao 已提交
692 693
    }

694
    varDataSetLen(output, resLen);
695
    colDataAppend(pOutputData, i , output, false);
696
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
697 698 699
  }

  pOutput->numOfRows = pInput->numOfRows;
700
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
701 702 703 704

  return TSDB_CODE_SUCCESS;
}

705
/** Conversion functions **/
G
Ganlin Zhao 已提交
706
int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
707
  int16_t inputType  = GET_PARAM_TYPE(&pInput[0]);
708
  int16_t inputLen   = GET_PARAM_BYTES(&pInput[0]);
709 710
  int16_t outputType = GET_PARAM_TYPE(&pOutput[0]);
  int64_t outputLen  = GET_PARAM_BYTES(&pOutput[0]);
G
Ganlin Zhao 已提交
711

wafwerar's avatar
wafwerar 已提交
712
  char *outputBuf = taosMemoryCalloc(outputLen * pInput[0].numOfRows + 1, 1);
G
Ganlin Zhao 已提交
713 714 715 716 717 718 719
  char *output = outputBuf;

  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }
720

721
    char *input = colDataGetData(pInput[0].columnData, i);
G
Ganlin Zhao 已提交
722 723

    switch(outputType) {
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
      case TSDB_DATA_TYPE_TINYINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(int8_t *)output = taosStr2Int8(varDataVal(input), NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(int8_t *)output = taosStr2Int8(newBuf, NULL, 10);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(int8_t *)output, int8_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_SMALLINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(int16_t *)output = taosStr2Int16(varDataVal(input), NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(int16_t *)output = taosStr2Int16(newBuf, NULL, 10);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(int16_t *)output, int16_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_INT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(int32_t *)output = taosStr2Int32(varDataVal(input), NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(int32_t *)output = taosStr2Int32(newBuf, NULL, 10);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(int32_t *)output, int32_t, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
778 779
      case TSDB_DATA_TYPE_BIGINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
780
          *(int64_t *)output = taosStr2Int64(varDataVal(input), NULL, 10);
G
Ganlin Zhao 已提交
781
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
782
          char *newBuf = taosMemoryCalloc(1, inputLen);
G
Ganlin Zhao 已提交
783 784 785 786 787 788
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
wafwerar's avatar
wafwerar 已提交
789
          *(int64_t *)output = taosStr2Int64(newBuf, NULL, 10);
G
Ganlin Zhao 已提交
790 791 792 793 794 795
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(int64_t *)output, int64_t, inputType, input);
        }
        break;
      }
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
      case TSDB_DATA_TYPE_UTINYINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(uint8_t *)output = taosStr2UInt8(varDataVal(input), NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(uint8_t *)output = taosStr2UInt8(newBuf, NULL, 10);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(uint8_t *)output, uint8_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_USMALLINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(uint16_t *)output = taosStr2UInt16(varDataVal(input), NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(uint16_t *)output = taosStr2UInt16(newBuf, NULL, 10);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(uint16_t *)output, uint16_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_UINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(uint32_t *)output = taosStr2UInt32(varDataVal(input), NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(uint32_t *)output = taosStr2UInt32(newBuf, NULL, 10);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(uint32_t *)output, uint32_t, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
850 851
      case TSDB_DATA_TYPE_UBIGINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
852
          *(uint64_t *)output = taosStr2UInt64(varDataVal(input), NULL, 10);
G
Ganlin Zhao 已提交
853
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
854
          char *newBuf = taosMemoryCalloc(1, inputLen);
G
Ganlin Zhao 已提交
855 856 857 858 859 860
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
wafwerar's avatar
wafwerar 已提交
861
          *(uint64_t *)output = taosStr2UInt64(newBuf, NULL, 10);
G
Ganlin Zhao 已提交
862 863 864 865 866 867
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(uint64_t *)output, uint64_t, inputType, input);
        }
        break;
      }
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
      case TSDB_DATA_TYPE_FLOAT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(float *)output = taosStr2Float(varDataVal(input), NULL);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(float *)output = taosStr2Float(newBuf, NULL);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(float *)output, float, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(double *)output = taosStr2Double(varDataVal(input), NULL);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(double *)output = taosStr2Double(newBuf, NULL);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(double *)output, double, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
      case TSDB_DATA_TYPE_BOOL: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          *(bool *)output = taosStr2Int8(varDataVal(input), NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(bool *)output = taosStr2Int8(newBuf, NULL, 10);
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(bool *)output, bool, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
922
      case TSDB_DATA_TYPE_TIMESTAMP: {
923
        int64_t timeVal;
G
Ganlin Zhao 已提交
924
        if (inputType == TSDB_DATA_TYPE_BINARY || inputType == TSDB_DATA_TYPE_NCHAR) {
925 926 927
          int64_t timePrec;
          GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);
          int32_t ret = convertStringToTimestamp(inputType, input, timePrec, &timeVal);
928 929 930 931 932
          if (ret != TSDB_CODE_SUCCESS) {
            *(int64_t *)output = 0;
          } else {
            *(int64_t *)output = timeVal;
          }
G
Ganlin Zhao 已提交
933 934 935 936 937 938 939 940 941 942
        } else {
          GET_TYPED_DATA(*(int64_t *)output, int64_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_BINARY: {
        if (inputType == TSDB_DATA_TYPE_BOOL) {
          int32_t len = sprintf(varDataVal(output), "%.*s", (int32_t)(outputLen - VARSTR_HEADER_SIZE), *(int8_t *)input ? "true" : "false");
          varDataSetLen(output, len);
        } else if (inputType == TSDB_DATA_TYPE_BINARY) {
wafwerar's avatar
wafwerar 已提交
943
          int32_t len = TMIN(varDataLen(input), outputLen - VARSTR_HEADER_SIZE);
944
          len = sprintf(varDataVal(output), "%.*s", len, varDataVal(input));
G
Ganlin Zhao 已提交
945
          varDataSetLen(output, len);
946
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
947 948 949 950 951 952 953 954 955 956
          char *newBuf = taosMemoryCalloc(1, inputLen);
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          len = TMIN(len, outputLen - VARSTR_HEADER_SIZE);
          memcpy(varDataVal(output), newBuf, len);
          varDataSetLen(output, len);
          taosMemoryFree(newBuf);
G
Ganlin Zhao 已提交
957 958 959 960 961 962 963 964 965 966 967 968
        } else {
          char tmp[400] = {0};
          NUM_TO_STRING(inputType, input, sizeof(tmp), tmp);
          int32_t len = (int32_t)strlen(tmp);
          len = (outputLen - VARSTR_HEADER_SIZE) > len ? len : (outputLen - VARSTR_HEADER_SIZE);
          memcpy(varDataVal(output), tmp, len);
          varDataSetLen(output, len);
        }
        break;
      }
      case TSDB_DATA_TYPE_NCHAR: {
        int32_t outputCharLen = (outputLen - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
969
        int32_t len;
G
Ganlin Zhao 已提交
970 971
        if (inputType == TSDB_DATA_TYPE_BOOL) {
          char tmp[8] = {0};
972
          len = sprintf(tmp, "%.*s", outputCharLen, *(int8_t *)input ? "true" : "false" );
G
Ganlin Zhao 已提交
973 974 975 976 977 978
          bool ret = taosMbsToUcs4(tmp, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len);
          if (!ret) {
            return TSDB_CODE_FAILED;
          }
          varDataSetLen(output, len);
        } else if (inputType == TSDB_DATA_TYPE_BINARY) {
979
          len = outputCharLen > varDataLen(input) ? varDataLen(input) : outputCharLen;
G
Ganlin Zhao 已提交
980 981 982 983 984 985
          bool ret = taosMbsToUcs4(input + VARSTR_HEADER_SIZE, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len);
          if (!ret) {
            return TSDB_CODE_FAILED;
          }
          varDataSetLen(output, len);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
986 987 988
          len = TMIN(outputLen - VARSTR_HEADER_SIZE, varDataLen(input));
          memcpy(output, input, len + VARSTR_HEADER_SIZE);
          varDataSetLen(output, len);
G
Ganlin Zhao 已提交
989 990 991
        } else {
          char tmp[400] = {0};
          NUM_TO_STRING(inputType, input, sizeof(tmp), tmp);
992
          len = (int32_t)strlen(tmp);
G
Ganlin Zhao 已提交
993 994 995 996 997 998 999
          len = outputCharLen > len ? len : outputCharLen;
          bool ret = taosMbsToUcs4(tmp, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len);
          if (!ret) {
            return TSDB_CODE_FAILED;
          }
          varDataSetLen(output, len);
        }
1000
        //for constant conversion, need to set proper length of pOutput description
G
Ganlin Zhao 已提交
1001
        if (len < outputLen) {
D
dapan1121 已提交
1002
          pOutput->columnData->info.bytes = len + VARSTR_HEADER_SIZE;
1003
        }
G
Ganlin Zhao 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
        break;
      }
      default: {
        return TSDB_CODE_FAILED;
      }
    }

    colDataAppend(pOutput->columnData, i, output, false);
    if (IS_VAR_DATA_TYPE(outputType)) {
      output += varDataTLen(output);
    } else {
      output += tDataTypes[outputType].bytes;
    }
  }

  pOutput->numOfRows = pInput->numOfRows;
  taosMemoryFree(outputBuf);
  return TSDB_CODE_SUCCESS;
}
G
Ganlin Zhao 已提交
1023

1024 1025 1026
int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);

1027
  bool tzPresent = (inputNum == 2) ? true : false;
1028 1029
  char* tz;
  int32_t tzLen;
1030 1031 1032 1033
  if (tzPresent) {
    tz = varDataVal(pInput[1].columnData->pData);
    tzLen = varDataLen(pInput[1].columnData->pData);
  }
1034

1035 1036 1037 1038 1039 1040
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }

1041
    char *input = colDataGetData(pInput[0].columnData, i);
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
    char fraction[20] = {0};
    bool hasFraction = false;
    NUM_TO_STRING(type, input, sizeof(fraction), fraction);
    int32_t tsDigits = (int32_t)strlen(fraction);

    char buf[64] = {0};
    int64_t timeVal;
    GET_TYPED_DATA(timeVal, int64_t, type, input);
    if (tsDigits > TSDB_TIME_PRECISION_SEC_DIGITS) {
      if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
        timeVal = timeVal / 1000;
      } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
        timeVal = timeVal / (1000 * 1000);
      } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
        timeVal = timeVal / (1000 * 1000 * 1000);
      } else {
1058 1059
        colDataAppendNULL(pOutput->columnData, i);
        continue;
1060 1061 1062 1063 1064
      }
      hasFraction = true;
      memmove(fraction, fraction + TSDB_TIME_PRECISION_SEC_DIGITS, TSDB_TIME_PRECISION_SEC_DIGITS);
    }

1065
    struct tm *tmInfo = taosLocalTime((const time_t *)&timeVal, NULL);
1066
    strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", tmInfo);
1067 1068
    int32_t len = (int32_t)strlen(buf);

1069 1070 1071 1072
    //add timezone string
    snprintf(buf + len, tzLen + 1, "%s", tz);
    len += tzLen;

1073 1074
    if (hasFraction) {
      int32_t fracLen = (int32_t)strlen(fraction) + 1;
1075 1076 1077 1078

      char *tzInfo;
      if (buf[len - 1] == 'z' || buf[len - 1] == 'Z') {
        tzInfo = &buf[len - 1];
1079 1080
        memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
      } else {
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
        tzInfo = strchr(buf, '+');
        if (tzInfo) {
          memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
        } else {
          //search '-' backwards
          tzInfo = strrchr(buf, '-');
          if (tzInfo) {
            memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
          }
        }
1091 1092
      }

1093
      char tmp[32] = {0};
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
      sprintf(tmp, ".%s", fraction);
      memcpy(tzInfo, tmp, fracLen);
      len += fracLen;
    }

    memmove(buf + VARSTR_HEADER_SIZE, buf, len);
    varDataSetLen(buf, len);

    colDataAppend(pOutput->columnData, i, buf, false);
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

1110 1111
int32_t toUnixtimestampFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);
1112 1113
  int64_t timePrec;
  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);
1114 1115 1116 1117 1118 1119

  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }
1120
    char *input = colDataGetData(pInput[0].columnData, i);
1121 1122

    int64_t timeVal = 0;
1123 1124 1125
    int32_t ret = convertStringToTimestamp(type, input, timePrec, &timeVal);
    if (ret != TSDB_CODE_SUCCESS) {
      colDataAppendNULL(pOutput->columnData, i);
1126 1127
    } else {
      colDataAppend(pOutput->columnData, i, (char *)&timeVal, false);
1128
    }
1129 1130 1131 1132 1133 1134 1135
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1136 1137 1138
int32_t toJsonFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);

wmmhello's avatar
wmmhello 已提交
1139
  char tmp[TSDB_MAX_JSON_TAG_LEN] = {0};
wmmhello's avatar
wmmhello 已提交
1140
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
wmmhello's avatar
wmmhello 已提交
1141 1142
    SArray* pTagVals = taosArrayInit(8, sizeof(STagVal));
    STag*   pTag = NULL;
wmmhello's avatar
wmmhello 已提交
1143

wmmhello's avatar
wmmhello 已提交
1144 1145
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      tTagNew(pTagVals, 1, true, &pTag);
wmmhello's avatar
wmmhello 已提交
1146
    }else{
wmmhello's avatar
wmmhello 已提交
1147
      char *input = pInput[0].columnData->pData + pInput[0].columnData->varmeta.offset[i];
wmmhello's avatar
wmmhello 已提交
1148
      if (varDataLen(input) > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE){
wmmhello's avatar
wmmhello 已提交
1149 1150
        taosArrayDestroy(pTagVals);
        return TSDB_CODE_FAILED;
wmmhello's avatar
wmmhello 已提交
1151 1152
      }
      memcpy(tmp, varDataVal(input), varDataLen(input));
wmmhello's avatar
wmmhello 已提交
1153
      tmp[varDataLen(input)] = 0;
wmmhello's avatar
wmmhello 已提交
1154 1155 1156
      if(parseJsontoTagData(tmp, pTagVals, &pTag, NULL)){
        tTagNew(pTagVals, 1, true, &pTag);
      }
wmmhello's avatar
wmmhello 已提交
1157 1158
    }

wmmhello's avatar
wmmhello 已提交
1159 1160 1161
    colDataAppend(pOutput->columnData, i, (const char*)pTag, false);
    tTagFree(pTag);
    taosArrayDestroy(pTagVals);
wmmhello's avatar
wmmhello 已提交
1162 1163 1164 1165 1166 1167
  }

  pOutput->numOfRows = pInput->numOfRows;
  return TSDB_CODE_SUCCESS;
}

1168
/** Time functions **/
1169 1170 1171
int32_t timeTruncateFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(&pInput[0]);

1172
  int64_t timeUnit, timePrec, timeVal = 0;
1173
  GET_TYPED_DATA(timeUnit, int64_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);
1174
  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
1175 1176 1177 1178

  int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 :
                   (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000);

1179
  int64_t unit = timeUnit * 1000 / factor;
1180

1181 1182 1183 1184 1185 1186
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }

1187 1188
    char *input = colDataGetData(pInput[0].columnData, i);

1189
    if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
1190 1191 1192 1193 1194
      int32_t ret = convertStringToTimestamp(type, input, TSDB_TIME_PRECISION_NANO, &timeVal);
      if (ret != TSDB_CODE_SUCCESS) {
        colDataAppendNULL(pOutput->columnData, i);
        continue;
      }
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
      //If converted value is less than 10digits in second, use value in second instead
      int64_t timeValSec = timeVal / 1000000000;
      if (timeValSec < 1000000000) {
        timeVal = timeValSec;
      }
    } else if (type == TSDB_DATA_TYPE_BIGINT) { /* unix timestamp */
      GET_TYPED_DATA(timeVal, int64_t, type, input);
    } else if (type == TSDB_DATA_TYPE_TIMESTAMP) { /* timestamp column*/
      GET_TYPED_DATA(timeVal, int64_t, type, input);
      int64_t timeValSec = timeVal / factor;
      if (timeValSec < 1000000000) {
        timeVal = timeValSec;
      }
    }

    char buf[20] = {0};
    NUM_TO_STRING(TSDB_DATA_TYPE_BIGINT, &timeVal, sizeof(buf), buf);
    int32_t tsDigits = (int32_t)strlen(buf);

1214 1215
    switch (unit) {
      case 0: { /* 1u or 1b */
1216
        if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
1217 1218 1219 1220 1221
          if (timePrec == TSDB_TIME_PRECISION_NANO && timeUnit == 1) {
            timeVal = timeVal * 1;
          } else {
            timeVal = timeVal / 1000 * 1000;
          }
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
          timeVal = timeVal * factor;
        } else {
          timeVal = timeVal * 1;
        }
        break;
      }
      case 1: { /* 1a */
        if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal * 1;
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal / 1000 * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000000 * 1000000;
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS){
          timeVal = timeVal * factor;
        } else {
1239 1240
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
        }
        break;
      }
      case 1000: { /* 1s */
        if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal / 1000 * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal / 1000000 * 1000000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000000000 * 1000000000;
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
          timeVal = timeVal * factor;
        } else {
1254 1255
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
        }
        break;
      }
      case 60000: { /* 1m */
        if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal / 1000 / 60 * 60 * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal / 1000000 / 60 * 60 * 1000000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000000000 / 60 * 60 * 1000000000;
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
          timeVal = timeVal * factor / factor / 60 * 60 * factor;
        } else {
1269 1270
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
        }
        break;
      }
      case 3600000: { /* 1h */
        if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal / 1000 / 3600 * 3600 * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal / 1000000 / 3600 * 3600 * 1000000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000000000 / 3600 * 3600 * 1000000000;
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
          timeVal = timeVal * factor / factor / 3600 * 3600 * factor;
        } else {
1284 1285
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
        }
        break;
      }
      case 86400000: { /* 1d */
        if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal / 1000 / 86400 * 86400 * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal / 1000000 / 86400 * 86400 * 1000000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000000000 / 86400 * 86400 * 1000000000;
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
          timeVal = timeVal * factor / factor / 86400* 86400 * factor;
        } else {
1299 1300
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
        }
        break;
      }
      case 604800000: { /* 1w */
        if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal / 1000 / 604800 * 604800 * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal / 1000000 / 604800 * 604800 * 1000000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000000000 / 604800 * 604800 * 1000000000;
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
          timeVal = timeVal * factor / factor / 604800 * 604800* factor;
        } else {
1314 1315
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
        }
        break;
      }
      default: {
        timeVal = timeVal * 1;
        break;
      }
    }

    //truncate the timestamp to db precision
    switch (timePrec) {
      case TSDB_TIME_PRECISION_MILLI: {
        if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal / 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000000;
        }
        break;
      }
      case TSDB_TIME_PRECISION_MICRO: {
        if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal * 1000;
        }
        break;
      }
      case TSDB_TIME_PRECISION_NANO: {
        if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal = timeVal * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal = timeVal * 1000000;
        }
        break;
      }
    }

    colDataAppend(pOutput->columnData, i, (char *)&timeVal, false);
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1361
int32_t timeDiffFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1362 1363
  int64_t timeUnit = -1, timePrec, timeVal[2] = {0};
  if (inputNum == 4) {
G
Ganlin Zhao 已提交
1364
    GET_TYPED_DATA(timeUnit, int64_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
1365 1366 1367
    GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[3]), pInput[3].columnData->pData);
  } else {
    GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
G
Ganlin Zhao 已提交
1368 1369
  }

1370 1371 1372
  int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 :
                   (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000);

G
Ganlin Zhao 已提交
1373 1374 1375 1376
  int32_t numOfRows = 0;
  for (int32_t i = 0; i < inputNum; ++i) {
    if (pInput[i].numOfRows > numOfRows) {
      numOfRows = pInput[i].numOfRows;
G
Ganlin Zhao 已提交
1377 1378 1379
    }
  }

G
Ganlin Zhao 已提交
1380 1381 1382
  char *input[2];
  for (int32_t i = 0; i < numOfRows; ++i) {
    bool hasNull = false;
G
Ganlin Zhao 已提交
1383
    for (int32_t k = 0; k < 2; ++k) {
G
Ganlin Zhao 已提交
1384 1385 1386
      if (colDataIsNull_s(pInput[k].columnData, i)) {
        hasNull = true;
        break;
G
Ganlin Zhao 已提交
1387 1388
      }

1389 1390 1391
      int32_t rowIdx = (pInput[k].numOfRows == 1) ? 0 : i;
      input[k] = colDataGetData(pInput[k].columnData, rowIdx);

G
Ganlin Zhao 已提交
1392 1393
      int32_t type = GET_PARAM_TYPE(&pInput[k]);
      if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
1394 1395
        int32_t ret = convertStringToTimestamp(type, input[k], TSDB_TIME_PRECISION_NANO, &timeVal[k]);
        if (ret != TSDB_CODE_SUCCESS) {
1396 1397
          hasNull = true;
          break;
1398
        }
G
Ganlin Zhao 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
      } else if (type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_TIMESTAMP) { /* unix timestamp or ts column*/
        GET_TYPED_DATA(timeVal[k], int64_t, type, input[k]);
        if (type == TSDB_DATA_TYPE_TIMESTAMP) {
          int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 :
                           (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000);
          int64_t timeValSec = timeVal[k] / factor;
          if (timeValSec < 1000000000) {
            timeVal[k] = timeValSec;
          }
        }

        char buf[20] = {0};
        NUM_TO_STRING(TSDB_DATA_TYPE_BIGINT, &timeVal[k], sizeof(buf), buf);
        int32_t tsDigits = (int32_t)strlen(buf);
        if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
          timeVal[k] = timeVal[k] * 1000000000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
          timeVal[k] = timeVal[k] * 1000000;
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
          timeVal[k] = timeVal[k] * 1000;
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal[k] = timeVal[k];
G
Ganlin Zhao 已提交
1421
        } else {
1422 1423
          hasNull = true;
          break;
G
Ganlin Zhao 已提交
1424
        }
G
Ganlin Zhao 已提交
1425 1426 1427
      }
    }

G
Ganlin Zhao 已提交
1428 1429 1430 1431 1432
    if (hasNull) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }

G
Ganlin Zhao 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
    int64_t result = (timeVal[0] >= timeVal[1]) ? (timeVal[0] - timeVal[1]) :
                                                  (timeVal[1] - timeVal[0]);

    if (timeUnit < 0) { // if no time unit given use db precision
      switch(timePrec) {
        case TSDB_TIME_PRECISION_MILLI: {
          result = result / 1000000;
          break;
        }
        case TSDB_TIME_PRECISION_MICRO: {
          result = result / 1000;
          break;
        }
        case TSDB_TIME_PRECISION_NANO: {
          result = result / 1;
          break;
        }
      }
    } else {
1452 1453 1454 1455 1456 1457 1458 1459
      int64_t unit = timeUnit * 1000 / factor;
      switch(unit) {
        case 0: { /* 1u or 1b */
          if (timePrec == TSDB_TIME_PRECISION_NANO && timeUnit == 1) {
            result = result / 1;
          } else {
            result = result / 1000;
          }
G
Ganlin Zhao 已提交
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
          break;
        }
        case 1: { /* 1a */
          result = result / 1000000;
          break;
        }
        case 1000: { /* 1s */
          result = result / 1000000000;
          break;
        }
        case 60000: { /* 1m */
          result = result / 1000000000 / 60;
          break;
        }
        case 3600000: { /* 1h */
          result = result / 1000000000 / 3600;
          break;
        }
        case 86400000: { /* 1d */
          result = result / 1000000000 / 86400;
          break;
        }
        case 604800000: { /* 1w */
          result = result / 1000000000 / 604800;
          break;
        }
        default: {
          break;
        }
      }
    }

    colDataAppend(pOutput->columnData, i, (char *)&result, false);
  }

G
Ganlin Zhao 已提交
1495
  pOutput->numOfRows = numOfRows;
G
Ganlin Zhao 已提交
1496 1497 1498 1499

  return TSDB_CODE_SUCCESS;
}

1500
int32_t nowFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1501 1502 1503 1504
  int64_t timePrec;
  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[0]), pInput[0].columnData->pData);

  int64_t ts = taosGetTimestamp(timePrec);
X
Xiaoyu Wang 已提交
1505 1506
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    colDataAppendInt64(pOutput->columnData, i, &ts);
1507
  }
X
Xiaoyu Wang 已提交
1508
  pOutput->numOfRows = pInput->numOfRows;
1509 1510 1511 1512
  return TSDB_CODE_SUCCESS;
}

int32_t todayFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1513
  int64_t timePrec;
1514
  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[0]), pInput[0].columnData->pData);
1515 1516

  int64_t ts = taosGetTimestampToday(timePrec);
X
Xiaoyu Wang 已提交
1517 1518
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    colDataAppendInt64(pOutput->columnData, i, &ts);
1519
  }
X
Xiaoyu Wang 已提交
1520
  pOutput->numOfRows = pInput->numOfRows;
1521 1522 1523
  return TSDB_CODE_SUCCESS;
}

1524
int32_t timezoneFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1525 1526 1527
  char output[TD_TIMEZONE_LEN + VARSTR_HEADER_SIZE] = {0};
  memcpy(varDataVal(output), tsTimezoneStr, TD_TIMEZONE_LEN);
  varDataSetLen(output, strlen(tsTimezoneStr));
X
Xiaoyu Wang 已提交
1528
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
1529
    colDataAppend(pOutput->columnData, i, output, false);
1530
  }
X
Xiaoyu Wang 已提交
1531
  pOutput->numOfRows = pInput->numOfRows;
1532 1533 1534
  return TSDB_CODE_SUCCESS;
}

1535 1536 1537
int32_t atanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, atan);
}
1538

1539 1540 1541
int32_t sinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sin);
}
1542

1543 1544 1545
int32_t cosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, cos);
}
1546

1547 1548 1549
int32_t tanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, tan);
}
1550

1551 1552 1553
int32_t asinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, asin);
}
1554

1555 1556 1557
int32_t acosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, acos);
}
H
Haojun Liao 已提交
1558

G
Ganlin Zhao 已提交
1559 1560 1561 1562 1563
int32_t powFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique2(pInput, inputNum, pOutput, pow);
}

int32_t logFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1564 1565 1566 1567 1568
  if (inputNum == 1) {
    return doScalarFunctionUnique(pInput, inputNum, pOutput, tlog);
  } else {
    return doScalarFunctionUnique2(pInput, inputNum, pOutput, tlog2);
  }
G
Ganlin Zhao 已提交
1569 1570
}

1571 1572 1573
int32_t sqrtFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sqrt);
}
1574

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
int32_t ceilFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunction(pInput, inputNum, pOutput, ceilf, ceil);
}

int32_t floorFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunction(pInput, inputNum, pOutput, floorf, floor);
}

int32_t roundFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunction(pInput, inputNum, pOutput, roundf, round);
1585 1586
}

G
Ganlin Zhao 已提交
1587
int32_t lowerFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
wafwerar's avatar
wafwerar 已提交
1588 1589 1590
#ifdef WINDOWS
  return doCaseConvFunction(pInput, inputNum, pOutput, towlower);
#else
G
Ganlin Zhao 已提交
1591
  return doCaseConvFunction(pInput, inputNum, pOutput, tolower);
wafwerar's avatar
wafwerar 已提交
1592
#endif
1593 1594
}

G
Ganlin Zhao 已提交
1595
int32_t upperFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
wafwerar's avatar
wafwerar 已提交
1596 1597 1598
#ifdef WINDOWS
  return doCaseConvFunction(pInput, inputNum, pOutput, towupper);
#else
G
Ganlin Zhao 已提交
1599
  return doCaseConvFunction(pInput, inputNum, pOutput, toupper);
wafwerar's avatar
wafwerar 已提交
1600
#endif
1601 1602
}

G
Ganlin Zhao 已提交
1603 1604
int32_t ltrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, tltrim);
1605 1606
}

G
Ganlin Zhao 已提交
1607 1608
int32_t rtrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, trtrim);
1609 1610
}

G
Ganlin Zhao 已提交
1611 1612 1613
int32_t lengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tlength);
}
1614

G
Ganlin Zhao 已提交
1615 1616
int32_t charLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tcharlength);
1617 1618
}

H
Haojun Liao 已提交
1619
#if 0
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
static void reverseCopy(char* dest, const char* src, int16_t type, int32_t numOfRows) {
  switch(type) {
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_UTINYINT:{
      int8_t* p = (int8_t*) dest;
      int8_t* pSrc = (int8_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
      return;
    }

    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_USMALLINT:{
      int16_t* p = (int16_t*) dest;
      int16_t* pSrc = (int16_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
      return;
    }
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_UINT: {
      int32_t* p = (int32_t*) dest;
      int32_t* pSrc = (int32_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
      return;
    }
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_UBIGINT: {
      int64_t* p = (int64_t*) dest;
      int64_t* pSrc = (int64_t*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
      return;
    }
    case TSDB_DATA_TYPE_FLOAT: {
      float* p = (float*) dest;
      float* pSrc = (float*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
      return;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
      double* p = (double*) dest;
      double* pSrc = (double*) src;

      for(int32_t i = 0; i < numOfRows; ++i) {
        p[i] = pSrc[numOfRows - i - 1];
      }
      return;
    }
    default: assert(0);
  }
}
H
Haojun Liao 已提交
1684
#endif
1685

1686 1687 1688 1689 1690 1691 1692 1693
bool getTimePseudoFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
  pEnv->calcMemSize = sizeof(int64_t);
  return true;
}

int32_t qStartTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 0));
1694
  return TSDB_CODE_SUCCESS;
1695 1696 1697 1698 1699
}

int32_t qEndTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 1));
1700
  return TSDB_CODE_SUCCESS;
1701 1702 1703 1704 1705
}

int32_t winDurFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 2));
1706
  return TSDB_CODE_SUCCESS;
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
}

int32_t winStartTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t*) colDataGetData(pInput->columnData, 3));
  return TSDB_CODE_SUCCESS;
}

int32_t winEndTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t*) colDataGetData(pInput->columnData, 4));
  return TSDB_CODE_SUCCESS;
1719
}
1720 1721 1722

int32_t qTbnameFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738

  SMetaReader mr = {0};
  metaReaderInit(&mr, pInput->param, 0);

  uint64_t uid = *(uint64_t *)colDataGetData(pInput->columnData, 0);
  metaGetTableEntryByUid(&mr, uid);

  char str[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
  STR_TO_VARSTR(str, mr.me.name);
  metaReaderClear(&mr);

  for(int32_t i = 0; i < pInput->numOfRows; ++i) {
    colDataAppend(pOutput->columnData, pOutput->numOfRows + i, str, false);
  }

  pOutput->numOfRows += pInput->numOfRows;
1739 1740
  return TSDB_CODE_SUCCESS;
}
1741 1742 1743 1744 1745 1746 1747 1748


/** Aggregation functions **/
int32_t countScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

  int64_t *out = (int64_t *)pOutputData->pData;
G
Ganlin Zhao 已提交
1749
  bool hasNull = false;
1750 1751 1752
  *out = 0;
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
1753
      hasNull = true;
1754 1755 1756 1757 1758
      break;
    }
    (*out)++;
  }

G
Ganlin Zhao 已提交
1759 1760 1761 1762 1763
  if (hasNull) {
    colDataAppendNULL(pOutputData, 0);
  }

  pOutput->numOfRows = 1;
1764 1765
  return TSDB_CODE_SUCCESS;
}
1766 1767 1768 1769 1770 1771

int32_t sumScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
G
Ganlin Zhao 已提交
1772
  bool hasNull = false;
1773 1774
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
1775
      hasNull = true;
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
      break;
    }

    if (IS_SIGNED_NUMERIC_TYPE(type)) {
      int64_t *in  = (int64_t *)pInputData->pData;
      int64_t *out = (int64_t *)pOutputData->pData;
      *out += in[i];
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
      uint64_t *in  = (uint64_t *)pInputData->pData;
      uint64_t *out = (uint64_t *)pOutputData->pData;
      *out += in[i];
    } else if (IS_FLOAT_TYPE(type)) {
      double *in  = (double *)pInputData->pData;
      double *out = (double *)pOutputData->pData;
      *out += in[i];
    }
  }

G
Ganlin Zhao 已提交
1794 1795 1796 1797 1798
  if (hasNull) {
    colDataAppendNULL(pOutputData, 0);
  }

  pOutput->numOfRows = 1;
1799 1800
  return TSDB_CODE_SUCCESS;
}
1801

1802
static int32_t doMinMaxScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, bool isMinFunc) {
1803 1804 1805 1806
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
1807

G
Ganlin Zhao 已提交
1808
  bool hasNull = false;
1809 1810 1811 1812 1813
  if (isMinFunc) {
    SET_TYPED_DATA_MAX(pOutputData->pData, type);
  } else {
    SET_TYPED_DATA_MIN(pOutputData->pData, type);
  }
1814 1815 1816

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
1817
      hasNull = true;
1818 1819 1820 1821 1822 1823 1824 1825
      break;
    }

    switch(type) {
      case TSDB_DATA_TYPE_BOOL:
      case TSDB_DATA_TYPE_TINYINT: {
        int8_t *in  = (int8_t *)pInputData->pData;
        int8_t *out = (int8_t *)pOutputData->pData;
1826
        if((in[i] > *out) ^ isMinFunc) {
1827 1828 1829 1830 1831 1832 1833
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_SMALLINT: {
        int16_t *in  = (int16_t *)pInputData->pData;
        int16_t *out = (int16_t *)pOutputData->pData;
1834
        if((in[i] > *out) ^ isMinFunc) {
1835 1836 1837 1838 1839 1840 1841
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_INT: {
        int32_t *in  = (int32_t *)pInputData->pData;
        int32_t *out = (int32_t *)pOutputData->pData;
1842
        if((in[i] > *out) ^ isMinFunc) {
1843 1844 1845 1846 1847 1848 1849
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_BIGINT: {
        int64_t *in  = (int64_t *)pInputData->pData;
        int64_t *out = (int64_t *)pOutputData->pData;
1850
        if((in[i] > *out) ^ isMinFunc) {
1851 1852 1853 1854 1855 1856 1857
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_UTINYINT: {
        uint8_t *in  = (uint8_t *)pInputData->pData;
        uint8_t *out = (uint8_t *)pOutputData->pData;
1858
        if((in[i] > *out) ^ isMinFunc) {
1859 1860 1861 1862 1863 1864 1865
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_USMALLINT: {
        uint16_t *in  = (uint16_t *)pInputData->pData;
        uint16_t *out = (uint16_t *)pOutputData->pData;
1866
        if((in[i] > *out) ^ isMinFunc) {
1867 1868 1869 1870 1871 1872 1873
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_UINT: {
        uint32_t *in  = (uint32_t *)pInputData->pData;
        uint32_t *out = (uint32_t *)pOutputData->pData;
1874
        if((in[i] > *out) ^ isMinFunc) {
1875 1876 1877 1878 1879 1880 1881
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_UBIGINT: {
        uint64_t *in  = (uint64_t *)pInputData->pData;
        uint64_t *out = (uint64_t *)pOutputData->pData;
1882
        if((in[i] > *out) ^ isMinFunc) {
1883 1884 1885 1886 1887 1888 1889
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
        float *in  = (float *)pInputData->pData;
        float *out = (float *)pOutputData->pData;
1890
        if((in[i] > *out) ^ isMinFunc) {
1891 1892 1893 1894 1895 1896 1897
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
        double *in  = (double *)pInputData->pData;
        double *out = (double *)pOutputData->pData;
1898
        if((in[i] > *out) ^ isMinFunc) {
1899 1900 1901 1902 1903 1904 1905
          *out = in[i];
        }
        break;
      }
    }
  }

G
Ganlin Zhao 已提交
1906 1907 1908 1909 1910
  if (hasNull) {
    colDataAppendNULL(pOutputData, 0);
  }

  pOutput->numOfRows = 1;
1911 1912
  return TSDB_CODE_SUCCESS;
}
1913 1914 1915 1916 1917 1918 1919 1920

int32_t minScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doMinMaxScalarFunction(pInput, inputNum, pOutput, true);
}

int32_t maxScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doMinMaxScalarFunction(pInput, inputNum, pOutput, false);
}
1921 1922 1923 1924 1925 1926

int32_t stddevScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
G
Ganlin Zhao 已提交
1927 1928
  //int64_t count = 0, sum = 0, qSum = 0;
  bool hasNull = false;
1929 1930 1931

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
1932 1933
      hasNull = true;
      break;
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
    }
#if 0
    switch(type) {
      case TSDB_DATA_TYPE_TINYINT: {
        int8_t *in  = (int8_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_SMALLINT: {
        int16_t *in  = (int16_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_INT: {
        int32_t *in  = (int32_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_BIGINT: {
        int64_t *in  = (int64_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UTINYINT: {
        uint8_t *in  = (uint8_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_USMALLINT: {
        uint16_t *in  = (uint16_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UINT: {
        uint32_t *in  = (uint32_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UBIGINT: {
        uint64_t *in  = (uint64_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
        float *in  = (float *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
        double *in  = (double *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
    }
#endif
  }

  double *out  = (double *)pOutputData->pData;
G
Ganlin Zhao 已提交
2012
  if (hasNull) {
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
    colDataAppendNULL(pOutputData, 0);
  } else {
    *out = 0;
#if 0
    double avg = 0;
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
      avg = (int64_t)sum / (double)count;
      *out =  sqrt(fabs((int64_t)qSum / ((double)count) - avg * avg));
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
      avg = (uint64_t)sum / (double)count;
      *out =  sqrt(fabs((uint64_t)qSum / ((double)count) - avg * avg));
    } else if (IS_FLOAT_TYPE(type)) {
      avg = (double)sum / (double)count;
      *out =  sqrt(fabs((double)qSum / ((double)count) - avg * avg));
    }
#endif
  }

G
Ganlin Zhao 已提交
2031
  pOutput->numOfRows = 1;
2032 2033
  return TSDB_CODE_SUCCESS;
}