parAstCreater.c 69.2 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 237 238
  SNode*        pRealizedExpr = pRawExpr->pNode;
  if (nodesIsExprNode(pRealizedExpr)) {
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
X
Xiaoyu Wang 已提交
239
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
X
Xiaoyu Wang 已提交
240 241
      strcpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName);
      strcpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName);
X
Xiaoyu Wang 已提交
242
    } else {
X
Xiaoyu Wang 已提交
243 244 245 246 247
      int32_t len = TMIN(sizeof(pExpr->aliasName) - 1, pRawExpr->n);
      strncpy(pExpr->aliasName, pRawExpr->p, len);
      pExpr->aliasName[len] = '\0';
      strncpy(pExpr->userAlias, pRawExpr->p, len);
      pExpr->userAlias[len] = '\0';
X
Xiaoyu Wang 已提交
248
    }
D
dapan1121 已提交
249
  }
250 251
  pRawExpr->pNode = NULL;
  nodesDestroyNode(pNode);
X
Xiaoyu Wang 已提交
252
  return pRealizedExpr;
253 254 255
}

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

265
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
266
  CHECK_PARSER_STATUS(pCxt);
267 268
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
269
  pCxt->errCode = nodesListAppend(list, pNode);
X
Xiaoyu Wang 已提交
270
  return list;
271 272 273
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
274
  CHECK_PARSER_STATUS(pCxt);
275
  pCxt->errCode = nodesListAppend(pList, pNode);
X
Xiaoyu Wang 已提交
276
  return pList;
277 278
}

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

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

X
Xiaoyu Wang 已提交
313 314 315 316 317
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
  trimEscape(pLiteral);
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
}

318
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
319
  CHECK_PARSER_STATUS(pCxt);
320 321
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
322 323 324
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
325
  val->translate = false;
326 327 328
  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;
329
  return (SNode*)val;
330 331
}

X
Xiaoyu Wang 已提交
332
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
333
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
334 335 336 337
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
338 339 340 341 342 343 344 345 346 347 348 349
  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;
}

350
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
351
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
352 353 354 355
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
    return NULL;
  }
356 357
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
358 359
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
D
dapan1121 已提交
360
  val->placeholderNo = ++pCxt->placeholderNo;
X
Xiaoyu Wang 已提交
361 362 363
  if (NULL == pCxt->pPlaceholderValues) {
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    if (NULL == pCxt->pPlaceholderValues) {
364
      nodesDestroyNode((SNode*)val);
X
Xiaoyu Wang 已提交
365 366 367 368
      return NULL;
    }
  }
  taosArrayPush(pCxt->pPlaceholderValues, &val);
369 370 371
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
372 373 374 375 376 377 378 379 380 381 382
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);
  }
}

383
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
X
Xiaoyu Wang 已提交
384
  CHECK_PARSER_STATUS(pCxt);
385 386 387 388
  SLogicConditionNode* cond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  CHECK_OUT_OF_MEM(cond);
  cond->condType = type;
  cond->pParameterList = nodesMakeList();
X
Xiaoyu Wang 已提交
389 390 391
  int32_t code = addParamToLogicConditionNode(cond, pParam1);
  if (TSDB_CODE_SUCCESS == code && NULL != pParam2) {
    code = addParamToLogicConditionNode(cond, pParam2);
X
Xiaoyu Wang 已提交
392
  }
X
Xiaoyu Wang 已提交
393
  if (TSDB_CODE_SUCCESS != code) {
394
    nodesDestroyNode((SNode*)cond);
X
Xiaoyu Wang 已提交
395
    return NULL;
396
  }
397 398 399
  return (SNode*)cond;
}

400 401 402 403 404 405 406 407 408 409 410 411 412
static uint8_t getMinusDataType(uint8_t orgType) {
  switch (orgType) {
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      return TSDB_DATA_TYPE_BIGINT;
    default:
      break;
  }
  return orgType;
}

413
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
414
  CHECK_PARSER_STATUS(pCxt);
415 416
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
    SValueNode* pVal = (SValueNode*)pLeft;
417
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
418
    CHECK_OUT_OF_MEM(pNewLiteral);
419 420 421 422 423 424 425
    if ('+' == pVal->literal[0]) {
      sprintf(pNewLiteral, "-%s", pVal->literal + 1);
    } else if ('-' == pVal->literal[0]) {
      sprintf(pNewLiteral, "%s", pVal->literal + 1);
    } else {
      sprintf(pNewLiteral, "-%s", pVal->literal);
    }
426 427
    taosMemoryFree(pVal->literal);
    pVal->literal = pNewLiteral;
428
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
429 430
    return pLeft;
  }
431 432 433 434 435 436 437 438 439
  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 已提交
440
  CHECK_PARSER_STATUS(pCxt);
441
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
442 443
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
444 445 446
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
447
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
448 449
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
450 451
}

452
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
453
  CHECK_PARSER_STATUS(pCxt);
454
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
455 456
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
457 458 459 460 461
  if (NULL == pFuncName) {
    strcpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME);
  } else {
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
  }
X
Xiaoyu Wang 已提交
462 463 464
  return (SNode*)pCol;
}

465
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
466
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
467
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
468
    return createPrimaryKeyCol(pCxt, pFuncName);
X
Xiaoyu Wang 已提交
469
  }
470 471
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
472
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
473 474 475 476
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

477
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
X
Xiaoyu Wang 已提交
478
  CHECK_PARSER_STATUS(pCxt);
479 480 481 482
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
483 484 485 486
  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;
487
  }
488 489 490 491
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

492
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
493
  CHECK_PARSER_STATUS(pCxt);
494 495 496 497
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
498 499
}

X
Xiaoyu Wang 已提交
500
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
X
Xiaoyu Wang 已提交
501
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
502 503 504 505 506 507 508 509 510
  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 已提交
511
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
512
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
513
  if (!checkDbName(pCxt, pDbName, true) || !checkTableName(pCxt, pTableName) || !checkTableName(pCxt, pTableAlias)) {
514 515 516 517 518
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
519
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
520
  } else {
X
Xiaoyu Wang 已提交
521
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
522
  }
523
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
524
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
525
  } else {
526
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
527
  }
528
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
529
  return (SNode*)realTable;
530
}
531

532
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
533
  CHECK_PARSER_STATUS(pCxt);
534 535 536
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
537
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
538
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
X
Xiaoyu Wang 已提交
539
  } else {
540
    taosRandStr(tempTable->table.tableAlias, 8);
X
Xiaoyu Wang 已提交
541 542 543
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
544
    ((SSelectStmt*)pSubquery)->isSubquery = true;
545 546
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
547
  }
548
  return (SNode*)tempTable;
549 550
}

551
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
552
  CHECK_PARSER_STATUS(pCxt);
553 554 555
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
556 557
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
558
  joinTable->pOnCond = pJoinCond;
559
  return (SNode*)joinTable;
560
}
561

562
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
X
Xiaoyu Wang 已提交
563
  CHECK_PARSER_STATUS(pCxt);
564 565
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
wafwerar's avatar
wafwerar 已提交
566
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
567
  if (NULL != pOffset) {
wafwerar's avatar
wafwerar 已提交
568
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
569
  }
570
  return (SNode*)limitNode;
571 572
}

573
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
574
  CHECK_PARSER_STATUS(pCxt);
575 576
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
577
  orderByExpr->pExpr = pExpr;
578
  orderByExpr->order = order;
579 580 581
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
582
  orderByExpr->nullOrder = nullOrder;
583 584 585
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
586
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
587
  CHECK_PARSER_STATUS(pCxt);
588 589
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
590 591
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
592 593 594
  return (SNode*)session;
}

595
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
596
  CHECK_PARSER_STATUS(pCxt);
597 598
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
599
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
600
  if (NULL == state->pCol) {
601
    nodesDestroyNode((SNode*)state);
X
Xiaoyu Wang 已提交
602
    CHECK_OUT_OF_MEM(NULL);
603
  }
604
  state->pExpr = pExpr;
605 606 607
  return (SNode*)state;
}

X
Xiaoyu Wang 已提交
608 609
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                                SNode* pFill) {
X
Xiaoyu Wang 已提交
610
  CHECK_PARSER_STATUS(pCxt);
611 612
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
613
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
X
bugfix  
Xiaoyu Wang 已提交
614
  if (NULL == interval->pCol) {
615
    nodesDestroyNode((SNode*)interval);
X
Xiaoyu Wang 已提交
616
    CHECK_OUT_OF_MEM(NULL);
X
bugfix  
Xiaoyu Wang 已提交
617
  }
618 619 620 621 622 623 624 625
  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 已提交
626
  CHECK_PARSER_STATUS(pCxt);
627 628 629 630
  SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
  CHECK_OUT_OF_MEM(fill);
  fill->mode = mode;
  fill->pValues = pValues;
X
Xiaoyu Wang 已提交
631 632
  fill->pWStartTs = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == fill->pWStartTs) {
633
    nodesDestroyNode((SNode*)fill);
X
Xiaoyu Wang 已提交
634
    CHECK_OUT_OF_MEM(NULL);
X
Xiaoyu Wang 已提交
635
  }
636
  strcpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart");
637
  return (SNode*)fill;
638
}
639

640
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
641
  CHECK_PARSER_STATUS(pCxt);
642 643 644 645 646 647 648 649
  SGroupingSetNode* groupingSet = (SGroupingSetNode*)nodesMakeNode(QUERY_NODE_GROUPING_SET);
  CHECK_OUT_OF_MEM(groupingSet);
  groupingSet->groupingSetType = GP_TYPE_NORMAL;
  groupingSet->pParameterList = nodesMakeList();
  nodesListAppend(groupingSet->pParameterList, pNode);
  return (SNode*)groupingSet;
}

X
Xiaoyu Wang 已提交
650 651
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
  CHECK_PARSER_STATUS(pCxt);
652
  return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
X
Xiaoyu Wang 已提交
653 654
}

X
Xiaoyu Wang 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
  CHECK_PARSER_STATUS(pCxt);
  SWhenThenNode* pWhenThen = (SWhenThenNode*)nodesMakeNode(QUERY_NODE_WHEN_THEN);
  CHECK_OUT_OF_MEM(pWhenThen);
  pWhenThen->pWhen = pWhen;
  pWhenThen->pThen = pThen;
  return (SNode*)pWhenThen;
}

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

X
Xiaoyu Wang 已提交
674
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
X
Xiaoyu Wang 已提交
675
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
676
  trimEscape(pAlias);
X
Xiaoyu Wang 已提交
677 678 679 680 681 682
  SExprNode* pExpr = (SExprNode*)pNode;
  int32_t    len = TMIN(sizeof(pExpr->aliasName) - 1, pAlias->n);
  strncpy(pExpr->aliasName, pAlias->z, len);
  pExpr->aliasName[len] = '\0';
  strncpy(pExpr->userAlias, pAlias->z, len);
  pExpr->userAlias[len] = '\0';
683
  return pNode;
684 685
}

686
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
X
Xiaoyu Wang 已提交
687
  CHECK_PARSER_STATUS(pCxt);
688
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
689
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
690 691 692
  }
  return pStmt;
}
693

694
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
X
Xiaoyu Wang 已提交
695
  CHECK_PARSER_STATUS(pCxt);
696
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
697
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
698 699
  }
  return pStmt;
700 701
}

702
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
X
Xiaoyu Wang 已提交
703
  CHECK_PARSER_STATUS(pCxt);
704
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
705
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
706 707 708
  }
  return pStmt;
}
709

710
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
X
Xiaoyu Wang 已提交
711
  CHECK_PARSER_STATUS(pCxt);
712
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
713
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
714 715
  }
  return pStmt;
716 717
}

718
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
X
Xiaoyu Wang 已提交
719
  CHECK_PARSER_STATUS(pCxt);
720
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
721
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
722 723 724 725 726
  }
  return pStmt;
}

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
X
Xiaoyu Wang 已提交
727
  CHECK_PARSER_STATUS(pCxt);
728 729 730
  if (NULL == pOrderByList) {
    return pStmt;
  }
731
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
732
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
733 734
  } else {
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
735 736 737
  }
  return pStmt;
}
738

739
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
X
Xiaoyu Wang 已提交
740
  CHECK_PARSER_STATUS(pCxt);
741 742 743
  if (NULL == pSlimit) {
    return pStmt;
  }
744
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
745
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
746 747
  }
  return pStmt;
748 749
}

750
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
X
Xiaoyu Wang 已提交
751
  CHECK_PARSER_STATUS(pCxt);
752 753 754
  if (NULL == pLimit) {
    return pStmt;
  }
755
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
756
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
X
Xiaoyu Wang 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
  } else {
    ((SSetOperator*)pStmt)->pLimit = pLimit;
  }
  return pStmt;
}

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

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

SNode* addFillClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pFill) {
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
781 782 783
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
    SFillNode* pFillClause = (SFillNode*)pFill;
    nodesDestroyNode(pFillClause->pWStartTs);
784
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
X
Xiaoyu Wang 已提交
785
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
786
  }
787
  return pStmt;
788 789 790
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
791
  CHECK_PARSER_STATUS(pCxt);
792
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
793
  CHECK_OUT_OF_MEM(select);
794
  select->isDistinct = isDistinct;
795 796
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
797
  sprintf(select->stmtName, "%p", select);
798
  select->isTimeLineResult = true;
799
  select->onlyHasKeepOrderFunc = true;
800
  select->timeRange = TSWINDOW_INITIALIZER;
801
  return (SNode*)select;
802 803
}

X
Xiaoyu Wang 已提交
804 805 806 807 808 809
static void setSubquery(SNode* pStmt) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    ((SSelectStmt*)pStmt)->isSubquery = true;
  }
}

810
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
811
  CHECK_PARSER_STATUS(pCxt);
812 813 814
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
815
  setOp->pLeft = pLeft;
X
Xiaoyu Wang 已提交
816
  setSubquery(setOp->pLeft);
817
  setOp->pRight = pRight;
X
Xiaoyu Wang 已提交
818
  setSubquery(setOp->pRight);
819
  sprintf(setOp->stmtName, "%p", setOp);
820
  return (SNode*)setOp;
821 822
}

823 824 825 826 827 828 829 830 831 832 833 834 835 836
static void updateWalOptionsDefault(SDatabaseOptions* pOptions) {
  if (!pOptions->walRetentionPeriodIsSet) {
    pOptions->walRetentionPeriod =
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_PERIOD : TSDB_REP_DEF_DB_WAL_RET_PERIOD;
  }
  if (!pOptions->walRetentionSizeIsSet) {
    pOptions->walRetentionSize = pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_RET_SIZE : TSDB_REP_DEF_DB_WAL_RET_SIZE;
  }
  if (!pOptions->walRollPeriodIsSet) {
    pOptions->walRollPeriod =
        pOptions->replica > 1 ? TSDB_REPS_DEF_DB_WAL_ROLL_PERIOD : TSDB_REP_DEF_DB_WAL_ROLL_PERIOD;
  }
}

X
Xiaoyu Wang 已提交
837
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
838
  CHECK_PARSER_STATUS(pCxt);
839
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
840
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
841
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
842 843
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
X
Xiaoyu Wang 已提交
844 845 846 847 848 849 850 851 852 853
  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;
854
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
X
Xiaoyu Wang 已提交
855 856 857 858 859 860
  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 已提交
861
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
862
  updateWalOptionsDefault(pOptions);
863
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
864
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
865 866
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
X
Xiaoyu Wang 已提交
867
  return (SNode*)pOptions;
868 869
}

X
Xiaoyu Wang 已提交
870
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
871
  CHECK_PARSER_STATUS(pCxt);
872
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
873 874
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
875
  pOptions->cacheModel = -1;
876
  pOptions->cacheLastSize = -1;
X
Xiaoyu Wang 已提交
877 878 879 880 881 882 883 884 885 886
  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;
887
  pOptions->tsdbPageSize = -1;
X
Xiaoyu Wang 已提交
888 889 890 891 892 893
  pOptions->precision = -1;
  pOptions->replica = -1;
  pOptions->strict = -1;
  pOptions->walLevel = -1;
  pOptions->numOfVgroups = -1;
  pOptions->singleStable = -1;
X
Xiaoyu Wang 已提交
894
  pOptions->schemaless = -1;
895 896 897 898
  pOptions->walRetentionPeriod = -1;
  pOptions->walRetentionSize = -1;
  pOptions->walRollPeriod = -1;
  pOptions->walSegmentSize = -1;
899
  pOptions->sstTrigger = -1;
900 901
  pOptions->tablePrefix = -1;
  pOptions->tableSuffix = -1;
X
Xiaoyu Wang 已提交
902 903 904 905
  return (SNode*)pOptions;
}

SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
906
  CHECK_PARSER_STATUS(pCxt);
907
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
X
Xiaoyu Wang 已提交
908 909
  switch (type) {
    case DB_OPTION_BUFFER:
910
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
911
      break;
912
    case DB_OPTION_CACHEMODEL:
913
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
914
      break;
915
    case DB_OPTION_CACHESIZE:
916
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
917 918
      break;
    case DB_OPTION_COMP:
919
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
920
      break;
X
Xiaoyu Wang 已提交
921 922 923
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
924
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
925
      } else {
926
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
X
Xiaoyu Wang 已提交
927
      }
X
Xiaoyu Wang 已提交
928
      break;
X
Xiaoyu Wang 已提交
929
    }
X
Xiaoyu Wang 已提交
930
    case DB_OPTION_FSYNC:
931
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
932 933
      break;
    case DB_OPTION_MAXROWS:
934
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
935 936
      break;
    case DB_OPTION_MINROWS:
937
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
938 939
      break;
    case DB_OPTION_KEEP:
940
      pDbOptions->pKeep = pVal;
X
Xiaoyu Wang 已提交
941 942
      break;
    case DB_OPTION_PAGES:
943
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
944 945
      break;
    case DB_OPTION_PAGESIZE:
946
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
947
      break;
948 949 950
    case DB_OPTION_TSDB_PAGESIZE:
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
X
Xiaoyu Wang 已提交
951
    case DB_OPTION_PRECISION:
952
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
953 954
      break;
    case DB_OPTION_REPLICA:
955 956
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
      updateWalOptionsDefault(pDbOptions);
X
Xiaoyu Wang 已提交
957
      break;
X
Xiaoyu Wang 已提交
958
    case DB_OPTION_STRICT:
959
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
960 961
      break;
    case DB_OPTION_WAL:
962
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
963 964
      break;
    case DB_OPTION_VGROUPS:
965
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
966 967
      break;
    case DB_OPTION_SINGLE_STABLE:
968
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
969 970
      break;
    case DB_OPTION_RETENTIONS:
971
      pDbOptions->pRetentions = pVal;
X
Xiaoyu Wang 已提交
972
      break;
X
Xiaoyu Wang 已提交
973
    case DB_OPTION_WAL_RETENTION_PERIOD:
974 975
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionPeriodIsSet = true;
X
Xiaoyu Wang 已提交
976 977
      break;
    case DB_OPTION_WAL_RETENTION_SIZE:
978 979
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionSizeIsSet = true;
X
Xiaoyu Wang 已提交
980 981
      break;
    case DB_OPTION_WAL_ROLL_PERIOD:
982 983
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRollPeriodIsSet = true;
X
Xiaoyu Wang 已提交
984 985
      break;
    case DB_OPTION_WAL_SEGMENT_SIZE:
986
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
987
      break;
988
    case DB_OPTION_STT_TRIGGER:
989 990
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
991 992 993 994 995 996
    case DB_OPTION_TABLE_PREFIX:
      pDbOptions->tablePrefix = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
    case DB_OPTION_TABLE_SUFFIX:
      pDbOptions->tableSuffix = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
X
Xiaoyu Wang 已提交
997 998
    default:
      break;
X
Xiaoyu Wang 已提交
999
  }
X
Xiaoyu Wang 已提交
1000
  return pOptions;
X
Xiaoyu Wang 已提交
1001 1002
}

X
Xiaoyu Wang 已提交
1003
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
1004
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
  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 已提交
1015
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1016
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1017
  if (!checkDbName(pCxt, pDbName, false)) {
1018 1019 1020 1021
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1022
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1023
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1024
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1025
  return (SNode*)pStmt;
1026
}
X
Xiaoyu Wang 已提交
1027

X
Xiaoyu Wang 已提交
1028
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1029
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1030
  if (!checkDbName(pCxt, pDbName, false)) {
1031 1032 1033 1034
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1035
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1036 1037 1038 1039
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1040
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1041
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1042
  if (!checkDbName(pCxt, pDbName, false)) {
1043 1044
    return NULL;
  }
1045
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
1046
  CHECK_OUT_OF_MEM(pStmt);
1047
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1048 1049 1050 1051
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1052 1053 1054 1055 1056
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
1057 1058 1059 1060 1061 1062
  SFlushDatabaseStmt* pStmt = (SFlushDatabaseStmt*)nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
}

1063
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
X
Xiaoyu Wang 已提交
1064 1065 1066 1067 1068
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
  STrimDatabaseStmt* pStmt = (STrimDatabaseStmt*)nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT);
X
Xiaoyu Wang 已提交
1069 1070
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1071
  pStmt->maxSpeed = maxSpeed;
X
Xiaoyu Wang 已提交
1072 1073 1074
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1075
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1076
  CHECK_PARSER_STATUS(pCxt);
1077
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
1078
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
1079 1080 1081 1082
  pOptions->maxDelay1 = -1;
  pOptions->maxDelay2 = -1;
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
X
Xiaoyu Wang 已提交
1083
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
X
Xiaoyu Wang 已提交
1084
  pOptions->commentNull = true;  // mark null
1085
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
1086 1087
}

X
Xiaoyu Wang 已提交
1088
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1089
  CHECK_PARSER_STATUS(pCxt);
1090
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
1091 1092
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->ttl = -1;
X
Xiaoyu Wang 已提交
1093
  pOptions->commentNull = true;  // mark null
X
Xiaoyu Wang 已提交
1094 1095 1096 1097
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
1098
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1099
  switch (type) {
X
Xiaoyu Wang 已提交
1100
    case TABLE_OPTION_COMMENT:
1101
      if (checkComment(pCxt, (SToken*)pVal, true)) {
wmmhello's avatar
wmmhello 已提交
1102
        ((STableOptions*)pOptions)->commentNull = false;
1103
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
1104
      }
X
Xiaoyu Wang 已提交
1105
      break;
X
Xiaoyu Wang 已提交
1106 1107 1108 1109 1110
    case TABLE_OPTION_MAXDELAY:
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
      break;
    case TABLE_OPTION_WATERMARK:
      ((STableOptions*)pOptions)->pWatermark = pVal;
X
Xiaoyu Wang 已提交
1111
      break;
X
Xiaoyu Wang 已提交
1112 1113 1114
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
X
Xiaoyu Wang 已提交
1115
    case TABLE_OPTION_TTL: {
1116
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1117
      if (ttl > INT32_MAX) {
1118 1119 1120 1121
        ttl = INT32_MAX;
      }
      // ttl can not be smaller than 0, because there is a limitation in sql.y (TTL NK_INTEGER)
      ((STableOptions*)pOptions)->ttl = ttl;
X
Xiaoyu Wang 已提交
1122
      break;
1123
    }
X
Xiaoyu Wang 已提交
1124 1125
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
1126 1127 1128
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
1129
  }
X
Xiaoyu Wang 已提交
1130
  return pOptions;
X
Xiaoyu Wang 已提交
1131 1132
}

X
Xiaoyu Wang 已提交
1133
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
1134
  CHECK_PARSER_STATUS(pCxt);
1135
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
1136 1137
    return NULL;
  }
X
Xiaoyu Wang 已提交
1138 1139
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
1140
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
X
Xiaoyu Wang 已提交
1141 1142
  pCol->dataType = dataType;
  if (NULL != pComment) {
1143
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
1144
  }
1145
  pCol->sma = true;
X
Xiaoyu Wang 已提交
1146 1147 1148
  return (SNode*)pCol;
}

1149
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
1150
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
1151 1152 1153 1154
  return dt;
}

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

X
Xiaoyu Wang 已提交
1159 1160
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1161
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1162 1163
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1164 1165
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1166 1167
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
1168
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
1169
  pStmt->pOptions = (STableOptions*)pOptions;
1170
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1171 1172
  return (SNode*)pStmt;
}
1173

X
Xiaoyu Wang 已提交
1174
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
1175
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1176
  CHECK_PARSER_STATUS(pCxt);
1177
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
1178
  CHECK_OUT_OF_MEM(pStmt);
1179 1180 1181 1182
  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);
1183 1184 1185
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
wmmhello's avatar
wmmhello 已提交
1186
  pStmt->pOptions = (STableOptions*)pOptions;
1187 1188
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
1189 1190 1191 1192
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
1193
  CHECK_PARSER_STATUS(pCxt);
1194
  SCreateMultiTableStmt* pStmt = (SCreateMultiTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLE_STMT);
1195 1196 1197 1198 1199
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

1200
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1201
  CHECK_PARSER_STATUS(pCxt);
1202
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
1203 1204 1205 1206 1207 1208 1209 1210 1211
  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 已提交
1212
  CHECK_PARSER_STATUS(pCxt);
1213
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
1214 1215 1216 1217 1218
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

1219
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1220
  CHECK_PARSER_STATUS(pCxt);
1221
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
1222 1223 1224 1225 1226 1227 1228 1229
  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 已提交
1230 1231 1232 1233 1234 1235 1236
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 已提交
1237
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1238
  CHECK_PARSER_STATUS(pCxt);
1239
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1240 1241 1242
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1243
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1244 1245
}

X
Xiaoyu Wang 已提交
1246 1247
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
                                    SDataType dataType) {
X
Xiaoyu Wang 已提交
1248 1249
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1250 1251
    return NULL;
  }
1252
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1253 1254
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1255
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1256
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
1257
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1258 1259
}

X
Xiaoyu Wang 已提交
1260
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1261 1262
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1263 1264
    return NULL;
  }
1265
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1266 1267
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1268
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
X
Xiaoyu Wang 已提交
1269
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1270 1271
}

X
Xiaoyu Wang 已提交
1272 1273
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1274 1275
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1276 1277
    return NULL;
  }
1278
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1279 1280
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1281 1282
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
X
Xiaoyu Wang 已提交
1283
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1284 1285
}

X
Xiaoyu Wang 已提交
1286
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1287 1288
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
X
bugfix  
Xiaoyu Wang 已提交
1289 1290
    return NULL;
  }
1291
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1292 1293
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
1294
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
1295
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1296
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1297 1298
}

1299 1300 1301 1302 1303
SNode* setAlterSuperTableType(SNode* pStmt) {
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
  return pStmt;
}

1304
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1305
  CHECK_PARSER_STATUS(pCxt);
1306 1307 1308
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1309 1310
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1311
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1312 1313 1314
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1315
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1316
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
1317
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
1318
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type;
X
Xiaoyu Wang 已提交
1319 1320
}

1321 1322 1323 1324 1325 1326 1327 1328 1329
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type) {
  CHECK_PARSER_STATUS(pCxt);
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

SNode* createShowStmtWithCond(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbName,
                              EOperatorType tableCondType) {
X
Xiaoyu Wang 已提交
1330
  CHECK_PARSER_STATUS(pCxt);
1331
  if (needDbShowStmt(type) && NULL == pDbName) {
X
Xiaoyu Wang 已提交
1332
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1333
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1334 1335
    return NULL;
  }
1336
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1337
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1338
  pStmt->pDbName = pDbName;
1339 1340
  pStmt->pTbName = pTbName;
  pStmt->tableCondType = tableCondType;
1341
  return (SNode*)pStmt;
1342
}
1343

1344
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1345
  CHECK_PARSER_STATUS(pCxt);
1346 1347 1348 1349
  if (!checkDbName(pCxt, pDbName, true)) {
    return NULL;
  }
  SShowCreateDatabaseStmt* pStmt = (SShowCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
1350
  CHECK_OUT_OF_MEM(pStmt);
1351 1352
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
1353 1354 1355
}

SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1356
  CHECK_PARSER_STATUS(pCxt);
1357
  SShowCreateTableStmt* pStmt = (SShowCreateTableStmt*)nodesMakeNode(type);
1358
  CHECK_OUT_OF_MEM(pStmt);
1359 1360
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
  nodesDestroyNode(pRealTable);
  return (SNode*)pStmt;
}

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

1375
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
1376 1377 1378 1379
  CHECK_PARSER_STATUS(pCxt);
  SShowDnodeVariablesStmt* pStmt = (SShowDnodeVariablesStmt*)nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDnodeId = pDnodeId;
1380
  pStmt->pLikePattern = pLikePattern;
1381 1382 1383
  return (SNode*)pStmt;
}

1384 1385 1386 1387 1388 1389 1390 1391 1392
SNode* createShowVnodesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pDnodeEndpoint) {
  CHECK_PARSER_STATUS(pCxt);
  SShowVnodesStmt* pStmt = (SShowVnodesStmt*)nodesMakeNode(QUERY_NODE_SHOW_VNODES_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDnodeId = pDnodeId;
  pStmt->pDnodeEndpoint = pDnodeEndpoint;
  return (SNode*)pStmt;
}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
  CHECK_PARSER_STATUS(pCxt);
  if (NULL == pDbName) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
    return NULL;
  }
  SShowTableTagsStmt* pStmt = (SShowTableTagsStmt*)nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDbName = pDbName;
  pStmt->pTbName = pTbName;
  pStmt->pTags = pTags;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1408
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo) {
X
Xiaoyu Wang 已提交
1409
  CHECK_PARSER_STATUS(pCxt);
1410
  char password[TSDB_USET_PASSWORD_LEN + 3] = {0};
1411 1412 1413 1414 1415
  if (!checkUserName(pCxt, pUserName) || !checkPassword(pCxt, pPassword, password)) {
    return NULL;
  }
  SCreateUserStmt* pStmt = (SCreateUserStmt*)nodesMakeNode(QUERY_NODE_CREATE_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1416
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1417
  strcpy(pStmt->password, password);
X
Xiaoyu Wang 已提交
1418
  pStmt->sysinfo = sysinfo;
1419 1420 1421
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1422
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1423
  CHECK_PARSER_STATUS(pCxt);
1424 1425 1426 1427 1428
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1429
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
X
Xiaoyu Wang 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
  pStmt->alterType = alterType;
  switch (alterType) {
    case TSDB_ALTER_USER_PASSWD: {
      char password[TSDB_USET_PASSWORD_LEN] = {0};
      if (!checkPassword(pCxt, pVal, password)) {
        nodesDestroyNode((SNode*)pStmt);
        return NULL;
      }
      strcpy(pStmt->password, password);
      break;
1440
    }
X
Xiaoyu Wang 已提交
1441 1442 1443 1444 1445 1446 1447 1448
    case TSDB_ALTER_USER_ENABLE:
      pStmt->enable = taosStr2Int8(pVal->z, NULL, 10);
      break;
    case TSDB_ALTER_USER_SYSINFO:
      pStmt->sysinfo = taosStr2Int8(pVal->z, NULL, 10);
      break;
    default:
      break;
1449 1450 1451 1452
  }
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1453
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1454
  CHECK_PARSER_STATUS(pCxt);
1455 1456 1457 1458 1459
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1460
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1461 1462 1463 1464
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1465
  CHECK_PARSER_STATUS(pCxt);
1466 1467
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1468 1469 1470
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
    nodesDestroyNode((SNode*)pStmt);
    return NULL;
1471 1472 1473 1474
  }
  return (SNode*)pStmt;
}

1475
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force) {
X
Xiaoyu Wang 已提交
1476
  CHECK_PARSER_STATUS(pCxt);
1477 1478 1479
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1480
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1481
  } else {
1482
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
1483
      nodesDestroyNode((SNode*)pStmt);
1484 1485 1486
      return NULL;
    }
  }
1487
  pStmt->force = force;
1488 1489
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1490

X
Xiaoyu Wang 已提交
1491 1492
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1493
  CHECK_PARSER_STATUS(pCxt);
1494
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1495
  CHECK_OUT_OF_MEM(pStmt);
1496 1497 1498 1499 1500
  if (NULL != pDnode) {
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
  } else {
    pStmt->dnodeId = -1;
  }
1501 1502 1503 1504 1505 1506 1507
  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 已提交
1508
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
1509
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1510
  CHECK_PARSER_STATUS(pCxt);
1511
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1512 1513
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1514
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1515 1516 1517 1518
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1519
  nodesDestroyNode(pIndexName);
1520
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1521 1522 1523 1524 1525
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

1526 1527
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                         SNode* pStreamOptions) {
X
Xiaoyu Wang 已提交
1528
  CHECK_PARSER_STATUS(pCxt);
1529
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1530 1531 1532 1533 1534
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
1535
  pOptions->pStreamOptions = pStreamOptions;
X
Xiaoyu Wang 已提交
1536 1537
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1538

X
Xiaoyu Wang 已提交
1539
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
X
Xiaoyu Wang 已提交
1540
  CHECK_PARSER_STATUS(pCxt);
1541
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1542
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1543
  pStmt->ignoreNotExists = ignoreNotExists;
X
Xiaoyu Wang 已提交
1544 1545
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
X
Xiaoyu Wang 已提交
1546
  nodesDestroyNode(pIndexName);
1547 1548 1549
  return (SNode*)pStmt;
}

1550
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1551
  CHECK_PARSER_STATUS(pCxt);
1552
  SCreateComponentNodeStmt* pStmt = (SCreateComponentNodeStmt*)nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1553
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1554
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
X
Xiaoyu Wang 已提交
1555 1556
  return (SNode*)pStmt;
}
1557

1558
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1559
  CHECK_PARSER_STATUS(pCxt);
1560
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1561
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1562
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1563 1564 1565
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1566 1567
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName,
                                     SNode* pQuery) {
X
Xiaoyu Wang 已提交
1568
  CHECK_PARSER_STATUS(pCxt);
1569
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1570
  CHECK_OUT_OF_MEM(pStmt);
1571
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1572
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}

SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName,
                                  const SToken* pSubDbName, bool withMeta) {
  CHECK_PARSER_STATUS(pCxt);
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  pStmt->ignoreExists = ignoreExists;
  COPY_STRING_FORM_ID_TOKEN(pStmt->subDbName, pSubDbName);
  pStmt->withMeta = withMeta;
  return (SNode*)pStmt;
}

SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName,
                                     SNode* pRealTable, bool withMeta) {
  CHECK_PARSER_STATUS(pCxt);
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  pStmt->ignoreExists = ignoreExists;
  pStmt->withMeta = withMeta;
  strcpy(pStmt->subDbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->subSTbName, ((SRealTableNode*)pRealTable)->table.tableName);
  nodesDestroyNode(pRealTable);
1600 1601 1602 1603
  return (SNode*)pStmt;
}

SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1604
  CHECK_PARSER_STATUS(pCxt);
1605
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1606
  CHECK_OUT_OF_MEM(pStmt);
1607
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1608 1609 1610
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1611

X
Xiaoyu Wang 已提交
1612 1613 1614
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pCGroupId,
                            const SToken* pTopicName) {
  CHECK_PARSER_STATUS(pCxt);
1615
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1616 1617
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
1618 1619
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
X
Xiaoyu Wang 已提交
1620 1621 1622
  return (SNode*)pStmt;
}

1623
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1624
  CHECK_PARSER_STATUS(pCxt);
1625
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1626 1627 1628 1629 1630 1631 1632
  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;
}
1633 1634

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1635
  CHECK_PARSER_STATUS(pCxt);
1636
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1637 1638 1639 1640 1641 1642 1643
  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 已提交
1644
  CHECK_PARSER_STATUS(pCxt);
1645 1646 1647 1648 1649
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1650
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1651
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1652 1653 1654 1655
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1656
  CHECK_PARSER_STATUS(pCxt);
1657
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1658 1659 1660 1661 1662 1663
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1664 1665

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1666
  CHECK_PARSER_STATUS(pCxt);
1667
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1668 1669 1670 1671 1672 1673 1674 1675
  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 已提交
1676
  CHECK_PARSER_STATUS(pCxt);
1677 1678 1679 1680
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1681 1682

SNode* createCompactStmt(SAstCreateContext* pCxt, SNodeList* pVgroups) {
X
Xiaoyu Wang 已提交
1683
  CHECK_PARSER_STATUS(pCxt);
1684 1685 1686 1687 1688
  SNode* pStmt = nodesMakeNode(QUERY_NODE_COMPACT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1689 1690
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
X
Xiaoyu Wang 已提交
1691
  CHECK_PARSER_STATUS(pCxt);
1692
  if (pLibPath->n <= 2) {
1693
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1694 1695
    return NULL;
  }
1696
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1697
  CHECK_OUT_OF_MEM(pStmt);
1698
  pStmt->ignoreExists = ignoreExists;
1699
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1700
  pStmt->isAgg = aggFunc;
1701
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
1702 1703 1704
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
  return (SNode*)pStmt;
1705 1706
}

1707
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1708
  CHECK_PARSER_STATUS(pCxt);
1709
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1710
  CHECK_OUT_OF_MEM(pStmt);
1711
  pStmt->ignoreNotExists = ignoreNotExists;
1712
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1713
  return (SNode*)pStmt;
1714 1715
}

1716
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1717
  CHECK_PARSER_STATUS(pCxt);
1718
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1719 1720
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->triggerType = STREAM_TRIGGER_AT_ONCE;
1721
  pOptions->fillHistory = STREAM_DEFAULT_FILL_HISTORY;
X
Xiaoyu Wang 已提交
1722
  pOptions->ignoreExpired = STREAM_DEFAULT_IGNORE_EXPIRED;
1723 1724 1725
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1726
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pStreamName, SNode* pRealTable,
1727
                              SNode* pOptions, SNodeList* pTags, SNode* pSubtable, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1728
  CHECK_PARSER_STATUS(pCxt);
1729
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1730
  CHECK_OUT_OF_MEM(pStmt);
1731
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1732 1733 1734 1735 1736 1737 1738 1739
  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;
1740 1741
  pStmt->pTags = pTags;
  pStmt->pSubtable = pSubtable;
1742
  return (SNode*)pStmt;
1743 1744
}

1745
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1746
  CHECK_PARSER_STATUS(pCxt);
1747
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1748
  CHECK_OUT_OF_MEM(pStmt);
1749
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1750 1751
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1752 1753 1754
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
1755
  CHECK_PARSER_STATUS(pCxt);
1756
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
1757
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1758
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
1759
  return (SNode*)pStmt;
1760 1761
}

X
Xiaoyu Wang 已提交
1762 1763 1764 1765
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 已提交
1766
  trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
X
Xiaoyu Wang 已提交
1767 1768 1769
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1770 1771
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
1772
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
1773 1774 1775 1776
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

1777
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
1778
  CHECK_PARSER_STATUS(pCxt);
1779
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
1780
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1781 1782 1783
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
1784 1785 1786
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
1787
  CHECK_PARSER_STATUS(pCxt);
1788
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
1789
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1790 1791 1792
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
1793 1794 1795
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
1796
  CHECK_PARSER_STATUS(pCxt);
1797
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
1798
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1799 1800
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
1801 1802 1803
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
1804
  CHECK_PARSER_STATUS(pCxt);
1805 1806 1807 1808
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1809 1810

SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1811
  CHECK_PARSER_STATUS(pCxt);
1812 1813 1814
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1815
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
1816 1817
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1818
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, pDbName);
1819
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1820 1821 1822 1823
  return (SNode*)pStmt;
}

SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1824
  CHECK_PARSER_STATUS(pCxt);
1825 1826 1827
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1828
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
1829 1830
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1831
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, pDbName);
1832
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1833 1834
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1835

1836
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
1837
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
1838
  CHECK_OUT_OF_MEM(pFunc);
X
Xiaoyu Wang 已提交
1839
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
1840
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt, NULL))) {
1841
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
1842 1843 1844 1845 1846
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
1847 1848
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
1849
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
1850 1851 1852
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
1853 1854 1855 1856
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
  if (NULL == pStmt->pCountFunc || NULL == pStmt->pFirstFunc || NULL == pStmt->pLastFunc) {
1857
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
1858 1859
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
1860 1861
  return (SNode*)pStmt;
}
1862 1863 1864 1865 1866 1867 1868 1869

SNode* createInsertStmt(SAstCreateContext* pCxt, SNode* pTable, SNodeList* pCols, SNode* pQuery) {
  CHECK_PARSER_STATUS(pCxt);
  SInsertStmt* pStmt = (SInsertStmt*)nodesMakeNode(QUERY_NODE_INSERT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTable = pTable;
  pStmt->pCols = pCols;
  pStmt->pQuery = pQuery;
1870 1871 1872 1873 1874
  if (QUERY_NODE_SELECT_STMT == nodeType(pQuery)) {
    strcpy(((SSelectStmt*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias);
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pQuery)) {
    strcpy(((SSetOperator*)pQuery)->stmtName, ((STableNode*)pTable)->tableAlias);
  }
1875 1876
  return (SNode*)pStmt;
}