parAstCreater.c 56.6 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"
X
Xiaoyu Wang 已提交
21
#include "ttime.h"
22

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

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

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

41
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
42
  memset(pCxt, 0, sizeof(SAstCreateContext));
43
  pCxt->pQueryCxt = pParseCxt;
44 45
  pCxt->msgBuf.buf = pParseCxt->pMsg;
  pCxt->msgBuf.len = pParseCxt->msgLen;
46 47
  pCxt->notSupport = false;
  pCxt->pRootNode = NULL;
D
dapan1121 已提交
48
  pCxt->placeholderNo = 0;
X
Xiaoyu Wang 已提交
49
  pCxt->pPlaceholderValues = NULL;
50
  pCxt->errCode = TSDB_CODE_SUCCESS;
51 52
}

X
Xiaoyu Wang 已提交
53 54 55 56 57 58
static void copyStringFormStringToken(SToken* pToken, char* pBuf, int32_t len) {
  if (pToken->n > 2) {
    strncpy(pBuf, pToken->z + 1, TMIN(pToken->n - 2, len - 1));
  }
}

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

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

81 82 83 84 85 86 87 88 89 90 91 92 93
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;
}

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

static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
  if (NULL == pEp) {
113
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
114
  } else if (pEp->n >= TSDB_FQDN_LEN + 2 + 6) {  // format 'fqdn:port'
115
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
X
Xiaoyu Wang 已提交
116 117 118 119 120 121 122
  } else {
    char ep[TSDB_FQDN_LEN + 2 + 6];
    strncpy(ep, pEp->z, pEp->n);
    strdequote(ep);
    strtrim(ep);
    char* pColon = strchr(ep, ':');
    if (NULL == pColon) {
123
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ENDPOINT);
X
Xiaoyu Wang 已提交
124 125
    } else {
      strncpy(pFqdn, ep, pColon - ep);
wafwerar's avatar
wafwerar 已提交
126
      *pPort = taosStr2Int32(pColon + 1, NULL, 10);
X
Xiaoyu Wang 已提交
127
      if (*pPort >= UINT16_MAX || *pPort <= 0) {
128
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
X
Xiaoyu Wang 已提交
129 130
      }
    }
131
  }
132
  return TSDB_CODE_SUCCESS == pCxt->errCode;
133 134 135 136
}

static bool checkFqdn(SAstCreateContext* pCxt, const SToken* pFqdn) {
  if (NULL == pFqdn) {
137
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
138 139
  } else {
    if (pFqdn->n >= TSDB_FQDN_LEN) {
140
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
X
Xiaoyu Wang 已提交
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS == pCxt->errCode;
144 145 146 147
}

static bool checkPort(SAstCreateContext* pCxt, const SToken* pPortToken, int32_t* pPort) {
  if (NULL == pPortToken) {
148
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
149
  } else {
wafwerar's avatar
wafwerar 已提交
150
    *pPort = taosStr2Int32(pPortToken->z, NULL, 10);
X
Xiaoyu Wang 已提交
151
    if (*pPort >= UINT16_MAX || *pPort <= 0) {
152
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
X
Xiaoyu Wang 已提交
153
    }
154
  }
155
  return TSDB_CODE_SUCCESS == pCxt->errCode;
156 157
}

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

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

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

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

199 200 201 202 203 204 205 206 207
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;
}

208
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
X
Xiaoyu Wang 已提交
209
  CHECK_PARSER_STATUS(pCxt);
210 211 212 213 214 215 216 217 218
  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 已提交
219
  CHECK_PARSER_STATUS(pCxt);
220 221 222 223 224 225 226 227 228
  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 已提交
229
  CHECK_PARSER_STATUS(pCxt);
230
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
231
  SNode*        pExpr = pRawExpr->pNode;
D
dapan1121 已提交
232
  if (nodesIsExprNode(pExpr)) {
233 234 235
    int32_t len = TMIN(sizeof(((SExprNode*)pExpr)->aliasName) - 1, pRawExpr->n);
    strncpy(((SExprNode*)pExpr)->aliasName, pRawExpr->p, len);
    ((SExprNode*)pExpr)->aliasName[len] = '\0';
D
dapan1121 已提交
236
  }
wafwerar's avatar
wafwerar 已提交
237
  taosMemoryFreeClear(pNode);
238
  return pExpr;
239 240 241
}

SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
242
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
243
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
244 245
    return nil_token;
  }
246
  SRawExprNode* target = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
247
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
248 249 250
  return t;
}

251
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
252
  CHECK_PARSER_STATUS(pCxt);
253 254
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
255
  pCxt->errCode = nodesListAppend(list, pNode);
X
Xiaoyu Wang 已提交
256
  return list;
257 258 259
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
260
  CHECK_PARSER_STATUS(pCxt);
261
  pCxt->errCode = nodesListAppend(pList, pNode);
X
Xiaoyu Wang 已提交
262
  return pList;
263 264
}

X
Xiaoyu Wang 已提交
265
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
X
Xiaoyu Wang 已提交
266
  CHECK_PARSER_STATUS(pCxt);
267
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
268 269 270 271
    return NULL;
  }
  SColumnNode* col = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(col);
272 273
  if (NULL != pTableAlias) {
    strncpy(col->tableAlias, pTableAlias->z, pTableAlias->n);
274 275
  }
  strncpy(col->colName, pColumnName->z, pColumnName->n);
276
  return (SNode*)col;
277
}
278

279
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
280
  CHECK_PARSER_STATUS(pCxt);
281 282
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
283 284 285 286
  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 已提交
287
  }
288
  CHECK_OUT_OF_MEM(val->literal);
289
  val->node.resType.type = dataType;
X
Xiaoyu Wang 已提交
290
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
291 292 293
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  }
294 295
  val->isDuration = false;
  val->translate = false;
296 297 298 299
  return (SNode*)val;
}

SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
300
  CHECK_PARSER_STATUS(pCxt);
301 302
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
303 304 305
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
306
  val->translate = false;
307 308 309
  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;
310
  return (SNode*)val;
311 312
}

X
Xiaoyu Wang 已提交
313
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
314
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
315 316 317 318
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
319 320 321 322 323 324 325 326 327 328 329 330
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
  val->literal = strdup(pCxt->pQueryCxt->db);
  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;
}

331
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, 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
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
D
dapan1121 已提交
337
  val->placeholderNo = ++pCxt->placeholderNo;
X
Xiaoyu Wang 已提交
338 339 340
  if (NULL == pCxt->pPlaceholderValues) {
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    if (NULL == pCxt->pPlaceholderValues) {
341
      nodesDestroyNode((SNode*)val);
X
Xiaoyu Wang 已提交
342 343 344 345
      return NULL;
    }
  }
  taosArrayPush(pCxt->pPlaceholderValues, &val);
346 347 348
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
349 350 351 352 353 354 355 356 357 358 359
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);
  }
}

360
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
X
Xiaoyu Wang 已提交
361
  CHECK_PARSER_STATUS(pCxt);
362 363 364 365
  SLogicConditionNode* cond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  CHECK_OUT_OF_MEM(cond);
  cond->condType = type;
  cond->pParameterList = nodesMakeList();
X
Xiaoyu Wang 已提交
366 367 368
  int32_t code = addParamToLogicConditionNode(cond, pParam1);
  if (TSDB_CODE_SUCCESS == code && NULL != pParam2) {
    code = addParamToLogicConditionNode(cond, pParam2);
X
Xiaoyu Wang 已提交
369
  }
X
Xiaoyu Wang 已提交
370
  if (TSDB_CODE_SUCCESS != code) {
371
    nodesDestroyNode((SNode*)cond);
X
Xiaoyu Wang 已提交
372
    return NULL;
373
  }
374 375 376 377
  return (SNode*)cond;
}

SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
378
  CHECK_PARSER_STATUS(pCxt);
379 380 381 382 383 384 385 386 387
  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 已提交
388
  CHECK_PARSER_STATUS(pCxt);
389
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
390 391
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
392 393 394
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
395
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
396 397
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
398 399
}

X
Xiaoyu Wang 已提交
400
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
401
  CHECK_PARSER_STATUS(pCxt);
402
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
403 404 405 406 407 408
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(pCol->colName, PK_TS_COL_INTERNAL_NAME);
  return (SNode*)pCol;
}

409
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
410
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
411 412 413
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
    return createPrimaryKeyCol(pCxt);
  }
414 415 416 417 418 419 420
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strncpy(func->functionName, pFuncName->z, pFuncName->n);
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

421
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
X
Xiaoyu Wang 已提交
422
  CHECK_PARSER_STATUS(pCxt);
423 424 425 426
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
427 428 429 430
  if (TSDB_DATA_TYPE_VARCHAR == dt.type) {
    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;
431
  }
432 433 434 435
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

436
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
437
  CHECK_PARSER_STATUS(pCxt);
438 439 440 441
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
442 443
}

X
Xiaoyu Wang 已提交
444
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
X
Xiaoyu Wang 已提交
445
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
446 447 448 449 450 451 452 453 454
  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 已提交
455
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
456
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
457
  if (!checkDbName(pCxt, pDbName, true) || !checkTableName(pCxt, pTableName) || !checkTableName(pCxt, pTableAlias)) {
458 459 460 461 462
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
463
    strncpy(realTable->table.dbName, pDbName->z, pDbName->n);
464 465 466
  } else {
    strcpy(realTable->table.dbName, pCxt->pQueryCxt->db);
  }
467
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
468 469 470
    strncpy(realTable->table.tableAlias, pTableAlias->z, pTableAlias->n);
  } else {
    strncpy(realTable->table.tableAlias, pTableName->z, pTableName->n);
471
  }
X
Xiaoyu Wang 已提交
472
  strncpy(realTable->table.tableName, pTableName->z, pTableName->n);
473
  return (SNode*)realTable;
474
}
475

476
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
477
  CHECK_PARSER_STATUS(pCxt);
478 479 480
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
481
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
482
    strncpy(tempTable->table.tableAlias, pTableAlias->z, pTableAlias->n);
X
Xiaoyu Wang 已提交
483 484 485 486 487
  } else {
    sprintf(tempTable->table.tableAlias, "%p", tempTable);
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
488 489
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
490
  }
491
  return (SNode*)tempTable;
492 493
}

494
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
495
  CHECK_PARSER_STATUS(pCxt);
496 497 498
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
499 500
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
501
  joinTable->pOnCond = pJoinCond;
502
  return (SNode*)joinTable;
503
}
504

505
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
X
Xiaoyu Wang 已提交
506
  CHECK_PARSER_STATUS(pCxt);
507 508
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
wafwerar's avatar
wafwerar 已提交
509
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
510
  if (NULL != pOffset) {
wafwerar's avatar
wafwerar 已提交
511
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
512
  }
513
  return (SNode*)limitNode;
514 515
}

516
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
517
  CHECK_PARSER_STATUS(pCxt);
518 519
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
520
  orderByExpr->pExpr = pExpr;
521
  orderByExpr->order = order;
522 523 524
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
525
  orderByExpr->nullOrder = nullOrder;
526 527 528
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
529
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
530
  CHECK_PARSER_STATUS(pCxt);
531 532
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
533 534
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
535 536 537
  return (SNode*)session;
}

538
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
539
  CHECK_PARSER_STATUS(pCxt);
540 541
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
X
Xiaoyu Wang 已提交
542
  state->pCol = createPrimaryKeyCol(pCxt);
543
  if (NULL == state->pCol) {
544
    nodesDestroyNode((SNode*)state);
545 546
    CHECK_OUT_OF_MEM(state->pCol);
  }
547
  state->pExpr = pExpr;
548 549 550
  return (SNode*)state;
}

X
Xiaoyu Wang 已提交
551 552
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                                SNode* pFill) {
X
Xiaoyu Wang 已提交
553
  CHECK_PARSER_STATUS(pCxt);
554 555
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
X
Xiaoyu Wang 已提交
556
  interval->pCol = createPrimaryKeyCol(pCxt);
X
bugfix  
Xiaoyu Wang 已提交
557
  if (NULL == interval->pCol) {
558
    nodesDestroyNode((SNode*)interval);
X
bugfix  
Xiaoyu Wang 已提交
559 560
    CHECK_OUT_OF_MEM(interval->pCol);
  }
561 562 563 564 565 566 567 568
  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 已提交
569
  CHECK_PARSER_STATUS(pCxt);
570 571 572 573
  SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
  CHECK_OUT_OF_MEM(fill);
  fill->mode = mode;
  fill->pValues = pValues;
X
Xiaoyu Wang 已提交
574 575
  fill->pWStartTs = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == fill->pWStartTs) {
576
    nodesDestroyNode((SNode*)fill);
X
Xiaoyu Wang 已提交
577 578 579
    CHECK_OUT_OF_MEM(fill->pWStartTs);
  }
  strcpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstartts");
580
  return (SNode*)fill;
581
}
582

583
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
584
  CHECK_PARSER_STATUS(pCxt);
585 586 587 588 589 590 591 592
  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;
}

593
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) {
X
Xiaoyu Wang 已提交
594
  CHECK_PARSER_STATUS(pCxt);
D
dapan1121 已提交
595 596 597
  int32_t len = TMIN(sizeof(((SExprNode*)pNode)->aliasName) - 1, pAlias->n);
  strncpy(((SExprNode*)pNode)->aliasName, pAlias->z, len);
  ((SExprNode*)pNode)->aliasName[len] = '\0';
598 599
  strncpy(((SExprNode*)pNode)->userAlias, pAlias->z, len);
  ((SExprNode*)pNode)->userAlias[len] = '\0';
600
  return pNode;
601 602
}

603
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
X
Xiaoyu Wang 已提交
604
  CHECK_PARSER_STATUS(pCxt);
605
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
606
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
607 608 609
  }
  return pStmt;
}
610

611
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
X
Xiaoyu Wang 已提交
612
  CHECK_PARSER_STATUS(pCxt);
613
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
614
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
615 616
  }
  return pStmt;
617 618
}

619
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
X
Xiaoyu Wang 已提交
620
  CHECK_PARSER_STATUS(pCxt);
621
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
622
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
623 624 625
  }
  return pStmt;
}
626

627
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
X
Xiaoyu Wang 已提交
628
  CHECK_PARSER_STATUS(pCxt);
629
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
630
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
631 632
  }
  return pStmt;
633 634
}

635
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
X
Xiaoyu Wang 已提交
636
  CHECK_PARSER_STATUS(pCxt);
637
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
638
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
639 640 641 642 643
  }
  return pStmt;
}

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
X
Xiaoyu Wang 已提交
644
  CHECK_PARSER_STATUS(pCxt);
645
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
646
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
647 648 649
  }
  return pStmt;
}
650

651
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
X
Xiaoyu Wang 已提交
652
  CHECK_PARSER_STATUS(pCxt);
653
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
654
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
655 656
  }
  return pStmt;
657 658
}

659
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
X
Xiaoyu Wang 已提交
660
  CHECK_PARSER_STATUS(pCxt);
661
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
662
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
663
  }
664
  return pStmt;
665 666 667
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
668
  CHECK_PARSER_STATUS(pCxt);
669
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
670
  CHECK_OUT_OF_MEM(select);
671
  select->isDistinct = isDistinct;
672 673
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
674
  sprintf(select->stmtName, "%p", select);
X
Xiaoyu Wang 已提交
675
  select->isTimeOrderQuery = true;
676
  return (SNode*)select;
677 678 679
}

SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
680
  CHECK_PARSER_STATUS(pCxt);
681 682 683
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
684 685
  setOp->pLeft = pLeft;
  setOp->pRight = pRight;
686
  sprintf(setOp->stmtName, "%p", setOp);
687
  return (SNode*)setOp;
688 689
}

X
Xiaoyu Wang 已提交
690
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
691
  CHECK_PARSER_STATUS(pCxt);
692
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
693
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
  pOptions->cachelast = TSDB_DEFAULT_CACHE_LAST_ROW;
  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;
  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 已提交
712
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
X
Xiaoyu Wang 已提交
713
  return (SNode*)pOptions;
714 715
}

X
Xiaoyu Wang 已提交
716
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
717
  CHECK_PARSER_STATUS(pCxt);
718
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
  pOptions->cachelast = -1;
  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;
  pOptions->precision = -1;
  pOptions->replica = -1;
  pOptions->strict = -1;
  pOptions->walLevel = -1;
  pOptions->numOfVgroups = -1;
  pOptions->singleStable = -1;
X
Xiaoyu Wang 已提交
738
  pOptions->schemaless = -1;
X
Xiaoyu Wang 已提交
739 740 741 742
  return (SNode*)pOptions;
}

SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
743
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
744 745
  switch (type) {
    case DB_OPTION_BUFFER:
wafwerar's avatar
wafwerar 已提交
746
      ((SDatabaseOptions*)pOptions)->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
747 748
      break;
    case DB_OPTION_CACHELAST:
wafwerar's avatar
wafwerar 已提交
749
      ((SDatabaseOptions*)pOptions)->cachelast = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
750 751
      break;
    case DB_OPTION_COMP:
wafwerar's avatar
wafwerar 已提交
752
      ((SDatabaseOptions*)pOptions)->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
753
      break;
X
Xiaoyu Wang 已提交
754 755 756
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
wafwerar's avatar
wafwerar 已提交
757
        ((SDatabaseOptions*)pOptions)->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
758 759 760
      } else {
        ((SDatabaseOptions*)pOptions)->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
      }
X
Xiaoyu Wang 已提交
761
      break;
X
Xiaoyu Wang 已提交
762
    }
X
Xiaoyu Wang 已提交
763
    case DB_OPTION_FSYNC:
wafwerar's avatar
wafwerar 已提交
764
      ((SDatabaseOptions*)pOptions)->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
765 766
      break;
    case DB_OPTION_MAXROWS:
wafwerar's avatar
wafwerar 已提交
767
      ((SDatabaseOptions*)pOptions)->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
768 769
      break;
    case DB_OPTION_MINROWS:
wafwerar's avatar
wafwerar 已提交
770
      ((SDatabaseOptions*)pOptions)->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
771 772
      break;
    case DB_OPTION_KEEP:
X
Xiaoyu Wang 已提交
773 774 775
      ((SDatabaseOptions*)pOptions)->pKeep = pVal;
      break;
    case DB_OPTION_PAGES:
wafwerar's avatar
wafwerar 已提交
776
      ((SDatabaseOptions*)pOptions)->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
777 778
      break;
    case DB_OPTION_PAGESIZE:
wafwerar's avatar
wafwerar 已提交
779
      ((SDatabaseOptions*)pOptions)->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
780 781
      break;
    case DB_OPTION_PRECISION:
X
Xiaoyu Wang 已提交
782 783
      copyStringFormStringToken((SToken*)pVal, ((SDatabaseOptions*)pOptions)->precisionStr,
                                sizeof(((SDatabaseOptions*)pOptions)->precisionStr));
X
Xiaoyu Wang 已提交
784 785
      break;
    case DB_OPTION_REPLICA:
wafwerar's avatar
wafwerar 已提交
786
      ((SDatabaseOptions*)pOptions)->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
787
      break;
X
Xiaoyu Wang 已提交
788
    case DB_OPTION_STRICT:
wafwerar's avatar
wafwerar 已提交
789
      ((SDatabaseOptions*)pOptions)->strict = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
790 791
      break;
    case DB_OPTION_WAL:
wafwerar's avatar
wafwerar 已提交
792
      ((SDatabaseOptions*)pOptions)->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
793 794
      break;
    case DB_OPTION_VGROUPS:
wafwerar's avatar
wafwerar 已提交
795
      ((SDatabaseOptions*)pOptions)->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
796 797
      break;
    case DB_OPTION_SINGLE_STABLE:
wafwerar's avatar
wafwerar 已提交
798
      ((SDatabaseOptions*)pOptions)->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
799 800
      break;
    case DB_OPTION_RETENTIONS:
X
Xiaoyu Wang 已提交
801
      ((SDatabaseOptions*)pOptions)->pRetentions = pVal;
X
Xiaoyu Wang 已提交
802
      break;
X
Xiaoyu Wang 已提交
803
    case DB_OPTION_SCHEMALESS:
804 805
//      ((SDatabaseOptions*)pOptions)->schemaless = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
      ((SDatabaseOptions*)pOptions)->schemaless = 1;
X
Xiaoyu Wang 已提交
806
      break;
X
Xiaoyu Wang 已提交
807 808
    default:
      break;
X
Xiaoyu Wang 已提交
809
  }
X
Xiaoyu Wang 已提交
810
  return pOptions;
X
Xiaoyu Wang 已提交
811 812
}

X
Xiaoyu Wang 已提交
813
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
814
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
815 816 817 818 819 820 821 822 823 824
  switch (pAlterOption->type) {
    case DB_OPTION_KEEP:
    case DB_OPTION_RETENTIONS:
      return setDatabaseOption(pCxt, pOptions, pAlterOption->type, pAlterOption->pList);
    default:
      break;
  }
  return setDatabaseOption(pCxt, pOptions, pAlterOption->type, &pAlterOption->val);
}

X
Xiaoyu Wang 已提交
825
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
826
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
827
  if (!checkDbName(pCxt, pDbName, false)) {
828 829 830 831 832 833
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
834
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
835
  return (SNode*)pStmt;
836
}
X
Xiaoyu Wang 已提交
837

X
Xiaoyu Wang 已提交
838
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
839
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
840
  if (!checkDbName(pCxt, pDbName, false)) {
841 842 843 844 845 846 847 848 849
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
850
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
851
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
852
  if (!checkDbName(pCxt, pDbName, false)) {
853 854
    return NULL;
  }
855
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
856 857 858 859 860 861
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
862
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
863
  CHECK_PARSER_STATUS(pCxt);
864
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
865
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
866 867
  pOptions->filesFactor = TSDB_DEFAULT_ROLLUP_FILE_FACTOR;
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
868
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
869 870
}

X
Xiaoyu Wang 已提交
871
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
872
  CHECK_PARSER_STATUS(pCxt);
873
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
874 875 876 877 878 879 880
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->filesFactor = -1;
  pOptions->ttl = -1;
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
881
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
882
  switch (type) {
X
Xiaoyu Wang 已提交
883
    case TABLE_OPTION_COMMENT:
884 885 886 887
      if (checkComment(pCxt, (SToken*)pVal, true)) {
        copyStringFormStringToken((SToken*)pVal, ((STableOptions*)pOptions)->comment,
                                  sizeof(((STableOptions*)pOptions)->comment));
      }
X
Xiaoyu Wang 已提交
888 889
      break;
    case TABLE_OPTION_FILE_FACTOR:
5
54liuyao 已提交
890
      ((STableOptions*)pOptions)->filesFactor = taosStr2Double(((SToken*)pVal)->z, NULL);
X
Xiaoyu Wang 已提交
891
      break;
X
Xiaoyu Wang 已提交
892 893 894 895
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
    case TABLE_OPTION_TTL:
wafwerar's avatar
wafwerar 已提交
896
      ((STableOptions*)pOptions)->ttl = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
897 898 899
      break;
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
900 901 902
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
903
  }
X
Xiaoyu Wang 已提交
904
  return pOptions;
X
Xiaoyu Wang 已提交
905 906
}

X
Xiaoyu Wang 已提交
907
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
908
  CHECK_PARSER_STATUS(pCxt);
909
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
910 911
    return NULL;
  }
X
Xiaoyu Wang 已提交
912 913 914 915 916
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
  strncpy(pCol->colName, pColName->z, pColName->n);
  pCol->dataType = dataType;
  if (NULL != pComment) {
917
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
918
  }
919
  pCol->sma = true;
X
Xiaoyu Wang 已提交
920 921 922
  return (SNode*)pCol;
}

923
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
924
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
925 926 927 928
  return dt;
}

SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
wafwerar's avatar
wafwerar 已提交
929
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = taosStr2Int16(pLen->z, NULL, 10)};
930 931 932
  return dt;
}

X
Xiaoyu Wang 已提交
933 934
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
935
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
936 937
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
938 939
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
940 941
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
942
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
943
  pStmt->pOptions = (STableOptions*)pOptions;
944
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
945 946
  return (SNode*)pStmt;
}
947

X
Xiaoyu Wang 已提交
948
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
949
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
950
  CHECK_PARSER_STATUS(pCxt);
951
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
952
  CHECK_OUT_OF_MEM(pStmt);
953 954 955 956
  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);
957 958 959
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
960 961
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
962
  nodesDestroyNode(pOptions);
963 964 965 966
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
967
  CHECK_PARSER_STATUS(pCxt);
968
  SCreateMultiTableStmt* pStmt = (SCreateMultiTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLE_STMT);
969 970 971 972 973
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

974
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
975
  CHECK_PARSER_STATUS(pCxt);
976
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
977 978 979 980 981 982 983 984 985
  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 已提交
986
  CHECK_PARSER_STATUS(pCxt);
987
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
988 989 990 991 992
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

993
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
994
  CHECK_PARSER_STATUS(pCxt);
995
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
996 997 998 999 1000 1001 1002 1003
  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 已提交
1004 1005 1006 1007 1008 1009 1010
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 已提交
1011
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1012
  CHECK_PARSER_STATUS(pCxt);
1013
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1014 1015 1016
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1017
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1018 1019
}

X
Xiaoyu Wang 已提交
1020 1021
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
                                    SDataType dataType) {
X
Xiaoyu Wang 已提交
1022 1023
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1024 1025
    return NULL;
  }
1026
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1027 1028 1029 1030
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
  strncpy(pStmt->colName, pColName->z, pColName->n);
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
1031
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1032 1033
}

X
Xiaoyu Wang 已提交
1034
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1035 1036
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1037 1038
    return NULL;
  }
1039
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1040 1041 1042
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
  strncpy(pStmt->colName, pColName->z, pColName->n);
X
Xiaoyu Wang 已提交
1043
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1044 1045
}

X
Xiaoyu Wang 已提交
1046 1047
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1048 1049
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1050 1051
    return NULL;
  }
1052
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1053 1054 1055 1056
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
  strncpy(pStmt->colName, pOldColName->z, pOldColName->n);
  strncpy(pStmt->newColName, pNewColName->z, pNewColName->n);
X
Xiaoyu Wang 已提交
1057
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1058 1059
}

X
Xiaoyu Wang 已提交
1060
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1061 1062
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
X
bugfix  
Xiaoyu Wang 已提交
1063 1064
    return NULL;
  }
1065
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1066 1067 1068 1069
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
  strncpy(pStmt->colName, pTagName->z, pTagName->n);
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1070
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1071 1072
}

1073
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1074
  CHECK_PARSER_STATUS(pCxt);
1075 1076 1077
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1078 1079 1080 1081 1082 1083
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1084
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1085 1086
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
         QUERY_NODE_SHOW_VGROUPS_STMT == type;
X
Xiaoyu Wang 已提交
1087 1088
}

X
Xiaoyu Wang 已提交
1089
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbNamePattern) {
X
Xiaoyu Wang 已提交
1090
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1091 1092
  if (needDbShowStmt(type) && NULL == pDbName && NULL == pCxt->pQueryCxt->db) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "db not specified");
1093
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1094 1095
    return NULL;
  }
1096
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1097
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1098 1099
  pStmt->pDbName = pDbName;
  pStmt->pTbNamePattern = pTbNamePattern;
1100
  return (SNode*)pStmt;
1101
}
1102

1103
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
1104
  CHECK_PARSER_STATUS(pCxt);
1105 1106 1107 1108 1109 1110
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1111
  CHECK_PARSER_STATUS(pCxt);
1112 1113 1114 1115 1116
  SNode* pStmt = nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1117
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword) {
X
Xiaoyu Wang 已提交
1118
  CHECK_PARSER_STATUS(pCxt);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  char password[TSDB_USET_PASSWORD_LEN] = {0};
  if (!checkUserName(pCxt, pUserName) || !checkPassword(pCxt, pPassword, password)) {
    return NULL;
  }
  SCreateUserStmt* pStmt = (SCreateUserStmt*)nodesMakeNode(QUERY_NODE_CREATE_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->useName, pUserName->z, pUserName->n);
  strcpy(pStmt->password, password);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1130
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1131
  CHECK_PARSER_STATUS(pCxt);
1132 1133 1134 1135 1136 1137 1138 1139 1140
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->useName, pUserName->z, pUserName->n);
  if (TSDB_ALTER_USER_PASSWD == alterType) {
    char password[TSDB_USET_PASSWORD_LEN] = {0};
    if (!checkPassword(pCxt, pVal, password)) {
1141
      nodesDestroyNode((SNode*)pStmt);
1142 1143 1144 1145 1146 1147 1148 1149
      return NULL;
    }
    strcpy(pStmt->password, password);
  }
  pStmt->alterType = alterType;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1150
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1151
  CHECK_PARSER_STATUS(pCxt);
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->useName, pUserName->z, pUserName->n);
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1162
  CHECK_PARSER_STATUS(pCxt);
1163
  int32_t port = 0;
X
Xiaoyu Wang 已提交
1164
  char    fqdn[TSDB_FQDN_LEN] = {0};
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
  if (NULL == pPort) {
    if (!checkAndSplitEndpoint(pCxt, pFqdn, fqdn, &port)) {
      return NULL;
    }
  } else if (!checkFqdn(pCxt, pFqdn) || !checkPort(pCxt, pPort, &port)) {
    return NULL;
  }
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (NULL == pPort) {
    strcpy(pStmt->fqdn, fqdn);
  } else {
    strncpy(pStmt->fqdn, pFqdn->z, pFqdn->n);
  }
  pStmt->port = port;
  return (SNode*)pStmt;
}

SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode) {
X
Xiaoyu Wang 已提交
1184
  CHECK_PARSER_STATUS(pCxt);
1185 1186 1187
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1188
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1189 1190
  } else {
    if (!checkAndSplitEndpoint(pCxt, pDnode, pStmt->fqdn, &pStmt->port)) {
1191
      nodesDestroyNode((SNode*)pStmt);
1192 1193 1194 1195 1196
      return NULL;
    }
  }
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1197

X
Xiaoyu Wang 已提交
1198 1199
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1200
  CHECK_PARSER_STATUS(pCxt);
1201
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1202
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1203
  pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1204 1205 1206 1207 1208 1209 1210
  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;
}

X
Xiaoyu Wang 已提交
1211 1212
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName,
                             SToken* pTableName, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1213
  CHECK_PARSER_STATUS(pCxt);
1214
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName) || !checkDbName(pCxt, NULL, true)) {
X
Xiaoyu Wang 已提交
1215 1216
    return NULL;
  }
1217
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1218 1219
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1220
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1221 1222 1223 1224 1225 1226 1227
  strncpy(pStmt->indexName, pIndexName->z, pIndexName->n);
  strncpy(pStmt->tableName, pTableName->z, pTableName->n);
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1228 1229
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset,
                         SNode* pSliding) {
X
Xiaoyu Wang 已提交
1230
  CHECK_PARSER_STATUS(pCxt);
1231
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1232 1233 1234 1235 1236 1237 1238
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1239

X
Xiaoyu Wang 已提交
1240
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName, SToken* pTableName) {
X
Xiaoyu Wang 已提交
1241
  CHECK_PARSER_STATUS(pCxt);
1242 1243 1244
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName)) {
    return NULL;
  }
1245
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1246
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1247
  pStmt->ignoreNotExists = ignoreNotExists;
1248 1249 1250 1251 1252
  strncpy(pStmt->indexName, pIndexName->z, pIndexName->n);
  strncpy(pStmt->tableName, pTableName->z, pTableName->n);
  return (SNode*)pStmt;
}

1253
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1254
  CHECK_PARSER_STATUS(pCxt);
1255
  SCreateComponentNodeStmt* pStmt = (SCreateComponentNodeStmt*)nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1256
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1257
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
X
Xiaoyu Wang 已提交
1258 1259
  return (SNode*)pStmt;
}
1260

1261
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1262
  CHECK_PARSER_STATUS(pCxt);
1263
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1264
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1265
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1266 1267 1268
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1269
SNode* createCreateTopicStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName, SNode* pQuery,
1270
                             const SToken* pSubDbName, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1271
  CHECK_PARSER_STATUS(pCxt);
1272
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1273 1274 1275
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->topicName, pTopicName->z, pTopicName->n);
  pStmt->ignoreExists = ignoreExists;
1276 1277 1278 1279 1280 1281 1282 1283
  if (NULL != pRealTable) {
    strcpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName);
    strcpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName);
    nodesDestroyNode(pRealTable);
  } else if (NULL != pSubDbName) {
    strncpy(pStmt->subDbName, pSubDbName->z, pSubDbName->n);
  } else {
    pStmt->pQuery = pQuery;
1284 1285 1286 1287 1288
  }
  return (SNode*)pStmt;
}

SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1289
  CHECK_PARSER_STATUS(pCxt);
1290
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1291 1292 1293 1294 1295
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->topicName, pTopicName->z, pTopicName->n);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1296

X
Xiaoyu Wang 已提交
1297 1298 1299
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pCGroupId,
                            const SToken* pTopicName) {
  CHECK_PARSER_STATUS(pCxt);
1300
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1301 1302 1303 1304 1305 1306 1307
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
  strncpy(pStmt->topicName, pTopicName->z, pTopicName->n);
  strncpy(pStmt->cgroup, pCGroupId->z, pCGroupId->n);
  return (SNode*)pStmt;
}

1308
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1309
  CHECK_PARSER_STATUS(pCxt);
1310
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1311 1312 1313 1314 1315 1316 1317
  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;
}
1318 1319

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1320
  CHECK_PARSER_STATUS(pCxt);
1321
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1322 1323 1324 1325 1326 1327 1328
  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 已提交
1329
  CHECK_PARSER_STATUS(pCxt);
1330 1331 1332 1333 1334
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1335
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1336
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1337 1338 1339 1340
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1341
  CHECK_PARSER_STATUS(pCxt);
1342
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1343 1344 1345 1346 1347 1348
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1349 1350

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1351
  CHECK_PARSER_STATUS(pCxt);
1352
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1353 1354 1355 1356 1357 1358 1359 1360
  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 已提交
1361
  CHECK_PARSER_STATUS(pCxt);
1362 1363 1364 1365
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1366 1367

SNode* createCompactStmt(SAstCreateContext* pCxt, SNodeList* pVgroups) {
X
Xiaoyu Wang 已提交
1368
  CHECK_PARSER_STATUS(pCxt);
1369 1370 1371 1372 1373
  SNode* pStmt = nodesMakeNode(QUERY_NODE_COMPACT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1374 1375
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
X
Xiaoyu Wang 已提交
1376
  CHECK_PARSER_STATUS(pCxt);
1377
  if (pLibPath->n <= 2) {
1378
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1379 1380
    return NULL;
  }
1381
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1382
  CHECK_OUT_OF_MEM(pStmt);
1383 1384 1385 1386 1387 1388 1389
  pStmt->ignoreExists = ignoreExists;
  strncpy(pStmt->funcName, pFuncName->z, pFuncName->n);
  pStmt->isAgg = aggFunc;
  strncpy(pStmt->libraryPath, pLibPath->z + 1, pLibPath->n - 2);
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
  return (SNode*)pStmt;
1390 1391
}

1392
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1393
  CHECK_PARSER_STATUS(pCxt);
1394
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1395
  CHECK_OUT_OF_MEM(pStmt);
1396 1397 1398
  pStmt->ignoreNotExists = ignoreNotExists;
  strncpy(pStmt->funcName, pFuncName->z, pFuncName->n);
  return (SNode*)pStmt;
1399 1400
}

1401
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1402
  CHECK_PARSER_STATUS(pCxt);
1403
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1404 1405 1406 1407 1408
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->triggerType = STREAM_TRIGGER_AT_ONCE;
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1409 1410
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pStreamName, SNode* pRealTable,
                              SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1411
  CHECK_PARSER_STATUS(pCxt);
1412
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1413
  CHECK_OUT_OF_MEM(pStmt);
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
  strncpy(pStmt->streamName, pStreamName->z, pStreamName->n);
  if (NULL != pRealTable) {
    strcpy(pStmt->targetDbName, ((SRealTableNode*)pRealTable)->table.dbName);
    strcpy(pStmt->targetTabName, ((SRealTableNode*)pRealTable)->table.tableName);
    nodesDestroyNode(pRealTable);
  }
  pStmt->ignoreExists = ignoreExists;
  pStmt->pOptions = (SStreamOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
1424 1425
}

1426
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1427
  CHECK_PARSER_STATUS(pCxt);
1428
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1429
  CHECK_OUT_OF_MEM(pStmt);
1430 1431 1432
  strncpy(pStmt->streamName, pStreamName->z, pStreamName->n);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1433 1434 1435
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
1436
  CHECK_PARSER_STATUS(pCxt);
1437
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
1438
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1439
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
1440
  return (SNode*)pStmt;
1441 1442
}

X
Xiaoyu Wang 已提交
1443 1444
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
1445
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
1446 1447 1448 1449
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

1450
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
1451
  CHECK_PARSER_STATUS(pCxt);
1452
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
1453
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1454 1455 1456
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
1457 1458 1459
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
1460
  CHECK_PARSER_STATUS(pCxt);
1461
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
1462
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1463 1464 1465
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
1466 1467 1468
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
1469
  CHECK_PARSER_STATUS(pCxt);
1470
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
1471
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1472 1473
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
1474 1475 1476
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
1477
  CHECK_PARSER_STATUS(pCxt);
1478 1479 1480 1481
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1482 1483

SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1484
  CHECK_PARSER_STATUS(pCxt);
1485 1486 1487
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1488
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
1489 1490 1491 1492 1493 1494 1495 1496
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  strncpy(pStmt->userName, pUserName->z, pUserName->n);
  return (SNode*)pStmt;
}

SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1497
  CHECK_PARSER_STATUS(pCxt);
1498 1499 1500
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1501
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
1502 1503 1504 1505 1506 1507
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  strncpy(pStmt->userName, pUserName->z, pUserName->n);
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1508

X
Xiaoyu Wang 已提交
1509
SNode* createCountFuncForDelete(SAstCreateContext* pCxt) {
1510
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
1511 1512 1513
  CHECK_OUT_OF_MEM(pFunc);
  strcpy(pFunc->functionName, "count");
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt))) {
1514
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
1515 1516 1517 1518 1519
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
1520 1521
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
1522
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
1523 1524 1525
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
X
Xiaoyu Wang 已提交
1526 1527
  pStmt->pCountFunc = createCountFuncForDelete(pCxt);
  if (NULL == pStmt->pCountFunc) {
1528
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
1529 1530
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
1531 1532
  return (SNode*)pStmt;
}