parAstCreater.c 57.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

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

213
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
X
Xiaoyu Wang 已提交
214
  CHECK_PARSER_STATUS(pCxt);
215 216 217 218 219 220 221 222 223
  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 已提交
224
  CHECK_PARSER_STATUS(pCxt);
225 226 227 228 229 230 231 232 233
  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 已提交
234
  CHECK_PARSER_STATUS(pCxt);
235
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
236
  SNode*        pExpr = pRawExpr->pNode;
D
dapan1121 已提交
237
  if (nodesIsExprNode(pExpr)) {
X
Xiaoyu Wang 已提交
238 239 240 241 242 243 244
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
      strcpy(((SExprNode*)pExpr)->aliasName, ((SColumnNode*)pExpr)->colName);
    } else {
      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 已提交
245
  }
wafwerar's avatar
wafwerar 已提交
246
  taosMemoryFreeClear(pNode);
247
  return pExpr;
248 249 250
}

SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
251
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
252
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
253 254
    return nil_token;
  }
255
  SRawExprNode* target = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
256
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
257 258 259
  return t;
}

260
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
261
  CHECK_PARSER_STATUS(pCxt);
262 263
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
264
  pCxt->errCode = nodesListAppend(list, pNode);
X
Xiaoyu Wang 已提交
265
  return list;
266 267 268
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
269
  CHECK_PARSER_STATUS(pCxt);
270
  pCxt->errCode = nodesListAppend(pList, pNode);
X
Xiaoyu Wang 已提交
271
  return pList;
272 273
}

X
Xiaoyu Wang 已提交
274
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
X
Xiaoyu Wang 已提交
275
  CHECK_PARSER_STATUS(pCxt);
276
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
277 278 279 280
    return NULL;
  }
  SColumnNode* col = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(col);
281
  if (NULL != pTableAlias) {
282
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
283
  }
284
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
285
  return (SNode*)col;
286
}
287

288
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
289
  CHECK_PARSER_STATUS(pCxt);
290 291
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
292 293 294 295
  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 已提交
296
  }
297
  CHECK_OUT_OF_MEM(val->literal);
298
  val->node.resType.type = dataType;
X
Xiaoyu Wang 已提交
299
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
300 301 302
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  }
303 304
  val->isDuration = false;
  val->translate = false;
305 306 307 308
  return (SNode*)val;
}

SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
309
  CHECK_PARSER_STATUS(pCxt);
310 311
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
312 313 314
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
315
  val->translate = false;
316 317 318
  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;
319
  return (SNode*)val;
320 321
}

X
Xiaoyu Wang 已提交
322
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
323
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
324 325 326 327
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
328 329 330 331 332 333 334 335 336 337 338 339
  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;
}

340
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
341
  CHECK_PARSER_STATUS(pCxt);
342 343
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
344 345
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
D
dapan1121 已提交
346
  val->placeholderNo = ++pCxt->placeholderNo;
X
Xiaoyu Wang 已提交
347 348 349
  if (NULL == pCxt->pPlaceholderValues) {
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    if (NULL == pCxt->pPlaceholderValues) {
350
      nodesDestroyNode((SNode*)val);
X
Xiaoyu Wang 已提交
351 352 353 354
      return NULL;
    }
  }
  taosArrayPush(pCxt->pPlaceholderValues, &val);
355 356 357
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
358 359 360 361 362 363 364 365 366 367 368
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);
  }
}

369
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
X
Xiaoyu Wang 已提交
370
  CHECK_PARSER_STATUS(pCxt);
371 372 373 374
  SLogicConditionNode* cond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  CHECK_OUT_OF_MEM(cond);
  cond->condType = type;
  cond->pParameterList = nodesMakeList();
X
Xiaoyu Wang 已提交
375 376 377
  int32_t code = addParamToLogicConditionNode(cond, pParam1);
  if (TSDB_CODE_SUCCESS == code && NULL != pParam2) {
    code = addParamToLogicConditionNode(cond, pParam2);
X
Xiaoyu Wang 已提交
378
  }
X
Xiaoyu Wang 已提交
379
  if (TSDB_CODE_SUCCESS != code) {
380
    nodesDestroyNode((SNode*)cond);
X
Xiaoyu Wang 已提交
381
    return NULL;
382
  }
383 384 385 386
  return (SNode*)cond;
}

SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
387
  CHECK_PARSER_STATUS(pCxt);
388 389 390 391 392 393 394 395 396
  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 已提交
397
  CHECK_PARSER_STATUS(pCxt);
398
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
399 400
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
401 402 403
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
404
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
405 406
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
407 408
}

X
Xiaoyu Wang 已提交
409
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
410
  CHECK_PARSER_STATUS(pCxt);
411
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
412 413 414 415 416 417
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(pCol->colName, PK_TS_COL_INTERNAL_NAME);
  return (SNode*)pCol;
}

418
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
419
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
420 421 422
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
    return createPrimaryKeyCol(pCxt);
  }
423 424
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
425
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
426 427 428 429
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

430
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
X
Xiaoyu Wang 已提交
431
  CHECK_PARSER_STATUS(pCxt);
432 433 434 435
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
436 437 438 439
  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;
440
  }
441 442 443 444
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

445
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
446
  CHECK_PARSER_STATUS(pCxt);
447 448 449 450
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
451 452
}

X
Xiaoyu Wang 已提交
453
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
X
Xiaoyu Wang 已提交
454
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
455 456 457 458 459 460 461 462 463
  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 已提交
464
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
465
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
466
  if (!checkDbName(pCxt, pDbName, true) || !checkTableName(pCxt, pTableName) || !checkTableName(pCxt, pTableAlias)) {
467 468 469 470 471
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
472
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
473 474 475
  } else {
    strcpy(realTable->table.dbName, pCxt->pQueryCxt->db);
  }
476
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
477
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
478
  } else {
479
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
480
  }
481
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
482
  return (SNode*)realTable;
483
}
484

485
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
486
  CHECK_PARSER_STATUS(pCxt);
487 488 489
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
490
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
491
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
X
Xiaoyu Wang 已提交
492 493 494 495 496
  } else {
    sprintf(tempTable->table.tableAlias, "%p", tempTable);
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
497 498
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
499
  }
500
  return (SNode*)tempTable;
501 502
}

503
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
504
  CHECK_PARSER_STATUS(pCxt);
505 506 507
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
508 509
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
510
  joinTable->pOnCond = pJoinCond;
511
  return (SNode*)joinTable;
512
}
513

514
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
X
Xiaoyu Wang 已提交
515
  CHECK_PARSER_STATUS(pCxt);
516 517
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
wafwerar's avatar
wafwerar 已提交
518
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
519
  if (NULL != pOffset) {
wafwerar's avatar
wafwerar 已提交
520
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
521
  }
522
  return (SNode*)limitNode;
523 524
}

525
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
526
  CHECK_PARSER_STATUS(pCxt);
527 528
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
529
  orderByExpr->pExpr = pExpr;
530
  orderByExpr->order = order;
531 532 533
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
534
  orderByExpr->nullOrder = nullOrder;
535 536 537
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
538
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
539
  CHECK_PARSER_STATUS(pCxt);
540 541
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
542 543
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
544 545 546
  return (SNode*)session;
}

547
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
548
  CHECK_PARSER_STATUS(pCxt);
549 550
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
X
Xiaoyu Wang 已提交
551
  state->pCol = createPrimaryKeyCol(pCxt);
552
  if (NULL == state->pCol) {
553
    nodesDestroyNode((SNode*)state);
554 555
    CHECK_OUT_OF_MEM(state->pCol);
  }
556
  state->pExpr = pExpr;
557 558 559
  return (SNode*)state;
}

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

592
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
593
  CHECK_PARSER_STATUS(pCxt);
594 595 596 597 598 599 600 601
  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;
}

602
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) {
X
Xiaoyu Wang 已提交
603
  CHECK_PARSER_STATUS(pCxt);
D
dapan1121 已提交
604 605 606
  int32_t len = TMIN(sizeof(((SExprNode*)pNode)->aliasName) - 1, pAlias->n);
  strncpy(((SExprNode*)pNode)->aliasName, pAlias->z, len);
  ((SExprNode*)pNode)->aliasName[len] = '\0';
607 608
  strncpy(((SExprNode*)pNode)->userAlias, pAlias->z, len);
  ((SExprNode*)pNode)->userAlias[len] = '\0';
609
  return pNode;
610 611
}

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

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

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

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

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

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
X
Xiaoyu Wang 已提交
653
  CHECK_PARSER_STATUS(pCxt);
654
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
655
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
656 657
  } else {
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
658 659 660
  }
  return pStmt;
}
661

662
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
X
Xiaoyu Wang 已提交
663
  CHECK_PARSER_STATUS(pCxt);
664
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
665
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
666 667
  }
  return pStmt;
668 669
}

670
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
X
Xiaoyu Wang 已提交
671
  CHECK_PARSER_STATUS(pCxt);
672
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
673
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
674
  }
675
  return pStmt;
676 677 678
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
679
  CHECK_PARSER_STATUS(pCxt);
680
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
681
  CHECK_OUT_OF_MEM(select);
682
  select->isDistinct = isDistinct;
683 684
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
685
  sprintf(select->stmtName, "%p", select);
X
Xiaoyu Wang 已提交
686
  select->isTimeOrderQuery = true;
687
  return (SNode*)select;
688 689 690
}

SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
691
  CHECK_PARSER_STATUS(pCxt);
692 693 694
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
695 696
  setOp->pLeft = pLeft;
  setOp->pRight = pRight;
697
  sprintf(setOp->stmtName, "%p", setOp);
698
  return (SNode*)setOp;
699 700
}

X
Xiaoyu Wang 已提交
701
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
702
  CHECK_PARSER_STATUS(pCxt);
703
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
704
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  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 已提交
723
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
X
Xiaoyu Wang 已提交
724
  return (SNode*)pOptions;
725 726
}

X
Xiaoyu Wang 已提交
727
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
728
  CHECK_PARSER_STATUS(pCxt);
729
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
  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 已提交
749
  pOptions->schemaless = -1;
X
Xiaoyu Wang 已提交
750 751 752 753
  return (SNode*)pOptions;
}

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

X
Xiaoyu Wang 已提交
823
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
824
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
825 826 827 828 829 830 831 832 833 834
  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 已提交
835
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
836
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
837
  if (!checkDbName(pCxt, pDbName, false)) {
838 839 840 841
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
842
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
843
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
844
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
845
  return (SNode*)pStmt;
846
}
X
Xiaoyu Wang 已提交
847

X
Xiaoyu Wang 已提交
848
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
849
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
850
  if (!checkDbName(pCxt, pDbName, false)) {
851 852 853 854
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
855
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
856 857 858 859
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
860
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
861
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
862
  if (!checkDbName(pCxt, pDbName, false)) {
863 864
    return NULL;
  }
865
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
866
  CHECK_OUT_OF_MEM(pStmt);
867
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
868 869 870 871
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
872
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
873
  CHECK_PARSER_STATUS(pCxt);
874
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
875
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
876 877 878 879
  pOptions->maxDelay1 = -1;
  pOptions->maxDelay2 = -1;
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
X
Xiaoyu Wang 已提交
880
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
881
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
882 883
}

X
Xiaoyu Wang 已提交
884
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
885
  CHECK_PARSER_STATUS(pCxt);
886
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
887 888 889 890 891 892
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->ttl = -1;
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
893
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
894
  switch (type) {
X
Xiaoyu Wang 已提交
895
    case TABLE_OPTION_COMMENT:
896
      if (checkComment(pCxt, (SToken*)pVal, true)) {
897
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
898
      }
X
Xiaoyu Wang 已提交
899
      break;
X
Xiaoyu Wang 已提交
900 901 902 903 904
    case TABLE_OPTION_MAXDELAY:
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
      break;
    case TABLE_OPTION_WATERMARK:
      ((STableOptions*)pOptions)->pWatermark = pVal;
X
Xiaoyu Wang 已提交
905
      break;
X
Xiaoyu Wang 已提交
906 907 908 909
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
    case TABLE_OPTION_TTL:
wafwerar's avatar
wafwerar 已提交
910
      ((STableOptions*)pOptions)->ttl = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
911 912 913
      break;
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
914 915 916
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
917
  }
X
Xiaoyu Wang 已提交
918
  return pOptions;
X
Xiaoyu Wang 已提交
919 920
}

X
Xiaoyu Wang 已提交
921
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
922
  CHECK_PARSER_STATUS(pCxt);
923
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
924 925
    return NULL;
  }
X
Xiaoyu Wang 已提交
926 927
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
928
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
X
Xiaoyu Wang 已提交
929 930
  pCol->dataType = dataType;
  if (NULL != pComment) {
931
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
932
  }
933
  pCol->sma = true;
X
Xiaoyu Wang 已提交
934 935 936
  return (SNode*)pCol;
}

937
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
938
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
939 940 941 942
  return dt;
}

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

X
Xiaoyu Wang 已提交
947 948
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
949
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
950 951
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
952 953
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
954 955
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
956
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
957
  pStmt->pOptions = (STableOptions*)pOptions;
958
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
959 960
  return (SNode*)pStmt;
}
961

X
Xiaoyu Wang 已提交
962
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
963
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
964
  CHECK_PARSER_STATUS(pCxt);
965
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
966
  CHECK_OUT_OF_MEM(pStmt);
967 968 969 970
  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);
971 972 973
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
974 975
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
976
  nodesDestroyNode(pOptions);
977 978 979 980
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
981
  CHECK_PARSER_STATUS(pCxt);
982
  SCreateMultiTableStmt* pStmt = (SCreateMultiTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLE_STMT);
983 984 985 986 987
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

988
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
989
  CHECK_PARSER_STATUS(pCxt);
990
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
991 992 993 994 995 996 997 998 999
  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 已提交
1000
  CHECK_PARSER_STATUS(pCxt);
1001
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
1002 1003 1004 1005 1006
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

1007
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1008
  CHECK_PARSER_STATUS(pCxt);
1009
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
1010 1011 1012 1013 1014 1015 1016 1017
  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 已提交
1018 1019 1020 1021 1022 1023 1024
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 已提交
1025
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1026
  CHECK_PARSER_STATUS(pCxt);
1027
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1028 1029 1030
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1031
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1032 1033
}

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

X
Xiaoyu Wang 已提交
1048
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1049 1050
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1051 1052
    return NULL;
  }
1053
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1054 1055
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1056
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
X
Xiaoyu Wang 已提交
1057
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1058 1059
}

X
Xiaoyu Wang 已提交
1060 1061
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1062 1063
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1064 1065
    return NULL;
  }
1066
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1067 1068
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1069 1070
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
X
Xiaoyu Wang 已提交
1071
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1072 1073
}

X
Xiaoyu Wang 已提交
1074
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1075 1076
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
X
bugfix  
Xiaoyu Wang 已提交
1077 1078
    return NULL;
  }
1079
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1080 1081
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
1082
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
1083
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1084
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1085 1086
}

1087
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1088
  CHECK_PARSER_STATUS(pCxt);
1089 1090 1091
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1092 1093
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1094
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1095 1096 1097
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1098
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1099 1100
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
         QUERY_NODE_SHOW_VGROUPS_STMT == type;
X
Xiaoyu Wang 已提交
1101 1102
}

X
Xiaoyu Wang 已提交
1103
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbNamePattern) {
X
Xiaoyu Wang 已提交
1104
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1105 1106
  if (needDbShowStmt(type) && NULL == pDbName && NULL == pCxt->pQueryCxt->db) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "db not specified");
1107
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1108 1109
    return NULL;
  }
1110
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1111
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1112 1113
  pStmt->pDbName = pDbName;
  pStmt->pTbNamePattern = pTbNamePattern;
1114
  return (SNode*)pStmt;
1115
}
1116

1117
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1118
  CHECK_PARSER_STATUS(pCxt);
1119 1120 1121 1122
  if (!checkDbName(pCxt, pDbName, true)) {
    return NULL;
  }
  SShowCreateDatabaseStmt* pStmt = (SShowCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
1123
  CHECK_OUT_OF_MEM(pStmt);
1124 1125
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
1126 1127 1128
}

SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1129
  CHECK_PARSER_STATUS(pCxt);
1130
  SShowCreateTableStmt* pStmt = (SShowCreateTableStmt*)nodesMakeNode(type);
1131
  CHECK_OUT_OF_MEM(pStmt);
1132 1133 1134
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
  return (SNode*)pStmt;
1135 1136
}

X
Xiaoyu Wang 已提交
1137
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword) {
X
Xiaoyu Wang 已提交
1138
  CHECK_PARSER_STATUS(pCxt);
1139 1140 1141 1142 1143 1144
  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);
1145
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1146 1147 1148 1149
  strcpy(pStmt->password, password);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1150
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1151
  CHECK_PARSER_STATUS(pCxt);
1152 1153 1154 1155 1156
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1157
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1158 1159 1160
  if (TSDB_ALTER_USER_PASSWD == alterType) {
    char password[TSDB_USET_PASSWORD_LEN] = {0};
    if (!checkPassword(pCxt, pVal, password)) {
1161
      nodesDestroyNode((SNode*)pStmt);
1162 1163 1164 1165 1166 1167 1168 1169
      return NULL;
    }
    strcpy(pStmt->password, password);
  }
  pStmt->alterType = alterType;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1170
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1171
  CHECK_PARSER_STATUS(pCxt);
1172 1173 1174 1175 1176
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1177
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1178 1179 1180 1181
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1182
  CHECK_PARSER_STATUS(pCxt);
1183 1184
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1185 1186 1187
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
    nodesDestroyNode((SNode*)pStmt);
    return NULL;
1188 1189 1190 1191 1192
  }
  return (SNode*)pStmt;
}

SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode) {
X
Xiaoyu Wang 已提交
1193
  CHECK_PARSER_STATUS(pCxt);
1194 1195 1196
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1197
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1198
  } else {
1199
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
1200
      nodesDestroyNode((SNode*)pStmt);
1201 1202 1203 1204 1205
      return NULL;
    }
  }
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1206

X
Xiaoyu Wang 已提交
1207 1208
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1209
  CHECK_PARSER_STATUS(pCxt);
1210
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1211
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1212
  pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1213 1214 1215 1216 1217 1218 1219
  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 已提交
1220 1221
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName,
                             SToken* pTableName, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1222
  CHECK_PARSER_STATUS(pCxt);
1223
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName) || !checkDbName(pCxt, NULL, true)) {
X
Xiaoyu Wang 已提交
1224 1225
    return NULL;
  }
1226
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1227 1228
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1229
  pStmt->ignoreExists = ignoreExists;
1230 1231
  COPY_STRING_FORM_ID_TOKEN(pStmt->indexName, pIndexName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->tableName, pTableName);
X
Xiaoyu Wang 已提交
1232 1233 1234 1235 1236
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1237 1238
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset,
                         SNode* pSliding) {
X
Xiaoyu Wang 已提交
1239
  CHECK_PARSER_STATUS(pCxt);
1240
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1241 1242 1243 1244 1245 1246 1247
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1248

X
Xiaoyu Wang 已提交
1249
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName, SToken* pTableName) {
X
Xiaoyu Wang 已提交
1250
  CHECK_PARSER_STATUS(pCxt);
1251 1252 1253
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName)) {
    return NULL;
  }
1254
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1255
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1256
  pStmt->ignoreNotExists = ignoreNotExists;
1257 1258
  COPY_STRING_FORM_ID_TOKEN(pStmt->indexName, pIndexName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->tableName, pTableName);
1259 1260 1261
  return (SNode*)pStmt;
}

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

1270
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1271
  CHECK_PARSER_STATUS(pCxt);
1272
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1273
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1274
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1275 1276 1277
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1278
SNode* createCreateTopicStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName, SNode* pQuery,
1279
                             const SToken* pSubDbName, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1280
  CHECK_PARSER_STATUS(pCxt);
1281
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1282
  CHECK_OUT_OF_MEM(pStmt);
1283
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1284
  pStmt->ignoreExists = ignoreExists;
1285 1286 1287 1288 1289
  if (NULL != pRealTable) {
    strcpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName);
    strcpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName);
    nodesDestroyNode(pRealTable);
  } else if (NULL != pSubDbName) {
1290
    COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
1291 1292
  } else {
    pStmt->pQuery = pQuery;
1293 1294 1295 1296 1297
  }
  return (SNode*)pStmt;
}

SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1298
  CHECK_PARSER_STATUS(pCxt);
1299
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1300
  CHECK_OUT_OF_MEM(pStmt);
1301
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1302 1303 1304
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1305

X
Xiaoyu Wang 已提交
1306 1307 1308
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pCGroupId,
                            const SToken* pTopicName) {
  CHECK_PARSER_STATUS(pCxt);
1309
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1310 1311
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
1312 1313
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
X
Xiaoyu Wang 已提交
1314 1315 1316
  return (SNode*)pStmt;
}

1317
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1318
  CHECK_PARSER_STATUS(pCxt);
1319
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1320 1321 1322 1323 1324 1325 1326
  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;
}
1327 1328

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1329
  CHECK_PARSER_STATUS(pCxt);
1330
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1331 1332 1333 1334 1335 1336 1337
  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 已提交
1338
  CHECK_PARSER_STATUS(pCxt);
1339 1340 1341 1342 1343
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1344
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1345
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1346 1347 1348 1349
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1350
  CHECK_PARSER_STATUS(pCxt);
1351
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1352 1353 1354 1355 1356 1357
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1358 1359

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1360
  CHECK_PARSER_STATUS(pCxt);
1361
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1362 1363 1364 1365 1366 1367 1368 1369
  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 已提交
1370
  CHECK_PARSER_STATUS(pCxt);
1371 1372 1373 1374
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1375 1376

SNode* createCompactStmt(SAstCreateContext* pCxt, SNodeList* pVgroups) {
X
Xiaoyu Wang 已提交
1377
  CHECK_PARSER_STATUS(pCxt);
1378 1379 1380 1381 1382
  SNode* pStmt = nodesMakeNode(QUERY_NODE_COMPACT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1383 1384
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
X
Xiaoyu Wang 已提交
1385
  CHECK_PARSER_STATUS(pCxt);
1386
  if (pLibPath->n <= 2) {
1387
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1388 1389
    return NULL;
  }
1390
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1391
  CHECK_OUT_OF_MEM(pStmt);
1392
  pStmt->ignoreExists = ignoreExists;
1393
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1394
  pStmt->isAgg = aggFunc;
1395
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
1396 1397 1398
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
  return (SNode*)pStmt;
1399 1400
}

1401
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1402
  CHECK_PARSER_STATUS(pCxt);
1403
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1404
  CHECK_OUT_OF_MEM(pStmt);
1405
  pStmt->ignoreNotExists = ignoreNotExists;
1406
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1407
  return (SNode*)pStmt;
1408 1409
}

1410
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1411
  CHECK_PARSER_STATUS(pCxt);
1412
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1413 1414 1415 1416 1417
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->triggerType = STREAM_TRIGGER_AT_ONCE;
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1418 1419
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pStreamName, SNode* pRealTable,
                              SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1420
  CHECK_PARSER_STATUS(pCxt);
1421
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1422
  CHECK_OUT_OF_MEM(pStmt);
1423
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1424 1425 1426 1427 1428 1429 1430 1431 1432
  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;
1433 1434
}

1435
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1436
  CHECK_PARSER_STATUS(pCxt);
1437
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1438
  CHECK_OUT_OF_MEM(pStmt);
1439
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1440 1441
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1442 1443 1444
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
1445
  CHECK_PARSER_STATUS(pCxt);
1446
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
1447
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1448
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
1449
  return (SNode*)pStmt;
1450 1451
}

X
Xiaoyu Wang 已提交
1452 1453 1454 1455
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 已提交
1456
  trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
X
Xiaoyu Wang 已提交
1457 1458 1459
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1460 1461
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
1462
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
1463 1464 1465 1466
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

1467
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
1468
  CHECK_PARSER_STATUS(pCxt);
1469
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
1470
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1471 1472 1473
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
1474 1475 1476
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
1477
  CHECK_PARSER_STATUS(pCxt);
1478
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
1479
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1480 1481 1482
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
1483 1484 1485
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
1486
  CHECK_PARSER_STATUS(pCxt);
1487
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
1488
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1489 1490
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
1491 1492 1493
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
1494
  CHECK_PARSER_STATUS(pCxt);
1495 1496 1497 1498
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1499 1500

SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1501
  CHECK_PARSER_STATUS(pCxt);
1502 1503 1504
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1505
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
1506 1507
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1508 1509
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1510 1511 1512 1513
  return (SNode*)pStmt;
}

SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1514
  CHECK_PARSER_STATUS(pCxt);
1515 1516 1517
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1518
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
1519 1520
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1521 1522
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1523 1524
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1525

X
Xiaoyu Wang 已提交
1526
SNode* createCountFuncForDelete(SAstCreateContext* pCxt) {
1527
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
1528 1529 1530
  CHECK_OUT_OF_MEM(pFunc);
  strcpy(pFunc->functionName, "count");
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt))) {
1531
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
1532 1533 1534 1535 1536
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
1537 1538
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
1539
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
1540 1541 1542
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
X
Xiaoyu Wang 已提交
1543 1544
  pStmt->pCountFunc = createCountFuncForDelete(pCxt);
  if (NULL == pStmt->pCountFunc) {
1545
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
1546 1547
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
1548 1549
  return (SNode*)pStmt;
}