builtins.c 120.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "builtins.h"
H
Haojun Liao 已提交
17
#include "builtinsimpl.h"
18
#include "cJSON.h"
19
#include "querynodes.h"
20
#include "scalar.h"
21
#include "taoserror.h"
G
Ganlin Zhao 已提交
22
#include "ttime.h"
23

24 25 26 27 28 29 30 31 32
static int32_t buildFuncErrMsg(char* pErrBuf, int32_t len, int32_t errCode, const char* pFormat, ...) {
  va_list vArgList;
  va_start(vArgList, pFormat);
  vsnprintf(pErrBuf, len, pFormat, vArgList);
  va_end(vArgList);
  return errCode;
}

static int32_t invaildFuncParaNumErrMsg(char* pErrBuf, int32_t len, const char* pFuncName) {
33
  return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_NUM, "Invalid number of parameters : %s", pFuncName);
34 35 36
}

static int32_t invaildFuncParaTypeErrMsg(char* pErrBuf, int32_t len, const char* pFuncName) {
37
  return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_TYPE, "Invalid parameter data type : %s", pFuncName);
38 39
}

40 41
static int32_t invaildFuncParaValueErrMsg(char* pErrBuf, int32_t len, const char* pFuncName) {
  return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_VALUE, "Invalid parameter value : %s", pFuncName);
42 43
}

44
#define TIME_UNIT_INVALID   1
45 46 47 48 49 50 51
#define TIME_UNIT_TOO_SMALL 2

static int32_t validateTimeUnitParam(uint8_t dbPrec, const SValueNode* pVal) {
  if (!pVal->isDuration) {
    return TIME_UNIT_INVALID;
  }

X
Xiaoyu Wang 已提交
52 53
  if (TSDB_TIME_PRECISION_MILLI == dbPrec &&
      (0 == strcasecmp(pVal->literal, "1u") || 0 == strcasecmp(pVal->literal, "1b"))) {
54 55 56 57
    return TIME_UNIT_TOO_SMALL;
  }

  if (TSDB_TIME_PRECISION_MICRO == dbPrec && 0 == strcasecmp(pVal->literal, "1b")) {
58 59 60
    return TIME_UNIT_TOO_SMALL;
  }

X
Xiaoyu Wang 已提交
61 62 63
  if (pVal->literal[0] != '1' ||
      (pVal->literal[1] != 'u' && pVal->literal[1] != 'a' && pVal->literal[1] != 's' && pVal->literal[1] != 'm' &&
       pVal->literal[1] != 'h' && pVal->literal[1] != 'd' && pVal->literal[1] != 'w' && pVal->literal[1] != 'b')) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    return TIME_UNIT_INVALID;
  }

  return TSDB_CODE_SUCCESS;
}

/* Following are valid ISO-8601 timezone format:
 * 1 z/Z
 * 2 ±hh:mm
 * 3 ±hhmm
 * 4 ±hh
 *
 */

static bool validateHourRange(int8_t hour) {
  if (hour < 0 || hour > 12) {
    return false;
  }

  return true;
}

static bool validateMinuteRange(int8_t hour, int8_t minute, char sign) {
  if (minute == 0 || (minute == 30 && (hour == 3 || hour == 5) && sign == '+')) {
    return true;
  }

  return false;
}

static bool validateTimestampDigits(const SValueNode* pVal) {
  if (!IS_INTEGER_TYPE(pVal->node.resType.type)) {
    return false;
  }

  int64_t tsVal = pVal->datum.i;
  char    fraction[20] = {0};
  NUM_TO_STRING(pVal->node.resType.type, &tsVal, sizeof(fraction), fraction);
  int32_t tsDigits = (int32_t)strlen(fraction);

  if (tsDigits > TSDB_TIME_PRECISION_SEC_DIGITS) {
    if (tsDigits == TSDB_TIME_PRECISION_MILLI_DIGITS || tsDigits == TSDB_TIME_PRECISION_MICRO_DIGITS ||
        tsDigits == TSDB_TIME_PRECISION_NANO_DIGITS) {
      return true;
    } else {
      return false;
    }
  }

  return true;
}

static bool validateTimezoneFormat(const SValueNode* pVal) {
  if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) {
    return false;
  }

  char*   tz = varDataVal(pVal->datum.p);
  int32_t len = varDataLen(pVal->datum.p);

  char   buf[3] = {0};
  int8_t hour = -1, minute = -1;
  if (len == 0) {
    return false;
  } else if (len == 1 && (tz[0] == 'z' || tz[0] == 'Z')) {
    return true;
  } else if ((tz[0] == '+' || tz[0] == '-')) {
    switch (len) {
      case 3:
      case 5: {
        for (int32_t i = 1; i < len; ++i) {
          if (!isdigit(tz[i])) {
            return false;
          }

          if (i == 2) {
            memcpy(buf, &tz[i - 1], 2);
            hour = taosStr2Int8(buf, NULL, 10);
            if (!validateHourRange(hour)) {
              return false;
            }
          } else if (i == 4) {
            memcpy(buf, &tz[i - 1], 2);
            minute = taosStr2Int8(buf, NULL, 10);
            if (!validateMinuteRange(hour, minute, tz[0])) {
              return false;
            }
          }
        }
        break;
      }
      case 6: {
        for (int32_t i = 1; i < len; ++i) {
          if (i == 3) {
            if (tz[i] != ':') {
              return false;
            }
            continue;
          }

          if (!isdigit(tz[i])) {
            return false;
          }

          if (i == 2) {
            memcpy(buf, &tz[i - 1], 2);
            hour = taosStr2Int8(buf, NULL, 10);
            if (!validateHourRange(hour)) {
              return false;
            }
          } else if (i == 5) {
            memcpy(buf, &tz[i - 1], 2);
            minute = taosStr2Int8(buf, NULL, 10);
            if (!validateMinuteRange(hour, minute, tz[0])) {
              return false;
            }
          }
        }
        break;
      }
      default: {
        return false;
      }
    }
  } else {
    return false;
  }

  return true;
}

195 196 197 198 199 200 201
static int32_t countTrailingSpaces(const SValueNode* pVal, bool isLtrim) {
  int32_t numOfSpaces = 0;
  int32_t len = varDataLen(pVal->datum.p);
  char*   str = varDataVal(pVal->datum.p);

  int32_t startPos = isLtrim ? 0 : len - 1;
  int32_t step = isLtrim ? 1 : -1;
G
Ganlin Zhao 已提交
202
  for (int32_t i = startPos; i < len || i >= 0; i += step) {
203 204 205 206 207 208 209 210 211
    if (!isspace(str[i])) {
      break;
    }
    numOfSpaces++;
  }

  return numOfSpaces;
}

G
Ganlin Zhao 已提交
212
static int32_t addTimezoneParam(SNodeList* pList) {
213 214 215
  char      buf[6] = {0};
  time_t    t = taosTime(NULL);
  struct tm tmInfo;
216
  if (taosLocalTime(&t, &tmInfo, buf) != NULL) {
217 218
    strftime(buf, sizeof(buf), "%z", &tmInfo);
  }
219 220 221
  int32_t len = (int32_t)strlen(buf);

  SValueNode* pVal = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
G
Ganlin Zhao 已提交
222 223 224 225
  if (pVal == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

226 227 228 229 230 231 232 233 234 235 236
  pVal->literal = strndup(buf, len);
  pVal->isDuration = false;
  pVal->translate = true;
  pVal->node.resType.type = TSDB_DATA_TYPE_BINARY;
  pVal->node.resType.bytes = len + VARSTR_HEADER_SIZE;
  pVal->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  pVal->datum.p = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE + 1);
  varDataSetLen(pVal->datum.p, len);
  strncpy(varDataVal(pVal->datum.p), pVal->literal, len);

  nodesListAppend(pList, (SNode*)pVal);
G
Ganlin Zhao 已提交
237
  return TSDB_CODE_SUCCESS;
238 239
}

G
Ganlin Zhao 已提交
240
static int32_t addDbPrecisonParam(SNodeList** pList, uint8_t precision) {
241
  SValueNode* pVal = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
G
Ganlin Zhao 已提交
242 243 244 245
  if (pVal == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

246 247 248
  pVal->literal = NULL;
  pVal->isDuration = false;
  pVal->translate = true;
249
  pVal->notReserved = true;
250 251 252 253 254 255 256
  pVal->node.resType.type = TSDB_DATA_TYPE_TINYINT;
  pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TINYINT].bytes;
  pVal->node.resType.precision = precision;
  pVal->datum.i = (int64_t)precision;
  pVal->typeData = (int64_t)precision;

  nodesListMakeAppend(pList, (SNode*)pVal);
G
Ganlin Zhao 已提交
257
  return TSDB_CODE_SUCCESS;
258 259
}

260 261 262 263 264 265 266
// There is only one parameter of numeric type, and the return type is parameter type
static int32_t translateInOutNum(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
267
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
268
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
269 270
  } else if (IS_NULL_TYPE(paraType)) {
    paraType = TSDB_DATA_TYPE_BIGINT;
271 272
  }

X
Xiaoyu Wang 已提交
273
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[paraType].bytes, .type = paraType};
274 275 276 277 278 279 280 281 282 283
  return TSDB_CODE_SUCCESS;
}

// There is only one parameter of numeric type, and the return type is double type
static int32_t translateInNumOutDou(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
284
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
285 286 287
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
288
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
289 290 291 292 293 294 295 296 297 298 299
  return TSDB_CODE_SUCCESS;
}

// There are two parameters of numeric type, and the return type is double type
static int32_t translateIn2NumOutDou(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (2 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
300 301
  if ((!IS_NUMERIC_TYPE(para1Type) && !IS_NULL_TYPE(para1Type)) ||
      (!IS_NUMERIC_TYPE(para2Type) && !IS_NULL_TYPE(para2Type))) {
302 303 304
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
305
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
306 307 308 309 310 311 312 313 314 315
  return TSDB_CODE_SUCCESS;
}

// There is only one parameter of string type, and the return type is parameter type
static int32_t translateInOutStr(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  SExprNode* pPara1 = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0);
316
  if (!IS_STR_DATA_TYPE(pPara1->resType.type)) {
317 318 319
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
320
  pFunc->node.resType = (SDataType){.bytes = pPara1->resType.bytes, .type = pPara1->resType.type};
321 322 323
  return TSDB_CODE_SUCCESS;
}

324 325 326 327 328 329
static int32_t translateTrimStr(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isLtrim) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  SExprNode* pPara1 = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0);
330
  if (!IS_STR_DATA_TYPE(pPara1->resType.type)) {
331 332 333 334
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  int32_t numOfSpaces = 0;
335
  SNode*  pParamNode1 = nodesListGetNode(pFunc->pParameterList, 0);
336 337 338 339
  // for select trim functions with constant value from table,
  // need to set the proper result result schema bytes to avoid
  // trailing garbage characters
  if (nodeType(pParamNode1) == QUERY_NODE_VALUE) {
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    SValueNode* pValue = (SValueNode*)pParamNode1;
    numOfSpaces = countTrailingSpaces(pValue, isLtrim);
  }

  int32_t resBytes = pPara1->resType.bytes - numOfSpaces;
  pFunc->node.resType = (SDataType){.bytes = resBytes, .type = pPara1->resType.type};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateLtrim(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateTrimStr(pFunc, pErrBuf, len, true);
}

static int32_t translateRtrim(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateTrimStr(pFunc, pErrBuf, len, false);
}

357 358 359 360 361 362 363
static int32_t translateLogarithm(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (1 != numOfParams && 2 != numOfParams) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
364
  if (!IS_NUMERIC_TYPE(para1Type) && !IS_NULL_TYPE(para1Type)) {
365 366 367 368 369
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  if (2 == numOfParams) {
    uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
370
    if (!IS_NUMERIC_TYPE(para2Type) && !IS_NULL_TYPE(para2Type)) {
371 372 373 374 375 376 377 378
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
  }

  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  return TSDB_CODE_SUCCESS;
}

379 380 381 382
static int32_t translateCount(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }
G
Ganlin Zhao 已提交
383
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
384 385 386 387 388 389 390 391 392
  return TSDB_CODE_SUCCESS;
}

static int32_t translateSum(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
393
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
394 395 396 397
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t resType = 0;
398
  if (IS_SIGNED_NUMERIC_TYPE(paraType) || TSDB_DATA_TYPE_BOOL == paraType || IS_NULL_TYPE(paraType)) {
399 400 401 402 403 404
    resType = TSDB_DATA_TYPE_BIGINT;
  } else if (IS_UNSIGNED_NUMERIC_TYPE(paraType)) {
    resType = TSDB_DATA_TYPE_UBIGINT;
  } else if (IS_FLOAT_TYPE(paraType)) {
    resType = TSDB_DATA_TYPE_DOUBLE;
  }
405

X
Xiaoyu Wang 已提交
406
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[resType].bytes, .type = resType};
407 408 409
  return TSDB_CODE_SUCCESS;
}

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
static int32_t translateAvgPartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pFunc->node.resType = (SDataType){.bytes = getAvgInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateAvgMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (TSDB_DATA_TYPE_BINARY != paraType) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};

  return TSDB_CODE_SUCCESS;
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static int32_t translateStddevPartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pFunc->node.resType = (SDataType){.bytes = getStddevInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateStddevMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (TSDB_DATA_TYPE_BINARY != paraType) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};

  return TSDB_CODE_SUCCESS;
}

468 469
static int32_t translateWduration(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  // pseudo column do not need to check parameters
470
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
471 472 473
  return TSDB_CODE_SUCCESS;
}

474
static int32_t translateNowToday(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
475
  // pseudo column do not need to check parameters
476

477
  // add database precision as param
478
  uint8_t dbPrec = pFunc->node.resType.precision;
479
  int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec);
G
Ganlin Zhao 已提交
480 481 482
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
483

484 485
  pFunc->node.resType =
      (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes, .type = TSDB_DATA_TYPE_TIMESTAMP};
486 487 488 489 490
  return TSDB_CODE_SUCCESS;
}

static int32_t translateTimePseudoColumn(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  // pseudo column do not need to check parameters
491

492 493
  pFunc->node.resType =
      (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes, .type = TSDB_DATA_TYPE_TIMESTAMP};
494 495 496 497 498 499 500
  return TSDB_CODE_SUCCESS;
}

static int32_t translateIsFilledPseudoColumn(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  // pseudo column do not need to check parameters

  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes, .type = TSDB_DATA_TYPE_BOOL};
501 502 503
  return TSDB_CODE_SUCCESS;
}

504
static int32_t translateTimezone(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
X
Xiaoyu Wang 已提交
505
  pFunc->node.resType = (SDataType){.bytes = TD_TIMEZONE_LEN, .type = TSDB_DATA_TYPE_BINARY};
506 507 508
  return TSDB_CODE_SUCCESS;
}

509
static int32_t translatePercentile(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
510
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
G
Ganlin Zhao 已提交
511
  if (numOfParams < 2 || numOfParams > 11) {
512 513 514
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

515 516 517
  uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (!IS_NUMERIC_TYPE(para1Type)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
518 519
  }

520 521 522 523 524
  for (int32_t i = 1; i < numOfParams; ++i) {
    SValueNode* pValue = (SValueNode*)nodesListGetNode(pFunc->pParameterList, i);
    pValue->notReserved = true;

    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
G
Ganlin Zhao 已提交
525
    if (!IS_NUMERIC_TYPE(paraType)) {
526 527
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
G
Ganlin Zhao 已提交
528 529 530 531 532 533 534 535 536 537 538

    double v = 0;
    if (IS_INTEGER_TYPE(paraType)) {
      v = (double)pValue->datum.i;
    } else {
      v = pValue->datum.d;
    }

    if (v < 0 || v > 100) {
      return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
    }
539 540
  }

X
Xiaoyu Wang 已提交
541
  // set result type
542
  if (numOfParams > 2) {
G
Ganlin Zhao 已提交
543
    pFunc->node.resType = (SDataType){.bytes = 512, .type = TSDB_DATA_TYPE_VARCHAR};
544 545 546
  } else {
    pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  }
547 548 549
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
550
static bool validateApercentileAlgo(const SValueNode* pVal) {
551 552 553
  if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) {
    return false;
  }
X
Xiaoyu Wang 已提交
554 555
  return (0 == strcasecmp(varDataVal(pVal->datum.p), "default") ||
          0 == strcasecmp(varDataVal(pVal->datum.p), "t-digest"));
556 557
}

558
static int32_t translateApercentile(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
559 560
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (2 != numOfParams && 3 != numOfParams) {
561 562 563
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
564
  // param1
565 566
  SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
  if (nodeType(pParamNode1) != QUERY_NODE_VALUE) {
D
dapan1121 已提交
567 568
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }
X
Xiaoyu Wang 已提交
569

570
  SValueNode* pValue = (SValueNode*)pParamNode1;
D
dapan1121 已提交
571 572 573 574 575
  if (pValue->datum.i < 0 || pValue->datum.i > 100) {
    return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pValue->notReserved = true;
X
Xiaoyu Wang 已提交
576

577 578 579 580 581 582
  uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
  if (!IS_NUMERIC_TYPE(para1Type) || !IS_INTEGER_TYPE(para2Type)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
583
  // param2
584
  if (3 == numOfParams) {
585
    uint8_t para3Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type;
586
    if (!IS_STR_DATA_TYPE(para3Type)) {
587 588 589 590
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SNode* pParamNode2 = nodesListGetNode(pFunc->pParameterList, 2);
G
Ganlin Zhao 已提交
591
    if (QUERY_NODE_VALUE != nodeType(pParamNode2) || !validateApercentileAlgo((SValueNode*)pParamNode2)) {
X
Xiaoyu Wang 已提交
592 593
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "Third parameter algorithm of apercentile must be 'default' or 't-digest'");
594
    }
595 596 597

    pValue = (SValueNode*)pParamNode2;
    pValue->notReserved = true;
598 599
  }

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateApercentileImpl(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isPartial) {
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);

  if (isPartial) {
    if (2 != numOfParams && 3 != numOfParams) {
      return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
    }
    // param1
    SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
    if (nodeType(pParamNode1) != QUERY_NODE_VALUE) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)pParamNode1;
    if (pValue->datum.i < 0 || pValue->datum.i > 100) {
      return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
    }

    pValue->notReserved = true;

    uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
    uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
    if (!IS_NUMERIC_TYPE(para1Type) || !IS_INTEGER_TYPE(para2Type)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    // param2
    if (3 == numOfParams) {
      uint8_t para3Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type;
633
      if (!IS_STR_DATA_TYPE(para3Type)) {
634 635 636 637 638 639 640 641 642 643 644 645 646
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }

      SNode* pParamNode2 = nodesListGetNode(pFunc->pParameterList, 2);
      if (QUERY_NODE_VALUE != nodeType(pParamNode2) || !validateApercentileAlgo((SValueNode*)pParamNode2)) {
        return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                               "Third parameter algorithm of apercentile must be 'default' or 't-digest'");
      }

      pValue = (SValueNode*)pParamNode2;
      pValue->notReserved = true;
    }

X
Xiaoyu Wang 已提交
647 648
    pFunc->node.resType =
        (SDataType){.bytes = getApercentileMaxSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
649
  } else {
650 651
    // original percent param is reserved
    if (2 != numOfParams) {
652 653 654
      return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
    }
    uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
655 656
    uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
    if (TSDB_DATA_TYPE_BINARY != para1Type || !IS_INTEGER_TYPE(para2Type)) {
657 658 659 660
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
661
  }
662

663 664 665
  return TSDB_CODE_SUCCESS;
}

666 667 668
static int32_t translateApercentilePartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateApercentileImpl(pFunc, pErrBuf, len, true);
}
669

670
static int32_t translateApercentileMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
671 672 673
  return translateApercentileImpl(pFunc, pErrBuf, len, false);
}

674 675
static int32_t translateTbnameColumn(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  // pseudo column do not need to check parameters
X
Xiaoyu Wang 已提交
676 677
  pFunc->node.resType =
      (SDataType){.bytes = TSDB_TABLE_FNAME_LEN - 1 + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_VARCHAR};
678 679 680
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
681
static int32_t translateTopBot(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
682 683
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (2 != numOfParams) {
684 685 686
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

687 688 689
  uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
  if (!IS_NUMERIC_TYPE(para1Type) || !IS_INTEGER_TYPE(para2Type)) {
690 691 692
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
693
  // param1
694 695 696 697 698 699
  SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
  if (nodeType(pParamNode1) != QUERY_NODE_VALUE) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  SValueNode* pValue = (SValueNode*)pParamNode1;
700
  if (!IS_INTEGER_TYPE(pValue->node.resType.type)) {
701 702 703
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

H
Haojun Liao 已提交
704
  if (pValue->datum.i < 1 || pValue->datum.i > TOP_BOTTOM_QUERY_LIMIT) {
705 706 707
    return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
  }

708 709
  pValue->notReserved = true;

X
Xiaoyu Wang 已提交
710
  // set result type
711 712
  SDataType* pType = &((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType;
  pFunc->node.resType = (SDataType){.bytes = pType->bytes, .type = pType->type};
713 714 715
  return TSDB_CODE_SUCCESS;
}

716
static int32_t reserveFirstMergeParam(SNodeList* pRawParameters, SNode* pPartialRes, SNodeList** pParameters) {
717 718 719 720 721 722 723
  int32_t code = nodesListMakeAppend(pParameters, pPartialRes);
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesListStrictAppend(*pParameters, nodesCloneNode(nodesListGetNode(pRawParameters, 1)));
  }
  return TSDB_CODE_SUCCESS;
}

724 725 726 727 728 729 730 731
int32_t topBotCreateMergeParam(SNodeList* pRawParameters, SNode* pPartialRes, SNodeList** pParameters) {
  return reserveFirstMergeParam(pRawParameters, pPartialRes, pParameters);
}

int32_t apercentileCreateMergeParam(SNodeList* pRawParameters, SNode* pPartialRes, SNodeList** pParameters) {
  return reserveFirstMergeParam(pRawParameters, pPartialRes, pParameters);
}

732
static int32_t translateSpread(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
G
Ganlin Zhao 已提交
733 734 735 736 737
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
738
  if (!IS_NUMERIC_TYPE(paraType) && !IS_TIMESTAMP_TYPE(paraType)) {
G
Ganlin Zhao 已提交
739 740 741
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
742
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
743 744 745
  return TSDB_CODE_SUCCESS;
}

746 747 748 749 750 751 752
static int32_t translateSpreadImpl(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isPartial) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (isPartial) {
753
    if (!IS_NUMERIC_TYPE(paraType) && !IS_TIMESTAMP_TYPE(paraType)) {
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
    pFunc->node.resType = (SDataType){.bytes = getSpreadInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
  } else {
    if (TSDB_DATA_TYPE_BINARY != paraType) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
    pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t translateSpreadPartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateSpreadImpl(pFunc, pErrBuf, len, true);
}
770

771 772 773 774
static int32_t translateSpreadMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateSpreadImpl(pFunc, pErrBuf, len, false);
}

G
Ganlin Zhao 已提交
775
static int32_t translateElapsed(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
776 777
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (1 != numOfParams && 2 != numOfParams) {
G
Ganlin Zhao 已提交
778 779 780
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
781 782 783 784
  SNode* pPara1 = nodesListGetNode(pFunc->pParameterList, 0);
  if (QUERY_NODE_COLUMN != nodeType(pPara1) || PRIMARYKEY_TIMESTAMP_COL_ID != ((SColumnNode*)pPara1)->colId) {
    return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                           "The first parameter of the ELAPSED function can only be the timestamp primary key");
G
Ganlin Zhao 已提交
785 786
  }

X
Xiaoyu Wang 已提交
787
  // param1
788 789 790 791 792 793 794 795 796 797
  if (2 == numOfParams) {
    SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
    if (QUERY_NODE_VALUE != nodeType(pParamNode1)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)pParamNode1;

    pValue->notReserved = true;

X
Xiaoyu Wang 已提交
798
    if (!IS_INTEGER_TYPE(pValue->node.resType.type)) {
799 800
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
801

802 803
    uint8_t dbPrec = pFunc->node.resType.precision;

804
    int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1));
805
    if (ret == TIME_UNIT_TOO_SMALL) {
806 807
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "ELAPSED function time unit parameter should be greater than db precision");
808
    } else if (ret == TIME_UNIT_INVALID) {
X
Xiaoyu Wang 已提交
809 810 811
      return buildFuncErrMsg(
          pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
          "ELAPSED function time unit parameter should be one of the following: [1b, 1u, 1a, 1s, 1m, 1h, 1d, 1w]");
812
    }
813 814
  }

G
Ganlin Zhao 已提交
815 816 817 818
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  return TSDB_CODE_SUCCESS;
}

819 820 821 822 823 824 825 826 827
static int32_t translateElapsedImpl(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isPartial) {
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);

  if (isPartial) {
    if (1 != numOfParams && 2 != numOfParams) {
      return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
    }

    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
828
    if (!IS_TIMESTAMP_TYPE(paraType)) {
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    // param1
    if (2 == numOfParams) {
      SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
      if (QUERY_NODE_VALUE != nodeType(pParamNode1)) {
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }

      SValueNode* pValue = (SValueNode*)pParamNode1;

      pValue->notReserved = true;

      paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
      if (!IS_INTEGER_TYPE(paraType)) {
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }

      if (pValue->datum.i == 0) {
        return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                               "ELAPSED function time unit parameter should be greater than db precision");
      }
    }

X
Xiaoyu Wang 已提交
854 855
    pFunc->node.resType =
        (SDataType){.bytes = getElapsedInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
  } else {
    if (1 != numOfParams) {
      return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
    }

    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
    if (TSDB_DATA_TYPE_BINARY != paraType) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
    pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t translateElapsedPartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
871
#if 0
872
  return translateElapsedImpl(pFunc, pErrBuf, len, true);
873 874
#endif
  return 0;
875 876 877
}

static int32_t translateElapsedMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
878
#if 0
879
  return translateElapsedImpl(pFunc, pErrBuf, len, false);
880 881
#endif
  return 0;
882 883
}

884 885 886 887 888 889 890
static int32_t translateLeastSQR(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (3 != numOfParams) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  for (int32_t i = 0; i < numOfParams; ++i) {
891
    SNode* pParamNode = nodesListGetNode(pFunc->pParameterList, i);
X
Xiaoyu Wang 已提交
892
    if (i > 0) {  // param1 & param2
893 894 895 896 897 898 899 900 901
      if (QUERY_NODE_VALUE != nodeType(pParamNode)) {
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }

      SValueNode* pValue = (SValueNode*)pParamNode;

      pValue->notReserved = true;
    }

902 903 904 905 906 907
    uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
    if (!IS_NUMERIC_TYPE(colType)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
  }

X
Xiaoyu Wang 已提交
908
  pFunc->node.resType = (SDataType){.bytes = 64, .type = TSDB_DATA_TYPE_BINARY};
909 910 911
  return TSDB_CODE_SUCCESS;
}

912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
typedef enum { UNKNOWN_BIN = 0, USER_INPUT_BIN, LINEAR_BIN, LOG_BIN } EHistoBinType;

static int8_t validateHistogramBinType(char* binTypeStr) {
  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;
}

static bool validateHistogramBinDesc(char* binDescStr, int8_t binType, char* errMsg, int32_t msgLen) {
930 931 932 933 934 935 936
  const char* msg1 = "HISTOGRAM function requires four parameters";
  const char* msg3 = "HISTOGRAM function invalid format for binDesc parameter";
  const char* msg4 = "HISTOGRAM function binDesc parameter \"count\" should be in range [1, 1000]";
  const char* msg5 = "HISTOGRAM function bin/parameter should be in range [-DBL_MAX, DBL_MAX]";
  const char* msg6 = "HISTOGRAM function binDesc parameter \"width\" cannot be 0";
  const char* msg7 = "HISTOGRAM function binDesc parameter \"start\" cannot be 0 with \"log_bin\" type";
  const char* msg8 = "HISTOGRAM function binDesc parameter \"factor\" cannot be negative or equal to 0/1";
937 938 939 940 941 942 943 944 945

  cJSON*  binDesc = cJSON_Parse(binDescStr);
  int32_t numOfBins;
  double* intervals;
  if (cJSON_IsObject(binDesc)) { /* linaer/log bins */
    int32_t numOfParams = cJSON_GetArraySize(binDesc);
    int32_t startIndex;
    if (numOfParams != 4) {
      snprintf(errMsg, msgLen, "%s", msg1);
946
      cJSON_Delete(binDesc);
947 948 949 950 951 952 953 954 955 956 957
      return false;
    }

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

    if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
      snprintf(errMsg, msgLen, "%s", msg3);
958
      cJSON_Delete(binDesc);
959 960 961 962 963
      return false;
    }

    if (count->valueint <= 0 || count->valueint > 1000) {  // limit count to 1000
      snprintf(errMsg, msgLen, "%s", msg4);
964
      cJSON_Delete(binDesc);
965 966 967 968 969 970
      return false;
    }

    if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) ||
        (factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) {
      snprintf(errMsg, msgLen, "%s", msg5);
971
      cJSON_Delete(binDesc);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
      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) {
        snprintf(errMsg, msgLen, "%s", msg6);
        taosMemoryFree(intervals);
990
        cJSON_Delete(binDesc);
991 992 993 994 995 996 997
        return false;
      }
      for (int i = 0; i < counter + 1; ++i) {
        intervals[startIndex] = start->valuedouble + i * width->valuedouble;
        if (isinf(intervals[startIndex])) {
          snprintf(errMsg, msgLen, "%s", msg5);
          taosMemoryFree(intervals);
998
          cJSON_Delete(binDesc);
999 1000 1001 1002 1003 1004 1005 1006 1007
          return false;
        }
        startIndex++;
      }
    } else if (cJSON_IsNumber(factor) && width == NULL && binType == LOG_BIN) {
      // log bin process
      if (start->valuedouble == 0) {
        snprintf(errMsg, msgLen, "%s", msg7);
        taosMemoryFree(intervals);
1008
        cJSON_Delete(binDesc);
1009 1010 1011 1012 1013
        return false;
      }
      if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) {
        snprintf(errMsg, msgLen, "%s", msg8);
        taosMemoryFree(intervals);
1014
        cJSON_Delete(binDesc);
1015 1016 1017 1018 1019 1020 1021
        return false;
      }
      for (int i = 0; i < counter + 1; ++i) {
        intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0);
        if (isinf(intervals[startIndex])) {
          snprintf(errMsg, msgLen, "%s", msg5);
          taosMemoryFree(intervals);
1022
          cJSON_Delete(binDesc);
1023 1024 1025 1026 1027 1028 1029
          return false;
        }
        startIndex++;
      }
    } else {
      snprintf(errMsg, msgLen, "%s", msg3);
      taosMemoryFree(intervals);
1030
      cJSON_Delete(binDesc);
1031 1032 1033 1034 1035 1036 1037
      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 已提交
1038 1039 1040 1041
      if (numOfBins < 4) {
        return false;
      }

1042 1043 1044 1045 1046 1047 1048
      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) {
      snprintf(errMsg, msgLen, "%s", msg3);
1049
      cJSON_Delete(binDesc);
1050 1051 1052 1053 1054 1055 1056 1057
      return false;
    }
    numOfBins = cJSON_GetArraySize(binDesc);
    intervals = taosMemoryCalloc(numOfBins, sizeof(double));
    cJSON* bin = binDesc->child;
    if (bin == NULL) {
      snprintf(errMsg, msgLen, "%s", msg3);
      taosMemoryFree(intervals);
1058
      cJSON_Delete(binDesc);
1059 1060 1061 1062 1063 1064 1065 1066
      return false;
    }
    int i = 0;
    while (bin) {
      intervals[i] = bin->valuedouble;
      if (!cJSON_IsNumber(bin)) {
        snprintf(errMsg, msgLen, "%s", msg3);
        taosMemoryFree(intervals);
1067
        cJSON_Delete(binDesc);
1068 1069 1070 1071 1072
        return false;
      }
      if (i != 0 && intervals[i] <= intervals[i - 1]) {
        snprintf(errMsg, msgLen, "%s", msg3);
        taosMemoryFree(intervals);
1073
        cJSON_Delete(binDesc);
1074 1075 1076 1077 1078 1079 1080
        return false;
      }
      bin = bin->next;
      i++;
    }
  } else {
    snprintf(errMsg, msgLen, "%s", msg3);
1081
    cJSON_Delete(binDesc);
1082 1083 1084
    return false;
  }

1085
  cJSON_Delete(binDesc);
1086 1087 1088 1089
  taosMemoryFree(intervals);
  return true;
}

1090
static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1091 1092
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (4 != numOfParams) {
1093 1094 1095 1096 1097 1098 1099 1100
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (!IS_NUMERIC_TYPE(colType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
1101
  // param1 ~ param3
1102 1103
  if (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type != TSDB_DATA_TYPE_BINARY ||
      ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_BINARY ||
1104
      !IS_INTEGER_TYPE(((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type)) {
1105 1106 1107
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1108 1109
  int8_t binType;
  char*  binDesc;
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  for (int32_t i = 1; i < numOfParams; ++i) {
    SNode* pParamNode = nodesListGetNode(pFunc->pParameterList, i);
    if (QUERY_NODE_VALUE != nodeType(pParamNode)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)pParamNode;

    pValue->notReserved = true;

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    if (i == 1) {
      binType = validateHistogramBinType(varDataVal(pValue->datum.p));
      if (binType == UNKNOWN_BIN) {
        return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                               "HISTOGRAM function binType parameter should be "
                               "\"user_input\", \"log_bin\" or \"linear_bin\"");
      }
    }

    if (i == 2) {
G
Ganlin Zhao 已提交
1130
      char errMsg[128] = {0};
1131 1132 1133 1134 1135 1136
      binDesc = varDataVal(pValue->datum.p);
      if (!validateHistogramBinDesc(binDesc, binType, errMsg, (int32_t)sizeof(errMsg))) {
        return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, errMsg);
      }
    }

1137
    if (i == 3 && pValue->datum.i != 1 && pValue->datum.i != 0) {
1138 1139
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "HISTOGRAM function normalized parameter should be 0/1");
1140
    }
1141 1142
  }

X
Xiaoyu Wang 已提交
1143
  pFunc->node.resType = (SDataType){.bytes = 512, .type = TSDB_DATA_TYPE_BINARY};
1144 1145 1146
  return TSDB_CODE_SUCCESS;
}

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
static int32_t translateHistogramImpl(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isPartial) {
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (isPartial) {
    if (4 != numOfParams) {
      return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
    }

    uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
    if (!IS_NUMERIC_TYPE(colType)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    // param1 ~ param3
1160 1161
    if (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type != TSDB_DATA_TYPE_BINARY ||
        ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_BINARY ||
1162
        !IS_INTEGER_TYPE(((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type)) {
1163 1164 1165
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

1166 1167
    int8_t binType;
    char*  binDesc;
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    for (int32_t i = 1; i < numOfParams; ++i) {
      SNode* pParamNode = nodesListGetNode(pFunc->pParameterList, i);
      if (QUERY_NODE_VALUE != nodeType(pParamNode)) {
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }

      SValueNode* pValue = (SValueNode*)pParamNode;

      pValue->notReserved = true;

1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
      if (i == 1) {
        binType = validateHistogramBinType(varDataVal(pValue->datum.p));
        if (binType == UNKNOWN_BIN) {
          return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                                 "HISTOGRAM function binType parameter should be "
                                 "\"user_input\", \"log_bin\" or \"linear_bin\"");
        }
      }

      if (i == 2) {
        char errMsg[128] = {0};
        binDesc = varDataVal(pValue->datum.p);
        if (!validateHistogramBinDesc(binDesc, binType, errMsg, (int32_t)sizeof(errMsg))) {
          return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, errMsg);
        }
      }

1195
      if (i == 3 && pValue->datum.i != 1 && pValue->datum.i != 0) {
1196 1197
        return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                               "HISTOGRAM function normalized parameter should be 0/1");
1198
      }
1199 1200
    }

X
Xiaoyu Wang 已提交
1201 1202
    pFunc->node.resType =
        (SDataType){.bytes = getHistogramInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  } else {
    if (1 != numOfParams) {
      return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
    }

    if (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type != TSDB_DATA_TYPE_BINARY) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    pFunc->node.resType = (SDataType){.bytes = 512, .type = TSDB_DATA_TYPE_BINARY};
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t translateHistogramPartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateHistogramImpl(pFunc, pErrBuf, len, true);
}
1220

1221 1222 1223 1224
static int32_t translateHistogramMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateHistogramImpl(pFunc, pErrBuf, len, false);
}

G
Ganlin Zhao 已提交
1225 1226 1227 1228 1229
static int32_t translateHLL(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1230
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
G
Ganlin Zhao 已提交
1231 1232 1233
  return TSDB_CODE_SUCCESS;
}

1234 1235 1236 1237 1238 1239
static int32_t translateHLLImpl(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isPartial) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  if (isPartial) {
X
Xiaoyu Wang 已提交
1240 1241
    pFunc->node.resType =
        (SDataType){.bytes = getHistogramInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
  } else {
    pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t translateHLLPartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateHLLImpl(pFunc, pErrBuf, len, true);
}

static int32_t translateHLLMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateHLLImpl(pFunc, pErrBuf, len, false);
}

1257 1258 1259 1260
static bool validateStateOper(const SValueNode* pVal) {
  if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) {
    return false;
  }
1261 1262 1263 1264
  return (
      0 == strncasecmp(varDataVal(pVal->datum.p), "GT", 2) || 0 == strncasecmp(varDataVal(pVal->datum.p), "GE", 2) ||
      0 == strncasecmp(varDataVal(pVal->datum.p), "LT", 2) || 0 == strncasecmp(varDataVal(pVal->datum.p), "LE", 2) ||
      0 == strncasecmp(varDataVal(pVal->datum.p), "EQ", 2) || 0 == strncasecmp(varDataVal(pVal->datum.p), "NE", 2));
1265 1266
}

1267
static int32_t translateStateCount(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1268 1269
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (3 != numOfParams) {
1270 1271 1272 1273 1274 1275 1276 1277
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (!IS_NUMERIC_TYPE(colType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
1278
  // param1 & param2
1279 1280 1281 1282 1283 1284 1285 1286
  for (int32_t i = 1; i < numOfParams; ++i) {
    SNode* pParamNode = nodesListGetNode(pFunc->pParameterList, i);
    if (QUERY_NODE_VALUE != nodeType(pParamNode)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)pParamNode;

1287 1288
    if (i == 1 && !validateStateOper(pValue)) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
1289
                             "Second parameter of STATECOUNT function"
1290 1291 1292
                             "must be one of the following: 'GE', 'GT', 'LE', 'LT', 'EQ', 'NE'");
    }

1293 1294 1295
    pValue->notReserved = true;
  }

1296 1297 1298 1299 1300 1301
  if (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type != TSDB_DATA_TYPE_BINARY ||
      (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_BIGINT &&
       ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_DOUBLE)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
1302
  // set result type
1303
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
1304 1305 1306
  return TSDB_CODE_SUCCESS;
}

1307
static int32_t translateStateDuration(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1308 1309
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (3 != numOfParams && 4 != numOfParams) {
1310 1311 1312 1313 1314 1315 1316 1317
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (!IS_NUMERIC_TYPE(colType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
1318
  // param1, param2 & param3
1319 1320 1321 1322 1323 1324 1325 1326
  for (int32_t i = 1; i < numOfParams; ++i) {
    SNode* pParamNode = nodesListGetNode(pFunc->pParameterList, i);
    if (QUERY_NODE_VALUE != nodeType(pParamNode)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)pParamNode;

1327 1328
    if (i == 1 && !validateStateOper(pValue)) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
1329
                             "Second parameter of STATEDURATION function"
1330
                             "must be one of the following: 'GE', 'GT', 'LE', 'LT', 'EQ', 'NE'");
1331 1332 1333
    } else if (i == 3 && pValue->datum.i == 0) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "STATEDURATION function time unit parameter should be greater than db precision");
1334 1335
    }

1336 1337 1338
    pValue->notReserved = true;
  }

1339 1340 1341 1342 1343 1344
  if (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type != TSDB_DATA_TYPE_BINARY ||
      (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_BIGINT &&
       ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_DOUBLE)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
1345 1346
  if (numOfParams == 4 &&
      ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type != TSDB_DATA_TYPE_BIGINT) {
1347 1348 1349
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1350 1351 1352
  if (numOfParams == 4) {
    uint8_t dbPrec = pFunc->node.resType.precision;

1353
    int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 3));
1354 1355 1356 1357 1358
    if (ret == TIME_UNIT_TOO_SMALL) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "STATEDURATION function time unit parameter should be greater than db precision");
    } else if (ret == TIME_UNIT_INVALID) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
X
Xiaoyu Wang 已提交
1359 1360
                             "STATEDURATION function time unit parameter should be one of the following: [1b, 1u, 1a, "
                             "1s, 1m, 1h, 1d, 1w]");
1361 1362 1363
    }
  }

X
Xiaoyu Wang 已提交
1364
  // set result type
1365
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
1366 1367 1368
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1369 1370
static int32_t translateCsum(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
1371
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
G
Ganlin Zhao 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
  }

  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  uint8_t resType;
  if (!IS_NUMERIC_TYPE(colType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  } else {
    if (IS_SIGNED_NUMERIC_TYPE(colType)) {
      resType = TSDB_DATA_TYPE_BIGINT;
    } else if (IS_UNSIGNED_NUMERIC_TYPE(colType)) {
      resType = TSDB_DATA_TYPE_UBIGINT;
    } else if (IS_FLOAT_TYPE(colType)) {
      resType = TSDB_DATA_TYPE_DOUBLE;
    } else {
G
Ganlin Zhao 已提交
1386
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
G
Ganlin Zhao 已提交
1387 1388 1389
    }
  }

1390
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[resType].bytes, .type = resType};
G
Ganlin Zhao 已提交
1391 1392 1393
  return TSDB_CODE_SUCCESS;
}

1394 1395
static EFuncReturnRows csumEstReturnRows(SFunctionNode* pFunc) { return FUNC_RETURN_ROWS_N; }

G
Ganlin Zhao 已提交
1396 1397 1398 1399 1400 1401
static int32_t translateMavg(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (2 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
1402

X
Xiaoyu Wang 已提交
1403
  // param1
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
  SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
  if (QUERY_NODE_VALUE != nodeType(pParamNode1)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  SValueNode* pValue = (SValueNode*)pParamNode1;
  if (pValue->datum.i < 1 || pValue->datum.i > 1000) {
    return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pValue->notReserved = true;

G
Ganlin Zhao 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424
  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
  if (!IS_NUMERIC_TYPE(colType) || !IS_INTEGER_TYPE(paraType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1425 1426 1427 1428 1429
static int32_t translateSample(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (2 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1430 1431 1432
  SExprNode* pCol = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0);
  uint8_t    colType = pCol->resType.type;

X
Xiaoyu Wang 已提交
1433
  // param1
1434 1435 1436 1437 1438 1439 1440 1441
  SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
  if (QUERY_NODE_VALUE != nodeType(pParamNode1)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  SValueNode* pValue = (SValueNode*)pParamNode1;
  if (pValue->datum.i < 1 || pValue->datum.i > 1000) {
    return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
G
Ganlin Zhao 已提交
1442 1443
  }

1444 1445
  pValue->notReserved = true;

G
Ganlin Zhao 已提交
1446 1447 1448 1449 1450
  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
  if (!IS_INTEGER_TYPE(paraType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
1451
  // set result type
1452
  if (IS_STR_DATA_TYPE(colType)) {
G
Ganlin Zhao 已提交
1453 1454 1455 1456
    pFunc->node.resType = (SDataType){.bytes = pCol->resType.bytes, .type = colType};
  } else {
    pFunc->node.resType = (SDataType){.bytes = tDataTypes[colType].bytes, .type = colType};
  }
1457

G
Ganlin Zhao 已提交
1458 1459 1460
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1461
static int32_t translateTail(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1462 1463
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (2 != numOfParams && 3 != numOfParams) {
G
Ganlin Zhao 已提交
1464 1465 1466
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1467 1468
  SExprNode* pCol = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0);
  uint8_t    colType = pCol->resType.type;
G
Ganlin Zhao 已提交
1469

X
Xiaoyu Wang 已提交
1470
  // param1 & param2
1471
  for (int32_t i = 1; i < numOfParams; ++i) {
1472 1473 1474 1475 1476 1477 1478
    SNode* pParamNode = nodesListGetNode(pFunc->pParameterList, i);
    if (QUERY_NODE_VALUE != nodeType(pParamNode)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)pParamNode;

1479 1480
    if ((IS_SIGNED_NUMERIC_TYPE(pValue->node.resType.type) ? pValue->datum.i : pValue->datum.u) < ((i > 1) ? 0 : 1) ||
        (IS_SIGNED_NUMERIC_TYPE(pValue->node.resType.type) ? pValue->datum.i : pValue->datum.u) > 100) {
1481
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
X
Xiaoyu Wang 已提交
1482 1483
                             "TAIL function second parameter should be in range [1, 100], "
                             "third parameter should be in range [0, 100]");
1484 1485 1486 1487
    }

    pValue->notReserved = true;

1488 1489 1490 1491
    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
    if (!IS_INTEGER_TYPE(paraType)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
G
Ganlin Zhao 已提交
1492 1493
  }

X
Xiaoyu Wang 已提交
1494
  // set result type
1495
  if (IS_STR_DATA_TYPE(colType)) {
G
Ganlin Zhao 已提交
1496 1497 1498 1499 1500 1501 1502
    pFunc->node.resType = (SDataType){.bytes = pCol->resType.bytes, .type = colType};
  } else {
    pFunc->node.resType = (SDataType){.bytes = tDataTypes[colType].bytes, .type = colType};
  }
  return TSDB_CODE_SUCCESS;
}

1503 1504 1505 1506 1507 1508 1509 1510
static int32_t translateDerivative(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (3 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;

  // param1
1511 1512
  SNode*      pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
  SValueNode* pValue1 = (SValueNode*)pParamNode1;
1513 1514 1515 1516
  if (QUERY_NODE_VALUE != nodeType(pParamNode1)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1517 1518
  if (pValue1->datum.i <= 0) {
    return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
1519 1520 1521 1522 1523 1524 1525 1526 1527
  }

  SValueNode* pValue = (SValueNode*)pParamNode1;
  pValue->notReserved = true;

  if (!IS_NUMERIC_TYPE(colType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1528
  SNode*      pParamNode2 = nodesListGetNode(pFunc->pParameterList, 2);
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
  SValueNode* pValue2 = (SValueNode*)pParamNode2;
  pValue2->notReserved = true;

  if (pValue2->datum.i != 0 && pValue2->datum.i != 1) {
    return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
  }

  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  return TSDB_CODE_SUCCESS;
}

1540 1541 1542 1543 1544
static EFuncReturnRows derivativeEstReturnRows(SFunctionNode* pFunc) {
  return 1 == ((SValueNode*)nodesListGetNode(pFunc->pParameterList, 2))->datum.i ? FUNC_RETURN_ROWS_INDEFINITE
                                                                                 : FUNC_RETURN_ROWS_N_MINUS_1;
}

G
Ganlin Zhao 已提交
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
static int32_t translateIrate(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;

  if (!IS_NUMERIC_TYPE(colType)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1556
  // add database precision as param
1557
  uint8_t dbPrec = pFunc->node.resType.precision;
1558
  int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec);
G
Ganlin Zhao 已提交
1559 1560 1561
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
1562

G
Ganlin Zhao 已提交
1563 1564 1565 1566
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1567 1568 1569 1570
static int32_t translateInterp(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  uint8_t dbPrec = pFunc->node.resType.precision;

1571
  if (2 < numOfParams) {
G
Ganlin Zhao 已提交
1572 1573 1574
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1575 1576
  uint8_t nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, 0));
  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
1577
  if ((!IS_NUMERIC_TYPE(paraType) && !IS_BOOLEAN_TYPE(paraType)) || QUERY_NODE_VALUE == nodeType) {
1578 1579 1580
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
  if (2 == numOfParams) {
    nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, 1));
    paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
    if (!IS_INTEGER_TYPE(paraType) || QUERY_NODE_VALUE != nodeType) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
    if (pValue->datum.i != 0 && pValue->datum.i != 1) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "INTERP function second parameter should be 0/1");
    }
G
Ganlin Zhao 已提交
1593 1594

    pValue->notReserved = true;
1595 1596
  }

G
Ganlin Zhao 已提交
1597
#if 0
G
Ganlin Zhao 已提交
1598 1599 1600
  if (3 <= numOfParams) {
    int64_t timeVal[2] = {0};
    for (int32_t i = 1; i < 3; ++i) {
1601 1602
      nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, i));
      paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
1603
      if (!IS_STR_DATA_TYPE(paraType) || QUERY_NODE_VALUE != nodeType) {
G
Ganlin Zhao 已提交
1604 1605 1606 1607
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }

      SValueNode* pValue = (SValueNode*)nodesListGetNode(pFunc->pParameterList, i);
1608
      int32_t     ret = convertStringToTimestamp(paraType, pValue->datum.p, dbPrec, &timeVal[i - 1]);
G
Ganlin Zhao 已提交
1609 1610 1611 1612 1613 1614
      if (ret != TSDB_CODE_SUCCESS) {
        return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
      }
    }

    if (timeVal[0] > timeVal[1]) {
1615
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, "INTERP function invalid time range");
G
Ganlin Zhao 已提交
1616 1617 1618 1619
    }
  }

  if (4 == numOfParams) {
1620 1621
    nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, 3));
    paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type;
G
Ganlin Zhao 已提交
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
    if (!IS_INTEGER_TYPE(paraType) || QUERY_NODE_VALUE != nodeType) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 3));
    if (ret == TIME_UNIT_TOO_SMALL) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "INTERP function time interval parameter should be greater than db precision");
    } else if (ret == TIME_UNIT_INVALID) {
      return buildFuncErrMsg(
          pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
          "INTERP function time interval parameter should be one of the following: [1b, 1u, 1a, 1s, 1m, 1h, 1d, 1w]");
    }
  }
G
Ganlin Zhao 已提交
1636
#endif
G
Ganlin Zhao 已提交
1637 1638 1639 1640 1641

  pFunc->node.resType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType;
  return TSDB_CODE_SUCCESS;
}

1642 1643 1644 1645 1646 1647 1648 1649 1650
static EFuncReturnRows interpEstReturnRows(SFunctionNode* pFunc) {
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (1 < numOfParams && 1 == ((SValueNode*)nodesListGetNode(pFunc->pParameterList, 1))->datum.i) {
    return FUNC_RETURN_ROWS_INDEFINITE;
  } else {
    return FUNC_RETURN_ROWS_N;
  }
}

1651
static int32_t translateFirstLast(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1652 1653 1654 1655
  // forbid null as first/last input, since first(c0, null, 1) may have different number of input
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);

  for (int32_t i = 0; i < numOfParams; ++i) {
1656
    uint8_t nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, i));
1657
    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
1658
    if (IS_NULL_TYPE(paraType) && QUERY_NODE_VALUE == nodeType) {
1659 1660 1661 1662
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
  }

1663
  pFunc->node.resType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType;
1664 1665 1666
  return TSDB_CODE_SUCCESS;
}

1667 1668
static int32_t translateFirstLastImpl(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isPartial) {
  // first(col_list) will be rewritten as first(col)
1669
  SNode*  pPara = nodesListGetNode(pFunc->pParameterList, 0);
1670 1671 1672
  uint8_t paraType = ((SExprNode*)pPara)->resType.type;
  int32_t paraBytes = ((SExprNode*)pPara)->resType.bytes;
  if (isPartial) {
1673 1674
    int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
    for (int32_t i = 0; i < numOfParams; ++i) {
1675
      uint8_t nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, i));
1676
      uint8_t pType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
1677
      if (IS_NULL_TYPE(pType) && QUERY_NODE_VALUE == nodeType) {
1678 1679 1680 1681
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }
    }

1682 1683
    pFunc->node.resType =
        (SDataType){.bytes = getFirstLastInfoSize(paraBytes) + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
  } else {
    if (TSDB_DATA_TYPE_BINARY != paraType) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    pFunc->node.resType = ((SExprNode*)pPara)->resType;
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t translateFirstLastPartial(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateFirstLastImpl(pFunc, pErrBuf, len, true);
}

static int32_t translateFirstLastMerge(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateFirstLastImpl(pFunc, pErrBuf, len, false);
}

G
Ganlin Zhao 已提交
1702
static int32_t translateUniqueMode(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isUnique) {
G
Ganlin Zhao 已提交
1703
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
1704
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
G
Ganlin Zhao 已提交
1705 1706
  }

1707 1708
  SNode* pPara = nodesListGetNode(pFunc->pParameterList, 0);
  if (!nodesExprHasColumn(pPara)) {
L
Liu Jicong 已提交
1709 1710
    return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, "The parameters of %s must contain columns",
                           isUnique ? "UNIQUE" : "MODE");
1711 1712 1713
  }

  pFunc->node.resType = ((SExprNode*)pPara)->resType;
G
Ganlin Zhao 已提交
1714 1715 1716
  return TSDB_CODE_SUCCESS;
}

G
Ganlin Zhao 已提交
1717 1718 1719 1720 1721 1722 1723 1724
static int32_t translateUnique(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateUniqueMode(pFunc, pErrBuf, len, true);
}

static int32_t translateMode(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  return translateUniqueMode(pFunc, pErrBuf, len, false);
}

1725
static int32_t translateDiff(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1726
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
1727
  if (numOfParams > 2) {
1728 1729 1730
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1731
  uint8_t colType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
1732
  if (!IS_SIGNED_NUMERIC_TYPE(colType) && !IS_FLOAT_TYPE(colType) && TSDB_DATA_TYPE_BOOL != colType &&
1733
      !IS_TIMESTAMP_TYPE(colType)) {
1734 1735
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }
1736

X
Xiaoyu Wang 已提交
1737
  // param1
1738
  if (numOfParams == 2) {
1739 1740 1741 1742 1743
    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
    if (!IS_INTEGER_TYPE(paraType)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

1744 1745 1746 1747
    SNode* pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
    if (QUERY_NODE_VALUE != nodeType(pParamNode1)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
1748

1749 1750 1751 1752 1753 1754 1755
    SValueNode* pValue = (SValueNode*)pParamNode1;
    if (pValue->datum.i != 0 && pValue->datum.i != 1) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "Second parameter of DIFF function should be only 0 or 1");
    }

    pValue->notReserved = true;
1756
  }
1757

1758
  uint8_t resType;
1759
  if (IS_SIGNED_NUMERIC_TYPE(colType) || IS_TIMESTAMP_TYPE(colType) || TSDB_DATA_TYPE_BOOL == colType) {
1760 1761 1762 1763 1764
    resType = TSDB_DATA_TYPE_BIGINT;
  } else {
    resType = TSDB_DATA_TYPE_DOUBLE;
  }
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[resType].bytes, .type = resType};
1765 1766 1767
  return TSDB_CODE_SUCCESS;
}

1768 1769 1770 1771 1772 1773 1774 1775
static EFuncReturnRows diffEstReturnRows(SFunctionNode* pFunc) {
  if (1 == LIST_LENGTH(pFunc->pParameterList)) {
    return FUNC_RETURN_ROWS_N_MINUS_1;
  }
  return 1 == ((SValueNode*)nodesListGetNode(pFunc->pParameterList, 1))->datum.i ? FUNC_RETURN_ROWS_INDEFINITE
                                                                                 : FUNC_RETURN_ROWS_N_MINUS_1;
}

1776 1777 1778 1779 1780
static int32_t translateLength(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1781
  if (!IS_STR_DATA_TYPE(((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type)) {
1782 1783 1784
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
1785
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
1786 1787 1788
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1789 1790
static int32_t translateConcatImpl(SFunctionNode* pFunc, char* pErrBuf, int32_t len, int32_t minParaNum,
                                   int32_t maxParaNum, bool hasSep) {
1791 1792
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (numOfParams < minParaNum || numOfParams > maxParaNum) {
1793 1794 1795
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1796
  uint8_t resultType = TSDB_DATA_TYPE_BINARY;
1797 1798
  int32_t resultBytes = 0;
  int32_t sepBytes = 0;
1799

1800
  // concat_ws separator should be constant string
1801 1802 1803 1804 1805 1806 1807 1808
  if (hasSep) {
    SNode* pPara = nodesListGetNode(pFunc->pParameterList, 0);
    if (nodeType(pPara) != QUERY_NODE_VALUE) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "The first parameter of CONCAT_WS function can only be constant string");
    }
  }

1809
  /* For concat/concat_ws function, if params have NCHAR type, promote the final result to NCHAR */
1810
  for (int32_t i = 0; i < numOfParams; ++i) {
X
Xiaoyu Wang 已提交
1811
    SNode*  pPara = nodesListGetNode(pFunc->pParameterList, i);
1812
    uint8_t paraType = ((SExprNode*)pPara)->resType.type;
1813
    if (!IS_STR_DATA_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
1814 1815
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
1816 1817 1818 1819 1820
    if (TSDB_DATA_TYPE_NCHAR == paraType) {
      resultType = paraType;
    }
  }

1821
  for (int32_t i = 0; i < numOfParams; ++i) {
X
Xiaoyu Wang 已提交
1822
    SNode*  pPara = nodesListGetNode(pFunc->pParameterList, i);
1823 1824 1825
    uint8_t paraType = ((SExprNode*)pPara)->resType.type;
    int32_t paraBytes = ((SExprNode*)pPara)->resType.bytes;
    int32_t factor = 1;
1826 1827 1828 1829 1830 1831
    if (IS_NULL_TYPE(paraType)) {
      resultType = TSDB_DATA_TYPE_VARCHAR;
      resultBytes = 0;
      sepBytes = 0;
      break;
    }
1832 1833
    if (TSDB_DATA_TYPE_NCHAR == resultType && TSDB_DATA_TYPE_VARCHAR == paraType) {
      factor *= TSDB_NCHAR_SIZE;
1834
    }
1835 1836 1837 1838
    resultBytes += paraBytes * factor;

    if (i == 0) {
      sepBytes = paraBytes * factor;
1839 1840
    }
  }
1841 1842

  if (hasSep) {
1843
    resultBytes += sepBytes * (numOfParams - 3);
1844 1845
  }

X
Xiaoyu Wang 已提交
1846
  pFunc->node.resType = (SDataType){.bytes = resultBytes, .type = resultType};
1847 1848 1849 1850
  return TSDB_CODE_SUCCESS;
}

static int32_t translateConcat(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1851
  return translateConcatImpl(pFunc, pErrBuf, len, 2, 8, false);
1852 1853 1854
}

static int32_t translateConcatWs(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1855
  return translateConcatImpl(pFunc, pErrBuf, len, 3, 9, true);
1856 1857 1858
}

static int32_t translateSubstr(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1859 1860
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (2 != numOfParams && 3 != numOfParams) {
1861 1862 1863
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1864
  SExprNode* pPara0 = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0);
1865
  SExprNode* pPara1 = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 1);
1866

1867 1868
  uint8_t para0Type = pPara0->resType.type;
  uint8_t para1Type = pPara1->resType.type;
1869
  if (!IS_STR_DATA_TYPE(para0Type) || !IS_INTEGER_TYPE(para1Type)) {
1870 1871
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }
1872

1873
  if (((SValueNode*)pPara1)->datum.i == 0) {
1874 1875 1876
    return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
  }

1877
  if (3 == numOfParams) {
1878 1879
    SExprNode* pPara2 = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 2);
    uint8_t    para2Type = pPara2->resType.type;
1880
    if (!IS_INTEGER_TYPE(para2Type)) {
1881 1882
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
1883

1884 1885
    int64_t v = ((SValueNode*)pPara2)->datum.i;
    if (v < 0) {
1886 1887
      return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
    }
1888 1889
  }

1890
  pFunc->node.resType = (SDataType){.bytes = pPara0->resType.bytes, .type = pPara0->resType.type};
1891 1892 1893 1894 1895
  return TSDB_CODE_SUCCESS;
}

static int32_t translateCast(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  // The number of parameters has been limited by the syntax definition
1896

1897 1898
  // The function return type has been set during syntax parsing
  uint8_t para2Type = pFunc->node.resType.type;
1899

1900
  int32_t para2Bytes = pFunc->node.resType.bytes;
1901
  if (IS_STR_DATA_TYPE(para2Type)) {
1902 1903
    para2Bytes -= VARSTR_HEADER_SIZE;
  }
1904
  if (para2Bytes <= 0 || para2Bytes > 4096) {  // cast dst var type length limits to 4096 bytes
1905
    return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
1906
                           "CAST function converted length should be in range [0, 4096] bytes");
1907
  }
1908 1909 1910

  // add database precision as param
  uint8_t dbPrec = pFunc->node.resType.precision;
1911
  int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec);
G
Ganlin Zhao 已提交
1912 1913 1914
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
1915

1916 1917 1918 1919
  return TSDB_CODE_SUCCESS;
}

static int32_t translateToIso8601(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1920 1921
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (1 != numOfParams && 2 != numOfParams) {
1922 1923 1924
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1925
  // param0
1926
  uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
1927
  if (!IS_INTEGER_TYPE(paraType) && !IS_TIMESTAMP_TYPE(paraType)) {
1928 1929 1930
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1931 1932 1933 1934 1935 1936 1937 1938 1939
  if (QUERY_NODE_VALUE == nodeType(nodesListGetNode(pFunc->pParameterList, 0))) {
    SValueNode* pValue = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 0);

    if (!validateTimestampDigits(pValue)) {
      pFunc->node.resType = (SDataType){.bytes = 0, .type = TSDB_DATA_TYPE_BINARY};
      return TSDB_CODE_SUCCESS;
    }
  }

1940
  // param1
1941 1942 1943 1944
  if (numOfParams == 2) {
    SValueNode* pValue = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);

    if (!validateTimezoneFormat(pValue)) {
1945
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, "Invalid timzone format");
1946
    }
1947
  } else {  // add default client timezone
G
Ganlin Zhao 已提交
1948 1949 1950 1951
    int32_t code = addTimezoneParam(pFunc->pParameterList);
    if (code != TSDB_CODE_SUCCESS) {
      return code;
    }
1952 1953
  }

1954
  // set result type
X
Xiaoyu Wang 已提交
1955
  pFunc->node.resType = (SDataType){.bytes = 64, .type = TSDB_DATA_TYPE_BINARY};
1956 1957 1958 1959
  return TSDB_CODE_SUCCESS;
}

static int32_t translateToUnixtimestamp(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1960 1961 1962 1963
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  int16_t resType = TSDB_DATA_TYPE_BIGINT;

  if (1 != numOfParams && 2 != numOfParams) {
1964 1965 1966
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

1967 1968
  uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  if (!IS_STR_DATA_TYPE(para1Type)) {
1969 1970 1971
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
  if (2 == numOfParams) {
    uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
    if (!IS_INTEGER_TYPE(para2Type)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

    SValueNode* pValue = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1);
    if (pValue->datum.i == 1) {
      resType = TSDB_DATA_TYPE_TIMESTAMP;
    } else if (pValue->datum.i == 0) {
      resType = TSDB_DATA_TYPE_BIGINT;
    } else {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "TO_UNIXTIMESTAMP function second parameter should be 0/1");
    }
  }

1989
  // add database precision as param
1990
  uint8_t dbPrec = pFunc->node.resType.precision;
1991
  int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec);
G
Ganlin Zhao 已提交
1992 1993 1994
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
1995

1996
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[resType].bytes, .type = resType};
1997 1998 1999 2000
  return TSDB_CODE_SUCCESS;
}

static int32_t translateTimeTruncate(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
2001 2002
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (2 != numOfParams && 3 != numOfParams) {
2003 2004 2005 2006 2007
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
  uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
2008
  if ((!IS_STR_DATA_TYPE(para1Type) && !IS_INTEGER_TYPE(para1Type) && !IS_TIMESTAMP_TYPE(para1Type)) ||
X
Xiaoyu Wang 已提交
2009
      !IS_INTEGER_TYPE(para2Type)) {
2010 2011 2012
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

2013
  uint8_t dbPrec = pFunc->node.resType.precision;
2014
  int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1));
2015 2016 2017 2018
  if (ret == TIME_UNIT_TOO_SMALL) {
    return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                           "TIMETRUNCATE function time unit parameter should be greater than db precision");
  } else if (ret == TIME_UNIT_INVALID) {
X
Xiaoyu Wang 已提交
2019 2020 2021
    return buildFuncErrMsg(
        pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
        "TIMETRUNCATE function time unit parameter should be one of the following: [1b, 1u, 1a, 1s, 1m, 1h, 1d, 1w]");
2022 2023
  }

2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
  if (3 == numOfParams) {
    uint8_t para3Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type;
    if (!IS_INTEGER_TYPE(para3Type)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
    SValueNode* pValue = (SValueNode*)nodesListGetNode(pFunc->pParameterList, 2);
    if (pValue->datum.i != 0 && pValue->datum.i != 1) {
      return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
    }
  }

  // add database precision as param

G
Ganlin Zhao 已提交
2037 2038 2039 2040
  int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
2041

2042 2043 2044 2045 2046 2047
  // add client timezone as param
  code = addTimezoneParam(pFunc->pParameterList);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }

X
Xiaoyu Wang 已提交
2048 2049
  pFunc->node.resType =
      (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes, .type = TSDB_DATA_TYPE_TIMESTAMP};
2050 2051 2052 2053
  return TSDB_CODE_SUCCESS;
}

static int32_t translateTimeDiff(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
2054 2055
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (2 != numOfParams && 3 != numOfParams) {
2056 2057 2058
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

2059 2060
  for (int32_t i = 0; i < 2; ++i) {
    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
2061
    if (!IS_STR_DATA_TYPE(paraType) && !IS_INTEGER_TYPE(paraType) && !IS_TIMESTAMP_TYPE(paraType)) {
2062 2063
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
2064
  }
2065

2066
  if (3 == numOfParams) {
2067 2068 2069 2070 2071
    if (!IS_INTEGER_TYPE(((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type)) {
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
  }

2072
  // add database precision as param
2073
  uint8_t dbPrec = pFunc->node.resType.precision;
2074

G
Ganlin Zhao 已提交
2075
  if (3 == numOfParams) {
2076
    int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 2));
G
Ganlin Zhao 已提交
2077 2078 2079 2080
    if (ret == TIME_UNIT_TOO_SMALL) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "TIMEDIFF function time unit parameter should be greater than db precision");
    } else if (ret == TIME_UNIT_INVALID) {
X
Xiaoyu Wang 已提交
2081 2082 2083
      return buildFuncErrMsg(
          pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
          "TIMEDIFF function time unit parameter should be one of the following: [1b, 1u, 1a, 1s, 1m, 1h, 1d, 1w]");
G
Ganlin Zhao 已提交
2084
    }
2085 2086
  }

G
Ganlin Zhao 已提交
2087 2088 2089 2090
  int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
2091

X
Xiaoyu Wang 已提交
2092
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
2093 2094
  return TSDB_CODE_SUCCESS;
}
2095

2096 2097 2098 2099 2100
static int32_t translateToJson(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

2101
  SExprNode* pPara = (SExprNode*)nodesListGetNode(pFunc->pParameterList, 0);
2102 2103 2104 2105
  if (QUERY_NODE_VALUE != nodeType(pPara) || (!IS_VAR_DATA_TYPE(pPara->resType.type))) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

wmmhello's avatar
wmmhello 已提交
2106
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_JSON].bytes, .type = TSDB_DATA_TYPE_JSON};
2107 2108 2109
  return TSDB_CODE_SUCCESS;
}

2110 2111 2112 2113 2114
static int32_t translateSelectValue(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType;
  return TSDB_CODE_SUCCESS;
}

2115
static int32_t translateBlockDistFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
X
Xiaoyu Wang 已提交
2116
  pFunc->node.resType = (SDataType){.bytes = 128, .type = TSDB_DATA_TYPE_VARCHAR};
2117 2118 2119
  return TSDB_CODE_SUCCESS;
}

2120 2121 2122 2123 2124
static int32_t translateBlockDistInfoFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = (SDataType){.bytes = 128, .type = TSDB_DATA_TYPE_VARCHAR};
  return TSDB_CODE_SUCCESS;
}

2125 2126 2127 2128 2129
static bool getBlockDistFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
  pEnv->calcMemSize = sizeof(STableBlockDistInfo);
  return true;
}

2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
static int32_t translateGroupKey(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return TSDB_CODE_SUCCESS;
  }

  SNode* pPara = nodesListGetNode(pFunc->pParameterList, 0);
  pFunc->node.resType = ((SExprNode*)pPara)->resType;
  return TSDB_CODE_SUCCESS;
}

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 2167 2168 2169
static int32_t translateDatabaseFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = (SDataType){.bytes = TSDB_DB_NAME_LEN, .type = TSDB_DATA_TYPE_VARCHAR};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateClientVersionFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = (SDataType){.bytes = TSDB_VERSION_LEN, .type = TSDB_DATA_TYPE_VARCHAR};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateServerVersionFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = (SDataType){.bytes = TSDB_VERSION_LEN, .type = TSDB_DATA_TYPE_VARCHAR};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateServerStatusFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_INT].bytes, .type = TSDB_DATA_TYPE_INT};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateCurrentUserFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = (SDataType){.bytes = TSDB_USER_LEN, .type = TSDB_DATA_TYPE_VARCHAR};
  return TSDB_CODE_SUCCESS;
}

static int32_t translateUserFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = (SDataType){.bytes = TSDB_USER_LEN, .type = TSDB_DATA_TYPE_VARCHAR};
  return TSDB_CODE_SUCCESS;
}

2170 2171 2172 2173 2174
static int32_t translateTagsPseudoColumn(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  // The _tags pseudo-column will be expanded to the actual tags on the client side
  return TSDB_CODE_SUCCESS;
}

2175
static int32_t translateTableCountPseudoColumn(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
X
Xiaoyu Wang 已提交
2176
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
2177 2178 2179
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
2180
// clang-format off
2181
const SBuiltinFuncDefinition funcMgtBuiltins[] = {
G
Ganlin Zhao 已提交
2182 2183 2184 2185 2186 2187 2188 2189 2190
  {
    .name = "count",
    .type = FUNCTION_TYPE_COUNT,
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
    .translateFunc = translateCount,
    .dataRequiredFunc = countDataRequired,
    .getEnvFunc   = getCountFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = countFunction,
2191
    .sprocessFunc = countScalarFunction,
5
54liuyao 已提交
2192
    .finalizeFunc = functionFinalize,
5
54liuyao 已提交
2193
    .invertFunc   = countInvertFunction,
2194 2195 2196
    .combineFunc  = combineFunction,
    .pPartialFunc = "count",
    .pMergeFunc   = "sum"
G
Ganlin Zhao 已提交
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
  },
  {
    .name = "sum",
    .type = FUNCTION_TYPE_SUM,
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
    .translateFunc = translateSum,
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getSumFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = sumFunction,
2207
    .sprocessFunc = sumScalarFunction,
5
54liuyao 已提交
2208
    .finalizeFunc = functionFinalize,
5
54liuyao 已提交
2209
    .invertFunc   = sumInvertFunction,
2210 2211 2212
    .combineFunc  = sumCombine,
    .pPartialFunc = "sum",
    .pMergeFunc   = "sum"
G
Ganlin Zhao 已提交
2213 2214 2215 2216
  },
  {
    .name = "min",
    .type = FUNCTION_TYPE_MIN,
2217
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED | FUNC_MGT_SELECT_FUNC,
2218
    .translateFunc = translateInOutNum,
G
Ganlin Zhao 已提交
2219 2220
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getMinmaxFuncEnv,
2221
    .initFunc     = minmaxFunctionSetup,
G
Ganlin Zhao 已提交
2222
    .processFunc  = minFunction,
2223
    .sprocessFunc = minScalarFunction,
5
54liuyao 已提交
2224
    .finalizeFunc = minmaxFunctionFinalize,
2225 2226 2227
    .combineFunc  = minCombine,
    .pPartialFunc = "min",
    .pMergeFunc   = "min"
G
Ganlin Zhao 已提交
2228 2229 2230 2231
  },
  {
    .name = "max",
    .type = FUNCTION_TYPE_MAX,
2232
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED | FUNC_MGT_SELECT_FUNC,
2233
    .translateFunc = translateInOutNum,
G
Ganlin Zhao 已提交
2234 2235
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getMinmaxFuncEnv,
2236
    .initFunc     = minmaxFunctionSetup,
G
Ganlin Zhao 已提交
2237
    .processFunc  = maxFunction,
2238
    .sprocessFunc = maxScalarFunction,
5
54liuyao 已提交
2239
    .finalizeFunc = minmaxFunctionFinalize,
2240 2241 2242
    .combineFunc  = maxCombine,
    .pPartialFunc = "max",
    .pMergeFunc   = "max"
G
Ganlin Zhao 已提交
2243 2244 2245 2246 2247 2248 2249 2250 2251
  },
  {
    .name = "stddev",
    .type = FUNCTION_TYPE_STDDEV,
    .classification = FUNC_MGT_AGG_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = getStddevFuncEnv,
    .initFunc     = stddevFunctionSetup,
    .processFunc  = stddevFunction,
2252
    .sprocessFunc = stddevScalarFunction,
5
54liuyao 已提交
2253
    .finalizeFunc = stddevFinalize,
5
54liuyao 已提交
2254 2255
    .invertFunc   = stddevInvertFunction,
    .combineFunc  = stddevCombine,
G
Ganlin Zhao 已提交
2256 2257
    .pPartialFunc = "_stddev_partial",
    .pMergeFunc   = "_stddev_merge"
G
Ganlin Zhao 已提交
2258
  },
2259 2260 2261 2262 2263 2264 2265 2266
  {
    .name = "_stddev_partial",
    .type = FUNCTION_TYPE_STDDEV_PARTIAL,
    .classification = FUNC_MGT_AGG_FUNC,
    .translateFunc = translateStddevPartial,
    .getEnvFunc   = getStddevFuncEnv,
    .initFunc     = stddevFunctionSetup,
    .processFunc  = stddevFunction,
G
Ganlin Zhao 已提交
2267
    .finalizeFunc = stddevPartialFinalize,
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277
    .invertFunc   = stddevInvertFunction,
    .combineFunc  = stddevCombine,
  },
  {
    .name = "_stddev_merge",
    .type = FUNCTION_TYPE_STDDEV_MERGE,
    .classification = FUNC_MGT_AGG_FUNC,
    .translateFunc = translateStddevMerge,
    .getEnvFunc   = getStddevFuncEnv,
    .initFunc     = stddevFunctionSetup,
G
Ganlin Zhao 已提交
2278
    .processFunc  = stddevFunctionMerge,
2279 2280 2281 2282
    .finalizeFunc = stddevFinalize,
    .invertFunc   = stddevInvertFunction,
    .combineFunc  = stddevCombine,
  },
2283 2284 2285
  {
    .name = "leastsquares",
    .type = FUNCTION_TYPE_LEASTSQUARES,
2286
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
2287 2288 2289 2290
    .translateFunc = translateLeastSQR,
    .getEnvFunc   = getLeastSQRFuncEnv,
    .initFunc     = leastSQRFunctionSetup,
    .processFunc  = leastSQRFunction,
2291
    .sprocessFunc = leastSQRScalarFunction,
2292
    .finalizeFunc = leastSQRFinalize,
5
54liuyao 已提交
2293 2294
    .invertFunc   = NULL,
    .combineFunc  = leastSQRCombine,
2295
  },
G
Ganlin Zhao 已提交
2296 2297 2298
  {
    .name = "avg",
    .type = FUNCTION_TYPE_AVG,
2299
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
G
Ganlin Zhao 已提交
2300
    .translateFunc = translateInNumOutDou,
H
Haojun Liao 已提交
2301
    .dataRequiredFunc = statisDataRequired,
G
Ganlin Zhao 已提交
2302 2303 2304
    .getEnvFunc   = getAvgFuncEnv,
    .initFunc     = avgFunctionSetup,
    .processFunc  = avgFunction,
2305
    .sprocessFunc = avgScalarFunction,
5
54liuyao 已提交
2306
    .finalizeFunc = avgFinalize,
5
54liuyao 已提交
2307 2308
    .invertFunc   = avgInvertFunction,
    .combineFunc  = avgCombine,
G
Ganlin Zhao 已提交
2309 2310
    .pPartialFunc = "_avg_partial",
    .pMergeFunc   = "_avg_merge"
G
Ganlin Zhao 已提交
2311
  },
2312 2313 2314 2315
  {
    .name = "_avg_partial",
    .type = FUNCTION_TYPE_AVG_PARTIAL,
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2316
    .translateFunc = translateAvgPartial,
2317
    .dataRequiredFunc = statisDataRequired,
2318 2319 2320
    .getEnvFunc   = getAvgFuncEnv,
    .initFunc     = avgFunctionSetup,
    .processFunc  = avgFunction,
G
Ganlin Zhao 已提交
2321
    .finalizeFunc = avgPartialFinalize,
2322 2323 2324 2325 2326 2327 2328
    .invertFunc   = avgInvertFunction,
    .combineFunc  = avgCombine,
  },
  {
    .name = "_avg_merge",
    .type = FUNCTION_TYPE_AVG_MERGE,
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2329
    .translateFunc = translateAvgMerge,
2330 2331
    .getEnvFunc   = getAvgFuncEnv,
    .initFunc     = avgFunctionSetup,
G
Ganlin Zhao 已提交
2332
    .processFunc  = avgFunctionMerge,
2333 2334 2335 2336
    .finalizeFunc = avgFinalize,
    .invertFunc   = avgInvertFunction,
    .combineFunc  = avgCombine,
  },
G
Ganlin Zhao 已提交
2337 2338 2339
  {
    .name = "percentile",
    .type = FUNCTION_TYPE_PERCENTILE,
G
Ganlin Zhao 已提交
2340
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_REPEAT_SCAN_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED | FUNC_MGT_FORBID_STREAM_FUNC,
G
Ganlin Zhao 已提交
2341
    .translateFunc = translatePercentile,
G
Ganlin Zhao 已提交
2342
    .dataRequiredFunc = statisDataRequired,
G
Ganlin Zhao 已提交
2343 2344 2345
    .getEnvFunc   = getPercentileFuncEnv,
    .initFunc     = percentileFunctionSetup,
    .processFunc  = percentileFunction,
2346
    .sprocessFunc = percentileScalarFunction,
2347 2348 2349
    .finalizeFunc = percentileFinalize,
    .invertFunc   = NULL,
    .combineFunc  = NULL,
G
Ganlin Zhao 已提交
2350 2351 2352 2353
  },
  {
    .name = "apercentile",
    .type = FUNCTION_TYPE_APERCENTILE,
2354
    .classification = FUNC_MGT_AGG_FUNC,
2355
    .translateFunc = translateApercentile,
2356 2357 2358
    .getEnvFunc   = getApercentileFuncEnv,
    .initFunc     = apercentileFunctionSetup,
    .processFunc  = apercentileFunction,
2359
    .sprocessFunc = apercentileScalarFunction,
G
Ganlin Zhao 已提交
2360
    .finalizeFunc = apercentileFinalize,
2361
    .invertFunc   = NULL,
2362
    .combineFunc  = apercentileCombine,
G
Ganlin Zhao 已提交
2363
    .pPartialFunc = "_apercentile_partial",
2364 2365
    .pMergeFunc   = "_apercentile_merge",
    .createMergeParaFuc = apercentileCreateMergeParam
2366 2367 2368
  },
  {
    .name = "_apercentile_partial",
2369
    .type = FUNCTION_TYPE_APERCENTILE_PARTIAL,
2370 2371 2372 2373 2374
    .classification = FUNC_MGT_AGG_FUNC,
    .translateFunc = translateApercentilePartial,
    .getEnvFunc   = getApercentileFuncEnv,
    .initFunc     = apercentileFunctionSetup,
    .processFunc  = apercentileFunction,
2375 2376 2377
    .finalizeFunc = apercentilePartialFinalize,
    .invertFunc   = NULL,
    .combineFunc = apercentileCombine,
2378 2379 2380 2381
  },
  {
    .name = "_apercentile_merge",
    .type = FUNCTION_TYPE_APERCENTILE_MERGE,
2382
    .classification = FUNC_MGT_AGG_FUNC,
2383
    .translateFunc = translateApercentileMerge,
2384
    .getEnvFunc   = getApercentileFuncEnv,
2385
    .initFunc     = apercentileFunctionSetup,
G
Ganlin Zhao 已提交
2386
    .processFunc  = apercentileFunctionMerge,
2387 2388 2389
    .finalizeFunc = apercentileFinalize,
    .invertFunc   = NULL,
    .combineFunc = apercentileCombine,
G
Ganlin Zhao 已提交
2390 2391 2392 2393
  },
  {
    .name = "top",
    .type = FUNCTION_TYPE_TOP,
2394
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_FILL_FUNC,
G
Ganlin Zhao 已提交
2395 2396
    .translateFunc = translateTopBot,
    .getEnvFunc   = getTopBotFuncEnv,
G
Ganlin Zhao 已提交
2397
    .initFunc     = topBotFunctionSetup,
G
Ganlin Zhao 已提交
2398
    .processFunc  = topFunction,
G
Ganlin Zhao 已提交
2399
    .sprocessFunc = topBotScalarFunction,
G
Ganlin Zhao 已提交
2400 2401
    .finalizeFunc = topBotFinalize,
    .combineFunc  = topCombine,
2402 2403
    .pPartialFunc = "top",
    .pMergeFunc   = "top",
2404
    .createMergeParaFuc = topBotCreateMergeParam
G
Ganlin Zhao 已提交
2405 2406 2407 2408
  },
  {
    .name = "bottom",
    .type = FUNCTION_TYPE_BOTTOM,
2409
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_FILL_FUNC,
G
Ganlin Zhao 已提交
2410
    .translateFunc = translateTopBot,
2411
    .getEnvFunc   = getTopBotFuncEnv,
2412
    .initFunc     = topBotFunctionSetup,
2413
    .processFunc  = bottomFunction,
G
Ganlin Zhao 已提交
2414
    .sprocessFunc = topBotScalarFunction,
2415 2416
    .finalizeFunc = topBotFinalize,
    .combineFunc  = bottomCombine,
2417 2418
    .pPartialFunc = "bottom",
    .pMergeFunc   = "bottom",
2419
    .createMergeParaFuc = topBotCreateMergeParam
G
Ganlin Zhao 已提交
2420 2421 2422 2423
  },
  {
    .name = "spread",
    .type = FUNCTION_TYPE_SPREAD,
2424
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
G
Ganlin Zhao 已提交
2425 2426 2427 2428 2429
    .translateFunc = translateSpread,
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getSpreadFuncEnv,
    .initFunc     = spreadFunctionSetup,
    .processFunc  = spreadFunction,
2430
    .sprocessFunc = spreadScalarFunction,
G
Ganlin Zhao 已提交
2431
    .finalizeFunc = spreadFinalize,
2432 2433
    .invertFunc   = NULL,
    .combineFunc  = spreadCombine,
G
Ganlin Zhao 已提交
2434 2435
    .pPartialFunc = "_spread_partial",
    .pMergeFunc   = "_spread_merge"
G
Ganlin Zhao 已提交
2436
  },
2437 2438 2439 2440
  {
    .name = "_spread_partial",
    .type = FUNCTION_TYPE_SPREAD_PARTIAL,
    .classification = FUNC_MGT_AGG_FUNC,
2441
    .translateFunc = translateSpreadPartial,
2442 2443 2444 2445
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getSpreadFuncEnv,
    .initFunc     = spreadFunctionSetup,
    .processFunc  = spreadFunction,
2446 2447 2448
    .finalizeFunc = spreadPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = spreadCombine,
2449 2450 2451 2452 2453
  },
  {
    .name = "_spread_merge",
    .type = FUNCTION_TYPE_SPREAD_MERGE,
    .classification = FUNC_MGT_AGG_FUNC,
2454
    .translateFunc = translateSpreadMerge,
2455 2456 2457
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getSpreadFuncEnv,
    .initFunc     = spreadFunctionSetup,
G
Ganlin Zhao 已提交
2458
    .processFunc  = spreadFunctionMerge,
2459 2460 2461
    .finalizeFunc = spreadFinalize,
    .invertFunc   = NULL,
    .combineFunc  = spreadCombine,
2462
  },
G
Ganlin Zhao 已提交
2463 2464 2465
  {
    .name = "elapsed",
    .type = FUNCTION_TYPE_ELAPSED,
2466
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_INTERVAL_INTERPO_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
G
Ganlin Zhao 已提交
2467 2468 2469 2470 2471
    .dataRequiredFunc = statisDataRequired,
    .translateFunc = translateElapsed,
    .getEnvFunc   = getElapsedFuncEnv,
    .initFunc     = elapsedFunctionSetup,
    .processFunc  = elapsedFunction,
G
Ganlin Zhao 已提交
2472
    .finalizeFunc = elapsedFinalize,
2473 2474
    .invertFunc   = NULL,
    .combineFunc  = elapsedCombine,
G
Ganlin Zhao 已提交
2475
  },
2476 2477 2478 2479 2480 2481 2482 2483 2484
  {
    .name = "_elapsed_partial",
    .type = FUNCTION_TYPE_ELAPSED,
    .classification = FUNC_MGT_AGG_FUNC,
    .dataRequiredFunc = statisDataRequired,
    .translateFunc = translateElapsedPartial,
    .getEnvFunc   = getElapsedFuncEnv,
    .initFunc     = elapsedFunctionSetup,
    .processFunc  = elapsedFunction,
2485 2486 2487
    .finalizeFunc = elapsedPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = elapsedCombine,
2488 2489 2490 2491 2492 2493 2494 2495 2496
  },
  {
    .name = "_elapsed_merge",
    .type = FUNCTION_TYPE_ELAPSED,
    .classification = FUNC_MGT_AGG_FUNC,
    .dataRequiredFunc = statisDataRequired,
    .translateFunc = translateElapsedMerge,
    .getEnvFunc   = getElapsedFuncEnv,
    .initFunc     = elapsedFunctionSetup,
2497
    .processFunc  = elapsedFunctionMerge,
2498 2499 2500
    .finalizeFunc = elapsedFinalize,
    .invertFunc   = NULL,
    .combineFunc  = elapsedCombine,
G
Ganlin Zhao 已提交
2501
  },
2502 2503 2504
  {
    .name = "interp",
    .type = FUNCTION_TYPE_INTERP,
2505
    .classification = FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_INTERVAL_INTERPO_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2506
                      FUNC_MGT_FORBID_STREAM_FUNC|FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
2507
    .translateFunc = translateInterp,
2508 2509 2510
    .getEnvFunc    = getSelectivityFuncEnv,
    .initFunc      = functionSetup,
    .processFunc   = NULL,
2511 2512
    .finalizeFunc  = NULL,
    .estimateReturnRowsFunc = interpEstReturnRows
2513
  },
2514 2515 2516
  {
    .name = "derivative",
    .type = FUNCTION_TYPE_DERIVATIVE,
2517
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
2518
                      FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_CUMULATIVE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
2519 2520 2521 2522
    .translateFunc = translateDerivative,
    .getEnvFunc   = getDerivativeFuncEnv,
    .initFunc     = derivativeFuncSetup,
    .processFunc  = derivativeFunction,
G
Ganlin Zhao 已提交
2523
    .sprocessFunc = derivativeScalarFunction,
2524 2525
    .finalizeFunc = functionFinalize,
    .estimateReturnRowsFunc = derivativeEstReturnRows
2526
  },
G
Ganlin Zhao 已提交
2527 2528 2529
  {
    .name = "irate",
    .type = FUNCTION_TYPE_IRATE,
5
54liuyao 已提交
2530
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
G
Ganlin Zhao 已提交
2531 2532 2533 2534
    .translateFunc = translateIrate,
    .getEnvFunc   = getIrateFuncEnv,
    .initFunc     = irateFuncSetup,
    .processFunc  = irateFunction,
2535
    .sprocessFunc = irateScalarFunction,
G
Ganlin Zhao 已提交
2536 2537
    .finalizeFunc = irateFinalize
  },
G
Ganlin Zhao 已提交
2538 2539 2540
  {
    .name = "last_row",
    .type = FUNCTION_TYPE_LAST_ROW,
2541
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
2542
    .translateFunc = translateFirstLast,
2543
    .dynDataRequiredFunc = lastDynDataReq,
2544 2545
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
2546
    .processFunc  = lastRowFunction,
2547
    .sprocessFunc = firstLastScalarFunction,
2548 2549
    .pPartialFunc = "_last_row_partial",
    .pMergeFunc   = "_last_row_merge",
5
54liuyao 已提交
2550 2551
    .finalizeFunc = firstLastFinalize,
    .combineFunc  = lastCombine
2552 2553 2554 2555
  },
  {
    .name = "_cache_last_row",
    .type = FUNCTION_TYPE_CACHE_LAST_ROW,
2556
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
2557 2558 2559
    .translateFunc = translateFirstLast,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
2560
    .processFunc  = cachedLastRowFunction,
2561
    .finalizeFunc = firstLastFinalize
G
Ganlin Zhao 已提交
2562
  },
X
Xiaoyu Wang 已提交
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
  {
    .name = "_cache_last",
    .type = FUNCTION_TYPE_CACHE_LAST,
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
    .translateFunc = translateFirstLast,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunctionMerge,
    .finalizeFunc = firstLastFinalize
  },
2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
  {
    .name = "_last_row_partial",
    .type = FUNCTION_TYPE_LAST_PARTIAL,
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
    .translateFunc = translateFirstLastPartial,
    .dynDataRequiredFunc = lastDynDataReq,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastRowFunction,
    .finalizeFunc = firstLastPartialFinalize,
  },
  {
    .name = "_last_row_merge",
    .type = FUNCTION_TYPE_LAST_MERGE,
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
    .translateFunc = translateFirstLastMerge,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunctionMerge,
    .finalizeFunc = firstLastFinalize,
  },
G
Ganlin Zhao 已提交
2594 2595 2596
  {
    .name = "first",
    .type = FUNCTION_TYPE_FIRST,
X
Xiaoyu Wang 已提交
2597
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
2598
    .translateFunc = translateFirstLast,
2599
    .dynDataRequiredFunc = firstDynDataReq,
G
Ganlin Zhao 已提交
2600 2601 2602
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = firstFunction,
2603
    .sprocessFunc = firstLastScalarFunction,
2604
    .finalizeFunc = firstLastFinalize,
G
Ganlin Zhao 已提交
2605 2606
    .pPartialFunc = "_first_partial",
    .pMergeFunc   = "_first_merge",
2607
    .combineFunc  = firstCombine,
G
Ganlin Zhao 已提交
2608
  },
2609 2610 2611
  {
    .name = "_first_partial",
    .type = FUNCTION_TYPE_FIRST_PARTIAL,
2612
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
2613
    .translateFunc = translateFirstLastPartial,
2614
    .dynDataRequiredFunc = firstDynDataReq,
2615 2616 2617
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = firstFunction,
G
Ganlin Zhao 已提交
2618
    .finalizeFunc = firstLastPartialFinalize,
2619 2620 2621 2622 2623
    .combineFunc  = firstCombine,
  },
  {
    .name = "_first_merge",
    .type = FUNCTION_TYPE_FIRST_MERGE,
2624
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
2625 2626 2627
    .translateFunc = translateFirstLastMerge,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
G
Ganlin Zhao 已提交
2628
    .processFunc  = firstFunctionMerge,
2629 2630 2631
    .finalizeFunc = firstLastFinalize,
    .combineFunc  = firstCombine,
  },
G
Ganlin Zhao 已提交
2632 2633 2634
  {
    .name = "last",
    .type = FUNCTION_TYPE_LAST,
X
Xiaoyu Wang 已提交
2635
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
2636
    .translateFunc = translateFirstLast,
2637
    .dynDataRequiredFunc = lastDynDataReq,
G
Ganlin Zhao 已提交
2638 2639 2640
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunction,
2641
    .sprocessFunc = firstLastScalarFunction,
2642
    .finalizeFunc = firstLastFinalize,
G
Ganlin Zhao 已提交
2643 2644
    .pPartialFunc = "_last_partial",
    .pMergeFunc   = "_last_merge",
5
54liuyao 已提交
2645
    .combineFunc  = lastCombine,
G
Ganlin Zhao 已提交
2646
  },
G
Ganlin Zhao 已提交
2647 2648 2649
  {
    .name = "_last_partial",
    .type = FUNCTION_TYPE_LAST_PARTIAL,
2650
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
G
Ganlin Zhao 已提交
2651
    .translateFunc = translateFirstLastPartial,
2652
    .dynDataRequiredFunc = lastDynDataReq,
G
Ganlin Zhao 已提交
2653 2654 2655 2656 2657 2658 2659 2660 2661
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunction,
    .finalizeFunc = firstLastPartialFinalize,
    .combineFunc  = lastCombine,
  },
  {
    .name = "_last_merge",
    .type = FUNCTION_TYPE_LAST_MERGE,
2662
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
G
Ganlin Zhao 已提交
2663 2664 2665 2666 2667 2668 2669
    .translateFunc = translateFirstLastMerge,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunctionMerge,
    .finalizeFunc = firstLastFinalize,
    .combineFunc  = lastCombine,
  },
2670 2671 2672
  {
    .name = "twa",
    .type = FUNCTION_TYPE_TWA,
2673
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_INTERVAL_INTERPO_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
2674
    .translateFunc = translateInNumOutDou,
H
Haojun Liao 已提交
2675
    .dataRequiredFunc = statisDataRequired,
2676 2677 2678
    .getEnvFunc    = getTwaFuncEnv,
    .initFunc      = twaFunctionSetup,
    .processFunc   = twaFunction,
2679
    .sprocessFunc  = twaScalarFunction,
2680 2681
    .finalizeFunc  = twaFinalize
  },
2682 2683 2684
  {
    .name = "histogram",
    .type = FUNCTION_TYPE_HISTOGRAM,
2685
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_FORBID_FILL_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
2686 2687 2688 2689
    .translateFunc = translateHistogram,
    .getEnvFunc   = getHistogramFuncEnv,
    .initFunc     = histogramFunctionSetup,
    .processFunc  = histogramFunction,
2690
    .sprocessFunc = histogramScalarFunction,
G
Ganlin Zhao 已提交
2691
    .finalizeFunc = histogramFinalize,
2692 2693
    .invertFunc   = NULL,
    .combineFunc  = histogramCombine,
G
Ganlin Zhao 已提交
2694
    .pPartialFunc = "_histogram_partial",
2695
    .pMergeFunc   = "_histogram_merge",
2696
  },
2697 2698 2699
  {
    .name = "_histogram_partial",
    .type = FUNCTION_TYPE_HISTOGRAM_PARTIAL,
2700
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_FORBID_FILL_FUNC,
2701
    .translateFunc = translateHistogramPartial,
2702 2703
    .getEnvFunc   = getHistogramFuncEnv,
    .initFunc     = histogramFunctionSetup,
2704
    .processFunc  = histogramFunctionPartial,
2705 2706 2707
    .finalizeFunc = histogramPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = histogramCombine,
2708 2709 2710 2711
  },
  {
    .name = "_histogram_merge",
    .type = FUNCTION_TYPE_HISTOGRAM_MERGE,
2712
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_FORBID_FILL_FUNC,
2713
    .translateFunc = translateHistogramMerge,
2714
    .getEnvFunc   = getHistogramFuncEnv,
G
Ganlin Zhao 已提交
2715
    .initFunc     = functionSetup,
2716
    .processFunc  = histogramFunctionMerge,
2717 2718 2719
    .finalizeFunc = histogramFinalize,
    .invertFunc   = NULL,
    .combineFunc  = histogramCombine,
2720
  },
G
Ganlin Zhao 已提交
2721 2722 2723
  {
    .name = "hyperloglog",
    .type = FUNCTION_TYPE_HYPERLOGLOG,
2724
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2725 2726 2727 2728
    .translateFunc = translateHLL,
    .getEnvFunc   = getHLLFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = hllFunction,
2729
    .sprocessFunc = hllScalarFunction,
G
Ganlin Zhao 已提交
2730
    .finalizeFunc = hllFinalize,
2731 2732
    .invertFunc   = NULL,
    .combineFunc  = hllCombine,
G
Ganlin Zhao 已提交
2733 2734
    .pPartialFunc = "_hyperloglog_partial",
    .pMergeFunc   = "_hyperloglog_merge"
G
Ganlin Zhao 已提交
2735
  },
2736 2737
  {
    .name = "_hyperloglog_partial",
2738
    .type = FUNCTION_TYPE_HYPERLOGLOG_PARTIAL,
2739
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2740
    .translateFunc = translateHLLPartial,
2741 2742 2743
    .getEnvFunc   = getHLLFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = hllFunction,
2744 2745 2746
    .finalizeFunc = hllPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = hllCombine,
2747 2748 2749
  },
  {
    .name = "_hyperloglog_merge",
2750
    .type = FUNCTION_TYPE_HYPERLOGLOG_MERGE,
2751
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2752
    .translateFunc = translateHLLMerge,
2753 2754
    .getEnvFunc   = getHLLFuncEnv,
    .initFunc     = functionSetup,
G
Ganlin Zhao 已提交
2755
    .processFunc  = hllFunctionMerge,
2756 2757 2758
    .finalizeFunc = hllFinalize,
    .invertFunc   = NULL,
    .combineFunc  = hllCombine,
G
Ganlin Zhao 已提交
2759
  },
G
Ganlin Zhao 已提交
2760 2761 2762
  {
    .name = "diff",
    .type = FUNCTION_TYPE_DIFF,
2763 2764
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_CUMULATIVE_FUNC,
G
Ganlin Zhao 已提交
2765 2766 2767 2768
    .translateFunc = translateDiff,
    .getEnvFunc   = getDiffFuncEnv,
    .initFunc     = diffFunctionSetup,
    .processFunc  = diffFunction,
2769
    .sprocessFunc = diffScalarFunction,
2770
    .finalizeFunc = functionFinalize,
2771
    .estimateReturnRowsFunc = diffEstReturnRows,
G
Ganlin Zhao 已提交
2772
  },
2773
  {
2774
    .name = "statecount",
2775
    .type = FUNCTION_TYPE_STATE_COUNT,
2776 2777
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_STREAM_FUNC,
2778
    .translateFunc = translateStateCount,
2779 2780 2781
    .getEnvFunc   = getStateFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = stateCountFunction,
2782
    .sprocessFunc = stateCountScalarFunction,
2783 2784
    .finalizeFunc = NULL
  },
2785
  {
2786
    .name = "stateduration",
2787
    .type = FUNCTION_TYPE_STATE_DURATION,
2788 2789
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_STREAM_FUNC,
2790 2791 2792 2793
    .translateFunc = translateStateDuration,
    .getEnvFunc   = getStateFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = stateDurationFunction,
2794
    .sprocessFunc = stateDurationScalarFunction,
2795 2796
    .finalizeFunc = NULL
  },
G
Ganlin Zhao 已提交
2797 2798 2799
  {
    .name = "csum",
    .type = FUNCTION_TYPE_CSUM,
2800
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
2801
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_CUMULATIVE_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
2802 2803 2804 2805
    .translateFunc = translateCsum,
    .getEnvFunc   = getCsumFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = csumFunction,
2806
    .sprocessFunc = csumScalarFunction,
2807 2808
    .finalizeFunc = NULL,
    .estimateReturnRowsFunc = csumEstReturnRows,
G
Ganlin Zhao 已提交
2809
  },
G
Ganlin Zhao 已提交
2810 2811 2812
  {
    .name = "mavg",
    .type = FUNCTION_TYPE_MAVG,
2813 2814
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_STREAM_FUNC,
G
Ganlin Zhao 已提交
2815 2816
    .translateFunc = translateMavg,
    .getEnvFunc   = getMavgFuncEnv,
G
Ganlin Zhao 已提交
2817
    .initFunc     = mavgFunctionSetup,
G
Ganlin Zhao 已提交
2818
    .processFunc  = mavgFunction,
2819
    .sprocessFunc = mavgScalarFunction,
G
Ganlin Zhao 已提交
2820 2821
    .finalizeFunc = NULL
  },
G
Ganlin Zhao 已提交
2822 2823 2824
  {
    .name = "sample",
    .type = FUNCTION_TYPE_SAMPLE,
2825
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_FILL_FUNC,
G
Ganlin Zhao 已提交
2826 2827 2828 2829
    .translateFunc = translateSample,
    .getEnvFunc   = getSampleFuncEnv,
    .initFunc     = sampleFunctionSetup,
    .processFunc  = sampleFunction,
2830
    .sprocessFunc = sampleScalarFunction,
2831
    .finalizeFunc = sampleFinalize
G
Ganlin Zhao 已提交
2832
  },
G
Ganlin Zhao 已提交
2833 2834 2835
  {
    .name = "tail",
    .type = FUNCTION_TYPE_TAIL,
2836 2837
    .classification = FUNC_MGT_SELECT_FUNC | FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_TIMELINE_FUNC |
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
G
Ganlin Zhao 已提交
2838 2839 2840 2841
    .translateFunc = translateTail,
    .getEnvFunc   = getTailFuncEnv,
    .initFunc     = tailFunctionSetup,
    .processFunc  = tailFunction,
2842
    .sprocessFunc = tailScalarFunction,
G
Ganlin Zhao 已提交
2843
    .finalizeFunc = NULL
G
Ganlin Zhao 已提交
2844
  },
2845 2846 2847
  {
    .name = "unique",
    .type = FUNCTION_TYPE_UNIQUE,
2848
    .classification = FUNC_MGT_SELECT_FUNC | FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_TIMELINE_FUNC |
2849
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
2850 2851 2852 2853
    .translateFunc = translateUnique,
    .getEnvFunc   = getUniqueFuncEnv,
    .initFunc     = uniqueFunctionSetup,
    .processFunc  = uniqueFunction,
2854
    .sprocessFunc = uniqueScalarFunction,
G
Ganlin Zhao 已提交
2855
    .finalizeFunc = NULL
2856
  },
G
Ganlin Zhao 已提交
2857 2858 2859
  {
    .name = "mode",
    .type = FUNCTION_TYPE_MODE,
5
54liuyao 已提交
2860
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
G
Ganlin Zhao 已提交
2861
    .translateFunc = translateMode,
G
Ganlin Zhao 已提交
2862 2863 2864
    .getEnvFunc   = getModeFuncEnv,
    .initFunc     = modeFunctionSetup,
    .processFunc  = modeFunction,
2865
    .sprocessFunc = modeScalarFunction,
G
Ganlin Zhao 已提交
2866
    .finalizeFunc = modeFinalize,
G
Ganlin Zhao 已提交
2867
  },
G
Ganlin Zhao 已提交
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881
  {
    .name = "abs",
    .type = FUNCTION_TYPE_ABS,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInOutNum,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = absFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "log",
    .type = FUNCTION_TYPE_LOG,
    .classification = FUNC_MGT_SCALAR_FUNC,
2882
    .translateFunc = translateLogarithm,
G
Ganlin Zhao 已提交
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = logFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "pow",
    .type = FUNCTION_TYPE_POW,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateIn2NumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = powFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "sqrt",
    .type = FUNCTION_TYPE_SQRT,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = sqrtFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "ceil",
    .type = FUNCTION_TYPE_CEIL,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInOutNum,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = ceilFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "floor",
    .type = FUNCTION_TYPE_FLOOR,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInOutNum,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = floorFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "round",
    .type = FUNCTION_TYPE_ROUND,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInOutNum,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = roundFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "sin",
    .type = FUNCTION_TYPE_SIN,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = sinFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "cos",
    .type = FUNCTION_TYPE_COS,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = cosFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "tan",
    .type = FUNCTION_TYPE_TAN,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = tanFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "asin",
    .type = FUNCTION_TYPE_ASIN,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = asinFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "acos",
    .type = FUNCTION_TYPE_ACOS,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = acosFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "atan",
    .type = FUNCTION_TYPE_ATAN,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = atanFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "length",
    .type = FUNCTION_TYPE_LENGTH,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
    .translateFunc = translateLength,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = lengthFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "char_length",
    .type = FUNCTION_TYPE_CHAR_LENGTH,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
    .translateFunc = translateLength,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = charLengthFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "concat",
    .type = FUNCTION_TYPE_CONCAT,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
    .translateFunc = translateConcat,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = concatFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "concat_ws",
    .type = FUNCTION_TYPE_CONCAT_WS,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
    .translateFunc = translateConcatWs,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = concatWsFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "lower",
    .type = FUNCTION_TYPE_LOWER,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
    .translateFunc = translateInOutStr,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = lowerFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "upper",
    .type = FUNCTION_TYPE_UPPER,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
    .translateFunc = translateInOutStr,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = upperFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "ltrim",
    .type = FUNCTION_TYPE_LTRIM,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
3062
    .translateFunc = translateLtrim,
G
Ganlin Zhao 已提交
3063 3064 3065 3066 3067 3068 3069 3070 3071
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = ltrimFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "rtrim",
    .type = FUNCTION_TYPE_RTRIM,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
3072
    .translateFunc = translateRtrim,
G
Ganlin Zhao 已提交
3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = rtrimFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "substr",
    .type = FUNCTION_TYPE_SUBSTR,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
    .translateFunc = translateSubstr,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = substrFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "cast",
    .type = FUNCTION_TYPE_CAST,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateCast,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = castFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "to_iso8601",
    .type = FUNCTION_TYPE_TO_ISO8601,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateToIso8601,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = toISO8601Function,
    .finalizeFunc = NULL
  },
  {
    .name = "to_unixtimestamp",
    .type = FUNCTION_TYPE_TO_UNIXTIMESTAMP,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateToUnixtimestamp,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = toUnixtimestampFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "timetruncate",
    .type = FUNCTION_TYPE_TIMETRUNCATE,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateTimeTruncate,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = timeTruncateFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "timediff",
    .type = FUNCTION_TYPE_TIMEDIFF,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateTimeDiff,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = timeDiffFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "now",
    .type = FUNCTION_TYPE_NOW,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_DATETIME_FUNC,
3142
    .translateFunc = translateNowToday,
G
Ganlin Zhao 已提交
3143 3144 3145 3146 3147 3148 3149 3150 3151
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = nowFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "today",
    .type = FUNCTION_TYPE_TODAY,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_DATETIME_FUNC,
3152
    .translateFunc = translateNowToday,
G
Ganlin Zhao 已提交
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = todayFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "timezone",
    .type = FUNCTION_TYPE_TIMEZONE,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateTimezone,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = timezoneFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "tbname",
    .type = FUNCTION_TYPE_TBNAME,
3171
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_SCAN_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3172 3173 3174
    .translateFunc = translateTbnameColumn,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
3175
    .sprocessFunc = qTbnameFunction,
G
Ganlin Zhao 已提交
3176 3177 3178
    .finalizeFunc = NULL
  },
  {
3179 3180
    .name = "_qstart",
    .type = FUNCTION_TYPE_QSTART,
3181
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_CLIENT_PC_FUNC,
G
Ganlin Zhao 已提交
3182
    .translateFunc = translateTimePseudoColumn,
3183
    .getEnvFunc   = NULL,
G
Ganlin Zhao 已提交
3184
    .initFunc     = NULL,
3185
    .sprocessFunc = NULL,
G
Ganlin Zhao 已提交
3186 3187 3188
    .finalizeFunc = NULL
  },
  {
3189 3190
    .name = "_qend",
    .type = FUNCTION_TYPE_QEND,
3191
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_CLIENT_PC_FUNC,
G
Ganlin Zhao 已提交
3192
    .translateFunc = translateTimePseudoColumn,
3193
    .getEnvFunc   = NULL,
G
Ganlin Zhao 已提交
3194
    .initFunc     = NULL,
3195
    .sprocessFunc = NULL,
3196 3197 3198 3199 3200
    .finalizeFunc = NULL
  },
  {
    .name = "_qduration",
    .type = FUNCTION_TYPE_QDURATION,
3201
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_CLIENT_PC_FUNC,
3202
    .translateFunc = translateWduration,
3203
    .getEnvFunc   = NULL,
G
Ganlin Zhao 已提交
3204
    .initFunc     = NULL,
3205
    .sprocessFunc = NULL,
G
Ganlin Zhao 已提交
3206 3207 3208
    .finalizeFunc = NULL
  },
  {
3209 3210
    .name = "_wstart",
    .type = FUNCTION_TYPE_WSTART,
3211
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_WINDOW_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3212 3213 3214 3215 3216 3217 3218
    .translateFunc = translateTimePseudoColumn,
    .getEnvFunc   = getTimePseudoFuncEnv,
    .initFunc     = NULL,
    .sprocessFunc = winStartTsFunction,
    .finalizeFunc = NULL
  },
  {
3219 3220
    .name = "_wend",
    .type = FUNCTION_TYPE_WEND,
3221
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_WINDOW_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3222 3223 3224 3225 3226 3227 3228 3229 3230
    .translateFunc = translateTimePseudoColumn,
    .getEnvFunc   = getTimePseudoFuncEnv,
    .initFunc     = NULL,
    .sprocessFunc = winEndTsFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "_wduration",
    .type = FUNCTION_TYPE_WDURATION,
3231
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_WINDOW_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246
    .translateFunc = translateWduration,
    .getEnvFunc   = getTimePseudoFuncEnv,
    .initFunc     = NULL,
    .sprocessFunc = winDurFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "to_json",
    .type = FUNCTION_TYPE_TO_JSON,
    .classification = FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateToJson,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = toJsonFunction,
    .finalizeFunc = NULL
3247 3248 3249 3250
  },
  {
    .name = "_select_value",
    .type = FUNCTION_TYPE_SELECT_VALUE,
3251
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
3252
    .translateFunc = translateSelectValue,
3253 3254
    .getEnvFunc   = getSelectivityFuncEnv,  // todo remove this function later.
    .initFunc     = functionSetup,
3255
    .processFunc  = NULL,
3256 3257 3258
    .finalizeFunc = NULL,
    .pPartialFunc = "_select_value",
    .pMergeFunc   = "_select_value"
3259 3260 3261 3262
  },
  {
    .name = "_block_dist",
    .type = FUNCTION_TYPE_BLOCK_DIST,
5
54liuyao 已提交
3263
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
3264
    .translateFunc = translateBlockDistFunc,
3265
    .getEnvFunc   = getBlockDistFuncEnv,
3266
    .initFunc     = blockDistSetup,
3267 3268
    .processFunc  = blockDistFunction,
    .finalizeFunc = blockDistFinalize
3269 3270 3271 3272 3273 3274
  },
  {
    .name = "_block_dist_info",
    .type = FUNCTION_TYPE_BLOCK_DIST_INFO,
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_SCAN_PC_FUNC,
    .translateFunc = translateBlockDistInfoFunc,
3275 3276 3277
  },
  {
    .name = "_group_key",
3278
    .type = FUNCTION_TYPE_GROUP_KEY,
3279
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
3280 3281 3282 3283
    .translateFunc = translateGroupKey,
    .getEnvFunc   = getGroupKeyFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = groupKeyFunction,
G
Ganlin Zhao 已提交
3284
    .finalizeFunc = groupKeyFinalize,
5
54liuyao 已提交
3285
    .combineFunc  = groupKeyCombine,
3286 3287
    .pPartialFunc = "_group_key",
    .pMergeFunc   = "_group_key"
3288
  },
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324
  {
    .name = "database",
    .type = FUNCTION_TYPE_DATABASE,
    .classification = FUNC_MGT_SYSTEM_INFO_FUNC | FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateDatabaseFunc,
  },
  {
    .name = "client_version",
    .type = FUNCTION_TYPE_CLIENT_VERSION,
    .classification = FUNC_MGT_SYSTEM_INFO_FUNC | FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateClientVersionFunc,
  },
  {
    .name = "server_version",
    .type = FUNCTION_TYPE_SERVER_VERSION,
    .classification = FUNC_MGT_SYSTEM_INFO_FUNC | FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateServerVersionFunc,
  },
  {
    .name = "server_status",
    .type = FUNCTION_TYPE_SERVER_STATUS,
    .classification = FUNC_MGT_SYSTEM_INFO_FUNC | FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateServerStatusFunc,
  },
  {
    .name = "current_user",
    .type = FUNCTION_TYPE_CURRENT_USER,
    .classification = FUNC_MGT_SYSTEM_INFO_FUNC | FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateCurrentUserFunc,
  },
  {
    .name = "user",
    .type = FUNCTION_TYPE_USER,
    .classification = FUNC_MGT_SYSTEM_INFO_FUNC | FUNC_MGT_SCALAR_FUNC,
    .translateFunc = translateUserFunc,
  },
3325 3326 3327
  {
    .name = "_irowts",
    .type = FUNCTION_TYPE_IROWTS,
3328
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_INTERP_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
3329 3330 3331 3332 3333 3334
    .translateFunc = translateTimePseudoColumn,
    .getEnvFunc   = getTimePseudoFuncEnv,
    .initFunc     = NULL,
    .sprocessFunc = NULL,
    .finalizeFunc = NULL
  },
3335 3336 3337 3338 3339 3340 3341 3342 3343 3344
  {
    .name = "_isfilled",
    .type = FUNCTION_TYPE_ISFILLED,
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_INTERP_PC_FUNC,
    .translateFunc = translateIsFilledPseudoColumn,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = NULL,
    .finalizeFunc = NULL
  },
3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
  {
    .name = "_tags",
    .type = FUNCTION_TYPE_TAGS,
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_MULTI_RES_FUNC,
    .translateFunc = translateTagsPseudoColumn,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = NULL,
    .finalizeFunc = NULL
  },
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
  {
    .name = "_table_count",
    .type = FUNCTION_TYPE_TABLE_COUNT,
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_SCAN_PC_FUNC,
    .translateFunc = translateTableCountPseudoColumn,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = NULL,
    .finalizeFunc = NULL
  },
G
Ganlin Zhao 已提交
3365
};
X
Xiaoyu Wang 已提交
3366
// clang-format on
3367

X
Xiaoyu Wang 已提交
3368
const int32_t funcMgtBuiltinsNum = (sizeof(funcMgtBuiltins) / sizeof(SBuiltinFuncDefinition));