parAstCreater.c 50.8 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"
X
Xiaoyu Wang 已提交
21
#include "ttime.h"
22

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

X
Xiaoyu Wang 已提交
32 33
#define CHECK_RAW_EXPR_NODE(node)                                  \
  do {                                                             \
34
    if (NULL == (node) || QUERY_NODE_RAW_EXPR != nodeType(node)) { \
35
      pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;                  \
X
Xiaoyu Wang 已提交
36 37
      return NULL;                                                 \
    }                                                              \
38 39
  } while (0)

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

42 43
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
  pCxt->pQueryCxt = pParseCxt;
44 45
  pCxt->msgBuf.buf = pParseCxt->pMsg;
  pCxt->msgBuf.len = pParseCxt->msgLen;
46 47
  pCxt->notSupport = false;
  pCxt->pRootNode = NULL;
D
dapan1121 已提交
48
  pCxt->placeholderNo = 0;
X
Xiaoyu Wang 已提交
49
  pCxt->pPlaceholderValues = NULL;
50
  pCxt->errCode = TSDB_CODE_SUCCESS;
51 52
}

X
Xiaoyu Wang 已提交
53 54 55 56 57 58
static void copyStringFormStringToken(SToken* pToken, char* pBuf, int32_t len) {
  if (pToken->n > 2) {
    strncpy(pBuf, pToken->z + 1, TMIN(pToken->n - 2, len - 1));
  }
}

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

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

81 82 83 84 85 86 87 88 89 90 91 92 93
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;
}

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

static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
  if (NULL == pEp) {
113
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
114
  } else if (pEp->n >= TSDB_FQDN_LEN + 2 + 6) {  // format 'fqdn:port'
115
    pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
X
Xiaoyu Wang 已提交
116 117 118 119 120 121 122
  } else {
    char ep[TSDB_FQDN_LEN + 2 + 6];
    strncpy(ep, pEp->z, pEp->n);
    strdequote(ep);
    strtrim(ep);
    char* pColon = strchr(ep, ':');
    if (NULL == pColon) {
123
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ENDPOINT);
X
Xiaoyu Wang 已提交
124 125 126 127
    } else {
      strncpy(pFqdn, ep, pColon - ep);
      *pPort = strtol(pColon + 1, NULL, 10);
      if (*pPort >= UINT16_MAX || *pPort <= 0) {
128
        pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
X
Xiaoyu Wang 已提交
129 130
      }
    }
131
  }
132
  return TSDB_CODE_SUCCESS == pCxt->errCode;
133 134 135 136
}

static bool checkFqdn(SAstCreateContext* pCxt, const SToken* pFqdn) {
  if (NULL == pFqdn) {
137
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
138 139
  } else {
    if (pFqdn->n >= TSDB_FQDN_LEN) {
140
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
X
Xiaoyu Wang 已提交
141
    }
142
  }
143
  return TSDB_CODE_SUCCESS == pCxt->errCode;
144 145 146 147
}

static bool checkPort(SAstCreateContext* pCxt, const SToken* pPortToken, int32_t* pPort) {
  if (NULL == pPortToken) {
148
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
149 150 151
  } else {
    *pPort = strtol(pPortToken->z, NULL, 10);
    if (*pPort >= UINT16_MAX || *pPort <= 0) {
152
      pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
X
Xiaoyu Wang 已提交
153
    }
154
  }
155
  return TSDB_CODE_SUCCESS == pCxt->errCode;
156 157
}

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

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

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

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

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
SNode* createRawExprNode(SAstCreateContext* pCxt, const SToken* pToken, SNode* pNode) {
  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) {
  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) {
  CHECK_RAW_EXPR_NODE(pNode);
219
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
220
  SNode*        pExpr = pRawExpr->pNode;
D
dapan1121 已提交
221
  if (nodesIsExprNode(pExpr)) {
222 223 224
    int32_t len = TMIN(sizeof(((SExprNode*)pExpr)->aliasName) - 1, pRawExpr->n);
    strncpy(((SExprNode*)pExpr)->aliasName, pRawExpr->p, len);
    ((SExprNode*)pExpr)->aliasName[len] = '\0';
D
dapan1121 已提交
225
  }
wafwerar's avatar
wafwerar 已提交
226
  taosMemoryFreeClear(pNode);
227
  return pExpr;
228 229 230
}

SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
231
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
232
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
233 234
    return nil_token;
  }
235
  SRawExprNode* target = (SRawExprNode*)pNode;
X
Xiaoyu Wang 已提交
236
  SToken        t = {.type = 0, .z = target->p, .n = target->n};
237 238 239
  return t;
}

240 241 242
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
243
  pCxt->errCode = nodesListAppend(list, pNode);
X
Xiaoyu Wang 已提交
244
  return list;
245 246 247
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
248
  pCxt->errCode = nodesListAppend(pList, pNode);
X
Xiaoyu Wang 已提交
249
  return pList;
250 251
}

X
Xiaoyu Wang 已提交
252
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
253
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
254 255 256 257
    return NULL;
  }
  SColumnNode* col = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(col);
258 259
  if (NULL != pTableAlias) {
    strncpy(col->tableAlias, pTableAlias->z, pTableAlias->n);
260 261
  }
  strncpy(col->colName, pColumnName->z, pColumnName->n);
262
  return (SNode*)col;
263
}
264

265 266 267
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
268 269 270 271
  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 已提交
272
  }
273
  CHECK_OUT_OF_MEM(val->literal);
274
  val->node.resType.type = dataType;
X
Xiaoyu Wang 已提交
275
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
276 277 278
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  }
279 280
  val->isDuration = false;
  val->translate = false;
281 282 283 284 285 286
  return (SNode*)val;
}

SNode* createDurationValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
287 288 289
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = true;
290
  val->translate = false;
291 292 293
  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;
294
  return (SNode*)val;
295 296
}

X
Xiaoyu Wang 已提交
297
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
298 299 300 301
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
302 303 304 305 306 307 308 309 310 311 312 313
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
  val->literal = strdup(pCxt->pQueryCxt->db);
  CHECK_OUT_OF_MEM(val->literal);
  val->isDuration = false;
  val->translate = false;
  val->node.resType.type = TSDB_DATA_TYPE_BINARY;
  val->node.resType.bytes = strlen(val->literal);
  val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  return (SNode*)val;
}

314
SNode* createPlaceholderValueNode(SAstCreateContext* pCxt, const SToken* pLiteral) {
315 316
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
317 318
  val->literal = strndup(pLiteral->z, pLiteral->n);
  CHECK_OUT_OF_MEM(val->literal);
D
dapan1121 已提交
319
  val->placeholderNo = ++pCxt->placeholderNo;
X
Xiaoyu Wang 已提交
320 321 322 323 324 325 326 327
  if (NULL == pCxt->pPlaceholderValues) {
    pCxt->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    if (NULL == pCxt->pPlaceholderValues) {
      nodesDestroyNode(val);
      return NULL;
    }
  }
  taosArrayPush(pCxt->pPlaceholderValues, &val);
328 329 330
  return (SNode*)val;
}

331 332 333 334 335
SNode* createLogicConditionNode(SAstCreateContext* pCxt, ELogicConditionType type, SNode* pParam1, SNode* pParam2) {
  SLogicConditionNode* cond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  CHECK_OUT_OF_MEM(cond);
  cond->condType = type;
  cond->pParameterList = nodesMakeList();
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
  if ((QUERY_NODE_LOGIC_CONDITION == nodeType(pParam1) && type != ((SLogicConditionNode*)pParam1)->condType) ||
      (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam2) && type != ((SLogicConditionNode*)pParam2)->condType)) {
    nodesListAppend(cond->pParameterList, pParam1);
    nodesListAppend(cond->pParameterList, pParam2);
  } else {
    if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam1)) {
      nodesListAppendList(cond->pParameterList, ((SLogicConditionNode*)pParam1)->pParameterList);
      ((SLogicConditionNode*)pParam1)->pParameterList = NULL;
      nodesDestroyNode(pParam1);
    } else {
      nodesListAppend(cond->pParameterList, pParam1);
    }
    if (QUERY_NODE_LOGIC_CONDITION == nodeType(pParam2)) {
      nodesListAppendList(cond->pParameterList, ((SLogicConditionNode*)pParam2)->pParameterList);
      ((SLogicConditionNode*)pParam2)->pParameterList = NULL;
      nodesDestroyNode(pParam2);
    } else {
      nodesListAppend(cond->pParameterList, pParam2);
    }
  }
356 357 358 359 360 361 362 363 364 365 366 367 368 369
  return (SNode*)cond;
}

SNode* createOperatorNode(SAstCreateContext* pCxt, EOperatorType type, SNode* pLeft, SNode* pRight) {
  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) {
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_AND,
X
Xiaoyu Wang 已提交
370 371
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
372 373 374
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
X
Xiaoyu Wang 已提交
375 376
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR, createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft),
                                  createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
377 378
}

X
Xiaoyu Wang 已提交
379 380 381 382 383 384 385 386
static SNode* createPrimaryKeyCol(SAstCreateContext* pCxt) {
  SColumnNode* pCol = nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(pCol);
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(pCol->colName, PK_TS_COL_INTERNAL_NAME);
  return (SNode*)pCol;
}

387
SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
X
Xiaoyu Wang 已提交
388 389 390
  if (0 == strncasecmp("_rowts", pFuncName->z, pFuncName->n) || 0 == strncasecmp("_c0", pFuncName->z, pFuncName->n)) {
    return createPrimaryKeyCol(pCxt);
  }
391 392 393 394 395 396 397
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strncpy(func->functionName, pFuncName->z, pFuncName->n);
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

398 399 400 401 402
SNode* createCastFunctionNode(SAstCreateContext* pCxt, SNode* pExpr, SDataType dt) {
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  strcpy(func->functionName, "cast");
  func->node.resType = dt;
403 404 405 406
  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;
407
  }
408 409 410 411
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

412 413 414 415 416
SNode* createNodeListNode(SAstCreateContext* pCxt, SNodeList* pList) {
  SNodeListNode* list = (SNodeListNode*)nodesMakeNode(QUERY_NODE_NODE_LIST);
  CHECK_OUT_OF_MEM(list);
  list->pNodeList = pList;
  return (SNode*)list;
417 418
}

X
Xiaoyu Wang 已提交
419 420 421 422 423 424 425 426 427 428
SNode* createNodeListNodeEx(SAstCreateContext* pCxt, SNode* p1, SNode* p2) {
  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 已提交
429 430
SNode* createRealTableNode(SAstCreateContext* pCxt, SToken* pDbName, SToken* pTableName, SToken* pTableAlias) {
  if (!checkDbName(pCxt, pDbName, true) || !checkTableName(pCxt, pTableName) || !checkTableName(pCxt, pTableAlias)) {
431 432 433 434 435
    return NULL;
  }
  SRealTableNode* realTable = (SRealTableNode*)nodesMakeNode(QUERY_NODE_REAL_TABLE);
  CHECK_OUT_OF_MEM(realTable);
  if (NULL != pDbName) {
436
    strncpy(realTable->table.dbName, pDbName->z, pDbName->n);
437 438 439
  } else {
    strcpy(realTable->table.dbName, pCxt->pQueryCxt->db);
  }
440
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
441 442 443
    strncpy(realTable->table.tableAlias, pTableAlias->z, pTableAlias->n);
  } else {
    strncpy(realTable->table.tableAlias, pTableName->z, pTableName->n);
444
  }
X
Xiaoyu Wang 已提交
445
  strncpy(realTable->table.tableName, pTableName->z, pTableName->n);
446
  return (SNode*)realTable;
447
}
448

449 450 451 452
SNode* createTempTableNode(SAstCreateContext* pCxt, SNode* pSubquery, const SToken* pTableAlias) {
  STempTableNode* tempTable = (STempTableNode*)nodesMakeNode(QUERY_NODE_TEMP_TABLE);
  CHECK_OUT_OF_MEM(tempTable);
  tempTable->pSubquery = pSubquery;
453
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
454
    strncpy(tempTable->table.tableAlias, pTableAlias->z, pTableAlias->n);
X
Xiaoyu Wang 已提交
455 456 457 458 459
  } else {
    sprintf(tempTable->table.tableAlias, "%p", tempTable);
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
460
  }
461
  return (SNode*)tempTable;
462 463
}

464 465 466 467
SNode* createJoinTableNode(SAstCreateContext* pCxt, EJoinType type, SNode* pLeft, SNode* pRight, SNode* pJoinCond) {
  SJoinTableNode* joinTable = (SJoinTableNode*)nodesMakeNode(QUERY_NODE_JOIN_TABLE);
  CHECK_OUT_OF_MEM(joinTable);
  joinTable->joinType = type;
468 469
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
470
  joinTable->pOnCond = pJoinCond;
471
  return (SNode*)joinTable;
472
}
473

474
SNode* createLimitNode(SAstCreateContext* pCxt, const SToken* pLimit, const SToken* pOffset) {
475 476
  SLimitNode* limitNode = (SLimitNode*)nodesMakeNode(QUERY_NODE_LIMIT);
  CHECK_OUT_OF_MEM(limitNode);
477 478 479 480
  limitNode->limit = strtol(pLimit->z, NULL, 10);
  if (NULL != pOffset) {
    limitNode->offset = strtol(pOffset->z, NULL, 10);
  }
481
  return (SNode*)limitNode;
482 483
}

484 485 486
SNode* createOrderByExprNode(SAstCreateContext* pCxt, SNode* pExpr, EOrder order, ENullOrder nullOrder) {
  SOrderByExprNode* orderByExpr = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR);
  CHECK_OUT_OF_MEM(orderByExpr);
487
  orderByExpr->pExpr = pExpr;
488
  orderByExpr->order = order;
489 490 491
  if (NULL_ORDER_DEFAULT == nullOrder) {
    nullOrder = (ORDER_ASC == order ? NULL_ORDER_FIRST : NULL_ORDER_LAST);
  }
492
  orderByExpr->nullOrder = nullOrder;
493 494 495
  return (SNode*)orderByExpr;
}

X
Xiaoyu Wang 已提交
496
SNode* createSessionWindowNode(SAstCreateContext* pCxt, SNode* pCol, SNode* pGap) {
497 498
  SSessionWindowNode* session = (SSessionWindowNode*)nodesMakeNode(QUERY_NODE_SESSION_WINDOW);
  CHECK_OUT_OF_MEM(session);
X
Xiaoyu Wang 已提交
499 500
  session->pCol = (SColumnNode*)pCol;
  session->pGap = (SValueNode*)pGap;
501 502 503
  return (SNode*)session;
}

504
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
505 506
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
X
Xiaoyu Wang 已提交
507
  state->pCol = createPrimaryKeyCol(pCxt);
508 509 510 511
  if (NULL == state->pCol) {
    nodesDestroyNode(state);
    CHECK_OUT_OF_MEM(state->pCol);
  }
512
  state->pExpr = pExpr;
513 514 515
  return (SNode*)state;
}

X
Xiaoyu Wang 已提交
516 517
SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding,
                                SNode* pFill) {
518 519
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
X
Xiaoyu Wang 已提交
520
  interval->pCol = createPrimaryKeyCol(pCxt);
X
bugfix  
Xiaoyu Wang 已提交
521 522 523 524
  if (NULL == interval->pCol) {
    nodesDestroyNode(interval);
    CHECK_OUT_OF_MEM(interval->pCol);
  }
525 526 527 528 529 530 531 532 533 534 535 536
  interval->pInterval = pInterval;
  interval->pOffset = pOffset;
  interval->pSliding = pSliding;
  interval->pFill = pFill;
  return (SNode*)interval;
}

SNode* createFillNode(SAstCreateContext* pCxt, EFillMode mode, SNode* pValues) {
  SFillNode* fill = (SFillNode*)nodesMakeNode(QUERY_NODE_FILL);
  CHECK_OUT_OF_MEM(fill);
  fill->mode = mode;
  fill->pValues = pValues;
X
Xiaoyu Wang 已提交
537 538 539 540 541 542
  fill->pWStartTs = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == fill->pWStartTs) {
    nodesDestroyNode(fill);
    CHECK_OUT_OF_MEM(fill->pWStartTs);
  }
  strcpy(((SFunctionNode*)fill->pWStartTs)->functionName, "_wstartts");
543
  return (SNode*)fill;
544
}
545

546 547 548 549 550 551 552 553 554
SNode* createGroupingSetNode(SAstCreateContext* pCxt, SNode* pNode) {
  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;
}

555
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) {
556
  if (NULL == pNode || TSDB_CODE_SUCCESS != pCxt->errCode) {
X
Xiaoyu Wang 已提交
557 558
    return pNode;
  }
D
dapan1121 已提交
559 560 561
  int32_t len = TMIN(sizeof(((SExprNode*)pNode)->aliasName) - 1, pAlias->n);
  strncpy(((SExprNode*)pNode)->aliasName, pAlias->z, len);
  ((SExprNode*)pNode)->aliasName[len] = '\0';
562
  return pNode;
563 564
}

565 566
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
567
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
568 569 570
  }
  return pStmt;
}
571

572 573
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
574
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
575 576
  }
  return pStmt;
577 578
}

579 580
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
581
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
582 583 584
  }
  return pStmt;
}
585

586 587
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
588
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
589 590
  }
  return pStmt;
591 592
}

593 594
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
595
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
596 597 598 599 600 601
  }
  return pStmt;
}

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
602
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
603 604 605
  }
  return pStmt;
}
606

607 608
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
609
    ((SSelectStmt*)pStmt)->pSlimit = (SLimitNode*)pSlimit;
610 611
  }
  return pStmt;
612 613
}

614 615
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
X
Xiaoyu Wang 已提交
616
    ((SSelectStmt*)pStmt)->pLimit = (SLimitNode*)pLimit;
617
  }
618
  return pStmt;
619 620 621 622
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
623
  CHECK_OUT_OF_MEM(select);
624
  select->isDistinct = isDistinct;
625 626
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
627
  sprintf(select->stmtName, "%p", select);
X
Xiaoyu Wang 已提交
628
  select->isTimeOrderQuery = true;
629
  return (SNode*)select;
630 631 632
}

SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
633 634 635
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
636 637 638
  setOp->pLeft = pLeft;
  setOp->pRight = pRight;
  return (SNode*)setOp;
639 640
}

X
Xiaoyu Wang 已提交
641
SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
642
  SDatabaseOptions* pOptions = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
643
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
  pOptions->buffer = TSDB_DEFAULT_BUFFER_PER_VNODE;
  pOptions->cachelast = TSDB_DEFAULT_CACHE_LAST_ROW;
  pOptions->compressionLevel = TSDB_DEFAULT_COMP_LEVEL;
  pOptions->daysPerFile = TSDB_DEFAULT_DAYS_PER_FILE;
  pOptions->fsyncPeriod = TSDB_DEFAULT_FSYNC_PERIOD;
  pOptions->maxRowsPerBlock = TSDB_DEFAULT_MAXROWS_FBLOCK;
  pOptions->minRowsPerBlock = TSDB_DEFAULT_MINROWS_FBLOCK;
  pOptions->keep[0] = TSDB_DEFAULT_KEEP;
  pOptions->keep[1] = TSDB_DEFAULT_KEEP;
  pOptions->keep[2] = TSDB_DEFAULT_KEEP;
  pOptions->pages = TSDB_DEFAULT_PAGES_PER_VNODE;
  pOptions->pagesize = TSDB_DEFAULT_PAGESIZE_PER_VNODE;
  pOptions->precision = TSDB_DEFAULT_PRECISION;
  pOptions->replica = TSDB_DEFAULT_DB_REPLICA;
  pOptions->strict = TSDB_DEFAULT_DB_STRICT;
  pOptions->walLevel = TSDB_DEFAULT_WAL_LEVEL;
  pOptions->numOfVgroups = TSDB_DEFAULT_VN_PER_DB;
  pOptions->singleStable = TSDB_DEFAULT_DB_SINGLE_STABLE;
X
Xiaoyu Wang 已提交
662
  return (SNode*)pOptions;
663 664
}

X
Xiaoyu Wang 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) {
  SDatabaseOptions* pOptions = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->buffer = -1;
  pOptions->cachelast = -1;
  pOptions->compressionLevel = -1;
  pOptions->daysPerFile = -1;
  pOptions->fsyncPeriod = -1;
  pOptions->maxRowsPerBlock = -1;
  pOptions->minRowsPerBlock = -1;
  pOptions->keep[0] = -1;
  pOptions->keep[1] = -1;
  pOptions->keep[2] = -1;
  pOptions->pages = -1;
  pOptions->pagesize = -1;
  pOptions->precision = -1;
  pOptions->replica = -1;
  pOptions->strict = -1;
  pOptions->walLevel = -1;
  pOptions->numOfVgroups = -1;
  pOptions->singleStable = -1;
  return (SNode*)pOptions;
}

SNode* setDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, EDatabaseOptionType type, void* pVal) {
  switch (type) {
    case DB_OPTION_BUFFER:
      ((SDatabaseOptions*)pOptions)->buffer = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
693 694
      break;
    case DB_OPTION_CACHELAST:
X
Xiaoyu Wang 已提交
695
      ((SDatabaseOptions*)pOptions)->cachelast = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
696 697
      break;
    case DB_OPTION_COMP:
X
Xiaoyu Wang 已提交
698
      ((SDatabaseOptions*)pOptions)->compressionLevel = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
699
      break;
X
Xiaoyu Wang 已提交
700 701 702
    case DB_OPTION_DAYS: {
      SToken* pToken = pVal;
      if (TK_NK_INTEGER == pToken->type) {
X
Xiaoyu Wang 已提交
703
        ((SDatabaseOptions*)pOptions)->daysPerFile = strtol(pToken->z, NULL, 10) * 1440;
X
Xiaoyu Wang 已提交
704 705 706
      } else {
        ((SDatabaseOptions*)pOptions)->pDaysPerFile = (SValueNode*)createDurationValueNode(pCxt, pToken);
      }
X
Xiaoyu Wang 已提交
707
      break;
X
Xiaoyu Wang 已提交
708
    }
X
Xiaoyu Wang 已提交
709
    case DB_OPTION_FSYNC:
X
Xiaoyu Wang 已提交
710
      ((SDatabaseOptions*)pOptions)->fsyncPeriod = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
711 712
      break;
    case DB_OPTION_MAXROWS:
X
Xiaoyu Wang 已提交
713
      ((SDatabaseOptions*)pOptions)->maxRowsPerBlock = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
714 715
      break;
    case DB_OPTION_MINROWS:
X
Xiaoyu Wang 已提交
716
      ((SDatabaseOptions*)pOptions)->minRowsPerBlock = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
717 718
      break;
    case DB_OPTION_KEEP:
X
Xiaoyu Wang 已提交
719 720 721 722 723 724 725
      ((SDatabaseOptions*)pOptions)->pKeep = pVal;
      break;
    case DB_OPTION_PAGES:
      ((SDatabaseOptions*)pOptions)->pages = strtol(((SToken*)pVal)->z, NULL, 10);
      break;
    case DB_OPTION_PAGESIZE:
      ((SDatabaseOptions*)pOptions)->pagesize = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
726 727
      break;
    case DB_OPTION_PRECISION:
X
Xiaoyu Wang 已提交
728 729
      copyStringFormStringToken((SToken*)pVal, ((SDatabaseOptions*)pOptions)->precisionStr,
                                sizeof(((SDatabaseOptions*)pOptions)->precisionStr));
X
Xiaoyu Wang 已提交
730 731
      break;
    case DB_OPTION_REPLICA:
X
Xiaoyu Wang 已提交
732
      ((SDatabaseOptions*)pOptions)->replica = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
733
      break;
X
Xiaoyu Wang 已提交
734 735
    case DB_OPTION_STRICT:
      ((SDatabaseOptions*)pOptions)->strict = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
736 737
      break;
    case DB_OPTION_WAL:
X
Xiaoyu Wang 已提交
738
      ((SDatabaseOptions*)pOptions)->walLevel = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
739 740
      break;
    case DB_OPTION_VGROUPS:
X
Xiaoyu Wang 已提交
741
      ((SDatabaseOptions*)pOptions)->numOfVgroups = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
742 743
      break;
    case DB_OPTION_SINGLE_STABLE:
X
Xiaoyu Wang 已提交
744
      ((SDatabaseOptions*)pOptions)->singleStable = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
745 746
      break;
    case DB_OPTION_RETENTIONS:
X
Xiaoyu Wang 已提交
747
      ((SDatabaseOptions*)pOptions)->pRetentions = pVal;
X
Xiaoyu Wang 已提交
748 749 750
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
751
  }
X
Xiaoyu Wang 已提交
752
  return pOptions;
X
Xiaoyu Wang 已提交
753 754
}

X
Xiaoyu Wang 已提交
755 756 757 758 759 760 761 762 763 764 765
SNode* setAlterDatabaseOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
  switch (pAlterOption->type) {
    case DB_OPTION_KEEP:
    case DB_OPTION_RETENTIONS:
      return setDatabaseOption(pCxt, pOptions, pAlterOption->type, pAlterOption->pList);
    default:
      break;
  }
  return setDatabaseOption(pCxt, pOptions, pAlterOption->type, &pAlterOption->val);
}

X
Xiaoyu Wang 已提交
766
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
767
  if (!checkDbName(pCxt, pDbName, false)) {
768 769 770 771 772 773
    return NULL;
  }
  SCreateDatabaseStmt* pStmt = (SCreateDatabaseStmt*)nodesMakeNode(QUERY_NODE_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
774
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
775
  return (SNode*)pStmt;
776
}
X
Xiaoyu Wang 已提交
777

X
Xiaoyu Wang 已提交
778
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
779
  if (!checkDbName(pCxt, pDbName, false)) {
780 781 782 783 784 785 786 787 788
    return NULL;
  }
  SDropDatabaseStmt* pStmt = (SDropDatabaseStmt*)nodesMakeNode(QUERY_NODE_DROP_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
789
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
790
  if (!checkDbName(pCxt, pDbName, false)) {
791 792 793 794 795 796 797 798 799
    return NULL;
  }
  SAlterDatabaseStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
800
SNode* createDefaultTableOptions(SAstCreateContext* pCxt) {
801 802
  STableOptions* pOptions = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
803 804 805
  pOptions->delay = TSDB_DEFAULT_ROLLUP_DELAY;
  pOptions->filesFactor = TSDB_DEFAULT_ROLLUP_FILE_FACTOR;
  pOptions->ttl = TSDB_DEFAULT_TABLE_TTL;
806
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
807 808
}

X
Xiaoyu Wang 已提交
809 810 811 812 813 814 815 816 817 818 819
SNode* createAlterTableOptions(SAstCreateContext* pCxt) {
  STableOptions* pOptions = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->delay = -1;
  pOptions->filesFactor = -1;
  pOptions->ttl = -1;
  return (SNode*)pOptions;
}

SNode* setTableOption(SAstCreateContext* pCxt, SNode* pOptions, ETableOptionType type, void* pVal) {
  switch (type) {
X
Xiaoyu Wang 已提交
820
    case TABLE_OPTION_COMMENT:
X
Xiaoyu Wang 已提交
821 822
      copyStringFormStringToken((SToken*)pVal, ((STableOptions*)pOptions)->comment,
                                sizeof(((STableOptions*)pOptions)->comment));
X
Xiaoyu Wang 已提交
823
      break;
X
Xiaoyu Wang 已提交
824 825
    case TABLE_OPTION_DELAY:
      ((STableOptions*)pOptions)->delay = strtol(((SToken*)pVal)->z, NULL, 10);
X
Xiaoyu Wang 已提交
826 827
      break;
    case TABLE_OPTION_FILE_FACTOR:
X
Xiaoyu Wang 已提交
828
      ((STableOptions*)pOptions)->filesFactor = strtod(((SToken*)pVal)->z, NULL);
X
Xiaoyu Wang 已提交
829
      break;
X
Xiaoyu Wang 已提交
830 831 832 833 834 835 836 837
    case TABLE_OPTION_ROLLUP:
      ((STableOptions*)pOptions)->pRollupFuncs = pVal;
      break;
    case TABLE_OPTION_TTL:
      ((STableOptions*)pOptions)->ttl = strtol(((SToken*)pVal)->z, NULL, 10);
      break;
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pVal;
X
Xiaoyu Wang 已提交
838 839 840
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
841
  }
X
Xiaoyu Wang 已提交
842
  return pOptions;
X
Xiaoyu Wang 已提交
843 844
}

X
Xiaoyu Wang 已提交
845 846 847 848
SNode* createColumnDefNode(SAstCreateContext* pCxt, SToken* pColName, SDataType dataType, const SToken* pComment) {
  if (!checkColumnName(pCxt, pColName)) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
849 850 851 852 853
  SColumnDefNode* pCol = (SColumnDefNode*)nodesMakeNode(QUERY_NODE_COLUMN_DEF);
  CHECK_OUT_OF_MEM(pCol);
  strncpy(pCol->colName, pColName->z, pColName->n);
  pCol->dataType = dataType;
  if (NULL != pComment) {
854
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
855
  }
856
  pCol->sma = true;
X
Xiaoyu Wang 已提交
857 858 859
  return (SNode*)pCol;
}

860
SDataType createDataType(uint8_t type) {
X
Xiaoyu Wang 已提交
861
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes};
862 863 864 865
  return dt;
}

SDataType createVarLenDataType(uint8_t type, const SToken* pLen) {
X
Xiaoyu Wang 已提交
866
  SDataType dt = {.type = type, .precision = 0, .scale = 0, .bytes = strtol(pLen->z, NULL, 10)};
867 868 869
  return dt;
}

X
Xiaoyu Wang 已提交
870 871
SNode* createCreateTableStmt(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNodeList* pCols,
                             SNodeList* pTags, SNode* pOptions) {
X
bugfix  
Xiaoyu Wang 已提交
872 873 874
  if (NULL == pRealTable) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
875 876
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
877 878
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
879 880
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
881
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
882
  pStmt->pOptions = (STableOptions*)pOptions;
883
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
884 885
  return (SNode*)pStmt;
}
886

X
Xiaoyu Wang 已提交
887
SNode* createCreateSubTableClause(SAstCreateContext* pCxt, bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable,
X
Xiaoyu Wang 已提交
888
                                  SNodeList* pSpecificTags, SNodeList* pValsOfTags, SNode* pOptions) {
X
bugfix  
Xiaoyu Wang 已提交
889 890 891
  if (NULL == pRealTable) {
    return NULL;
  }
892
  SCreateSubTableClause* pStmt = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
893
  CHECK_OUT_OF_MEM(pStmt);
894 895 896 897
  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);
898 899 900
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
901 902
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
903 904 905 906 907 908 909 910 911 912
  return (SNode*)pStmt;
}

SNode* createCreateMultiTableStmt(SAstCreateContext* pCxt, SNodeList* pSubTables) {
  SCreateMultiTableStmt* pStmt = nodesMakeNode(QUERY_NODE_CREATE_MULTI_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pSubTables = pSubTables;
  return (SNode*)pStmt;
}

913
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
bugfix  
Xiaoyu Wang 已提交
914 915 916
  if (NULL == pRealTable) {
    return NULL;
  }
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
  SDropTableClause* pStmt = nodesMakeNode(QUERY_NODE_DROP_TABLE_CLAUSE);
  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) {
  SDropTableStmt* pStmt = nodesMakeNode(QUERY_NODE_DROP_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->pTables = pTables;
  return (SNode*)pStmt;
}

933 934 935 936 937 938 939 940 941 942
SNode* createDropSuperTableStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
  SDropSuperTableStmt* pStmt = nodesMakeNode(QUERY_NODE_DROP_SUPER_TABLE_STMT);
  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 已提交
943 944 945 946 947 948 949
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 已提交
950
SNode* createAlterTableModifyOptions(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
bugfix  
Xiaoyu Wang 已提交
951 952 953
  if (NULL == pRealTable) {
    return NULL;
  }
954 955 956 957
  SAlterTableStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
X
Xiaoyu Wang 已提交
958
  return createAlterTableStmtFinalize(pRealTable, pStmt);
959 960
}

X
Xiaoyu Wang 已提交
961 962
SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
                                    const SToken* pColName, SDataType dataType) {
X
bugfix  
Xiaoyu Wang 已提交
963 964 965
  if (NULL == pRealTable) {
    return NULL;
  }
966 967 968 969 970
  SAlterTableStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
  strncpy(pStmt->colName, pColName->z, pColName->n);
  pStmt->dataType = dataType;
X
Xiaoyu Wang 已提交
971
  return createAlterTableStmtFinalize(pRealTable, pStmt);
972 973 974
}

SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, const SToken* pColName) {
X
bugfix  
Xiaoyu Wang 已提交
975 976 977
  if (NULL == pRealTable) {
    return NULL;
  }
978 979 980 981
  SAlterTableStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
  strncpy(pStmt->colName, pColName->z, pColName->n);
X
Xiaoyu Wang 已提交
982
  return createAlterTableStmtFinalize(pRealTable, pStmt);
983 984
}

X
Xiaoyu Wang 已提交
985 986
SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType,
                                 const SToken* pOldColName, const SToken* pNewColName) {
X
bugfix  
Xiaoyu Wang 已提交
987 988 989
  if (NULL == pRealTable) {
    return NULL;
  }
990 991 992 993 994
  SAlterTableStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
  strncpy(pStmt->colName, pOldColName->z, pOldColName->n);
  strncpy(pStmt->newColName, pNewColName->z, pNewColName->n);
X
Xiaoyu Wang 已提交
995
  return createAlterTableStmtFinalize(pRealTable, pStmt);
996 997 998
}

SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, const SToken* pTagName, SNode* pVal) {
X
bugfix  
Xiaoyu Wang 已提交
999 1000 1001
  if (NULL == pRealTable) {
    return NULL;
  }
1002 1003 1004 1005 1006
  SAlterTableStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_TAG_VAL;
  strncpy(pStmt->colName, pTagName->z, pTagName->n);
  pStmt->pVal = (SValueNode*)pVal;
X
Xiaoyu Wang 已提交
1007
  return createAlterTableStmtFinalize(pRealTable, pStmt);
1008 1009
}

1010 1011 1012 1013
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
1014 1015 1016 1017 1018 1019
  SUseDatabaseStmt* pStmt = (SUseDatabaseStmt*)nodesMakeNode(QUERY_NODE_USE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1020
static bool needDbShowStmt(ENodeType type) {
X
Xiaoyu Wang 已提交
1021 1022
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type ||
         QUERY_NODE_SHOW_VGROUPS_STMT == type;
X
Xiaoyu Wang 已提交
1023 1024
}

X
Xiaoyu Wang 已提交
1025
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbNamePattern) {
X
Xiaoyu Wang 已提交
1026 1027
  if (needDbShowStmt(type) && NULL == pDbName && NULL == pCxt->pQueryCxt->db) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "db not specified");
1028
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
X
Xiaoyu Wang 已提交
1029 1030
    return NULL;
  }
X
Xiaoyu Wang 已提交
1031 1032
  SShowStmt* pStmt = nodesMakeNode(type);
  ;
1033
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1034 1035
  pStmt->pDbName = pDbName;
  pStmt->pTbNamePattern = pTbNamePattern;
1036
  return (SNode*)pStmt;
1037
}
1038

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
SNode* createShowCreateDatabaseStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SHOW_CREATE_DATABASE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createShowCreateTableStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pRealTable) {
  SNode* pStmt = nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1051
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword) {
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  char password[TSDB_USET_PASSWORD_LEN] = {0};
  if (!checkUserName(pCxt, pUserName) || !checkPassword(pCxt, pPassword, password)) {
    return NULL;
  }
  SCreateUserStmt* pStmt = (SCreateUserStmt*)nodesMakeNode(QUERY_NODE_CREATE_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->useName, pUserName->z, pUserName->n);
  strcpy(pStmt->password, password);
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1063
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SAlterUserStmt* pStmt = (SAlterUserStmt*)nodesMakeNode(QUERY_NODE_ALTER_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->useName, pUserName->z, pUserName->n);
  if (TSDB_ALTER_USER_PASSWD == alterType) {
    char password[TSDB_USET_PASSWORD_LEN] = {0};
    if (!checkPassword(pCxt, pVal, password)) {
      nodesDestroyNode(pStmt);
      return NULL;
    }
    strcpy(pStmt->password, password);
  }
  pStmt->alterType = alterType;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1082
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
  if (!checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SDropUserStmt* pStmt = (SDropUserStmt*)nodesMakeNode(QUERY_NODE_DROP_USER_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->useName, pUserName->z, pUserName->n);
  return (SNode*)pStmt;
}

SNode* createCreateDnodeStmt(SAstCreateContext* pCxt, const SToken* pFqdn, const SToken* pPort) {
  int32_t port = 0;
X
Xiaoyu Wang 已提交
1094
  char    fqdn[TSDB_FQDN_LEN] = {0};
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
  if (NULL == pPort) {
    if (!checkAndSplitEndpoint(pCxt, pFqdn, fqdn, &port)) {
      return NULL;
    }
  } else if (!checkFqdn(pCxt, pFqdn) || !checkPort(pCxt, pPort, &port)) {
    return NULL;
  }
  SCreateDnodeStmt* pStmt = (SCreateDnodeStmt*)nodesMakeNode(QUERY_NODE_CREATE_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (NULL == pPort) {
    strcpy(pStmt->fqdn, fqdn);
  } else {
    strncpy(pStmt->fqdn, pFqdn->z, pFqdn->n);
  }
  pStmt->port = port;
  return (SNode*)pStmt;
}

SNode* createDropDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode) {
  SDropDnodeStmt* pStmt = (SDropDnodeStmt*)nodesMakeNode(QUERY_NODE_DROP_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  if (TK_NK_INTEGER == pDnode->type) {
    pStmt->dnodeId = strtol(pDnode->z, NULL, 10);
  } else {
    if (!checkAndSplitEndpoint(pCxt, pDnode, pStmt->fqdn, &pStmt->port)) {
      nodesDestroyNode(pStmt);
      return NULL;
    }
  }
  return (SNode*)pStmt;
}
X
Xiaoyu Wang 已提交
1126

X
Xiaoyu Wang 已提交
1127 1128
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig,
                            const SToken* pValue) {
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  SAlterDnodeStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_DNODE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->dnodeId = strtol(pDnode->z, NULL, 10);
  trimString(pConfig->z, pConfig->n, pStmt->config, sizeof(pStmt->config));
  if (NULL != pValue) {
    trimString(pValue->z, pValue->n, pStmt->value, sizeof(pStmt->value));
  }
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1139 1140
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName,
                             SToken* pTableName, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1141 1142 1143 1144 1145 1146
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName)) {
    return NULL;
  }
  SCreateIndexStmt* pStmt = nodesMakeNode(QUERY_NODE_CREATE_INDEX_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->indexType = type;
X
Xiaoyu Wang 已提交
1147
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1148 1149 1150 1151 1152 1153 1154
  strncpy(pStmt->indexName, pIndexName->z, pIndexName->n);
  strncpy(pStmt->tableName, pTableName->z, pTableName->n);
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1155 1156
SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset,
                         SNode* pSliding) {
X
Xiaoyu Wang 已提交
1157 1158 1159 1160 1161 1162 1163 1164
  SIndexOptions* pOptions = nodesMakeNode(QUERY_NODE_INDEX_OPTIONS);
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->pFuncs = pFuncs;
  pOptions->pInterval = pInterval;
  pOptions->pOffset = pOffset;
  pOptions->pSliding = pSliding;
  return (SNode*)pOptions;
}
X
Xiaoyu Wang 已提交
1165

X
Xiaoyu Wang 已提交
1166
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName, SToken* pTableName) {
1167 1168 1169 1170 1171
  if (!checkIndexName(pCxt, pIndexName) || !checkTableName(pCxt, pTableName)) {
    return NULL;
  }
  SDropIndexStmt* pStmt = nodesMakeNode(QUERY_NODE_DROP_INDEX_STMT);
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1172
  pStmt->ignoreNotExists = ignoreNotExists;
1173 1174 1175 1176 1177
  strncpy(pStmt->indexName, pIndexName->z, pIndexName->n);
  strncpy(pStmt->tableName, pTableName->z, pTableName->n);
  return (SNode*)pStmt;
}

1178 1179
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
  SCreateComponentNodeStmt* pStmt = nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1180
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1181 1182
  pStmt->dnodeId = strtol(pDnodeId->z, NULL, 10);
  ;
X
Xiaoyu Wang 已提交
1183 1184
  return (SNode*)pStmt;
}
1185

1186 1187
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
  SDropComponentNodeStmt* pStmt = nodesMakeNode(type);
1188
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
1189 1190
  pStmt->dnodeId = strtol(pDnodeId->z, NULL, 10);
  ;
1191 1192 1193
  return (SNode*)pStmt;
}

X
Xiaoyu Wang 已提交
1194 1195 1196 1197 1198 1199 1200 1201 1202
SNode* createTopicOptions(SAstCreateContext* pCxt) {
  STopicOptions* pOptions = nodesMakeNode(QUERY_NODE_TOPIC_OPTIONS);
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->withTable = false;
  pOptions->withSchema = false;
  pOptions->withTag = false;
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1203 1204
SNode* createCreateTopicStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName, SNode* pQuery,
                             const SToken* pSubscribeDbName, SNode* pOptions) {
1205 1206 1207 1208 1209 1210 1211 1212
  SCreateTopicStmt* pStmt = nodesMakeNode(QUERY_NODE_CREATE_TOPIC_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->topicName, pTopicName->z, pTopicName->n);
  pStmt->ignoreExists = ignoreExists;
  pStmt->pQuery = pQuery;
  if (NULL != pSubscribeDbName) {
    strncpy(pStmt->subscribeDbName, pSubscribeDbName->z, pSubscribeDbName->n);
  }
X
Xiaoyu Wang 已提交
1213
  pStmt->pOptions = (STopicOptions*)pOptions;
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
  return (SNode*)pStmt;
}

SNode* createDropTopicStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pTopicName) {
  SDropTopicStmt* pStmt = nodesMakeNode(QUERY_NODE_DROP_TOPIC_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  strncpy(pStmt->topicName, pTopicName->z, pTopicName->n);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
}
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233

SNode* createAlterLocalStmt(SAstCreateContext* pCxt, const SToken* pConfig, const SToken* pValue) {
  SAlterLocalStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_LOCAL_STMT);
  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;
}
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260

SNode* createDefaultExplainOptions(SAstCreateContext* pCxt) {
  SExplainOptions* pOptions = nodesMakeNode(QUERY_NODE_EXPLAIN_OPTIONS);
  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) {
  ((SExplainOptions*)pOptions)->verbose = (0 == strncasecmp(pVal->z, "true", pVal->n));
  return pOptions;
}

SNode* setExplainRatio(SAstCreateContext* pCxt, SNode* pOptions, const SToken* pVal) {
  ((SExplainOptions*)pOptions)->ratio = strtod(pVal->z, NULL);
  return pOptions;
}

SNode* createExplainStmt(SAstCreateContext* pCxt, bool analyze, SNode* pOptions, SNode* pQuery) {
  SExplainStmt* pStmt = nodesMakeNode(QUERY_NODE_EXPLAIN_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->analyze = analyze;
  pStmt->pOptions = (SExplainOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
}
1261 1262

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
bugfix  
Xiaoyu Wang 已提交
1263 1264 1265
  if (NULL == pRealTable) {
    return NULL;
  }
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  SDescribeStmt* pStmt = nodesMakeNode(QUERY_NODE_DESCRIBE_STMT);
  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) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_RESET_QUERY_CACHE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1279 1280 1281 1282 1283 1284 1285

SNode* createCompactStmt(SAstCreateContext* pCxt, SNodeList* pVgroups) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_COMPACT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

X
Xiaoyu Wang 已提交
1286 1287
SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool ignoreExists, bool aggFunc, const SToken* pFuncName,
                                const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
1288
  if (pLibPath->n <= 2) {
1289
    pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR;
1290 1291 1292
    return NULL;
  }
  SCreateFunctionStmt* pStmt = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
1293
  CHECK_OUT_OF_MEM(pStmt);
1294 1295 1296 1297 1298 1299 1300
  pStmt->ignoreExists = ignoreExists;
  strncpy(pStmt->funcName, pFuncName->z, pFuncName->n);
  pStmt->isAgg = aggFunc;
  strncpy(pStmt->libraryPath, pLibPath->z + 1, pLibPath->n - 2);
  pStmt->outputDt = dataType;
  pStmt->bufSize = bufSize;
  return (SNode*)pStmt;
1301 1302
}

1303 1304
SNode* createDropFunctionStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pFuncName) {
  SDropFunctionStmt* pStmt = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
1305
  CHECK_OUT_OF_MEM(pStmt);
1306 1307 1308
  pStmt->ignoreNotExists = ignoreNotExists;
  strncpy(pStmt->funcName, pFuncName->z, pFuncName->n);
  return (SNode*)pStmt;
1309 1310
}

1311 1312 1313 1314 1315 1316 1317
SNode* createStreamOptions(SAstCreateContext* pCxt) {
  SStreamOptions* pOptions = nodesMakeNode(QUERY_NODE_STREAM_OPTIONS);
  CHECK_OUT_OF_MEM(pOptions);
  pOptions->triggerType = STREAM_TRIGGER_AT_ONCE;
  return (SNode*)pOptions;
}

X
Xiaoyu Wang 已提交
1318 1319
SNode* createCreateStreamStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pStreamName, SNode* pRealTable,
                              SNode* pOptions, SNode* pQuery) {
1320
  SCreateStreamStmt* pStmt = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
1321
  CHECK_OUT_OF_MEM(pStmt);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
  strncpy(pStmt->streamName, pStreamName->z, pStreamName->n);
  if (NULL != pRealTable) {
    strcpy(pStmt->targetDbName, ((SRealTableNode*)pRealTable)->table.dbName);
    strcpy(pStmt->targetTabName, ((SRealTableNode*)pRealTable)->table.tableName);
    nodesDestroyNode(pRealTable);
  }
  pStmt->ignoreExists = ignoreExists;
  pStmt->pOptions = (SStreamOptions*)pOptions;
  pStmt->pQuery = pQuery;
  return (SNode*)pStmt;
1332 1333
}

1334 1335
SNode* createDropStreamStmt(SAstCreateContext* pCxt, bool ignoreNotExists, const SToken* pStreamName) {
  SDropStreamStmt* pStmt = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
1336
  CHECK_OUT_OF_MEM(pStmt);
1337 1338 1339
  strncpy(pStmt->streamName, pStreamName->z, pStreamName->n);
  pStmt->ignoreNotExists = ignoreNotExists;
  return (SNode*)pStmt;
1340 1341 1342
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
1343
  SKillStmt* pStmt = nodesMakeNode(type);
1344
  CHECK_OUT_OF_MEM(pStmt);
1345 1346
  pStmt->targetId = strtol(pId->z, NULL, 10);
  return (SNode*)pStmt;
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
}

SNode* createMergeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId1, const SToken* pVgId2) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_MERGE_VGROUP_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createRedistributeVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId, SNodeList* pDnodes) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_REDISTRIBUTE_VGROUP_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createSplitVgroupStmt(SAstCreateContext* pCxt, const SToken* pVgId) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SPLIT_VGROUP_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createSyncdbStmt(SAstCreateContext* pCxt, const SToken* pDbName) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_SYNCDB_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395

SNode* createGrantStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SGrantStmt* pStmt = nodesMakeNode(QUERY_NODE_GRANT_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  strncpy(pStmt->userName, pUserName->z, pUserName->n);
  return (SNode*)pStmt;
}

SNode* createRevokeStmt(SAstCreateContext* pCxt, int64_t privileges, SToken* pDbName, SToken* pUserName) {
  if (!checkDbName(pCxt, pDbName, false) || !checkUserName(pCxt, pUserName)) {
    return NULL;
  }
  SRevokeStmt* pStmt = nodesMakeNode(QUERY_NODE_REVOKE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->privileges = privileges;
  strncpy(pStmt->dbName, pDbName->z, pDbName->n);
  strncpy(pStmt->userName, pUserName->z, pUserName->n);
  return (SNode*)pStmt;
}