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

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

X
Xiaoyu Wang 已提交
17 18
#include "parAst.h"
#include "parUtil.h"
X
Xiaoyu Wang 已提交
19
#include "ttime.h"
20

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

30 31 32 33 34 35 36 37
#define CHECK_RAW_EXPR_NODE(node) \
  do { \
    if (NULL == (node) || QUERY_NODE_RAW_EXPR != nodeType(node)) { \
      pCxt->valid = false; \
      return NULL; \
    } \
  } while (0)

38
SToken nil_token = { .type = TK_NK_NIL, .n = 0, .z = NULL };
39

40 41
void initAstCreateContext(SParseContext* pParseCxt, SAstCreateContext* pCxt) {
  pCxt->pQueryCxt = pParseCxt;
42 43
  pCxt->msgBuf.buf = pParseCxt->pMsg;
  pCxt->msgBuf.len = pParseCxt->msgLen;
44 45 46 47 48
  pCxt->notSupport = false;
  pCxt->valid = true;
  pCxt->pRootNode = NULL;
}

X
Xiaoyu Wang 已提交
49 50 51 52 53 54 55 56
static void trimEscape(SToken* pName) {
  if (NULL != pName && pName->n > 1 && '`' == pName->z[0]) {
    pName->z += 1;
    pName->n -= 2;
  }
}

static bool checkUserName(SAstCreateContext* pCxt, SToken* pUserName) {
57 58
  if (NULL == pUserName) {
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
59 60 61 62 63
  } else {
    if (pUserName->n >= TSDB_USER_LEN) {
      generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
      pCxt->valid = false;
    }
64
  }
X
Xiaoyu Wang 已提交
65 66 67
  if (pCxt->valid) {
    trimEscape(pUserName);
  }
68 69 70 71 72 73
  return pCxt->valid;
}

static bool checkPassword(SAstCreateContext* pCxt, const SToken* pPasswordToken, char* pPassword) {
  if (NULL == pPasswordToken) {
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
74 75
  } else if (pPasswordToken->n >= (TSDB_USET_PASSWORD_LEN - 2)) {
    generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
76
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
77 78 79 80 81 82 83
  } else {
    strncpy(pPassword, pPasswordToken->z, pPasswordToken->n);
    strdequote(pPassword);
    if (strtrim(pPassword) <= 0) {
      generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_PASSWD_EMPTY);
      pCxt->valid = false;
    }
84 85 86 87 88 89 90
  }
  return pCxt->valid;
}

static bool checkAndSplitEndpoint(SAstCreateContext* pCxt, const SToken* pEp, char* pFqdn, int32_t* pPort) {
  if (NULL == pEp) {
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
91 92
  } else if (pEp->n >= TSDB_FQDN_LEN + 2 + 6) { // format 'fqdn:port'
    generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
93
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  } 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) {
      generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ENDPOINT);
      pCxt->valid = false;
    } else {
      strncpy(pFqdn, ep, pColon - ep);
      *pPort = strtol(pColon + 1, NULL, 10);
      if (*pPort >= UINT16_MAX || *pPort <= 0) {
        generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
        pCxt->valid = false;
      }
    }
111 112 113 114 115 116 117
  }
  return pCxt->valid;
}

static bool checkFqdn(SAstCreateContext* pCxt, const SToken* pFqdn) {
  if (NULL == pFqdn) {
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
118 119 120 121 122
  } else {
    if (pFqdn->n >= TSDB_FQDN_LEN) {
      generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NAME_OR_PASSWD_TOO_LONG);
      pCxt->valid = false;
    }
123 124 125 126 127 128 129
  }
  return pCxt->valid;
}

static bool checkPort(SAstCreateContext* pCxt, const SToken* pPortToken, int32_t* pPort) {
  if (NULL == pPortToken) {
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
130 131 132 133 134 135
  } else {
    *pPort = strtol(pPortToken->z, NULL, 10);
    if (*pPort >= UINT16_MAX || *pPort <= 0) {
      generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_PORT);
      pCxt->valid = false;
    }
136 137 138 139
  }
  return pCxt->valid;
}

X
Xiaoyu Wang 已提交
140
static bool checkDbName(SAstCreateContext* pCxt, SToken* pDbName, bool query) {
141
  if (NULL == pDbName) {
X
Xiaoyu Wang 已提交
142 143 144
    if (query && NULL == pCxt->pQueryCxt->db) {
      generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DB_NOT_SPECIFIED);
      pCxt->valid = false;
X
bugfix  
Xiaoyu Wang 已提交
145
    }
X
Xiaoyu Wang 已提交
146
  } else {
X
Xiaoyu Wang 已提交
147 148 149 150 151 152 153
    if (pDbName->n >= TSDB_DB_NAME_LEN) {
      generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pDbName->z);
      pCxt->valid = false;
    }
  }
  if (pCxt->valid) {
    trimEscape(pDbName);
154 155 156
  }
  return pCxt->valid;
}
157

X
Xiaoyu Wang 已提交
158
static bool checkTableName(SAstCreateContext* pCxt, SToken* pTableName) {
X
Xiaoyu Wang 已提交
159 160 161 162
  if (NULL != pTableName && pTableName->n >= TSDB_TABLE_NAME_LEN) {
    generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pTableName->z);
    pCxt->valid = false;
    return false;
163
  }
X
Xiaoyu Wang 已提交
164
  trimEscape(pTableName);
X
Xiaoyu Wang 已提交
165
  return true;
166 167
}

X
Xiaoyu Wang 已提交
168
static bool checkColumnName(SAstCreateContext* pCxt, SToken* pColumnName) {
X
Xiaoyu Wang 已提交
169 170 171 172
  if (NULL != pColumnName && pColumnName->n >= TSDB_COL_NAME_LEN) {
    generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pColumnName->z);
    pCxt->valid = false;
    return false;
173
  }
X
Xiaoyu Wang 已提交
174
  trimEscape(pColumnName);
X
Xiaoyu Wang 已提交
175
  return true;
176 177
}

X
Xiaoyu Wang 已提交
178
static bool checkIndexName(SAstCreateContext* pCxt, SToken* pIndexName) {
X
Xiaoyu Wang 已提交
179 180
  if (NULL != pIndexName && pIndexName->n >= TSDB_INDEX_NAME_LEN) {
    generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_IDENTIFIER_NAME, pIndexName->z);
X
Xiaoyu Wang 已提交
181
    pCxt->valid = false;
X
Xiaoyu Wang 已提交
182
    return false;
X
Xiaoyu Wang 已提交
183
  }
X
Xiaoyu Wang 已提交
184
  trimEscape(pIndexName);
X
Xiaoyu Wang 已提交
185
  return true;
X
Xiaoyu Wang 已提交
186 187
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
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);
208 209
  SRawExprNode* pRawExpr = (SRawExprNode*)pNode;
  SNode* pExpr = pRawExpr->pNode;
D
dapan1121 已提交
210
  if (nodesIsExprNode(pExpr)) {
211 212 213
    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 已提交
214
  }
wafwerar's avatar
wafwerar 已提交
215
  taosMemoryFreeClear(pNode);
216
  return pExpr;
217 218 219
}

SToken getTokenFromRawExprNode(SAstCreateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
220 221 222 223
  if (NULL == pNode || QUERY_NODE_RAW_EXPR != nodeType(pNode)) {
    pCxt->valid = false;
    return nil_token;
  }
224 225 226 227 228
  SRawExprNode* target = (SRawExprNode*)pNode;
  SToken t = { .type = 0, .z = target->p, .n = target->n};
  return t;
}

229 230 231
SNodeList* createNodeList(SAstCreateContext* pCxt, SNode* pNode) {
  SNodeList* list = nodesMakeList();
  CHECK_OUT_OF_MEM(list);
X
Xiaoyu Wang 已提交
232 233 234 235
  if (TSDB_CODE_SUCCESS != nodesListAppend(list, pNode)) {
    pCxt->valid = false;
  }
  return list;
236 237 238
}

SNodeList* addNodeToList(SAstCreateContext* pCxt, SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
239 240 241 242
  if (TSDB_CODE_SUCCESS != nodesListAppend(pList, pNode)) {
    pCxt->valid = false;
  }
  return pList;
243 244
}

X
Xiaoyu Wang 已提交
245
SNode* createColumnNode(SAstCreateContext* pCxt, SToken* pTableAlias, SToken* pColumnName) {
246
  if (!checkTableName(pCxt, pTableAlias) || !checkColumnName(pCxt, pColumnName)) {
247 248 249 250
    return NULL;
  }
  SColumnNode* col = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
  CHECK_OUT_OF_MEM(col);
251 252
  if (NULL != pTableAlias) {
    strncpy(col->tableAlias, pTableAlias->z, pTableAlias->n);
253 254
  }
  strncpy(col->colName, pColumnName->z, pColumnName->n);
255
  return (SNode*)col;
256
}
257

258 259 260
SNode* createValueNode(SAstCreateContext* pCxt, int32_t dataType, const SToken* pLiteral) {
  SValueNode* val = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE);
  CHECK_OUT_OF_MEM(val);
X
Xiaoyu Wang 已提交
261 262
  if (NULL != pLiteral) {
    val->literal = strndup(pLiteral->z, pLiteral->n);
263 264
    if (TK_NK_ID != pLiteral->type && TK_TIMEZONE != pLiteral->type &&
       (IS_VAR_DATA_TYPE(dataType) || TSDB_DATA_TYPE_TIMESTAMP == dataType)) {
X
Xiaoyu Wang 已提交
265 266 267
      trimString(pLiteral->z, pLiteral->n, val->literal, pLiteral->n);
    }
    CHECK_OUT_OF_MEM(val->literal);
X
Xiaoyu Wang 已提交
268
  }
269
  val->node.resType.type = dataType;
X
Xiaoyu Wang 已提交
270
  val->node.resType.bytes = IS_VAR_DATA_TYPE(dataType) ? strlen(val->literal) : tDataTypes[dataType].bytes;
271 272 273
  if (TSDB_DATA_TYPE_TIMESTAMP == dataType) {
    val->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
  }
274 275
  val->isDuration = false;
  val->translate = false;
276 277 278 279 280 281
  return (SNode*)val;
}

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

X
Xiaoyu Wang 已提交
292
SNode* createDefaultDatabaseCondValue(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
293 294 295 296
  if (NULL == pCxt->pQueryCxt->db) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
297 298 299 300 301 302 303 304 305 306 307 308
  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;
}

309 310 311 312 313
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();
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  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);
    }
  }
334 335 336 337 338 339 340 341 342 343 344 345 346 347
  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 已提交
348
      createOperatorNode(pCxt, OP_TYPE_GREATER_EQUAL, pExpr, pLeft), createOperatorNode(pCxt, OP_TYPE_LOWER_EQUAL, nodesCloneNode(pExpr), pRight));
349 350 351 352
}

SNode* createNotBetweenAnd(SAstCreateContext* pCxt, SNode* pExpr, SNode* pLeft, SNode* pRight) {
  return createLogicConditionNode(pCxt, LOGIC_COND_TYPE_OR,
X
Xiaoyu Wang 已提交
353
      createOperatorNode(pCxt, OP_TYPE_LOWER_THAN, pExpr, pLeft), createOperatorNode(pCxt, OP_TYPE_GREATER_THAN, nodesCloneNode(pExpr), pRight));
354 355 356 357 358 359 360 361 362 363
}

SNode* createFunctionNode(SAstCreateContext* pCxt, const SToken* pFuncName, SNodeList* pParameterList) {
  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;
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
SNode* createFunctionNodeNoParam(SAstCreateContext* pCxt, const SToken* pFuncName) {
  SFunctionNode* func = (SFunctionNode*)nodesMakeNode(QUERY_NODE_FUNCTION);
  CHECK_OUT_OF_MEM(func);
  char buf[64] = {0};

  int32_t dataType;
  switch (pFuncName->type) {
    case TK_NOW: {
      int64_t ts = taosGetTimestamp(TSDB_TIME_PRECISION_MILLI);
      snprintf(buf, sizeof(buf), "%"PRId64, ts);
      dataType = TSDB_DATA_TYPE_BIGINT;
      break;
    }
    case TK_TODAY: {
      int64_t ts = taosGetTimestampToday(TSDB_TIME_PRECISION_MILLI);
      snprintf(buf, sizeof(buf), "%"PRId64, ts);
      dataType = TSDB_DATA_TYPE_BIGINT;
      break;
    }
383 384 385 386 387
    case TK_TIMEZONE: {
      strncpy(buf, tsTimezoneStr, strlen(tsTimezoneStr));
      dataType = TSDB_DATA_TYPE_BINARY;
      break;
    }
388 389 390 391 392 393 394 395 396
  }
  SToken token = {.type = pFuncName->type, .n = strlen(buf), .z = buf};

  SNodeList *pParameterList = createNodeList(pCxt, createValueNode(pCxt, dataType, &token));
  strncpy(func->functionName, pFuncName->z, pFuncName->n);
  func->pParameterList = pParameterList;
  return (SNode*)func;
}

397 398 399 400 401 402 403 404 405
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;
  nodesListMakeAppend(&func->pParameterList, pExpr);
  return (SNode*)func;
}

406 407 408 409 410
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;
411 412
}

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

446 447 448 449
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;
450
  if (NULL != pTableAlias && TK_NK_NIL != pTableAlias->type) {
451
    strncpy(tempTable->table.tableAlias, pTableAlias->z, pTableAlias->n);
X
Xiaoyu Wang 已提交
452 453 454 455 456
  } else {
    sprintf(tempTable->table.tableAlias, "%p", tempTable);
  }
  if (QUERY_NODE_SELECT_STMT == nodeType(pSubquery)) {
    strcpy(((SSelectStmt*)pSubquery)->stmtName, tempTable->table.tableAlias);
457
  }
458
  return (SNode*)tempTable;
459 460
}

461 462 463 464
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;
465 466
  joinTable->pLeft = pLeft;
  joinTable->pRight = pRight;
467
  joinTable->pOnCond = pJoinCond;
468
  return (SNode*)joinTable;
469
}
470

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

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

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

501
SNode* createStateWindowNode(SAstCreateContext* pCxt, SNode* pExpr) {
502 503
  SStateWindowNode* state = (SStateWindowNode*)nodesMakeNode(QUERY_NODE_STATE_WINDOW);
  CHECK_OUT_OF_MEM(state);
504 505 506 507 508 509 510
  state->pCol = nodesMakeNode(QUERY_NODE_COLUMN);
  if (NULL == state->pCol) {
    nodesDestroyNode(state);
    CHECK_OUT_OF_MEM(state->pCol);
  }
  ((SColumnNode*)state->pCol)->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(((SColumnNode*)state->pCol)->colName, PK_TS_COL_INTERNAL_NAME);
511
  state->pExpr = pExpr;
512 513 514 515 516 517
  return (SNode*)state;
}

SNode* createIntervalWindowNode(SAstCreateContext* pCxt, SNode* pInterval, SNode* pOffset, SNode* pSliding, SNode* pFill) {
  SIntervalWindowNode* interval = (SIntervalWindowNode*)nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  CHECK_OUT_OF_MEM(interval);
X
bugfix  
Xiaoyu Wang 已提交
518 519 520 521 522 523 524
  interval->pCol = nodesMakeNode(QUERY_NODE_COLUMN);
  if (NULL == interval->pCol) {
    nodesDestroyNode(interval);
    CHECK_OUT_OF_MEM(interval->pCol);
  }
  ((SColumnNode*)interval->pCol)->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(((SColumnNode*)interval->pCol)->colName, PK_TS_COL_INTERNAL_NAME);
525 526 527 528 529 530 531 532 533 534 535 536 537
  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;
  return (SNode*)fill;
538
}
539

540 541 542 543 544 545 546 547 548
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;
}

549
SNode* setProjectionAlias(SAstCreateContext* pCxt, SNode* pNode, const SToken* pAlias) {
X
Xiaoyu Wang 已提交
550 551 552
  if (NULL == pNode || !pCxt->valid) {
    return pNode;
  }
D
dapan1121 已提交
553 554 555
  int32_t len = TMIN(sizeof(((SExprNode*)pNode)->aliasName) - 1, pAlias->n);
  strncpy(((SExprNode*)pNode)->aliasName, pAlias->z, len);
  ((SExprNode*)pNode)->aliasName[len] = '\0';
556
  return pNode;
557 558
}

559 560
SNode* addWhereClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWhere) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
561
    ((SSelectStmt*)pStmt)->pWhere = pWhere;
562 563 564
  }
  return pStmt;
}
565

566 567
SNode* addPartitionByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pPartitionByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
568
    ((SSelectStmt*)pStmt)->pPartitionByList = pPartitionByList;
569 570
  }
  return pStmt;
571 572
}

573 574
SNode* addWindowClauseClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pWindow) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
575
    ((SSelectStmt*)pStmt)->pWindow = pWindow;
576 577 578
  }
  return pStmt;
}
579

580 581
SNode* addGroupByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pGroupByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
582
    ((SSelectStmt*)pStmt)->pGroupByList = pGroupByList;
583 584
  }
  return pStmt;
585 586
}

587 588
SNode* addHavingClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pHaving) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
589
    ((SSelectStmt*)pStmt)->pHaving = pHaving;
590 591 592 593 594 595
  }
  return pStmt;
}

SNode* addOrderByClause(SAstCreateContext* pCxt, SNode* pStmt, SNodeList* pOrderByList) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
596
    ((SSelectStmt*)pStmt)->pOrderByList = pOrderByList;
597 598 599
  }
  return pStmt;
}
600

601 602
SNode* addSlimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pSlimit) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
603
    ((SSelectStmt*)pStmt)->pSlimit = pSlimit;
604 605
  }
  return pStmt;
606 607
}

608 609
SNode* addLimitClause(SAstCreateContext* pCxt, SNode* pStmt, SNode* pLimit) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
610
    ((SSelectStmt*)pStmt)->pLimit = pLimit;
611
  }
612
  return pStmt;
613 614 615 616
}

SNode* createSelectStmt(SAstCreateContext* pCxt, bool isDistinct, SNodeList* pProjectionList, SNode* pTable) {
  SSelectStmt* select = (SSelectStmt*)nodesMakeNode(QUERY_NODE_SELECT_STMT);
617
  CHECK_OUT_OF_MEM(select);
618
  select->isDistinct = isDistinct;
619 620
  select->pProjectionList = pProjectionList;
  select->pFromTable = pTable;
X
Xiaoyu Wang 已提交
621
  sprintf(select->stmtName, "%p", select);
622
  return (SNode*)select;
623 624 625
}

SNode* createSetOperator(SAstCreateContext* pCxt, ESetOperatorType type, SNode* pLeft, SNode* pRight) {
626 627 628
  SSetOperator* setOp = (SSetOperator*)nodesMakeNode(QUERY_NODE_SET_OPERATOR);
  CHECK_OUT_OF_MEM(setOp);
  setOp->opType = type;
629 630 631
  setOp->pLeft = pLeft;
  setOp->pRight = pRight;
  return (SNode*)setOp;
632 633
}

X
Xiaoyu Wang 已提交
634
SNode* createDatabaseOptions(SAstCreateContext* pCxt) {
X
Xiaoyu Wang 已提交
635
  SDatabaseOptions* pOptions = nodesMakeNode(QUERY_NODE_DATABASE_OPTIONS);
636
  CHECK_OUT_OF_MEM(pOptions);
X
Xiaoyu Wang 已提交
637
  return (SNode*)pOptions;
638 639
}

X
Xiaoyu Wang 已提交
640
SNode* setDatabaseAlterOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 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 693 694 695 696 697
  switch (pAlterOption->type) {
    case DB_OPTION_BLOCKS:
      ((SDatabaseOptions*)pOptions)->pNumOfBlocks = pAlterOption->pVal;
      break;
    case DB_OPTION_CACHE:
      ((SDatabaseOptions*)pOptions)->pCacheBlockSize = pAlterOption->pVal;
      break;
    case DB_OPTION_CACHELAST:
      ((SDatabaseOptions*)pOptions)->pCachelast = pAlterOption->pVal;
      break;
    case DB_OPTION_COMP:
      ((SDatabaseOptions*)pOptions)->pCompressionLevel = pAlterOption->pVal;
      break;
    case DB_OPTION_DAYS:
      ((SDatabaseOptions*)pOptions)->pDaysPerFile = pAlterOption->pVal;
      break;
    case DB_OPTION_FSYNC:
      ((SDatabaseOptions*)pOptions)->pFsyncPeriod = pAlterOption->pVal;
      break;
    case DB_OPTION_MAXROWS:
      ((SDatabaseOptions*)pOptions)->pMaxRowsPerBlock = pAlterOption->pVal;
      break;
    case DB_OPTION_MINROWS:
      ((SDatabaseOptions*)pOptions)->pMinRowsPerBlock = pAlterOption->pVal;
      break;
    case DB_OPTION_KEEP:
      ((SDatabaseOptions*)pOptions)->pKeep = pAlterOption->pList;
      break;
    case DB_OPTION_PRECISION:
      ((SDatabaseOptions*)pOptions)->pPrecision = pAlterOption->pVal;
      break;
    case DB_OPTION_QUORUM:
      ((SDatabaseOptions*)pOptions)->pQuorum = pAlterOption->pVal;
      break;
    case DB_OPTION_REPLICA:
      ((SDatabaseOptions*)pOptions)->pReplica = pAlterOption->pVal;
      break;
    case DB_OPTION_TTL:
      ((SDatabaseOptions*)pOptions)->pTtl = pAlterOption->pVal;
      break;
    case DB_OPTION_WAL:
      ((SDatabaseOptions*)pOptions)->pWalLevel = pAlterOption->pVal;
      break;
    case DB_OPTION_VGROUPS:
      ((SDatabaseOptions*)pOptions)->pNumOfVgroups = pAlterOption->pVal;
      break;
    case DB_OPTION_SINGLE_STABLE:
      ((SDatabaseOptions*)pOptions)->pSingleStable = pAlterOption->pVal;
      break;
    case DB_OPTION_STREAM_MODE:
      ((SDatabaseOptions*)pOptions)->pStreamMode = pAlterOption->pVal;
      break;
    case DB_OPTION_RETENTIONS:
      ((SDatabaseOptions*)pOptions)->pRetentions = pAlterOption->pList;
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
698
  }
X
Xiaoyu Wang 已提交
699
  return pOptions;
X
Xiaoyu Wang 已提交
700 701
}

X
Xiaoyu Wang 已提交
702
SNode* createCreateDatabaseStmt(SAstCreateContext* pCxt, bool ignoreExists, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
703
  if (!checkDbName(pCxt, pDbName, false)) {
704 705 706 707 708 709
    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 已提交
710
  pStmt->pOptions = (SDatabaseOptions*)pOptions;
711
  return (SNode*)pStmt;
712
}
X
Xiaoyu Wang 已提交
713

X
Xiaoyu Wang 已提交
714
SNode* createDropDatabaseStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pDbName) {
X
Xiaoyu Wang 已提交
715
  if (!checkDbName(pCxt, pDbName, false)) {
716 717 718 719 720 721 722 723 724
    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 已提交
725
SNode* createAlterDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName, SNode* pOptions) {
X
Xiaoyu Wang 已提交
726
  if (!checkDbName(pCxt, pDbName, false)) {
727 728 729 730 731 732 733 734 735
    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 已提交
736
SNode* createTableOptions(SAstCreateContext* pCxt) {
737 738 739
  STableOptions* pOptions = nodesMakeNode(QUERY_NODE_TABLE_OPTIONS);
  CHECK_OUT_OF_MEM(pOptions);
  return (SNode*)pOptions;
X
Xiaoyu Wang 已提交
740 741
}

X
Xiaoyu Wang 已提交
742
SNode* setTableAlterOption(SAstCreateContext* pCxt, SNode* pOptions, SAlterOption* pAlterOption) {
X
Xiaoyu Wang 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
  switch (pAlterOption->type) {
    case TABLE_OPTION_KEEP:
      ((STableOptions*)pOptions)->pKeep = pAlterOption->pList;
      break;
    case TABLE_OPTION_TTL:
      ((STableOptions*)pOptions)->pTtl = pAlterOption->pVal;
      break;
    case TABLE_OPTION_COMMENT:
      ((STableOptions*)pOptions)->pComments = pAlterOption->pVal;
      break;
    case TABLE_OPTION_SMA:
      ((STableOptions*)pOptions)->pSma = pAlterOption->pList;
      break;
    case TABLE_OPTION_FILE_FACTOR:
      ((STableOptions*)pOptions)->pFilesFactor = pAlterOption->pVal;
      break;
    case TABLE_OPTION_DELAY:
      ((STableOptions*)pOptions)->pDelay = pAlterOption->pVal;
      break;
    default:
      break;
X
Xiaoyu Wang 已提交
764
  }
X
Xiaoyu Wang 已提交
765
  return pOptions;
X
Xiaoyu Wang 已提交
766 767
}

X
Xiaoyu Wang 已提交
768 769 770 771 772 773
SNode* createColumnDefNode(SAstCreateContext* pCxt, const SToken* pColName, SDataType dataType, const SToken* pComment) {
  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) {
774
    trimString(pComment->z, pComment->n, pCol->comments, sizeof(pCol->comments));
X
Xiaoyu Wang 已提交
775
  }
776
  pCol->sma = true;
X
Xiaoyu Wang 已提交
777 778 779
  return (SNode*)pCol;
}

780
SDataType createDataType(uint8_t type) {
781
  SDataType dt = { .type = type, .precision = 0, .scale = 0, .bytes = tDataTypes[type].bytes };
782 783 784 785
  return dt;
}

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

X
Xiaoyu Wang 已提交
790
SNode* createCreateTableStmt(SAstCreateContext* pCxt,
X
Xiaoyu Wang 已提交
791
    bool ignoreExists, SNode* pRealTable, SNodeList* pCols, SNodeList* pTags, SNode* pOptions) {
X
bugfix  
Xiaoyu Wang 已提交
792 793 794
  if (NULL == pRealTable) {
    return NULL;
  }
X
Xiaoyu Wang 已提交
795 796
  SCreateTableStmt* pStmt = (SCreateTableStmt*)nodesMakeNode(QUERY_NODE_CREATE_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
797 798
  strcpy(pStmt->dbName, ((SRealTableNode*)pRealTable)->table.dbName);
  strcpy(pStmt->tableName, ((SRealTableNode*)pRealTable)->table.tableName);
X
Xiaoyu Wang 已提交
799 800
  pStmt->ignoreExists = ignoreExists;
  pStmt->pCols = pCols;
801
  pStmt->pTags = pTags;
X
Xiaoyu Wang 已提交
802
  pStmt->pOptions = (STableOptions*)pOptions;
803
  nodesDestroyNode(pRealTable);
X
Xiaoyu Wang 已提交
804 805
  return (SNode*)pStmt;
}
806

807 808
SNode* createCreateSubTableClause(SAstCreateContext* pCxt,
    bool ignoreExists, SNode* pRealTable, SNode* pUseRealTable, SNodeList* pSpecificTags, SNodeList* pValsOfTags) {
X
bugfix  
Xiaoyu Wang 已提交
809 810 811
  if (NULL == pRealTable) {
    return NULL;
  }
812
  SCreateSubTableClause* pStmt = nodesMakeNode(QUERY_NODE_CREATE_SUBTABLE_CLAUSE);
813
  CHECK_OUT_OF_MEM(pStmt);
814 815 816 817
  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);
818 819 820
  pStmt->ignoreExists = ignoreExists;
  pStmt->pSpecificTags = pSpecificTags;
  pStmt->pValsOfTags = pValsOfTags;
821 822
  nodesDestroyNode(pRealTable);
  nodesDestroyNode(pUseRealTable);
823 824 825 826 827 828 829 830 831 832
  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;
}

833
SNode* createDropTableClause(SAstCreateContext* pCxt, bool ignoreNotExists, SNode* pRealTable) {
X
bugfix  
Xiaoyu Wang 已提交
834 835 836
  if (NULL == pRealTable) {
    return NULL;
  }
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
  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;
}

853 854 855 856 857 858 859 860 861 862
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;
}

863
SNode* createAlterTableOption(SAstCreateContext* pCxt, SNode* pRealTable, SNode* pOptions) {
X
bugfix  
Xiaoyu Wang 已提交
864 865 866
  if (NULL == pRealTable) {
    return NULL;
  }
867 868 869 870 871
  SAlterTableStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = TSDB_ALTER_TABLE_UPDATE_OPTIONS;
  pStmt->pOptions = (STableOptions*)pOptions;
  return (SNode*)pStmt;
872 873 874
}

SNode* createAlterTableAddModifyCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, const SToken* pColName, SDataType dataType) {
X
bugfix  
Xiaoyu Wang 已提交
875 876 877
  if (NULL == pRealTable) {
    return NULL;
  }
878 879 880 881 882 883
  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;
  return (SNode*)pStmt;
884 885 886
}

SNode* createAlterTableDropCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, const SToken* pColName) {
X
bugfix  
Xiaoyu Wang 已提交
887 888 889
  if (NULL == pRealTable) {
    return NULL;
  }
890 891 892 893 894
  SAlterTableStmt* pStmt = nodesMakeNode(QUERY_NODE_ALTER_TABLE_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->alterType = alterType;
  strncpy(pStmt->colName, pColName->z, pColName->n);
  return (SNode*)pStmt;
895 896 897
}

SNode* createAlterTableRenameCol(SAstCreateContext* pCxt, SNode* pRealTable, int8_t alterType, const SToken* pOldColName, const SToken* pNewColName) {
X
bugfix  
Xiaoyu Wang 已提交
898 899 900
  if (NULL == pRealTable) {
    return NULL;
  }
901 902 903 904 905 906
  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);
  return (SNode*)pStmt;
907 908 909
}

SNode* createAlterTableSetTag(SAstCreateContext* pCxt, SNode* pRealTable, const SToken* pTagName, SNode* pVal) {
X
bugfix  
Xiaoyu Wang 已提交
910 911 912
  if (NULL == pRealTable) {
    return NULL;
  }
913 914 915 916 917 918
  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;
  return (SNode*)pStmt;
919 920
}

921 922 923 924
SNode* createUseDatabaseStmt(SAstCreateContext* pCxt, SToken* pDbName) {
  if (!checkDbName(pCxt, pDbName, false)) {
    return NULL;
  }
925 926 927 928 929 930
  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 已提交
931 932 933 934
static bool needDbShowStmt(ENodeType type) {
  return QUERY_NODE_SHOW_TABLES_STMT == type || QUERY_NODE_SHOW_STABLES_STMT == type || QUERY_NODE_SHOW_VGROUPS_STMT == type;
}

X
Xiaoyu Wang 已提交
935
SNode* createShowStmt(SAstCreateContext* pCxt, ENodeType type, SNode* pDbName, SNode* pTbNamePattern) {
X
Xiaoyu Wang 已提交
936 937 938 939 940
  if (needDbShowStmt(type) && NULL == pDbName && NULL == pCxt->pQueryCxt->db) {
    snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "db not specified");
    pCxt->valid = false;
    return NULL;
  }
941
  SShowStmt* pStmt = nodesMakeNode(type);;
942
  CHECK_OUT_OF_MEM(pStmt);
X
Xiaoyu Wang 已提交
943 944
  pStmt->pDbName = pDbName;
  pStmt->pTbNamePattern = pTbNamePattern;
945
  return (SNode*)pStmt;
946
}
947

X
Xiaoyu Wang 已提交
948
SNode* createCreateUserStmt(SAstCreateContext* pCxt, SToken* pUserName, const SToken* pPassword) {
949 950 951 952 953 954 955 956 957 958 959
  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 已提交
960
SNode* createAlterUserStmt(SAstCreateContext* pCxt, SToken* pUserName, int8_t alterType, const SToken* pVal) {
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
  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 已提交
979
SNode* createDropUserStmt(SAstCreateContext* pCxt, SToken* pUserName) {
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  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;
  char fqdn[TSDB_FQDN_LEN] = {0};
  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 已提交
1023

1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
SNode* createAlterDnodeStmt(SAstCreateContext* pCxt, const SToken* pDnode, const SToken* pConfig, const SToken* pValue) {
  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 已提交
1035
SNode* createCreateIndexStmt(SAstCreateContext* pCxt, EIndexType type, bool ignoreExists, SToken* pIndexName, SToken* pTableName, SNodeList* pCols, SNode* pOptions) {
X
Xiaoyu Wang 已提交
1036 1037 1038 1039 1040 1041
  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 已提交
1042
  pStmt->ignoreExists = ignoreExists;
X
Xiaoyu Wang 已提交
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  strncpy(pStmt->indexName, pIndexName->z, pIndexName->n);
  strncpy(pStmt->tableName, pTableName->z, pTableName->n);
  pStmt->pCols = pCols;
  pStmt->pOptions = (SIndexOptions*)pOptions;
  return (SNode*)pStmt;
}

SNode* createIndexOption(SAstCreateContext* pCxt, SNodeList* pFuncs, SNode* pInterval, SNode* pOffset, SNode* pSliding) {
  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 已提交
1059

X
Xiaoyu Wang 已提交
1060
SNode* createDropIndexStmt(SAstCreateContext* pCxt, bool ignoreNotExists, SToken* pIndexName, SToken* pTableName) {
1061 1062 1063 1064 1065
  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 已提交
1066
  pStmt->ignoreNotExists = ignoreNotExists;
1067 1068 1069 1070 1071
  strncpy(pStmt->indexName, pIndexName->z, pIndexName->n);
  strncpy(pStmt->tableName, pTableName->z, pTableName->n);
  return (SNode*)pStmt;
}

1072 1073
SNode* createCreateComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
  SCreateComponentNodeStmt* pStmt = nodesMakeNode(type);
X
Xiaoyu Wang 已提交
1074 1075 1076 1077
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->dnodeId = strtol(pDnodeId->z, NULL, 10);;
  return (SNode*)pStmt;
}
1078

1079 1080
SNode* createDropComponentNodeStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pDnodeId) {
  SDropComponentNodeStmt* pStmt = nodesMakeNode(type);
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
  CHECK_OUT_OF_MEM(pStmt);
  pStmt->dnodeId = strtol(pDnodeId->z, NULL, 10);;
  return (SNode*)pStmt;
}

SNode* createCreateTopicStmt(SAstCreateContext* pCxt, bool ignoreExists, const SToken* pTopicName, SNode* pQuery, const SToken* pSubscribeDbName) {
  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);
  }
  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;
}
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114

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;
}
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

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;
}
1142 1143

SNode* createDescribeStmt(SAstCreateContext* pCxt, SNode* pRealTable) {
X
bugfix  
Xiaoyu Wang 已提交
1144 1145 1146
  if (NULL == pRealTable) {
    return NULL;
  }
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
  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;
}
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

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

SNode* createCreateFunctionStmt(SAstCreateContext* pCxt, bool aggFunc, const SToken* pFuncName, const SToken* pLibPath, SDataType dataType, int32_t bufSize) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_CREATE_FUNCTION_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createDropFunctionStmt(SAstCreateContext* pCxt, const SToken* pFuncName) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_DROP_FUNCTION_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createCreateStreamStmt(SAstCreateContext* pCxt, const SToken* pStreamName, const SToken* pTableName, SNode* pQuery) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_CREATE_STREAM_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createDropStreamStmt(SAstCreateContext* pCxt, const SToken* pStreamName) {
  SNode* pStmt = nodesMakeNode(QUERY_NODE_DROP_STREAM_STMT);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

SNode* createKillStmt(SAstCreateContext* pCxt, ENodeType type, const SToken* pId) {
  SNode* pStmt = nodesMakeNode(type);
  CHECK_OUT_OF_MEM(pStmt);
  return pStmt;
}

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;
}