parAstCreater.c 67.0 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
  }
wafwerar's avatar
wafwerar 已提交
250
  taosMemoryFreeClear(pNode);
X
Xiaoyu Wang 已提交
251
  return pRealizedExpr;
252 253 254
}

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

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

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

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

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

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

X
Xiaoyu Wang 已提交
326
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
327
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
328 329 330 331
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
332 333 334 335 336 337 338 339 340 341 342 343
  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;
}

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

X
Xiaoyu Wang 已提交
366 367 368 369 370 371 372 373 374 375 376
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);
  }
}

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

394 395 396 397 398 399 400 401 402 403 404 405 406
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;
}

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

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
441
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
442 443
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
444 445
}

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

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

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

486
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
487
  CHECK_PARSER_STATUS(pCxt);
488 489 490 491
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
492 493
}

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

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

545
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
546
  CHECK_PARSER_STATUS(pCxt);
547 548 549
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
550 551
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
552
  joinTable->pOnCond = pJoinCond;
553
  return (SNode*)joinTable;
554
}
555

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

567
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
568
  CHECK_PARSER_STATUS(pCxt);
569 570
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
571
  orderByExpr->pExpr = pExpr;
572
  orderByExpr->order = order;
573 574 575
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
576
  orderByExpr->nullOrder = nullOrder;
577 578 579
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
580
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
581
  CHECK_PARSER_STATUS(pCxt);
582 583
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
584 585
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
586 587 588
  return (SNode*)session;
}

589
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
590
  CHECK_PARSER_STATUS(pCxt);
591 592
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
593
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
594
  if (NULL == state->pCol) {
595
    nodesDestroyNode((SNode*)state);
596 597
    CHECK_OUT_OF_MEM(state->pCol);
  }
598
  state->pExpr = pExpr;
599 600 601
  return (SNode*)state;
}

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

634
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
635
  CHECK_PARSER_STATUS(pCxt);
636 637 638 639 640 641 642 643
  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 已提交
644 645
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
  CHECK_PARSER_STATUS(pCxt);
646
  return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
X
Xiaoyu Wang 已提交
647 648
}

X
Xiaoyu Wang 已提交
649
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
X
Xiaoyu Wang 已提交
650
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
651
  trimEscape(pAlias);
X
Xiaoyu Wang 已提交
652 653 654 655 656 657
  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';
658
  return pNode;
659 660
}

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

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

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

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

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

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
X
Xiaoyu Wang 已提交
702
  CHECK_PARSER_STATUS(pCxt);
703 704 705
  if (NULL == pOrderByList) {
    return pStmt;
  }
706
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
707
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
708 709
  } else {
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
710 711 712
  }
  return pStmt;
}
713

714
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
X
Xiaoyu Wang 已提交
715
  CHECK_PARSER_STATUS(pCxt);
716 717 718
  if (NULL == pSlimit) {
    return pStmt;
  }
719
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
720
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
721 722
  }
  return pStmt;
723 724
}

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

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
766
  CHECK_PARSER_STATUS(pCxt);
767
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
768
  CHECK_OUT_OF_MEM(select);
769
  select->isDistinct = isDistinct;
770 771
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
772
  sprintf(select->stmtName, "%p", select);
773
  select->isTimeLineResult = true;
774
  select->onlyHasKeepOrderFunc = true;
775
  select->timeRange = TSWINDOW_INITIALIZER;
776
  return (SNode*)select;
777 778
}

X
Xiaoyu Wang 已提交
779 780 781 782 783 784
static void setSubquery(SNode* pStmt) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    ((SSelectStmt*)pStmt)->isSubquery = true;
  }
}

785
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
786
  CHECK_PARSER_STATUS(pCxt);
787 788 789
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
790
  setOp->pLeft = pLeft;
X
Xiaoyu Wang 已提交
791
  setSubquery(setOp->pLeft);
792
  setOp->pRight = pRight;
X
Xiaoyu Wang 已提交
793
  setSubquery(setOp->pRight);
794
  sprintf(setOp->stmtName, "%p", setOp);
795
  return (SNode*)setOp;
796 797
}

798 799 800 801 802 803 804 805 806 807 808 809 810 811
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 已提交
812
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
813
  CHECK_PARSER_STATUS(pCxt);
814
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
815
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
816
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
817 818
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
X
Xiaoyu Wang 已提交
819 820 821 822 823 824 825 826 827 828
  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;
829
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
X
Xiaoyu Wang 已提交
830 831 832 833 834 835
  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 已提交
836
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
837
  updateWalOptionsDefault(pOptions);
838
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
839
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
840 841
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
X
Xiaoyu Wang 已提交
842
  return (SNode*)pOptions;
843 844
}

X
Xiaoyu Wang 已提交
845
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
846
  CHECK_PARSER_STATUS(pCxt);
847
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
848 849
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
850
  pOptions->cacheModel = -1;
851
  pOptions->cacheLastSize = -1;
X
Xiaoyu Wang 已提交
852 853 854 855 856 857 858 859 860 861
  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;
862
  pOptions->tsdbPageSize = -1;
X
Xiaoyu Wang 已提交
863 864 865 866 867 868
  pOptions->precision = -1;
  pOptions->replica = -1;
  pOptions->strict = -1;
  pOptions->walLevel = -1;
  pOptions->numOfVgroups = -1;
  pOptions->singleStable = -1;
X
Xiaoyu Wang 已提交
869
  pOptions->schemaless = -1;
870 871 872 873
  pOptions->walRetentionPeriod = -1;
  pOptions->walRetentionSize = -1;
  pOptions->walRollPeriod = -1;
  pOptions->walSegmentSize = -1;
874
  pOptions->sstTrigger = -1;
875 876
  pOptions->tablePrefix = -1;
  pOptions->tableSuffix = -1;
X
Xiaoyu Wang 已提交
877 878 879 880
  return (SNode*)pOptions;
}

SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
881
  CHECK_PARSER_STATUS(pCxt);
882
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
X
Xiaoyu Wang 已提交
883 884
  switch (type) {
    case DB_OPTION_BUFFER:
885
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
886
      break;
887
    case DB_OPTION_CACHEMODEL:
888
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
889
      break;
890
    case DB_OPTION_CACHESIZE:
891
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
892 893
      break;
    case DB_OPTION_COMP:
894
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
895
      break;
X
Xiaoyu Wang 已提交
896 897 898
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
899
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
900
      } else {
901
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
X
Xiaoyu Wang 已提交
902
      }
X
Xiaoyu Wang 已提交
903
      break;
X
Xiaoyu Wang 已提交
904
    }
X
Xiaoyu Wang 已提交
905
    case DB_OPTION_FSYNC:
906
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
907 908
      break;
    case DB_OPTION_MAXROWS:
909
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
910 911
      break;
    case DB_OPTION_MINROWS:
912
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
913 914
      break;
    case DB_OPTION_KEEP:
915
      pDbOptions->pKeep = pVal;
X
Xiaoyu Wang 已提交
916 917
      break;
    case DB_OPTION_PAGES:
918
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
919 920
      break;
    case DB_OPTION_PAGESIZE:
921
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
922
      break;
923 924 925
    case DB_OPTION_TSDB_PAGESIZE:
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
X
Xiaoyu Wang 已提交
926
    case DB_OPTION_PRECISION:
927
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
928 929
      break;
    case DB_OPTION_REPLICA:
930 931
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
      updateWalOptionsDefault(pDbOptions);
X
Xiaoyu Wang 已提交
932
      break;
X
Xiaoyu Wang 已提交
933
    case DB_OPTION_STRICT:
934
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
935 936
      break;
    case DB_OPTION_WAL:
937
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
938 939
      break;
    case DB_OPTION_VGROUPS:
940
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
941 942
      break;
    case DB_OPTION_SINGLE_STABLE:
943
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
944 945
      break;
    case DB_OPTION_RETENTIONS:
946
      pDbOptions->pRetentions = pVal;
X
Xiaoyu Wang 已提交
947
      break;
X
Xiaoyu Wang 已提交
948
    case DB_OPTION_WAL_RETENTION_PERIOD:
949 950
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionPeriodIsSet = true;
X
Xiaoyu Wang 已提交
951 952
      break;
    case DB_OPTION_WAL_RETENTION_SIZE:
953 954
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionSizeIsSet = true;
X
Xiaoyu Wang 已提交
955 956
      break;
    case DB_OPTION_WAL_ROLL_PERIOD:
957 958
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRollPeriodIsSet = true;
X
Xiaoyu Wang 已提交
959 960
      break;
    case DB_OPTION_WAL_SEGMENT_SIZE:
961
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
962
      break;
963
    case DB_OPTION_STT_TRIGGER:
964 965
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
966 967 968 969 970 971
    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 已提交
972 973
    default:
      break;
X
Xiaoyu Wang 已提交
974
  }
X
Xiaoyu Wang 已提交
975
  return pOptions;
X
Xiaoyu Wang 已提交
976 977
}

X
Xiaoyu Wang 已提交
978
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
979
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
980 981 982 983 984 985 986 987 988 989
  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 已提交
990
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
991
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
992
  if (!checkDbName(pCxt, pDbName, false)) {
993 994 995 996
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
997
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
998
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
999
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1000
  return (SNode*)pStmt;
1001
}
X
Xiaoyu Wang 已提交
1002

X
Xiaoyu Wang 已提交
1003
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1004
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1005
  if (!checkDbName(pCxt, pDbName, false)) {
1006 1007 1008 1009
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1010
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1011 1012 1013 1014
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1015
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1016
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1017
  if (!checkDbName(pCxt, pDbName, false)) {
1018 1019
    return NULL;
  }
1020
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
1021
  CHECK_OUT_OF_MEM(pStmt);
1022
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1023 1024 1025 1026
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1027 1028 1029 1030 1031
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  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 已提交
1044 1045 1046 1047 1048
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1049
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1050
  CHECK_PARSER_STATUS(pCxt);
1051
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
1052
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
1053 1054 1055 1056
  pOptions->maxDelay1 = -1;
  pOptions->maxDelay2 = -1;
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
X
Xiaoyu Wang 已提交
1057
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
X
Xiaoyu Wang 已提交
1058
  pOptions->commentNull = true;  // mark null
1059
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
1060 1061
}

X
Xiaoyu Wang 已提交
1062
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1063
  CHECK_PARSER_STATUS(pCxt);
1064
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
1065 1066
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->ttl = -1;
X
Xiaoyu Wang 已提交
1067
  pOptions->commentNull = true;  // mark null
X
Xiaoyu Wang 已提交
1068 1069 1070 1071
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
1072
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1073
  switch (type) {
X
Xiaoyu Wang 已提交
1074
    case TABLE_OPTION_COMMENT:
1075
      if (checkComment(pCxt, (SToken*)pVal, true)) {
wmmhello's avatar
wmmhello 已提交
1076
        ((STableOptions*)pOptions)->commentNull = false;
1077
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
1078
      }
X
Xiaoyu Wang 已提交
1079
      break;
X
Xiaoyu Wang 已提交
1080 1081 1082 1083 1084
    case TABLE_OPTION_MAXDELAY:
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
      break;
    case TABLE_OPTION_WATERMARK:
      ((STableOptions*)pOptions)->pWatermark = pVal;
X
Xiaoyu Wang 已提交
1085
      break;
X
Xiaoyu Wang 已提交
1086 1087 1088
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
X
Xiaoyu Wang 已提交
1089
    case TABLE_OPTION_TTL: {
1090
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1091
      if (ttl > INT32_MAX) {
1092 1093 1094 1095
        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 已提交
1096
      break;
1097
    }
X
Xiaoyu Wang 已提交
1098 1099
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
1100 1101 1102
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
1103
  }
X
Xiaoyu Wang 已提交
1104
  return pOptions;
X
Xiaoyu Wang 已提交
1105 1106
}

X
Xiaoyu Wang 已提交
1107
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
1108
  CHECK_PARSER_STATUS(pCxt);
1109
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
1110 1111
    return NULL;
  }
X
Xiaoyu Wang 已提交
1112 1113
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
1114
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
X
Xiaoyu Wang 已提交
1115 1116
  pCol->dataType = dataType;
  if (NULL != pComment) {
1117
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
1118
  }
1119
  pCol->sma = true;
X
Xiaoyu Wang 已提交
1120 1121 1122
  return (SNode*)pCol;
}

1123
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
1124
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
1125 1126 1127 1128
  return dt;
}

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

X
Xiaoyu Wang 已提交
1133 1134
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1135
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1136 1137
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1138 1139
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1140 1141
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
1142
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
1143
  pStmt->pOptions = (STableOptions*)pOptions;
1144
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1145 1146
  return (SNode*)pStmt;
}
1147

X
Xiaoyu Wang 已提交
1148
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
1149
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1150
  CHECK_PARSER_STATUS(pCxt);
1151
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
1152
  CHECK_OUT_OF_MEM(pStmt);
1153 1154 1155 1156
  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);
1157 1158 1159
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
wmmhello's avatar
wmmhello 已提交
1160
  pStmt->pOptions = (STableOptions*)pOptions;
1161 1162
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
1163 1164 1165 1166
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
1167
  CHECK_PARSER_STATUS(pCxt);
1168
  SCreateMultiTableStmt* pStmt = (SCreateMultiTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLE_STMT);
1169 1170 1171 1172 1173
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

1174
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1175
  CHECK_PARSER_STATUS(pCxt);
1176
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
1177 1178 1179 1180 1181 1182 1183 1184 1185
  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 已提交
1186
  CHECK_PARSER_STATUS(pCxt);
1187
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
1188 1189 1190 1191 1192
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

1193
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1194
  CHECK_PARSER_STATUS(pCxt);
1195
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
1196 1197 1198 1199 1200 1201 1202 1203
  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 已提交
1204 1205 1206 1207 1208 1209 1210
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 已提交
1211
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1212
  CHECK_PARSER_STATUS(pCxt);
1213
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1214 1215 1216
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1217
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1218 1219
}

X
Xiaoyu Wang 已提交
1220 1221
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
                                    SDataType dataType) {
X
Xiaoyu Wang 已提交
1222 1223
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1224 1225
    return NULL;
  }
1226
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1227 1228
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1229
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1230
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
1231
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1232 1233
}

X
Xiaoyu Wang 已提交
1234
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1235 1236
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1237 1238
    return NULL;
  }
1239
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1240 1241
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1242
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
X
Xiaoyu Wang 已提交
1243
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1244 1245
}

X
Xiaoyu Wang 已提交
1246 1247
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1248 1249
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
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 1256
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
X
Xiaoyu Wang 已提交
1257
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1258 1259
}

X
Xiaoyu Wang 已提交
1260
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1261 1262
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
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 = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
1268
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
1269
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1270
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1271 1272
}

1273 1274 1275 1276 1277
SNode* setAlterSuperTableType(SNode* pStmt) {
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
  return pStmt;
}

1278
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1279
  CHECK_PARSER_STATUS(pCxt);
1280 1281 1282
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1283 1284
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1285
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1286 1287 1288
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1289
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1290
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
1291 1292
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
         QUERY_NODE_SHOW_TAGS_STMT == type;
X
Xiaoyu Wang 已提交
1293 1294
}

1295 1296 1297 1298 1299 1300 1301 1302 1303
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 已提交
1304
  CHECK_PARSER_STATUS(pCxt);
1305
  if (needDbShowStmt(type) && NULL == pDbName) {
X
Xiaoyu Wang 已提交
1306
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "db not specified");
1307
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1308 1309
    return NULL;
  }
1310
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1311
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1312
  pStmt->pDbName = pDbName;
1313 1314
  pStmt->pTbName = pTbName;
  pStmt->tableCondType = tableCondType;
1315
  return (SNode*)pStmt;
1316
}
1317

1318
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1319
  CHECK_PARSER_STATUS(pCxt);
1320 1321 1322 1323
  if (!checkDbName(pCxt, pDbName, true)) {
    return NULL;
  }
  SShowCreateDatabaseStmt* pStmt = (SShowCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
1324
  CHECK_OUT_OF_MEM(pStmt);
1325 1326
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
1327 1328 1329
}

SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1330
  CHECK_PARSER_STATUS(pCxt);
1331
  SShowCreateTableStmt* pStmt = (SShowCreateTableStmt*)nodesMakeNode(type);
1332
  CHECK_OUT_OF_MEM(pStmt);
1333 1334
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
  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);
1346
  return (SNode*)pStmt;
1347 1348
}

1349 1350 1351 1352 1353 1354 1355 1356
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;
}

1357 1358 1359 1360 1361 1362 1363 1364 1365
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;
}

X
Xiaoyu Wang 已提交
1366
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo) {
X
Xiaoyu Wang 已提交
1367
  CHECK_PARSER_STATUS(pCxt);
1368 1369 1370 1371 1372 1373
  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);
1374
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1375
  strcpy(pStmt->password, password);
X
Xiaoyu Wang 已提交
1376
  pStmt->sysinfo = sysinfo;
1377 1378 1379
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1380
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1381
  CHECK_PARSER_STATUS(pCxt);
1382 1383 1384 1385 1386
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1387
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
X
Xiaoyu Wang 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
  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;
1398
    }
X
Xiaoyu Wang 已提交
1399 1400 1401 1402 1403 1404 1405 1406
    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;
1407 1408 1409 1410
  }
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1411
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1412
  CHECK_PARSER_STATUS(pCxt);
1413 1414 1415 1416 1417
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1418
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1419 1420 1421 1422
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1423
  CHECK_PARSER_STATUS(pCxt);
1424 1425
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1426 1427 1428
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
    nodesDestroyNode((SNode*)pStmt);
    return NULL;
1429 1430 1431 1432 1433
  }
  return (SNode*)pStmt;
}

SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode) {
X
Xiaoyu Wang 已提交
1434
  CHECK_PARSER_STATUS(pCxt);
1435 1436 1437
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1438
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1439
  } else {
1440
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
1441
      nodesDestroyNode((SNode*)pStmt);
1442 1443 1444 1445 1446
      return NULL;
    }
  }
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1447

X
Xiaoyu Wang 已提交
1448 1449
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1450
  CHECK_PARSER_STATUS(pCxt);
1451
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1452
  CHECK_OUT_OF_MEM(pStmt);
1453 1454 1455 1456 1457
  if (NULL != pDnode) {
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
  } else {
    pStmt->dnodeId = -1;
  }
1458 1459 1460 1461 1462 1463 1464
  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 已提交
1465
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
1466
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1467
  CHECK_PARSER_STATUS(pCxt);
1468
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1469 1470
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1471
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1472 1473
  strcpy(pStmt->indexDbName, ((SRealTableNode*)pIndexName)->table.dbName);
  strcpy(pStmt->indexName, ((SRealTableNode*)pIndexName)->table.tableName);
1474 1475
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1476
  nodesDestroyNode(pIndexName);
1477
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1478 1479 1480 1481 1482
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

1483 1484
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                         SNode* pStreamOptions) {
X
Xiaoyu Wang 已提交
1485
  CHECK_PARSER_STATUS(pCxt);
1486
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1487 1488 1489 1490 1491
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
1492
  pOptions->pStreamOptions = pStreamOptions;
X
Xiaoyu Wang 已提交
1493 1494
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1495

X
Xiaoyu Wang 已提交
1496
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
X
Xiaoyu Wang 已提交
1497
  CHECK_PARSER_STATUS(pCxt);
1498
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1499
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1500
  pStmt->ignoreNotExists = ignoreNotExists;
X
Xiaoyu Wang 已提交
1501 1502 1503
  strcpy(pStmt->indexDbName, ((SRealTableNode*)pIndexName)->table.dbName);
  strcpy(pStmt->indexName, ((SRealTableNode*)pIndexName)->table.tableName);
  nodesDestroyNode(pIndexName);
1504 1505 1506
  return (SNode*)pStmt;
}

1507
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1508
  CHECK_PARSER_STATUS(pCxt);
1509
  SCreateComponentNodeStmt* pStmt = (SCreateComponentNodeStmt*)nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1510
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1511
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
X
Xiaoyu Wang 已提交
1512 1513
  return (SNode*)pStmt;
}
1514

1515
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1516
  CHECK_PARSER_STATUS(pCxt);
1517
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1518
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1519
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1520 1521 1522
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1523 1524
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName,
                                     SNode* pQuery) {
X
Xiaoyu Wang 已提交
1525
  CHECK_PARSER_STATUS(pCxt);
1526
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1527
  CHECK_OUT_OF_MEM(pStmt);
1528
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1529
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
  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);
1557 1558 1559 1560
  return (SNode*)pStmt;
}

SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1561
  CHECK_PARSER_STATUS(pCxt);
1562
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1563
  CHECK_OUT_OF_MEM(pStmt);
1564
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1565 1566 1567
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1568

X
Xiaoyu Wang 已提交
1569 1570 1571
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pCGroupId,
                            const SToken* pTopicName) {
  CHECK_PARSER_STATUS(pCxt);
1572
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1573 1574
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
1575 1576
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
X
Xiaoyu Wang 已提交
1577 1578 1579
  return (SNode*)pStmt;
}

1580
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1581
  CHECK_PARSER_STATUS(pCxt);
1582
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1583 1584 1585 1586 1587 1588 1589
  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;
}
1590 1591

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1592
  CHECK_PARSER_STATUS(pCxt);
1593
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1594 1595 1596 1597 1598 1599 1600
  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 已提交
1601
  CHECK_PARSER_STATUS(pCxt);
1602 1603 1604 1605 1606
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1607
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1608
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1609 1610 1611 1612
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1613
  CHECK_PARSER_STATUS(pCxt);
1614
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1615 1616 1617 1618 1619 1620
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1621 1622

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1623
  CHECK_PARSER_STATUS(pCxt);
1624
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1625 1626 1627 1628 1629 1630 1631 1632
  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 已提交
1633
  CHECK_PARSER_STATUS(pCxt);
1634 1635 1636 1637
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1638 1639

SNode* createCompactStmt(SAstCreateContext* pCxt, SNodeList* pVgroups) {
X
Xiaoyu Wang 已提交
1640
  CHECK_PARSER_STATUS(pCxt);
1641 1642 1643 1644 1645
  SNode* pStmt = nodesMakeNode(QUERY_NODE_COMPACT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1646 1647
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
X
Xiaoyu Wang 已提交
1648
  CHECK_PARSER_STATUS(pCxt);
1649
  if (pLibPath->n <= 2) {
1650
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1651 1652
    return NULL;
  }
1653
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1654
  CHECK_OUT_OF_MEM(pStmt);
1655
  pStmt->ignoreExists = ignoreExists;
1656
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1657
  pStmt->isAgg = aggFunc;
1658
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
1659 1660 1661
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
  return (SNode*)pStmt;
1662 1663
}

1664
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1665
  CHECK_PARSER_STATUS(pCxt);
1666
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1667
  CHECK_OUT_OF_MEM(pStmt);
1668
  pStmt->ignoreNotExists = ignoreNotExists;
1669
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1670
  return (SNode*)pStmt;
1671 1672
}

1673
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1674
  CHECK_PARSER_STATUS(pCxt);
1675
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1676 1677
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->triggerType = STREAM_TRIGGER_AT_ONCE;
X
Xiaoyu Wang 已提交
1678
  pOptions->ignoreExpired = STREAM_DEFAULT_IGNORE_EXPIRED;
1679 1680 1681
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1682 1683
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pStreamName, SNode* pRealTable,
                              SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1684
  CHECK_PARSER_STATUS(pCxt);
1685
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1686
  CHECK_OUT_OF_MEM(pStmt);
1687
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1688 1689 1690 1691 1692 1693 1694 1695 1696
  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;
1697 1698
}

1699
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1700
  CHECK_PARSER_STATUS(pCxt);
1701
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1702
  CHECK_OUT_OF_MEM(pStmt);
1703
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1704 1705
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1706 1707 1708
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
1709
  CHECK_PARSER_STATUS(pCxt);
1710
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
1711
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1712
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
1713
  return (SNode*)pStmt;
1714 1715
}

X
Xiaoyu Wang 已提交
1716 1717 1718 1719
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 已提交
1720
  trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
X
Xiaoyu Wang 已提交
1721 1722 1723
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1724 1725
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
1726
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
1727 1728 1729 1730
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

1731
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
1732
  CHECK_PARSER_STATUS(pCxt);
1733
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
1734
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1735 1736 1737
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
1738 1739 1740
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
1741
  CHECK_PARSER_STATUS(pCxt);
1742
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
1743
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1744 1745 1746
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
1747 1748 1749
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
1750
  CHECK_PARSER_STATUS(pCxt);
1751
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
1752
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1753 1754
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
1755 1756 1757
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
1758
  CHECK_PARSER_STATUS(pCxt);
1759 1760 1761 1762
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1763 1764

SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1765
  CHECK_PARSER_STATUS(pCxt);
1766 1767 1768
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1769
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
1770 1771
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1772 1773
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1774 1775 1776 1777
  return (SNode*)pStmt;
}

SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1778
  CHECK_PARSER_STATUS(pCxt);
1779 1780 1781
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1782
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
1783 1784
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1785 1786
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1787 1788
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1789

X
Xiaoyu Wang 已提交
1790
SNode* createCountFuncForDelete(SAstCreateContext* pCxt) {
1791
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
1792 1793
  CHECK_OUT_OF_MEM(pFunc);
  strcpy(pFunc->functionName, "count");
1794
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt, NULL))) {
1795
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
1796 1797 1798 1799 1800
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
1801 1802
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
1803
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
1804 1805 1806
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
X
Xiaoyu Wang 已提交
1807 1808
  pStmt->pCountFunc = createCountFuncForDelete(pCxt);
  if (NULL == pStmt->pCountFunc) {
1809
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
1810 1811
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
1812 1813
  return (SNode*)pStmt;
}
1814 1815 1816 1817 1818 1819 1820 1821

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;
1822 1823 1824 1825 1826
  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);
  }
1827 1828
  return (SNode*)pStmt;
}