sclfunc.c 45.8 KB
Newer Older
H
Haojun Liao 已提交
1 2 3
#include "function.h"
#include "scalar.h"
#include "tdatablock.h"
4
#include "ttime.h"
H
Haojun Liao 已提交
5
#include "sclInt.h"
D
dapan1121 已提交
6
#include "sclvector.h"
7

G
Ganlin Zhao 已提交
8 9 10 11 12
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 已提交
13
typedef int16_t (*_len_fn)(char *, int32_t);
G
Ganlin Zhao 已提交
14

15
/** Math functions **/
16
static double tlog(double v, double base) {
G
Ganlin Zhao 已提交
17
  return log(v) / log(base);
18 19
}

20
int32_t absFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Haojun Liao 已提交
21 22 23 24 25
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
  if (!IS_NUMERIC_TYPE(type)) {
26 27
    return TSDB_CODE_FAILED;
  }
28

H
Haojun Liao 已提交
29
  switch (type) {
30
    case TSDB_DATA_TYPE_FLOAT: {
H
Haojun Liao 已提交
31 32
      float *in  = (float *)pInputData->pData;
      float *out = (float *)pOutputData->pData;
33
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
H
Haojun Liao 已提交
34
        if (colDataIsNull_f(pInputData->nullbitmap, i)) {
35
          colDataSetNull_f(pOutputData->nullbitmap, i);
H
Haojun Liao 已提交
36 37
          continue;
        }
38
        out[i] = (in[i] >= 0)? in[i] : -in[i];
39
      }
H
Haojun Liao 已提交
40
      break;
41 42 43
    }

    case TSDB_DATA_TYPE_DOUBLE: {
H
Haojun Liao 已提交
44 45
      double *in  = (double *)pInputData->pData;
      double *out = (double *)pOutputData->pData;
46
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
H
Haojun Liao 已提交
47
        if (colDataIsNull_f(pInputData->nullbitmap, i)) {
48
          colDataSetNull_f(pOutputData->nullbitmap, i);
H
Haojun Liao 已提交
49 50
          continue;
        }
51
        out[i] = (in[i] >= 0)? in[i] : -in[i];
H
Haojun Liao 已提交
52 53
      }
      break;
54 55
    }

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

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

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

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

H
Haojun Liao 已提交
108 109
    default: {
      colDataAssign(pOutputData, pInputData, pInput->numOfRows);
110 111
    }
  }
112

H
Haojun Liao 已提交
113
  pOutput->numOfRows = pInput->numOfRows;
114
  return TSDB_CODE_SUCCESS;
115 116
}

117
static int32_t doScalarFunctionUnique(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _double_fn valFn) {
H
Haojun Liao 已提交
118 119
  int32_t type = GET_PARAM_TYPE(pInput);
  if (inputNum != 1 || !IS_NUMERIC_TYPE(type)) {
120 121
    return TSDB_CODE_FAILED;
  }
122

123
  SColumnInfoData *pInputData = pInput->columnData;
H
Haojun Liao 已提交
124
  SColumnInfoData *pOutputData = pOutput->columnData;
125

126
  _getDoubleValue_fn_t getValueFn = getVectorDoubleValueFn(type);
127

128
  double *out = (double *)pOutputData->pData;
129

130 131 132
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_f(pInputData->nullbitmap, i)) {
      colDataSetNull_f(pOutputData->nullbitmap, i);
133
      continue;
134
    }
135
    out[i] = valFn(getValueFn(pInputData->pData, i));
136
  }
137

H
Haojun Liao 已提交
138
  pOutput->numOfRows = pInput->numOfRows;
139
  return TSDB_CODE_SUCCESS;
140 141
}

142
static int32_t doScalarFunctionUnique2(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _double_fn_2 valFn) {
G
Ganlin Zhao 已提交
143
  if (inputNum != 2 || !IS_NUMERIC_TYPE(GET_PARAM_TYPE(&pInput[0])) || !IS_NUMERIC_TYPE(GET_PARAM_TYPE(&pInput[1]))) {
144 145 146
    return TSDB_CODE_FAILED;
  }

G
Ganlin Zhao 已提交
147
  SColumnInfoData *pInputData[2];
H
Haojun Liao 已提交
148
  SColumnInfoData *pOutputData = pOutput->columnData;
G
Ganlin Zhao 已提交
149
  _getDoubleValue_fn_t getValueFn[2];
150

G
Ganlin Zhao 已提交
151 152 153 154
  for (int32_t i = 0; i < inputNum; ++i) {
    pInputData[i] = pInput[i].columnData;
    getValueFn[i]= getVectorDoubleValueFn(GET_PARAM_TYPE(&pInput[i]));
  }
155

156
  double *out = (double *)pOutputData->pData;
157

158
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
G
Ganlin Zhao 已提交
159 160
    if (colDataIsNull_f(pInputData[0]->nullbitmap, i) ||
        colDataIsNull_f(pInputData[1]->nullbitmap, 0)) {
161 162
      colDataSetNull_f(pOutputData->nullbitmap, i);
      continue;
163
    }
G
Ganlin Zhao 已提交
164
    out[i] = valFn(getValueFn[0](pInputData[0]->pData, i), getValueFn[1](pInputData[1]->pData, 0));
165
  }
166

H
Haojun Liao 已提交
167
  pOutput->numOfRows = pInput->numOfRows;
168 169 170
  return TSDB_CODE_SUCCESS;
}

171
static int32_t doScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam* pOutput, _float_fn f1, _double_fn d1) {
H
Haojun Liao 已提交
172 173
  int32_t type = GET_PARAM_TYPE(pInput);
  if (inputNum != 1 || !IS_NUMERIC_TYPE(type)) {
174 175 176
    return TSDB_CODE_FAILED;
  }

H
Haojun Liao 已提交
177 178
  SColumnInfoData *pInputData  = pInput->columnData;
  SColumnInfoData *pOutputData = pOutput->columnData;
179

H
Haojun Liao 已提交
180 181 182 183
  switch (type) {
    case TSDB_DATA_TYPE_FLOAT: {
      float *in  = (float *)pInputData->pData;
      float *out = (float *)pOutputData->pData;
184

185
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
H
Haojun Liao 已提交
186
        if (colDataIsNull_f(pInputData->nullbitmap, i)) {
187
          colDataSetNull_f(pOutputData->nullbitmap, i);
H
Haojun Liao 已提交
188 189
          continue;
        }
190
        out[i] = f1(in[i]);
191
      }
H
Haojun Liao 已提交
192 193
      break;
    }
194

H
Haojun Liao 已提交
195 196 197
    case TSDB_DATA_TYPE_DOUBLE: {
      double *in  = (double *)pInputData->pData;
      double *out = (double *)pOutputData->pData;
198

199
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
H
Haojun Liao 已提交
200
        if (colDataIsNull_f(pInputData->nullbitmap, i)) {
201
          colDataSetNull_f(pOutputData->nullbitmap, i);
H
Haojun Liao 已提交
202 203
          continue;
        }
204
        out[i] = d1(in[i]);
205
      }
H
Haojun Liao 已提交
206 207 208 209 210
      break;
    }

    default: {
      colDataAssign(pOutputData, pInputData, pInput->numOfRows);
211
    }
212 213
  }

H
Haojun Liao 已提交
214
  pOutput->numOfRows = pInput->numOfRows;
215 216
  return TSDB_CODE_SUCCESS;
}
217

G
Ganlin Zhao 已提交
218
/** String functions **/
219
static int16_t tlength(char *input, int32_t type) {
G
Ganlin Zhao 已提交
220 221 222
  return varDataLen(input);
}

223
static int16_t tcharlength(char *input, int32_t type) {
G
Ganlin Zhao 已提交
224 225 226 227
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    return varDataLen(input);
  } else { //NCHAR
    return varDataLen(input) / TSDB_NCHAR_SIZE;
G
Ganlin Zhao 已提交
228
  }
G
Ganlin Zhao 已提交
229
}
G
Ganlin Zhao 已提交
230

231
static void tltrim(char *input, char *output, int32_t type, int32_t charLen) {
G
Ganlin Zhao 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  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 已提交
248

G
Ganlin Zhao 已提交
249 250 251 252 253 254 255 256
  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 已提交
257

G
Ganlin Zhao 已提交
258 259 260
  varDataSetLen(output, resLen);
}

261
static void trtrim(char *input, char *output, int32_t type, int32_t charLen) {
G
Ganlin Zhao 已提交
262 263 264 265 266 267 268 269 270
  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
271
    for (int32_t i = charLen - 1; i >= 0; --i) {
G
Ganlin Zhao 已提交
272 273 274 275
      if (!iswspace(*((uint32_t *)varDataVal(input) + i))) {
        break;
      }
      numOfSpaces++;
G
Ganlin Zhao 已提交
276
    }
G
Ganlin Zhao 已提交
277
  }
G
Ganlin Zhao 已提交
278

G
Ganlin Zhao 已提交
279 280 281 282 283
  int32_t resLen;
  if (type == TSDB_DATA_TYPE_VARCHAR) {
    resLen = charLen - numOfSpaces;
  } else {
    resLen = (charLen - numOfSpaces) * TSDB_NCHAR_SIZE;
G
Ganlin Zhao 已提交
284
  }
G
Ganlin Zhao 已提交
285
  memcpy(varDataVal(output), varDataVal(input), resLen);
G
Ganlin Zhao 已提交
286

G
Ganlin Zhao 已提交
287
  varDataSetLen(output, resLen);
G
Ganlin Zhao 已提交
288 289
}

290
static int32_t doLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _len_fn lenFn) {
G
Ganlin Zhao 已提交
291 292 293 294 295 296 297 298 299 300 301
  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;

  int16_t *out = (int16_t *)pOutputData->pData;

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
302
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
303 304 305 306
      colDataSetNull_f(pOutputData->nullbitmap, i);
      continue;
    }

307
    char *in = pInputData->pData + pInputData->varmeta.offset[i];
G
Ganlin Zhao 已提交
308
    out[i] = lenFn(in, type);
G
Ganlin Zhao 已提交
309 310 311 312 313
  }

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

315 316
static int32_t concatCopyHelper(const char *input, char *output, bool hasNchar, int32_t type, int16_t *dataLen) {
  if (hasNchar && type == TSDB_DATA_TYPE_VARCHAR) {
317
    TdUcs4 *newBuf = taosMemoryCalloc((varDataLen(input) + 1) * TSDB_NCHAR_SIZE, 1);
318 319
    int32_t len = varDataLen(input);
    bool ret = taosMbsToUcs4(varDataVal(input), len, newBuf, (varDataLen(input) + 1) * TSDB_NCHAR_SIZE, &len);
320 321 322 323 324 325 326 327 328 329 330 331
    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;
332
}
G
Ganlin Zhao 已提交
333

334 335 336 337 338 339 340 341 342 343 344 345 346
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 已提交
347
int32_t concatFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
348
  int32_t ret = TSDB_CODE_SUCCESS;
G
Ganlin Zhao 已提交
349 350
  SColumnInfoData **pInputData = taosMemoryCalloc(inputNum, sizeof(SColumnInfoData *));
  SColumnInfoData *pOutputData = pOutput->columnData;
351
  char **input = taosMemoryCalloc(inputNum, POINTER_BYTES);
352
  char *outputBuf = NULL;
G
Ganlin Zhao 已提交
353

354
  int32_t inputLen = 0;
355
  int32_t numOfRows = 0;
356
  bool hasNchar = (GET_PARAM_TYPE(pOutput) == TSDB_DATA_TYPE_NCHAR) ? true : false;
357 358 359 360 361
  for (int32_t i = 0; i < inputNum; ++i) {
    if (pInput[i].numOfRows > numOfRows) {
      numOfRows = pInput[i].numOfRows;
    }
  }
G
Ganlin Zhao 已提交
362 363
  for (int32_t i = 0; i < inputNum; ++i) {
    pInputData[i] = pInput[i].columnData;
364
    int32_t factor = 1;
365
    if (hasNchar && (GET_PARAM_TYPE(&pInput[i]) == TSDB_DATA_TYPE_VARCHAR)) {
366 367
      factor = TSDB_NCHAR_SIZE;
    }
368 369

    int32_t numOfNulls = getNumOfNullEntries(pInputData[i], pInput[i].numOfRows);
370
    if (pInput[i].numOfRows == 1) {
371
      inputLen += (pInputData[i]->varmeta.length - VARSTR_HEADER_SIZE) * factor * (numOfRows - numOfNulls);
372
    } else {
373
      inputLen += pInputData[i]->varmeta.length - (numOfRows - numOfNulls) * VARSTR_HEADER_SIZE;
374
    }
375 376
  }

377 378 379
  int32_t outputLen = inputLen + numOfRows * VARSTR_HEADER_SIZE;
  outputBuf = taosMemoryCalloc(outputLen, 1);
  char *output = outputBuf;
G
Ganlin Zhao 已提交
380

381
  for (int32_t k = 0; k < numOfRows; ++k) {
382
    bool hasNull = false;
G
Ganlin Zhao 已提交
383
    for (int32_t i = 0; i < inputNum; ++i) {
384 385
      if (colDataIsNull_s(pInputData[i], k)) {
        colDataAppendNULL(pOutputData, k);
G
Ganlin Zhao 已提交
386 387 388 389 390 391 392 393 394
        hasNull = true;
        break;
      }
    }

    if (hasNull) {
      continue;
    }

395

G
Ganlin Zhao 已提交
396 397
    int16_t dataLen = 0;
    for (int32_t i = 0; i < inputNum; ++i) {
398 399 400 401
      if (pInput[i].numOfRows == 1) {
        input[i] = pInputData[i]->pData + pInputData[i]->varmeta.offset[0];
      } else {
        input[i] = pInputData[i]->pData + pInputData[i]->varmeta.offset[k];
402
      }
403 404 405 406

      ret = concatCopyHelper(input[i], output, hasNchar, GET_PARAM_TYPE(&pInput[i]), &dataLen);
      if (ret != TSDB_CODE_SUCCESS) {
        goto DONE;
407
      }
G
Ganlin Zhao 已提交
408
    }
409
    varDataSetLen(output, dataLen);
410 411
    colDataAppend(pOutputData, k, output, false);
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
412 413
  }

414
  pOutput->numOfRows = numOfRows;
415 416

DONE:
417
  taosMemoryFree(input);
418
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
419 420
  taosMemoryFree(pInputData);

421
  return ret;
G
Ganlin Zhao 已提交
422 423
}

424

G
Ganlin Zhao 已提交
425
int32_t concatWsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
426
  int32_t ret = TSDB_CODE_SUCCESS;
G
Ganlin Zhao 已提交
427 428
  SColumnInfoData **pInputData = taosMemoryCalloc(inputNum, sizeof(SColumnInfoData *));
  SColumnInfoData *pOutputData = pOutput->columnData;
429
  char **input = taosMemoryCalloc(inputNum, POINTER_BYTES);
430
  char *outputBuf = NULL;
G
Ganlin Zhao 已提交
431

432
  int32_t inputLen = 0;
433
  int32_t numOfRows = 0;
434
  bool hasNchar = (GET_PARAM_TYPE(pOutput) == TSDB_DATA_TYPE_NCHAR) ? true : false;
435
  for (int32_t i = 1; i < inputNum; ++i) {
436 437 438 439
    if (pInput[i].numOfRows > numOfRows) {
      numOfRows = pInput[i].numOfRows;
    }
  }
G
Ganlin Zhao 已提交
440 441
  for (int32_t i = 0; i < inputNum; ++i) {
    pInputData[i] = pInput[i].columnData;
442
    int32_t factor = 1;
443
    if (hasNchar && (GET_PARAM_TYPE(&pInput[i]) == TSDB_DATA_TYPE_VARCHAR)) {
444 445
      factor = TSDB_NCHAR_SIZE;
    }
446 447

    int32_t numOfNulls = getNumOfNullEntries(pInputData[i], pInput[i].numOfRows);
448 449
    if (i == 0) {
      // calculate required separator space
450
      inputLen += (pInputData[0]->varmeta.length - VARSTR_HEADER_SIZE) * (numOfRows - numOfNulls) * (inputNum - 2) * factor;
451
    } else if (pInput[i].numOfRows == 1) {
452
      inputLen += (pInputData[i]->varmeta.length - VARSTR_HEADER_SIZE) * (numOfRows - numOfNulls) * factor;
453
    } else {
454
      inputLen += pInputData[i]->varmeta.length - (numOfRows - numOfNulls) * VARSTR_HEADER_SIZE;
455 456 457
    }
  }

458 459 460
  int32_t outputLen = inputLen + numOfRows * VARSTR_HEADER_SIZE;
  outputBuf = taosMemoryCalloc(outputLen, 1);
  char *output = outputBuf;
G
Ganlin Zhao 已提交
461

462
  for (int32_t k = 0; k < numOfRows; ++k) {
463 464
    if (colDataIsNull_s(pInputData[0], k)) {
      colDataAppendNULL(pOutputData, k);
G
Ganlin Zhao 已提交
465 466 467 468
      continue;
    }

    int16_t dataLen = 0;
469
    bool hasNull = false;
G
Ganlin Zhao 已提交
470
    for (int32_t i = 1; i < inputNum; ++i) {
471
      if (colDataIsNull_s(pInputData[i], k)) {
472 473
        hasNull = true;
        break;
G
Ganlin Zhao 已提交
474 475
      }

476 477 478 479
      if (pInput[i].numOfRows == 1) {
        input[i] = pInputData[i]->pData + pInputData[i]->varmeta.offset[0];
      } else {
        input[i] = pInputData[i]->pData + pInputData[i]->varmeta.offset[k];
480 481
      }

482 483 484
      ret = concatCopyHelper(input[i], output, hasNchar, GET_PARAM_TYPE(&pInput[i]), &dataLen);
      if (ret != TSDB_CODE_SUCCESS) {
        goto DONE;
485
      }
G
Ganlin Zhao 已提交
486

487

G
Ganlin Zhao 已提交
488 489
      if (i < inputNum - 1) {
        //insert the separator
490 491
        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);
492
        if (ret != TSDB_CODE_SUCCESS) {
493
          goto DONE;
494
        }
G
Ganlin Zhao 已提交
495 496
      }
    }
497 498 499 500 501 502 503 504 505

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

508
  pOutput->numOfRows = numOfRows;
509 510

DONE:
511
  taosMemoryFree(input);
512
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
513 514
  taosMemoryFree(pInputData);

515
  return ret;
G
Ganlin Zhao 已提交
516 517
}

518
static int32_t doCaseConvFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _conv_fn convFn) {
G
Ganlin Zhao 已提交
519 520 521 522 523 524 525 526
  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;

527 528
  int32_t outputLen = pInputData->varmeta.length;
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
529
  char *output = outputBuf;
530

G
Ganlin Zhao 已提交
531
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
532 533
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
534 535 536
      continue;
    }

537
    char *input = pInputData->pData + pInputData->varmeta.offset[i];
538
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
539 540
    if (type == TSDB_DATA_TYPE_VARCHAR) {
      for (int32_t j = 0; j < len; ++j) {
541
        *(varDataVal(output) + j) = convFn(*(varDataVal(input) + j));
G
Ganlin Zhao 已提交
542 543 544
      }
    } else { //NCHAR
      for (int32_t j = 0; j < len / TSDB_NCHAR_SIZE; ++j) {
545
        *((uint32_t *)varDataVal(output) + j) = convFn(*((uint32_t *)varDataVal(input) + j));
G
Ganlin Zhao 已提交
546 547
      }
    }
548
    varDataSetLen(output, len);
549
    colDataAppend(pOutputData, i, output, false);
550
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
551 552 553
  }

  pOutput->numOfRows = pInput->numOfRows;
554
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
555 556 557 558 559

  return TSDB_CODE_SUCCESS;
}


560
static int32_t doTrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _trim_fn trimFn) {
G
Ganlin Zhao 已提交
561 562 563 564 565 566 567 568
  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;

569 570
  int32_t outputLen = pInputData->varmeta.length;
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
571
  char *output = outputBuf;
572

G
Ganlin Zhao 已提交
573
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
574 575
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
576 577
      continue;
    }
578
    char *input  = pInputData->pData + pInputData->varmeta.offset[i];
G
Ganlin Zhao 已提交
579

580
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
581
    int32_t charLen = (type == TSDB_DATA_TYPE_VARCHAR) ? len : len / TSDB_NCHAR_SIZE;
582 583 584
    trimFn(input, output, type, charLen);

    varDataSetLen(output, len);
585
    colDataAppend(pOutputData, i, output, false);
586
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
587 588 589
  }

  pOutput->numOfRows = pInput->numOfRows;
590
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
591 592 593 594 595

  return TSDB_CODE_SUCCESS;
}

int32_t substrFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
596
  if (inputNum != 2 && inputNum!= 3) {
G
Ganlin Zhao 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    return TSDB_CODE_FAILED;
  }

  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);
    if (subLen < 0) { //subLen cannot be negative
      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;

618
  char *input  = pInputData->pData + pInputData->varmeta.offset[0];
619 620
  char *output = NULL;

621
  int32_t outputLen = pInputData->varmeta.length * pInput->numOfRows;
622 623
  char *outputBuf = taosMemoryCalloc(outputLen, 1);
  output = outputBuf;
624 625

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
626 627
    if (colDataIsNull_s(pInputData, i)) {
      colDataAppendNULL(pOutputData, i);
G
Ganlin Zhao 已提交
628 629 630
      continue;
    }

631
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
632 633 634 635 636 637 638 639 640 641
    int32_t startPosBytes;

    if (subPos > 0) {
      startPosBytes = (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? subPos - 1 : (subPos - 1) * TSDB_NCHAR_SIZE;
      startPosBytes = MIN(startPosBytes, len);
    } else {
      startPosBytes = (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? len + subPos : len + subPos * TSDB_NCHAR_SIZE;
      startPosBytes = MAX(startPosBytes, 0);
    }

642 643 644
    int32_t resLen = MIN(subLen, len - startPosBytes);
    if (resLen > 0) {
      memcpy(varDataVal(output), varDataVal(input) + startPosBytes, resLen);
G
Ganlin Zhao 已提交
645 646
    }

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

  pOutput->numOfRows = pInput->numOfRows;
654
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
655 656 657 658

  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
659 660
int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int16_t inputType  = pInput[0].columnData->info.type;
X
Xiaoyu Wang 已提交
661 662
  int16_t outputType = pOutput[0].columnData->info.type;
  int64_t outputLen = pOutput[0].columnData->info.bytes;
G
Ganlin Zhao 已提交
663

664 665 666 667 668
  if (IS_VAR_DATA_TYPE(outputType)) {
    int32_t factor = (TSDB_DATA_TYPE_NCHAR == outputType) ? TSDB_NCHAR_SIZE : 1;
    outputLen = outputLen * factor + VARSTR_HEADER_SIZE;
  }

G
Ganlin Zhao 已提交
669 670 671 672 673 674 675 676
  char *outputBuf = taosMemoryCalloc(outputLen * pInput[0].numOfRows, 1);
  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;
    }
677
    char *input = colDataGetData(pInput[0].columnData, i);
G
Ganlin Zhao 已提交
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

    switch(outputType) {
      case TSDB_DATA_TYPE_BIGINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
          memcpy(output, varDataVal(input), varDataLen(input));
          *(int64_t *)output = strtoll(output, NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, outputLen * TSDB_NCHAR_SIZE + 1);
          int32_t len  = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(int64_t *)output = strtoll(newBuf, NULL, 10);
          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) {
          memcpy(output, varDataVal(input), varDataLen(input));
          *(uint64_t *)output = strtoull(output, NULL, 10);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
          char *newBuf = taosMemoryCalloc(1, outputLen * TSDB_NCHAR_SIZE + 1);
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), newBuf);
          if (len < 0) {
            taosMemoryFree(newBuf);
            return TSDB_CODE_FAILED;
          }
          newBuf[len] = 0;
          *(uint64_t *)output = strtoull(newBuf, NULL, 10);
          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) {
          int32_t len = sprintf(varDataVal(output), "%.*s", (int32_t)(outputLen - VARSTR_HEADER_SIZE), varDataVal(input));
          varDataSetLen(output, len);
734
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
G
Ganlin Zhao 已提交
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 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
          //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;
        if (inputType == TSDB_DATA_TYPE_BOOL) {
          char tmp[8] = {0};
          int32_t len = sprintf(tmp, "%.*s", outputCharLen, *(int8_t *)input ? "true" : "false" );
          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) {
          int32_t len = outputCharLen > varDataLen(input) ? varDataLen(input) : outputCharLen;
          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) {
          int32_t len = MIN(outputLen, varDataLen(input) + VARSTR_HEADER_SIZE);
          memcpy(output, input, len);
          varDataSetLen(output, len - VARSTR_HEADER_SIZE);
        } else {
          char tmp[400] = {0};
          NUM_TO_STRING(inputType, input, sizeof(tmp), tmp);
          int32_t len = (int32_t)strlen(tmp);
          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);
        }
        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 已提交
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
int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);

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

    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 {
        assert(0);
      }
      hasFraction = true;
      memmove(fraction, fraction + TSDB_TIME_PRECISION_SEC_DIGITS, TSDB_TIME_PRECISION_SEC_DIGITS);
    }

831
    struct tm *tmInfo = taosLocalTime((const time_t *)&timeVal, NULL);
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", tmInfo);
    int32_t len = (int32_t)strlen(buf);

    if (hasFraction) {
      int32_t fracLen = (int32_t)strlen(fraction) + 1;
      char *tzInfo = strchr(buf, '+');
      if (tzInfo) {
        memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
      } else {
        tzInfo = strchr(buf, '-');
        memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
      }

      char tmp[32];
      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);
    input   += tDataTypes[type].bytes;
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

863 864 865 866 867 868 869 870 871 872 873 874
int32_t toUnixtimestampFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);
  int32_t timePrec = GET_PARAM_PRECISON(pInput);

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

    int64_t timeVal = 0;
875 876 877 878 879
    int32_t ret = convertStringToTimestamp(type, input, timePrec, &timeVal);
    if (ret != TSDB_CODE_SUCCESS) {
      colDataAppendNULL(pOutput->columnData, i);
      continue;
    }
880 881 882 883 884 885 886 887 888 889

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

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
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);

  char *input = NULL;
  if (IS_VAR_DATA_TYPE(type)) {
    input = pInput[0].columnData->pData + pInput[0].columnData->varmeta.offset[0];
  } else {
    input = pInput[0].columnData->pData;
  }

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

    if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
914 915 916 917 918
      int32_t ret = convertStringToTimestamp(type, input, TSDB_TIME_PRECISION_NANO, &timeVal);
      if (ret != TSDB_CODE_SUCCESS) {
        colDataAppendNULL(pOutput->columnData, i);
        continue;
      }
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 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 1082
      //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 {
          assert(0);
        }
        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 {
          assert(0);
        }
        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 {
          assert(0);
        }
        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 {
          assert(0);
        }
        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 {
          assert(0);
        }
        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 {
          assert(0);
        }
        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);
    if (IS_VAR_DATA_TYPE(type)) {
      input += varDataTLen(input);
    } else {
      input += tDataTypes[type].bytes;
    }
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
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);
  }

  char *input[2];
  for (int32_t k = 0; k < 2; ++k) {
    int32_t type = GET_PARAM_TYPE(&pInput[k]);
    if (type != TSDB_DATA_TYPE_BIGINT && type != TSDB_DATA_TYPE_TIMESTAMP &&
        type != TSDB_DATA_TYPE_BINARY && type != TSDB_DATA_TYPE_NCHAR) {
      return TSDB_CODE_FAILED;
    }

    if (IS_VAR_DATA_TYPE(type)) {
      input[k] = pInput[k].columnData->pData + pInput[k].columnData->varmeta.offset[0];
    } else {
      input[k] = pInput[k].columnData->pData;
    }
  }

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

      int32_t type = GET_PARAM_TYPE(&pInput[k]);
      if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
1114 1115 1116 1117 1118
        int32_t ret = convertStringToTimestamp(type, input[k], TSDB_TIME_PRECISION_NANO, &timeVal[k]);
        if (ret != TSDB_CODE_SUCCESS) {
          colDataAppendNULL(pOutput->columnData, i);
          continue;
        }
G
Ganlin Zhao 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
      } 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 已提交
1144 1145 1146 1147 1148 1149
      if (pInput[k].numOfRows != 1) {
        if (IS_VAR_DATA_TYPE(type)) {
          input[k] += varDataTLen(input[k]);
        } else {
          input[k] += tDataTypes[type].bytes;
        }
G
Ganlin Zhao 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 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 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
      }
    }

    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);
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
int32_t nowFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  if (inputNum != 1) {
    return TSDB_CODE_FAILED;
  }
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 0));
  return TSDB_CODE_SUCCESS;
}

int32_t todayFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  if (inputNum != 1) {
    return TSDB_CODE_FAILED;
  }
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 0));
  return TSDB_CODE_SUCCESS;
}

1234 1235 1236 1237 1238 1239 1240 1241
int32_t timezoneFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  if (inputNum != 1) {
    return TSDB_CODE_FAILED;
  }
  colDataAppend(pOutput->columnData, pOutput->numOfRows, (char *)colDataGetData(pInput->columnData, 0), false);
  return TSDB_CODE_SUCCESS;
}

1242 1243 1244
int32_t atanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, atan);
}
1245

1246 1247 1248
int32_t sinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sin);
}
1249

1250 1251 1252
int32_t cosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, cos);
}
1253

1254 1255 1256
int32_t tanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, tan);
}
1257

1258 1259 1260
int32_t asinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, asin);
}
1261

1262 1263 1264
int32_t acosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, acos);
}
H
Haojun Liao 已提交
1265

G
Ganlin Zhao 已提交
1266 1267 1268 1269 1270 1271 1272 1273
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) {
  return doScalarFunctionUnique2(pInput, inputNum, pOutput, tlog);
}

1274 1275 1276
int32_t sqrtFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sqrt);
}
1277

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
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);
1288 1289
}

G
Ganlin Zhao 已提交
1290 1291
int32_t lowerFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doCaseConvFunction(pInput, inputNum, pOutput, tolower);
1292 1293
}

G
Ganlin Zhao 已提交
1294 1295
int32_t upperFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doCaseConvFunction(pInput, inputNum, pOutput, toupper);
1296 1297
}

G
Ganlin Zhao 已提交
1298 1299
int32_t ltrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, tltrim);
1300 1301
}

G
Ganlin Zhao 已提交
1302 1303
int32_t rtrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, trtrim);
1304 1305
}

G
Ganlin Zhao 已提交
1306 1307 1308
int32_t lengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tlength);
}
1309

G
Ganlin Zhao 已提交
1310 1311
int32_t charLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tcharlength);
1312 1313
}

H
Haojun Liao 已提交
1314
#if 0
1315 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 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
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 已提交
1379
#endif
1380

1381 1382 1383 1384 1385 1386 1387 1388
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));
1389
  return TSDB_CODE_SUCCESS;
1390 1391 1392 1393 1394
}

int32_t qEndTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 1));
1395
  return TSDB_CODE_SUCCESS;
1396 1397 1398 1399 1400
}

int32_t winDurFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  ASSERT(inputNum == 1);
  colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 2));
1401
  return TSDB_CODE_SUCCESS;
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
}

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;
1414
}