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

G
Ganlin Zhao 已提交
10 11 12 13
typedef float (*_float_fn)(float);
typedef double (*_double_fn)(double);
typedef double (*_double_fn_2)(double, double);
typedef int (*_conv_fn)(int);
H
Hongze Cheng 已提交
14
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 **/
H
Hongze Cheng 已提交
18
static double tlog(double v) { return log(v); }
19 20

static double tlog2(double v, double base) {
21 22 23 24 25 26
  double a = log(v);
  double b = log(base);
  if (isnan(a) || isinf(a)) {
    return a;
  } else if (isnan(b) || isinf(b)) {
    return b;
G
Ganlin Zhao 已提交
27 28
  } else if (b == 0) {
    return INFINITY;
29 30
  } else {
    return a / b;
31
  }
32 33
}

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

  int32_t type = GET_PARAM_TYPE(pInput);
39

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

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

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

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

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

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

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

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

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

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

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

141
  _getDoubleValue_fn_t getValueFn = getVectorDoubleValueFn(type);
142

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

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

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

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

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

173
  double *out = (double *)pOutputData->pData;
H
Hongze Cheng 已提交
174
  double  result;
175

H
Hongze Cheng 已提交
176
  bool hasNullType = (IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0])) || IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[1])));
177

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

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

        out[i] = result;
      }
210
    }
211
  } else if (pInput[1].numOfRows == 1) {
212
    if (colDataIsNull_s(pInputData[1], 0) || hasNullType) {
213
      colDataSetNNULL(pOutputData, 0, pInput[0].numOfRows);
214
    } else {
215 216
      for (int32_t i = 0; i < numOfRows; ++i) {
        if (colDataIsNull_s(pInputData[0], i)) {
217
          colDataSetNULL(pOutputData, i);
218 219 220 221 222
          continue;
        }

        result = valFn(getValueFn[0](pInputData[0]->pData, i), getValueFn[1](pInputData[1]->pData, 0));
        if (isinf(result) || isnan(result)) {
223
          colDataSetNULL(pOutputData, i);
224 225 226 227 228
          continue;
        }

        out[i] = result;
      }
229
    }
230
  }
231

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

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

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

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

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

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

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

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

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

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

G
Ganlin Zhao 已提交
288
/** String functions **/
H
Hongze Cheng 已提交
289
static int16_t tlength(char *input, int32_t type) { return varDataLen(input); }
G
Ganlin Zhao 已提交
290

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

299
static void tltrim(char *input, char *output, int32_t type, int32_t charLen) {
G
Ganlin Zhao 已提交
300 301 302 303 304 305 306 307
  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++;
    }
H
Hongze Cheng 已提交
308
  } else {  // NCHAR
G
Ganlin Zhao 已提交
309 310 311 312 313 314 315
    for (int32_t i = 0; i < charLen; ++i) {
      if (!iswspace(*((uint32_t *)varDataVal(input) + i))) {
        break;
      }
      numOfSpaces++;
    }
  }
G
Ganlin Zhao 已提交
316

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

G
Ganlin Zhao 已提交
326 327 328
  varDataSetLen(output, resLen);
}

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

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

G
Ganlin Zhao 已提交
355
  varDataSetLen(output, resLen);
G
Ganlin Zhao 已提交
356 357
}

358
static int32_t doLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _len_fn lenFn) {
G
Ganlin Zhao 已提交
359 360
  int32_t type = GET_PARAM_TYPE(pInput);

H
Hongze Cheng 已提交
361
  SColumnInfoData *pInputData = pInput->columnData;
G
Ganlin Zhao 已提交
362 363
  SColumnInfoData *pOutputData = pOutput->columnData;

364
  int64_t *out = (int64_t *)pOutputData->pData;
G
Ganlin Zhao 已提交
365 366

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

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

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

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

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

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

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

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

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

    if (hasNull) {
      continue;
    }

    int16_t dataLen = 0;
    for (int32_t i = 0; i < inputNum; ++i) {
462 463
      int32_t rowIdx = (pInput[i].numOfRows == 1) ? 0 : k;
      input[i] = colDataGetData(pInputData[i], rowIdx);
464 465 466 467

      ret = concatCopyHelper(input[i], output, hasNchar, GET_PARAM_TYPE(&pInput[i]), &dataLen);
      if (ret != TSDB_CODE_SUCCESS) {
        goto DONE;
468
      }
G
Ganlin Zhao 已提交
469
    }
470
    varDataSetLen(output, dataLen);
471
    colDataSetVal(pOutputData, k, output, false);
472
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
473 474
  }

475
  pOutput->numOfRows = numOfRows;
476 477

DONE:
478
  taosMemoryFree(input);
479
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
480 481
  taosMemoryFree(pInputData);

482
  return ret;
G
Ganlin Zhao 已提交
483 484 485
}

int32_t concatWsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
486
  int32_t           ret = TSDB_CODE_SUCCESS;
G
Ganlin Zhao 已提交
487
  SColumnInfoData **pInputData = taosMemoryCalloc(inputNum, sizeof(SColumnInfoData *));
H
Hongze Cheng 已提交
488 489 490
  SColumnInfoData  *pOutputData = pOutput->columnData;
  char            **input = taosMemoryCalloc(inputNum, POINTER_BYTES);
  char             *outputBuf = NULL;
G
Ganlin Zhao 已提交
491

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

    int32_t numOfNulls = getNumOfNullEntries(pInputData[i], pInput[i].numOfRows);
508 509
    if (i == 0) {
      // calculate required separator space
H
Hongze Cheng 已提交
510 511
      inputLen +=
          (pInputData[0]->varmeta.length - VARSTR_HEADER_SIZE) * (numOfRows - numOfNulls) * (inputNum - 2) * factor;
512
    } else if (pInput[i].numOfRows == 1) {
513
      inputLen += (pInputData[i]->varmeta.length - VARSTR_HEADER_SIZE) * (numOfRows - numOfNulls) * factor;
514
    } else {
515
      inputLen += (pInputData[i]->varmeta.length - (numOfRows - numOfNulls) * VARSTR_HEADER_SIZE) * factor;
516 517 518
    }
  }

519 520 521
  int32_t outputLen = inputLen + numOfRows * VARSTR_HEADER_SIZE;
  outputBuf = taosMemoryCalloc(outputLen, 1);
  char *output = outputBuf;
G
Ganlin Zhao 已提交
522

523
  for (int32_t k = 0; k < numOfRows; ++k) {
H
Hongze Cheng 已提交
524
    if (colDataIsNull_s(pInputData[0], k) || IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0]))) {
525
      colDataSetNULL(pOutputData, k);
G
Ganlin Zhao 已提交
526 527 528 529
      continue;
    }

    int16_t dataLen = 0;
H
Hongze Cheng 已提交
530
    bool    hasNull = false;
G
Ganlin Zhao 已提交
531
    for (int32_t i = 1; i < inputNum; ++i) {
H
Hongze Cheng 已提交
532
      if (colDataIsNull_s(pInputData[i], k) || IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[i]))) {
533 534
        hasNull = true;
        break;
G
Ganlin Zhao 已提交
535 536
      }

537 538
      int32_t rowIdx = (pInput[i].numOfRows == 1) ? 0 : k;
      input[i] = colDataGetData(pInputData[i], rowIdx);
539

540 541 542
      ret = concatCopyHelper(input[i], output, hasNchar, GET_PARAM_TYPE(&pInput[i]), &dataLen);
      if (ret != TSDB_CODE_SUCCESS) {
        goto DONE;
543
      }
G
Ganlin Zhao 已提交
544 545

      if (i < inputNum - 1) {
H
Hongze Cheng 已提交
546
        // insert the separator
547
        char *sep = (pInput[0].numOfRows == 1) ? colDataGetData(pInputData[0], 0) : colDataGetData(pInputData[0], k);
H
Hongze Cheng 已提交
548
        ret = concatCopyHelper(sep, output, hasNchar, GET_PARAM_TYPE(&pInput[0]), &dataLen);
549
        if (ret != TSDB_CODE_SUCCESS) {
550
          goto DONE;
551
        }
G
Ganlin Zhao 已提交
552 553
      }
    }
554 555

    if (hasNull) {
556
      colDataSetNULL(pOutputData, k);
557 558 559
      memset(output, 0, dataLen);
    } else {
      varDataSetLen(output, dataLen);
560
      colDataSetVal(pOutputData, k, output, false);
561 562
      output += varDataTLen(output);
    }
G
Ganlin Zhao 已提交
563 564
  }

565
  pOutput->numOfRows = numOfRows;
566 567

DONE:
568
  taosMemoryFree(input);
569
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
570 571
  taosMemoryFree(pInputData);

572
  return ret;
G
Ganlin Zhao 已提交
573 574
}

575
static int32_t doCaseConvFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _conv_fn convFn) {
G
Ganlin Zhao 已提交
576 577
  int32_t type = GET_PARAM_TYPE(pInput);

H
Hongze Cheng 已提交
578
  SColumnInfoData *pInputData = pInput->columnData;
G
Ganlin Zhao 已提交
579 580
  SColumnInfoData *pOutputData = pOutput->columnData;

581
  int32_t outputLen = pInputData->varmeta.length;
H
Hongze Cheng 已提交
582 583
  char   *outputBuf = taosMemoryCalloc(outputLen, 1);
  char   *output = outputBuf;
584

G
Ganlin Zhao 已提交
585
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
586
    if (colDataIsNull_s(pInputData, i)) {
587
      colDataSetNULL(pOutputData, i);
G
Ganlin Zhao 已提交
588 589 590
      continue;
    }

H
Hongze Cheng 已提交
591
    char   *input = colDataGetData(pInput[0].columnData, i);
592
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
593 594
    if (type == TSDB_DATA_TYPE_VARCHAR) {
      for (int32_t j = 0; j < len; ++j) {
595
        *(varDataVal(output) + j) = convFn(*(varDataVal(input) + j));
G
Ganlin Zhao 已提交
596
      }
H
Hongze Cheng 已提交
597
    } else {  // NCHAR
G
Ganlin Zhao 已提交
598
      for (int32_t j = 0; j < len / TSDB_NCHAR_SIZE; ++j) {
599
        *((uint32_t *)varDataVal(output) + j) = convFn(*((uint32_t *)varDataVal(input) + j));
G
Ganlin Zhao 已提交
600 601
      }
    }
602
    varDataSetLen(output, len);
603
    colDataSetVal(pOutputData, i, output, false);
604
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
605 606 607
  }

  pOutput->numOfRows = pInput->numOfRows;
608
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
609 610 611 612

  return TSDB_CODE_SUCCESS;
}

613
static int32_t doTrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, _trim_fn trimFn) {
G
Ganlin Zhao 已提交
614 615
  int32_t type = GET_PARAM_TYPE(pInput);

H
Hongze Cheng 已提交
616
  SColumnInfoData *pInputData = pInput->columnData;
G
Ganlin Zhao 已提交
617 618
  SColumnInfoData *pOutputData = pOutput->columnData;

619
  int32_t outputLen = pInputData->varmeta.length;
H
Hongze Cheng 已提交
620 621
  char   *outputBuf = taosMemoryCalloc(outputLen, 1);
  char   *output = outputBuf;
622

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

H
Hongze Cheng 已提交
629
    char   *input = colDataGetData(pInputData, i);
630
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
631
    int32_t charLen = (type == TSDB_DATA_TYPE_VARCHAR) ? len : len / TSDB_NCHAR_SIZE;
632 633
    trimFn(input, output, type, charLen);

634
    colDataSetVal(pOutputData, i, output, false);
635
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
636 637 638
  }

  pOutput->numOfRows = pInput->numOfRows;
639
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
640 641 642 643 644 645 646 647 648 649 650 651 652 653

  return TSDB_CODE_SUCCESS;
}

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

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

H
Hongze Cheng 已提交
654
  SColumnInfoData *pInputData = pInput->columnData;
G
Ganlin Zhao 已提交
655 656
  SColumnInfoData *pOutputData = pOutput->columnData;

657
  int32_t outputLen = pInputData->varmeta.length * pInput->numOfRows;
H
Hongze Cheng 已提交
658 659
  char   *outputBuf = taosMemoryCalloc(outputLen, 1);
  char   *output = outputBuf;
660 661

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
662
    if (colDataIsNull_s(pInputData, i)) {
663
      colDataSetNULL(pOutputData, i);
G
Ganlin Zhao 已提交
664 665
      continue;
    }
H
Hongze Cheng 已提交
666
    char   *input = colDataGetData(pInput[0].columnData, i);
667
    int32_t len = varDataLen(input);
G
Ganlin Zhao 已提交
668 669 670 671
    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 已提交
672
      startPosBytes = TMIN(startPosBytes, len);
G
Ganlin Zhao 已提交
673
    } else {
H
Hongze Cheng 已提交
674 675
      startPosBytes =
          (GET_PARAM_TYPE(pInput) == TSDB_DATA_TYPE_VARCHAR) ? len + subPos : len + subPos * TSDB_NCHAR_SIZE;
wafwerar's avatar
wafwerar 已提交
676
      startPosBytes = TMAX(startPosBytes, 0);
G
Ganlin Zhao 已提交
677 678
    }

wafwerar's avatar
wafwerar 已提交
679
    int32_t resLen = TMIN(subLen, len - startPosBytes);
680 681
    if (resLen > 0) {
      memcpy(varDataVal(output), varDataVal(input) + startPosBytes, resLen);
G
Ganlin Zhao 已提交
682 683
    }

684
    varDataSetLen(output, resLen);
685
    colDataSetVal(pOutputData, i, output, false);
686
    output += varDataTLen(output);
G
Ganlin Zhao 已提交
687 688 689
  }

  pOutput->numOfRows = pInput->numOfRows;
690
  taosMemoryFree(outputBuf);
G
Ganlin Zhao 已提交
691 692 693 694

  return TSDB_CODE_SUCCESS;
}

695
/** Conversion functions **/
G
Ganlin Zhao 已提交
696
int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
697 698
  int16_t inputType = GET_PARAM_TYPE(&pInput[0]);
  int16_t inputLen = GET_PARAM_BYTES(&pInput[0]);
699
  int16_t outputType = GET_PARAM_TYPE(&pOutput[0]);
H
Hongze Cheng 已提交
700
  int64_t outputLen = GET_PARAM_BYTES(&pOutput[0]);
G
Ganlin Zhao 已提交
701

702
  int32_t code = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
703 704
  char   *convBuf = taosMemoryMalloc(inputLen);
  char   *output = taosMemoryCalloc(1, outputLen + TSDB_NCHAR_SIZE);
705 706 707 708 709 710
  char    buf[400] = {0};

  if (convBuf == NULL || output == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }
G
Ganlin Zhao 已提交
711 712 713

  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
714
      colDataSetNULL(pOutput->columnData, i);
G
Ganlin Zhao 已提交
715 716
      continue;
    }
717

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

H
Hongze Cheng 已提交
720
    switch (outputType) {
721 722
      case TSDB_DATA_TYPE_TINYINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
723 724 725
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(int8_t *)output = taosStr2Int8(buf, NULL, 10);
726
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
H
Hongze Cheng 已提交
727
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
728
          if (len < 0) {
729 730
            code = TSDB_CODE_FAILED;
            goto _end;
731
          }
732 733 734

          convBuf[len] = 0;
          *(int8_t *)output = taosStr2Int8(convBuf, NULL, 10);
735 736 737 738 739 740 741
        } else {
          GET_TYPED_DATA(*(int8_t *)output, int8_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_SMALLINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
742 743 744
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(int16_t *)output = taosStr2Int16(buf, NULL, 10);
745
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
H
Hongze Cheng 已提交
746
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
747
          if (len < 0) {
748 749
            code = TSDB_CODE_FAILED;
            goto _end;
750
          }
751 752
          convBuf[len] = 0;
          *(int16_t *)output = taosStr2Int16(convBuf, NULL, 10);
753 754 755 756 757 758 759
        } else {
          GET_TYPED_DATA(*(int16_t *)output, int16_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_INT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
760 761 762
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(int32_t *)output = taosStr2Int32(buf, NULL, 10);
763
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
H
Hongze Cheng 已提交
764
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
765
          if (len < 0) {
766 767
            code = TSDB_CODE_FAILED;
            goto _end;
768
          }
769 770 771

          convBuf[len] = 0;
          *(int32_t *)output = taosStr2Int32(convBuf, NULL, 10);
772 773 774 775 776
        } else {
          GET_TYPED_DATA(*(int32_t *)output, int32_t, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
777 778
      case TSDB_DATA_TYPE_BIGINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
779 780 781
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(int64_t *)output = taosStr2Int64(buf, NULL, 10);
G
Ganlin Zhao 已提交
782
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
H
Hongze Cheng 已提交
783
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
G
Ganlin Zhao 已提交
784
          if (len < 0) {
785 786
            code = TSDB_CODE_FAILED;
            goto _end;
G
Ganlin Zhao 已提交
787
          }
788 789
          convBuf[len] = 0;
          *(int64_t *)output = taosStr2Int64(convBuf, NULL, 10);
G
Ganlin Zhao 已提交
790 791 792 793 794
        } else {
          GET_TYPED_DATA(*(int64_t *)output, int64_t, inputType, input);
        }
        break;
      }
795 796
      case TSDB_DATA_TYPE_UTINYINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
797 798 799
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(uint8_t *)output = taosStr2UInt8(buf, NULL, 10);
800
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
801
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
802
          if (len < 0) {
803 804
            code = TSDB_CODE_FAILED;
            goto _end;
805
          }
806 807
          convBuf[len] = 0;
          *(uint8_t *)output = taosStr2UInt8(convBuf, NULL, 10);
808 809 810 811 812 813 814
        } else {
          GET_TYPED_DATA(*(uint8_t *)output, uint8_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_USMALLINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
815 816 817
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(uint16_t *)output = taosStr2UInt16(buf, NULL, 10);
818
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
819
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
820
          if (len < 0) {
821 822
            code = TSDB_CODE_FAILED;
            goto _end;
823
          }
824 825
          convBuf[len] = 0;
          *(uint16_t *)output = taosStr2UInt16(convBuf, NULL, 10);
826 827 828 829 830 831 832
        } else {
          GET_TYPED_DATA(*(uint16_t *)output, uint16_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_UINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
833 834 835
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(uint32_t *)output = taosStr2UInt32(buf, NULL, 10);
836
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
837
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
838
          if (len < 0) {
839 840
            code = TSDB_CODE_FAILED;
            goto _end;
841
          }
842 843
          convBuf[len] = 0;
          *(uint32_t *)output = taosStr2UInt32(convBuf, NULL, 10);
844 845 846 847 848
        } else {
          GET_TYPED_DATA(*(uint32_t *)output, uint32_t, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
849 850
      case TSDB_DATA_TYPE_UBIGINT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
851 852 853
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(uint64_t *)output = taosStr2UInt64(buf, NULL, 10);
G
Ganlin Zhao 已提交
854
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
855
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
G
Ganlin Zhao 已提交
856
          if (len < 0) {
857 858
            code = TSDB_CODE_FAILED;
            goto _end;
G
Ganlin Zhao 已提交
859
          }
860 861 862

          convBuf[len] = 0;
          *(uint64_t *)output = taosStr2UInt64(convBuf, NULL, 10);
G
Ganlin Zhao 已提交
863 864 865 866 867
        } else {
          GET_TYPED_DATA(*(uint64_t *)output, uint64_t, inputType, input);
        }
        break;
      }
868 869
      case TSDB_DATA_TYPE_FLOAT: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
870 871 872
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(float *)output = taosStr2Float(buf, NULL);
873
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
H
Hongze Cheng 已提交
874
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
875
          if (len < 0) {
876 877
            code = TSDB_CODE_FAILED;
            goto _end;
878
          }
879 880
          convBuf[len] = 0;
          *(float *)output = taosStr2Float(convBuf, NULL);
881 882 883 884 885 886 887
        } else {
          GET_TYPED_DATA(*(float *)output, float, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
888 889 890
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(double *)output = taosStr2Double(buf, NULL);
891
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
H
Hongze Cheng 已提交
892
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
893
          if (len < 0) {
894 895
            code = TSDB_CODE_FAILED;
            goto _end;
896
          }
897 898
          convBuf[len] = 0;
          *(double *)output = taosStr2Double(convBuf, NULL);
899 900 901 902 903
        } else {
          GET_TYPED_DATA(*(double *)output, double, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
904 905
      case TSDB_DATA_TYPE_BOOL: {
        if (inputType == TSDB_DATA_TYPE_BINARY) {
906 907 908
          memcpy(buf, varDataVal(input), varDataLen(input));
          buf[varDataLen(input)] = 0;
          *(bool *)output = taosStr2Int8(buf, NULL, 10);
G
Ganlin Zhao 已提交
909
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
H
Hongze Cheng 已提交
910
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
G
Ganlin Zhao 已提交
911
          if (len < 0) {
912 913
            code = TSDB_CODE_FAILED;
            goto _end;
G
Ganlin Zhao 已提交
914
          }
915 916
          convBuf[len] = 0;
          *(bool *)output = taosStr2Int8(convBuf, NULL, 10);
G
Ganlin Zhao 已提交
917 918 919 920 921
        } else {
          GET_TYPED_DATA(*(bool *)output, bool, inputType, input);
        }
        break;
      }
G
Ganlin Zhao 已提交
922
      case TSDB_DATA_TYPE_TIMESTAMP: {
923
        int64_t timeVal;
G
Ganlin Zhao 已提交
924
        if (inputType == TSDB_DATA_TYPE_BINARY || inputType == TSDB_DATA_TYPE_NCHAR) {
925 926 927
          int64_t timePrec;
          GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);
          int32_t ret = convertStringToTimestamp(inputType, input, timePrec, &timeVal);
928 929 930 931 932
          if (ret != TSDB_CODE_SUCCESS) {
            *(int64_t *)output = 0;
          } else {
            *(int64_t *)output = timeVal;
          }
G
Ganlin Zhao 已提交
933 934 935 936 937 938 939
        } else {
          GET_TYPED_DATA(*(int64_t *)output, int64_t, inputType, input);
        }
        break;
      }
      case TSDB_DATA_TYPE_BINARY: {
        if (inputType == TSDB_DATA_TYPE_BOOL) {
H
Haojun Liao 已提交
940
          // NOTE: sprintf will append '\0' at the end of string
H
Hongze Cheng 已提交
941 942
          int32_t len = sprintf(varDataVal(output), "%.*s", (int32_t)(outputLen - VARSTR_HEADER_SIZE),
                                *(int8_t *)input ? "true" : "false");
G
Ganlin Zhao 已提交
943 944
          varDataSetLen(output, len);
        } else if (inputType == TSDB_DATA_TYPE_BINARY) {
wafwerar's avatar
wafwerar 已提交
945
          int32_t len = TMIN(varDataLen(input), outputLen - VARSTR_HEADER_SIZE);
H
Haojun Liao 已提交
946
          memcpy(varDataVal(output), varDataVal(input), len);
G
Ganlin Zhao 已提交
947
          varDataSetLen(output, len);
948
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
949
          int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(input), varDataLen(input), convBuf);
950
          if (len < 0) {
951 952
            code = TSDB_CODE_FAILED;
            goto _end;
953 954
          }
          len = TMIN(len, outputLen - VARSTR_HEADER_SIZE);
955
          memcpy(varDataVal(output), convBuf, len);
956
          varDataSetLen(output, len);
G
Ganlin Zhao 已提交
957
        } else {
958 959
          NUM_TO_STRING(inputType, input, sizeof(buf), buf);
          int32_t len = (int32_t)strlen(buf);
G
Ganlin Zhao 已提交
960
          len = (outputLen - VARSTR_HEADER_SIZE) > len ? len : (outputLen - VARSTR_HEADER_SIZE);
961
          memcpy(varDataVal(output), buf, len);
G
Ganlin Zhao 已提交
962 963 964 965 966 967
          varDataSetLen(output, len);
        }
        break;
      }
      case TSDB_DATA_TYPE_NCHAR: {
        int32_t outputCharLen = (outputLen - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
968
        int32_t len;
G
Ganlin Zhao 已提交
969 970
        if (inputType == TSDB_DATA_TYPE_BOOL) {
          char tmp[8] = {0};
H
Hongze Cheng 已提交
971
          len = sprintf(tmp, "%.*s", outputCharLen, *(int8_t *)input ? "true" : "false");
G
Ganlin Zhao 已提交
972 973
          bool ret = taosMbsToUcs4(tmp, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len);
          if (!ret) {
974 975
            code = TSDB_CODE_FAILED;
            goto _end;
G
Ganlin Zhao 已提交
976
          }
977

G
Ganlin Zhao 已提交
978 979
          varDataSetLen(output, len);
        } else if (inputType == TSDB_DATA_TYPE_BINARY) {
980
          len = outputCharLen > varDataLen(input) ? varDataLen(input) : outputCharLen;
H
Hongze Cheng 已提交
981 982
          bool ret = taosMbsToUcs4(input + VARSTR_HEADER_SIZE, len, (TdUcs4 *)varDataVal(output),
                                   outputLen - VARSTR_HEADER_SIZE, &len);
G
Ganlin Zhao 已提交
983
          if (!ret) {
984 985
            code = TSDB_CODE_FAILED;
            goto _end;
G
Ganlin Zhao 已提交
986 987 988
          }
          varDataSetLen(output, len);
        } else if (inputType == TSDB_DATA_TYPE_NCHAR) {
989 990 991
          len = TMIN(outputLen - VARSTR_HEADER_SIZE, varDataLen(input));
          memcpy(output, input, len + VARSTR_HEADER_SIZE);
          varDataSetLen(output, len);
G
Ganlin Zhao 已提交
992
        } else {
993 994
          NUM_TO_STRING(inputType, input, sizeof(buf), buf);
          len = (int32_t)strlen(buf);
G
Ganlin Zhao 已提交
995
          len = outputCharLen > len ? len : outputCharLen;
996
          bool ret = taosMbsToUcs4(buf, len, (TdUcs4 *)varDataVal(output), outputLen - VARSTR_HEADER_SIZE, &len);
G
Ganlin Zhao 已提交
997
          if (!ret) {
998 999
            code = TSDB_CODE_FAILED;
            goto _end;
G
Ganlin Zhao 已提交
1000 1001 1002
          }
          varDataSetLen(output, len);
        }
1003

H
Hongze Cheng 已提交
1004
        // for constant conversion, need to set proper length of pOutput description
G
Ganlin Zhao 已提交
1005
        if (len < outputLen) {
D
dapan1121 已提交
1006
          pOutput->columnData->info.bytes = len + VARSTR_HEADER_SIZE;
1007
        }
1008

G
Ganlin Zhao 已提交
1009 1010 1011
        break;
      }
      default: {
1012 1013
        code = TSDB_CODE_FAILED;
        goto _end;
G
Ganlin Zhao 已提交
1014 1015 1016
      }
    }

1017
    colDataSetVal(pOutput->columnData, i, output, false);
G
Ganlin Zhao 已提交
1018 1019 1020
  }

  pOutput->numOfRows = pInput->numOfRows;
1021

H
Hongze Cheng 已提交
1022
_end:
1023
  taosMemoryFree(output);
H
Haojun Liao 已提交
1024
  taosMemoryFree(convBuf);
1025
  return code;
G
Ganlin Zhao 已提交
1026
}
G
Ganlin Zhao 已提交
1027

1028 1029 1030
int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);

H
Hongze Cheng 已提交
1031
  bool    tzPresent = (inputNum == 2) ? true : false;
G
Ganlin Zhao 已提交
1032 1033
  char    tz[20] = {0};
  int32_t tzLen = 0;
1034 1035
  if (tzPresent) {
    tzLen = varDataLen(pInput[1].columnData->pData);
G
Ganlin Zhao 已提交
1036
    memcpy(tz, varDataVal(pInput[1].columnData->pData), tzLen);
1037
  }
1038

1039 1040
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
1041
      colDataSetNULL(pOutput->columnData, i);
1042 1043 1044
      continue;
    }

1045
    char *input = colDataGetData(pInput[0].columnData, i);
H
Hongze Cheng 已提交
1046 1047
    char  fraction[20] = {0};
    bool  hasFraction = false;
1048 1049 1050
    NUM_TO_STRING(type, input, sizeof(fraction), fraction);
    int32_t tsDigits = (int32_t)strlen(fraction);

H
Hongze Cheng 已提交
1051
    char    buf[64] = {0};
1052 1053 1054 1055 1056 1057
    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) {
1058
         timeVal = timeVal / ((int64_t)(1000 * 1000));
1059
      } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
1060
         timeVal = timeVal / ((int64_t)(1000 * 1000 * 1000));
1061
      } else {
1062
        colDataSetNULL(pOutput->columnData, i);
1063
        continue;
1064 1065 1066 1067 1068
      }
      hasFraction = true;
      memmove(fraction, fraction + TSDB_TIME_PRECISION_SEC_DIGITS, TSDB_TIME_PRECISION_SEC_DIGITS);
    }

1069
    struct tm tmInfo;
1070 1071 1072 1073 1074 1075 1076 1077
    int32_t len = 0;

    if (taosLocalTime((const time_t *)&timeVal, &tmInfo) == NULL) {
      sprintf(buf, "NaN");
      len = (int32_t)strlen(buf);
      goto _end;
    }

1078
    strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tmInfo);
1079
    len = (int32_t)strlen(buf);
1080

H
Hongze Cheng 已提交
1081
    // add timezone string
G
Ganlin Zhao 已提交
1082 1083 1084 1085
    if (tzLen > 0) {
      snprintf(buf + len, tzLen + 1, "%s", tz);
      len += tzLen;
    }
1086

1087 1088
    if (hasFraction) {
      int32_t fracLen = (int32_t)strlen(fraction) + 1;
1089 1090 1091 1092

      char *tzInfo;
      if (buf[len - 1] == 'z' || buf[len - 1] == 'Z') {
        tzInfo = &buf[len - 1];
1093 1094
        memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
      } else {
1095 1096 1097 1098
        tzInfo = strchr(buf, '+');
        if (tzInfo) {
          memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
        } else {
H
Hongze Cheng 已提交
1099
          // search '-' backwards
1100 1101 1102 1103 1104
          tzInfo = strrchr(buf, '-');
          if (tzInfo) {
            memmove(tzInfo + fracLen, tzInfo, strlen(tzInfo));
          }
        }
1105 1106
      }

1107
      char tmp[32] = {0};
1108 1109 1110 1111 1112
      sprintf(tmp, ".%s", fraction);
      memcpy(tzInfo, tmp, fracLen);
      len += fracLen;
    }

1113
_end:
1114 1115 1116
    memmove(buf + VARSTR_HEADER_SIZE, buf, len);
    varDataSetLen(buf, len);

1117
    colDataSetVal(pOutput->columnData, i, buf, false);
1118 1119 1120 1121 1122 1123 1124
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

1125 1126
int32_t toUnixtimestampFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);
1127 1128
  int64_t timePrec;
  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);
1129 1130 1131

  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
1132
      colDataSetNULL(pOutput->columnData, i);
1133 1134
      continue;
    }
1135
    char *input = colDataGetData(pInput[0].columnData, i);
1136 1137

    int64_t timeVal = 0;
1138 1139
    int32_t ret = convertStringToTimestamp(type, input, timePrec, &timeVal);
    if (ret != TSDB_CODE_SUCCESS) {
1140
      colDataSetNULL(pOutput->columnData, i);
1141
    } else {
1142
      colDataSetVal(pOutput->columnData, i, (char *)&timeVal, false);
1143
    }
1144 1145 1146 1147 1148 1149 1150
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

wmmhello's avatar
wmmhello 已提交
1151 1152 1153
int32_t toJsonFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(pInput);

wmmhello's avatar
wmmhello 已提交
1154
  char tmp[TSDB_MAX_JSON_TAG_LEN] = {0};
wmmhello's avatar
wmmhello 已提交
1155
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
H
Hongze Cheng 已提交
1156 1157
    SArray *pTagVals = taosArrayInit(8, sizeof(STagVal));
    STag   *pTag = NULL;
wmmhello's avatar
wmmhello 已提交
1158

wmmhello's avatar
wmmhello 已提交
1159 1160
    if (colDataIsNull_s(pInput[0].columnData, i)) {
      tTagNew(pTagVals, 1, true, &pTag);
H
Hongze Cheng 已提交
1161
    } else {
wmmhello's avatar
wmmhello 已提交
1162
      char *input = pInput[0].columnData->pData + pInput[0].columnData->varmeta.offset[i];
H
Hongze Cheng 已提交
1163
      if (varDataLen(input) > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
wmmhello's avatar
wmmhello 已提交
1164 1165
        taosArrayDestroy(pTagVals);
        return TSDB_CODE_FAILED;
wmmhello's avatar
wmmhello 已提交
1166 1167
      }
      memcpy(tmp, varDataVal(input), varDataLen(input));
wmmhello's avatar
wmmhello 已提交
1168
      tmp[varDataLen(input)] = 0;
H
Hongze Cheng 已提交
1169
      if (parseJsontoTagData(tmp, pTagVals, &pTag, NULL)) {
wmmhello's avatar
wmmhello 已提交
1170 1171
        tTagNew(pTagVals, 1, true, &pTag);
      }
wmmhello's avatar
wmmhello 已提交
1172 1173
    }

1174
    colDataSetVal(pOutput->columnData, i, (const char *)pTag, false);
wmmhello's avatar
wmmhello 已提交
1175 1176
    tTagFree(pTag);
    taosArrayDestroy(pTagVals);
wmmhello's avatar
wmmhello 已提交
1177 1178 1179 1180 1181 1182
  }

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

1183
/** Time functions **/
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
static int64_t offsetFromTz(char *timezone, int64_t factor) {
  char *minStr = &timezone[3];
  int64_t minutes = taosStr2Int64(minStr, NULL, 10);
  memset(minStr, 0, strlen(minStr));
  int64_t hours = taosStr2Int64(timezone, NULL, 10);
  int64_t seconds = hours * 3600 + minutes * 60;

  return seconds * factor;

}

1195 1196 1197
int32_t timeTruncateFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  int32_t type = GET_PARAM_TYPE(&pInput[0]);

1198
  int64_t timeUnit, timePrec, timeVal = 0;
1199 1200 1201
  bool    ignoreTz = true;
  char    timezone[20] = {0};

1202
  GET_TYPED_DATA(timeUnit, int64_t, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212

  int32_t timePrecIdx = 2, timeZoneIdx = 3;
  if (inputNum == 5) {
    timePrecIdx += 1;
    timeZoneIdx += 1;
    GET_TYPED_DATA(ignoreTz, bool, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
  }

  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[timePrecIdx]), pInput[timePrecIdx].columnData->pData);
  memcpy(timezone, varDataVal(pInput[timeZoneIdx].columnData->pData), varDataLen(pInput[timeZoneIdx].columnData->pData));
1213

1214
  int64_t factor = TSDB_TICK_PER_SECOND(timePrec);
1215
  int64_t unit = timeUnit * 1000 / factor;
1216

1217 1218
  for (int32_t i = 0; i < pInput[0].numOfRows; ++i) {
    if (colDataIsNull_s(pInput[0].columnData, i)) {
1219
      colDataSetNULL(pOutput->columnData, i);
1220 1221 1222
      continue;
    }

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

1225
    if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
1226 1227
      int32_t ret = convertStringToTimestamp(type, input, TSDB_TIME_PRECISION_NANO, &timeVal);
      if (ret != TSDB_CODE_SUCCESS) {
1228
        colDataSetNULL(pOutput->columnData, i);
1229 1230
        continue;
      }
H
Hongze Cheng 已提交
1231
      // If converted value is less than 10digits in second, use value in second instead
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
      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);

1250 1251
    switch (unit) {
      case 0: { /* 1u or 1b */
1252
        if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
1253 1254 1255 1256 1257
          if (timePrec == TSDB_TIME_PRECISION_NANO && timeUnit == 1) {
            timeVal = timeVal * 1;
          } else {
            timeVal = timeVal / 1000 * 1000;
          }
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        } 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;
H
Hongze Cheng 已提交
1272
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
1273 1274
          timeVal = timeVal * factor;
        } else {
1275
          colDataSetNULL(pOutput->columnData, i);
1276
          continue;
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
        }
        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 {
1290
          colDataSetNULL(pOutput->columnData, i);
1291
          continue;
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
        }
        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 {
1305
          colDataSetNULL(pOutput->columnData, i);
1306
          continue;
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
        }
        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 {
1320
          colDataSetNULL(pOutput->columnData, i);
1321
          continue;
1322 1323 1324 1325 1326
        }
        break;
      }
      case 86400000: { /* 1d */
        if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS) {
1327
          if (ignoreTz) {
1328
            timeVal = timeVal - (timeVal + offsetFromTz(timezone, 1000)) % (((int64_t)86400) * 1000);
1329 1330 1331
          } else {
            timeVal = timeVal / 1000 / 86400 * 86400 * 1000;
          }
1332
        } else if (tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS) {
1333
          if (ignoreTz) {
1334
            timeVal = timeVal - (timeVal + offsetFromTz(timezone, 1000000)) % (((int64_t)86400) * 1000000);
1335 1336 1337
          } else {
            timeVal = timeVal / 1000000 / 86400 * 86400 * 1000000;
          }
1338
        } else if (tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
1339
          if (ignoreTz) {
1340
            timeVal = timeVal - (timeVal + offsetFromTz(timezone, 1000000000)) % (((int64_t)86400) * 1000000000);
1341 1342 1343
          } else {
            timeVal = timeVal / 1000000000 / 86400 * 86400 * 1000000000;
          }
1344
        } else if (tsDigits <= TSDB_TIME_PRECISION_SEC_DIGITS) {
1345 1346 1347 1348 1349
          if (ignoreTz) {
            timeVal = (timeVal - (timeVal + offsetFromTz(timezone, 1)) % (86400L)) * factor;
          } else {
            timeVal = timeVal * factor / factor / 86400 * 86400 * factor;
          }
1350
        } else {
1351
          colDataSetNULL(pOutput->columnData, i);
1352
          continue;
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
        }
        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) {
H
Hongze Cheng 已提交
1364
          timeVal = timeVal * factor / factor / 604800 * 604800 * factor;
1365
        } else {
1366
          colDataSetNULL(pOutput->columnData, i);
1367
          continue;
1368 1369 1370 1371 1372 1373 1374 1375 1376
        }
        break;
      }
      default: {
        timeVal = timeVal * 1;
        break;
      }
    }

H
Hongze Cheng 已提交
1377
    // truncate the timestamp to db precision
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
    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;
      }
    }

1405
    colDataSetVal(pOutput->columnData, i, (char *)&timeVal, false);
1406 1407 1408 1409 1410 1411 1412
  }

  pOutput->numOfRows = pInput->numOfRows;

  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1413
int32_t timeDiffFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1414 1415
  int64_t timeUnit = -1, timePrec, timeVal[2] = {0};
  if (inputNum == 4) {
G
Ganlin Zhao 已提交
1416
    GET_TYPED_DATA(timeUnit, int64_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
1417 1418 1419
    GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[3]), pInput[3].columnData->pData);
  } else {
    GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);
G
Ganlin Zhao 已提交
1420 1421
  }

1422
  int64_t factor = TSDB_TICK_PER_SECOND(timePrec);
G
Ganlin Zhao 已提交
1423 1424 1425 1426
  int32_t numOfRows = 0;
  for (int32_t i = 0; i < inputNum; ++i) {
    if (pInput[i].numOfRows > numOfRows) {
      numOfRows = pInput[i].numOfRows;
G
Ganlin Zhao 已提交
1427 1428 1429
    }
  }

G
Ganlin Zhao 已提交
1430 1431 1432
  char *input[2];
  for (int32_t i = 0; i < numOfRows; ++i) {
    bool hasNull = false;
G
Ganlin Zhao 已提交
1433
    for (int32_t k = 0; k < 2; ++k) {
G
Ganlin Zhao 已提交
1434 1435 1436
      if (colDataIsNull_s(pInput[k].columnData, i)) {
        hasNull = true;
        break;
G
Ganlin Zhao 已提交
1437 1438
      }

1439 1440 1441
      int32_t rowIdx = (pInput[k].numOfRows == 1) ? 0 : i;
      input[k] = colDataGetData(pInput[k].columnData, rowIdx);

G
Ganlin Zhao 已提交
1442 1443
      int32_t type = GET_PARAM_TYPE(&pInput[k]);
      if (IS_VAR_DATA_TYPE(type)) { /* datetime format strings */
1444 1445
        int32_t ret = convertStringToTimestamp(type, input[k], TSDB_TIME_PRECISION_NANO, &timeVal[k]);
        if (ret != TSDB_CODE_SUCCESS) {
1446 1447
          hasNull = true;
          break;
1448
        }
G
Ganlin Zhao 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
      } 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 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) {
G
Ganlin Zhao 已提交
1468
          timeVal[k] = timeVal[k] * 1;
G
Ganlin Zhao 已提交
1469
        } else {
1470 1471
          hasNull = true;
          break;
G
Ganlin Zhao 已提交
1472
        }
G
Ganlin Zhao 已提交
1473 1474 1475
      }
    }

G
Ganlin Zhao 已提交
1476
    if (hasNull) {
1477
      colDataSetNULL(pOutput->columnData, i);
G
Ganlin Zhao 已提交
1478 1479 1480
      continue;
    }

H
Hongze Cheng 已提交
1481
    int64_t result = (timeVal[0] >= timeVal[1]) ? (timeVal[0] - timeVal[1]) : (timeVal[1] - timeVal[0]);
G
Ganlin Zhao 已提交
1482

H
Hongze Cheng 已提交
1483 1484
    if (timeUnit < 0) {  // if no time unit given use db precision
      switch (timePrec) {
G
Ganlin Zhao 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
        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 {
1499
      int64_t unit = timeUnit * 1000 / factor;
H
Hongze Cheng 已提交
1500
      switch (unit) {
1501 1502 1503 1504 1505 1506
        case 0: { /* 1u or 1b */
          if (timePrec == TSDB_TIME_PRECISION_NANO && timeUnit == 1) {
            result = result / 1;
          } else {
            result = result / 1000;
          }
G
Ganlin Zhao 已提交
1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
          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;
        }
      }
    }

1539
    colDataSetVal(pOutput->columnData, i, (char *)&result, false);
G
Ganlin Zhao 已提交
1540 1541
  }

G
Ganlin Zhao 已提交
1542
  pOutput->numOfRows = numOfRows;
G
Ganlin Zhao 已提交
1543 1544 1545 1546

  return TSDB_CODE_SUCCESS;
}

1547
int32_t nowFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1548 1549 1550 1551
  int64_t timePrec;
  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[0]), pInput[0].columnData->pData);

  int64_t ts = taosGetTimestamp(timePrec);
X
Xiaoyu Wang 已提交
1552
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
1553
    colDataSetInt64(pOutput->columnData, i, &ts);
1554
  }
X
Xiaoyu Wang 已提交
1555
  pOutput->numOfRows = pInput->numOfRows;
1556 1557 1558 1559
  return TSDB_CODE_SUCCESS;
}

int32_t todayFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1560
  int64_t timePrec;
1561
  GET_TYPED_DATA(timePrec, int64_t, GET_PARAM_TYPE(&pInput[0]), pInput[0].columnData->pData);
1562 1563

  int64_t ts = taosGetTimestampToday(timePrec);
X
Xiaoyu Wang 已提交
1564
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
1565
    colDataSetInt64(pOutput->columnData, i, &ts);
1566
  }
X
Xiaoyu Wang 已提交
1567
  pOutput->numOfRows = pInput->numOfRows;
1568 1569 1570
  return TSDB_CODE_SUCCESS;
}

1571
int32_t timezoneFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1572 1573 1574
  char output[TD_TIMEZONE_LEN + VARSTR_HEADER_SIZE] = {0};
  memcpy(varDataVal(output), tsTimezoneStr, TD_TIMEZONE_LEN);
  varDataSetLen(output, strlen(tsTimezoneStr));
X
Xiaoyu Wang 已提交
1575
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
1576
    colDataSetVal(pOutput->columnData, i, output, false);
1577
  }
X
Xiaoyu Wang 已提交
1578
  pOutput->numOfRows = pInput->numOfRows;
1579 1580 1581
  return TSDB_CODE_SUCCESS;
}

1582 1583 1584
int32_t atanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, atan);
}
1585

1586 1587 1588
int32_t sinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sin);
}
1589

1590 1591 1592
int32_t cosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, cos);
}
1593

1594 1595 1596
int32_t tanFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, tan);
}
1597

1598 1599 1600
int32_t asinFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, asin);
}
1601

1602 1603 1604
int32_t acosFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, acos);
}
H
Haojun Liao 已提交
1605

G
Ganlin Zhao 已提交
1606 1607 1608 1609 1610
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) {
1611 1612 1613 1614 1615
  if (inputNum == 1) {
    return doScalarFunctionUnique(pInput, inputNum, pOutput, tlog);
  } else {
    return doScalarFunctionUnique2(pInput, inputNum, pOutput, tlog2);
  }
G
Ganlin Zhao 已提交
1616 1617
}

1618 1619 1620
int32_t sqrtFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doScalarFunctionUnique(pInput, inputNum, pOutput, sqrt);
}
1621

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
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);
1632 1633
}

G
Ganlin Zhao 已提交
1634
int32_t lowerFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
wafwerar's avatar
wafwerar 已提交
1635 1636 1637
#ifdef WINDOWS
  return doCaseConvFunction(pInput, inputNum, pOutput, towlower);
#else
G
Ganlin Zhao 已提交
1638
  return doCaseConvFunction(pInput, inputNum, pOutput, tolower);
wafwerar's avatar
wafwerar 已提交
1639
#endif
1640 1641
}

G
Ganlin Zhao 已提交
1642
int32_t upperFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
wafwerar's avatar
wafwerar 已提交
1643 1644 1645
#ifdef WINDOWS
  return doCaseConvFunction(pInput, inputNum, pOutput, towupper);
#else
G
Ganlin Zhao 已提交
1646
  return doCaseConvFunction(pInput, inputNum, pOutput, toupper);
wafwerar's avatar
wafwerar 已提交
1647
#endif
1648 1649
}

G
Ganlin Zhao 已提交
1650 1651
int32_t ltrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, tltrim);
1652 1653
}

G
Ganlin Zhao 已提交
1654 1655
int32_t rtrimFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doTrimFunction(pInput, inputNum, pOutput, trtrim);
1656 1657
}

G
Ganlin Zhao 已提交
1658 1659 1660
int32_t lengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tlength);
}
1661

G
Ganlin Zhao 已提交
1662 1663
int32_t charLengthFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doLengthFunction(pInput, inputNum, pOutput, tcharlength);
1664 1665
}

H
Haojun Liao 已提交
1666
#if 0
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
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 已提交
1731
#endif
1732

H
Hongze Cheng 已提交
1733
bool getTimePseudoFuncEnv(SFunctionNode *UNUSED_PARAM(pFunc), SFuncExecEnv *pEnv) {
1734 1735 1736 1737 1738
  pEnv->calcMemSize = sizeof(int64_t);
  return true;
}

int32_t qStartTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1739
  colDataSetInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 0));
1740
  return TSDB_CODE_SUCCESS;
1741 1742 1743
}

int32_t qEndTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1744
  colDataSetInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 1));
1745
  return TSDB_CODE_SUCCESS;
1746 1747 1748
}

int32_t winDurFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1749
  colDataSetInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 2));
1750
  return TSDB_CODE_SUCCESS;
1751 1752 1753
}

int32_t winStartTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1754
  colDataSetInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 3));
1755 1756 1757 1758
  return TSDB_CODE_SUCCESS;
}

int32_t winEndTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
1759
  colDataSetInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 4));
1760
  return TSDB_CODE_SUCCESS;
1761
}
1762 1763

int32_t qTbnameFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Haojun Liao 已提交
1764
  char* p = colDataGetVarData(pInput->columnData, 0);
1765

1766
  colDataSetNItems(pOutput->columnData, pOutput->numOfRows, p, pInput->numOfRows);
1767
  pOutput->numOfRows += pInput->numOfRows;
1768 1769
  return TSDB_CODE_SUCCESS;
}
1770 1771 1772

/** Aggregation functions **/
int32_t countScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
1773
  SColumnInfoData *pInputData = pInput->columnData;
1774 1775 1776 1777 1778 1779
  SColumnInfoData *pOutputData = pOutput->columnData;

  int64_t *out = (int64_t *)pOutputData->pData;
  *out = 0;
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
1780
      continue;
1781 1782 1783 1784
    }
    (*out)++;
  }

G
Ganlin Zhao 已提交
1785
  pOutput->numOfRows = 1;
1786 1787
  return TSDB_CODE_SUCCESS;
}
1788 1789

int32_t sumScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
1790
  SColumnInfoData *pInputData = pInput->columnData;
1791 1792 1793
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
H
Hongze Cheng 已提交
1794
  bool    hasNull = false;
1795 1796
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
1797
      hasNull = true;
1798 1799 1800
      break;
    }

1801
    if (IS_SIGNED_NUMERIC_TYPE(type) || type == TSDB_DATA_TYPE_BOOL) {
1802
      int64_t *out = (int64_t *)pOutputData->pData;
1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
      if (type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_BOOL) {
        int8_t *in = (int8_t *)pInputData->pData;
        *out += in[i];
      } else if (type == TSDB_DATA_TYPE_SMALLINT) {
        int16_t *in = (int16_t *)pInputData->pData;
        *out += in[i];
      } else if (type == TSDB_DATA_TYPE_INT) {
        int32_t *in = (int32_t *)pInputData->pData;
        *out += in[i];
      } else if (type == TSDB_DATA_TYPE_BIGINT) {
        int64_t *in = (int64_t *)pInputData->pData;
        *out += in[i];
      }
1816 1817
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
      uint64_t *out = (uint64_t *)pOutputData->pData;
1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
      if (type == TSDB_DATA_TYPE_UTINYINT) {
        uint8_t *in = (uint8_t *)pInputData->pData;
        *out += in[i];
      } else if (type == TSDB_DATA_TYPE_USMALLINT) {
        uint16_t *in = (uint16_t *)pInputData->pData;
        *out += in[i];
      } else if (type == TSDB_DATA_TYPE_UINT) {
        uint32_t *in = (uint32_t *)pInputData->pData;
        *out += in[i];
      } else if (type == TSDB_DATA_TYPE_UBIGINT) {
        uint64_t *in = (uint64_t *)pInputData->pData;
        *out += in[i];
      }
1831 1832
    } else if (IS_FLOAT_TYPE(type)) {
      double *out = (double *)pOutputData->pData;
1833 1834 1835 1836 1837 1838 1839
      if (type == TSDB_DATA_TYPE_FLOAT) {
        float *in = (float *)pInputData->pData;
        *out += in[i];
      } else if (type == TSDB_DATA_TYPE_DOUBLE) {
        double *in = (double *)pInputData->pData;
        *out += in[i];
      }
1840 1841 1842
    }
  }

G
Ganlin Zhao 已提交
1843
  if (hasNull) {
1844
    colDataSetNULL(pOutputData, 0);
G
Ganlin Zhao 已提交
1845 1846 1847
  }

  pOutput->numOfRows = 1;
1848 1849
  return TSDB_CODE_SUCCESS;
}
1850

1851
static int32_t doMinMaxScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput, bool isMinFunc) {
H
Hongze Cheng 已提交
1852
  SColumnInfoData *pInputData = pInput->columnData;
1853 1854 1855
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
1856

G
Ganlin Zhao 已提交
1857
  bool hasNull = false;
1858 1859 1860 1861 1862
  if (isMinFunc) {
    SET_TYPED_DATA_MAX(pOutputData->pData, type);
  } else {
    SET_TYPED_DATA_MIN(pOutputData->pData, type);
  }
1863 1864 1865

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
1866
      hasNull = true;
1867 1868 1869
      break;
    }

H
Hongze Cheng 已提交
1870
    switch (type) {
1871 1872
      case TSDB_DATA_TYPE_BOOL:
      case TSDB_DATA_TYPE_TINYINT: {
H
Hongze Cheng 已提交
1873
        int8_t *in = (int8_t *)pInputData->pData;
1874
        int8_t *out = (int8_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1875
        if ((in[i] > *out) ^ isMinFunc) {
1876 1877 1878 1879 1880
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_SMALLINT: {
H
Hongze Cheng 已提交
1881
        int16_t *in = (int16_t *)pInputData->pData;
1882
        int16_t *out = (int16_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1883
        if ((in[i] > *out) ^ isMinFunc) {
1884 1885 1886 1887 1888
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_INT: {
H
Hongze Cheng 已提交
1889
        int32_t *in = (int32_t *)pInputData->pData;
1890
        int32_t *out = (int32_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1891
        if ((in[i] > *out) ^ isMinFunc) {
1892 1893 1894 1895 1896
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_BIGINT: {
H
Hongze Cheng 已提交
1897
        int64_t *in = (int64_t *)pInputData->pData;
1898
        int64_t *out = (int64_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1899
        if ((in[i] > *out) ^ isMinFunc) {
1900 1901 1902 1903 1904
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_UTINYINT: {
H
Hongze Cheng 已提交
1905
        uint8_t *in = (uint8_t *)pInputData->pData;
1906
        uint8_t *out = (uint8_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1907
        if ((in[i] > *out) ^ isMinFunc) {
1908 1909 1910 1911 1912
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_USMALLINT: {
H
Hongze Cheng 已提交
1913
        uint16_t *in = (uint16_t *)pInputData->pData;
1914
        uint16_t *out = (uint16_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1915
        if ((in[i] > *out) ^ isMinFunc) {
1916 1917 1918 1919 1920
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_UINT: {
H
Hongze Cheng 已提交
1921
        uint32_t *in = (uint32_t *)pInputData->pData;
1922
        uint32_t *out = (uint32_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1923
        if ((in[i] > *out) ^ isMinFunc) {
1924 1925 1926 1927 1928
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_UBIGINT: {
H
Hongze Cheng 已提交
1929
        uint64_t *in = (uint64_t *)pInputData->pData;
1930
        uint64_t *out = (uint64_t *)pOutputData->pData;
H
Hongze Cheng 已提交
1931
        if ((in[i] > *out) ^ isMinFunc) {
1932 1933 1934 1935 1936
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
H
Hongze Cheng 已提交
1937
        float *in = (float *)pInputData->pData;
1938
        float *out = (float *)pOutputData->pData;
H
Hongze Cheng 已提交
1939
        if ((in[i] > *out) ^ isMinFunc) {
1940 1941 1942 1943 1944
          *out = in[i];
        }
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
H
Hongze Cheng 已提交
1945
        double *in = (double *)pInputData->pData;
1946
        double *out = (double *)pOutputData->pData;
H
Hongze Cheng 已提交
1947
        if ((in[i] > *out) ^ isMinFunc) {
1948 1949 1950 1951 1952 1953 1954
          *out = in[i];
        }
        break;
      }
    }
  }

G
Ganlin Zhao 已提交
1955
  if (hasNull) {
1956
    colDataSetNULL(pOutputData, 0);
G
Ganlin Zhao 已提交
1957 1958 1959
  }

  pOutput->numOfRows = 1;
1960 1961
  return TSDB_CODE_SUCCESS;
}
1962 1963 1964 1965 1966 1967 1968 1969

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

int32_t maxScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return doMinMaxScalarFunction(pInput, inputNum, pOutput, false);
}
1970

1971
int32_t avgScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
1972
  SColumnInfoData *pInputData = pInput->columnData;
1973 1974 1975
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
1976
  int64_t count = 0;
H
Hongze Cheng 已提交
1977
  bool    hasNull = false;
1978 1979 1980 1981 1982 1983 1984

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
      hasNull = true;
      break;
    }

H
Hongze Cheng 已提交
1985
    switch (type) {
1986
      case TSDB_DATA_TYPE_TINYINT: {
H
Hongze Cheng 已提交
1987
        int8_t  *in = (int8_t *)pInputData->pData;
1988 1989
        int64_t *out = (int64_t *)pOutputData->pData;
        *out += in[i];
1990 1991 1992 1993
        count++;
        break;
      }
      case TSDB_DATA_TYPE_SMALLINT: {
H
Hongze Cheng 已提交
1994
        int16_t *in = (int16_t *)pInputData->pData;
1995 1996
        int64_t *out = (int64_t *)pOutputData->pData;
        *out += in[i];
1997 1998 1999 2000
        count++;
        break;
      }
      case TSDB_DATA_TYPE_INT: {
H
Hongze Cheng 已提交
2001
        int32_t *in = (int32_t *)pInputData->pData;
2002 2003
        int64_t *out = (int64_t *)pOutputData->pData;
        *out += in[i];
2004 2005 2006 2007
        count++;
        break;
      }
      case TSDB_DATA_TYPE_BIGINT: {
H
Hongze Cheng 已提交
2008
        int64_t *in = (int64_t *)pInputData->pData;
2009 2010
        int64_t *out = (int64_t *)pOutputData->pData;
        *out += in[i];
2011 2012 2013 2014
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UTINYINT: {
H
Hongze Cheng 已提交
2015
        uint8_t  *in = (uint8_t *)pInputData->pData;
2016 2017
        uint64_t *out = (uint64_t *)pOutputData->pData;
        *out += in[i];
2018 2019 2020 2021
        count++;
        break;
      }
      case TSDB_DATA_TYPE_USMALLINT: {
H
Hongze Cheng 已提交
2022
        uint16_t *in = (uint16_t *)pInputData->pData;
2023 2024
        uint64_t *out = (uint64_t *)pOutputData->pData;
        *out += in[i];
2025 2026 2027 2028
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UINT: {
H
Hongze Cheng 已提交
2029
        uint32_t *in = (uint32_t *)pInputData->pData;
2030 2031
        uint64_t *out = (uint64_t *)pOutputData->pData;
        *out += in[i];
2032 2033 2034 2035
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UBIGINT: {
H
Hongze Cheng 已提交
2036
        uint64_t *in = (uint64_t *)pInputData->pData;
2037 2038
        uint64_t *out = (uint64_t *)pOutputData->pData;
        *out += in[i];
2039 2040 2041 2042
        count++;
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
H
Hongze Cheng 已提交
2043
        float *in = (float *)pInputData->pData;
2044 2045
        float *out = (float *)pOutputData->pData;
        *out += in[i];
2046 2047 2048 2049
        count++;
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
H
Hongze Cheng 已提交
2050
        double *in = (double *)pInputData->pData;
2051 2052
        double *out = (double *)pOutputData->pData;
        *out += in[i];
2053 2054 2055 2056 2057 2058
        count++;
        break;
      }
    }
  }

G
Ganlin Zhao 已提交
2059
  if (hasNull || (count == 0)) {
2060
    colDataSetNULL(pOutputData, 0);
2061 2062
  } else {
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
H
Hongze Cheng 已提交
2063
      int64_t *out = (int64_t *)pOutputData->pData;
2064
      *(double *)out = *out / (double)count;
2065
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
H
Hongze Cheng 已提交
2066
      uint64_t *out = (uint64_t *)pOutputData->pData;
2067
      *(double *)out = *out / (double)count;
2068
    } else if (IS_FLOAT_TYPE(type)) {
H
Hongze Cheng 已提交
2069
      double *out = (double *)pOutputData->pData;
2070
      *(double *)out = *out / (double)count;
2071 2072 2073 2074 2075 2076 2077
    }
  }

  pOutput->numOfRows = 1;
  return TSDB_CODE_SUCCESS;
}

2078
int32_t stddevScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2079
  SColumnInfoData *pInputData = pInput->columnData;
2080 2081 2082
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
H
Hongze Cheng 已提交
2083
  // int64_t count = 0, sum = 0, qSum = 0;
G
Ganlin Zhao 已提交
2084
  bool hasNull = false;
2085 2086 2087

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
G
Ganlin Zhao 已提交
2088 2089
      hasNull = true;
      break;
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166
    }
#if 0
    switch(type) {
      case TSDB_DATA_TYPE_TINYINT: {
        int8_t *in  = (int8_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_SMALLINT: {
        int16_t *in  = (int16_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_INT: {
        int32_t *in  = (int32_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_BIGINT: {
        int64_t *in  = (int64_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UTINYINT: {
        uint8_t *in  = (uint8_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_USMALLINT: {
        uint16_t *in  = (uint16_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UINT: {
        uint32_t *in  = (uint32_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_UBIGINT: {
        uint64_t *in  = (uint64_t *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_FLOAT: {
        float *in  = (float *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
      case TSDB_DATA_TYPE_DOUBLE: {
        double *in  = (double *)pInputData->pData;
        sum += in[i];
        qSum += in[i] * in[i];
        count++;
        break;
      }
    }
#endif
  }

H
Hongze Cheng 已提交
2167
  double *out = (double *)pOutputData->pData;
G
Ganlin Zhao 已提交
2168
  if (hasNull) {
2169
    colDataSetNULL(pOutputData, 0);
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186
  } else {
    *out = 0;
#if 0
    double avg = 0;
    if (IS_SIGNED_NUMERIC_TYPE(type)) {
      avg = (int64_t)sum / (double)count;
      *out =  sqrt(fabs((int64_t)qSum / ((double)count) - avg * avg));
    } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) {
      avg = (uint64_t)sum / (double)count;
      *out =  sqrt(fabs((uint64_t)qSum / ((double)count) - avg * avg));
    } else if (IS_FLOAT_TYPE(type)) {
      avg = (double)sum / (double)count;
      *out =  sqrt(fabs((double)qSum / ((double)count) - avg * avg));
    }
#endif
  }

G
Ganlin Zhao 已提交
2187
  pOutput->numOfRows = 1;
2188 2189
  return TSDB_CODE_SUCCESS;
}
2190

2191 2192 2193 2194 2195 2196 2197 2198 2199 2200
#define LEASTSQR_CAL(p, x, y, index, step) \
  do {                                     \
    (p)[0][0] += (double)(x) * (x);        \
    (p)[0][1] += (double)(x);              \
    (p)[0][2] += (double)(x) * (y)[index]; \
    (p)[1][2] += (y)[index];               \
    (x) += step;                           \
  } while (0)

int32_t leastSQRScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2201
  SColumnInfoData *pInputData = pInput->columnData;
2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
  SColumnInfoData *pOutputData = pOutput->columnData;

  double startVal, stepVal;
  double matrix[2][3] = {0};
  GET_TYPED_DATA(startVal, double, GET_PARAM_TYPE(&pInput[1]), pInput[1].columnData->pData);
  GET_TYPED_DATA(stepVal, double, GET_PARAM_TYPE(&pInput[2]), pInput[2].columnData->pData);

  int32_t type = GET_PARAM_TYPE(pInput);
  int64_t count = 0;

H
Hongze Cheng 已提交
2212
  switch (type) {
2213
    case TSDB_DATA_TYPE_TINYINT: {
H
Hongze Cheng 已提交
2214
      int8_t *in = (int8_t *)pInputData->pData;
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_SMALLINT: {
H
Hongze Cheng 已提交
2226
      int16_t *in = (int16_t *)pInputData->pData;
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_INT: {
H
Hongze Cheng 已提交
2238
      int32_t *in = (int32_t *)pInputData->pData;
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_BIGINT: {
H
Hongze Cheng 已提交
2250
      int64_t *in = (int64_t *)pInputData->pData;
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_UTINYINT: {
H
Hongze Cheng 已提交
2262
      uint8_t *in = (uint8_t *)pInputData->pData;
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_USMALLINT: {
H
Hongze Cheng 已提交
2274
      uint16_t *in = (uint16_t *)pInputData->pData;
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_UINT: {
H
Hongze Cheng 已提交
2286
      uint32_t *in = (uint32_t *)pInputData->pData;
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
H
Hongze Cheng 已提交
2298
      uint64_t *in = (uint64_t *)pInputData->pData;
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
H
Hongze Cheng 已提交
2310
      float *in = (float *)pInputData->pData;
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
H
Hongze Cheng 已提交
2322
      double *in = (double *)pInputData->pData;
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
      for (int32_t i = 0; i < pInput->numOfRows; ++i) {
        if (colDataIsNull_s(pInputData, i)) {
          continue;
        }

        count++;
        LEASTSQR_CAL(matrix, startVal, in, i, stepVal);
      }
      break;
    }
  }

  if (count == 0) {
2336
    colDataSetNULL(pOutputData, 0);
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
  } else {
    matrix[1][1] = (double)count;
    matrix[1][0] = matrix[0][1];

    double matrix00 = matrix[0][0] - matrix[1][0] * (matrix[0][1] / matrix[1][1]);
    double matrix02 = matrix[0][2] - matrix[1][2] * (matrix[0][1] / matrix[1][1]);
    double matrix12 = matrix[1][2] - matrix02 * (matrix[1][0] / matrix00);
    matrix02 /= matrix00;

    matrix12 /= matrix[1][1];

    char   buf[64] = {0};
H
Hongze Cheng 已提交
2349 2350
    size_t len = snprintf(varDataVal(buf), sizeof(buf) - VARSTR_HEADER_SIZE, "{slop:%.6lf, intercept:%.6lf}", matrix02,
                          matrix12);
2351
    varDataSetLen(buf, len);
2352
    colDataSetVal(pOutputData, 0, buf, false);
2353 2354 2355 2356 2357
  }

  pOutput->numOfRows = 1;
  return TSDB_CODE_SUCCESS;
}
2358 2359

int32_t percentileScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2360
  SColumnInfoData *pInputData = pInput->columnData;
2361 2362 2363 2364 2365
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);

  double val;
H
Hongze Cheng 已提交
2366
  bool   hasNull = false;
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
      hasNull = true;
      break;
    }
    char *in = pInputData->pData;
    GET_TYPED_DATA(val, double, type, in);
  }

  if (hasNull) {
2377
    colDataSetNULL(pOutputData, 0);
2378
  } else {
2379
    colDataSetVal(pOutputData, 0, (char *)&val, false);
2380 2381 2382 2383 2384 2385 2386 2387 2388
  }

  pOutput->numOfRows = 1;
  return TSDB_CODE_SUCCESS;
}

int32_t apercentileScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return percentileScalarFunction(pInput, inputNum, pOutput);
}
2389 2390

int32_t spreadScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2391
  SColumnInfoData *pInputData = pInput->columnData;
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);

  double min, max;
  SET_DOUBLE_VAL(&min, DBL_MAX);
  SET_DOUBLE_VAL(&max, -DBL_MAX);

  bool hasNull = false;
  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
      hasNull = true;
      break;
    }

    char *in = pInputData->pData;

    double val = 0;
    GET_TYPED_DATA(val, double, type, in);

    if (val < GET_DOUBLE_VAL(&min)) {
      SET_DOUBLE_VAL(&min, val);
    }

    if (val > GET_DOUBLE_VAL(&max)) {
      SET_DOUBLE_VAL(&max, val);
    }
  }

  if (hasNull) {
2422
    colDataSetNULL(pOutputData, 0);
2423 2424
  } else {
    double result = max - min;
2425
    colDataSetVal(pOutputData, 0, (char *)&result, false);
2426 2427 2428 2429 2430
  }

  pOutput->numOfRows = 1;
  return TSDB_CODE_SUCCESS;
}
2431

G
Ganlin Zhao 已提交
2432
int32_t nonCalcScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2433
  SColumnInfoData *pInputData = pInput->columnData;
2434 2435 2436
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);
H
Hongze Cheng 已提交
2437
  bool    hasNull = false;
2438 2439 2440 2441 2442 2443 2444 2445

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
      hasNull = true;
      break;
    }
  }

H
Hongze Cheng 已提交
2446
  double *out = (double *)pOutputData->pData;
2447
  if (hasNull) {
2448
    colDataSetNULL(pOutputData, 0);
2449 2450 2451 2452 2453 2454 2455
  } else {
    *out = 0;
  }

  pOutput->numOfRows = 1;
  return TSDB_CODE_SUCCESS;
}
G
Ganlin Zhao 已提交
2456 2457 2458 2459 2460 2461 2462 2463

int32_t derivativeScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return nonCalcScalarFunction(pInput, inputNum, pOutput);
}

int32_t irateScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return nonCalcScalarFunction(pInput, inputNum, pOutput);
}
2464

2465 2466 2467 2468
int32_t diffScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return nonCalcScalarFunction(pInput, inputNum, pOutput);
}

2469 2470 2471
int32_t twaScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return avgScalarFunction(pInput, inputNum, pOutput);
}
2472 2473 2474 2475

int32_t mavgScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return avgScalarFunction(pInput, inputNum, pOutput);
}
2476 2477 2478 2479

int32_t hllScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return countScalarFunction(pInput, inputNum, pOutput);
}
2480 2481 2482 2483

int32_t csumScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return sumScalarFunction(pInput, inputNum, pOutput);
}
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524

typedef enum {
  STATE_OPER_INVALID = 0,
  STATE_OPER_LT,
  STATE_OPER_GT,
  STATE_OPER_LE,
  STATE_OPER_GE,
  STATE_OPER_NE,
  STATE_OPER_EQ,
} EStateOperType;

#define STATE_COMP(_op, _lval, _rval, _rtype) STATE_COMP_IMPL(_op, _lval, GET_STATE_VAL(_rval, _rtype))

#define GET_STATE_VAL(_val, _type) ((_type == TSDB_DATA_TYPE_BIGINT) ? (*(int64_t *)_val) : (*(double *)_val))

#define STATE_COMP_IMPL(_op, _lval, _rval) \
  do {                                     \
    switch (_op) {                         \
      case STATE_OPER_LT:                  \
        return ((_lval) < (_rval));        \
        break;                             \
      case STATE_OPER_GT:                  \
        return ((_lval) > (_rval));        \
        break;                             \
      case STATE_OPER_LE:                  \
        return ((_lval) <= (_rval));       \
        break;                             \
      case STATE_OPER_GE:                  \
        return ((_lval) >= (_rval));       \
        break;                             \
      case STATE_OPER_NE:                  \
        return ((_lval) != (_rval));       \
        break;                             \
      case STATE_OPER_EQ:                  \
        return ((_lval) == (_rval));       \
        break;                             \
      default:                             \
        break;                             \
    }                                      \
  } while (0)

H
Hongze Cheng 已提交
2525
static int8_t getStateOpType(char *opStr) {
2526
  int8_t opType;
2527
  if (strncasecmp(opStr, "LT", 2) == 0) {
2528
    opType = STATE_OPER_LT;
2529
  } else if (strncasecmp(opStr, "GT", 2) == 0) {
2530
    opType = STATE_OPER_GT;
2531
  } else if (strncasecmp(opStr, "LE", 2) == 0) {
2532
    opType = STATE_OPER_LE;
2533
  } else if (strncasecmp(opStr, "GE", 2) == 0) {
2534
    opType = STATE_OPER_GE;
2535
  } else if (strncasecmp(opStr, "NE", 2) == 0) {
2536
    opType = STATE_OPER_NE;
2537
  } else if (strncasecmp(opStr, "EQ", 2) == 0) {
2538 2539 2540 2541 2542 2543 2544 2545
    opType = STATE_OPER_EQ;
  } else {
    opType = STATE_OPER_INVALID;
  }

  return opType;
}

H
Hongze Cheng 已提交
2546 2547 2548
static bool checkStateOp(int8_t op, SColumnInfoData *pCol, int32_t index, SScalarParam *pCondParam) {
  char   *data = colDataGetData(pCol, index);
  char   *param = pCondParam->columnData->pData;
2549 2550 2551
  int32_t paramType = GET_PARAM_TYPE(pCondParam);
  switch (pCol->info.type) {
    case TSDB_DATA_TYPE_TINYINT: {
H
Hongze Cheng 已提交
2552
      int8_t v = *(int8_t *)data;
2553 2554 2555 2556
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_UTINYINT: {
H
Hongze Cheng 已提交
2557
      uint8_t v = *(uint8_t *)data;
2558 2559 2560 2561
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_SMALLINT: {
H
Hongze Cheng 已提交
2562
      int16_t v = *(int16_t *)data;
2563 2564 2565 2566
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_USMALLINT: {
H
Hongze Cheng 已提交
2567
      uint16_t v = *(uint16_t *)data;
2568 2569 2570 2571
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_INT: {
H
Hongze Cheng 已提交
2572
      int32_t v = *(int32_t *)data;
2573 2574 2575 2576
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_UINT: {
H
Hongze Cheng 已提交
2577
      uint32_t v = *(uint32_t *)data;
2578 2579 2580 2581
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_BIGINT: {
H
Hongze Cheng 已提交
2582
      int64_t v = *(int64_t *)data;
2583 2584 2585 2586
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_UBIGINT: {
H
Hongze Cheng 已提交
2587
      uint64_t v = *(uint64_t *)data;
2588 2589 2590 2591
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_FLOAT: {
H
Hongze Cheng 已提交
2592
      float v = *(float *)data;
2593 2594 2595 2596
      STATE_COMP(op, v, param, paramType);
      break;
    }
    case TSDB_DATA_TYPE_DOUBLE: {
H
Hongze Cheng 已提交
2597
      double v = *(double *)data;
2598 2599 2600 2601
      STATE_COMP(op, v, param, paramType);
      break;
    }
    default: {
G
Ganlin Zhao 已提交
2602
      return false;
2603 2604 2605 2606 2607 2608
    }
  }
  return false;
}

int32_t stateCountScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2609
  SColumnInfoData *pInputData = pInput->columnData;
2610 2611
  SColumnInfoData *pOutputData = pOutput->columnData;

H
Hongze Cheng 已提交
2612
  int8_t  op = getStateOpType(varDataVal(pInput[1].columnData->pData));
2613 2614 2615 2616
  int64_t count = 0;

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
2617
      colDataSetNULL(pOutputData, i);
2618 2619 2620
      continue;
    }

H
Hongze Cheng 已提交
2621
    bool    ret = checkStateOp(op, pInputData, i, &pInput[2]);
2622 2623 2624 2625 2626 2627
    int64_t out = -1;
    if (ret) {
      out = ++count;
    } else {
      count = 0;
    }
2628
    colDataSetVal(pOutputData, i, (char *)&out, false);
2629 2630 2631 2632 2633
  }

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

int32_t stateDurationScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2636
  SColumnInfoData *pInputData = pInput->columnData;
2637 2638 2639 2640 2641 2642
  SColumnInfoData *pOutputData = pOutput->columnData;

  int8_t op = getStateOpType(varDataVal(pInput[1].columnData->pData));

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
2643
      colDataSetNULL(pOutputData, i);
2644 2645 2646
      continue;
    }

H
Hongze Cheng 已提交
2647
    bool    ret = checkStateOp(op, pInputData, i, &pInput[2]);
2648 2649 2650 2651
    int64_t out = -1;
    if (ret) {
      out = 0;
    }
2652
    colDataSetVal(pOutputData, i, (char *)&out, false);
2653 2654 2655 2656 2657
  }

  pOutput->numOfRows = pInput->numOfRows;
  return TSDB_CODE_SUCCESS;
}
2658 2659 2660

typedef enum { UNKNOWN_BIN = 0, USER_INPUT_BIN, LINEAR_BIN, LOG_BIN } EHistoBinType;

H
Hongze Cheng 已提交
2661
static int8_t getHistogramBinType(char *binTypeStr) {
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
  int8_t binType;
  if (strcasecmp(binTypeStr, "user_input") == 0) {
    binType = USER_INPUT_BIN;
  } else if (strcasecmp(binTypeStr, "linear_bin") == 0) {
    binType = LINEAR_BIN;
  } else if (strcasecmp(binTypeStr, "log_bin") == 0) {
    binType = LOG_BIN;
  } else {
    binType = UNKNOWN_BIN;
  }

  return binType;
}

typedef struct SHistoFuncBin {
  double  lower;
  double  upper;
  int64_t count;
  double  percentage;
} SHistoFuncBin;

H
Hongze Cheng 已提交
2683 2684 2685
static bool getHistogramBinDesc(SHistoFuncBin **bins, int32_t *binNum, char *binDescStr, int8_t binType,
                                bool normalized) {
  cJSON  *binDesc = cJSON_Parse(binDescStr);
2686
  int32_t numOfBins;
H
Hongze Cheng 已提交
2687
  double *intervals;
2688 2689 2690 2691
  if (cJSON_IsObject(binDesc)) { /* linaer/log bins */
    int32_t numOfParams = cJSON_GetArraySize(binDesc);
    int32_t startIndex;
    if (numOfParams != 4) {
2692
      cJSON_Delete(binDesc);
2693 2694 2695
      return false;
    }

H
Hongze Cheng 已提交
2696 2697 2698 2699 2700
    cJSON *start = cJSON_GetObjectItem(binDesc, "start");
    cJSON *factor = cJSON_GetObjectItem(binDesc, "factor");
    cJSON *width = cJSON_GetObjectItem(binDesc, "width");
    cJSON *count = cJSON_GetObjectItem(binDesc, "count");
    cJSON *infinity = cJSON_GetObjectItem(binDesc, "infinity");
2701 2702

    if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
2703
      cJSON_Delete(binDesc);
2704 2705 2706 2707
      return false;
    }

    if (count->valueint <= 0 || count->valueint > 1000) {  // limit count to 1000
2708
      cJSON_Delete(binDesc);
2709 2710 2711 2712 2713
      return false;
    }

    if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) ||
        (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) {
2714
      cJSON_Delete(binDesc);
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731
      return false;
    }

    int32_t counter = (int32_t)count->valueint;
    if (infinity->valueint == false) {
      startIndex = 0;
      numOfBins = counter + 1;
    } else {
      startIndex = 1;
      numOfBins = counter + 3;
    }

    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
    if (cJSON_IsNumber(width) && factor == NULL && binType == LINEAR_BIN) {
      // linear bin process
      if (width->valuedouble == 0) {
        taosMemoryFree(intervals);
2732
        cJSON_Delete(binDesc);
2733 2734 2735 2736 2737 2738
        return false;
      }
      for (int i = 0; i < counter + 1; ++i) {
        intervals[startIndex] = start->valuedouble + i * width->valuedouble;
        if (isinf(intervals[startIndex])) {
          taosMemoryFree(intervals);
2739
          cJSON_Delete(binDesc);
2740 2741 2742 2743 2744 2745 2746 2747
          return false;
        }
        startIndex++;
      }
    } else if (cJSON_IsNumber(factor) && width == NULL && binType == LOG_BIN) {
      // log bin process
      if (start->valuedouble == 0) {
        taosMemoryFree(intervals);
2748
        cJSON_Delete(binDesc);
2749 2750 2751 2752
        return false;
      }
      if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) {
        taosMemoryFree(intervals);
2753
        cJSON_Delete(binDesc);
2754 2755 2756 2757 2758 2759
        return false;
      }
      for (int i = 0; i < counter + 1; ++i) {
        intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0);
        if (isinf(intervals[startIndex])) {
          taosMemoryFree(intervals);
2760
          cJSON_Delete(binDesc);
2761 2762 2763 2764 2765 2766
          return false;
        }
        startIndex++;
      }
    } else {
      taosMemoryFree(intervals);
2767
      cJSON_Delete(binDesc);
2768 2769 2770 2771 2772 2773 2774
      return false;
    }

    if (infinity->valueint == true) {
      intervals[0] = -INFINITY;
      intervals[numOfBins - 1] = INFINITY;
      // in case of desc bin orders, -inf/inf should be swapped
G
Ganlin Zhao 已提交
2775 2776 2777
      if (numOfBins < 4) {
        return false;
      }
2778 2779 2780 2781 2782 2783
      if (intervals[1] > intervals[numOfBins - 2]) {
        TSWAP(intervals[0], intervals[numOfBins - 1]);
      }
    }
  } else if (cJSON_IsArray(binDesc)) { /* user input bins */
    if (binType != USER_INPUT_BIN) {
2784
      cJSON_Delete(binDesc);
2785 2786 2787 2788
      return false;
    }
    numOfBins = cJSON_GetArraySize(binDesc);
    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
H
Hongze Cheng 已提交
2789
    cJSON *bin = binDesc->child;
2790 2791
    if (bin == NULL) {
      taosMemoryFree(intervals);
2792
      cJSON_Delete(binDesc);
2793 2794 2795 2796 2797 2798 2799
      return false;
    }
    int i = 0;
    while (bin) {
      intervals[i] = bin->valuedouble;
      if (!cJSON_IsNumber(bin)) {
        taosMemoryFree(intervals);
2800
        cJSON_Delete(binDesc);
2801 2802 2803 2804
        return false;
      }
      if (i != 0 && intervals[i] <= intervals[i - 1]) {
        taosMemoryFree(intervals);
2805
        cJSON_Delete(binDesc);
2806 2807 2808 2809 2810 2811
        return false;
      }
      bin = bin->next;
      i++;
    }
  } else {
2812
    cJSON_Delete(binDesc);
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824
    return false;
  }

  *binNum = numOfBins - 1;
  *bins = taosMemoryCalloc(numOfBins, sizeof(SHistoFuncBin));
  for (int32_t i = 0; i < *binNum; ++i) {
    (*bins)[i].lower = intervals[i] < intervals[i + 1] ? intervals[i] : intervals[i + 1];
    (*bins)[i].upper = intervals[i + 1] > intervals[i] ? intervals[i + 1] : intervals[i];
    (*bins)[i].count = 0;
  }

  taosMemoryFree(intervals);
2825 2826
  cJSON_Delete(binDesc);

2827 2828 2829 2830
  return true;
}

int32_t histogramScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2831
  SColumnInfoData *pInputData = pInput->columnData;
2832 2833 2834
  SColumnInfoData *pOutputData = pOutput->columnData;

  SHistoFuncBin *bins;
H
Hongze Cheng 已提交
2835 2836
  int32_t        numOfBins = 0;
  int32_t        totalCount = 0;
2837

2838 2839 2840 2841 2842
  char *binTypeStr = strndup(varDataVal(pInput[1].columnData->pData), varDataLen(pInput[1].columnData->pData));
  int8_t binType = getHistogramBinType(binTypeStr);
  taosMemoryFree(binTypeStr);

  char   *binDesc = strndup(varDataVal(pInput[2].columnData->pData), varDataLen(pInput[2].columnData->pData));
2843 2844 2845 2846
  int64_t normalized = *(int64_t *)(pInput[3].columnData->pData);

  int32_t type = GET_PARAM_TYPE(pInput);
  if (!getHistogramBinDesc(&bins, &numOfBins, binDesc, binType, (bool)normalized)) {
2847
    taosMemoryFree(binDesc);
2848 2849
    return TSDB_CODE_FAILED;
  }
2850
  taosMemoryFree(binDesc);
2851 2852 2853 2854 2855 2856

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
      continue;
    }

H
Hongze Cheng 已提交
2857
    char  *data = colDataGetData(pInputData, i);
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879
    double v;
    GET_TYPED_DATA(v, double, type, data);

    for (int32_t k = 0; k < numOfBins; ++k) {
      if (v > bins[k].lower && v <= bins[k].upper) {
        bins[k].count++;
        totalCount++;
        break;
      }
    }
  }

  if (normalized) {
    for (int32_t k = 0; k < numOfBins; ++k) {
      if (totalCount != 0) {
        bins[k].percentage = bins[k].count / (double)totalCount;
      } else {
        bins[k].percentage = 0;
      }
    }
  }

2880 2881
  colInfoDataEnsureCapacity(pOutputData, numOfBins, false);

2882 2883 2884 2885
  for (int32_t k = 0; k < numOfBins; ++k) {
    int32_t len;
    char    buf[512] = {0};
    if (!normalized) {
H
Hongze Cheng 已提交
2886 2887
      len = sprintf(varDataVal(buf), "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%" PRId64 "}", bins[k].lower,
                    bins[k].upper, bins[k].count);
2888
    } else {
H
Hongze Cheng 已提交
2889 2890
      len = sprintf(varDataVal(buf), "{\"lower_bin\":%g, \"upper_bin\":%g, \"count\":%lf}", bins[k].lower,
                    bins[k].upper, bins[k].percentage);
2891 2892
    }
    varDataSetLen(buf, len);
2893
    colDataSetVal(pOutputData, k, buf, false);
2894 2895 2896 2897 2898 2899
  }

  taosMemoryFree(bins);
  pOutput->numOfRows = numOfBins;
  return TSDB_CODE_SUCCESS;
}
2900 2901

int32_t selectScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
H
Hongze Cheng 已提交
2902
  SColumnInfoData *pInputData = pInput->columnData;
2903 2904 2905 2906 2907 2908
  SColumnInfoData *pOutputData = pOutput->columnData;

  int32_t type = GET_PARAM_TYPE(pInput);

  for (int32_t i = 0; i < pInput->numOfRows; ++i) {
    if (colDataIsNull_s(pInputData, i)) {
2909
      colDataSetNULL(pOutputData, 0);
2910 2911 2912
      continue;
    }

H
Hongze Cheng 已提交
2913
    char *data = colDataGetData(pInputData, i);
2914
    colDataSetVal(pOutputData, i, data, false);
2915 2916 2917 2918 2919 2920
  }

  pOutput->numOfRows = 1;
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
2921
int32_t topBotScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
2922 2923
  return selectScalarFunction(pInput, inputNum, pOutput);
}
2924 2925 2926 2927

int32_t firstLastScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return selectScalarFunction(pInput, inputNum, pOutput);
}
2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943

int32_t sampleScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return selectScalarFunction(pInput, inputNum, pOutput);
}

int32_t tailScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return selectScalarFunction(pInput, inputNum, pOutput);
}

int32_t uniqueScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return selectScalarFunction(pInput, inputNum, pOutput);
}

int32_t modeScalarFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) {
  return selectScalarFunction(pInput, inputNum, pOutput);
}