parAstCreater.c 64.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);
X
Xiaoyu Wang 已提交
342 343 344 345
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
    return NULL;
  }
346 347
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
348 349
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
D
dapan1121 已提交
350
  val->placeholderNo = ++pCxt->placeholderNo;
X
Xiaoyu Wang 已提交
351 352 353
  if (NULL == pCxt->pPlaceholderValues) {
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    if (NULL == pCxt->pPlaceholderValues) {
354
      nodesDestroyNode((SNode*)val);
X
Xiaoyu Wang 已提交
355 356 357 358
      return NULL;
    }
  }
  taosArrayPush(pCxt->pPlaceholderValues, &val);
359 360 361
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
362 363 364 365 366 367 368 369 370 371 372
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);
  }
}

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

390 391 392 393 394 395 396 397 398 399 400 401 402
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;
}

403
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
404
  CHECK_PARSER_STATUS(pCxt);
405 406
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
    SValueNode* pVal = (SValueNode*)pLeft;
407
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
408
    CHECK_OUT_OF_MEM(pNewLiteral);
409 410 411 412 413 414 415
    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);
    }
416 417
    taosMemoryFree(pVal->literal);
    pVal->literal = pNewLiteral;
418
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
419 420
    return pLeft;
  }
421 422 423 424 425 426 427 428 429
  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 已提交
430
  CHECK_PARSER_STATUS(pCxt);
431
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
432 433
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
434 435 436
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
437
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
438 439
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
440 441
}

X
Xiaoyu Wang 已提交
442
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
443
  CHECK_PARSER_STATUS(pCxt);
444
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
445 446 447 448 449 450
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(pCol->colName, PK_TS_COL_INTERNAL_NAME);
  return (SNode*)pCol;
}

451
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
452
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
453 454 455
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
    return createPrimaryKeyCol(pCxt);
  }
456 457
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
458
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
459 460 461 462
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

463
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
X
Xiaoyu Wang 已提交
464
  CHECK_PARSER_STATUS(pCxt);
465 466 467 468
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
469 470 471 472
  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;
473
  }
474 475 476 477
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

478
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
479
  CHECK_PARSER_STATUS(pCxt);
480 481 482 483
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
484 485
}

X
Xiaoyu Wang 已提交
486
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
X
Xiaoyu Wang 已提交
487
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
488 489 490 491 492 493 494 495 496
  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 已提交
497
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
498
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
499
  if (!checkDbName(pCxt, pDbName, true) || !checkTableName(pCxt, pTableName) || !checkTableName(pCxt, pTableAlias)) {
500 501 502 503 504
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
505
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
506 507 508
  } else {
    strcpy(realTable->table.dbName, pCxt->pQueryCxt->db);
  }
509
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
510
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
511
  } else {
512
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
513
  }
514
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
515
  return (SNode*)realTable;
516
}
517

518
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
519
  CHECK_PARSER_STATUS(pCxt);
520 521 522
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
523
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
524
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
X
Xiaoyu Wang 已提交
525
  } else {
526
    taosRandStr(tempTable->table.tableAlias, 8);
X
Xiaoyu Wang 已提交
527 528 529
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
530
    ((SSelectStmt*)pSubquery)->isSubquery = true;
531 532
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
533
  }
534
  return (SNode*)tempTable;
535 536
}

537
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
538
  CHECK_PARSER_STATUS(pCxt);
539 540 541
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
542 543
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
544
  joinTable->pOnCond = pJoinCond;
545
  return (SNode*)joinTable;
546
}
547

548
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
X
Xiaoyu Wang 已提交
549
  CHECK_PARSER_STATUS(pCxt);
550 551
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
wafwerar's avatar
wafwerar 已提交
552
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
553
  if (NULL != pOffset) {
wafwerar's avatar
wafwerar 已提交
554
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
555
  }
556
  return (SNode*)limitNode;
557 558
}

559
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
560
  CHECK_PARSER_STATUS(pCxt);
561 562
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
563
  orderByExpr->pExpr = pExpr;
564
  orderByExpr->order = order;
565 566 567
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
568
  orderByExpr->nullOrder = nullOrder;
569 570 571
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
572
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
573
  CHECK_PARSER_STATUS(pCxt);
574 575
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
576 577
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
578 579 580
  return (SNode*)session;
}

581
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
582
  CHECK_PARSER_STATUS(pCxt);
583 584
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
X
Xiaoyu Wang 已提交
585
  state->pCol = createPrimaryKeyCol(pCxt);
586
  if (NULL == state->pCol) {
587
    nodesDestroyNode((SNode*)state);
588 589
    CHECK_OUT_OF_MEM(state->pCol);
  }
590
  state->pExpr = pExpr;
591 592 593
  return (SNode*)state;
}

X
Xiaoyu Wang 已提交
594 595
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                                SNode* pFill) {
X
Xiaoyu Wang 已提交
596
  CHECK_PARSER_STATUS(pCxt);
597 598
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
X
Xiaoyu Wang 已提交
599
  interval->pCol = createPrimaryKeyCol(pCxt);
X
bugfix  
Xiaoyu Wang 已提交
600
  if (NULL == interval->pCol) {
601
    nodesDestroyNode((SNode*)interval);
X
bugfix  
Xiaoyu Wang 已提交
602 603
    CHECK_OUT_OF_MEM(interval->pCol);
  }
604 605 606 607 608 609 610 611
  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 已提交
612
  CHECK_PARSER_STATUS(pCxt);
613 614 615 616
  SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
  CHECK_OUT_OF_MEM(fill);
  fill->mode = mode;
  fill->pValues = pValues;
X
Xiaoyu Wang 已提交
617 618
  fill->pWStartTs = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == fill->pWStartTs) {
619
    nodesDestroyNode((SNode*)fill);
X
Xiaoyu Wang 已提交
620 621
    CHECK_OUT_OF_MEM(fill->pWStartTs);
  }
622
  strcpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart");
623
  return (SNode*)fill;
624
}
625

626
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
627
  CHECK_PARSER_STATUS(pCxt);
628 629 630 631 632 633 634 635
  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 已提交
636 637 638 639 640
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
  CHECK_PARSER_STATUS(pCxt);
  return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt), pStart, pEnd);
}

X
Xiaoyu Wang 已提交
641
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
X
Xiaoyu Wang 已提交
642
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
643
  trimEscape(pAlias);
D
dapan1121 已提交
644 645 646
  int32_t len = TMIN(sizeof(((SExprNode*)pNode)->aliasName) - 1, pAlias->n);
  strncpy(((SExprNode*)pNode)->aliasName, pAlias->z, len);
  ((SExprNode*)pNode)->aliasName[len] = '\0';
647 648
  strncpy(((SExprNode*)pNode)->userAlias, pAlias->z, len);
  ((SExprNode*)pNode)->userAlias[len] = '\0';
649
  return pNode;
650 651
}

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

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

668
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
X
Xiaoyu Wang 已提交
669
  CHECK_PARSER_STATUS(pCxt);
670
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
671
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
672 673 674
  }
  return pStmt;
}
675

676
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
X
Xiaoyu Wang 已提交
677
  CHECK_PARSER_STATUS(pCxt);
678
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
679
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
680 681
  }
  return pStmt;
682 683
}

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

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
X
Xiaoyu Wang 已提交
693
  CHECK_PARSER_STATUS(pCxt);
694 695 696
  if (NULL == pOrderByList) {
    return pStmt;
  }
697
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
698
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
699 700
  } else {
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
701 702 703
  }
  return pStmt;
}
704

705
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
X
Xiaoyu Wang 已提交
706
  CHECK_PARSER_STATUS(pCxt);
707 708 709
  if (NULL == pSlimit) {
    return pStmt;
  }
710
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
711
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
712 713
  }
  return pStmt;
714 715
}

716
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
X
Xiaoyu Wang 已提交
717
  CHECK_PARSER_STATUS(pCxt);
718 719 720
  if (NULL == pLimit) {
    return pStmt;
  }
721
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
722
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
X
Xiaoyu Wang 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
  } 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 已提交
747 748 749 750 751
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
    SFillNode* pFillClause = (SFillNode*)pFill;
    nodesDestroyNode(pFillClause->pWStartTs);
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt);
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
752
  }
753
  return pStmt;
754 755 756
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
757
  CHECK_PARSER_STATUS(pCxt);
758
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
759
  CHECK_OUT_OF_MEM(select);
760
  select->isDistinct = isDistinct;
761 762
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
763
  sprintf(select->stmtName, "%p", select);
764
  select->isTimeLineResult = true;
765
  select->timeRange = TSWINDOW_INITIALIZER;
766
  return (SNode*)select;
767 768 769
}

SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
770
  CHECK_PARSER_STATUS(pCxt);
771 772 773
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
774 775
  setOp->pLeft = pLeft;
  setOp->pRight = pRight;
776
  sprintf(setOp->stmtName, "%p", setOp);
777
  return (SNode*)setOp;
778 779
}

X
Xiaoyu Wang 已提交
780
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
781
  CHECK_PARSER_STATUS(pCxt);
782
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
783
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
784
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
785 786
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
X
Xiaoyu Wang 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
  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 已提交
803
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
X
Xiaoyu Wang 已提交
804
  return (SNode*)pOptions;
805 806
}

X
Xiaoyu Wang 已提交
807
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
808
  CHECK_PARSER_STATUS(pCxt);
809
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
810 811
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
812
  pOptions->cacheModel = -1;
813
  pOptions->cacheLastSize = -1;
X
Xiaoyu Wang 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  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 已提交
830
  pOptions->schemaless = -1;
X
Xiaoyu Wang 已提交
831 832 833 834
  return (SNode*)pOptions;
}

SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
835
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
836 837
  switch (type) {
    case DB_OPTION_BUFFER:
wafwerar's avatar
wafwerar 已提交
838
      ((SDatabaseOptions*)pOptions)->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
839
      break;
840 841
    case DB_OPTION_CACHEMODEL:
      COPY_STRING_FORM_STR_TOKEN(((SDatabaseOptions*)pOptions)->cacheModelStr, (SToken*)pVal);
842
      break;
843
    case DB_OPTION_CACHESIZE:
844
      ((SDatabaseOptions*)pOptions)->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
845 846
      break;
    case DB_OPTION_COMP:
wafwerar's avatar
wafwerar 已提交
847
      ((SDatabaseOptions*)pOptions)->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
848
      break;
X
Xiaoyu Wang 已提交
849 850 851
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
wafwerar's avatar
wafwerar 已提交
852
        ((SDatabaseOptions*)pOptions)->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
853 854 855
      } else {
        ((SDatabaseOptions*)pOptions)->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
      }
X
Xiaoyu Wang 已提交
856
      break;
X
Xiaoyu Wang 已提交
857
    }
X
Xiaoyu Wang 已提交
858
    case DB_OPTION_FSYNC:
wafwerar's avatar
wafwerar 已提交
859
      ((SDatabaseOptions*)pOptions)->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
860 861
      break;
    case DB_OPTION_MAXROWS:
wafwerar's avatar
wafwerar 已提交
862
      ((SDatabaseOptions*)pOptions)->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
863 864
      break;
    case DB_OPTION_MINROWS:
wafwerar's avatar
wafwerar 已提交
865
      ((SDatabaseOptions*)pOptions)->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
866 867
      break;
    case DB_OPTION_KEEP:
X
Xiaoyu Wang 已提交
868 869 870
      ((SDatabaseOptions*)pOptions)->pKeep = pVal;
      break;
    case DB_OPTION_PAGES:
wafwerar's avatar
wafwerar 已提交
871
      ((SDatabaseOptions*)pOptions)->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
872 873
      break;
    case DB_OPTION_PAGESIZE:
wafwerar's avatar
wafwerar 已提交
874
      ((SDatabaseOptions*)pOptions)->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
875 876
      break;
    case DB_OPTION_PRECISION:
877
      COPY_STRING_FORM_STR_TOKEN(((SDatabaseOptions*)pOptions)->precisionStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
878 879
      break;
    case DB_OPTION_REPLICA:
wafwerar's avatar
wafwerar 已提交
880
      ((SDatabaseOptions*)pOptions)->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
881
      break;
X
Xiaoyu Wang 已提交
882
    case DB_OPTION_STRICT:
883
      COPY_STRING_FORM_STR_TOKEN(((SDatabaseOptions*)pOptions)->strictStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
884 885
      break;
    case DB_OPTION_WAL:
wafwerar's avatar
wafwerar 已提交
886
      ((SDatabaseOptions*)pOptions)->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
887 888
      break;
    case DB_OPTION_VGROUPS:
wafwerar's avatar
wafwerar 已提交
889
      ((SDatabaseOptions*)pOptions)->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
890 891
      break;
    case DB_OPTION_SINGLE_STABLE:
wafwerar's avatar
wafwerar 已提交
892
      ((SDatabaseOptions*)pOptions)->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
893 894
      break;
    case DB_OPTION_RETENTIONS:
X
Xiaoyu Wang 已提交
895
      ((SDatabaseOptions*)pOptions)->pRetentions = pVal;
X
Xiaoyu Wang 已提交
896
      break;
X
Xiaoyu Wang 已提交
897 898 899 900 901 902 903 904 905 906 907 908
    case DB_OPTION_WAL_RETENTION_PERIOD:
      ((SDatabaseOptions*)pOptions)->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
    case DB_OPTION_WAL_RETENTION_SIZE:
      ((SDatabaseOptions*)pOptions)->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
    case DB_OPTION_WAL_ROLL_PERIOD:
      ((SDatabaseOptions*)pOptions)->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
    case DB_OPTION_WAL_SEGMENT_SIZE:
      ((SDatabaseOptions*)pOptions)->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
X
Xiaoyu Wang 已提交
909 910
    default:
      break;
X
Xiaoyu Wang 已提交
911
  }
X
Xiaoyu Wang 已提交
912
  return pOptions;
X
Xiaoyu Wang 已提交
913 914
}

X
Xiaoyu Wang 已提交
915
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
916
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
917 918 919 920 921 922 923 924 925 926
  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 已提交
927
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
928
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
929
  if (!checkDbName(pCxt, pDbName, false)) {
930 931 932 933
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
934
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
935
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
936
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
937
  return (SNode*)pStmt;
938
}
X
Xiaoyu Wang 已提交
939

X
Xiaoyu Wang 已提交
940
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
941
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
942
  if (!checkDbName(pCxt, pDbName, false)) {
943 944 945 946
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
947
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
948 949 950 951
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
952
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
953
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
954
  if (!checkDbName(pCxt, pDbName, false)) {
955 956
    return NULL;
  }
957
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
958
  CHECK_OUT_OF_MEM(pStmt);
959
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
960 961 962 963
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
964 965 966 967 968
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
969 970 971 972 973 974 975 976 977 978 979 980
  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;
}

SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
  STrimDatabaseStmt* pStmt = (STrimDatabaseStmt*)nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT);
X
Xiaoyu Wang 已提交
981 982 983 984 985
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
986
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
987
  CHECK_PARSER_STATUS(pCxt);
988
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
989
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
990 991 992 993
  pOptions->maxDelay1 = -1;
  pOptions->maxDelay2 = -1;
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
X
Xiaoyu Wang 已提交
994
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
X
Xiaoyu Wang 已提交
995
  pOptions->commentNull = true;  // mark null
996
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
997 998
}

X
Xiaoyu Wang 已提交
999
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1000
  CHECK_PARSER_STATUS(pCxt);
1001
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
1002 1003
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->ttl = -1;
X
Xiaoyu Wang 已提交
1004
  pOptions->commentNull = true;  // mark null
X
Xiaoyu Wang 已提交
1005 1006 1007 1008
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
1009
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1010
  switch (type) {
X
Xiaoyu Wang 已提交
1011
    case TABLE_OPTION_COMMENT:
1012
      if (checkComment(pCxt, (SToken*)pVal, true)) {
wmmhello's avatar
wmmhello 已提交
1013
        ((STableOptions*)pOptions)->commentNull = false;
1014
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
1015
      }
X
Xiaoyu Wang 已提交
1016
      break;
X
Xiaoyu Wang 已提交
1017 1018 1019 1020 1021
    case TABLE_OPTION_MAXDELAY:
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
      break;
    case TABLE_OPTION_WATERMARK:
      ((STableOptions*)pOptions)->pWatermark = pVal;
X
Xiaoyu Wang 已提交
1022
      break;
X
Xiaoyu Wang 已提交
1023 1024 1025
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
X
Xiaoyu Wang 已提交
1026
    case TABLE_OPTION_TTL: {
1027
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1028
      if (ttl > INT32_MAX) {
1029 1030 1031 1032
        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 已提交
1033
      break;
1034
    }
X
Xiaoyu Wang 已提交
1035 1036
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
1037 1038 1039
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
1040
  }
X
Xiaoyu Wang 已提交
1041
  return pOptions;
X
Xiaoyu Wang 已提交
1042 1043
}

X
Xiaoyu Wang 已提交
1044
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
1045
  CHECK_PARSER_STATUS(pCxt);
1046
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
1047 1048
    return NULL;
  }
X
Xiaoyu Wang 已提交
1049 1050
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
1051
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
X
Xiaoyu Wang 已提交
1052 1053
  pCol->dataType = dataType;
  if (NULL != pComment) {
1054
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
1055
  }
1056
  pCol->sma = true;
X
Xiaoyu Wang 已提交
1057 1058 1059
  return (SNode*)pCol;
}

1060
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
1061
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
1062 1063 1064 1065
  return dt;
}

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

X
Xiaoyu Wang 已提交
1070 1071
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1072
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1073 1074
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1075 1076
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1077 1078
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
1079
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
1080
  pStmt->pOptions = (STableOptions*)pOptions;
1081
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1082 1083
  return (SNode*)pStmt;
}
1084

X
Xiaoyu Wang 已提交
1085
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
1086
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1087
  CHECK_PARSER_STATUS(pCxt);
1088
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
1089
  CHECK_OUT_OF_MEM(pStmt);
1090 1091 1092 1093
  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);
1094 1095 1096
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
wmmhello's avatar
wmmhello 已提交
1097
  pStmt->pOptions = (STableOptions*)pOptions;
1098 1099
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
1100 1101 1102 1103
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
1104
  CHECK_PARSER_STATUS(pCxt);
1105
  SCreateMultiTableStmt* pStmt = (SCreateMultiTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLE_STMT);
1106 1107 1108 1109 1110
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

1111
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1112
  CHECK_PARSER_STATUS(pCxt);
1113
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
1114 1115 1116 1117 1118 1119 1120 1121 1122
  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 已提交
1123
  CHECK_PARSER_STATUS(pCxt);
1124
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
1125 1126 1127 1128 1129
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

1130
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1131
  CHECK_PARSER_STATUS(pCxt);
1132
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
1133 1134 1135 1136 1137 1138 1139 1140
  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 已提交
1141 1142 1143 1144 1145 1146 1147
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 已提交
1148
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1149
  CHECK_PARSER_STATUS(pCxt);
1150
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1151 1152 1153
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1154
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1155 1156
}

X
Xiaoyu Wang 已提交
1157 1158
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
                                    SDataType dataType) {
X
Xiaoyu Wang 已提交
1159 1160
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1161 1162
    return NULL;
  }
1163
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1164 1165
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1166
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1167
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
1168
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1169 1170
}

X
Xiaoyu Wang 已提交
1171
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1172 1173
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1174 1175
    return NULL;
  }
1176
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1177 1178
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1179
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
X
Xiaoyu Wang 已提交
1180
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1181 1182
}

X
Xiaoyu Wang 已提交
1183 1184
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1185 1186
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1187 1188
    return NULL;
  }
1189
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1190 1191
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1192 1193
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
X
Xiaoyu Wang 已提交
1194
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1195 1196
}

X
Xiaoyu Wang 已提交
1197
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1198 1199
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
X
bugfix  
Xiaoyu Wang 已提交
1200 1201
    return NULL;
  }
1202
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1203 1204
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
1205
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
1206
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1207
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1208 1209
}

1210 1211 1212 1213 1214
SNode* setAlterSuperTableType(SNode* pStmt) {
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
  return pStmt;
}

1215
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1216
  CHECK_PARSER_STATUS(pCxt);
1217 1218 1219
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1220 1221
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1222
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1223 1224 1225
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1226
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1227 1228
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
         QUERY_NODE_SHOW_VGROUPS_STMT == type;
X
Xiaoyu Wang 已提交
1229 1230
}

1231 1232 1233 1234 1235 1236 1237 1238 1239
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 已提交
1240
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1241 1242
  if (needDbShowStmt(type) && NULL == pDbName && NULL == pCxt->pQueryCxt->db) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "db not specified");
1243
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1244 1245
    return NULL;
  }
1246
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1247
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1248
  pStmt->pDbName = pDbName;
1249 1250
  pStmt->pTbName = pTbName;
  pStmt->tableCondType = tableCondType;
1251
  return (SNode*)pStmt;
1252
}
1253

1254
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1255
  CHECK_PARSER_STATUS(pCxt);
1256 1257 1258 1259
  if (!checkDbName(pCxt, pDbName, true)) {
    return NULL;
  }
  SShowCreateDatabaseStmt* pStmt = (SShowCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
1260
  CHECK_OUT_OF_MEM(pStmt);
1261 1262
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
1263 1264 1265
}

SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1266
  CHECK_PARSER_STATUS(pCxt);
1267
  SShowCreateTableStmt* pStmt = (SShowCreateTableStmt*)nodesMakeNode(type);
1268
  CHECK_OUT_OF_MEM(pStmt);
1269 1270
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
  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);
1282
  return (SNode*)pStmt;
1283 1284
}

1285 1286 1287 1288 1289 1290 1291 1292
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId) {
  CHECK_PARSER_STATUS(pCxt);
  SShowDnodeVariablesStmt* pStmt = (SShowDnodeVariablesStmt*)nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDnodeId = pDnodeId;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1293
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo) {
X
Xiaoyu Wang 已提交
1294
  CHECK_PARSER_STATUS(pCxt);
1295 1296 1297 1298 1299 1300
  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);
1301
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1302
  strcpy(pStmt->password, password);
X
Xiaoyu Wang 已提交
1303
  pStmt->sysinfo = sysinfo;
1304 1305 1306
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1307
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1308
  CHECK_PARSER_STATUS(pCxt);
1309 1310 1311 1312 1313
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1314
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
X
Xiaoyu Wang 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
  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;
1325
    }
X
Xiaoyu Wang 已提交
1326 1327 1328 1329 1330 1331 1332 1333
    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;
1334 1335 1336 1337
  }
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1338
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1339
  CHECK_PARSER_STATUS(pCxt);
1340 1341 1342 1343 1344
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1345
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1346 1347 1348 1349
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1350
  CHECK_PARSER_STATUS(pCxt);
1351 1352
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1353 1354 1355
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
    nodesDestroyNode((SNode*)pStmt);
    return NULL;
1356 1357 1358 1359 1360
  }
  return (SNode*)pStmt;
}

SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode) {
X
Xiaoyu Wang 已提交
1361
  CHECK_PARSER_STATUS(pCxt);
1362 1363 1364
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1365
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1366
  } else {
1367
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
1368
      nodesDestroyNode((SNode*)pStmt);
1369 1370 1371 1372 1373
      return NULL;
    }
  }
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1374

X
Xiaoyu Wang 已提交
1375 1376
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1377
  CHECK_PARSER_STATUS(pCxt);
1378
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1379
  CHECK_OUT_OF_MEM(pStmt);
1380 1381 1382 1383 1384
  if (NULL != pDnode) {
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
  } else {
    pStmt->dnodeId = -1;
  }
1385 1386 1387 1388 1389 1390 1391
  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 已提交
1392 1393
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName,
                             SToken* pTableName, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1394
  CHECK_PARSER_STATUS(pCxt);
1395
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName) || !checkDbName(pCxt, NULL, true)) {
X
Xiaoyu Wang 已提交
1396 1397
    return NULL;
  }
1398
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1399 1400
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1401
  pStmt->ignoreExists = ignoreExists;
1402 1403
  COPY_STRING_FORM_ID_TOKEN(pStmt->indexName, pIndexName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->tableName, pTableName);
X
Xiaoyu Wang 已提交
1404 1405 1406 1407 1408
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

1409 1410
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                         SNode* pStreamOptions) {
X
Xiaoyu Wang 已提交
1411
  CHECK_PARSER_STATUS(pCxt);
1412
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1413 1414 1415 1416 1417
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
1418
  pOptions->pStreamOptions = pStreamOptions;
X
Xiaoyu Wang 已提交
1419 1420
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1421

X
Xiaoyu Wang 已提交
1422
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName) {
X
Xiaoyu Wang 已提交
1423
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1424
  if (!checkDbName(pCxt, NULL, true) || !checkIndexName(pCxt, pIndexName)) {
1425 1426
    return NULL;
  }
1427
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1428
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1429
  pStmt->ignoreNotExists = ignoreNotExists;
1430
  COPY_STRING_FORM_ID_TOKEN(pStmt->indexName, pIndexName);
1431 1432 1433
  return (SNode*)pStmt;
}

1434
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1435
  CHECK_PARSER_STATUS(pCxt);
1436
  SCreateComponentNodeStmt* pStmt = (SCreateComponentNodeStmt*)nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1437
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1438
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
X
Xiaoyu Wang 已提交
1439 1440
  return (SNode*)pStmt;
}
1441

1442
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1443
  CHECK_PARSER_STATUS(pCxt);
1444
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1445
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1446
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1447 1448 1449
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1450 1451
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName,
                                     SNode* pQuery) {
X
Xiaoyu Wang 已提交
1452
  CHECK_PARSER_STATUS(pCxt);
1453
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1454
  CHECK_OUT_OF_MEM(pStmt);
1455
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1456
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  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);
1484 1485 1486 1487
  return (SNode*)pStmt;
}

SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1488
  CHECK_PARSER_STATUS(pCxt);
1489
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1490
  CHECK_OUT_OF_MEM(pStmt);
1491
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1492 1493 1494
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1495

X
Xiaoyu Wang 已提交
1496 1497 1498
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pCGroupId,
                            const SToken* pTopicName) {
  CHECK_PARSER_STATUS(pCxt);
1499
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1500 1501
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
1502 1503
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
X
Xiaoyu Wang 已提交
1504 1505 1506
  return (SNode*)pStmt;
}

1507
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1508
  CHECK_PARSER_STATUS(pCxt);
1509
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1510 1511 1512 1513 1514 1515 1516
  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;
}
1517 1518

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1519
  CHECK_PARSER_STATUS(pCxt);
1520
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1521 1522 1523 1524 1525 1526 1527
  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 已提交
1528
  CHECK_PARSER_STATUS(pCxt);
1529 1530 1531 1532 1533
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1534
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1535
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1536 1537 1538 1539
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1540
  CHECK_PARSER_STATUS(pCxt);
1541
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1542 1543 1544 1545 1546 1547
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1548 1549

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1550
  CHECK_PARSER_STATUS(pCxt);
1551
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1552 1553 1554 1555 1556 1557 1558 1559
  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 已提交
1560
  CHECK_PARSER_STATUS(pCxt);
1561 1562 1563 1564
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1565 1566

SNode* createCompactStmt(SAstCreateContext* pCxt, SNodeList* pVgroups) {
X
Xiaoyu Wang 已提交
1567
  CHECK_PARSER_STATUS(pCxt);
1568 1569 1570 1571 1572
  SNode* pStmt = nodesMakeNode(QUERY_NODE_COMPACT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1573 1574
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
X
Xiaoyu Wang 已提交
1575
  CHECK_PARSER_STATUS(pCxt);
1576
  if (pLibPath->n <= 2) {
1577
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1578 1579
    return NULL;
  }
1580
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1581
  CHECK_OUT_OF_MEM(pStmt);
1582
  pStmt->ignoreExists = ignoreExists;
1583
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1584
  pStmt->isAgg = aggFunc;
1585
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
1586 1587 1588
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
  return (SNode*)pStmt;
1589 1590
}

1591
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1592
  CHECK_PARSER_STATUS(pCxt);
1593
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1594
  CHECK_OUT_OF_MEM(pStmt);
1595
  pStmt->ignoreNotExists = ignoreNotExists;
1596
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1597
  return (SNode*)pStmt;
1598 1599
}

1600
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1601
  CHECK_PARSER_STATUS(pCxt);
1602
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1603 1604 1605 1606 1607
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->triggerType = STREAM_TRIGGER_AT_ONCE;
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1608 1609
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pStreamName, SNode* pRealTable,
                              SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1610
  CHECK_PARSER_STATUS(pCxt);
1611
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1612
  CHECK_OUT_OF_MEM(pStmt);
1613
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1614 1615 1616 1617 1618 1619 1620 1621 1622
  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;
1623 1624
}

1625
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1626
  CHECK_PARSER_STATUS(pCxt);
1627
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1628
  CHECK_OUT_OF_MEM(pStmt);
1629
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1630 1631
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1632 1633 1634
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
1635
  CHECK_PARSER_STATUS(pCxt);
1636
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
1637
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1638
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
1639
  return (SNode*)pStmt;
1640 1641
}

X
Xiaoyu Wang 已提交
1642 1643 1644 1645
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 已提交
1646
  trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
X
Xiaoyu Wang 已提交
1647 1648 1649
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1650 1651
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
1652
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
1653 1654 1655 1656
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

1657
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
1658
  CHECK_PARSER_STATUS(pCxt);
1659
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
1660
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1661 1662 1663
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
1664 1665 1666
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
1667
  CHECK_PARSER_STATUS(pCxt);
1668
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
1669
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1670 1671 1672
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
1673 1674 1675
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
1676
  CHECK_PARSER_STATUS(pCxt);
1677
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
1678
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1679 1680
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
1681 1682 1683
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
1684
  CHECK_PARSER_STATUS(pCxt);
1685 1686 1687 1688
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1689 1690

SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1691
  CHECK_PARSER_STATUS(pCxt);
1692 1693 1694
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1695
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
1696 1697
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1698 1699
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1700 1701 1702 1703
  return (SNode*)pStmt;
}

SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1704
  CHECK_PARSER_STATUS(pCxt);
1705 1706 1707
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1708
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
1709 1710
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1711 1712
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1713 1714
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1715

X
Xiaoyu Wang 已提交
1716
SNode* createCountFuncForDelete(SAstCreateContext* pCxt) {
1717
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
1718 1719 1720
  CHECK_OUT_OF_MEM(pFunc);
  strcpy(pFunc->functionName, "count");
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt))) {
1721
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
1722 1723 1724 1725 1726
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
1727 1728
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
1729
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
1730 1731 1732
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
X
Xiaoyu Wang 已提交
1733 1734
  pStmt->pCountFunc = createCountFuncForDelete(pCxt);
  if (NULL == pStmt->pCountFunc) {
1735
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
1736 1737
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
1738 1739
  return (SNode*)pStmt;
}
1740 1741 1742 1743 1744 1745 1746 1747

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;
1748 1749 1750 1751 1752
  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);
  }
1753 1754
  return (SNode*)pStmt;
}