sclfunc.c 49.9 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 39
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
  if (!IS_NUMERIC_TYPE(type)) {
40 41
    return TSDB_CODE_FAILED;
  }
42

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

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

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

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

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

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

H
Haojun Liao 已提交
122 123
    default: {
      colDataAssign(pOutputData, pInputData, pInput->numOfRows);
124 125
    }
  }
126

H
Haojun Liao 已提交
127
  pOutput->numOfRows = pInput->numOfRows;
128
  return TSDB_CODE_SUCCESS;
129 130
}

131
static int32_t doScalarFunctionUnique(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _double_fn valFn) {
H
Haojun Liao 已提交
132 133
  int32_t type = GET_PARAM_TYPE(pInput);
  if (inputNum != 1 || !IS_NUMERIC_TYPE(type)) {
134 135
    return TSDB_CODE_FAILED;
  }
136

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

140
  _getDoubleValue_fn_t getValueFn = getVectorDoubleValueFn(type);
141

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

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

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

161
static int32_t doScalarFunctionUnique2(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _double_fn_2 valFn) {
G
Ganlin Zhao 已提交
162
  if (inputNum != 2 || !IS_NUMERIC_TYPE(GET_PARAM_TYPE(&pInput[0])) || !IS_NUMERIC_TYPE(GET_PARAM_TYPE(&pInput[1]))) {
163 164 165
    return TSDB_CODE_FAILED;
  }

G
Ganlin Zhao 已提交
166
  SColumnInfoData *pInputData[2];
H
Haojun Liao 已提交
167
  SColumnInfoData *pOutputData = pOutput->columnData;
G
Ganlin Zhao 已提交
168
  _getDoubleValue_fn_t getValueFn[2];
169

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

175
  double *out = (double *)pOutputData->pData;
176
  double result;
177

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

203 204 205 206 207 208 209 210
        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;
      }
211
    }
212 213 214
  } else if (pInput[1].numOfRows == 1) {
    if (colDataIsNull_s(pInputData[1], 0)) {
      colDataAppendNNULL(pOutputData, 0, pInput[0].numOfRows);
215
    } else {
216 217 218 219 220 221 222 223 224 225 226 227 228 229
      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;
      }
230
    }
231
  }
232

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

237
static int32_t doScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _float_fn f1, _double_fn d1) {
H
Haojun Liao 已提交
238 239
  int32_t type = GET_PARAM_TYPE(pInput);
  if (inputNum != 1 || !IS_NUMERIC_TYPE(type)) {
240 241 242
    return TSDB_CODE_FAILED;
  }

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

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

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

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

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

    default: {
      colDataAssign(pOutputData, pInputData, pInput->numOfRows);
277
    }
278 279
  }

H
Haojun Liao 已提交
280
  pOutput->numOfRows = pInput->numOfRows;
281 282
  return TSDB_CODE_SUCCESS;
}
283

G
Ganlin Zhao 已提交
284
/** String functions **/
285
static int16_t tlength(char *input, int32_t type) {
G
Ganlin Zhao 已提交
286 287 288
  return varDataLen(input);
}

289
static int16_t tcharlength(char *input, int32_t type) {
G
Ganlin Zhao 已提交
290 291 292 293
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    return varDataLen(input);
  } else { //NCHAR
    return varDataLen(input) / TSDB_NCHAR_SIZE;
G
Ganlin Zhao 已提交
294
  }
G
Ganlin Zhao 已提交
295
}
G
Ganlin Zhao 已提交
296

297
static void tltrim(char *input, char *output, int32_t type, int32_t charLen) {
G
Ganlin Zhao 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  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 已提交
314

G
Ganlin Zhao 已提交
315 316 317 318 319 320 321 322
  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 已提交
323

G
Ganlin Zhao 已提交
324 325 326
  varDataSetLen(output, resLen);
}

327
static void trtrim(char *input, char *output, int32_t type, int32_t charLen) {
G
Ganlin Zhao 已提交
328 329 330 331 332 333 334 335 336
  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
337
    for (int32_t i = charLen - 1; i >= 0; --i) {
G
Ganlin Zhao 已提交
338 339 340 341
      if (!iswspace(*((uint32_t *)varDataVal(input) + i))) {
        break;
      }
      numOfSpaces++;
G
Ganlin Zhao 已提交
342
    }
G
Ganlin Zhao 已提交
343
  }
G
Ganlin Zhao 已提交
344

G
Ganlin Zhao 已提交
345 346 347 348 349
  int32_t resLen;
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    resLen = charLen - numOfSpaces;
  } else {
    resLen = (charLen - numOfSpaces) * TSDB_NCHAR_SIZE;
G
Ganlin Zhao 已提交
350
  }
G
Ganlin Zhao 已提交
351
  memcpy(varDataVal(output), varDataVal(input), resLen);
G
Ganlin Zhao 已提交
352

G
Ganlin Zhao 已提交
353
  varDataSetLen(output, resLen);
G
Ganlin Zhao 已提交
354 355
}

356
static int32_t doLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _len_fn lenFn) {
G
Ganlin Zhao 已提交
357 358 359 360 361 362 363 364
  int32_t type = GET_PARAM_TYPE(pInput);
  if (inputNum != 1 || !IS_VAR_DATA_TYPE(type)) {
    return TSDB_CODE_FAILED;
  }

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

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

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

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

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

382 383
static int32_t concatCopyHelper(const char *input, char *output, bool hasNchar, int32_t type, int16_t *dataLen) {
  if (hasNchar && type == TSDB_DATA_TYPE_VARCHAR) {
384
    TdUcs4 *newBuf = taosMemoryCalloc((varDataLen(input) + 1) * TSDB_NCHAR_SIZE, 1);
385 386
    int32_t len = varDataLen(input);
    bool ret = taosMbsToUcs4(varDataVal(input), len, newBuf, (varDataLen(input) + 1) * TSDB_NCHAR_SIZE, &len);
387 388 389 390 391 392 393 394 395 396 397 398
    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;
399
}
G
Ganlin Zhao 已提交
400

401 402 403 404 405 406 407 408 409 410 411 412 413
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 已提交
414
int32_t concatFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
415
  int32_t ret = TSDB_CODE_SUCCESS;
G
Ganlin Zhao 已提交
416 417
  SColumnInfoData **pInputData = taosMemoryCalloc(inputNum, sizeof(SColumnInfoData *));
  SColumnInfoData *pOutputData = pOutput->columnData;
418
  char **input = taosMemoryCalloc(inputNum, POINTER_BYTES);
419
  char *outputBuf = NULL;
G
Ganlin Zhao 已提交
420

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

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

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

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

    if (hasNull) {
      continue;
    }

463

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

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

479
  pOutput->numOfRows = numOfRows;
480 481

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

486
  return ret;
G
Ganlin Zhao 已提交
487 488
}

489

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

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

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

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

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

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

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

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

551

G
Ganlin Zhao 已提交
552 553
      if (i < inputNum - 1) {
        //insert the separator
554 555
        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);
556
        if (ret != TSDB_CODE_SUCCESS) {
557
          goto DONE;
558
        }
G
Ganlin Zhao 已提交
559 560
      }
    }
561 562 563 564 565 566 567 568 569

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

572
  pOutput->numOfRows = numOfRows;
573 574

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

579
  return ret;
G
Ganlin Zhao 已提交
580 581
}

582
static int32_t doCaseConvFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _conv_fn convFn) {
G
Ganlin Zhao 已提交
583 584 585 586 587 588 589 590
  int32_t type = GET_PARAM_TYPE(pInput);
  if (inputNum != 1 || !IS_VAR_DATA_TYPE(type)) {
    return TSDB_CODE_FAILED;
  }

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
}


624
static int32_t doTrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _trim_fn trimFn) {
G
Ganlin Zhao 已提交
625 626 627 628 629 630 631 632
  int32_t type = GET_PARAM_TYPE(pInput);
  if (inputNum != 1 || !IS_VAR_DATA_TYPE(type)) {
    return TSDB_CODE_FAILED;
  }

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

633 634
  int32_t outputLen = pInputData->varmeta.length;
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
635
  char *output = outputBuf;
636

G
Ganlin Zhao 已提交
637
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
638 639
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
640 641 642
      continue;
    }

643
    char *input = colDataGetData(pInputData, i);
644
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
645
    int32_t charLen = (type == TSDB_DATA_TYPE_VARCHAR) ? len : len / TSDB_NCHAR_SIZE;
646 647 648
    trimFn(input, output, type, charLen);

    varDataSetLen(output, len);
649
    colDataAppend(pOutputData, i, output, false);
650
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
651 652 653
  }

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

  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);
  if (subPos == 0) { //subPos needs to be positive or negative values;
    return TSDB_CODE_FAILED;
  }

  int32_t subLen = INT16_MAX;
  if (inputNum == 3) {
    GET_TYPED_DATA(subLen, int32_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
669
    if (subLen < 0 || subLen > INT16_MAX) { //subLen cannot be negative
G
Ganlin Zhao 已提交
670 671 672 673 674 675 676 677
      return TSDB_CODE_FAILED;
    }
    subLen = (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? subLen : subLen * TSDB_NCHAR_SIZE;
  }

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

678
  int32_t outputLen = pInputData->varmeta.length * pInput->numOfRows;
679
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
680
  char *output = outputBuf;
681 682

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
683 684
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
685 686
      continue;
    }
687
    char *input = colDataGetData(pInput[0].columnData, i);
688
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
689 690 691 692
    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 已提交
693
      startPosBytes = TMIN(startPosBytes, len);
G
Ganlin Zhao 已提交
694 695
    } else {
      startPosBytes = (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? len + subPos : len + subPos * TSDB_NCHAR_SIZE;
wafwerar's avatar
wafwerar 已提交
696
      startPosBytes = TMAX(startPosBytes, 0);
G
Ganlin Zhao 已提交
697 698
    }

wafwerar's avatar
wafwerar 已提交
699
    int32_t resLen = TMIN(subLen, len - startPosBytes);
700 701
    if (resLen > 0) {
      memcpy(varDataVal(output), varDataVal(input) + startPosBytes, resLen);
G
Ganlin Zhao 已提交
702 703
    }

704
    varDataSetLen(output, resLen);
705
    colDataAppend(pOutputData, i , output, false);
706
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
707 708 709
  }

  pOutput->numOfRows = pInput->numOfRows;
710
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
711 712 713 714

  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
715
int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
716
  int16_t inputType  = GET_PARAM_TYPE(&pInput[0]);
717
  int16_t inputLen   = GET_PARAM_BYTES(&pInput[0]);
718 719
  int16_t outputType = GET_PARAM_TYPE(&pOutput[0]);
  int64_t outputLen  = GET_PARAM_BYTES(&pOutput[0]);
G
Ganlin Zhao 已提交
720

wafwerar's avatar
wafwerar 已提交
721
  char *outputBuf = taosMemoryCalloc(outputLen * pInput[0].numOfRows + 1, 1);
G
Ganlin Zhao 已提交
722 723 724 725 726 727 728
  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;
    }
729

730
    char *input = colDataGetData(pInput[0].columnData, i);
G
Ganlin Zhao 已提交
731 732 733 734

    switch(outputType) {
      case TSDB_DATA_TYPE_BIGINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
735
          *(int64_t *)output = taosStr2Int64(varDataVal(input), NULL, 10);
G
Ganlin Zhao 已提交
736
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
737
          char *newBuf = taosMemoryCalloc(1, inputLen);
G
Ganlin Zhao 已提交
738 739 740 741 742 743
          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 已提交
744
          *(int64_t *)output = taosStr2Int64(newBuf, NULL, 10);
G
Ganlin Zhao 已提交
745 746 747 748 749 750 751 752
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(int64_t *)output, int64_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_UBIGINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
753
          *(uint64_t *)output = taosStr2UInt64(varDataVal(input), NULL, 10);
G
Ganlin Zhao 已提交
754
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
755
          char *newBuf = taosMemoryCalloc(1, inputLen);
G
Ganlin Zhao 已提交
756 757 758 759 760 761
          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 已提交
762
          *(uint64_t *)output = taosStr2UInt64(newBuf, NULL, 10);
G
Ganlin Zhao 已提交
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
          taosMemoryFree(newBuf);
        } else {
          GET_TYPED_DATA(*(uint64_t *)output, uint64_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_TIMESTAMP: {
        if (inputType == TSDB_DATA_TYPE_BINARY || inputType == TSDB_DATA_TYPE_NCHAR) {
          //not support
          return TSDB_CODE_FAILED;
        } 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 已提交
783
          int32_t len = TMIN(varDataLen(input), outputLen - VARSTR_HEADER_SIZE);
784
          len = sprintf(varDataVal(output), "%.*s", len, varDataVal(input));
G
Ganlin Zhao 已提交
785
          varDataSetLen(output, len);
786
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
G
Ganlin Zhao 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800
          //not support
          return TSDB_CODE_FAILED;
        } 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;
801
        int32_t len;
G
Ganlin Zhao 已提交
802 803
        if (inputType == TSDB_DATA_TYPE_BOOL) {
          char tmp[8] = {0};
804
          len = sprintf(tmp, "%.*s", outputCharLen, *(int8_t *)input ? "true" : "false" );
G
Ganlin Zhao 已提交
805 806 807 808 809 810
          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) {
811
          len = outputCharLen > varDataLen(input) ? varDataLen(input) : outputCharLen;
G
Ganlin Zhao 已提交
812 813 814 815 816 817
          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) {
818 819 820
          len = TMIN(outputLen - VARSTR_HEADER_SIZE, varDataLen(input));
          memcpy(output, input, len + VARSTR_HEADER_SIZE);
          varDataSetLen(output, len);
G
Ganlin Zhao 已提交
821 822 823
        } else {
          char tmp[400] = {0};
          NUM_TO_STRING(inputType, input, sizeof(tmp), tmp);
824
          len = (int32_t)strlen(tmp);
G
Ganlin Zhao 已提交
825 826 827 828 829 830 831
          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);
        }
832
        //for constant conversion, need to set proper length of pOutput description
G
Ganlin Zhao 已提交
833
        if (len < outputLen) {
D
dapan1121 已提交
834
          pOutput->columnData->info.bytes = len + VARSTR_HEADER_SIZE;
835
        }
G
Ganlin Zhao 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
        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 已提交
855

856 857 858
int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);

859 860
  char* tz;
  int32_t tzLen;
861 862
  tz = varDataVal(pInput[1].columnData->pData);
  tzLen = varDataLen(pInput[1].columnData->pData);
863

864 865 866 867 868 869
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }

870
    char *input = colDataGetData(pInput[0].columnData, i);
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
    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 {
887 888
        colDataAppendNULL(pOutput->columnData, i);
        continue;
889 890 891 892 893
      }
      hasFraction = true;
      memmove(fraction, fraction + TSDB_TIME_PRECISION_SEC_DIGITS, TSDB_TIME_PRECISION_SEC_DIGITS);
    }

894
    struct tm *tmInfo = taosLocalTime((const time_t *)&timeVal, NULL);
895
    strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", tmInfo);
896 897
    int32_t len = (int32_t)strlen(buf);

898 899 900 901
    //add timezone string
    snprintf(buf + len, tzLen + 1, "%s", tz);
    len += tzLen;

902 903
    if (hasFraction) {
      int32_t fracLen = (int32_t)strlen(fraction) + 1;
904 905 906 907

      char *tzInfo;
      if (buf[len - 1] == 'z' || buf[len - 1] == 'Z') {
        tzInfo = &buf[len - 1];
908 909
        memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
      } else {
910 911 912 913 914 915 916 917 918 919
        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));
          }
        }
920 921
      }

922
      char tmp[32] = {0};
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
      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;
}

939 940 941 942 943 944 945 946 947
int32_t toUnixtimestampFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);
  int32_t timePrec = GET_PARAM_PRECISON(pInput);

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

    int64_t timeVal = 0;
951 952 953
    int32_t ret = convertStringToTimestamp(type, input, timePrec, &timeVal);
    if (ret != TSDB_CODE_SUCCESS) {
      colDataAppendNULL(pOutput->columnData, i);
954 955
    } else {
      colDataAppend(pOutput->columnData, i, (char *)&timeVal, false);
956
    }
957 958 959 960 961 962 963
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
964 965 966 967 968 969 970 971 972 973
int32_t toJsonFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);
  if (type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR) {
    return TSDB_CODE_FAILED;
  }

  if (inputNum != 1) {
    return TSDB_CODE_FAILED;
  }

wmmhello's avatar
wmmhello 已提交
974
  char tmp[TSDB_MAX_JSON_TAG_LEN] = {0};
wmmhello's avatar
wmmhello 已提交
975 976 977 978 979
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }
wmmhello's avatar
wmmhello 已提交
980
    char *input = pInput[0].columnData->pData + pInput[0].columnData->varmeta.offset[i];
wmmhello's avatar
wmmhello 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

    if(type == TSDB_DATA_TYPE_NCHAR){
      if (varDataTLen(input) > TSDB_MAX_JSON_TAG_LEN){
        colDataAppendNULL(pOutput->columnData, i);
        continue;
      }
      int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), tmp);
      if (len < 0) {
        colDataAppendNULL(pOutput->columnData, i);
        continue;
      }
      tmp[len] = 0;
    }else{
      if (varDataLen(input) > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE){
        colDataAppendNULL(pOutput->columnData, i);
        continue;
      }
      memcpy(tmp, varDataVal(input), varDataLen(input));
wmmhello's avatar
wmmhello 已提交
999
      tmp[varDataLen(input)] = 0;
wmmhello's avatar
wmmhello 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    }

    if(!tjsonValidateJson(tmp)){
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }

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

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
int32_t timeTruncateFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(&pInput[0]);
  int32_t timePrec = GET_PARAM_PRECISON(&pInput[0]);

  int64_t timeUnit, timeVal = 0;
  GET_TYPED_DATA(timeUnit, int64_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);

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

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

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

1033
    if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
1034 1035 1036 1037 1038
      int32_t ret = convertStringToTimestamp(type, input, TSDB_TIME_PRECISION_NANO, &timeVal);
      if (ret != TSDB_CODE_SUCCESS) {
        colDataAppendNULL(pOutput->columnData, i);
        continue;
      }
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
      //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);
    timeUnit = timeUnit * 1000 / factor;

    switch (timeUnit) {
      case 0: { /* 1u */
        if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
          timeVal = timeVal / 1000 * 1000;
        //} else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
        //  //timeVal = timeVal / 1000;
        } 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 {
1082 1083
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
        }
        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 {
1097 1098
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
        }
        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 {
1112 1113
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
        }
        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 {
1127 1128
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
        }
        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 {
1142 1143
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
        }
        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 {
1157 1158
          colDataAppendNULL(pOutput->columnData, i);
          continue;
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
        }
        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 已提交
1204 1205 1206 1207 1208 1209 1210
int32_t timeDiffFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t timePrec = GET_PARAM_PRECISON(&pInput[0]);
  int64_t timeUnit = -1, timeVal[2] = {0};
  if (inputNum == 3) {
    GET_TYPED_DATA(timeUnit, int64_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
  }

G
Ganlin Zhao 已提交
1211 1212 1213 1214
  int32_t numOfRows = 0;
  for (int32_t i = 0; i < inputNum; ++i) {
    if (pInput[i].numOfRows > numOfRows) {
      numOfRows = pInput[i].numOfRows;
G
Ganlin Zhao 已提交
1215 1216 1217
    }
  }

G
Ganlin Zhao 已提交
1218 1219 1220
  char *input[2];
  for (int32_t i = 0; i < numOfRows; ++i) {
    bool hasNull = false;
G
Ganlin Zhao 已提交
1221
    for (int32_t k = 0; k < 2; ++k) {
G
Ganlin Zhao 已提交
1222 1223 1224
      if (colDataIsNull_s(pInput[k].columnData, i)) {
        hasNull = true;
        break;
G
Ganlin Zhao 已提交
1225 1226
      }

1227 1228 1229
      int32_t rowIdx = (pInput[k].numOfRows == 1) ? 0 : i;
      input[k] = colDataGetData(pInput[k].columnData, rowIdx);

G
Ganlin Zhao 已提交
1230 1231
      int32_t type = GET_PARAM_TYPE(&pInput[k]);
      if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
1232 1233
        int32_t ret = convertStringToTimestamp(type, input[k], TSDB_TIME_PRECISION_NANO, &timeVal[k]);
        if (ret != TSDB_CODE_SUCCESS) {
1234 1235
          hasNull = true;
          break;
1236
        }
G
Ganlin Zhao 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
      } 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 已提交
1259
        } else {
1260 1261
          hasNull = true;
          break;
G
Ganlin Zhao 已提交
1262
        }
G
Ganlin Zhao 已提交
1263 1264 1265
      }
    }

G
Ganlin Zhao 已提交
1266 1267 1268 1269 1270
    if (hasNull) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }

G
Ganlin Zhao 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    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 {
      int64_t factor = (timePrec == TSDB_TIME_PRECISION_MILLI) ? 1000 :
                       (timePrec == TSDB_TIME_PRECISION_MICRO ? 1000000 : 1000000000);
      timeUnit = timeUnit * 1000 / factor;
      switch(timeUnit) {
        case 0: { /* 1u */
          result = result / 1000;
          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 已提交
1331
  pOutput->numOfRows = numOfRows;
G
Ganlin Zhao 已提交
1332 1333 1334 1335

  return TSDB_CODE_SUCCESS;
}

1336
int32_t nowFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
X
Xiaoyu Wang 已提交
1337 1338 1339
  int64_t ts = taosGetTimestamp(TSDB_TIME_PRECISION_MILLI);
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    colDataAppendInt64(pOutput->columnData, i, &ts);
1340
  }
X
Xiaoyu Wang 已提交
1341
  pOutput->numOfRows = pInput->numOfRows;
1342 1343 1344 1345
  return TSDB_CODE_SUCCESS;
}

int32_t todayFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
X
Xiaoyu Wang 已提交
1346 1347 1348
  int64_t ts = taosGetTimestampToday(TSDB_TIME_PRECISION_MILLI);
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    colDataAppendInt64(pOutput->columnData, i, &ts);
1349
  }
X
Xiaoyu Wang 已提交
1350
  pOutput->numOfRows = pInput->numOfRows;
1351 1352 1353
  return TSDB_CODE_SUCCESS;
}

1354
int32_t timezoneFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1355 1356 1357
  char output[TD_TIMEZONE_LEN + VARSTR_HEADER_SIZE] = {0};
  memcpy(varDataVal(output), tsTimezoneStr, TD_TIMEZONE_LEN);
  varDataSetLen(output, strlen(tsTimezoneStr));
X
Xiaoyu Wang 已提交
1358
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
1359
    colDataAppend(pOutput->columnData, i, output, false);
1360
  }
X
Xiaoyu Wang 已提交
1361
  pOutput->numOfRows = pInput->numOfRows;
1362 1363 1364
  return TSDB_CODE_SUCCESS;
}

1365 1366 1367
int32_t atanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, atan);
}
1368

1369 1370 1371
int32_t sinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sin);
}
1372

1373 1374 1375
int32_t cosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, cos);
}
1376

1377 1378 1379
int32_t tanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, tan);
}
1380

1381 1382 1383
int32_t asinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, asin);
}
1384

1385 1386 1387
int32_t acosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, acos);
}
H
Haojun Liao 已提交
1388

G
Ganlin Zhao 已提交
1389 1390 1391 1392 1393
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) {
1394 1395 1396 1397 1398
  if (inputNum == 1) {
    return doScalarFunctionUnique(pInput, inputNum, pOutput, tlog);
  } else {
    return doScalarFunctionUnique2(pInput, inputNum, pOutput, tlog2);
  }
G
Ganlin Zhao 已提交
1399 1400
}

1401 1402 1403
int32_t sqrtFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sqrt);
}
1404

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
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);
1415 1416
}

G
Ganlin Zhao 已提交
1417 1418
int32_t lowerFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doCaseConvFunction(pInput, inputNum, pOutput, tolower);
1419 1420
}

G
Ganlin Zhao 已提交
1421 1422
int32_t upperFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doCaseConvFunction(pInput, inputNum, pOutput, toupper);
1423 1424
}

G
Ganlin Zhao 已提交
1425 1426
int32_t ltrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, tltrim);
1427 1428
}

G
Ganlin Zhao 已提交
1429 1430
int32_t rtrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, trtrim);
1431 1432
}

G
Ganlin Zhao 已提交
1433 1434 1435
int32_t lengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tlength);
}
1436

G
Ganlin Zhao 已提交
1437 1438
int32_t charLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tcharlength);
1439 1440
}

H
Haojun Liao 已提交
1441
#if 0
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 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 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
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 已提交
1506
#endif
1507

1508 1509 1510 1511 1512 1513 1514 1515
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));
1516
  return TSDB_CODE_SUCCESS;
1517 1518 1519 1520 1521
}

int32_t qEndTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 1));
1522
  return TSDB_CODE_SUCCESS;
1523 1524 1525 1526 1527
}

int32_t winDurFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 2));
1528
  return TSDB_CODE_SUCCESS;
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
}

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;
1541
}
1542 1543 1544

int32_t qTbnameFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560

  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;
1561 1562
  return TSDB_CODE_SUCCESS;
}