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

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

17 18
#include <regex.h>

X
Xiaoyu Wang 已提交
19 20
#include "parAst.h"
#include "parUtil.h"
21
#include "tglobal.h"
X
Xiaoyu Wang 已提交
22
#include "ttime.h"
23

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

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

40 41 42 43 44 45 46 47
#define COPY_STRING_FORM_ID_TOKEN(buf, pToken) strncpy(buf, (pToken)->z, TMIN((pToken)->n, sizeof(buf) - 1))
#define COPY_STRING_FORM_STR_TOKEN(buf, pToken)                              \
  do {                                                                       \
    if ((pToken)->n > 2) {                                                   \
      strncpy(buf, (pToken)->z + 1, TMIN((pToken)->n - 2, sizeof(buf) - 1)); \
    }                                                                        \
  } while (0)

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

50
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
51
  memset(pCxt, 0, sizeof(SAstCreateContext));
52
  pCxt->pQueryCxt = pParseCxt;
53 54
  pCxt->msgBuf.buf = pParseCxt->pMsg;
  pCxt->msgBuf.len = pParseCxt->msgLen;
55 56
  pCxt->notSupport = false;
  pCxt->pRootNode = NULL;
D
dapan1121 已提交
57
  pCxt->placeholderNo = 0;
X
Xiaoyu Wang 已提交
58
  pCxt->pPlaceholderValues = NULL;
59
  pCxt->errCode = TSDB_CODE_SUCCESS;
60 61
}

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

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

84 85 86 87 88 89 90 91 92 93 94 95 96
static bool invalidPassword(const char* pPassword) {
  regex_t regex;

  if (regcomp(&regex, "[ '\"`\\]", REG_EXTENDED | REG_ICASE) != 0) {
    return false;
  }

  /* Execute regular expression */
  int32_t res = regexec(&regex, pPassword, 0, NULL, 0);
  regfree(&regex);
  return 0 == res;
}

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

114 115 116 117
static int32_t parsePort(SAstCreateContext* pCxt, const char* p, int32_t* pPort) {
  *pPort = taosStr2Int32(p, NULL, 10);
  if (*pPort >= UINT16_MAX || *pPort <= 0) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
118
  }
119
  return TSDB_CODE_SUCCESS;
120 121
}

122 123 124
static int32_t parseEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
  if (pEp->n >= (NULL == pPort ? (TSDB_FQDN_LEN + 1 + 5) : TSDB_FQDN_LEN)) {  // format 'fqdn:port' or 'fqdn'
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
125
  }
126

127
  char ep[TSDB_FQDN_LEN + 1 + 5] = {0};
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  COPY_STRING_FORM_ID_TOKEN(ep, pEp);
  strdequote(ep);
  strtrim(ep);
  if (NULL == pPort) {
    strcpy(pFqdn, ep);
    return TSDB_CODE_SUCCESS;
  }
  char* pColon = strchr(ep, ':');
  if (NULL == pColon) {
    *pPort = tsServerPort;
    strcpy(pFqdn, ep);
    return TSDB_CODE_SUCCESS;
  }
  strncpy(pFqdn, ep, pColon - ep);
  return parsePort(pCxt, pColon + 1, pPort);
143 144
}

145 146 147
static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, const SToken* pPortToken, char* pFqdn,
                                  int32_t* pPort) {
  if (NULL == pEp) {
148
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
149 150 151 152 153
    return false;
  }

  if (NULL != pPortToken) {
    pCxt->errCode = parsePort(pCxt, pPortToken->z, pPort);
154
  }
155 156 157 158 159

  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
    pCxt->errCode = parseEndpoint(pCxt, pEp, pFqdn, (NULL != pPortToken ? NULL : pPort));
  }

160
  return TSDB_CODE_SUCCESS == pCxt->errCode;
161 162
}

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

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

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

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

204 205 206 207 208 209 210 211 212
static bool checkComment(SAstCreateContext* pCxt, const SToken* pCommentToken, bool demand) {
  if (NULL == pCommentToken) {
    pCxt->errCode = demand ? TSDB_CODE_PAR_SYNTAX_ERROR : TSDB_CODE_SUCCESS;
  } else if (pCommentToken->n >= (TSDB_TB_COMMENT_LEN + 2)) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_COMMENT_TOO_LONG);
  }
  return TSDB_CODE_SUCCESS == pCxt->errCode;
}

213
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
X
Xiaoyu Wang 已提交
214
  CHECK_PARSER_STATUS(pCxt);
215 216 217 218 219 220 221 222 223
  SRawExprNode* target = (SRawExprNode*)nodesMakeNode(QUERY_NODE_RAW_EXPR);
  CHECK_OUT_OF_MEM(target);
  target->p = pToken->z;
  target->n = pToken->n;
  target->pNode = pNode;
  return (SNode*)target;
}

SNode* createRawExprNodeExt(SAstCreateContext* pCxt, const SToken* pStart, const SToken* pEnd, SNode* pNode) {
X
Xiaoyu Wang 已提交
224
  CHECK_PARSER_STATUS(pCxt);
225 226 227 228 229 230 231 232 233
  SRawExprNode* target = (SRawExprNode*)nodesMakeNode(QUERY_NODE_RAW_EXPR);
  CHECK_OUT_OF_MEM(target);
  target->p = pStart->z;
  target->n = (pEnd->z + pEnd->n) - pStart->z;
  target->pNode = pNode;
  return (SNode*)target;
}

SNode* releaseRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
234
  CHECK_PARSER_STATUS(pCxt);
235
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
236
  SNode*        pExpr = pRawExpr->pNode;
D
dapan1121 已提交
237
  if (nodesIsExprNode(pExpr)) {
X
Xiaoyu Wang 已提交
238 239 240 241 242 243 244
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
      strcpy(((SExprNode*)pExpr)->aliasName, ((SColumnNode*)pExpr)->colName);
    } else {
      int32_t len = TMIN(sizeof(((SExprNode*)pExpr)->aliasName) - 1, pRawExpr->n);
      strncpy(((SExprNode*)pExpr)->aliasName, pRawExpr->p, len);
      ((SExprNode*)pExpr)->aliasName[len] = '\0';
    }
D
dapan1121 已提交
245
  }
wafwerar's avatar
wafwerar 已提交
246
  taosMemoryFreeClear(pNode);
247
  return pExpr;
248 249 250
}

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

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

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

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

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

SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
309
  CHECK_PARSER_STATUS(pCxt);
310 311
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
312 313 314
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
315
  val->translate = false;
316 317 318
  val->node.resType.type = TSDB_DATA_TYPE_BIGINT;
  val->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
319
  return (SNode*)val;
320 321
}

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

X
Xiaoyu Wang 已提交
328 329 330 331 332 333 334 335 336 337 338 339
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
  val->literal = strdup(pCxt->pQueryCxt->db);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = false;
  val->translate = false;
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
  val->node.resType.bytes = strlen(val->literal);
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  return (SNode*)val;
}

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

X
Xiaoyu Wang 已提交
358 359 360 361 362 363 364 365 366 367 368
static int32_t addParamToLogicConditionNode(SLogicConditionNode* pCond, SNode* pParam) {
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam) && pCond->condType == ((SLogicConditionNode*)pParam)->condType) {
    int32_t code = nodesListAppendList(pCond->pParameterList, ((SLogicConditionNode*)pParam)->pParameterList);
    ((SLogicConditionNode*)pParam)->pParameterList = NULL;
    nodesDestroyNode(pParam);
    return code;
  } else {
    return nodesListAppend(pCond->pParameterList, pParam);
  }
}

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

SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
387
  CHECK_PARSER_STATUS(pCxt);
388 389
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
    SValueNode* pVal = (SValueNode*)pLeft;
390
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
391 392 393 394 395 396
    CHECK_OUT_OF_MEM(pNewLiteral);
    sprintf(pNewLiteral, "-%s", pVal->literal);
    taosMemoryFree(pVal->literal);
    pVal->literal = pNewLiteral;
    return pLeft;
  }
397 398 399 400 401 402 403 404 405
  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 已提交
406
  CHECK_PARSER_STATUS(pCxt);
407
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
408 409
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
410 411 412
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
413
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
414 415
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
416 417
}

X
Xiaoyu Wang 已提交
418
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
419
  CHECK_PARSER_STATUS(pCxt);
420
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
421 422 423 424 425 426
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(pCol->colName, PK_TS_COL_INTERNAL_NAME);
  return (SNode*)pCol;
}

427
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
428
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
429 430 431
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
    return createPrimaryKeyCol(pCxt);
  }
432 433
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
434
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
435 436 437 438
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

439
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
X
Xiaoyu Wang 已提交
440
  CHECK_PARSER_STATUS(pCxt);
441 442 443 444
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
445 446 447 448
  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;
449
  }
450 451 452 453
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

454
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
455
  CHECK_PARSER_STATUS(pCxt);
456 457 458 459
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
460 461
}

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

494
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
495
  CHECK_PARSER_STATUS(pCxt);
496 497 498
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
499
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
500
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
X
Xiaoyu Wang 已提交
501 502 503 504 505
  } else {
    sprintf(tempTable->table.tableAlias, "%p", tempTable);
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
506 507
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
508
  }
509
  return (SNode*)tempTable;
510 511
}

512
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
513
  CHECK_PARSER_STATUS(pCxt);
514 515 516
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
517 518
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
519
  joinTable->pOnCond = pJoinCond;
520
  return (SNode*)joinTable;
521
}
522

523
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
X
Xiaoyu Wang 已提交
524
  CHECK_PARSER_STATUS(pCxt);
525 526
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
wafwerar's avatar
wafwerar 已提交
527
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
528
  if (NULL != pOffset) {
wafwerar's avatar
wafwerar 已提交
529
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
530
  }
531
  return (SNode*)limitNode;
532 533
}

534
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
535
  CHECK_PARSER_STATUS(pCxt);
536 537
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
538
  orderByExpr->pExpr = pExpr;
539
  orderByExpr->order = order;
540 541 542
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
543
  orderByExpr->nullOrder = nullOrder;
544 545 546
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
547
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
548
  CHECK_PARSER_STATUS(pCxt);
549 550
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
551 552
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
553 554 555
  return (SNode*)session;
}

556
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
557
  CHECK_PARSER_STATUS(pCxt);
558 559
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
X
Xiaoyu Wang 已提交
560
  state->pCol = createPrimaryKeyCol(pCxt);
561
  if (NULL == state->pCol) {
562
    nodesDestroyNode((SNode*)state);
563 564
    CHECK_OUT_OF_MEM(state->pCol);
  }
565
  state->pExpr = pExpr;
566 567 568
  return (SNode*)state;
}

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

601
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
602
  CHECK_PARSER_STATUS(pCxt);
603 604 605 606 607 608 609 610
  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 已提交
611 612 613 614 615
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
  CHECK_PARSER_STATUS(pCxt);
  return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt), pStart, pEnd);
}

616
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) {
X
Xiaoyu Wang 已提交
617
  CHECK_PARSER_STATUS(pCxt);
D
dapan1121 已提交
618 619 620
  int32_t len = TMIN(sizeof(((SExprNode*)pNode)->aliasName) - 1, pAlias->n);
  strncpy(((SExprNode*)pNode)->aliasName, pAlias->z, len);
  ((SExprNode*)pNode)->aliasName[len] = '\0';
621 622
  strncpy(((SExprNode*)pNode)->userAlias, pAlias->z, len);
  ((SExprNode*)pNode)->userAlias[len] = '\0';
623
  return pNode;
624 625
}

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

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

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

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

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

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

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

684
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
X
Xiaoyu Wang 已提交
685
  CHECK_PARSER_STATUS(pCxt);
686
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
687
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
X
Xiaoyu Wang 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
  } 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 已提交
712 713 714 715 716
  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;
717
  }
718
  return pStmt;
719 720 721
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
722
  CHECK_PARSER_STATUS(pCxt);
723
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
724
  CHECK_OUT_OF_MEM(select);
725
  select->isDistinct = isDistinct;
726 727
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
728
  sprintf(select->stmtName, "%p", select);
729
  select->isTimeLineResult = true;
730
  return (SNode*)select;
731 732 733
}

SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
734
  CHECK_PARSER_STATUS(pCxt);
735 736 737
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
738 739
  setOp->pLeft = pLeft;
  setOp->pRight = pRight;
740
  sprintf(setOp->stmtName, "%p", setOp);
741
  return (SNode*)setOp;
742 743
}

X
Xiaoyu Wang 已提交
744
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
745
  CHECK_PARSER_STATUS(pCxt);
746
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
747
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
  pOptions->cachelast = TSDB_DEFAULT_CACHE_LAST_ROW;
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
  pOptions->precision = TSDB_DEFAULT_PRECISION;
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
X
Xiaoyu Wang 已提交
766
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
X
Xiaoyu Wang 已提交
767
  return (SNode*)pOptions;
768 769
}

X
Xiaoyu Wang 已提交
770
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
771
  CHECK_PARSER_STATUS(pCxt);
772
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
  pOptions->cachelast = -1;
  pOptions->compressionLevel = -1;
  pOptions->daysPerFile = -1;
  pOptions->fsyncPeriod = -1;
  pOptions->maxRowsPerBlock = -1;
  pOptions->minRowsPerBlock = -1;
  pOptions->keep[0] = -1;
  pOptions->keep[1] = -1;
  pOptions->keep[2] = -1;
  pOptions->pages = -1;
  pOptions->pagesize = -1;
  pOptions->precision = -1;
  pOptions->replica = -1;
  pOptions->strict = -1;
  pOptions->walLevel = -1;
  pOptions->numOfVgroups = -1;
  pOptions->singleStable = -1;
X
Xiaoyu Wang 已提交
792
  pOptions->schemaless = -1;
X
Xiaoyu Wang 已提交
793 794 795 796
  return (SNode*)pOptions;
}

SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
797
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
798 799
  switch (type) {
    case DB_OPTION_BUFFER:
wafwerar's avatar
wafwerar 已提交
800
      ((SDatabaseOptions*)pOptions)->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
801 802
      break;
    case DB_OPTION_CACHELAST:
wafwerar's avatar
wafwerar 已提交
803
      ((SDatabaseOptions*)pOptions)->cachelast = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
804 805
      break;
    case DB_OPTION_COMP:
wafwerar's avatar
wafwerar 已提交
806
      ((SDatabaseOptions*)pOptions)->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
807
      break;
X
Xiaoyu Wang 已提交
808 809 810
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
wafwerar's avatar
wafwerar 已提交
811
        ((SDatabaseOptions*)pOptions)->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
812 813 814
      } else {
        ((SDatabaseOptions*)pOptions)->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
      }
X
Xiaoyu Wang 已提交
815
      break;
X
Xiaoyu Wang 已提交
816
    }
X
Xiaoyu Wang 已提交
817
    case DB_OPTION_FSYNC:
wafwerar's avatar
wafwerar 已提交
818
      ((SDatabaseOptions*)pOptions)->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
819 820
      break;
    case DB_OPTION_MAXROWS:
wafwerar's avatar
wafwerar 已提交
821
      ((SDatabaseOptions*)pOptions)->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
822 823
      break;
    case DB_OPTION_MINROWS:
wafwerar's avatar
wafwerar 已提交
824
      ((SDatabaseOptions*)pOptions)->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
825 826
      break;
    case DB_OPTION_KEEP:
X
Xiaoyu Wang 已提交
827 828 829
      ((SDatabaseOptions*)pOptions)->pKeep = pVal;
      break;
    case DB_OPTION_PAGES:
wafwerar's avatar
wafwerar 已提交
830
      ((SDatabaseOptions*)pOptions)->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
831 832
      break;
    case DB_OPTION_PAGESIZE:
wafwerar's avatar
wafwerar 已提交
833
      ((SDatabaseOptions*)pOptions)->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
834 835
      break;
    case DB_OPTION_PRECISION:
836
      COPY_STRING_FORM_STR_TOKEN(((SDatabaseOptions*)pOptions)->precisionStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
837 838
      break;
    case DB_OPTION_REPLICA:
wafwerar's avatar
wafwerar 已提交
839
      ((SDatabaseOptions*)pOptions)->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
840
      break;
X
Xiaoyu Wang 已提交
841
    case DB_OPTION_STRICT:
wafwerar's avatar
wafwerar 已提交
842
      ((SDatabaseOptions*)pOptions)->strict = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
843 844
      break;
    case DB_OPTION_WAL:
wafwerar's avatar
wafwerar 已提交
845
      ((SDatabaseOptions*)pOptions)->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
846 847
      break;
    case DB_OPTION_VGROUPS:
wafwerar's avatar
wafwerar 已提交
848
      ((SDatabaseOptions*)pOptions)->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
849 850
      break;
    case DB_OPTION_SINGLE_STABLE:
wafwerar's avatar
wafwerar 已提交
851
      ((SDatabaseOptions*)pOptions)->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
852 853
      break;
    case DB_OPTION_RETENTIONS:
X
Xiaoyu Wang 已提交
854
      ((SDatabaseOptions*)pOptions)->pRetentions = pVal;
X
Xiaoyu Wang 已提交
855
      break;
X
Xiaoyu Wang 已提交
856 857 858 859
      //    case DB_OPTION_SCHEMALESS:
      //      ((SDatabaseOptions*)pOptions)->schemaless = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
      //      ((SDatabaseOptions*)pOptions)->schemaless = 0;
      //      break;
X
Xiaoyu Wang 已提交
860 861
    default:
      break;
X
Xiaoyu Wang 已提交
862
  }
X
Xiaoyu Wang 已提交
863
  return pOptions;
X
Xiaoyu Wang 已提交
864 865
}

X
Xiaoyu Wang 已提交
866
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
867
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
868 869 870 871 872 873 874 875 876 877
  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 已提交
878
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
879
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
880
  if (!checkDbName(pCxt, pDbName, false)) {
881 882 883 884
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
885
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
886
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
887
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
888
  return (SNode*)pStmt;
889
}
X
Xiaoyu Wang 已提交
890

X
Xiaoyu Wang 已提交
891
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
892
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
893
  if (!checkDbName(pCxt, pDbName, false)) {
894 895 896 897
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
898
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
899 900 901 902
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
903
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
904
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
905
  if (!checkDbName(pCxt, pDbName, false)) {
906 907
    return NULL;
  }
908
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
909
  CHECK_OUT_OF_MEM(pStmt);
910
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
911 912 913 914
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
915 916 917 918 919 920 921 922 923 924 925
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_FLUSH_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
926
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
927
  CHECK_PARSER_STATUS(pCxt);
928
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
929
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
930 931 932 933
  pOptions->maxDelay1 = -1;
  pOptions->maxDelay2 = -1;
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
X
Xiaoyu Wang 已提交
934
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
X
Xiaoyu Wang 已提交
935
  pOptions->commentNull = true;  // mark null
936
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
937 938
}

X
Xiaoyu Wang 已提交
939
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
940
  CHECK_PARSER_STATUS(pCxt);
941
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
942 943
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->ttl = -1;
X
Xiaoyu Wang 已提交
944
  pOptions->commentNull = true;  // mark null
X
Xiaoyu Wang 已提交
945 946 947 948
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
949
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
950
  switch (type) {
X
Xiaoyu Wang 已提交
951
    case TABLE_OPTION_COMMENT:
952
      if (checkComment(pCxt, (SToken*)pVal, true)) {
wmmhello's avatar
wmmhello 已提交
953
        ((STableOptions*)pOptions)->commentNull = false;
954
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
955
      }
X
Xiaoyu Wang 已提交
956
      break;
X
Xiaoyu Wang 已提交
957 958 959 960 961
    case TABLE_OPTION_MAXDELAY:
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
      break;
    case TABLE_OPTION_WATERMARK:
      ((STableOptions*)pOptions)->pWatermark = pVal;
X
Xiaoyu Wang 已提交
962
      break;
X
Xiaoyu Wang 已提交
963 964 965
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
X
Xiaoyu Wang 已提交
966
    case TABLE_OPTION_TTL: {
967
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
968
      if (ttl > INT32_MAX) {
969 970 971 972
        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 已提交
973
      break;
974
    }
X
Xiaoyu Wang 已提交
975 976
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
977 978 979
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
980
  }
X
Xiaoyu Wang 已提交
981
  return pOptions;
X
Xiaoyu Wang 已提交
982 983
}

X
Xiaoyu Wang 已提交
984
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
985
  CHECK_PARSER_STATUS(pCxt);
986
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
987 988
    return NULL;
  }
X
Xiaoyu Wang 已提交
989 990
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
991
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
X
Xiaoyu Wang 已提交
992 993
  pCol->dataType = dataType;
  if (NULL != pComment) {
994
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
995
  }
996
  pCol->sma = true;
X
Xiaoyu Wang 已提交
997 998 999
  return (SNode*)pCol;
}

1000
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
1001
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
1002 1003 1004 1005
  return dt;
}

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

X
Xiaoyu Wang 已提交
1010 1011
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1012
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1013 1014
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1015 1016
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1017 1018
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
1019
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
1020
  pStmt->pOptions = (STableOptions*)pOptions;
1021
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1022 1023
  return (SNode*)pStmt;
}
1024

X
Xiaoyu Wang 已提交
1025
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
1026
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1027
  CHECK_PARSER_STATUS(pCxt);
1028
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
1029
  CHECK_OUT_OF_MEM(pStmt);
1030 1031 1032 1033
  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);
1034 1035 1036
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
wmmhello's avatar
wmmhello 已提交
1037
  pStmt->pOptions = (STableOptions*)pOptions;
1038 1039
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
1040 1041 1042 1043
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
1044
  CHECK_PARSER_STATUS(pCxt);
1045
  SCreateMultiTableStmt* pStmt = (SCreateMultiTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLE_STMT);
1046 1047 1048 1049 1050
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

1051
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1052
  CHECK_PARSER_STATUS(pCxt);
1053
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
1054 1055 1056 1057 1058 1059 1060 1061 1062
  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 已提交
1063
  CHECK_PARSER_STATUS(pCxt);
1064
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
1065 1066 1067 1068 1069
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

1070
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1071
  CHECK_PARSER_STATUS(pCxt);
1072
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
1073 1074 1075 1076 1077 1078 1079 1080
  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 已提交
1081 1082 1083 1084 1085 1086 1087
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 已提交
1088
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1089
  CHECK_PARSER_STATUS(pCxt);
1090
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1091 1092 1093
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1094
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1095 1096
}

X
Xiaoyu Wang 已提交
1097 1098
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
                                    SDataType dataType) {
X
Xiaoyu Wang 已提交
1099 1100
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1101 1102
    return NULL;
  }
1103
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1104 1105
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1106
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1107
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
1108
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1109 1110
}

X
Xiaoyu Wang 已提交
1111
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1112 1113
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1114 1115
    return NULL;
  }
1116
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1117 1118
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1119
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
X
Xiaoyu Wang 已提交
1120
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1121 1122
}

X
Xiaoyu Wang 已提交
1123 1124
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1125 1126
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1127 1128
    return NULL;
  }
1129
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1130 1131
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1132 1133
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
X
Xiaoyu Wang 已提交
1134
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1135 1136
}

X
Xiaoyu Wang 已提交
1137
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1138 1139
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
X
bugfix  
Xiaoyu Wang 已提交
1140 1141
    return NULL;
  }
1142
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1143 1144
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
1145
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
1146
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1147
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1148 1149
}

1150 1151 1152 1153 1154
SNode* setAlterSuperTableType(SNode* pStmt) {
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
  return pStmt;
}

1155
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1156
  CHECK_PARSER_STATUS(pCxt);
1157 1158 1159
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1160 1161
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1162
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1163 1164 1165
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1166
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1167 1168
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
         QUERY_NODE_SHOW_VGROUPS_STMT == type;
X
Xiaoyu Wang 已提交
1169 1170
}

1171 1172 1173 1174 1175 1176 1177 1178 1179
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 已提交
1180
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1181 1182
  if (needDbShowStmt(type) && NULL == pDbName && NULL == pCxt->pQueryCxt->db) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "db not specified");
1183
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1184 1185
    return NULL;
  }
1186
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1187
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1188
  pStmt->pDbName = pDbName;
1189 1190
  pStmt->pTbName = pTbName;
  pStmt->tableCondType = tableCondType;
1191
  return (SNode*)pStmt;
1192
}
1193

1194
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1195
  CHECK_PARSER_STATUS(pCxt);
1196 1197 1198 1199
  if (!checkDbName(pCxt, pDbName, true)) {
    return NULL;
  }
  SShowCreateDatabaseStmt* pStmt = (SShowCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
1200
  CHECK_OUT_OF_MEM(pStmt);
1201 1202
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
1203 1204 1205
}

SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1206
  CHECK_PARSER_STATUS(pCxt);
1207
  SShowCreateTableStmt* pStmt = (SShowCreateTableStmt*)nodesMakeNode(type);
1208
  CHECK_OUT_OF_MEM(pStmt);
1209 1210
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
  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);
1222
  return (SNode*)pStmt;
1223 1224
}

1225 1226 1227 1228 1229 1230 1231 1232
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 已提交
1233
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo) {
X
Xiaoyu Wang 已提交
1234
  CHECK_PARSER_STATUS(pCxt);
1235 1236 1237 1238 1239 1240
  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);
1241
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1242
  strcpy(pStmt->password, password);
X
Xiaoyu Wang 已提交
1243
  pStmt->sysinfo = sysinfo;
1244 1245 1246
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1247
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1248
  CHECK_PARSER_STATUS(pCxt);
1249 1250 1251 1252 1253
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1254
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
X
Xiaoyu Wang 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
  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;
1265
    }
X
Xiaoyu Wang 已提交
1266 1267 1268 1269 1270 1271 1272 1273
    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;
1274 1275 1276 1277
  }
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1278
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1279
  CHECK_PARSER_STATUS(pCxt);
1280 1281 1282 1283 1284
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1285
  COPY_STRING_FORM_ID_TOKEN(pStmt->useName, pUserName);
1286 1287 1288 1289
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1290
  CHECK_PARSER_STATUS(pCxt);
1291 1292
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1293 1294 1295
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
    nodesDestroyNode((SNode*)pStmt);
    return NULL;
1296 1297 1298 1299 1300
  }
  return (SNode*)pStmt;
}

SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode) {
X
Xiaoyu Wang 已提交
1301
  CHECK_PARSER_STATUS(pCxt);
1302 1303 1304
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1305
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1306
  } else {
1307
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
1308
      nodesDestroyNode((SNode*)pStmt);
1309 1310 1311 1312 1313
      return NULL;
    }
  }
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1314

X
Xiaoyu Wang 已提交
1315 1316
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1317
  CHECK_PARSER_STATUS(pCxt);
1318
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1319
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1320
  pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1321 1322 1323 1324 1325 1326 1327
  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 已提交
1328 1329
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName,
                             SToken* pTableName, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1330
  CHECK_PARSER_STATUS(pCxt);
1331
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName) || !checkDbName(pCxt, NULL, true)) {
X
Xiaoyu Wang 已提交
1332 1333
    return NULL;
  }
1334
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1335 1336
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1337
  pStmt->ignoreExists = ignoreExists;
1338 1339
  COPY_STRING_FORM_ID_TOKEN(pStmt->indexName, pIndexName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->tableName, pTableName);
X
Xiaoyu Wang 已提交
1340 1341 1342 1343 1344
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

1345 1346
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                         SNode* pStreamOptions) {
X
Xiaoyu Wang 已提交
1347
  CHECK_PARSER_STATUS(pCxt);
1348
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1349 1350 1351 1352 1353
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
1354
  pOptions->pStreamOptions = pStreamOptions;
X
Xiaoyu Wang 已提交
1355 1356
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1357

X
Xiaoyu Wang 已提交
1358
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName) {
X
Xiaoyu Wang 已提交
1359
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1360
  if (!checkDbName(pCxt, NULL, true) || !checkIndexName(pCxt, pIndexName)) {
1361 1362
    return NULL;
  }
1363
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1364
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1365
  pStmt->ignoreNotExists = ignoreNotExists;
1366
  COPY_STRING_FORM_ID_TOKEN(pStmt->indexName, pIndexName);
1367 1368 1369
  return (SNode*)pStmt;
}

1370
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1371
  CHECK_PARSER_STATUS(pCxt);
1372
  SCreateComponentNodeStmt* pStmt = (SCreateComponentNodeStmt*)nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1373
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1374
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
X
Xiaoyu Wang 已提交
1375 1376
  return (SNode*)pStmt;
}
1377

1378
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1379
  CHECK_PARSER_STATUS(pCxt);
1380
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1381
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1382
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1383 1384 1385
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1386 1387
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName,
                                     SNode* pQuery) {
X
Xiaoyu Wang 已提交
1388
  CHECK_PARSER_STATUS(pCxt);
1389
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1390
  CHECK_OUT_OF_MEM(pStmt);
1391
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1392
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
  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);
1420 1421 1422 1423
  return (SNode*)pStmt;
}

SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1424
  CHECK_PARSER_STATUS(pCxt);
1425
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1426
  CHECK_OUT_OF_MEM(pStmt);
1427
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1428 1429 1430
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1431

X
Xiaoyu Wang 已提交
1432 1433 1434
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pCGroupId,
                            const SToken* pTopicName) {
  CHECK_PARSER_STATUS(pCxt);
1435
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1436 1437
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
1438 1439
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
X
Xiaoyu Wang 已提交
1440 1441 1442
  return (SNode*)pStmt;
}

1443
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1444
  CHECK_PARSER_STATUS(pCxt);
1445
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1446 1447 1448 1449 1450 1451 1452
  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;
}
1453 1454

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1455
  CHECK_PARSER_STATUS(pCxt);
1456
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1457 1458 1459 1460 1461 1462 1463
  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 已提交
1464
  CHECK_PARSER_STATUS(pCxt);
1465 1466 1467 1468 1469
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1470
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1471
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1472 1473 1474 1475
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1476
  CHECK_PARSER_STATUS(pCxt);
1477
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1478 1479 1480 1481 1482 1483
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1484 1485

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1486
  CHECK_PARSER_STATUS(pCxt);
1487
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1488 1489 1490 1491 1492 1493 1494 1495
  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 已提交
1496
  CHECK_PARSER_STATUS(pCxt);
1497 1498 1499 1500
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1501 1502

SNode* createCompactStmt(SAstCreateContext* pCxt, SNodeList* pVgroups) {
X
Xiaoyu Wang 已提交
1503
  CHECK_PARSER_STATUS(pCxt);
1504 1505 1506 1507 1508
  SNode* pStmt = nodesMakeNode(QUERY_NODE_COMPACT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1509 1510
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
X
Xiaoyu Wang 已提交
1511
  CHECK_PARSER_STATUS(pCxt);
1512
  if (pLibPath->n <= 2) {
1513
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1514 1515
    return NULL;
  }
1516
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1517
  CHECK_OUT_OF_MEM(pStmt);
1518
  pStmt->ignoreExists = ignoreExists;
1519
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1520
  pStmt->isAgg = aggFunc;
1521
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
1522 1523 1524
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
  return (SNode*)pStmt;
1525 1526
}

1527
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1528
  CHECK_PARSER_STATUS(pCxt);
1529
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1530
  CHECK_OUT_OF_MEM(pStmt);
1531
  pStmt->ignoreNotExists = ignoreNotExists;
1532
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1533
  return (SNode*)pStmt;
1534 1535
}

1536
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1537
  CHECK_PARSER_STATUS(pCxt);
1538
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1539 1540 1541 1542 1543
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->triggerType = STREAM_TRIGGER_AT_ONCE;
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1544 1545
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pStreamName, SNode* pRealTable,
                              SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1546
  CHECK_PARSER_STATUS(pCxt);
1547
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1548
  CHECK_OUT_OF_MEM(pStmt);
1549
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1550 1551 1552 1553 1554 1555 1556 1557 1558
  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;
1559 1560
}

1561
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1562
  CHECK_PARSER_STATUS(pCxt);
1563
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1564
  CHECK_OUT_OF_MEM(pStmt);
1565
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1566 1567
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1568 1569 1570
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
1571
  CHECK_PARSER_STATUS(pCxt);
1572
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
1573
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1574
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
1575
  return (SNode*)pStmt;
1576 1577
}

X
Xiaoyu Wang 已提交
1578 1579 1580 1581
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 已提交
1582
  trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
X
Xiaoyu Wang 已提交
1583 1584 1585
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1586 1587
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
1588
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
1589 1590 1591 1592
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

1593
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
1594
  CHECK_PARSER_STATUS(pCxt);
1595
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
1596
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1597 1598 1599
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
1600 1601 1602
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
1603
  CHECK_PARSER_STATUS(pCxt);
1604
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
1605
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1606 1607 1608
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
1609 1610 1611
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
1612
  CHECK_PARSER_STATUS(pCxt);
1613
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
1614
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1615 1616
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
1617 1618 1619
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
1620
  CHECK_PARSER_STATUS(pCxt);
1621 1622 1623 1624
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1625 1626

SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1627
  CHECK_PARSER_STATUS(pCxt);
1628 1629 1630
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1631
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
1632 1633
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1634 1635
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1636 1637 1638 1639
  return (SNode*)pStmt;
}

SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1640
  CHECK_PARSER_STATUS(pCxt);
1641 1642 1643
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
1644
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
1645 1646
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
1647 1648
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1649 1650
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1651

X
Xiaoyu Wang 已提交
1652
SNode* createCountFuncForDelete(SAstCreateContext* pCxt) {
1653
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
1654 1655 1656
  CHECK_OUT_OF_MEM(pFunc);
  strcpy(pFunc->functionName, "count");
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt))) {
1657
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
1658 1659 1660 1661 1662
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
1663 1664
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
1665
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
1666 1667 1668
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
X
Xiaoyu Wang 已提交
1669 1670
  pStmt->pCountFunc = createCountFuncForDelete(pCxt);
  if (NULL == pStmt->pCountFunc) {
1671
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
1672 1673
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
1674 1675
  return (SNode*)pStmt;
}
1676 1677 1678 1679 1680 1681 1682 1683

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;
1684 1685 1686 1687 1688
  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);
  }
1689 1690
  return (SNode*)pStmt;
}