parAstCreater.c 78.4 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/>.
 */

17 18
#include <regex.h>

X
Xiaoyu Wang 已提交
19 20
#include "parAst.h"
#include "parUtil.h"
21
#include "tglobal.h"
X
Xiaoyu Wang 已提交
22
#include "ttime.h"
23

X
Xiaoyu Wang 已提交
24 25 26
#define CHECK_OUT_OF_MEM(p)                                                      \
  do {                                                                           \
    if (NULL == (p)) {                                                           \
27
      pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;                                   \
X
Xiaoyu Wang 已提交
28
      snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "Out of memory"); \
X
Xiaoyu Wang 已提交
29 30
      return NULL;                                                               \
    }                                                                            \
31 32
  } while (0)

X
Xiaoyu Wang 已提交
33 34 35 36 37
#define CHECK_PARSER_STATUS(pCxt)             \
  do {                                        \
    if (TSDB_CODE_SUCCESS != pCxt->errCode) { \
      return NULL;                            \
    }                                         \
38 39
  } while (0)

40 41 42 43 44 45 46 47
#define COPY_STRING_FORM_ID_TOKEN(buf, pToken) strncpy(buf, (pToken)->z, TMIN((pToken)->n, sizeof(buf) - 1))
#define COPY_STRING_FORM_STR_TOKEN(buf, pToken)                              \
  do {                                                                       \
    if ((pToken)->n > 2) {                                                   \
      strncpy(buf, (pToken)->z + 1, TMIN((pToken)->n - 2, sizeof(buf) - 1)); \
    }                                                                        \
  } while (0)

X
Xiaoyu Wang 已提交
48
SToken nil_token = {.type = TK_NK_NIL, .n = 0, .z = NULL};
49

50
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
51
  memset(pCxt, 0, sizeof(SAstCreateContext));
52
  pCxt->pQueryCxt = pParseCxt;
53 54
  pCxt->msgBuf.buf = pParseCxt->pMsg;
  pCxt->msgBuf.len = pParseCxt->msgLen;
55 56
  pCxt->notSupport = false;
  pCxt->pRootNode = NULL;
D
dapan1121 已提交
57
  pCxt->placeholderNo = 0;
X
Xiaoyu Wang 已提交
58
  pCxt->pPlaceholderValues = NULL;
59
  pCxt->errCode = TSDB_CODE_SUCCESS;
60 61
}

X
Xiaoyu Wang 已提交
62
static void trimEscape(SToken* pName) {
63
  // todo need to deal with `ioo``ii` -> ioo`ii
X
Xiaoyu Wang 已提交
64 65 66 67 68 69 70
  if (NULL != pName && pName->n > 1 && '`' == pName->z[0]) {
    pName->z += 1;
    pName->n -= 2;
  }
}

static bool checkUserName(SAstCreateContext* pCxt, SToken* pUserName) {
71
  if (NULL == pUserName) {
72
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
73 74
  } else {
    if (pUserName->n >= TSDB_USER_LEN) {
75
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
X
Xiaoyu Wang 已提交
76
    }
77
  }
78
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
X
Xiaoyu Wang 已提交
79 80
    trimEscape(pUserName);
  }
81
  return TSDB_CODE_SUCCESS == pCxt->errCode;
82 83
}

84 85 86 87 88 89 90 91 92 93 94 95 96
static bool invalidPassword(const char* pPassword) {
  regex_t regex;

  if (regcomp(&regex, "[ '\"`\\]", REG_EXTENDED | REG_ICASE) != 0) {
    return false;
  }

  /* Execute regular expression */
  int32_t res = regexec(&regex, pPassword, 0, NULL, 0);
  regfree(&regex);
  return 0 == res;
}

97 98
static bool checkPassword(SAstCreateContext* pCxt, const SToken* pPasswordToken, char* pPassword) {
  if (NULL == pPasswordToken) {
99
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
100
  } else if (pPasswordToken->n >= (TSDB_USET_PASSWORD_LEN + 2)) {
101
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
X
Xiaoyu Wang 已提交
102
  } else {
103
    strncpy(pPassword, pPasswordToken->z, pPasswordToken->n);
X
Xiaoyu Wang 已提交
104 105
    strdequote(pPassword);
    if (strtrim(pPassword) <= 0) {
106
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_EMPTY);
107 108
    } else if (invalidPassword(pPassword)) {
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PASSWD);
X
Xiaoyu Wang 已提交
109
    }
110
  }
111
  return TSDB_CODE_SUCCESS == pCxt->errCode;
112 113
}

114 115 116 117
static int32_t parsePort(SAstCreateContext* pCxt, const char* p, int32_t* pPort) {
  *pPort = taosStr2Int32(p, NULL, 10);
  if (*pPort >= UINT16_MAX || *pPort <= 0) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
118
  }
119
  return TSDB_CODE_SUCCESS;
120 121
}

122 123 124
static int32_t parseEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
  if (pEp->n >= (NULL == pPort ? (TSDB_FQDN_LEN + 1 + 5) : TSDB_FQDN_LEN)) {  // format 'fqdn:port' or 'fqdn'
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
125
  }
126

127
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
  strdequote(ep);
  strtrim(ep);
  if (NULL == pPort) {
    strcpy(pFqdn, ep);
    return TSDB_CODE_SUCCESS;
  }
  char* pColon = strchr(ep, ':');
  if (NULL == pColon) {
    *pPort = tsServerPort;
    strcpy(pFqdn, ep);
    return TSDB_CODE_SUCCESS;
  }
  strncpy(pFqdn, ep, pColon - ep);
  return parsePort(pCxt, pColon + 1, pPort);
143 144
}

145 146 147
static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, const SToken* pPortToken, char* pFqdn,
                                  int32_t* pPort) {
  if (NULL == pEp) {
148
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
149 150 151 152 153
    return false;
  }

  if (NULL != pPortToken) {
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
154
  }
155 156 157 158 159

  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
  }

160
  return TSDB_CODE_SUCCESS == pCxt->errCode;
161 162
}

163
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool demandDb) {
164
  if (NULL == pDbName) {
165
    if (demandDb && NULL == pCxt->pQueryCxt->db) {
166
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
X
bugfix  
Xiaoyu Wang 已提交
167
    }
X
Xiaoyu Wang 已提交
168
  } else {
X
Xiaoyu Wang 已提交
169
    trimEscape(pDbName);
X
Xiaoyu Wang 已提交
170
    if (pDbName->n >= TSDB_DB_NAME_LEN) {
171
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
X
Xiaoyu Wang 已提交
172 173
    }
  }
174
  return TSDB_CODE_SUCCESS == pCxt->errCode;
175
}
176

X
Xiaoyu Wang 已提交
177
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
X
Xiaoyu Wang 已提交
178
  trimEscape(pTableName);
X
Xiaoyu Wang 已提交
179
  if (NULL != pTableName && pTableName->n >= TSDB_TABLE_NAME_LEN) {
180
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
X
Xiaoyu Wang 已提交
181
    return false;
182
  }
X
Xiaoyu Wang 已提交
183
  return true;
184 185
}

X
Xiaoyu Wang 已提交
186
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
X
Xiaoyu Wang 已提交
187
  trimEscape(pColumnName);
X
Xiaoyu Wang 已提交
188
  if (NULL != pColumnName && pColumnName->n >= TSDB_COL_NAME_LEN) {
189
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
X
Xiaoyu Wang 已提交
190
    return false;
191
  }
X
Xiaoyu Wang 已提交
192
  return true;
193 194
}

X
Xiaoyu Wang 已提交
195
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
X
Xiaoyu Wang 已提交
196
  trimEscape(pIndexName);
X
Xiaoyu Wang 已提交
197
  if (NULL != pIndexName && pIndexName->n >= TSDB_INDEX_NAME_LEN) {
198
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
X
Xiaoyu Wang 已提交
199
    return false;
X
Xiaoyu Wang 已提交
200
  }
X
Xiaoyu Wang 已提交
201
  return true;
X
Xiaoyu Wang 已提交
202 203
}

204 205 206 207 208 209 210 211 212
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
  trimEscape(pTopicName);
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
    return false;
  }
  return true;
}

D
dapan1121 已提交
213 214 215 216 217 218 219 220 221
static bool checkCGroupName(SAstCreateContext* pCxt, SToken* pCGroup) {
  trimEscape(pCGroup);
  if (pCGroup->n >= TSDB_CGROUP_LEN) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pCGroup->z);
    return false;
  }
  return true;
}

222 223 224 225 226 227 228 229 230
static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
  trimEscape(pStreamName);
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
    return false;
  }
  return true;
}

231 232 233 234 235 236 237 238 239
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
  if (NULL == pCommentToken) {
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
  }
  return TSDB_CODE_SUCCESS == pCxt->errCode;
}

240
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
X
Xiaoyu Wang 已提交
241
  CHECK_PARSER_STATUS(pCxt);
242 243 244 245 246 247 248 249 250
  SRawExprNode* target = (SRawExprNode*)nodesMakeNode(QUERY_NODE_RAW_EXPR);
  CHECK_OUT_OF_MEM(target);
  target->p = pToken->z;
  target->n = pToken->n;
  target->pNode = pNode;
  return (SNode*)target;
}

SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
X
Xiaoyu Wang 已提交
251
  CHECK_PARSER_STATUS(pCxt);
252 253 254 255 256 257 258 259 260
  SRawExprNode* target = (SRawExprNode*)nodesMakeNode(QUERY_NODE_RAW_EXPR);
  CHECK_OUT_OF_MEM(target);
  target->p = pStart->z;
  target->n = (pEnd->z + pEnd->n) - pStart->z;
  target->pNode = pNode;
  return (SNode*)target;
}

SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
261
  CHECK_PARSER_STATUS(pCxt);
262
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
263 264 265
  SNode*        pRealizedExpr = pRawExpr->pNode;
  if (nodesIsExprNode(pRealizedExpr)) {
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
X
Xiaoyu Wang 已提交
266
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
X
Xiaoyu Wang 已提交
267 268
      strcpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName);
      strcpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName);
X
Xiaoyu Wang 已提交
269
    } else {
X
Xiaoyu Wang 已提交
270
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
271 272 273 274 275 276 277 278 279 280 281 282 283

      // See TS-3398.
      // Len of pRawExpr->p could be larger than len of aliasName[TSDB_COL_NAME_LEN].
      // If aliasName is truncated, hash value of aliasName could be the same.
      T_MD5_CTX ctx;
      tMD5Init(&ctx);
      tMD5Update(&ctx, (uint8_t*)pRawExpr->p, pRawExpr->n);
      tMD5Final(&ctx);
      char* p = pExpr->aliasName;
      for (uint8_t i = 0; i < tListLen(ctx.digest); ++i) {
        sprintf(p, "%02x", ctx.digest[i]);
        p += 2;
      }
X
Xiaoyu Wang 已提交
284 285
      strncpy(pExpr->userAlias, pRawExpr->p, len);
      pExpr->userAlias[len] = '\0';
X
Xiaoyu Wang 已提交
286
    }
D
dapan1121 已提交
287
  }
288 289
  pRawExpr->pNode = NULL;
  nodesDestroyNode(pNode);
X
Xiaoyu Wang 已提交
290
  return pRealizedExpr;
291 292 293
}

SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
294
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
295
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
296 297
    return nil_token;
  }
298
  SRawExprNode* target = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
299
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
300 301 302
  return t;
}

303
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
304
  CHECK_PARSER_STATUS(pCxt);
305 306
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
307
  pCxt->errCode = nodesListAppend(list, pNode);
X
Xiaoyu Wang 已提交
308
  return list;
309 310 311
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
312
  CHECK_PARSER_STATUS(pCxt);
313
  pCxt->errCode = nodesListAppend(pList, pNode);
X
Xiaoyu Wang 已提交
314
  return pList;
315 316
}

X
Xiaoyu Wang 已提交
317
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
X
Xiaoyu Wang 已提交
318
  CHECK_PARSER_STATUS(pCxt);
319
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
320 321 322 323
    return NULL;
  }
  SColumnNode* col = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(col);
324
  if (NULL != pTableAlias) {
325
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
326
  }
327
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
328
  return (SNode*)col;
329
}
330

331
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
332
  CHECK_PARSER_STATUS(pCxt);
333 334
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
335 336 337 338
  val->literal = strndup(pLiteral->z, pLiteral->n);
  if (TK_NK_ID != pLiteral->type && TK_TIMEZONE != pLiteral->type &&
      (IS_VAR_DATA_TYPE(dataType) || TSDB_DATA_TYPE_TIMESTAMP == dataType)) {
    trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
X
Xiaoyu Wang 已提交
339
  }
340
  CHECK_OUT_OF_MEM(val->literal);
341
  val->node.resType.type = dataType;
X
Xiaoyu Wang 已提交
342
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
343 344 345
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  }
346 347
  val->isDuration = false;
  val->translate = false;
348 349 350
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
351 352 353 354 355
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
  trimEscape(pLiteral);
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
}

356
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
357
  CHECK_PARSER_STATUS(pCxt);
358 359
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
360 361 362
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
363
  val->translate = false;
364 365 366
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
367
  return (SNode*)val;
368 369
}

X
Xiaoyu Wang 已提交
370
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
371
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
372 373 374 375
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
376 377
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
378
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
X
Xiaoyu Wang 已提交
379 380 381 382 383 384 385 386 387
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = false;
  val->translate = false;
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
  val->node.resType.bytes = strlen(val->literal);
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  return (SNode*)val;
}

388
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
389
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
390 391 392 393
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
    return NULL;
  }
394 395
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
396 397
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
D
dapan1121 已提交
398
  val->placeholderNo = ++pCxt->placeholderNo;
X
Xiaoyu Wang 已提交
399 400 401
  if (NULL == pCxt->pPlaceholderValues) {
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    if (NULL == pCxt->pPlaceholderValues) {
402
      nodesDestroyNode((SNode*)val);
X
Xiaoyu Wang 已提交
403 404 405 406
      return NULL;
    }
  }
  taosArrayPush(pCxt->pPlaceholderValues, &val);
407 408 409
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
410 411 412 413 414 415 416 417 418 419 420
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType) {
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
    nodesDestroyNode(pParam);
    return code;
  } else {
    return nodesListAppend(pCond->pParameterList, pParam);
  }
}

421
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
X
Xiaoyu Wang 已提交
422
  CHECK_PARSER_STATUS(pCxt);
423 424 425 426
  SLogicConditionNode* cond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  CHECK_OUT_OF_MEM(cond);
  cond->condType = type;
  cond->pParameterList = nodesMakeList();
X
Xiaoyu Wang 已提交
427 428 429
  int32_t code = addParamToLogicConditionNode(cond, pParam1);
  if (TSDB_CODE_SUCCESS == code && NULL != pParam2) {
    code = addParamToLogicConditionNode(cond, pParam2);
X
Xiaoyu Wang 已提交
430
  }
X
Xiaoyu Wang 已提交
431
  if (TSDB_CODE_SUCCESS != code) {
432
    nodesDestroyNode((SNode*)cond);
X
Xiaoyu Wang 已提交
433
    return NULL;
434
  }
435 436 437
  return (SNode*)cond;
}

438 439 440 441 442 443 444 445 446 447 448 449 450
static uint8_t getMinusDataType(uint8_t orgType) {
  switch (orgType) {
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      return TSDB_DATA_TYPE_BIGINT;
    default:
      break;
  }
  return orgType;
}

451
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
452
  CHECK_PARSER_STATUS(pCxt);
453 454
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
    SValueNode* pVal = (SValueNode*)pLeft;
455
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
456
    CHECK_OUT_OF_MEM(pNewLiteral);
457 458 459 460 461 462 463
    if ('+' == pVal->literal[0]) {
      sprintf(pNewLiteral, "-%s", pVal->literal + 1);
    } else if ('-' == pVal->literal[0]) {
      sprintf(pNewLiteral, "%s", pVal->literal + 1);
    } else {
      sprintf(pNewLiteral, "-%s", pVal->literal);
    }
464 465
    taosMemoryFree(pVal->literal);
    pVal->literal = pNewLiteral;
466
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
467 468
    return pLeft;
  }
469 470 471 472 473 474 475 476 477
  SOperatorNode* op = (SOperatorNode*)nodesMakeNode(QUERY_NODE_OPERATOR);
  CHECK_OUT_OF_MEM(op);
  op->opType = type;
  op->pLeft = pLeft;
  op->pRight = pRight;
  return (SNode*)op;
}

SNode* createBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
478
  CHECK_PARSER_STATUS(pCxt);
479
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
480 481
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
482 483 484
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
485
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
486 487
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
488 489
}

490
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
491
  CHECK_PARSER_STATUS(pCxt);
492
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
493 494
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
495 496 497 498 499
  if (NULL == pFuncName) {
    strcpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME);
  } else {
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
  }
X
Xiaoyu Wang 已提交
500 501 502
  return (SNode*)pCol;
}

503
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
504
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
505
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
506
    return createPrimaryKeyCol(pCxt, pFuncName);
X
Xiaoyu Wang 已提交
507
  }
508 509
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
510
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
511 512 513 514
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

515
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
X
Xiaoyu Wang 已提交
516
  CHECK_PARSER_STATUS(pCxt);
517 518 519 520
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
D
Dingle Zhang 已提交
521
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_GEOMETRY == dt.type) {
522 523 524
    func->node.resType.bytes = func->node.resType.bytes + VARSTR_HEADER_SIZE;
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
    func->node.resType.bytes = func->node.resType.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
525
  }
526 527 528 529
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

530
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
531
  CHECK_PARSER_STATUS(pCxt);
532 533 534 535
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
536 537
}

X
Xiaoyu Wang 已提交
538
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
X
Xiaoyu Wang 已提交
539
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
540 541 542 543 544 545 546 547 548
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = nodesMakeList();
  CHECK_OUT_OF_MEM(list->pNodeList);
  nodesListAppend(list->pNodeList, p1);
  nodesListAppend(list->pNodeList, p2);
  return (SNode*)list;
}

X
Xiaoyu Wang 已提交
549
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
550
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
551
  if (!checkDbName(pCxt, pDbName, true) || !checkTableName(pCxt, pTableName) || !checkTableName(pCxt, pTableAlias)) {
552 553 554 555 556
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
557
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
558
  } else {
X
Xiaoyu Wang 已提交
559
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
560
  }
561
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
562
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
563
  } else {
564
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
565
  }
566
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
567
  return (SNode*)realTable;
568
}
569

570
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
571
  CHECK_PARSER_STATUS(pCxt);
572 573 574
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
575
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
576
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
X
Xiaoyu Wang 已提交
577
  } else {
578
    taosRandStr(tempTable->table.tableAlias, 8);
X
Xiaoyu Wang 已提交
579 580 581
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
582
    ((SSelectStmt*)pSubquery)->isSubquery = true;
583 584
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
585
  }
586
  return (SNode*)tempTable;
587 588
}

589
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
590
  CHECK_PARSER_STATUS(pCxt);
591 592 593
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
594 595
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
596
  joinTable->pOnCond = pJoinCond;
597
  return (SNode*)joinTable;
598
}
599

600
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
X
Xiaoyu Wang 已提交
601
  CHECK_PARSER_STATUS(pCxt);
602 603
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
wafwerar's avatar
wafwerar 已提交
604
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
605
  if (NULL != pOffset) {
wafwerar's avatar
wafwerar 已提交
606
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
607
  }
608
  return (SNode*)limitNode;
609 610
}

611
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
612
  CHECK_PARSER_STATUS(pCxt);
613 614
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
615
  orderByExpr->pExpr = pExpr;
616
  orderByExpr->order = order;
617 618 619
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
620
  orderByExpr->nullOrder = nullOrder;
621 622 623
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
624
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
625
  CHECK_PARSER_STATUS(pCxt);
626 627
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
628 629
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
630 631 632
  return (SNode*)session;
}

633
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
634
  CHECK_PARSER_STATUS(pCxt);
635 636
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
637
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
638
  if (NULL == state->pCol) {
639
    nodesDestroyNode((SNode*)state);
X
Xiaoyu Wang 已提交
640
    CHECK_OUT_OF_MEM(NULL);
641
  }
642
  state->pExpr = pExpr;
643 644 645
  return (SNode*)state;
}

X
Xiaoyu Wang 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond) {
  CHECK_PARSER_STATUS(pCxt);
  SEventWindowNode* pEvent = (SEventWindowNode*)nodesMakeNode(QUERY_NODE_EVENT_WINDOW);
  CHECK_OUT_OF_MEM(pEvent);
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
  if (NULL == pEvent->pCol) {
    nodesDestroyNode((SNode*)pEvent);
    CHECK_OUT_OF_MEM(NULL);
  }
  pEvent->pStartCond = pStartCond;
  pEvent->pEndCond = pEndCond;
  return (SNode*)pEvent;
}

X
Xiaoyu Wang 已提交
660 661
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                                SNode* pFill) {
X
Xiaoyu Wang 已提交
662
  CHECK_PARSER_STATUS(pCxt);
663 664
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
665
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
X
bugfix  
Xiaoyu Wang 已提交
666
  if (NULL == interval->pCol) {
667
    nodesDestroyNode((SNode*)interval);
X
Xiaoyu Wang 已提交
668
    CHECK_OUT_OF_MEM(NULL);
X
bugfix  
Xiaoyu Wang 已提交
669
  }
670 671 672 673 674 675 676 677
  interval->pInterval = pInterval;
  interval->pOffset = pOffset;
  interval->pSliding = pSliding;
  interval->pFill = pFill;
  return (SNode*)interval;
}

SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
X
Xiaoyu Wang 已提交
678
  CHECK_PARSER_STATUS(pCxt);
679 680 681 682
  SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
  CHECK_OUT_OF_MEM(fill);
  fill->mode = mode;
  fill->pValues = pValues;
X
Xiaoyu Wang 已提交
683 684
  fill->pWStartTs = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == fill->pWStartTs) {
685
    nodesDestroyNode((SNode*)fill);
X
Xiaoyu Wang 已提交
686
    CHECK_OUT_OF_MEM(NULL);
X
Xiaoyu Wang 已提交
687
  }
688
  strcpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart");
689
  return (SNode*)fill;
690
}
691

692
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
693
  CHECK_PARSER_STATUS(pCxt);
694 695 696 697 698 699 700 701
  SGroupingSetNode* groupingSet = (SGroupingSetNode*)nodesMakeNode(QUERY_NODE_GROUPING_SET);
  CHECK_OUT_OF_MEM(groupingSet);
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
  groupingSet->pParameterList = nodesMakeList();
  nodesListAppend(groupingSet->pParameterList, pNode);
  return (SNode*)groupingSet;
}

X
Xiaoyu Wang 已提交
702 703
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
  CHECK_PARSER_STATUS(pCxt);
704
  return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
X
Xiaoyu Wang 已提交
705 706
}

707 708 709 710 711
SNode* createInterpTimePoint(SAstCreateContext* pCxt, SNode* pPoint) {
  CHECK_PARSER_STATUS(pCxt);
  return createOperatorNode(pCxt, OP_TYPE_EQUAL, createPrimaryKeyCol(pCxt, NULL), pPoint);
}

X
Xiaoyu Wang 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
  CHECK_PARSER_STATUS(pCxt);
  SWhenThenNode* pWhenThen = (SWhenThenNode*)nodesMakeNode(QUERY_NODE_WHEN_THEN);
  CHECK_OUT_OF_MEM(pWhenThen);
  pWhenThen->pWhen = pWhen;
  pWhenThen->pThen = pThen;
  return (SNode*)pWhenThen;
}

SNode* createCaseWhenNode(SAstCreateContext* pCxt, SNode* pCase, SNodeList* pWhenThenList, SNode* pElse) {
  CHECK_PARSER_STATUS(pCxt);
  SCaseWhenNode* pCaseWhen = (SCaseWhenNode*)nodesMakeNode(QUERY_NODE_CASE_WHEN);
  CHECK_OUT_OF_MEM(pCaseWhen);
  pCaseWhen->pCase = pCase;
  pCaseWhen->pWhenThenList = pWhenThenList;
  pCaseWhen->pElse = pElse;
  return (SNode*)pCaseWhen;
}

X
Xiaoyu Wang 已提交
731
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
X
Xiaoyu Wang 已提交
732
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
733
  trimEscape(pAlias);
X
Xiaoyu Wang 已提交
734 735 736 737 738 739
  SExprNode* pExpr = (SExprNode*)pNode;
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
  strncpy(pExpr->aliasName, pAlias->z, len);
  pExpr->aliasName[len] = '\0';
  strncpy(pExpr->userAlias, pAlias->z, len);
  pExpr->userAlias[len] = '\0';
740
  pExpr->asAlias = true;
741
  return pNode;
742 743
}

744
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
X
Xiaoyu Wang 已提交
745
  CHECK_PARSER_STATUS(pCxt);
746
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
747
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
748 749 750
  }
  return pStmt;
}
751

752
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
X
Xiaoyu Wang 已提交
753
  CHECK_PARSER_STATUS(pCxt);
754
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
755
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
756 757
  }
  return pStmt;
758 759
}

760
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
X
Xiaoyu Wang 已提交
761
  CHECK_PARSER_STATUS(pCxt);
762
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
763
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
764 765 766
  }
  return pStmt;
}
767

768
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
X
Xiaoyu Wang 已提交
769
  CHECK_PARSER_STATUS(pCxt);
770
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
771
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
772 773
  }
  return pStmt;
774 775
}

776
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
X
Xiaoyu Wang 已提交
777
  CHECK_PARSER_STATUS(pCxt);
778
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
779
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
780 781 782 783 784
  }
  return pStmt;
}

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
X
Xiaoyu Wang 已提交
785
  CHECK_PARSER_STATUS(pCxt);
786 787 788
  if (NULL == pOrderByList) {
    return pStmt;
  }
789
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
790
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
791 792
  } else {
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
793 794 795
  }
  return pStmt;
}
796

797
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
X
Xiaoyu Wang 已提交
798
  CHECK_PARSER_STATUS(pCxt);
799 800 801
  if (NULL == pSlimit) {
    return pStmt;
  }
802
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
803
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
804 805
  }
  return pStmt;
806 807
}

808
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
X
Xiaoyu Wang 已提交
809
  CHECK_PARSER_STATUS(pCxt);
810 811 812
  if (NULL == pLimit) {
    return pStmt;
  }
813
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
814
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
X
Xiaoyu Wang 已提交
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
  } else {
    ((SSetOperator*)pStmt)->pLimit = pLimit;
  }
  return pStmt;
}

SNode* addRangeClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pRange) {
  CHECK_PARSER_STATUS(pCxt);
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    ((SSelectStmt*)pStmt)->pRange = pRange;
  }
  return pStmt;
}

SNode* addEveryClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pEvery) {
  CHECK_PARSER_STATUS(pCxt);
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    ((SSelectStmt*)pStmt)->pEvery = pEvery;
  }
  return pStmt;
}

SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
839 840 841
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
    SFillNode* pFillClause = (SFillNode*)pFill;
    nodesDestroyNode(pFillClause->pWStartTs);
842
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
X
Xiaoyu Wang 已提交
843
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
844
  }
845
  return pStmt;
846 847 848
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
849
  CHECK_PARSER_STATUS(pCxt);
850
  SNode* select = createSelectStmtImpl(isDistinct, pProjectionList, pTable);
851
  CHECK_OUT_OF_MEM(select);
852
  return select;  
853 854
}

X
Xiaoyu Wang 已提交
855 856 857 858 859 860
static void setSubquery(SNode* pStmt) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    ((SSelectStmt*)pStmt)->isSubquery = true;
  }
}

861
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
862
  CHECK_PARSER_STATUS(pCxt);
863 864 865
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
866
  setOp->pLeft = pLeft;
X
Xiaoyu Wang 已提交
867
  setSubquery(setOp->pLeft);
868
  setOp->pRight = pRight;
X
Xiaoyu Wang 已提交
869
  setSubquery(setOp->pRight);
870
  sprintf(setOp->stmtName, "%p", setOp);
871
  return (SNode*)setOp;
872 873
}

874 875 876 877 878 879 880 881 882 883 884 885 886 887
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
  if (!pOptions->walRetentionPeriodIsSet) {
    pOptions->walRetentionPeriod =
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
  }
  if (!pOptions->walRetentionSizeIsSet) {
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
  }
  if (!pOptions->walRollPeriodIsSet) {
    pOptions->walRollPeriod =
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
  }
}

X
Xiaoyu Wang 已提交
888
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
889
  CHECK_PARSER_STATUS(pCxt);
890
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
891
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
892
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
893 894
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
X
Xiaoyu Wang 已提交
895 896 897 898 899 900 901 902 903 904
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
905
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
X
Xiaoyu Wang 已提交
906 907 908 909 910 911
  pOptions->precision = TSDB_DEFAULT_PRECISION;
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
X
Xiaoyu Wang 已提交
912
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
913
  updateWalOptionsDefault(pOptions);
914
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
915
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
916 917
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
X
Xiaoyu Wang 已提交
918
  return (SNode*)pOptions;
919 920
}

X
Xiaoyu Wang 已提交
921
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
922
  CHECK_PARSER_STATUS(pCxt);
923
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
924 925
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
926
  pOptions->cacheModel = -1;
927
  pOptions->cacheLastSize = -1;
X
Xiaoyu Wang 已提交
928 929 930 931 932 933 934 935 936 937
  pOptions->compressionLevel = -1;
  pOptions->daysPerFile = -1;
  pOptions->fsyncPeriod = -1;
  pOptions->maxRowsPerBlock = -1;
  pOptions->minRowsPerBlock = -1;
  pOptions->keep[0] = -1;
  pOptions->keep[1] = -1;
  pOptions->keep[2] = -1;
  pOptions->pages = -1;
  pOptions->pagesize = -1;
938
  pOptions->tsdbPageSize = -1;
X
Xiaoyu Wang 已提交
939 940 941 942 943 944
  pOptions->precision = -1;
  pOptions->replica = -1;
  pOptions->strict = -1;
  pOptions->walLevel = -1;
  pOptions->numOfVgroups = -1;
  pOptions->singleStable = -1;
X
Xiaoyu Wang 已提交
945
  pOptions->schemaless = -1;
946 947
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
  pOptions->walRetentionSize = -2;    // -1 is a valid value
948 949
  pOptions->walRollPeriod = -1;
  pOptions->walSegmentSize = -1;
950
  pOptions->sstTrigger = -1;
951 952
  pOptions->tablePrefix = -1;
  pOptions->tableSuffix = -1;
X
Xiaoyu Wang 已提交
953 954 955
  return (SNode*)pOptions;
}

956 957
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
                                    bool alter) {
X
Xiaoyu Wang 已提交
958
  CHECK_PARSER_STATUS(pCxt);
959
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
X
Xiaoyu Wang 已提交
960 961
  switch (type) {
    case DB_OPTION_BUFFER:
962
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
963
      break;
964
    case DB_OPTION_CACHEMODEL:
965
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
966
      break;
967
    case DB_OPTION_CACHESIZE:
968
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
969 970
      break;
    case DB_OPTION_COMP:
971
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
972
      break;
X
Xiaoyu Wang 已提交
973 974 975
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
976
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
977
      } else {
978
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
X
Xiaoyu Wang 已提交
979
      }
X
Xiaoyu Wang 已提交
980
      break;
X
Xiaoyu Wang 已提交
981
    }
X
Xiaoyu Wang 已提交
982
    case DB_OPTION_FSYNC:
983
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
984 985
      break;
    case DB_OPTION_MAXROWS:
986
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
987 988
      break;
    case DB_OPTION_MINROWS:
989
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
990 991
      break;
    case DB_OPTION_KEEP:
992
      pDbOptions->pKeep = pVal;
X
Xiaoyu Wang 已提交
993 994
      break;
    case DB_OPTION_PAGES:
995
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
996 997
      break;
    case DB_OPTION_PAGESIZE:
998
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
999
      break;
1000 1001 1002
    case DB_OPTION_TSDB_PAGESIZE:
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
X
Xiaoyu Wang 已提交
1003
    case DB_OPTION_PRECISION:
1004
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
1005 1006
      break;
    case DB_OPTION_REPLICA:
1007
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
1008 1009 1010
      if (!alter) {
        updateWalOptionsDefault(pDbOptions);
      }
X
Xiaoyu Wang 已提交
1011
      break;
X
Xiaoyu Wang 已提交
1012
    case DB_OPTION_STRICT:
1013
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
1014 1015
      break;
    case DB_OPTION_WAL:
1016
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1017 1018
      break;
    case DB_OPTION_VGROUPS:
1019
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1020 1021
      break;
    case DB_OPTION_SINGLE_STABLE:
1022
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1023 1024
      break;
    case DB_OPTION_RETENTIONS:
1025
      pDbOptions->pRetentions = pVal;
X
Xiaoyu Wang 已提交
1026
      break;
X
Xiaoyu Wang 已提交
1027
    case DB_OPTION_WAL_RETENTION_PERIOD:
1028 1029
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionPeriodIsSet = true;
X
Xiaoyu Wang 已提交
1030 1031
      break;
    case DB_OPTION_WAL_RETENTION_SIZE:
1032 1033
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionSizeIsSet = true;
X
Xiaoyu Wang 已提交
1034 1035
      break;
    case DB_OPTION_WAL_ROLL_PERIOD:
1036 1037
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRollPeriodIsSet = true;
X
Xiaoyu Wang 已提交
1038 1039
      break;
    case DB_OPTION_WAL_SEGMENT_SIZE:
1040
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1041
      break;
1042
    case DB_OPTION_STT_TRIGGER:
1043 1044
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
1045
    case DB_OPTION_TABLE_PREFIX: {
1046
      SValueNode* pNode = (SValueNode*)pVal;
1047 1048 1049 1050
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
      } else {
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
1051
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1052
      }
D
dapan1121 已提交
1053
      nodesDestroyNode((SNode*)pNode);
1054
      break;
1055
    }
1056 1057
    case DB_OPTION_TABLE_SUFFIX: {
      SValueNode* pNode = (SValueNode*)pVal;
1058 1059 1060 1061
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
      } else {
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
1062
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1063
      }
D
dapan1121 已提交
1064
      nodesDestroyNode((SNode*)pNode);
1065
      break;
1066
    }
X
Xiaoyu Wang 已提交
1067 1068
    default:
      break;
X
Xiaoyu Wang 已提交
1069
  }
X
Xiaoyu Wang 已提交
1070
  return pOptions;
X
Xiaoyu Wang 已提交
1071 1072
}

1073 1074 1075 1076
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
}

X
Xiaoyu Wang 已提交
1077
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
1078
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1079 1080 1081
  switch (pAlterOption->type) {
    case DB_OPTION_KEEP:
    case DB_OPTION_RETENTIONS:
1082
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
X
Xiaoyu Wang 已提交
1083 1084 1085
    default:
      break;
  }
1086
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
X
Xiaoyu Wang 已提交
1087 1088
}

X
Xiaoyu Wang 已提交
1089
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1090
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1091
  if (!checkDbName(pCxt, pDbName, false)) {
1092 1093 1094 1095
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1096
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1097
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1098
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1099
  return (SNode*)pStmt;
1100
}
X
Xiaoyu Wang 已提交
1101

X
Xiaoyu Wang 已提交
1102
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1103
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1104
  if (!checkDbName(pCxt, pDbName, false)) {
1105 1106 1107 1108
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1109
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1110 1111 1112 1113
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1114
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1115
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1116
  if (!checkDbName(pCxt, pDbName, false)) {
1117 1118
    return NULL;
  }
1119
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
1120
  CHECK_OUT_OF_MEM(pStmt);
1121
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1122 1123 1124 1125
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1126 1127 1128 1129 1130
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
1131 1132 1133 1134 1135 1136
  SFlushDatabaseStmt* pStmt = (SFlushDatabaseStmt*)nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
}

1137
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
X
Xiaoyu Wang 已提交
1138 1139 1140 1141 1142
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
  STrimDatabaseStmt* pStmt = (STrimDatabaseStmt*)nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT);
X
Xiaoyu Wang 已提交
1143 1144
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1145
  pStmt->maxSpeed = maxSpeed;
X
Xiaoyu Wang 已提交
1146 1147 1148
  return (SNode*)pStmt;
}

1149
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
X
Xiaoyu Wang 已提交
1150 1151 1152 1153 1154 1155 1156
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
  SCompactDatabaseStmt* pStmt = (SCompactDatabaseStmt*)nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1157 1158
  pStmt->pStart = pStart;
  pStmt->pEnd = pEnd;
X
Xiaoyu Wang 已提交
1159 1160 1161
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1162
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1163
  CHECK_PARSER_STATUS(pCxt);
1164
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
1165
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
1166 1167 1168 1169
  pOptions->maxDelay1 = -1;
  pOptions->maxDelay2 = -1;
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
X
Xiaoyu Wang 已提交
1170
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
X
Xiaoyu Wang 已提交
1171
  pOptions->commentNull = true;  // mark null
1172
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
1173 1174
}

X
Xiaoyu Wang 已提交
1175
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1176
  CHECK_PARSER_STATUS(pCxt);
1177
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
1178 1179
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->ttl = -1;
X
Xiaoyu Wang 已提交
1180
  pOptions->commentNull = true;  // mark null
X
Xiaoyu Wang 已提交
1181 1182 1183 1184
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
1185
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1186
  switch (type) {
X
Xiaoyu Wang 已提交
1187
    case TABLE_OPTION_COMMENT:
1188
      if (checkComment(pCxt, (SToken*)pVal, true)) {
wmmhello's avatar
wmmhello 已提交
1189
        ((STableOptions*)pOptions)->commentNull = false;
1190
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
1191
      }
X
Xiaoyu Wang 已提交
1192
      break;
X
Xiaoyu Wang 已提交
1193 1194 1195 1196 1197
    case TABLE_OPTION_MAXDELAY:
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
      break;
    case TABLE_OPTION_WATERMARK:
      ((STableOptions*)pOptions)->pWatermark = pVal;
X
Xiaoyu Wang 已提交
1198
      break;
X
Xiaoyu Wang 已提交
1199 1200 1201
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
X
Xiaoyu Wang 已提交
1202
    case TABLE_OPTION_TTL: {
1203
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1204
      if (ttl > INT32_MAX) {
1205 1206 1207 1208
        ttl = INT32_MAX;
      }
      // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
      ((STableOptions*)pOptions)->ttl = ttl;
X
Xiaoyu Wang 已提交
1209
      break;
1210
    }
X
Xiaoyu Wang 已提交
1211 1212
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
1213
      break;
1214 1215 1216
    case TABLE_OPTION_DELETE_MARK:
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
      break;
X
Xiaoyu Wang 已提交
1217 1218
    default:
      break;
X
Xiaoyu Wang 已提交
1219
  }
X
Xiaoyu Wang 已提交
1220
  return pOptions;
X
Xiaoyu Wang 已提交
1221 1222
}

X
Xiaoyu Wang 已提交
1223
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
1224
  CHECK_PARSER_STATUS(pCxt);
1225
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
1226 1227
    return NULL;
  }
X
Xiaoyu Wang 已提交
1228 1229
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
1230
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
X
Xiaoyu Wang 已提交
1231 1232
  pCol->dataType = dataType;
  if (NULL != pComment) {
1233
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
1234
  }
1235
  pCol->sma = true;
X
Xiaoyu Wang 已提交
1236 1237 1238
  return (SNode*)pCol;
}

1239
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
1240
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
1241 1242 1243 1244
  return dt;
}

SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
K
kailixu 已提交
1245
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = taosStr2Int32(pLen->z, NULL, 10)};
1246 1247 1248
  return dt;
}

X
Xiaoyu Wang 已提交
1249 1250
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1251
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1252 1253
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1254 1255
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1256 1257
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
1258
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
1259
  pStmt->pOptions = (STableOptions*)pOptions;
1260
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1261 1262
  return (SNode*)pStmt;
}
1263

X
Xiaoyu Wang 已提交
1264
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
1265
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1266
  CHECK_PARSER_STATUS(pCxt);
1267
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
1268
  CHECK_OUT_OF_MEM(pStmt);
1269 1270 1271 1272
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
  strcpy(pStmt->useDbName, ((SRealTableNode*)pUseRealTable)->table.dbName);
  strcpy(pStmt->useTableName, ((SRealTableNode*)pUseRealTable)->table.tableName);
1273 1274 1275
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
wmmhello's avatar
wmmhello 已提交
1276
  pStmt->pOptions = (STableOptions*)pOptions;
1277 1278
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
1279 1280 1281 1282
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
1283
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1284
  SCreateMultiTablesStmt* pStmt = (SCreateMultiTablesStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT);
1285 1286 1287 1288 1289
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

1290
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1291
  CHECK_PARSER_STATUS(pCxt);
1292
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
1293 1294 1295 1296 1297 1298 1299 1300 1301
  CHECK_OUT_OF_MEM(pStmt);
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
  pStmt->ignoreNotExists = ignoreNotExists;
  nodesDestroyNode(pRealTable);
  return (SNode*)pStmt;
}

SNode* createDropTableStmt(SAstCreateContext* pCxt, SNodeList* pTables) {
X
Xiaoyu Wang 已提交
1302
  CHECK_PARSER_STATUS(pCxt);
1303
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
1304 1305 1306 1307 1308
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

1309
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1310
  CHECK_PARSER_STATUS(pCxt);
1311
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
1312 1313 1314 1315 1316 1317 1318 1319
  CHECK_OUT_OF_MEM(pStmt);
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
  pStmt->ignoreNotExists = ignoreNotExists;
  nodesDestroyNode(pRealTable);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1320 1321 1322 1323 1324 1325 1326
static SNode* createAlterTableStmtFinalize(SNode* pRealTable, SAlterTableStmt* pStmt) {
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
  nodesDestroyNode(pRealTable);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1327
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1328
  CHECK_PARSER_STATUS(pCxt);
1329
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1330 1331 1332
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1333
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1334 1335
}

X
Xiaoyu Wang 已提交
1336 1337
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
                                    SDataType dataType) {
X
Xiaoyu Wang 已提交
1338 1339
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1340 1341
    return NULL;
  }
1342
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1343 1344
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1345
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1346
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
1347
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1348 1349
}

X
Xiaoyu Wang 已提交
1350
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1351 1352
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1353 1354
    return NULL;
  }
1355
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1356 1357
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1358
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
X
Xiaoyu Wang 已提交
1359
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1360 1361
}

X
Xiaoyu Wang 已提交
1362 1363
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1364 1365
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1366 1367
    return NULL;
  }
1368
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1369 1370
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1371 1372
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
X
Xiaoyu Wang 已提交
1373
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1374 1375
}

X
Xiaoyu Wang 已提交
1376
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1377 1378
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
X
bugfix  
Xiaoyu Wang 已提交
1379 1380
    return NULL;
  }
1381
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1382 1383
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
1384
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
1385
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1386
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1387 1388
}

1389 1390 1391 1392 1393
SNode* setAlterSuperTableType(SNode* pStmt) {
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
  return pStmt;
}

1394
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1395
  CHECK_PARSER_STATUS(pCxt);
1396 1397 1398
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1399 1400
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1401
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1402 1403 1404
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1405
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1406
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
1407
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
1408
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type;
X
Xiaoyu Wang 已提交
1409 1410
}

1411 1412 1413 1414 1415 1416 1417 1418 1419
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
  CHECK_PARSER_STATUS(pCxt);
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
                              EOperatorType tableCondType) {
X
Xiaoyu Wang 已提交
1420
  CHECK_PARSER_STATUS(pCxt);
1421
  if (needDbShowStmt(type) && NULL == pDbName) {
X
Xiaoyu Wang 已提交
1422
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1423
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1424 1425
    return NULL;
  }
1426
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1427
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1428
  pStmt->pDbName = pDbName;
1429 1430
  pStmt->pTbName = pTbName;
  pStmt->tableCondType = tableCondType;
1431
  return (SNode*)pStmt;
1432
}
1433

1434
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1435
  CHECK_PARSER_STATUS(pCxt);
1436 1437 1438 1439
  if (!checkDbName(pCxt, pDbName, true)) {
    return NULL;
  }
  SShowCreateDatabaseStmt* pStmt = (SShowCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
1440
  CHECK_OUT_OF_MEM(pStmt);
1441 1442
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
1443 1444
}

1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
  CHECK_PARSER_STATUS(pCxt);
  SToken  dbToken = {0};
  SToken* pDbToken = NULL;

  if (pNode) {
    SValueNode* pDbName = (SValueNode*)pNode;
    if (pDbName->literal) {
      dbToken.z = pDbName->literal;
      dbToken.n = strlen(pDbName->literal);
      pDbToken = &dbToken;
    }
  }

  if (pDbToken && !checkDbName(pCxt, pDbToken, true)) {
    nodesDestroyNode(pNode);
    return NULL;
  }

  SShowAliveStmt* pStmt = (SShowAliveStmt*)nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);

  if (pDbToken) {
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
  }
  if (pNode) {
    nodesDestroyNode(pNode);
  }

  return (SNode*)pStmt;
}

1477
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1478
  CHECK_PARSER_STATUS(pCxt);
1479
  SShowCreateTableStmt* pStmt = (SShowCreateTableStmt*)nodesMakeNode(type);
1480
  CHECK_OUT_OF_MEM(pStmt);
1481 1482
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
  nodesDestroyNode(pRealTable);
  return (SNode*)pStmt;
}

SNode* createShowTableDistributedStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
  CHECK_PARSER_STATUS(pCxt);
  SShowTableDistributedStmt* pStmt = (SShowTableDistributedStmt*)nodesMakeNode(QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
  nodesDestroyNode(pRealTable);
1494
  return (SNode*)pStmt;
1495 1496
}

1497
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
1498 1499 1500 1501
  CHECK_PARSER_STATUS(pCxt);
  SShowDnodeVariablesStmt* pStmt = (SShowDnodeVariablesStmt*)nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDnodeId = pDnodeId;
1502
  pStmt->pLikePattern = pLikePattern;
1503 1504 1505
  return (SNode*)pStmt;
}

1506 1507 1508 1509 1510 1511 1512 1513 1514
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
  CHECK_PARSER_STATUS(pCxt);
  SShowVnodesStmt* pStmt = (SShowVnodesStmt*)nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDnodeId = pDnodeId;
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
  return (SNode*)pStmt;
}

1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
  CHECK_PARSER_STATUS(pCxt);
  if (NULL == pDbName) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
    return NULL;
  }
  SShowTableTagsStmt* pStmt = (SShowTableTagsStmt*)nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDbName = pDbName;
  pStmt->pTbName = pTbName;
  pStmt->pTags = pTags;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1530
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo) {
X
Xiaoyu Wang 已提交
1531
  CHECK_PARSER_STATUS(pCxt);
1532
  char password[TSDB_USET_PASSWORD_LEN + 3] = {0};
1533 1534 1535 1536 1537
  if (!checkUserName(pCxt, pUserName) || !checkPassword(pCxt, pPassword, password)) {
    return NULL;
  }
  SCreateUserStmt* pStmt = (SCreateUserStmt*)nodesMakeNode(QUERY_NODE_CREATE_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1538
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1539
  strcpy(pStmt->password, password);
X
Xiaoyu Wang 已提交
1540
  pStmt->sysinfo = sysinfo;
1541 1542 1543
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1544
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1545
  CHECK_PARSER_STATUS(pCxt);
1546 1547 1548 1549 1550
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1551
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
X
Xiaoyu Wang 已提交
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
  pStmt->alterType = alterType;
  switch (alterType) {
    case TSDB_ALTER_USER_PASSWD: {
      char password[TSDB_USET_PASSWORD_LEN] = {0};
      if (!checkPassword(pCxt, pVal, password)) {
        nodesDestroyNode((SNode*)pStmt);
        return NULL;
      }
      strcpy(pStmt->password, password);
      break;
1562
    }
X
Xiaoyu Wang 已提交
1563 1564 1565 1566 1567 1568 1569 1570
    case TSDB_ALTER_USER_ENABLE:
      pStmt->enable = taosStr2Int8(pVal->z, NULL, 10);
      break;
    case TSDB_ALTER_USER_SYSINFO:
      pStmt->sysinfo = taosStr2Int8(pVal->z, NULL, 10);
      break;
    default:
      break;
1571 1572 1573 1574
  }
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1575
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1576
  CHECK_PARSER_STATUS(pCxt);
1577 1578 1579 1580 1581
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1582
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1583 1584 1585 1586
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1587
  CHECK_PARSER_STATUS(pCxt);
1588 1589
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1590 1591 1592
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
    nodesDestroyNode((SNode*)pStmt);
    return NULL;
1593 1594 1595 1596
  }
  return (SNode*)pStmt;
}

D
dapan1121 已提交
1597
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force, bool unsafe) {
X
Xiaoyu Wang 已提交
1598
  CHECK_PARSER_STATUS(pCxt);
1599 1600 1601
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1602
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1603
  } else {
1604
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
1605
      nodesDestroyNode((SNode*)pStmt);
1606 1607 1608
      return NULL;
    }
  }
1609
  pStmt->force = force;
D
dapan1121 已提交
1610
  pStmt->unsafe = unsafe;
1611 1612
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1613

X
Xiaoyu Wang 已提交
1614 1615
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1616
  CHECK_PARSER_STATUS(pCxt);
1617
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1618
  CHECK_OUT_OF_MEM(pStmt);
1619 1620 1621 1622 1623
  if (NULL != pDnode) {
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
  } else {
    pStmt->dnodeId = -1;
  }
1624 1625 1626 1627 1628 1629 1630
  trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
  if (NULL != pValue) {
    trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
  }
  return (SNode*)pStmt;
}

1631 1632 1633 1634 1635 1636 1637
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
  if (!checkIndexName(pCxt, pIndexName)) {
    return NULL;
  }
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
}

X
Xiaoyu Wang 已提交
1638
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
1639
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1640
  CHECK_PARSER_STATUS(pCxt);
1641
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1642 1643
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1644
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1645 1646 1647 1648
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1649
  nodesDestroyNode(pIndexName);
1650
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1651 1652 1653 1654 1655
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

1656 1657
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                         SNode* pStreamOptions) {
X
Xiaoyu Wang 已提交
1658
  CHECK_PARSER_STATUS(pCxt);
1659
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1660 1661 1662 1663 1664
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
1665
  pOptions->pStreamOptions = pStreamOptions;
X
Xiaoyu Wang 已提交
1666 1667
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1668

X
Xiaoyu Wang 已提交
1669
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
X
Xiaoyu Wang 已提交
1670
  CHECK_PARSER_STATUS(pCxt);
1671
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1672
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1673
  pStmt->ignoreNotExists = ignoreNotExists;
X
Xiaoyu Wang 已提交
1674 1675
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
X
Xiaoyu Wang 已提交
1676
  nodesDestroyNode(pIndexName);
1677 1678 1679
  return (SNode*)pStmt;
}

1680
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1681
  CHECK_PARSER_STATUS(pCxt);
1682
  SCreateComponentNodeStmt* pStmt = (SCreateComponentNodeStmt*)nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1683
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1684
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
X
Xiaoyu Wang 已提交
1685 1686
  return (SNode*)pStmt;
}
1687

1688
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1689
  CHECK_PARSER_STATUS(pCxt);
1690
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1691
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1692
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1693 1694 1695
  return (SNode*)pStmt;
}

D
dapan1121 已提交
1696 1697 1698 1699 1700 1701 1702 1703
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
  CHECK_PARSER_STATUS(pCxt);
  SRestoreComponentNodeStmt* pStmt = (SRestoreComponentNodeStmt*)nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
  return (SNode*)pStmt;
}

1704
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1705
  CHECK_PARSER_STATUS(pCxt);
1706 1707 1708
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
1709
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1710
  CHECK_OUT_OF_MEM(pStmt);
1711
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1712
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1713 1714 1715 1716
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}

1717
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
1718
                                  int8_t withMeta) {
X
Xiaoyu Wang 已提交
1719
  CHECK_PARSER_STATUS(pCxt);
1720
  if (!checkTopicName(pCxt, pTopicName) || !checkDbName(pCxt, pSubDbName, true)) {
X
Xiaoyu Wang 已提交
1721 1722
    return NULL;
  }
X
Xiaoyu Wang 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  pStmt->ignoreExists = ignoreExists;
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
  pStmt->withMeta = withMeta;
  return (SNode*)pStmt;
}

1732
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
1733
                                     int8_t withMeta, SNode* pWhere) {
X
Xiaoyu Wang 已提交
1734
  CHECK_PARSER_STATUS(pCxt);
1735 1736 1737
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
1738 1739 1740 1741 1742
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  pStmt->ignoreExists = ignoreExists;
  pStmt->withMeta = withMeta;
1743
  pStmt->pWhere = pWhere;
wmmhello's avatar
wmmhello 已提交
1744

X
Xiaoyu Wang 已提交
1745 1746 1747
  strcpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName);
  nodesDestroyNode(pRealTable);
1748 1749 1750
  return (SNode*)pStmt;
}

1751
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1752
  CHECK_PARSER_STATUS(pCxt);
1753 1754 1755
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
1756
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1757
  CHECK_OUT_OF_MEM(pStmt);
1758
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1759 1760 1761
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1762

D
dapan1121 已提交
1763
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pCGroupId,
1764
                            SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1765
  CHECK_PARSER_STATUS(pCxt);
1766 1767 1768
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
D
dapan1121 已提交
1769 1770 1771
  if (!checkCGroupName(pCxt, pCGroupId)) {
    return NULL;
  }
1772
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1773 1774
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
1775 1776
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
X
Xiaoyu Wang 已提交
1777 1778 1779
  return (SNode*)pStmt;
}

1780
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1781
  CHECK_PARSER_STATUS(pCxt);
1782
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1783 1784 1785 1786 1787 1788 1789
  CHECK_OUT_OF_MEM(pStmt);
  trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
  if (NULL != pValue) {
    trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
  }
  return (SNode*)pStmt;
}
1790 1791

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1792
  CHECK_PARSER_STATUS(pCxt);
1793
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1794 1795 1796 1797 1798 1799 1800
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->verbose = TSDB_DEFAULT_EXPLAIN_VERBOSE;
  pOptions->ratio = TSDB_DEFAULT_EXPLAIN_RATIO;
  return (SNode*)pOptions;
}

SNode* setExplainVerbose(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1801
  CHECK_PARSER_STATUS(pCxt);
1802 1803 1804 1805 1806
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1807
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1808
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1809 1810 1811 1812
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1813
  CHECK_PARSER_STATUS(pCxt);
1814
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1815 1816 1817 1818 1819 1820
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1821 1822

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1823
  CHECK_PARSER_STATUS(pCxt);
1824
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1825 1826 1827 1828 1829 1830 1831 1832
  CHECK_OUT_OF_MEM(pStmt);
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
  nodesDestroyNode(pRealTable);
  return (SNode*)pStmt;
}

SNode* createResetQueryCacheStmt(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1833
  CHECK_PARSER_STATUS(pCxt);
1834 1835 1836 1837
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1838

1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
  } else {
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
                                            "udf programming language supports c and python");
  }
  return pCxt->errCode;
}

X
Xiaoyu Wang 已提交
1851
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
1852 1853
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
                                bool orReplace) {
X
Xiaoyu Wang 已提交
1854
  CHECK_PARSER_STATUS(pCxt);
1855
  if (pLibPath->n <= 2) {
1856
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1857 1858
    return NULL;
  }
1859 1860 1861 1862
  int8_t language = 0;
  if (TSDB_CODE_SUCCESS != convertUdfLanguageType(pCxt, pLanguage, &language)) {
    return NULL;
  }
1863
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1864
  CHECK_OUT_OF_MEM(pStmt);
1865
  pStmt->orReplace = orReplace;
1866
  pStmt->ignoreExists = ignoreExists;
1867
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1868
  pStmt->isAgg = aggFunc;
1869
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
1870 1871
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
1872
  pStmt->language = language;
1873
  return (SNode*)pStmt;
1874 1875
}

1876
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1877
  CHECK_PARSER_STATUS(pCxt);
1878
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1879
  CHECK_OUT_OF_MEM(pStmt);
1880
  pStmt->ignoreNotExists = ignoreNotExists;
1881
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1882
  return (SNode*)pStmt;
1883 1884
}

1885
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1886
  CHECK_PARSER_STATUS(pCxt);
1887
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1888
  CHECK_OUT_OF_MEM(pOptions);
5
54liuyao 已提交
1889
  pOptions->triggerType = STREAM_TRIGGER_WINDOW_CLOSE;
1890
  pOptions->fillHistory = STREAM_DEFAULT_FILL_HISTORY;
X
Xiaoyu Wang 已提交
1891
  pOptions->ignoreExpired = STREAM_DEFAULT_IGNORE_EXPIRED;
1892
  pOptions->ignoreUpdate = STREAM_DEFAULT_IGNORE_UPDATE;
1893 1894 1895
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
static int8_t getTriggerType(uint32_t tokenType) {
  switch (tokenType) {
    case TK_AT_ONCE:
      return STREAM_TRIGGER_AT_ONCE;
    case TK_WINDOW_CLOSE:
      return STREAM_TRIGGER_WINDOW_CLOSE;
    case TK_MAX_DELAY:
      return STREAM_TRIGGER_MAX_DELAY;
    default:
      break;
  }
  return STREAM_TRIGGER_WINDOW_CLOSE;
}

SNode* setStreamOptions(SAstCreateContext* pCxt, SNode* pOptions, EStreamOptionsSetFlag setflag, SToken* pToken,
                        SNode* pNode) {
  SStreamOptions* pStreamOptions = (SStreamOptions*)pOptions;
  if (BIT_FLAG_TEST_MASK(setflag, pStreamOptions->setFlag)) {
    pCxt->errCode =
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream options each item is only set once");
    return pOptions;
  }

  switch (setflag) {
    case SOPT_TRIGGER_TYPE_SET:
      pStreamOptions->triggerType = getTriggerType(pToken->type);
      if (STREAM_TRIGGER_MAX_DELAY == pStreamOptions->triggerType) {
        pStreamOptions->pDelay = pNode;
      }
      break;
    case SOPT_WATERMARK_SET:
      pStreamOptions->pWatermark = pNode;
      break;
    case SOPT_DELETE_MARK_SET:
      pStreamOptions->pDeleteMark = pNode;
      break;
    case SOPT_FILL_HISTORY_SET:
      pStreamOptions->fillHistory = taosStr2Int8(pToken->z, NULL, 10);
      break;
    case SOPT_IGNORE_EXPIRED_SET:
      pStreamOptions->ignoreExpired = taosStr2Int8(pToken->z, NULL, 10);
      break;
    case SOPT_IGNORE_UPDATE_SET:
      pStreamOptions->ignoreUpdate = taosStr2Int8(pToken->z, NULL, 10);
      break;
    default:
      break;
  }
  BIT_FLAG_SET_MASK(pStreamOptions->setFlag, setflag);

  return pOptions;
}

1949
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pStreamName, SNode* pRealTable,
1950
                              SNode* pOptions, SNodeList* pTags, SNode* pSubtable, SNode* pQuery, SNodeList* pCols) {
X
Xiaoyu Wang 已提交
1951
  CHECK_PARSER_STATUS(pCxt);
1952 1953 1954
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
1955
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1956
  CHECK_OUT_OF_MEM(pStmt);
1957
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1958 1959 1960
  strcpy(pStmt->targetDbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->targetTabName, ((SRealTableNode*)pRealTable)->table.tableName);
  nodesDestroyNode(pRealTable);
1961 1962 1963
  pStmt->ignoreExists = ignoreExists;
  pStmt->pOptions = (SStreamOptions*)pOptions;
  pStmt->pQuery = pQuery;
1964 1965
  pStmt->pTags = pTags;
  pStmt->pSubtable = pSubtable;
1966
  pStmt->pCols = pCols;
1967
  return (SNode*)pStmt;
1968 1969
}

1970
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1971
  CHECK_PARSER_STATUS(pCxt);
1972 1973 1974
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
1975
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1976
  CHECK_OUT_OF_MEM(pStmt);
1977
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1978 1979
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1980 1981
}

1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
  SPauseStreamStmt* pStmt = (SPauseStreamStmt*)nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated,
                              SToken* pStreamName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
  SResumeStreamStmt* pStmt = (SResumeStreamStmt*)nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
  pStmt->ignoreNotExists = ignoreNotExists;
  pStmt->ignoreUntreated = ignoreUntreated;
  return (SNode*)pStmt;
}

2008
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
2009
  CHECK_PARSER_STATUS(pCxt);
2010
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
2011
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
2012
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
2013
  return (SNode*)pStmt;
2014 2015
}

X
Xiaoyu Wang 已提交
2016 2017 2018 2019
SNode* createKillQueryStmt(SAstCreateContext* pCxt, const SToken* pQueryId) {
  CHECK_PARSER_STATUS(pCxt);
  SKillQueryStmt* pStmt = (SKillQueryStmt*)nodesMakeNode(QUERY_NODE_KILL_QUERY_STMT);
  CHECK_OUT_OF_MEM(pStmt);
D
dapan1121 已提交
2020
  trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
X
Xiaoyu Wang 已提交
2021 2022 2023
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
2024 2025
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
2026
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
2027 2028 2029 2030
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

C
cadem 已提交
2031 2032 2033 2034 2035 2036 2037
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
  SBalanceVgroupLeaderStmt* pStmt = (SBalanceVgroupLeaderStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

2038
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
2039
  CHECK_PARSER_STATUS(pCxt);
2040
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
2041
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
2042 2043 2044
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
2045 2046 2047
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
2048
  CHECK_PARSER_STATUS(pCxt);
2049
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
2050
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
2051 2052 2053
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
2054 2055 2056
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
2057
  CHECK_PARSER_STATUS(pCxt);
2058
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
2059
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
2060 2061
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
2062 2063 2064
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
2065
  CHECK_PARSER_STATUS(pCxt);
2066 2067 2068 2069
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
2070

X
Xiaoyu Wang 已提交
2071 2072
SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
                       SNode* pTagCond) {
X
Xiaoyu Wang 已提交
2073
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
2074
  if (!checkDbName(pCxt, &pPrivLevel->first, false) || !checkUserName(pCxt, pUserName)) {
2075 2076
    return NULL;
  }
2077
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
2078 2079
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
X
Xiaoyu Wang 已提交
2080 2081 2082 2083
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
  }
2084
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
X
Xiaoyu Wang 已提交
2085
  pStmt->pTagCond = pTagCond;
2086 2087 2088
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
2089 2090
SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
                        SNode* pTagCond) {
X
Xiaoyu Wang 已提交
2091
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
2092
  if (!checkDbName(pCxt, &pPrivLevel->first, false) || !checkUserName(pCxt, pUserName)) {
2093 2094
    return NULL;
  }
2095
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
2096 2097
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
X
Xiaoyu Wang 已提交
2098 2099 2100 2101
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
  }
2102
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
X
Xiaoyu Wang 已提交
2103
  pStmt->pTagCond = pTagCond;
2104 2105
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
2106

2107
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
2108
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
2109
  CHECK_OUT_OF_MEM(pFunc);
X
Xiaoyu Wang 已提交
2110
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
2111
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt, NULL))) {
2112
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
2113 2114 2115 2116 2117
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
2118 2119
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
2120
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
2121 2122 2123
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
2124 2125 2126 2127
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
  if (NULL == pStmt->pCountFunc || NULL == pStmt->pFirstFunc || NULL == pStmt->pLastFunc) {
2128
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
2129 2130
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
2131 2132
  return (SNode*)pStmt;
}
2133 2134 2135 2136 2137 2138 2139 2140

SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
  CHECK_PARSER_STATUS(pCxt);
  SInsertStmt* pStmt = (SInsertStmt*)nodesMakeNode(QUERY_NODE_INSERT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTable = pTable;
  pStmt->pCols = pCols;
  pStmt->pQuery = pQuery;
2141 2142 2143 2144 2145
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
    strcpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias);
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
    strcpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias);
  }
2146 2147
  return (SNode*)pStmt;
}