parAstCreater.c 77.6 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 213 214 215 216 217 218 219 220 221
static bool checkTopicName(SAstCreateContext* pCxt, SToken* pTopicName) {
  trimEscape(pTopicName);
  if (pTopicName->n >= TSDB_TOPIC_NAME_LEN) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTopicName->z);
    return false;
  }
  return true;
}

static bool checkStreamName(SAstCreateContext* pCxt, SToken* pStreamName) {
  trimEscape(pStreamName);
  if (pStreamName->n >= TSDB_STREAM_NAME_LEN) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pStreamName->z);
    return false;
  }
  return true;
}

222 223 224 225 226 227 228 229 230
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;
}

231
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
X
Xiaoyu Wang 已提交
232
  CHECK_PARSER_STATUS(pCxt);
233 234 235 236 237 238 239 240 241
  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 已提交
242
  CHECK_PARSER_STATUS(pCxt);
243 244 245 246 247 248 249 250 251
  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 已提交
252
  CHECK_PARSER_STATUS(pCxt);
253
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
254 255 256
  SNode*        pRealizedExpr = pRawExpr->pNode;
  if (nodesIsExprNode(pRealizedExpr)) {
    SExprNode* pExpr = (SExprNode*)pRealizedExpr;
X
Xiaoyu Wang 已提交
257
    if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
X
Xiaoyu Wang 已提交
258 259
      strcpy(pExpr->aliasName, ((SColumnNode*)pExpr)->colName);
      strcpy(pExpr->userAlias, ((SColumnNode*)pExpr)->colName);
X
Xiaoyu Wang 已提交
260
    } else {
X
Xiaoyu Wang 已提交
261 262 263 264 265
      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 已提交
266
    }
D
dapan1121 已提交
267
  }
268 269
  pRawExpr->pNode = NULL;
  nodesDestroyNode(pNode);
X
Xiaoyu Wang 已提交
270
  return pRealizedExpr;
271 272 273
}

SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
274
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
275
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
276 277
    return nil_token;
  }
278
  SRawExprNode* target = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
279
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
280 281 282
  return t;
}

283
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
284
  CHECK_PARSER_STATUS(pCxt);
285 286
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
287
  pCxt->errCode = nodesListAppend(list, pNode);
X
Xiaoyu Wang 已提交
288
  return list;
289 290 291
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
292
  CHECK_PARSER_STATUS(pCxt);
293
  pCxt->errCode = nodesListAppend(pList, pNode);
X
Xiaoyu Wang 已提交
294
  return pList;
295 296
}

X
Xiaoyu Wang 已提交
297
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
X
Xiaoyu Wang 已提交
298
  CHECK_PARSER_STATUS(pCxt);
299
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
300 301 302 303
    return NULL;
  }
  SColumnNode* col = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(col);
304
  if (NULL != pTableAlias) {
305
    COPY_STRING_FORM_ID_TOKEN(col->tableAlias, pTableAlias);
306
  }
307
  COPY_STRING_FORM_ID_TOKEN(col->colName, pColumnName);
308
  return (SNode*)col;
309
}
310

311
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
312
  CHECK_PARSER_STATUS(pCxt);
313 314
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
315 316 317 318
  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 已提交
319
  }
320
  CHECK_OUT_OF_MEM(val->literal);
321
  val->node.resType.type = dataType;
X
Xiaoyu Wang 已提交
322
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
323 324 325
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  }
326 327
  val->isDuration = false;
  val->translate = false;
328 329 330
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
331 332 333 334 335
SNode* createIdentifierValueNode(SAstCreateContext* pCxt, SToken* pLiteral) {
  trimEscape(pLiteral);
  return createValueNode(pCxt, TSDB_DATA_TYPE_BINARY, pLiteral);
}

336
SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
337
  CHECK_PARSER_STATUS(pCxt);
338 339
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
340 341 342
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
343
  val->translate = false;
344 345 346
  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;
347
  return (SNode*)val;
348 349
}

X
Xiaoyu Wang 已提交
350
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
351
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
352 353 354 355
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
356 357
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
358
  val->literal = taosStrdup(pCxt->pQueryCxt->db);
X
Xiaoyu Wang 已提交
359 360 361 362 363 364 365 366 367
  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;
}

368
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
X
Xiaoyu Wang 已提交
369
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
370 371 372 373
  if (NULL == pCxt->pQueryCxt->pStmtCb) {
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, pLiteral->z);
    return NULL;
  }
374 375
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
376 377
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
D
dapan1121 已提交
378
  val->placeholderNo = ++pCxt->placeholderNo;
X
Xiaoyu Wang 已提交
379 380 381
  if (NULL == pCxt->pPlaceholderValues) {
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    if (NULL == pCxt->pPlaceholderValues) {
382
      nodesDestroyNode((SNode*)val);
X
Xiaoyu Wang 已提交
383 384 385 386
      return NULL;
    }
  }
  taosArrayPush(pCxt->pPlaceholderValues, &val);
387 388 389
  return (SNode*)val;
}

X
Xiaoyu Wang 已提交
390 391 392 393 394 395 396 397 398 399 400
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);
  }
}

401
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
X
Xiaoyu Wang 已提交
402
  CHECK_PARSER_STATUS(pCxt);
403 404 405 406
  SLogicConditionNode* cond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  CHECK_OUT_OF_MEM(cond);
  cond->condType = type;
  cond->pParameterList = nodesMakeList();
X
Xiaoyu Wang 已提交
407 408 409
  int32_t code = addParamToLogicConditionNode(cond, pParam1);
  if (TSDB_CODE_SUCCESS == code && NULL != pParam2) {
    code = addParamToLogicConditionNode(cond, pParam2);
X
Xiaoyu Wang 已提交
410
  }
X
Xiaoyu Wang 已提交
411
  if (TSDB_CODE_SUCCESS != code) {
412
    nodesDestroyNode((SNode*)cond);
X
Xiaoyu Wang 已提交
413
    return NULL;
414
  }
415 416 417
  return (SNode*)cond;
}

418 419 420 421 422 423 424 425 426 427 428 429 430
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;
}

431
SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
432
  CHECK_PARSER_STATUS(pCxt);
433 434
  if (OP_TYPE_MINUS == type && QUERY_NODE_VALUE == nodeType(pLeft)) {
    SValueNode* pVal = (SValueNode*)pLeft;
435
    char*       pNewLiteral = taosMemoryCalloc(1, strlen(pVal->literal) + 2);
436
    CHECK_OUT_OF_MEM(pNewLiteral);
437 438 439 440 441 442 443
    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);
    }
444 445
    taosMemoryFree(pVal->literal);
    pVal->literal = pNewLiteral;
446
    pVal->node.resType.type = getMinusDataType(pVal->node.resType.type);
447 448
    return pLeft;
  }
449 450 451 452 453 454 455 456 457
  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 已提交
458
  CHECK_PARSER_STATUS(pCxt);
459
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
460 461
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
462 463 464
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
465
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
466 467
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
468 469
}

470
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
471
  CHECK_PARSER_STATUS(pCxt);
472
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
473 474
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
475 476 477 478 479
  if (NULL == pFuncName) {
    strcpy(pCol->colName, ROWTS_PSEUDO_COLUMN_NAME);
  } else {
    strncpy(pCol->colName, pFuncName->z, pFuncName->n);
  }
X
Xiaoyu Wang 已提交
480 481 482
  return (SNode*)pCol;
}

483
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
484
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
485
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
486
    return createPrimaryKeyCol(pCxt, pFuncName);
X
Xiaoyu Wang 已提交
487
  }
488 489
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
490
  COPY_STRING_FORM_ID_TOKEN(func->functionName, pFuncName);
491 492 493 494
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

495
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
X
Xiaoyu Wang 已提交
496
  CHECK_PARSER_STATUS(pCxt);
497 498 499 500
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
501 502 503 504
  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;
505
  }
506 507 508 509
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

510
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
511
  CHECK_PARSER_STATUS(pCxt);
512 513 514 515
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
516 517
}

X
Xiaoyu Wang 已提交
518
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
X
Xiaoyu Wang 已提交
519
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
520 521 522 523 524 525 526 527 528
  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 已提交
529
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
530
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
531
  if (!checkDbName(pCxt, pDbName, true) || !checkTableName(pCxt, pTableName) || !checkTableName(pCxt, pTableAlias)) {
532 533 534 535 536
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
537
    COPY_STRING_FORM_ID_TOKEN(realTable->table.dbName, pDbName);
538
  } else {
X
Xiaoyu Wang 已提交
539
    snprintf(realTable->table.dbName, sizeof(realTable->table.dbName), "%s", pCxt->pQueryCxt->db);
540
  }
541
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
542
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableAlias);
543
  } else {
544
    COPY_STRING_FORM_ID_TOKEN(realTable->table.tableAlias, pTableName);
545
  }
546
  COPY_STRING_FORM_ID_TOKEN(realTable->table.tableName, pTableName);
547
  return (SNode*)realTable;
548
}
549

550
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
X
Xiaoyu Wang 已提交
551
  CHECK_PARSER_STATUS(pCxt);
552 553 554
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
555
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
556
    COPY_STRING_FORM_ID_TOKEN(tempTable->table.tableAlias, pTableAlias);
X
Xiaoyu Wang 已提交
557
  } else {
558
    taosRandStr(tempTable->table.tableAlias, 8);
X
Xiaoyu Wang 已提交
559 560 561
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
562
    ((SSelectStmt*)pSubquery)->isSubquery = true;
563 564
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pSubquery)) {
    strcpy(((SSetOperator*)pSubquery)->stmtName, tempTable->table.tableAlias);
565
  }
566
  return (SNode*)tempTable;
567 568
}

569
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
X
Xiaoyu Wang 已提交
570
  CHECK_PARSER_STATUS(pCxt);
571 572 573
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
574 575
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
576
  joinTable->pOnCond = pJoinCond;
577
  return (SNode*)joinTable;
578
}
579

580
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
X
Xiaoyu Wang 已提交
581
  CHECK_PARSER_STATUS(pCxt);
582 583
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
wafwerar's avatar
wafwerar 已提交
584
  limitNode->limit = taosStr2Int64(pLimit->z, NULL, 10);
585
  if (NULL != pOffset) {
wafwerar's avatar
wafwerar 已提交
586
    limitNode->offset = taosStr2Int64(pOffset->z, NULL, 10);
587
  }
588
  return (SNode*)limitNode;
589 590
}

591
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
X
Xiaoyu Wang 已提交
592
  CHECK_PARSER_STATUS(pCxt);
593 594
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
595
  orderByExpr->pExpr = pExpr;
596
  orderByExpr->order = order;
597 598 599
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
600
  orderByExpr->nullOrder = nullOrder;
601 602 603
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
604
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
X
Xiaoyu Wang 已提交
605
  CHECK_PARSER_STATUS(pCxt);
606 607
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
608 609
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
610 611 612
  return (SNode*)session;
}

613
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
X
Xiaoyu Wang 已提交
614
  CHECK_PARSER_STATUS(pCxt);
615 616
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
617
  state->pCol = createPrimaryKeyCol(pCxt, NULL);
618
  if (NULL == state->pCol) {
619
    nodesDestroyNode((SNode*)state);
X
Xiaoyu Wang 已提交
620
    CHECK_OUT_OF_MEM(NULL);
621
  }
622
  state->pExpr = pExpr;
623 624 625
  return (SNode*)state;
}

X
Xiaoyu Wang 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639
SNode* createEventWindowNode(SAstCreateContext* pCxt, SNode* pStartCond, SNode* pEndCond) {
  CHECK_PARSER_STATUS(pCxt);
  SEventWindowNode* pEvent = (SEventWindowNode*)nodesMakeNode(QUERY_NODE_EVENT_WINDOW);
  CHECK_OUT_OF_MEM(pEvent);
  pEvent->pCol = createPrimaryKeyCol(pCxt, NULL);
  if (NULL == pEvent->pCol) {
    nodesDestroyNode((SNode*)pEvent);
    CHECK_OUT_OF_MEM(NULL);
  }
  pEvent->pStartCond = pStartCond;
  pEvent->pEndCond = pEndCond;
  return (SNode*)pEvent;
}

X
Xiaoyu Wang 已提交
640 641
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                                SNode* pFill) {
X
Xiaoyu Wang 已提交
642
  CHECK_PARSER_STATUS(pCxt);
643 644
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
645
  interval->pCol = createPrimaryKeyCol(pCxt, NULL);
X
bugfix  
Xiaoyu Wang 已提交
646
  if (NULL == interval->pCol) {
647
    nodesDestroyNode((SNode*)interval);
X
Xiaoyu Wang 已提交
648
    CHECK_OUT_OF_MEM(NULL);
X
bugfix  
Xiaoyu Wang 已提交
649
  }
650 651 652 653 654 655 656 657
  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 已提交
658
  CHECK_PARSER_STATUS(pCxt);
659 660 661 662
  SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
  CHECK_OUT_OF_MEM(fill);
  fill->mode = mode;
  fill->pValues = pValues;
X
Xiaoyu Wang 已提交
663 664
  fill->pWStartTs = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == fill->pWStartTs) {
665
    nodesDestroyNode((SNode*)fill);
X
Xiaoyu Wang 已提交
666
    CHECK_OUT_OF_MEM(NULL);
X
Xiaoyu Wang 已提交
667
  }
668
  strcpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstart");
669
  return (SNode*)fill;
670
}
671

672
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
673
  CHECK_PARSER_STATUS(pCxt);
674 675 676 677 678 679 680 681
  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 已提交
682 683
SNode* createInterpTimeRange(SAstCreateContext* pCxt, SNode* pStart, SNode* pEnd) {
  CHECK_PARSER_STATUS(pCxt);
684
  return createBetweenAnd(pCxt, createPrimaryKeyCol(pCxt, NULL), pStart, pEnd);
X
Xiaoyu Wang 已提交
685 686
}

X
Xiaoyu Wang 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
SNode* createWhenThenNode(SAstCreateContext* pCxt, SNode* pWhen, SNode* pThen) {
  CHECK_PARSER_STATUS(pCxt);
  SWhenThenNode* pWhenThen = (SWhenThenNode*)nodesMakeNode(QUERY_NODE_WHEN_THEN);
  CHECK_OUT_OF_MEM(pWhenThen);
  pWhenThen->pWhen = pWhen;
  pWhenThen->pThen = pThen;
  return (SNode*)pWhenThen;
}

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

X
Xiaoyu Wang 已提交
706
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, SToken* pAlias) {
X
Xiaoyu Wang 已提交
707
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
708
  trimEscape(pAlias);
X
Xiaoyu Wang 已提交
709 710 711 712 713 714
  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';
715
  pExpr->asAlias = true;
716
  return pNode;
717 718
}

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

727
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
X
Xiaoyu Wang 已提交
728
  CHECK_PARSER_STATUS(pCxt);
729
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
730
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
731 732
  }
  return pStmt;
733 734
}

735
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
X
Xiaoyu Wang 已提交
736
  CHECK_PARSER_STATUS(pCxt);
737
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
738
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
739 740 741
  }
  return pStmt;
}
742

743
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
X
Xiaoyu Wang 已提交
744
  CHECK_PARSER_STATUS(pCxt);
745
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
746
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
747 748
  }
  return pStmt;
749 750
}

751
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
X
Xiaoyu Wang 已提交
752
  CHECK_PARSER_STATUS(pCxt);
753
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
754
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
755 756 757 758 759
  }
  return pStmt;
}

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
X
Xiaoyu Wang 已提交
760
  CHECK_PARSER_STATUS(pCxt);
761 762 763
  if (NULL == pOrderByList) {
    return pStmt;
  }
764
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
765
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
766 767
  } else {
    ((SSetOperator*)pStmt)->pOrderByList = pOrderByList;
768 769 770
  }
  return pStmt;
}
771

772
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
X
Xiaoyu Wang 已提交
773
  CHECK_PARSER_STATUS(pCxt);
774 775 776
  if (NULL == pSlimit) {
    return pStmt;
  }
777
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
778
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
779 780
  }
  return pStmt;
781 782
}

783
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
X
Xiaoyu Wang 已提交
784
  CHECK_PARSER_STATUS(pCxt);
785 786 787
  if (NULL == pLimit) {
    return pStmt;
  }
788
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
789
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
X
Xiaoyu Wang 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
  } 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 已提交
814 815 816
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt) && NULL != pFill) {
    SFillNode* pFillClause = (SFillNode*)pFill;
    nodesDestroyNode(pFillClause->pWStartTs);
817
    pFillClause->pWStartTs = createPrimaryKeyCol(pCxt, NULL);
X
Xiaoyu Wang 已提交
818
    ((SSelectStmt*)pStmt)->pFill = (SNode*)pFillClause;
819
  }
820
  return pStmt;
821 822 823
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
X
Xiaoyu Wang 已提交
824
  CHECK_PARSER_STATUS(pCxt);
825
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
826
  CHECK_OUT_OF_MEM(select);
827
  select->isDistinct = isDistinct;
828 829
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
830
  sprintf(select->stmtName, "%p", select);
831
  select->isTimeLineResult = true;
832
  select->onlyHasKeepOrderFunc = true;
833
  select->timeRange = TSWINDOW_INITIALIZER;
834
  return (SNode*)select;
835 836
}

X
Xiaoyu Wang 已提交
837 838 839 840 841 842
static void setSubquery(SNode* pStmt) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    ((SSelectStmt*)pStmt)->isSubquery = true;
  }
}

843
SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
844
  CHECK_PARSER_STATUS(pCxt);
845 846 847
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
848
  setOp->pLeft = pLeft;
X
Xiaoyu Wang 已提交
849
  setSubquery(setOp->pLeft);
850
  setOp->pRight = pRight;
X
Xiaoyu Wang 已提交
851
  setSubquery(setOp->pRight);
852
  sprintf(setOp->stmtName, "%p", setOp);
853
  return (SNode*)setOp;
854 855
}

856 857 858 859 860 861 862 863 864 865 866 867 868 869
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 已提交
870
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
871
  CHECK_PARSER_STATUS(pCxt);
872
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
873
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
874
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
875 876
  pOptions->cacheModel = TSDB_DEFAULT_CACHE_MODEL;
  pOptions->cacheLastSize = TSDB_DEFAULT_CACHE_SIZE;
X
Xiaoyu Wang 已提交
877 878 879 880 881 882 883 884 885 886
  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;
887
  pOptions->tsdbPageSize = TSDB_DEFAULT_TSDB_PAGESIZE;
X
Xiaoyu Wang 已提交
888 889 890 891 892 893
  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 已提交
894
  pOptions->schemaless = TSDB_DEFAULT_DB_SCHEMALESS;
895
  updateWalOptionsDefault(pOptions);
896
  pOptions->walSegmentSize = TSDB_DEFAULT_DB_WAL_SEGMENT_SIZE;
897
  pOptions->sstTrigger = TSDB_DEFAULT_SST_TRIGGER;
898 899
  pOptions->tablePrefix = TSDB_DEFAULT_HASH_PREFIX;
  pOptions->tableSuffix = TSDB_DEFAULT_HASH_SUFFIX;
X
Xiaoyu Wang 已提交
900
  return (SNode*)pOptions;
901 902
}

X
Xiaoyu Wang 已提交
903
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
904
  CHECK_PARSER_STATUS(pCxt);
905
  SDatabaseOptions* pOptions = (SDatabaseOptions*)nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
X
Xiaoyu Wang 已提交
906 907
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
908
  pOptions->cacheModel = -1;
909
  pOptions->cacheLastSize = -1;
X
Xiaoyu Wang 已提交
910 911 912 913 914 915 916 917 918 919
  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;
920
  pOptions->tsdbPageSize = -1;
X
Xiaoyu Wang 已提交
921 922 923 924 925 926
  pOptions->precision = -1;
  pOptions->replica = -1;
  pOptions->strict = -1;
  pOptions->walLevel = -1;
  pOptions->numOfVgroups = -1;
  pOptions->singleStable = -1;
X
Xiaoyu Wang 已提交
927
  pOptions->schemaless = -1;
928 929
  pOptions->walRetentionPeriod = -2;  // -1 is a valid value
  pOptions->walRetentionSize = -2;    // -1 is a valid value
930 931
  pOptions->walRollPeriod = -1;
  pOptions->walSegmentSize = -1;
932
  pOptions->sstTrigger = -1;
933 934
  pOptions->tablePrefix = -1;
  pOptions->tableSuffix = -1;
X
Xiaoyu Wang 已提交
935 936 937
  return (SNode*)pOptions;
}

938 939
static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal,
                                    bool alter) {
X
Xiaoyu Wang 已提交
940
  CHECK_PARSER_STATUS(pCxt);
941
  SDatabaseOptions* pDbOptions = (SDatabaseOptions*)pOptions;
X
Xiaoyu Wang 已提交
942 943
  switch (type) {
    case DB_OPTION_BUFFER:
944
      pDbOptions->buffer = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
945
      break;
946
    case DB_OPTION_CACHEMODEL:
947
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->cacheModelStr, (SToken*)pVal);
948
      break;
949
    case DB_OPTION_CACHESIZE:
950
      pDbOptions->cacheLastSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
951 952
      break;
    case DB_OPTION_COMP:
953
      pDbOptions->compressionLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
954
      break;
X
Xiaoyu Wang 已提交
955 956 957
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
958
        pDbOptions->daysPerFile = taosStr2Int32(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
959
      } else {
960
        pDbOptions->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
X
Xiaoyu Wang 已提交
961
      }
X
Xiaoyu Wang 已提交
962
      break;
X
Xiaoyu Wang 已提交
963
    }
X
Xiaoyu Wang 已提交
964
    case DB_OPTION_FSYNC:
965
      pDbOptions->fsyncPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
966 967
      break;
    case DB_OPTION_MAXROWS:
968
      pDbOptions->maxRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
969 970
      break;
    case DB_OPTION_MINROWS:
971
      pDbOptions->minRowsPerBlock = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
972 973
      break;
    case DB_OPTION_KEEP:
974
      pDbOptions->pKeep = pVal;
X
Xiaoyu Wang 已提交
975 976
      break;
    case DB_OPTION_PAGES:
977
      pDbOptions->pages = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
978 979
      break;
    case DB_OPTION_PAGESIZE:
980
      pDbOptions->pagesize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
981
      break;
982 983 984
    case DB_OPTION_TSDB_PAGESIZE:
      pDbOptions->tsdbPageSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
X
Xiaoyu Wang 已提交
985
    case DB_OPTION_PRECISION:
986
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->precisionStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
987 988
      break;
    case DB_OPTION_REPLICA:
989
      pDbOptions->replica = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
990 991 992
      if (!alter) {
        updateWalOptionsDefault(pDbOptions);
      }
X
Xiaoyu Wang 已提交
993
      break;
X
Xiaoyu Wang 已提交
994
    case DB_OPTION_STRICT:
995
      COPY_STRING_FORM_STR_TOKEN(pDbOptions->strictStr, (SToken*)pVal);
X
Xiaoyu Wang 已提交
996 997
      break;
    case DB_OPTION_WAL:
998
      pDbOptions->walLevel = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
999 1000
      break;
    case DB_OPTION_VGROUPS:
1001
      pDbOptions->numOfVgroups = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1002 1003
      break;
    case DB_OPTION_SINGLE_STABLE:
1004
      pDbOptions->singleStable = taosStr2Int8(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1005 1006
      break;
    case DB_OPTION_RETENTIONS:
1007
      pDbOptions->pRetentions = pVal;
X
Xiaoyu Wang 已提交
1008
      break;
X
Xiaoyu Wang 已提交
1009
    case DB_OPTION_WAL_RETENTION_PERIOD:
1010 1011
      pDbOptions->walRetentionPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionPeriodIsSet = true;
X
Xiaoyu Wang 已提交
1012 1013
      break;
    case DB_OPTION_WAL_RETENTION_SIZE:
1014 1015
      pDbOptions->walRetentionSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRetentionSizeIsSet = true;
X
Xiaoyu Wang 已提交
1016 1017
      break;
    case DB_OPTION_WAL_ROLL_PERIOD:
1018 1019
      pDbOptions->walRollPeriod = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      pDbOptions->walRollPeriodIsSet = true;
X
Xiaoyu Wang 已提交
1020 1021
      break;
    case DB_OPTION_WAL_SEGMENT_SIZE:
1022
      pDbOptions->walSegmentSize = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1023
      break;
1024
    case DB_OPTION_STT_TRIGGER:
1025 1026
      pDbOptions->sstTrigger = taosStr2Int32(((SToken*)pVal)->z, NULL, 10);
      break;
1027
    case DB_OPTION_TABLE_PREFIX: {
1028
      SValueNode* pNode = (SValueNode*)pVal;
1029 1030 1031 1032
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
        pDbOptions->tablePrefix = taosStr2Int32(pNode->literal, NULL, 10);
      } else {
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_prefix data type");
1033
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1034
      }
D
dapan1121 已提交
1035
      nodesDestroyNode((SNode*)pNode);
1036
      break;
1037
    }
1038 1039
    case DB_OPTION_TABLE_SUFFIX: {
      SValueNode* pNode = (SValueNode*)pVal;
1040 1041 1042 1043
      if (TSDB_DATA_TYPE_BIGINT == pNode->node.resType.type || TSDB_DATA_TYPE_UBIGINT == pNode->node.resType.type) {
        pDbOptions->tableSuffix = taosStr2Int32(pNode->literal, NULL, 10);
      } else {
        snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "invalid table_suffix data type");
1044
        pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1045
      }
D
dapan1121 已提交
1046
      nodesDestroyNode((SNode*)pNode);
1047
      break;
1048
    }
X
Xiaoyu Wang 已提交
1049 1050
    default:
      break;
X
Xiaoyu Wang 已提交
1051
  }
X
Xiaoyu Wang 已提交
1052
  return pOptions;
X
Xiaoyu Wang 已提交
1053 1054
}

1055 1056 1057 1058
SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
  return setDatabaseOptionImpl(pCxt, pOptions, type, pVal, false);
}

X
Xiaoyu Wang 已提交
1059
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
1060
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1061 1062 1063
  switch (pAlterOption->type) {
    case DB_OPTION_KEEP:
    case DB_OPTION_RETENTIONS:
1064
      return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, pAlterOption->pList, true);
X
Xiaoyu Wang 已提交
1065 1066 1067
    default:
      break;
  }
1068
  return setDatabaseOptionImpl(pCxt, pOptions, pAlterOption->type, &pAlterOption->val, true);
X
Xiaoyu Wang 已提交
1069 1070
}

X
Xiaoyu Wang 已提交
1071
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1072
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1073
  if (!checkDbName(pCxt, pDbName, false)) {
1074 1075 1076 1077
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1078
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1079
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1080
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
1081
  return (SNode*)pStmt;
1082
}
X
Xiaoyu Wang 已提交
1083

X
Xiaoyu Wang 已提交
1084
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1085
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1086
  if (!checkDbName(pCxt, pDbName, false)) {
1087 1088 1089 1090
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1091
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1092 1093 1094 1095
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1096
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1097
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1098
  if (!checkDbName(pCxt, pDbName, false)) {
1099 1100
    return NULL;
  }
1101
  SAlterDatabaseStmt* pStmt = (SAlterDatabaseStmt*)nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
1102
  CHECK_OUT_OF_MEM(pStmt);
1103
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1104 1105 1106 1107
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1108 1109 1110 1111 1112
SNode* createFlushDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
1113 1114 1115 1116 1117 1118
  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;
}

1119
SNode* createTrimDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, int32_t maxSpeed) {
X
Xiaoyu Wang 已提交
1120 1121 1122 1123 1124
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
  STrimDatabaseStmt* pStmt = (STrimDatabaseStmt*)nodesMakeNode(QUERY_NODE_TRIM_DATABASE_STMT);
X
Xiaoyu Wang 已提交
1125 1126
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1127
  pStmt->maxSpeed = maxSpeed;
X
Xiaoyu Wang 已提交
1128 1129 1130
  return (SNode*)pStmt;
}

1131
SNode* createCompactStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pStart, SNode* pEnd) {
X
Xiaoyu Wang 已提交
1132 1133 1134 1135 1136 1137 1138
  CHECK_PARSER_STATUS(pCxt);
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
  SCompactDatabaseStmt* pStmt = (SCompactDatabaseStmt*)nodesMakeNode(QUERY_NODE_COMPACT_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1139 1140
  pStmt->pStart = pStart;
  pStmt->pEnd = pEnd;
X
Xiaoyu Wang 已提交
1141 1142 1143
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1144
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1145
  CHECK_PARSER_STATUS(pCxt);
1146
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
1147
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
1148 1149 1150 1151
  pOptions->maxDelay1 = -1;
  pOptions->maxDelay2 = -1;
  pOptions->watermark1 = TSDB_DEFAULT_ROLLUP_WATERMARK;
  pOptions->watermark2 = TSDB_DEFAULT_ROLLUP_WATERMARK;
X
Xiaoyu Wang 已提交
1152
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
X
Xiaoyu Wang 已提交
1153
  pOptions->commentNull = true;  // mark null
1154
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
1155 1156
}

X
Xiaoyu Wang 已提交
1157
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1158
  CHECK_PARSER_STATUS(pCxt);
1159
  STableOptions* pOptions = (STableOptions*)nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
X
Xiaoyu Wang 已提交
1160 1161
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->ttl = -1;
X
Xiaoyu Wang 已提交
1162
  pOptions->commentNull = true;  // mark null
X
Xiaoyu Wang 已提交
1163 1164 1165 1166
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
X
Xiaoyu Wang 已提交
1167
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1168
  switch (type) {
X
Xiaoyu Wang 已提交
1169
    case TABLE_OPTION_COMMENT:
1170
      if (checkComment(pCxt, (SToken*)pVal, true)) {
wmmhello's avatar
wmmhello 已提交
1171
        ((STableOptions*)pOptions)->commentNull = false;
1172
        COPY_STRING_FORM_STR_TOKEN(((STableOptions*)pOptions)->comment, (SToken*)pVal);
1173
      }
X
Xiaoyu Wang 已提交
1174
      break;
X
Xiaoyu Wang 已提交
1175 1176 1177 1178 1179
    case TABLE_OPTION_MAXDELAY:
      ((STableOptions*)pOptions)->pMaxDelay = pVal;
      break;
    case TABLE_OPTION_WATERMARK:
      ((STableOptions*)pOptions)->pWatermark = pVal;
X
Xiaoyu Wang 已提交
1180
      break;
X
Xiaoyu Wang 已提交
1181 1182 1183
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
X
Xiaoyu Wang 已提交
1184
    case TABLE_OPTION_TTL: {
1185
      int64_t ttl = taosStr2Int64(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
1186
      if (ttl > INT32_MAX) {
1187 1188 1189 1190
        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 已提交
1191
      break;
1192
    }
X
Xiaoyu Wang 已提交
1193 1194
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
1195
      break;
1196 1197 1198
    case TABLE_OPTION_DELETE_MARK:
      ((STableOptions*)pOptions)->pDeleteMark = pVal;
      break;
X
Xiaoyu Wang 已提交
1199 1200
    default:
      break;
X
Xiaoyu Wang 已提交
1201
  }
X
Xiaoyu Wang 已提交
1202
  return pOptions;
X
Xiaoyu Wang 已提交
1203 1204
}

X
Xiaoyu Wang 已提交
1205
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
X
Xiaoyu Wang 已提交
1206
  CHECK_PARSER_STATUS(pCxt);
1207
  if (!checkColumnName(pCxt, pColName) || !checkComment(pCxt, pComment, false)) {
X
Xiaoyu Wang 已提交
1208 1209
    return NULL;
  }
X
Xiaoyu Wang 已提交
1210 1211
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
1212
  COPY_STRING_FORM_ID_TOKEN(pCol->colName, pColName);
X
Xiaoyu Wang 已提交
1213 1214
  pCol->dataType = dataType;
  if (NULL != pComment) {
1215
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
1216
  }
1217
  pCol->sma = true;
X
Xiaoyu Wang 已提交
1218 1219 1220
  return (SNode*)pCol;
}

1221
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
1222
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
1223 1224 1225 1226
  return dt;
}

SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
K
kailixu 已提交
1227
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = taosStr2Int32(pLen->z, NULL, 10)};
1228 1229 1230
  return dt;
}

X
Xiaoyu Wang 已提交
1231 1232
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1233
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1234 1235
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1236 1237
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1238 1239
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
1240
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
1241
  pStmt->pOptions = (STableOptions*)pOptions;
1242
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1243 1244
  return (SNode*)pStmt;
}
1245

X
Xiaoyu Wang 已提交
1246
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
1247
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1248
  CHECK_PARSER_STATUS(pCxt);
1249
  SCreateSubTableClause* pStmt = (SCreateSubTableClause*)nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
1250
  CHECK_OUT_OF_MEM(pStmt);
1251 1252 1253 1254
  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);
1255 1256 1257
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
wmmhello's avatar
wmmhello 已提交
1258
  pStmt->pOptions = (STableOptions*)pOptions;
1259 1260
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
1261 1262 1263 1264
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
X
Xiaoyu Wang 已提交
1265
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
1266
  SCreateMultiTablesStmt* pStmt = (SCreateMultiTablesStmt*)nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLES_STMT);
1267 1268 1269 1270 1271
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

1272
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1273
  CHECK_PARSER_STATUS(pCxt);
1274
  SDropTableClause* pStmt = (SDropTableClause*)nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
1275 1276 1277 1278 1279 1280 1281 1282 1283
  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 已提交
1284
  CHECK_PARSER_STATUS(pCxt);
1285
  SDropTableStmt* pStmt = (SDropTableStmt*)nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
1286 1287 1288 1289 1290
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

1291
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1292
  CHECK_PARSER_STATUS(pCxt);
1293
  SDropSuperTableStmt* pStmt = (SDropSuperTableStmt*)nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
1294 1295 1296 1297 1298 1299 1300 1301
  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 已提交
1302 1303 1304 1305 1306 1307 1308
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 已提交
1309
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1310
  CHECK_PARSER_STATUS(pCxt);
1311
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1312 1313 1314
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
1315
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1316 1317
}

X
Xiaoyu Wang 已提交
1318 1319
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName,
                                    SDataType dataType) {
X
Xiaoyu Wang 已提交
1320 1321
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1322 1323
    return NULL;
  }
1324
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1325 1326
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1327
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
1328
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
1329
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1330 1331
}

X
Xiaoyu Wang 已提交
1332
SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pColName) {
X
Xiaoyu Wang 已提交
1333 1334
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1335 1336
    return NULL;
  }
1337
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1338 1339
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1340
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pColName);
X
Xiaoyu Wang 已提交
1341
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1342 1343
}

X
Xiaoyu Wang 已提交
1344 1345
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, SToken* pOldColName,
                                 SToken* pNewColName) {
X
Xiaoyu Wang 已提交
1346 1347
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pOldColName) || !checkColumnName(pCxt, pNewColName)) {
X
bugfix  
Xiaoyu Wang 已提交
1348 1349
    return NULL;
  }
1350
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1351 1352
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
1353 1354
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pOldColName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->newColName, pNewColName);
X
Xiaoyu Wang 已提交
1355
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1356 1357
}

X
Xiaoyu Wang 已提交
1358
SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, SToken* pTagName, SNode* pVal) {
X
Xiaoyu Wang 已提交
1359 1360
  CHECK_PARSER_STATUS(pCxt);
  if (!checkColumnName(pCxt, pTagName)) {
X
bugfix  
Xiaoyu Wang 已提交
1361 1362
    return NULL;
  }
1363
  SAlterTableStmt* pStmt = (SAlterTableStmt*)nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
1364 1365
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
1366
  COPY_STRING_FORM_ID_TOKEN(pStmt->colName, pTagName);
1367
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1368
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1369 1370
}

1371 1372 1373 1374 1375
SNode* setAlterSuperTableType(SNode* pStmt) {
  setNodeType(pStmt, QUERY_NODE_ALTER_SUPER_TABLE_STMT);
  return pStmt;
}

1376
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1377
  CHECK_PARSER_STATUS(pCxt);
1378 1379 1380
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1381 1382
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1383
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
1384 1385 1386
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1387
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1388
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
1389
         QUERY_NODE_SHOW_VGROUPS_STMT == type || QUERY_NODE_SHOW_INDEXES_STMT == type ||
1390
         QUERY_NODE_SHOW_TAGS_STMT == type || QUERY_NODE_SHOW_TABLE_TAGS_STMT == type;
X
Xiaoyu Wang 已提交
1391 1392
}

1393 1394 1395 1396 1397 1398 1399 1400 1401
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 已提交
1402
  CHECK_PARSER_STATUS(pCxt);
1403
  if (needDbShowStmt(type) && NULL == pDbName) {
X
Xiaoyu Wang 已提交
1404
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
1405
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1406 1407
    return NULL;
  }
1408
  SShowStmt* pStmt = (SShowStmt*)nodesMakeNode(type);
1409
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1410
  pStmt->pDbName = pDbName;
1411 1412
  pStmt->pTbName = pTbName;
  pStmt->tableCondType = tableCondType;
1413
  return (SNode*)pStmt;
1414
}
1415

1416
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
X
Xiaoyu Wang 已提交
1417
  CHECK_PARSER_STATUS(pCxt);
1418 1419 1420 1421
  if (!checkDbName(pCxt, pDbName, true)) {
    return NULL;
  }
  SShowCreateDatabaseStmt* pStmt = (SShowCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
1422
  CHECK_OUT_OF_MEM(pStmt);
1423 1424
  COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbName);
  return (SNode*)pStmt;
1425 1426
}

1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
SNode* createShowAliveStmt(SAstCreateContext* pCxt, SNode* pNode, ENodeType type) {
  CHECK_PARSER_STATUS(pCxt);
  SToken  dbToken = {0};
  SToken* pDbToken = NULL;

  if (pNode) {
    SValueNode* pDbName = (SValueNode*)pNode;
    if (pDbName->literal) {
      dbToken.z = pDbName->literal;
      dbToken.n = strlen(pDbName->literal);
      pDbToken = &dbToken;
    }
  }

  if (pDbToken && !checkDbName(pCxt, pDbToken, true)) {
    nodesDestroyNode(pNode);
    return NULL;
  }

  SShowAliveStmt* pStmt = (SShowAliveStmt*)nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);

  if (pDbToken) {
    COPY_STRING_FORM_ID_TOKEN(pStmt->dbName, pDbToken);
  }
  if (pNode) {
    nodesDestroyNode(pNode);
  }

  return (SNode*)pStmt;
}

1459
SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1460
  CHECK_PARSER_STATUS(pCxt);
1461
  SShowCreateTableStmt* pStmt = (SShowCreateTableStmt*)nodesMakeNode(type);
1462
  CHECK_OUT_OF_MEM(pStmt);
1463 1464
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
  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);
1476
  return (SNode*)pStmt;
1477 1478
}

1479
SNode* createShowDnodeVariablesStmt(SAstCreateContext* pCxt, SNode* pDnodeId, SNode* pLikePattern) {
1480 1481 1482 1483
  CHECK_PARSER_STATUS(pCxt);
  SShowDnodeVariablesStmt* pStmt = (SShowDnodeVariablesStmt*)nodesMakeNode(QUERY_NODE_SHOW_DNODE_VARIABLES_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDnodeId = pDnodeId;
1484
  pStmt->pLikePattern = pLikePattern;
1485 1486 1487
  return (SNode*)pStmt;
}

1488 1489 1490 1491 1492 1493 1494 1495 1496
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;
}

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
SNode* createShowTableTagsStmt(SAstCreateContext* pCxt, SNode* pTbName, SNode* pDbName, SNodeList* pTags) {
  CHECK_PARSER_STATUS(pCxt);
  if (NULL == pDbName) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "database not specified");
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
    return NULL;
  }
  SShowTableTagsStmt* pStmt = (SShowTableTagsStmt*)nodesMakeNode(QUERY_NODE_SHOW_TABLE_TAGS_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pDbName = pDbName;
  pStmt->pTbName = pTbName;
  pStmt->pTags = pTags;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1512
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword, int8_t sysinfo) {
X
Xiaoyu Wang 已提交
1513
  CHECK_PARSER_STATUS(pCxt);
1514
  char password[TSDB_USET_PASSWORD_LEN + 3] = {0};
1515 1516 1517 1518 1519
  if (!checkUserName(pCxt, pUserName) || !checkPassword(pCxt, pPassword, password)) {
    return NULL;
  }
  SCreateUserStmt* pStmt = (SCreateUserStmt*)nodesMakeNode(QUERY_NODE_CREATE_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1520
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1521
  strcpy(pStmt->password, password);
X
Xiaoyu Wang 已提交
1522
  pStmt->sysinfo = sysinfo;
1523 1524 1525
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1526
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1527
  CHECK_PARSER_STATUS(pCxt);
1528 1529 1530 1531 1532
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1533
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
X
Xiaoyu Wang 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
  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;
1544
    }
X
Xiaoyu Wang 已提交
1545 1546 1547 1548 1549 1550 1551 1552
    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;
1553 1554 1555 1556
  }
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1557
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
X
Xiaoyu Wang 已提交
1558
  CHECK_PARSER_STATUS(pCxt);
1559 1560 1561 1562 1563
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1564
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
1565 1566 1567 1568
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
X
Xiaoyu Wang 已提交
1569
  CHECK_PARSER_STATUS(pCxt);
1570 1571
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
1572 1573 1574
  if (!checkAndSplitEndpoint(pCxt, pFqdn, pPort, pStmt->fqdn, &pStmt->port)) {
    nodesDestroyNode((SNode*)pStmt);
    return NULL;
1575 1576 1577 1578
  }
  return (SNode*)pStmt;
}

1579
SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, bool force) {
X
Xiaoyu Wang 已提交
1580
  CHECK_PARSER_STATUS(pCxt);
1581 1582 1583
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
wafwerar's avatar
wafwerar 已提交
1584
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
1585
  } else {
1586
    if (!checkAndSplitEndpoint(pCxt, pDnode, NULL, pStmt->fqdn, &pStmt->port)) {
1587
      nodesDestroyNode((SNode*)pStmt);
1588 1589 1590
      return NULL;
    }
  }
1591
  pStmt->force = force;
1592 1593
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1594

X
Xiaoyu Wang 已提交
1595 1596
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
X
Xiaoyu Wang 已提交
1597
  CHECK_PARSER_STATUS(pCxt);
1598
  SAlterDnodeStmt* pStmt = (SAlterDnodeStmt*)nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
1599
  CHECK_OUT_OF_MEM(pStmt);
1600 1601 1602 1603 1604
  if (NULL != pDnode) {
    pStmt->dnodeId = taosStr2Int32(pDnode->z, NULL, 10);
  } else {
    pStmt->dnodeId = -1;
  }
1605 1606 1607 1608 1609 1610 1611
  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;
}

1612 1613 1614 1615 1616 1617 1618
SNode* createRealTableNodeForIndexName(SAstCreateContext* pCxt, SToken* pDbName, SToken* pIndexName) {
  if (!checkIndexName(pCxt, pIndexName)) {
    return NULL;
  }
  return createRealTableNode(pCxt, pDbName, pIndexName, NULL);
}

X
Xiaoyu Wang 已提交
1619
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SNode* pIndexName,
1620
                             SNode* pRealTable, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1621
  CHECK_PARSER_STATUS(pCxt);
1622
  SCreateIndexStmt* pStmt = (SCreateIndexStmt*)nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
X
Xiaoyu Wang 已提交
1623 1624
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1625
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1626 1627 1628 1629
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
  snprintf(pStmt->dbName, sizeof(pStmt->dbName), "%s", ((SRealTableNode*)pRealTable)->table.dbName);
  snprintf(pStmt->tableName, sizeof(pStmt->tableName), "%s", ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
1630
  nodesDestroyNode(pIndexName);
1631
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
1632 1633 1634 1635 1636
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

1637 1638
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                         SNode* pStreamOptions) {
X
Xiaoyu Wang 已提交
1639
  CHECK_PARSER_STATUS(pCxt);
1640
  SIndexOptions* pOptions = (SIndexOptions*)nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
X
Xiaoyu Wang 已提交
1641 1642 1643 1644 1645
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
1646
  pOptions->pStreamOptions = pStreamOptions;
X
Xiaoyu Wang 已提交
1647 1648
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1649

X
Xiaoyu Wang 已提交
1650
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pIndexName) {
X
Xiaoyu Wang 已提交
1651
  CHECK_PARSER_STATUS(pCxt);
1652
  SDropIndexStmt* pStmt = (SDropIndexStmt*)nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
1653
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1654
  pStmt->ignoreNotExists = ignoreNotExists;
X
Xiaoyu Wang 已提交
1655 1656
  snprintf(pStmt->indexDbName, sizeof(pStmt->indexDbName), "%s", ((SRealTableNode*)pIndexName)->table.dbName);
  snprintf(pStmt->indexName, sizeof(pStmt->indexName), "%s", ((SRealTableNode*)pIndexName)->table.tableName);
X
Xiaoyu Wang 已提交
1657
  nodesDestroyNode(pIndexName);
1658 1659 1660
  return (SNode*)pStmt;
}

1661
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1662
  CHECK_PARSER_STATUS(pCxt);
1663
  SCreateComponentNodeStmt* pStmt = (SCreateComponentNodeStmt*)nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1664
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1665
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
X
Xiaoyu Wang 已提交
1666 1667
  return (SNode*)pStmt;
}
1668

1669
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
X
Xiaoyu Wang 已提交
1670
  CHECK_PARSER_STATUS(pCxt);
1671
  SDropComponentNodeStmt* pStmt = (SDropComponentNodeStmt*)nodesMakeNode(type);
1672
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1673
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
1674 1675 1676
  return (SNode*)pStmt;
}

D
dapan1121 已提交
1677 1678 1679 1680 1681 1682 1683 1684
SNode* createRestoreComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
  CHECK_PARSER_STATUS(pCxt);
  SRestoreComponentNodeStmt* pStmt = (SRestoreComponentNodeStmt*)nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->dnodeId = taosStr2Int32(pDnodeId->z, NULL, 10);
  return (SNode*)pStmt;
}

1685
SNode* createCreateTopicStmtUseQuery(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1686
  CHECK_PARSER_STATUS(pCxt);
1687 1688 1689
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
1690
  SCreateTopicStmt* pStmt = (SCreateTopicStmt*)nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
1691
  CHECK_OUT_OF_MEM(pStmt);
1692
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1693
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1694 1695 1696 1697
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}

1698 1699
SNode* createCreateTopicStmtUseDb(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SToken* pSubDbName,
                                  bool withMeta) {
X
Xiaoyu Wang 已提交
1700
  CHECK_PARSER_STATUS(pCxt);
1701
  if (!checkTopicName(pCxt, pTopicName) || !checkDbName(pCxt, pSubDbName, true)) {
X
Xiaoyu Wang 已提交
1702 1703
    return NULL;
  }
X
Xiaoyu Wang 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712
  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;
}

1713 1714
SNode* createCreateTopicStmtUseTable(SAstCreateContext* pCxt, bool ignoreExists, SToken* pTopicName, SNode* pRealTable,
                                     bool withMeta) {
X
Xiaoyu Wang 已提交
1715
  CHECK_PARSER_STATUS(pCxt);
1716 1717 1718
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
1719 1720 1721 1722 1723 1724 1725 1726
  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);
1727 1728 1729
  return (SNode*)pStmt;
}

1730
SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1731
  CHECK_PARSER_STATUS(pCxt);
1732 1733 1734
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
1735
  SDropTopicStmt* pStmt = (SDropTopicStmt*)nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
1736
  CHECK_OUT_OF_MEM(pStmt);
1737
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
1738 1739 1740
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1741

X
Xiaoyu Wang 已提交
1742
SNode* createDropCGroupStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pCGroupId,
1743
                            SToken* pTopicName) {
X
Xiaoyu Wang 已提交
1744
  CHECK_PARSER_STATUS(pCxt);
1745 1746 1747
  if (!checkTopicName(pCxt, pTopicName)) {
    return NULL;
  }
1748
  SDropCGroupStmt* pStmt = (SDropCGroupStmt*)nodesMakeNode(QUERY_NODE_DROP_CGROUP_STMT);
X
Xiaoyu Wang 已提交
1749 1750
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->ignoreNotExists = ignoreNotExists;
1751 1752
  COPY_STRING_FORM_ID_TOKEN(pStmt->topicName, pTopicName);
  COPY_STRING_FORM_ID_TOKEN(pStmt->cgroup, pCGroupId);
X
Xiaoyu Wang 已提交
1753 1754 1755
  return (SNode*)pStmt;
}

1756
SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
X
Xiaoyu Wang 已提交
1757
  CHECK_PARSER_STATUS(pCxt);
1758
  SAlterLocalStmt* pStmt = (SAlterLocalStmt*)nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
1759 1760 1761 1762 1763 1764 1765
  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;
}
1766 1767

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1768
  CHECK_PARSER_STATUS(pCxt);
1769
  SExplainOptions* pOptions = (SExplainOptions*)nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
1770 1771 1772 1773 1774 1775 1776
  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 已提交
1777
  CHECK_PARSER_STATUS(pCxt);
1778 1779 1780 1781 1782
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
X
Xiaoyu Wang 已提交
1783
  CHECK_PARSER_STATUS(pCxt);
wafwerar's avatar
wafwerar 已提交
1784
  ((SExplainOptions*)pOptions)->ratio = taosStr2Double(pVal->z, NULL);
1785 1786 1787 1788
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
X
Xiaoyu Wang 已提交
1789
  CHECK_PARSER_STATUS(pCxt);
1790
  SExplainStmt* pStmt = (SExplainStmt*)nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
1791 1792 1793 1794 1795 1796
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1797 1798

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
Xiaoyu Wang 已提交
1799
  CHECK_PARSER_STATUS(pCxt);
1800
  SDescribeStmt* pStmt = (SDescribeStmt*)nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
1801 1802 1803 1804 1805 1806 1807 1808
  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 已提交
1809
  CHECK_PARSER_STATUS(pCxt);
1810 1811 1812 1813
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1814

1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
static int32_t convertUdfLanguageType(SAstCreateContext* pCxt, const SToken* pLanguageToken, int8_t* pLanguage) {
  if (TK_NK_NIL == pLanguageToken->type || 0 == strncasecmp(pLanguageToken->z + 1, "c", pLanguageToken->n - 2)) {
    *pLanguage = TSDB_FUNC_SCRIPT_BIN_LIB;
  } else if (0 == strncasecmp(pLanguageToken->z + 1, "python", pLanguageToken->n - 2)) {
    *pLanguage = TSDB_FUNC_SCRIPT_PYTHON;
  } else {
    pCxt->errCode = generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR,
                                            "udf programming language supports c and python");
  }
  return pCxt->errCode;
}

X
Xiaoyu Wang 已提交
1827
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
1828 1829
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize, const SToken* pLanguage,
                                bool orReplace) {
X
Xiaoyu Wang 已提交
1830
  CHECK_PARSER_STATUS(pCxt);
1831
  if (pLibPath->n <= 2) {
1832
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1833 1834
    return NULL;
  }
1835 1836 1837 1838
  int8_t language = 0;
  if (TSDB_CODE_SUCCESS != convertUdfLanguageType(pCxt, pLanguage, &language)) {
    return NULL;
  }
1839
  SCreateFunctionStmt* pStmt = (SCreateFunctionStmt*)nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1840
  CHECK_OUT_OF_MEM(pStmt);
1841
  pStmt->orReplace = orReplace;
1842
  pStmt->ignoreExists = ignoreExists;
1843
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1844
  pStmt->isAgg = aggFunc;
1845
  COPY_STRING_FORM_STR_TOKEN(pStmt->libraryPath, pLibPath);
1846 1847
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
1848
  pStmt->language = language;
1849
  return (SNode*)pStmt;
1850 1851
}

1852
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
X
Xiaoyu Wang 已提交
1853
  CHECK_PARSER_STATUS(pCxt);
1854
  SDropFunctionStmt* pStmt = (SDropFunctionStmt*)nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1855
  CHECK_OUT_OF_MEM(pStmt);
1856
  pStmt->ignoreNotExists = ignoreNotExists;
1857
  COPY_STRING_FORM_ID_TOKEN(pStmt->funcName, pFuncName);
1858
  return (SNode*)pStmt;
1859 1860
}

1861
SNode* createStreamOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
1862
  CHECK_PARSER_STATUS(pCxt);
1863
  SStreamOptions* pOptions = (SStreamOptions*)nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
1864
  CHECK_OUT_OF_MEM(pOptions);
5
54liuyao 已提交
1865
  pOptions->triggerType = STREAM_TRIGGER_WINDOW_CLOSE;
1866
  pOptions->fillHistory = STREAM_DEFAULT_FILL_HISTORY;
X
Xiaoyu Wang 已提交
1867
  pOptions->ignoreExpired = STREAM_DEFAULT_IGNORE_EXPIRED;
1868
  pOptions->ignoreUpdate = STREAM_DEFAULT_IGNORE_UPDATE;
1869 1870 1871
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
static int8_t getTriggerType(uint32_t tokenType) {
  switch (tokenType) {
    case TK_AT_ONCE:
      return STREAM_TRIGGER_AT_ONCE;
    case TK_WINDOW_CLOSE:
      return STREAM_TRIGGER_WINDOW_CLOSE;
    case TK_MAX_DELAY:
      return STREAM_TRIGGER_MAX_DELAY;
    default:
      break;
  }
  return STREAM_TRIGGER_WINDOW_CLOSE;
}

SNode* setStreamOptions(SAstCreateContext* pCxt, SNode* pOptions, EStreamOptionsSetFlag setflag, SToken* pToken,
                        SNode* pNode) {
  SStreamOptions* pStreamOptions = (SStreamOptions*)pOptions;
  if (BIT_FLAG_TEST_MASK(setflag, pStreamOptions->setFlag)) {
    pCxt->errCode =
        generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_SYNTAX_ERROR, "stream options each item is only set once");
    return pOptions;
  }

  switch (setflag) {
    case SOPT_TRIGGER_TYPE_SET:
      pStreamOptions->triggerType = getTriggerType(pToken->type);
      if (STREAM_TRIGGER_MAX_DELAY == pStreamOptions->triggerType) {
        pStreamOptions->pDelay = pNode;
      }
      break;
    case SOPT_WATERMARK_SET:
      pStreamOptions->pWatermark = pNode;
      break;
    case SOPT_DELETE_MARK_SET:
      pStreamOptions->pDeleteMark = pNode;
      break;
    case SOPT_FILL_HISTORY_SET:
      pStreamOptions->fillHistory = taosStr2Int8(pToken->z, NULL, 10);
      break;
    case SOPT_IGNORE_EXPIRED_SET:
      pStreamOptions->ignoreExpired = taosStr2Int8(pToken->z, NULL, 10);
      break;
    case SOPT_IGNORE_UPDATE_SET:
      pStreamOptions->ignoreUpdate = taosStr2Int8(pToken->z, NULL, 10);
      break;
    default:
      break;
  }
  BIT_FLAG_SET_MASK(pStreamOptions->setFlag, setflag);

  return pOptions;
}

1925
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pStreamName, SNode* pRealTable,
1926
                              SNode* pOptions, SNodeList* pTags, SNode* pSubtable, SNode* pQuery, SNodeList* pCols) {
X
Xiaoyu Wang 已提交
1927
  CHECK_PARSER_STATUS(pCxt);
1928 1929 1930
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
1931
  SCreateStreamStmt* pStmt = (SCreateStreamStmt*)nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1932
  CHECK_OUT_OF_MEM(pStmt);
1933
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1934 1935 1936
  strcpy(pStmt->targetDbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->targetTabName, ((SRealTableNode*)pRealTable)->table.tableName);
  nodesDestroyNode(pRealTable);
1937 1938 1939
  pStmt->ignoreExists = ignoreExists;
  pStmt->pOptions = (SStreamOptions*)pOptions;
  pStmt->pQuery = pQuery;
1940 1941
  pStmt->pTags = pTags;
  pStmt->pSubtable = pSubtable;
1942
  pStmt->pCols = pCols;
1943
  return (SNode*)pStmt;
1944 1945
}

1946
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
X
Xiaoyu Wang 已提交
1947
  CHECK_PARSER_STATUS(pCxt);
1948 1949 1950
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
1951
  SDropStreamStmt* pStmt = (SDropStreamStmt*)nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1952
  CHECK_OUT_OF_MEM(pStmt);
1953
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
1954 1955
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1956 1957
}

1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
SNode* createPauseStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pStreamName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
  SPauseStreamStmt* pStmt = (SPauseStreamStmt*)nodesMakeNode(QUERY_NODE_PAUSE_STREAM_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

SNode* createResumeStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, bool ignoreUntreated,
                              SToken* pStreamName) {
  CHECK_PARSER_STATUS(pCxt);
  if (!checkStreamName(pCxt, pStreamName)) {
    return NULL;
  }
  SResumeStreamStmt* pStmt = (SResumeStreamStmt*)nodesMakeNode(QUERY_NODE_RESUME_STREAM_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  COPY_STRING_FORM_ID_TOKEN(pStmt->streamName, pStreamName);
  pStmt->ignoreNotExists = ignoreNotExists;
  pStmt->ignoreUntreated = ignoreUntreated;
  return (SNode*)pStmt;
}

1984
SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
X
Xiaoyu Wang 已提交
1985
  CHECK_PARSER_STATUS(pCxt);
1986
  SKillStmt* pStmt = (SKillStmt*)nodesMakeNode(type);
1987
  CHECK_OUT_OF_MEM(pStmt);
wafwerar's avatar
wafwerar 已提交
1988
  pStmt->targetId = taosStr2Int32(pId->z, NULL, 10);
1989
  return (SNode*)pStmt;
1990 1991
}

X
Xiaoyu Wang 已提交
1992 1993 1994 1995
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 已提交
1996
  trimString(pQueryId->z, pQueryId->n, pStmt->queryId, sizeof(pStmt->queryId) - 1);
X
Xiaoyu Wang 已提交
1997 1998 1999
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
2000 2001
SNode* createBalanceVgroupStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
2002
  SBalanceVgroupStmt* pStmt = (SBalanceVgroupStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_STMT);
X
Xiaoyu Wang 已提交
2003 2004 2005 2006
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

C
cadem 已提交
2007 2008 2009 2010 2011 2012 2013
SNode* createBalanceVgroupLeaderStmt(SAstCreateContext* pCxt) {
  CHECK_PARSER_STATUS(pCxt);
  SBalanceVgroupLeaderStmt* pStmt = (SBalanceVgroupLeaderStmt*)nodesMakeNode(QUERY_NODE_BALANCE_VGROUP_LEADER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return (SNode*)pStmt;
}

2014
SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
X
Xiaoyu Wang 已提交
2015
  CHECK_PARSER_STATUS(pCxt);
2016
  SMergeVgroupStmt* pStmt = (SMergeVgroupStmt*)nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
2017
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
2018 2019 2020
  pStmt->vgId1 = taosStr2Int32(pVgId1->z, NULL, 10);
  pStmt->vgId2 = taosStr2Int32(pVgId2->z, NULL, 10);
  return (SNode*)pStmt;
2021 2022 2023
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
X
Xiaoyu Wang 已提交
2024
  CHECK_PARSER_STATUS(pCxt);
2025
  SRedistributeVgroupStmt* pStmt = (SRedistributeVgroupStmt*)nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
2026
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
2027 2028 2029
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  pStmt->pDnodes = pDnodes;
  return (SNode*)pStmt;
2030 2031 2032
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
X
Xiaoyu Wang 已提交
2033
  CHECK_PARSER_STATUS(pCxt);
2034
  SSplitVgroupStmt* pStmt = (SSplitVgroupStmt*)nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
2035
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
2036 2037
  pStmt->vgId = taosStr2Int32(pVgId->z, NULL, 10);
  return (SNode*)pStmt;
2038 2039 2040
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
X
Xiaoyu Wang 已提交
2041
  CHECK_PARSER_STATUS(pCxt);
2042 2043 2044 2045
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
2046

X
Xiaoyu Wang 已提交
2047 2048
SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
                       SNode* pTagCond) {
X
Xiaoyu Wang 已提交
2049
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
2050
  if (!checkDbName(pCxt, &pPrivLevel->first, false) || !checkUserName(pCxt, pUserName)) {
2051 2052
    return NULL;
  }
2053
  SGrantStmt* pStmt = (SGrantStmt*)nodesMakeNode(QUERY_NODE_GRANT_STMT);
2054 2055
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
X
Xiaoyu Wang 已提交
2056 2057 2058 2059
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
  }
2060
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
X
Xiaoyu Wang 已提交
2061
  pStmt->pTagCond = pTagCond;
2062 2063 2064
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
2065 2066
SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, STokenPair* pPrivLevel, SToken* pUserName,
                        SNode* pTagCond) {
X
Xiaoyu Wang 已提交
2067
  CHECK_PARSER_STATUS(pCxt);
X
Xiaoyu Wang 已提交
2068
  if (!checkDbName(pCxt, &pPrivLevel->first, false) || !checkUserName(pCxt, pUserName)) {
2069 2070
    return NULL;
  }
2071
  SRevokeStmt* pStmt = (SRevokeStmt*)nodesMakeNode(QUERY_NODE_REVOKE_STMT);
2072 2073
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
X
Xiaoyu Wang 已提交
2074 2075 2076 2077
  COPY_STRING_FORM_ID_TOKEN(pStmt->objName, &pPrivLevel->first);
  if (TK_NK_NIL != pPrivLevel->second.type && TK_NK_STAR != pPrivLevel->second.type) {
    COPY_STRING_FORM_ID_TOKEN(pStmt->tabName, &pPrivLevel->second);
  }
2078
  COPY_STRING_FORM_ID_TOKEN(pStmt->userName, pUserName);
X
Xiaoyu Wang 已提交
2079
  pStmt->pTagCond = pTagCond;
2080 2081
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
2082

2083
SNode* createFuncForDelete(SAstCreateContext* pCxt, const char* pFuncName) {
2084
  SFunctionNode* pFunc = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
X
Xiaoyu Wang 已提交
2085
  CHECK_OUT_OF_MEM(pFunc);
X
Xiaoyu Wang 已提交
2086
  snprintf(pFunc->functionName, sizeof(pFunc->functionName), "%s", pFuncName);
2087
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createPrimaryKeyCol(pCxt, NULL))) {
2088
    nodesDestroyNode((SNode*)pFunc);
X
Xiaoyu Wang 已提交
2089 2090 2091 2092 2093
    CHECK_OUT_OF_MEM(NULL);
  }
  return (SNode*)pFunc;
}

X
Xiaoyu Wang 已提交
2094 2095
SNode* createDeleteStmt(SAstCreateContext* pCxt, SNode* pTable, SNode* pWhere) {
  CHECK_PARSER_STATUS(pCxt);
2096
  SDeleteStmt* pStmt = (SDeleteStmt*)nodesMakeNode(QUERY_NODE_DELETE_STMT);
X
Xiaoyu Wang 已提交
2097 2098 2099
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pFromTable = pTable;
  pStmt->pWhere = pWhere;
2100 2101 2102 2103
  pStmt->pCountFunc = createFuncForDelete(pCxt, "count");
  pStmt->pFirstFunc = createFuncForDelete(pCxt, "first");
  pStmt->pLastFunc = createFuncForDelete(pCxt, "last");
  if (NULL == pStmt->pCountFunc || NULL == pStmt->pFirstFunc || NULL == pStmt->pLastFunc) {
2104
    nodesDestroyNode((SNode*)pStmt);
X
Xiaoyu Wang 已提交
2105 2106
    CHECK_OUT_OF_MEM(NULL);
  }
X
Xiaoyu Wang 已提交
2107 2108
  return (SNode*)pStmt;
}
2109 2110 2111 2112 2113 2114 2115 2116

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;
2117 2118 2119 2120 2121
  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);
  }
2122 2123
  return (SNode*)pStmt;
}