builtins.c 126.5 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"
D
Dingle Zhang 已提交
21
#include "geomFunc.h"
22
#include "taoserror.h"
G
Ganlin Zhao 已提交
23
#include "ttime.h"
24

25 26 27 28 29 30 31 32 33
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) {
34
  return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_NUM, "Invalid number of parameters : %s", pFuncName);
35 36 37
}

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

41 42
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);
43 44
}

45
#define TIME_UNIT_INVALID   1
46 47 48 49 50 51 52
#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 已提交
53 54
  if (TSDB_TIME_PRECISION_MILLI == dbPrec &&
      (0 == strcasecmp(pVal->literal, "1u") || 0 == strcasecmp(pVal->literal, "1b"))) {
55 56 57 58
    return TIME_UNIT_TOO_SMALL;
  }

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

X
Xiaoyu Wang 已提交
62 63 64
  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')) {
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 195
    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;
}

196 197 198 199 200 201 202
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 已提交
203
  for (int32_t i = startPos; i < len || i >= 0; i += step) {
204 205 206 207 208 209 210 211 212
    if (!isspace(str[i])) {
      break;
    }
    numOfSpaces++;
  }

  return numOfSpaces;
}

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

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

227 228 229 230 231 232 233 234 235 236 237
  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 已提交
238
  return TSDB_CODE_SUCCESS;
239 240
}

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

247 248 249
  pVal->literal = NULL;
  pVal->isDuration = false;
  pVal->translate = true;
250
  pVal->notReserved = true;
251 252 253 254 255 256 257
  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 已提交
258
  return TSDB_CODE_SUCCESS;
259 260
}

261 262 263 264 265 266 267
// 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;
268
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
269
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
270 271
  } else if (IS_NULL_TYPE(paraType)) {
    paraType = TSDB_DATA_TYPE_BIGINT;
272 273
  }

X
Xiaoyu Wang 已提交
274
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[paraType].bytes, .type = paraType};
275 276 277 278 279 280 281 282 283 284
  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;
285
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
286 287 288
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
289
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
290 291 292 293 294 295 296 297 298 299 300
  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;
301 302
  if ((!IS_NUMERIC_TYPE(para1Type) && !IS_NULL_TYPE(para1Type)) ||
      (!IS_NUMERIC_TYPE(para2Type) && !IS_NULL_TYPE(para2Type))) {
303 304 305
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

X
Xiaoyu Wang 已提交
306
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes, .type = TSDB_DATA_TYPE_DOUBLE};
307 308 309 310 311 312 313 314 315 316
  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);
317
  if (!IS_STR_DATA_TYPE(pPara1->resType.type)) {
318 319 320
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

325 326 327 328 329 330
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);
331
  if (!IS_STR_DATA_TYPE(pPara1->resType.type)) {
332 333 334 335
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  int32_t numOfSpaces = 0;
336
  SNode*  pParamNode1 = nodesListGetNode(pFunc->pParameterList, 0);
337 338 339 340
  // 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) {
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    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);
}

358 359 360 361 362 363 364
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;
365
  if (!IS_NUMERIC_TYPE(para1Type) && !IS_NULL_TYPE(para1Type)) {
366 367 368 369 370
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  if (2 == numOfParams) {
    uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
371
    if (!IS_NUMERIC_TYPE(para2Type) && !IS_NULL_TYPE(para2Type)) {
372 373 374 375 376 377 378 379
      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;
}

380 381 382 383
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 已提交
384
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
385 386 387 388 389 390 391 392 393
  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;
394
  if (!IS_NUMERIC_TYPE(paraType) && !IS_NULL_TYPE(paraType)) {
395 396 397 398
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

  uint8_t resType = 0;
399
  if (IS_SIGNED_NUMERIC_TYPE(paraType) || TSDB_DATA_TYPE_BOOL == paraType || IS_NULL_TYPE(paraType)) {
400 401 402 403 404 405
    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;
  }
406

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

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

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

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

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

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

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

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

493 494
  pFunc->node.resType =
      (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes, .type = TSDB_DATA_TYPE_TIMESTAMP};
495 496 497 498 499 500 501
  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};
502 503 504
  return TSDB_CODE_SUCCESS;
}

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

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

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

521 522 523 524 525
  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 已提交
526
    if (!IS_NUMERIC_TYPE(paraType)) {
527 528
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
G
Ganlin Zhao 已提交
529 530 531 532 533 534 535 536 537 538 539

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

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

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

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

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

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

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

578 579 580 581 582 583
  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 已提交
584
  // param2
585
  if (3 == numOfParams) {
586
    uint8_t para3Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type;
587
    if (!IS_STR_DATA_TYPE(para3Type)) {
588 589 590 591
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

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

    pValue = (SValueNode*)pParamNode2;
    pValue->notReserved = true;
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 633
  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;
634
      if (!IS_STR_DATA_TYPE(para3Type)) {
635 636 637 638 639 640 641 642 643 644 645 646 647
        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 已提交
648 649
    pFunc->node.resType =
        (SDataType){.bytes = getApercentileMaxSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
650
  } else {
651 652
    // original percent param is reserved
    if (2 != numOfParams) {
653 654 655
      return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
    }
    uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
656 657
    uint8_t para2Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type;
    if (TSDB_DATA_TYPE_BINARY != para1Type || !IS_INTEGER_TYPE(para2Type)) {
658 659 660 661
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

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

664 665 666
  return TSDB_CODE_SUCCESS;
}

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

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

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

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

688 689 690
  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)) {
691 692 693
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

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

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

709 710
  pValue->notReserved = true;

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

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

725 726 727 728 729 730 731 732
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);
}

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

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

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

747 748 749 750 751 752 753
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) {
754
    if (!IS_NUMERIC_TYPE(paraType) && !IS_TIMESTAMP_TYPE(paraType)) {
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
      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);
}
771

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

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

X
Xiaoyu Wang 已提交
782 783 784 785
  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 已提交
786 787
  }

X
Xiaoyu Wang 已提交
788
  // param1
789 790 791 792 793 794 795 796 797 798
  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 已提交
799
    if (!IS_INTEGER_TYPE(pValue->node.resType.type)) {
800 801
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
802

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

805
    int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1));
806
    if (ret == TIME_UNIT_TOO_SMALL) {
807 808
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
                             "ELAPSED function time unit parameter should be greater than db precision");
809
    } else if (ret == TIME_UNIT_INVALID) {
X
Xiaoyu Wang 已提交
810 811 812
      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]");
813
    }
814 815
  }

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

820 821 822 823 824 825 826 827 828
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;
829
    if (!IS_TIMESTAMP_TYPE(paraType)) {
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
      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 已提交
855 856
    pFunc->node.resType =
        (SDataType){.bytes = getElapsedInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
  } 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) {
872
#if 0
873
  return translateElapsedImpl(pFunc, pErrBuf, len, true);
874 875
#endif
  return 0;
876 877 878
}

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

885 886 887 888 889 890 891
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) {
892
    SNode* pParamNode = nodesListGetNode(pFunc->pParameterList, i);
X
Xiaoyu Wang 已提交
893
    if (i > 0) {  // param1 & param2
894 895 896 897 898 899 900 901 902
      if (QUERY_NODE_VALUE != nodeType(pParamNode)) {
        return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
      }

      SValueNode* pValue = (SValueNode*)pParamNode;

      pValue->notReserved = true;
    }

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

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

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
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) {
931 932 933 934 935 936 937
  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";
938 939 940 941 942 943 944 945 946

  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);
947
      cJSON_Delete(binDesc);
948 949 950 951 952 953 954 955 956 957 958
      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);
959
      cJSON_Delete(binDesc);
960 961 962 963 964
      return false;
    }

    if (count->valueint <= 0 || count->valueint > 1000) {  // limit count to 1000
      snprintf(errMsg, msgLen, "%s", msg4);
965
      cJSON_Delete(binDesc);
966 967 968 969 970 971
      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);
972
      cJSON_Delete(binDesc);
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
      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);
991
        cJSON_Delete(binDesc);
992 993 994 995 996 997 998
        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);
999
          cJSON_Delete(binDesc);
1000 1001 1002 1003 1004 1005 1006 1007 1008
          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);
1009
        cJSON_Delete(binDesc);
1010 1011 1012 1013 1014
        return false;
      }
      if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) {
        snprintf(errMsg, msgLen, "%s", msg8);
        taosMemoryFree(intervals);
1015
        cJSON_Delete(binDesc);
1016 1017 1018 1019 1020 1021 1022
        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);
1023
          cJSON_Delete(binDesc);
1024 1025 1026 1027 1028 1029 1030
          return false;
        }
        startIndex++;
      }
    } else {
      snprintf(errMsg, msgLen, "%s", msg3);
      taosMemoryFree(intervals);
1031
      cJSON_Delete(binDesc);
1032 1033 1034 1035 1036 1037 1038
      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 已提交
1039 1040 1041 1042
      if (numOfBins < 4) {
        return false;
      }

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

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

1091
static int32_t translateHistogram(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1092 1093
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (4 != numOfParams) {
1094 1095 1096 1097 1098 1099 1100 1101
    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 已提交
1102
  // param1 ~ param3
1103 1104
  if (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type != TSDB_DATA_TYPE_BINARY ||
      ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_BINARY ||
1105
      !IS_INTEGER_TYPE(((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type)) {
1106 1107 1108
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

1109 1110
  int8_t binType;
  char*  binDesc;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  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;

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
    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 已提交
1131
      char errMsg[128] = {0};
1132 1133 1134 1135 1136 1137
      binDesc = varDataVal(pValue->datum.p);
      if (!validateHistogramBinDesc(binDesc, binType, errMsg, (int32_t)sizeof(errMsg))) {
        return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, errMsg);
      }
    }

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

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

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
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
1161 1162
    if (((SExprNode*)nodesListGetNode(pFunc->pParameterList, 1))->resType.type != TSDB_DATA_TYPE_BINARY ||
        ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 2))->resType.type != TSDB_DATA_TYPE_BINARY ||
1163
        !IS_INTEGER_TYPE(((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type)) {
1164 1165 1166
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }

1167 1168
    int8_t binType;
    char*  binDesc;
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    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;

1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
      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);
        }
      }

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

X
Xiaoyu Wang 已提交
1202 1203
    pFunc->node.resType =
        (SDataType){.bytes = getHistogramInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
  } 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);
}
1221

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

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

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

1235 1236 1237 1238 1239 1240
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 已提交
1241 1242
    pFunc->node.resType =
        (SDataType){.bytes = getHistogramInfoSize() + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
  } 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);
}

1258 1259 1260 1261
static bool validateStateOper(const SValueNode* pVal) {
  if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) {
    return false;
  }
1262 1263 1264 1265
  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));
1266 1267
}

1268
static int32_t translateStateCount(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1269 1270
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (3 != numOfParams) {
1271 1272 1273 1274 1275 1276 1277 1278
    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 已提交
1279
  // param1 & param2
1280 1281 1282 1283 1284 1285 1286 1287
  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;

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

1294 1295 1296
    pValue->notReserved = true;
  }

1297 1298 1299 1300 1301 1302
  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 已提交
1303
  // set result type
1304
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
1305 1306 1307
  return TSDB_CODE_SUCCESS;
}

1308
static int32_t translateStateDuration(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1309 1310
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (3 != numOfParams && 4 != numOfParams) {
1311 1312 1313 1314 1315 1316 1317 1318
    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 已提交
1319
  // param1, param2 & param3
1320 1321 1322 1323 1324 1325 1326 1327
  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;

1328 1329
    if (i == 1 && !validateStateOper(pValue)) {
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
1330
                             "Second parameter of STATEDURATION function"
1331
                             "must be one of the following: 'GE', 'GT', 'LE', 'LT', 'EQ', 'NE'");
1332 1333 1334
    } 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");
1335 1336
    }

1337 1338 1339
    pValue->notReserved = true;
  }

1340 1341 1342 1343 1344 1345
  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 已提交
1346 1347
  if (numOfParams == 4 &&
      ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type != TSDB_DATA_TYPE_BIGINT) {
1348 1349 1350
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

1354
    int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 3));
1355 1356 1357 1358 1359
    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 已提交
1360 1361
                             "STATEDURATION function time unit parameter should be one of the following: [1b, 1u, 1a, "
                             "1s, 1m, 1h, 1d, 1w]");
1362 1363 1364
    }
  }

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

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

  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 已提交
1387
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
G
Ganlin Zhao 已提交
1388 1389 1390
    }
  }

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

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

G
Ganlin Zhao 已提交
1397 1398 1399 1400 1401 1402
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;
1403

X
Xiaoyu Wang 已提交
1404
  // param1
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
  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 已提交
1417 1418 1419 1420 1421 1422 1423 1424 1425
  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 已提交
1426 1427 1428 1429 1430
static int32_t translateSample(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (2 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

X
Xiaoyu Wang 已提交
1434
  // param1
1435 1436 1437 1438 1439 1440 1441 1442
  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 已提交
1443 1444
  }

1445 1446
  pValue->notReserved = true;

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

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

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

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

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

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

    SValueNode* pValue = (SValueNode*)pParamNode;

1480 1481
    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) {
1482
      return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR,
X
Xiaoyu Wang 已提交
1483 1484
                             "TAIL function second parameter should be in range [1, 100], "
                             "third parameter should be in range [0, 100]");
1485 1486 1487 1488
    }

    pValue->notReserved = true;

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

X
Xiaoyu Wang 已提交
1495
  // set result type
1496
  if (IS_STR_DATA_TYPE(colType)) {
G
Ganlin Zhao 已提交
1497 1498 1499 1500 1501 1502 1503
    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;
}

1504 1505 1506 1507 1508 1509 1510 1511
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
1512 1513
  SNode*      pParamNode1 = nodesListGetNode(pFunc->pParameterList, 1);
  SValueNode* pValue1 = (SValueNode*)pParamNode1;
1514 1515 1516 1517
  if (QUERY_NODE_VALUE != nodeType(pParamNode1)) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

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

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

1529
  SNode*      pParamNode2 = nodesListGetNode(pFunc->pParameterList, 2);
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
  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;
}

1541 1542 1543 1544 1545
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 已提交
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
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);
  }

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

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

G
Ganlin Zhao 已提交
1568 1569 1570 1571
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;

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

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

1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
  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 已提交
1594 1595

    pValue->notReserved = true;
1596 1597
  }

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

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

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

  if (4 == numOfParams) {
1621 1622
    nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, 3));
    paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 3))->resType.type;
G
Ganlin Zhao 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    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 已提交
1637
#endif
G
Ganlin Zhao 已提交
1638 1639 1640 1641 1642

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

1643 1644 1645 1646 1647 1648 1649 1650 1651
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;
  }
}

1652
static int32_t translateFirstLast(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
1653 1654 1655 1656
  // 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) {
1657
    uint8_t nodeType = nodeType(nodesListGetNode(pFunc->pParameterList, i));
1658
    uint8_t paraType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, i))->resType.type;
1659
    if (IS_NULL_TYPE(paraType) && QUERY_NODE_VALUE == nodeType) {
1660 1661 1662 1663
      return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
    }
  }

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

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

1683 1684
    pFunc->node.resType =
        (SDataType){.bytes = getFirstLastInfoSize(paraBytes) + VARSTR_HEADER_SIZE, .type = TSDB_DATA_TYPE_BINARY};
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
  } 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 已提交
1703
static int32_t translateUniqueMode(SFunctionNode* pFunc, char* pErrBuf, int32_t len, bool isUnique) {
G
Ganlin Zhao 已提交
1704
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
1705
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
G
Ganlin Zhao 已提交
1706 1707
  }

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

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

G
Ganlin Zhao 已提交
1718 1719 1720 1721 1722 1723 1724 1725
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);
}

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

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

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

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

1750 1751 1752 1753 1754 1755 1756
    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;
1757
  }
1758

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

1769 1770 1771 1772 1773 1774 1775 1776
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;
}

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

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

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

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

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

1801
  // concat_ws separator should be constant string
1802 1803 1804 1805 1806 1807 1808 1809
  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");
    }
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

1891
  pFunc->node.resType = (SDataType){.bytes = pPara0->resType.bytes, .type = pPara0->resType.type};
1892 1893 1894 1895 1896
  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
1897

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

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

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

1917 1918 1919 1920
  return TSDB_CODE_SUCCESS;
}

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

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

1932 1933 1934 1935 1936 1937 1938 1939 1940
  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;
    }
  }

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

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

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

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

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

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

1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
  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");
    }
  }

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

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

static int32_t translateTimeTruncate(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
2002 2003
  int32_t numOfParams = LIST_LENGTH(pFunc->pParameterList);
  if (2 != numOfParams && 3 != numOfParams) {
2004 2005 2006 2007 2008
    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;
2009
  if ((!IS_STR_DATA_TYPE(para1Type) && !IS_INTEGER_TYPE(para1Type) && !IS_TIMESTAMP_TYPE(para1Type)) ||
X
Xiaoyu Wang 已提交
2010
      !IS_INTEGER_TYPE(para2Type)) {
2011 2012 2013
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

2014
  uint8_t dbPrec = pFunc->node.resType.precision;
2015
  int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 1));
2016 2017 2018 2019
  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 已提交
2020 2021 2022
    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]");
2023 2024
  }

2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
  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 已提交
2038 2039 2040 2041
  int32_t code = addDbPrecisonParam(&pFunc->pParameterList, dbPrec);
  if (code != TSDB_CODE_SUCCESS) {
    return code;
  }
2042

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

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

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

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

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

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

G
Ganlin Zhao 已提交
2076
  if (3 == numOfParams) {
2077
    int32_t ret = validateTimeUnitParam(dbPrec, (SValueNode*)nodesListGetNode(pFunc->pParameterList, 2));
G
Ganlin Zhao 已提交
2078 2079 2080 2081
    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 已提交
2082 2083 2084
      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 已提交
2085
    }
2086 2087
  }

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

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

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

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

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

D
Dingle Zhang 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
static int32_t translateInStrOutGeom(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  if (1 != LIST_LENGTH(pFunc->pParameterList)) {
    return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

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

  return TSDB_CODE_SUCCESS;
}

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

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

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

  return TSDB_CODE_SUCCESS;
}

static int32_t translateIn2NumOutGeom(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;
  if ((!IS_NUMERIC_TYPE(para1Type) && !IS_NULL_TYPE(para1Type)) ||
      (!IS_NUMERIC_TYPE(para2Type) && !IS_NULL_TYPE(para2Type))) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

  return TSDB_CODE_SUCCESS;
}

static int32_t translateIn2GeomOutBool(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;
  if ((para1Type != TSDB_DATA_TYPE_GEOMETRY && !IS_NULL_TYPE(para1Type)) ||
      (para2Type != TSDB_DATA_TYPE_GEOMETRY && !IS_NULL_TYPE(para2Type))) {
    return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
  }

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

  return TSDB_CODE_SUCCESS;
}

2175 2176 2177 2178 2179
static int32_t translateSelectValue(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
  pFunc->node.resType = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType;
  return TSDB_CODE_SUCCESS;
}

2180
static int32_t translateBlockDistFunc(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
X
Xiaoyu Wang 已提交
2181
  pFunc->node.resType = (SDataType){.bytes = 128, .type = TSDB_DATA_TYPE_VARCHAR};
2182 2183 2184
  return TSDB_CODE_SUCCESS;
}

2185 2186 2187 2188 2189
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;
}

2190 2191 2192 2193 2194
static bool getBlockDistFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) {
  pEnv->calcMemSize = sizeof(STableBlockDistInfo);
  return true;
}

2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
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;
}

2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
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;
}

2235 2236 2237 2238 2239
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;
}

2240
static int32_t translateTableCountPseudoColumn(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
X
Xiaoyu Wang 已提交
2241
  pFunc->node.resType = (SDataType){.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes, .type = TSDB_DATA_TYPE_BIGINT};
2242 2243 2244
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
2245
// clang-format off
2246
const SBuiltinFuncDefinition funcMgtBuiltins[] = {
G
Ganlin Zhao 已提交
2247 2248 2249 2250 2251 2252 2253 2254 2255
  {
    .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,
2256
    .sprocessFunc = countScalarFunction,
5
54liuyao 已提交
2257
    .finalizeFunc = functionFinalize,
5
54liuyao 已提交
2258
    .invertFunc   = countInvertFunction,
2259 2260 2261
    .combineFunc  = combineFunction,
    .pPartialFunc = "count",
    .pMergeFunc   = "sum"
G
Ganlin Zhao 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
  },
  {
    .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,
2272
    .sprocessFunc = sumScalarFunction,
5
54liuyao 已提交
2273
    .finalizeFunc = functionFinalize,
5
54liuyao 已提交
2274
    .invertFunc   = sumInvertFunction,
2275 2276 2277
    .combineFunc  = sumCombine,
    .pPartialFunc = "sum",
    .pMergeFunc   = "sum"
G
Ganlin Zhao 已提交
2278 2279 2280 2281
  },
  {
    .name = "min",
    .type = FUNCTION_TYPE_MIN,
2282
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED | FUNC_MGT_SELECT_FUNC,
2283
    .translateFunc = translateInOutNum,
G
Ganlin Zhao 已提交
2284 2285
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getMinmaxFuncEnv,
2286
    .initFunc     = minmaxFunctionSetup,
G
Ganlin Zhao 已提交
2287
    .processFunc  = minFunction,
2288
    .sprocessFunc = minScalarFunction,
5
54liuyao 已提交
2289
    .finalizeFunc = minmaxFunctionFinalize,
2290 2291 2292
    .combineFunc  = minCombine,
    .pPartialFunc = "min",
    .pMergeFunc   = "min"
G
Ganlin Zhao 已提交
2293 2294 2295 2296
  },
  {
    .name = "max",
    .type = FUNCTION_TYPE_MAX,
2297
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED | FUNC_MGT_SELECT_FUNC,
2298
    .translateFunc = translateInOutNum,
G
Ganlin Zhao 已提交
2299 2300
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getMinmaxFuncEnv,
2301
    .initFunc     = minmaxFunctionSetup,
G
Ganlin Zhao 已提交
2302
    .processFunc  = maxFunction,
2303
    .sprocessFunc = maxScalarFunction,
5
54liuyao 已提交
2304
    .finalizeFunc = minmaxFunctionFinalize,
2305 2306 2307
    .combineFunc  = maxCombine,
    .pPartialFunc = "max",
    .pMergeFunc   = "max"
G
Ganlin Zhao 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316
  },
  {
    .name = "stddev",
    .type = FUNCTION_TYPE_STDDEV,
    .classification = FUNC_MGT_AGG_FUNC,
    .translateFunc = translateInNumOutDou,
    .getEnvFunc   = getStddevFuncEnv,
    .initFunc     = stddevFunctionSetup,
    .processFunc  = stddevFunction,
2317
    .sprocessFunc = stddevScalarFunction,
5
54liuyao 已提交
2318
    .finalizeFunc = stddevFinalize,
5
54liuyao 已提交
2319 2320
    .invertFunc   = stddevInvertFunction,
    .combineFunc  = stddevCombine,
G
Ganlin Zhao 已提交
2321 2322
    .pPartialFunc = "_stddev_partial",
    .pMergeFunc   = "_stddev_merge"
G
Ganlin Zhao 已提交
2323
  },
2324 2325 2326 2327 2328 2329 2330 2331
  {
    .name = "_stddev_partial",
    .type = FUNCTION_TYPE_STDDEV_PARTIAL,
    .classification = FUNC_MGT_AGG_FUNC,
    .translateFunc = translateStddevPartial,
    .getEnvFunc   = getStddevFuncEnv,
    .initFunc     = stddevFunctionSetup,
    .processFunc  = stddevFunction,
G
Ganlin Zhao 已提交
2332
    .finalizeFunc = stddevPartialFinalize,
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
    .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 已提交
2343
    .processFunc  = stddevFunctionMerge,
2344 2345 2346 2347
    .finalizeFunc = stddevFinalize,
    .invertFunc   = stddevInvertFunction,
    .combineFunc  = stddevCombine,
  },
2348 2349 2350
  {
    .name = "leastsquares",
    .type = FUNCTION_TYPE_LEASTSQUARES,
D
dapan1121 已提交
2351
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
2352 2353 2354 2355
    .translateFunc = translateLeastSQR,
    .getEnvFunc   = getLeastSQRFuncEnv,
    .initFunc     = leastSQRFunctionSetup,
    .processFunc  = leastSQRFunction,
2356
    .sprocessFunc = leastSQRScalarFunction,
2357
    .finalizeFunc = leastSQRFinalize,
5
54liuyao 已提交
2358 2359
    .invertFunc   = NULL,
    .combineFunc  = leastSQRCombine,
2360
  },
G
Ganlin Zhao 已提交
2361 2362 2363
  {
    .name = "avg",
    .type = FUNCTION_TYPE_AVG,
2364
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
G
Ganlin Zhao 已提交
2365
    .translateFunc = translateInNumOutDou,
H
Haojun Liao 已提交
2366
    .dataRequiredFunc = statisDataRequired,
G
Ganlin Zhao 已提交
2367 2368 2369
    .getEnvFunc   = getAvgFuncEnv,
    .initFunc     = avgFunctionSetup,
    .processFunc  = avgFunction,
2370
    .sprocessFunc = avgScalarFunction,
5
54liuyao 已提交
2371
    .finalizeFunc = avgFinalize,
5
54liuyao 已提交
2372 2373
    .invertFunc   = avgInvertFunction,
    .combineFunc  = avgCombine,
G
Ganlin Zhao 已提交
2374 2375
    .pPartialFunc = "_avg_partial",
    .pMergeFunc   = "_avg_merge"
G
Ganlin Zhao 已提交
2376
  },
2377 2378 2379 2380
  {
    .name = "_avg_partial",
    .type = FUNCTION_TYPE_AVG_PARTIAL,
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2381
    .translateFunc = translateAvgPartial,
2382
    .dataRequiredFunc = statisDataRequired,
2383 2384 2385
    .getEnvFunc   = getAvgFuncEnv,
    .initFunc     = avgFunctionSetup,
    .processFunc  = avgFunction,
G
Ganlin Zhao 已提交
2386
    .finalizeFunc = avgPartialFinalize,
2387 2388 2389 2390 2391 2392 2393
    .invertFunc   = avgInvertFunction,
    .combineFunc  = avgCombine,
  },
  {
    .name = "_avg_merge",
    .type = FUNCTION_TYPE_AVG_MERGE,
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2394
    .translateFunc = translateAvgMerge,
2395 2396
    .getEnvFunc   = getAvgFuncEnv,
    .initFunc     = avgFunctionSetup,
G
Ganlin Zhao 已提交
2397
    .processFunc  = avgFunctionMerge,
2398 2399 2400 2401
    .finalizeFunc = avgFinalize,
    .invertFunc   = avgInvertFunction,
    .combineFunc  = avgCombine,
  },
G
Ganlin Zhao 已提交
2402 2403 2404
  {
    .name = "percentile",
    .type = FUNCTION_TYPE_PERCENTILE,
G
Ganlin Zhao 已提交
2405
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_REPEAT_SCAN_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED | FUNC_MGT_FORBID_STREAM_FUNC,
G
Ganlin Zhao 已提交
2406
    .translateFunc = translatePercentile,
G
Ganlin Zhao 已提交
2407
    .dataRequiredFunc = statisDataRequired,
G
Ganlin Zhao 已提交
2408 2409 2410
    .getEnvFunc   = getPercentileFuncEnv,
    .initFunc     = percentileFunctionSetup,
    .processFunc  = percentileFunction,
2411
    .sprocessFunc = percentileScalarFunction,
2412 2413 2414
    .finalizeFunc = percentileFinalize,
    .invertFunc   = NULL,
    .combineFunc  = NULL,
G
Ganlin Zhao 已提交
2415 2416 2417 2418
  },
  {
    .name = "apercentile",
    .type = FUNCTION_TYPE_APERCENTILE,
2419
    .classification = FUNC_MGT_AGG_FUNC,
2420
    .translateFunc = translateApercentile,
2421 2422 2423
    .getEnvFunc   = getApercentileFuncEnv,
    .initFunc     = apercentileFunctionSetup,
    .processFunc  = apercentileFunction,
2424
    .sprocessFunc = apercentileScalarFunction,
G
Ganlin Zhao 已提交
2425
    .finalizeFunc = apercentileFinalize,
2426
    .invertFunc   = NULL,
2427
    .combineFunc  = apercentileCombine,
G
Ganlin Zhao 已提交
2428
    .pPartialFunc = "_apercentile_partial",
2429 2430
    .pMergeFunc   = "_apercentile_merge",
    .createMergeParaFuc = apercentileCreateMergeParam
2431 2432 2433
  },
  {
    .name = "_apercentile_partial",
2434
    .type = FUNCTION_TYPE_APERCENTILE_PARTIAL,
2435 2436 2437 2438 2439
    .classification = FUNC_MGT_AGG_FUNC,
    .translateFunc = translateApercentilePartial,
    .getEnvFunc   = getApercentileFuncEnv,
    .initFunc     = apercentileFunctionSetup,
    .processFunc  = apercentileFunction,
2440 2441 2442
    .finalizeFunc = apercentilePartialFinalize,
    .invertFunc   = NULL,
    .combineFunc = apercentileCombine,
2443 2444 2445 2446
  },
  {
    .name = "_apercentile_merge",
    .type = FUNCTION_TYPE_APERCENTILE_MERGE,
2447
    .classification = FUNC_MGT_AGG_FUNC,
2448
    .translateFunc = translateApercentileMerge,
2449
    .getEnvFunc   = getApercentileFuncEnv,
2450
    .initFunc     = apercentileFunctionSetup,
G
Ganlin Zhao 已提交
2451
    .processFunc  = apercentileFunctionMerge,
2452 2453 2454
    .finalizeFunc = apercentileFinalize,
    .invertFunc   = NULL,
    .combineFunc = apercentileCombine,
G
Ganlin Zhao 已提交
2455 2456 2457 2458
  },
  {
    .name = "top",
    .type = FUNCTION_TYPE_TOP,
D
dapan1121 已提交
2459 2460
    .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 已提交
2461 2462
    .translateFunc = translateTopBot,
    .getEnvFunc   = getTopBotFuncEnv,
G
Ganlin Zhao 已提交
2463
    .initFunc     = topBotFunctionSetup,
G
Ganlin Zhao 已提交
2464
    .processFunc  = topFunction,
G
Ganlin Zhao 已提交
2465
    .sprocessFunc = topBotScalarFunction,
G
Ganlin Zhao 已提交
2466 2467
    .finalizeFunc = topBotFinalize,
    .combineFunc  = topCombine,
2468 2469
    .pPartialFunc = "top",
    .pMergeFunc   = "top",
2470
    .createMergeParaFuc = topBotCreateMergeParam
G
Ganlin Zhao 已提交
2471 2472 2473 2474
  },
  {
    .name = "bottom",
    .type = FUNCTION_TYPE_BOTTOM,
D
dapan1121 已提交
2475 2476
    .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 已提交
2477
    .translateFunc = translateTopBot,
2478
    .getEnvFunc   = getTopBotFuncEnv,
2479
    .initFunc     = topBotFunctionSetup,
2480
    .processFunc  = bottomFunction,
G
Ganlin Zhao 已提交
2481
    .sprocessFunc = topBotScalarFunction,
2482 2483
    .finalizeFunc = topBotFinalize,
    .combineFunc  = bottomCombine,
2484 2485
    .pPartialFunc = "bottom",
    .pMergeFunc   = "bottom",
2486
    .createMergeParaFuc = topBotCreateMergeParam
G
Ganlin Zhao 已提交
2487 2488 2489 2490
  },
  {
    .name = "spread",
    .type = FUNCTION_TYPE_SPREAD,
2491
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
G
Ganlin Zhao 已提交
2492 2493 2494 2495 2496
    .translateFunc = translateSpread,
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getSpreadFuncEnv,
    .initFunc     = spreadFunctionSetup,
    .processFunc  = spreadFunction,
2497
    .sprocessFunc = spreadScalarFunction,
G
Ganlin Zhao 已提交
2498
    .finalizeFunc = spreadFinalize,
2499 2500
    .invertFunc   = NULL,
    .combineFunc  = spreadCombine,
G
Ganlin Zhao 已提交
2501 2502
    .pPartialFunc = "_spread_partial",
    .pMergeFunc   = "_spread_merge"
G
Ganlin Zhao 已提交
2503
  },
2504 2505 2506 2507
  {
    .name = "_spread_partial",
    .type = FUNCTION_TYPE_SPREAD_PARTIAL,
    .classification = FUNC_MGT_AGG_FUNC,
2508
    .translateFunc = translateSpreadPartial,
2509 2510 2511 2512
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getSpreadFuncEnv,
    .initFunc     = spreadFunctionSetup,
    .processFunc  = spreadFunction,
2513 2514 2515
    .finalizeFunc = spreadPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = spreadCombine,
2516 2517 2518 2519 2520
  },
  {
    .name = "_spread_merge",
    .type = FUNCTION_TYPE_SPREAD_MERGE,
    .classification = FUNC_MGT_AGG_FUNC,
2521
    .translateFunc = translateSpreadMerge,
2522 2523 2524
    .dataRequiredFunc = statisDataRequired,
    .getEnvFunc   = getSpreadFuncEnv,
    .initFunc     = spreadFunctionSetup,
G
Ganlin Zhao 已提交
2525
    .processFunc  = spreadFunctionMerge,
2526 2527 2528
    .finalizeFunc = spreadFinalize,
    .invertFunc   = NULL,
    .combineFunc  = spreadCombine,
2529
  },
G
Ganlin Zhao 已提交
2530 2531 2532
  {
    .name = "elapsed",
    .type = FUNCTION_TYPE_ELAPSED,
D
dapan1121 已提交
2533 2534
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_INTERVAL_INTERPO_FUNC | FUNC_MGT_FORBID_STREAM_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC | FUNC_MGT_SPECIAL_DATA_REQUIRED,
G
Ganlin Zhao 已提交
2535 2536 2537 2538 2539
    .dataRequiredFunc = statisDataRequired,
    .translateFunc = translateElapsed,
    .getEnvFunc   = getElapsedFuncEnv,
    .initFunc     = elapsedFunctionSetup,
    .processFunc  = elapsedFunction,
G
Ganlin Zhao 已提交
2540
    .finalizeFunc = elapsedFinalize,
2541 2542
    .invertFunc   = NULL,
    .combineFunc  = elapsedCombine,
G
Ganlin Zhao 已提交
2543
  },
2544 2545 2546 2547 2548 2549 2550 2551 2552
  {
    .name = "_elapsed_partial",
    .type = FUNCTION_TYPE_ELAPSED,
    .classification = FUNC_MGT_AGG_FUNC,
    .dataRequiredFunc = statisDataRequired,
    .translateFunc = translateElapsedPartial,
    .getEnvFunc   = getElapsedFuncEnv,
    .initFunc     = elapsedFunctionSetup,
    .processFunc  = elapsedFunction,
2553 2554 2555
    .finalizeFunc = elapsedPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = elapsedCombine,
2556 2557 2558 2559 2560 2561 2562 2563 2564
  },
  {
    .name = "_elapsed_merge",
    .type = FUNCTION_TYPE_ELAPSED,
    .classification = FUNC_MGT_AGG_FUNC,
    .dataRequiredFunc = statisDataRequired,
    .translateFunc = translateElapsedMerge,
    .getEnvFunc   = getElapsedFuncEnv,
    .initFunc     = elapsedFunctionSetup,
2565
    .processFunc  = elapsedFunctionMerge,
2566 2567 2568
    .finalizeFunc = elapsedFinalize,
    .invertFunc   = NULL,
    .combineFunc  = elapsedCombine,
G
Ganlin Zhao 已提交
2569
  },
2570 2571 2572
  {
    .name = "interp",
    .type = FUNCTION_TYPE_INTERP,
2573
    .classification = FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_INTERVAL_INTERPO_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2574
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
2575
    .translateFunc = translateInterp,
2576 2577 2578
    .getEnvFunc    = getSelectivityFuncEnv,
    .initFunc      = functionSetup,
    .processFunc   = NULL,
2579 2580
    .finalizeFunc  = NULL,
    .estimateReturnRowsFunc = interpEstReturnRows
2581
  },
2582 2583 2584
  {
    .name = "derivative",
    .type = FUNCTION_TYPE_DERIVATIVE,
2585
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2586
                      FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_CUMULATIVE_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
2587 2588 2589 2590
    .translateFunc = translateDerivative,
    .getEnvFunc   = getDerivativeFuncEnv,
    .initFunc     = derivativeFuncSetup,
    .processFunc  = derivativeFunction,
G
Ganlin Zhao 已提交
2591
    .sprocessFunc = derivativeScalarFunction,
2592 2593
    .finalizeFunc = functionFinalize,
    .estimateReturnRowsFunc = derivativeEstReturnRows
2594
  },
G
Ganlin Zhao 已提交
2595 2596 2597
  {
    .name = "irate",
    .type = FUNCTION_TYPE_IRATE,
D
dapan1121 已提交
2598 2599
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_FORBID_STREAM_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2600 2601 2602 2603
    .translateFunc = translateIrate,
    .getEnvFunc   = getIrateFuncEnv,
    .initFunc     = irateFuncSetup,
    .processFunc  = irateFunction,
2604
    .sprocessFunc = irateScalarFunction,
G
Ganlin Zhao 已提交
2605 2606
    .finalizeFunc = irateFinalize
  },
G
Ganlin Zhao 已提交
2607 2608 2609
  {
    .name = "last_row",
    .type = FUNCTION_TYPE_LAST_ROW,
D
dapan1121 已提交
2610 2611
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
2612
    .translateFunc = translateFirstLast,
2613
    .dynDataRequiredFunc = lastDynDataReq,
2614 2615
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
2616
    .processFunc  = lastRowFunction,
2617
    .sprocessFunc = firstLastScalarFunction,
2618 2619
    .pPartialFunc = "_last_row_partial",
    .pMergeFunc   = "_last_row_merge",
5
54liuyao 已提交
2620 2621
    .finalizeFunc = firstLastFinalize,
    .combineFunc  = lastCombine
2622 2623 2624 2625
  },
  {
    .name = "_cache_last_row",
    .type = FUNCTION_TYPE_CACHE_LAST_ROW,
D
dapan1121 已提交
2626 2627
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
2628 2629 2630
    .translateFunc = translateFirstLast,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
2631
    .processFunc  = cachedLastRowFunction,
2632
    .finalizeFunc = firstLastFinalize
G
Ganlin Zhao 已提交
2633
  },
X
Xiaoyu Wang 已提交
2634 2635 2636
  {
    .name = "_cache_last",
    .type = FUNCTION_TYPE_CACHE_LAST,
D
dapan1121 已提交
2637
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
X
Xiaoyu Wang 已提交
2638 2639 2640 2641 2642 2643
    .translateFunc = translateFirstLast,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunctionMerge,
    .finalizeFunc = firstLastFinalize
  },
2644 2645 2646
  {
    .name = "_last_row_partial",
    .type = FUNCTION_TYPE_LAST_PARTIAL,
D
dapan1121 已提交
2647 2648
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC,
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658
    .translateFunc = translateFirstLastPartial,
    .dynDataRequiredFunc = lastDynDataReq,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastRowFunction,
    .finalizeFunc = firstLastPartialFinalize,
  },
  {
    .name = "_last_row_merge",
    .type = FUNCTION_TYPE_LAST_MERGE,
D
dapan1121 已提交
2659 2660
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC,
2661 2662 2663 2664 2665 2666
    .translateFunc = translateFirstLastMerge,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunctionMerge,
    .finalizeFunc = firstLastFinalize,
  },
G
Ganlin Zhao 已提交
2667 2668 2669
  {
    .name = "first",
    .type = FUNCTION_TYPE_FIRST,
D
dapan1121 已提交
2670 2671
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2672
    .translateFunc = translateFirstLast,
2673
    .dynDataRequiredFunc = firstDynDataReq,
G
Ganlin Zhao 已提交
2674 2675 2676
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = firstFunction,
2677
    .sprocessFunc = firstLastScalarFunction,
2678
    .finalizeFunc = firstLastFinalize,
G
Ganlin Zhao 已提交
2679 2680
    .pPartialFunc = "_first_partial",
    .pMergeFunc   = "_first_merge",
2681
    .combineFunc  = firstCombine,
G
Ganlin Zhao 已提交
2682
  },
2683 2684 2685
  {
    .name = "_first_partial",
    .type = FUNCTION_TYPE_FIRST_PARTIAL,
D
dapan1121 已提交
2686 2687
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC,
2688
    .translateFunc = translateFirstLastPartial,
2689
    .dynDataRequiredFunc = firstDynDataReq,
2690 2691 2692
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = firstFunction,
G
Ganlin Zhao 已提交
2693
    .finalizeFunc = firstLastPartialFinalize,
2694 2695 2696 2697 2698
    .combineFunc  = firstCombine,
  },
  {
    .name = "_first_merge",
    .type = FUNCTION_TYPE_FIRST_MERGE,
D
dapan1121 已提交
2699 2700
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC,
2701 2702 2703
    .translateFunc = translateFirstLastMerge,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
G
Ganlin Zhao 已提交
2704
    .processFunc  = firstFunctionMerge,
2705 2706 2707
    .finalizeFunc = firstLastFinalize,
    .combineFunc  = firstCombine,
  },
G
Ganlin Zhao 已提交
2708 2709 2710
  {
    .name = "last",
    .type = FUNCTION_TYPE_LAST,
D
dapan1121 已提交
2711 2712
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2713
    .translateFunc = translateFirstLast,
2714
    .dynDataRequiredFunc = lastDynDataReq,
G
Ganlin Zhao 已提交
2715 2716 2717
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunction,
2718
    .sprocessFunc = firstLastScalarFunction,
2719
    .finalizeFunc = firstLastFinalize,
G
Ganlin Zhao 已提交
2720 2721
    .pPartialFunc = "_last_partial",
    .pMergeFunc   = "_last_merge",
5
54liuyao 已提交
2722
    .combineFunc  = lastCombine,
G
Ganlin Zhao 已提交
2723
  },
G
Ganlin Zhao 已提交
2724 2725 2726
  {
    .name = "_last_partial",
    .type = FUNCTION_TYPE_LAST_PARTIAL,
D
dapan1121 已提交
2727 2728
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2729
    .translateFunc = translateFirstLastPartial,
2730
    .dynDataRequiredFunc = lastDynDataReq,
G
Ganlin Zhao 已提交
2731 2732 2733 2734 2735 2736 2737 2738 2739
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunction,
    .finalizeFunc = firstLastPartialFinalize,
    .combineFunc  = lastCombine,
  },
  {
    .name = "_last_merge",
    .type = FUNCTION_TYPE_LAST_MERGE,
D
dapan1121 已提交
2740 2741
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_MULTI_RES_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
                      FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2742 2743 2744 2745 2746 2747 2748
    .translateFunc = translateFirstLastMerge,
    .getEnvFunc   = getFirstLastFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = lastFunctionMerge,
    .finalizeFunc = firstLastFinalize,
    .combineFunc  = lastCombine,
  },
2749 2750 2751
  {
    .name = "twa",
    .type = FUNCTION_TYPE_TWA,
D
dapan1121 已提交
2752 2753
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_INTERVAL_INTERPO_FUNC | FUNC_MGT_FORBID_STREAM_FUNC |
                      FUNC_MGT_IMPLICIT_TS_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
2754
    .translateFunc = translateInNumOutDou,
H
Haojun Liao 已提交
2755
    .dataRequiredFunc = statisDataRequired,
2756 2757 2758
    .getEnvFunc    = getTwaFuncEnv,
    .initFunc      = twaFunctionSetup,
    .processFunc   = twaFunction,
2759
    .sprocessFunc  = twaScalarFunction,
2760 2761
    .finalizeFunc  = twaFinalize
  },
2762 2763 2764
  {
    .name = "histogram",
    .type = FUNCTION_TYPE_HISTOGRAM,
2765
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_FORBID_FILL_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
2766 2767 2768 2769
    .translateFunc = translateHistogram,
    .getEnvFunc   = getHistogramFuncEnv,
    .initFunc     = histogramFunctionSetup,
    .processFunc  = histogramFunction,
2770
    .sprocessFunc = histogramScalarFunction,
G
Ganlin Zhao 已提交
2771
    .finalizeFunc = histogramFinalize,
2772 2773
    .invertFunc   = NULL,
    .combineFunc  = histogramCombine,
G
Ganlin Zhao 已提交
2774
    .pPartialFunc = "_histogram_partial",
2775
    .pMergeFunc   = "_histogram_merge",
2776
  },
2777 2778 2779
  {
    .name = "_histogram_partial",
    .type = FUNCTION_TYPE_HISTOGRAM_PARTIAL,
2780
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_FORBID_FILL_FUNC,
2781
    .translateFunc = translateHistogramPartial,
2782 2783
    .getEnvFunc   = getHistogramFuncEnv,
    .initFunc     = histogramFunctionSetup,
2784
    .processFunc  = histogramFunctionPartial,
2785 2786 2787
    .finalizeFunc = histogramPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = histogramCombine,
2788 2789 2790 2791
  },
  {
    .name = "_histogram_merge",
    .type = FUNCTION_TYPE_HISTOGRAM_MERGE,
2792
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_MULTI_ROWS_FUNC | FUNC_MGT_FORBID_FILL_FUNC,
2793
    .translateFunc = translateHistogramMerge,
2794
    .getEnvFunc   = getHistogramFuncEnv,
G
Ganlin Zhao 已提交
2795
    .initFunc     = functionSetup,
2796
    .processFunc  = histogramFunctionMerge,
2797 2798 2799
    .finalizeFunc = histogramFinalize,
    .invertFunc   = NULL,
    .combineFunc  = histogramCombine,
2800
  },
G
Ganlin Zhao 已提交
2801 2802 2803
  {
    .name = "hyperloglog",
    .type = FUNCTION_TYPE_HYPERLOGLOG,
2804
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2805 2806 2807 2808
    .translateFunc = translateHLL,
    .getEnvFunc   = getHLLFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = hllFunction,
2809
    .sprocessFunc = hllScalarFunction,
G
Ganlin Zhao 已提交
2810
    .finalizeFunc = hllFinalize,
2811 2812
    .invertFunc   = NULL,
    .combineFunc  = hllCombine,
G
Ganlin Zhao 已提交
2813 2814
    .pPartialFunc = "_hyperloglog_partial",
    .pMergeFunc   = "_hyperloglog_merge"
G
Ganlin Zhao 已提交
2815
  },
2816 2817
  {
    .name = "_hyperloglog_partial",
2818
    .type = FUNCTION_TYPE_HYPERLOGLOG_PARTIAL,
2819
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2820
    .translateFunc = translateHLLPartial,
2821 2822 2823
    .getEnvFunc   = getHLLFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = hllFunction,
2824 2825 2826
    .finalizeFunc = hllPartialFinalize,
    .invertFunc   = NULL,
    .combineFunc  = hllCombine,
2827 2828 2829
  },
  {
    .name = "_hyperloglog_merge",
2830
    .type = FUNCTION_TYPE_HYPERLOGLOG_MERGE,
2831
    .classification = FUNC_MGT_AGG_FUNC,
G
Ganlin Zhao 已提交
2832
    .translateFunc = translateHLLMerge,
2833 2834
    .getEnvFunc   = getHLLFuncEnv,
    .initFunc     = functionSetup,
G
Ganlin Zhao 已提交
2835
    .processFunc  = hllFunctionMerge,
2836 2837 2838
    .finalizeFunc = hllFinalize,
    .invertFunc   = NULL,
    .combineFunc  = hllCombine,
G
Ganlin Zhao 已提交
2839
  },
G
Ganlin Zhao 已提交
2840 2841 2842
  {
    .name = "diff",
    .type = FUNCTION_TYPE_DIFF,
2843
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2844
                      FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_CUMULATIVE_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2845 2846 2847 2848
    .translateFunc = translateDiff,
    .getEnvFunc   = getDiffFuncEnv,
    .initFunc     = diffFunctionSetup,
    .processFunc  = diffFunction,
2849
    .sprocessFunc = diffScalarFunction,
2850
    .finalizeFunc = functionFinalize,
2851
    .estimateReturnRowsFunc = diffEstReturnRows,
G
Ganlin Zhao 已提交
2852
  },
2853
  {
2854
    .name = "statecount",
2855
    .type = FUNCTION_TYPE_STATE_COUNT,
2856
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2857
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
2858
    .translateFunc = translateStateCount,
2859 2860 2861
    .getEnvFunc   = getStateFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = stateCountFunction,
2862
    .sprocessFunc = stateCountScalarFunction,
2863 2864
    .finalizeFunc = NULL
  },
2865
  {
2866
    .name = "stateduration",
2867
    .type = FUNCTION_TYPE_STATE_DURATION,
2868
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2869
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
2870 2871 2872 2873
    .translateFunc = translateStateDuration,
    .getEnvFunc   = getStateFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = stateDurationFunction,
2874
    .sprocessFunc = stateDurationScalarFunction,
2875 2876
    .finalizeFunc = NULL
  },
G
Ganlin Zhao 已提交
2877 2878 2879
  {
    .name = "csum",
    .type = FUNCTION_TYPE_CSUM,
2880
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2881
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_CUMULATIVE_FUNC | FUNC_MGT_KEEP_ORDER_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2882 2883 2884 2885
    .translateFunc = translateCsum,
    .getEnvFunc   = getCsumFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = csumFunction,
2886
    .sprocessFunc = csumScalarFunction,
2887 2888
    .finalizeFunc = NULL,
    .estimateReturnRowsFunc = csumEstReturnRows,
G
Ganlin Zhao 已提交
2889
  },
G
Ganlin Zhao 已提交
2890 2891 2892
  {
    .name = "mavg",
    .type = FUNCTION_TYPE_MAVG,
2893
    .classification = FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_TIMELINE_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC |
D
dapan1121 已提交
2894
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_FORBID_SYSTABLE_FUNC,
G
Ganlin Zhao 已提交
2895 2896
    .translateFunc = translateMavg,
    .getEnvFunc   = getMavgFuncEnv,
G
Ganlin Zhao 已提交
2897
    .initFunc     = mavgFunctionSetup,
G
Ganlin Zhao 已提交
2898
    .processFunc  = mavgFunction,
2899
    .sprocessFunc = mavgScalarFunction,
G
Ganlin Zhao 已提交
2900 2901
    .finalizeFunc = NULL
  },
G
Ganlin Zhao 已提交
2902 2903 2904
  {
    .name = "sample",
    .type = FUNCTION_TYPE_SAMPLE,
D
dapan1121 已提交
2905 2906
    .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 已提交
2907 2908 2909 2910
    .translateFunc = translateSample,
    .getEnvFunc   = getSampleFuncEnv,
    .initFunc     = sampleFunctionSetup,
    .processFunc  = sampleFunction,
2911
    .sprocessFunc = sampleScalarFunction,
2912
    .finalizeFunc = sampleFinalize
G
Ganlin Zhao 已提交
2913
  },
G
Ganlin Zhao 已提交
2914 2915 2916
  {
    .name = "tail",
    .type = FUNCTION_TYPE_TAIL,
2917 2918
    .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 已提交
2919 2920 2921 2922
    .translateFunc = translateTail,
    .getEnvFunc   = getTailFuncEnv,
    .initFunc     = tailFunctionSetup,
    .processFunc  = tailFunction,
2923
    .sprocessFunc = tailScalarFunction,
G
Ganlin Zhao 已提交
2924
    .finalizeFunc = NULL
G
Ganlin Zhao 已提交
2925
  },
2926 2927 2928
  {
    .name = "unique",
    .type = FUNCTION_TYPE_UNIQUE,
2929
    .classification = FUNC_MGT_SELECT_FUNC | FUNC_MGT_INDEFINITE_ROWS_FUNC | FUNC_MGT_TIMELINE_FUNC |
2930
                      FUNC_MGT_FORBID_STREAM_FUNC | FUNC_MGT_IMPLICIT_TS_FUNC,
2931 2932 2933 2934
    .translateFunc = translateUnique,
    .getEnvFunc   = getUniqueFuncEnv,
    .initFunc     = uniqueFunctionSetup,
    .processFunc  = uniqueFunction,
2935
    .sprocessFunc = uniqueScalarFunction,
G
Ganlin Zhao 已提交
2936
    .finalizeFunc = NULL
2937
  },
G
Ganlin Zhao 已提交
2938 2939 2940
  {
    .name = "mode",
    .type = FUNCTION_TYPE_MODE,
5
54liuyao 已提交
2941
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
G
Ganlin Zhao 已提交
2942
    .translateFunc = translateMode,
G
Ganlin Zhao 已提交
2943 2944 2945
    .getEnvFunc   = getModeFuncEnv,
    .initFunc     = modeFunctionSetup,
    .processFunc  = modeFunction,
2946
    .sprocessFunc = modeScalarFunction,
G
Ganlin Zhao 已提交
2947
    .finalizeFunc = modeFinalize,
G
Ganlin Zhao 已提交
2948
  },
G
Ganlin Zhao 已提交
2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962
  {
    .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,
2963
    .translateFunc = translateLogarithm,
G
Ganlin Zhao 已提交
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 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 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 3142
    .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,
3143
    .translateFunc = translateLtrim,
G
Ganlin Zhao 已提交
3144 3145 3146 3147 3148 3149 3150 3151 3152
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = ltrimFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "rtrim",
    .type = FUNCTION_TYPE_RTRIM,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_STRING_FUNC,
3153
    .translateFunc = translateRtrim,
G
Ganlin Zhao 已提交
3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
    .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,
3223
    .translateFunc = translateNowToday,
G
Ganlin Zhao 已提交
3224 3225 3226 3227 3228 3229 3230 3231 3232
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = nowFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "today",
    .type = FUNCTION_TYPE_TODAY,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_DATETIME_FUNC,
3233
    .translateFunc = translateNowToday,
G
Ganlin Zhao 已提交
3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
    .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,
3252
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_SCAN_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3253 3254 3255
    .translateFunc = translateTbnameColumn,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
3256
    .sprocessFunc = qTbnameFunction,
G
Ganlin Zhao 已提交
3257 3258 3259
    .finalizeFunc = NULL
  },
  {
3260 3261
    .name = "_qstart",
    .type = FUNCTION_TYPE_QSTART,
3262
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_CLIENT_PC_FUNC,
G
Ganlin Zhao 已提交
3263
    .translateFunc = translateTimePseudoColumn,
3264
    .getEnvFunc   = NULL,
G
Ganlin Zhao 已提交
3265
    .initFunc     = NULL,
3266
    .sprocessFunc = NULL,
G
Ganlin Zhao 已提交
3267 3268 3269
    .finalizeFunc = NULL
  },
  {
3270 3271
    .name = "_qend",
    .type = FUNCTION_TYPE_QEND,
3272
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_CLIENT_PC_FUNC,
G
Ganlin Zhao 已提交
3273
    .translateFunc = translateTimePseudoColumn,
3274
    .getEnvFunc   = NULL,
G
Ganlin Zhao 已提交
3275
    .initFunc     = NULL,
3276
    .sprocessFunc = NULL,
3277 3278 3279 3280 3281
    .finalizeFunc = NULL
  },
  {
    .name = "_qduration",
    .type = FUNCTION_TYPE_QDURATION,
3282
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_CLIENT_PC_FUNC,
3283
    .translateFunc = translateWduration,
3284
    .getEnvFunc   = NULL,
G
Ganlin Zhao 已提交
3285
    .initFunc     = NULL,
3286
    .sprocessFunc = NULL,
G
Ganlin Zhao 已提交
3287 3288 3289
    .finalizeFunc = NULL
  },
  {
3290 3291
    .name = "_wstart",
    .type = FUNCTION_TYPE_WSTART,
3292
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_WINDOW_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3293 3294 3295 3296 3297 3298 3299
    .translateFunc = translateTimePseudoColumn,
    .getEnvFunc   = getTimePseudoFuncEnv,
    .initFunc     = NULL,
    .sprocessFunc = winStartTsFunction,
    .finalizeFunc = NULL
  },
  {
3300 3301
    .name = "_wend",
    .type = FUNCTION_TYPE_WEND,
3302
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_WINDOW_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3303 3304 3305 3306 3307 3308 3309 3310 3311
    .translateFunc = translateTimePseudoColumn,
    .getEnvFunc   = getTimePseudoFuncEnv,
    .initFunc     = NULL,
    .sprocessFunc = winEndTsFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "_wduration",
    .type = FUNCTION_TYPE_WDURATION,
3312
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_WINDOW_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
G
Ganlin Zhao 已提交
3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327
    .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
3328 3329 3330 3331
  },
  {
    .name = "_select_value",
    .type = FUNCTION_TYPE_SELECT_VALUE,
3332
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
3333
    .translateFunc = translateSelectValue,
3334 3335
    .getEnvFunc   = getSelectivityFuncEnv,  // todo remove this function later.
    .initFunc     = functionSetup,
3336
    .processFunc  = NULL,
3337 3338 3339
    .finalizeFunc = NULL,
    .pPartialFunc = "_select_value",
    .pMergeFunc   = "_select_value"
3340 3341 3342 3343
  },
  {
    .name = "_block_dist",
    .type = FUNCTION_TYPE_BLOCK_DIST,
5
54liuyao 已提交
3344
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_FORBID_STREAM_FUNC,
3345
    .translateFunc = translateBlockDistFunc,
3346
    .getEnvFunc   = getBlockDistFuncEnv,
3347
    .initFunc     = blockDistSetup,
3348 3349
    .processFunc  = blockDistFunction,
    .finalizeFunc = blockDistFinalize
3350 3351 3352 3353 3354 3355
  },
  {
    .name = "_block_dist_info",
    .type = FUNCTION_TYPE_BLOCK_DIST_INFO,
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_SCAN_PC_FUNC,
    .translateFunc = translateBlockDistInfoFunc,
3356 3357 3358
  },
  {
    .name = "_group_key",
3359
    .type = FUNCTION_TYPE_GROUP_KEY,
3360
    .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
3361 3362 3363 3364
    .translateFunc = translateGroupKey,
    .getEnvFunc   = getGroupKeyFuncEnv,
    .initFunc     = functionSetup,
    .processFunc  = groupKeyFunction,
G
Ganlin Zhao 已提交
3365
    .finalizeFunc = groupKeyFinalize,
5
54liuyao 已提交
3366
    .combineFunc  = groupKeyCombine,
3367 3368
    .pPartialFunc = "_group_key",
    .pMergeFunc   = "_group_key"
3369
  },
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405
  {
    .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,
  },
3406 3407 3408
  {
    .name = "_irowts",
    .type = FUNCTION_TYPE_IROWTS,
3409
    .classification = FUNC_MGT_PSEUDO_COLUMN_FUNC | FUNC_MGT_INTERP_PC_FUNC | FUNC_MGT_KEEP_ORDER_FUNC,
3410 3411 3412 3413 3414 3415
    .translateFunc = translateTimePseudoColumn,
    .getEnvFunc   = getTimePseudoFuncEnv,
    .initFunc     = NULL,
    .sprocessFunc = NULL,
    .finalizeFunc = NULL
  },
3416 3417 3418 3419 3420 3421 3422 3423 3424 3425
  {
    .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
  },
3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
  {
    .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
  },
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445
  {
    .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
  },
D
Dingle Zhang 已提交
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535
  {
    .name = "st_geomfromtext",
    .type = FUNCTION_TYPE_GEOM_FROM_TEXT,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateInStrOutGeom,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = geomFromTextFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_astext",
    .type = FUNCTION_TYPE_AS_TEXT,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateInGeomOutStr,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = asTextFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_makepoint",
    .type = FUNCTION_TYPE_MAKE_POINT,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateIn2NumOutGeom,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = makePointFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_intersects",
    .type = FUNCTION_TYPE_INTERSECTS,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateIn2GeomOutBool,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = intersectsFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_equals",
    .type = FUNCTION_TYPE_EQUALS,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateIn2GeomOutBool,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = equalsFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_touches",
    .type = FUNCTION_TYPE_TOUCHES,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateIn2GeomOutBool,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = touchesFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_covers",
    .type = FUNCTION_TYPE_COVERS,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateIn2GeomOutBool,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = coversFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_contains",
    .type = FUNCTION_TYPE_CONTAINS,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateIn2GeomOutBool,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = containsFunction,
    .finalizeFunc = NULL
  },
  {
    .name = "st_containsproperly",
    .type = FUNCTION_TYPE_CONTAINS_PROPERLY,
    .classification = FUNC_MGT_SCALAR_FUNC | FUNC_MGT_GEOMETRY_FUNC,
    .translateFunc = translateIn2GeomOutBool,
    .getEnvFunc   = NULL,
    .initFunc     = NULL,
    .sprocessFunc = containsProperlyFunction,
    .finalizeFunc = NULL
  },
G
Ganlin Zhao 已提交
3536
};
X
Xiaoyu Wang 已提交
3537
// clang-format on
3538

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