parTranslater.c 166.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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 已提交
16
#include "parInt.h"
17

X
Xiaoyu Wang 已提交
18
#include "catalog.h"
19
#include "cmdnodes.h"
X
Xiaoyu Wang 已提交
20
#include "filter.h"
21
#include "functionMgt.h"
X
Xiaoyu Wang 已提交
22
#include "parUtil.h"
23
#include "scalar.h"
24
#include "systable.h"
X
Xiaoyu Wang 已提交
25
#include "tglobal.h"
26
#include "ttime.h"
27

X
Xiaoyu Wang 已提交
28
#define generateDealNodeErrMsg(pCxt, code, ...) \
29
  (pCxt->errCode = generateSyntaxErrMsg(&pCxt->msgBuf, code, ##__VA_ARGS__), DEAL_RES_ERROR)
X
Xiaoyu Wang 已提交
30

31
typedef struct STranslateContext {
dengyihao's avatar
dengyihao 已提交
32 33 34 35 36 37 38 39 40 41
  SParseContext*   pParseCxt;
  int32_t          errCode;
  SMsgBuf          msgBuf;
  SArray*          pNsLevel;  // element is SArray*, the element of this subarray is STableNode*
  int32_t          currLevel;
  ESqlClause       currClause;
  SSelectStmt*     pCurrStmt;
  SCmdMsgInfo*     pCmdMsg;
  SHashObj*        pDbs;
  SHashObj*        pTables;
42
  SExplainOptions* pExplainOpt;
43 44
} STranslateContext;

X
Xiaoyu Wang 已提交
45 46 47 48
typedef struct SFullDatabaseName {
  char fullDbName[TSDB_DB_FNAME_LEN];
} SFullDatabaseName;

49
static int32_t translateSubquery(STranslateContext* pCxt, SNode* pNode);
50 51
static int32_t translateQuery(STranslateContext* pCxt, SNode* pNode);

dengyihao's avatar
dengyihao 已提交
52
static bool afterGroupBy(ESqlClause clause) { return clause > SQL_CLAUSE_GROUP_BY; }
53

dengyihao's avatar
dengyihao 已提交
54
static bool beforeHaving(ESqlClause clause) { return clause < SQL_CLAUSE_HAVING; }
55

56 57
static bool afterHaving(ESqlClause clause) { return clause > SQL_CLAUSE_HAVING; }

58
static int32_t addNamespace(STranslateContext* pCxt, void* pTable) {
59 60 61 62
  size_t currTotalLevel = taosArrayGetSize(pCxt->pNsLevel);
  if (currTotalLevel > pCxt->currLevel) {
    SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
    taosArrayPush(pTables, &pTable);
63
  } else {
64 65 66 67 68 69 70 71
    do {
      SArray* pTables = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
      if (pCxt->currLevel == currTotalLevel) {
        taosArrayPush(pTables, &pTable);
      }
      taosArrayPush(pCxt->pNsLevel, &pTables);
      ++currTotalLevel;
    } while (currTotalLevel <= pCxt->currLevel);
72 73 74 75
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
76
static SName* toName(int32_t acctId, const char* pDbName, const char* pTableName, SName* pName) {
77 78
  pName->type = TSDB_TABLE_NAME_T;
  pName->acctId = acctId;
X
Xiaoyu Wang 已提交
79 80
  strcpy(pName->dbname, pDbName);
  strcpy(pName->tname, pTableName);
81 82 83
  return pName;
}

D
dapan1121 已提交
84
static int32_t collectUseDatabaseImpl(const char* pFullDbName, SHashObj* pDbs) {
X
Xiaoyu Wang 已提交
85 86 87 88 89
  SFullDatabaseName name = {0};
  strcpy(name.fullDbName, pFullDbName);
  return taosHashPut(pDbs, pFullDbName, strlen(pFullDbName), &name, sizeof(SFullDatabaseName));
}

D
dapan1121 已提交
90 91 92 93 94 95
static int32_t collectUseDatabase(const SName* pName, SHashObj* pDbs) {
  char dbFName[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(pName, dbFName);
  return collectUseDatabaseImpl(dbFName, pDbs);
}

X
Xiaoyu Wang 已提交
96 97 98 99 100 101 102 103
static int32_t collectUseTable(const SName* pName, SHashObj* pDbs) {
  char fullName[TSDB_TABLE_FNAME_LEN];
  tNameExtractFullName(pName, fullName);
  return taosHashPut(pDbs, fullName, strlen(fullName), pName, sizeof(SName));
}

static int32_t getTableMetaImpl(STranslateContext* pCxt, const SName* pName, STableMeta** pMeta) {
  SParseContext* pParCxt = pCxt->pParseCxt;
dengyihao's avatar
dengyihao 已提交
104
  int32_t        code = collectUseDatabase(pName, pCxt->pDbs);
D
dapan1121 已提交
105 106 107
  if (TSDB_CODE_SUCCESS == code) {
    code = collectUseTable(pName, pCxt->pTables);
  }
X
Xiaoyu Wang 已提交
108 109 110
  if (TSDB_CODE_SUCCESS == code) {
    code = catalogGetTableMeta(pParCxt->pCatalog, pParCxt->pTransporter, &pParCxt->mgmtEpSet, pName, pMeta);
  }
X
Xiaoyu Wang 已提交
111
  if (TSDB_CODE_SUCCESS != code) {
dengyihao's avatar
dengyihao 已提交
112 113
    parserError("catalogGetTableMeta error, code:%s, dbName:%s, tbName:%s", tstrerror(code), pName->dbname,
                pName->tname);
X
Xiaoyu Wang 已提交
114 115 116 117
  }
  return code;
}

X
Xiaoyu Wang 已提交
118
static int32_t getTableMeta(STranslateContext* pCxt, const char* pDbName, const char* pTableName, STableMeta** pMeta) {
X
Xiaoyu Wang 已提交
119 120
  SName name;
  return getTableMetaImpl(pCxt, toName(pCxt->pParseCxt->acctId, pDbName, pTableName, &name), pMeta);
X
Xiaoyu Wang 已提交
121 122
}

X
Xiaoyu Wang 已提交
123 124
static int32_t getTableDistVgInfo(STranslateContext* pCxt, const SName* pName, SArray** pVgInfo) {
  SParseContext* pParCxt = pCxt->pParseCxt;
dengyihao's avatar
dengyihao 已提交
125
  int32_t        code = collectUseDatabase(pName, pCxt->pDbs);
D
dapan1121 已提交
126 127 128
  if (TSDB_CODE_SUCCESS == code) {
    code = collectUseTable(pName, pCxt->pTables);
  }
X
Xiaoyu Wang 已提交
129 130 131
  if (TSDB_CODE_SUCCESS == code) {
    code = catalogGetTableDistVgInfo(pParCxt->pCatalog, pParCxt->pTransporter, &pParCxt->mgmtEpSet, pName, pVgInfo);
  }
X
Xiaoyu Wang 已提交
132
  if (TSDB_CODE_SUCCESS != code) {
dengyihao's avatar
dengyihao 已提交
133 134
    parserError("catalogGetTableDistVgInfo error, code:%s, dbName:%s, tbName:%s", tstrerror(code), pName->dbname,
                pName->tname);
X
Xiaoyu Wang 已提交
135 136 137 138
  }
  return code;
}

X
Xiaoyu Wang 已提交
139 140
static int32_t getDBVgInfoImpl(STranslateContext* pCxt, const SName* pName, SArray** pVgInfo) {
  SParseContext* pParCxt = pCxt->pParseCxt;
dengyihao's avatar
dengyihao 已提交
141
  char           fullDbName[TSDB_DB_FNAME_LEN];
X
Xiaoyu Wang 已提交
142
  tNameGetFullDbName(pName, fullDbName);
D
dapan1121 已提交
143
  int32_t code = collectUseDatabaseImpl(fullDbName, pCxt->pDbs);
X
Xiaoyu Wang 已提交
144 145 146
  if (TSDB_CODE_SUCCESS == code) {
    code = catalogGetDBVgInfo(pParCxt->pCatalog, pParCxt->pTransporter, &pParCxt->mgmtEpSet, fullDbName, pVgInfo);
  }
X
Xiaoyu Wang 已提交
147 148 149 150 151 152
  if (TSDB_CODE_SUCCESS != code) {
    parserError("catalogGetDBVgInfo error, code:%s, dbFName:%s", tstrerror(code), fullDbName);
  }
  return code;
}

X
Xiaoyu Wang 已提交
153
static int32_t getDBVgInfo(STranslateContext* pCxt, const char* pDbName, SArray** pVgInfo) {
X
Xiaoyu Wang 已提交
154
  SName name;
X
Xiaoyu Wang 已提交
155
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pDbName, strlen(pDbName));
X
Xiaoyu Wang 已提交
156 157 158 159 160
  char dbFname[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFname);
  return getDBVgInfoImpl(pCxt, &name, pVgInfo);
}

X
Xiaoyu Wang 已提交
161 162
static int32_t getTableHashVgroupImpl(STranslateContext* pCxt, const SName* pName, SVgroupInfo* pInfo) {
  SParseContext* pParCxt = pCxt->pParseCxt;
dengyihao's avatar
dengyihao 已提交
163
  int32_t        code = collectUseDatabase(pName, pCxt->pDbs);
D
dapan1121 已提交
164 165 166
  if (TSDB_CODE_SUCCESS == code) {
    code = collectUseTable(pName, pCxt->pTables);
  }
X
Xiaoyu Wang 已提交
167 168 169
  if (TSDB_CODE_SUCCESS == code) {
    code = catalogGetTableHashVgroup(pParCxt->pCatalog, pParCxt->pTransporter, &pParCxt->mgmtEpSet, pName, pInfo);
  }
X
Xiaoyu Wang 已提交
170
  if (TSDB_CODE_SUCCESS != code) {
dengyihao's avatar
dengyihao 已提交
171 172
    parserError("catalogGetTableHashVgroup error, code:%s, dbName:%s, tbName:%s", tstrerror(code), pName->dbname,
                pName->tname);
X
Xiaoyu Wang 已提交
173 174 175 176
  }
  return code;
}

dengyihao's avatar
dengyihao 已提交
177 178
static int32_t getTableHashVgroup(STranslateContext* pCxt, const char* pDbName, const char* pTableName,
                                  SVgroupInfo* pInfo) {
X
Xiaoyu Wang 已提交
179 180
  SName name;
  return getTableHashVgroupImpl(pCxt, toName(pCxt->pParseCxt->acctId, pDbName, pTableName, &name), pInfo);
X
Xiaoyu Wang 已提交
181 182
}

dengyihao's avatar
dengyihao 已提交
183 184
static int32_t getDBVgVersion(STranslateContext* pCxt, const char* pDbFName, int32_t* pVersion, int64_t* pDbId,
                              int32_t* pTableNum) {
X
Xiaoyu Wang 已提交
185
  SParseContext* pParCxt = pCxt->pParseCxt;
dengyihao's avatar
dengyihao 已提交
186
  int32_t        code = collectUseDatabaseImpl(pDbFName, pCxt->pDbs);
X
Xiaoyu Wang 已提交
187 188 189
  if (TSDB_CODE_SUCCESS == code) {
    code = catalogGetDBVgVersion(pParCxt->pCatalog, pDbFName, pVersion, pDbId, pTableNum);
  }
X
Xiaoyu Wang 已提交
190 191 192 193 194 195
  if (TSDB_CODE_SUCCESS != code) {
    parserError("catalogGetDBVgVersion error, code:%s, dbFName:%s", tstrerror(code), pDbFName);
  }
  return code;
}

196 197
static int32_t getDBCfg(STranslateContext* pCxt, const char* pDbName, SDbCfgInfo* pInfo) {
  SParseContext* pParCxt = pCxt->pParseCxt;
L
Liu Jicong 已提交
198
  SName          name;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pDbName, strlen(pDbName));
  char dbFname[TSDB_DB_FNAME_LEN] = {0};
  tNameGetFullDbName(&name, dbFname);
  int32_t code = collectUseDatabaseImpl(dbFname, pCxt->pDbs);
  if (TSDB_CODE_SUCCESS == code) {
    code = catalogGetDBCfg(pParCxt->pCatalog, pParCxt->pTransporter, &pParCxt->mgmtEpSet, dbFname, pInfo);
  }
  if (TSDB_CODE_SUCCESS != code) {
    parserError("catalogGetDBCfg error, code:%s, dbFName:%s", tstrerror(code), dbFname);
  }
  return code;
}

static int32_t initTranslateContext(SParseContext* pParseCxt, STranslateContext* pCxt) {
  pCxt->pParseCxt = pParseCxt;
  pCxt->errCode = TSDB_CODE_SUCCESS;
  pCxt->msgBuf.buf = pParseCxt->pMsg;
  pCxt->msgBuf.len = pParseCxt->msgLen;
  pCxt->pNsLevel = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
  pCxt->currLevel = 0;
  pCxt->currClause = 0;
  pCxt->pDbs = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  pCxt->pTables = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
  if (NULL == pCxt->pNsLevel || NULL == pCxt->pDbs || NULL == pCxt->pTables) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static int32_t resetTranslateNamespace(STranslateContext* pCxt) {
  if (NULL != pCxt->pNsLevel) {
    size_t size = taosArrayGetSize(pCxt->pNsLevel);
    for (size_t i = 0; i < size; ++i) {
      taosArrayDestroy(taosArrayGetP(pCxt->pNsLevel, i));
    }
    taosArrayDestroy(pCxt->pNsLevel);
  }
  pCxt->pNsLevel = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
  if (NULL == pCxt->pNsLevel) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  return TSDB_CODE_SUCCESS;
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
static void destroyTranslateContext(STranslateContext* pCxt) {
  if (NULL != pCxt->pNsLevel) {
    size_t size = taosArrayGetSize(pCxt->pNsLevel);
    for (size_t i = 0; i < size; ++i) {
      taosArrayDestroy(taosArrayGetP(pCxt->pNsLevel, i));
    }
    taosArrayDestroy(pCxt->pNsLevel);
  }

  if (NULL != pCxt->pCmdMsg) {
    taosMemoryFreeClear(pCxt->pCmdMsg->pMsg);
    taosMemoryFreeClear(pCxt->pCmdMsg);
  }

  taosHashCleanup(pCxt->pDbs);
  taosHashCleanup(pCxt->pTables);
}

261 262 263 264 265 266 267 268 269 270 271 272
static bool isAliasColumn(const SNode* pNode) {
  return (QUERY_NODE_COLUMN == nodeType(pNode) && ('\0' == ((SColumnNode*)pNode)->tableAlias[0]));
}

static bool isAggFunc(const SNode* pNode) {
  return (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsAggFunc(((SFunctionNode*)pNode)->funcId));
}

static bool isSelectFunc(const SNode* pNode) {
  return (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsSelectFunc(((SFunctionNode*)pNode)->funcId));
}

273 274 275 276
static bool isTimelineFunc(const SNode* pNode) {
  return (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsTimelineFunc(((SFunctionNode*)pNode)->funcId));
}

277 278 279 280
static bool isScanPseudoColumnFunc(const SNode* pNode) {
  return (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsScanPseudoColumnFunc(((SFunctionNode*)pNode)->funcId));
}

281 282 283 284
static bool isNonstandardSQLFunc(const SNode* pNode) {
  return (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsNonstandardSQLFunc(((SFunctionNode*)pNode)->funcId));
}

285 286 287 288
static bool isDistinctOrderBy(STranslateContext* pCxt) {
  return (SQL_CLAUSE_ORDER_BY == pCxt->currClause && pCxt->pCurrStmt->isDistinct);
}

289 290 291 292 293
static bool belongTable(const char* currentDb, const SColumnNode* pCol, const STableNode* pTable) {
  int cmp = 0;
  if ('\0' != pCol->dbName[0]) {
    cmp = strcmp(pCol->dbName, pTable->dbName);
  } else {
294
    cmp = (QUERY_NODE_REAL_TABLE == nodeType(pTable) ? strcmp(currentDb, pTable->dbName) : 0);
295 296 297 298 299 300 301
  }
  if (0 == cmp) {
    cmp = strcmp(pCol->tableAlias, pTable->tableAlias);
  }
  return (0 == cmp);
}

X
Xiaoyu Wang 已提交
302
static SNodeList* getProjectList(const SNode* pNode) {
303 304
  if (QUERY_NODE_SELECT_STMT == nodeType(pNode)) {
    return ((SSelectStmt*)pNode)->pProjectionList;
X
Xiaoyu Wang 已提交
305 306
  } else if (QUERY_NODE_SET_OPERATOR == nodeType(pNode)) {
    return ((SSetOperator*)pNode)->pProjectionList;
307 308 309 310
  }
  return NULL;
}

dengyihao's avatar
dengyihao 已提交
311 312
static void setColumnInfoBySchema(const SRealTableNode* pTable, const SSchema* pColSchema, bool isTag,
                                  SColumnNode* pCol) {
X
Xiaoyu Wang 已提交
313 314 315
  strcpy(pCol->dbName, pTable->table.dbName);
  strcpy(pCol->tableAlias, pTable->table.tableAlias);
  strcpy(pCol->tableName, pTable->table.tableName);
316 317 318 319
  strcpy(pCol->colName, pColSchema->name);
  if ('\0' == pCol->node.aliasName[0]) {
    strcpy(pCol->node.aliasName, pColSchema->name);
  }
X
Xiaoyu Wang 已提交
320
  pCol->tableId = pTable->pMeta->uid;
X
Xiaoyu Wang 已提交
321
  pCol->tableType = pTable->pMeta->tableType;
322
  pCol->colId = pColSchema->colId;
X
Xiaoyu Wang 已提交
323
  pCol->colType = isTag ? COLUMN_TYPE_TAG : COLUMN_TYPE_COLUMN;
324
  pCol->node.resType.type = pColSchema->type;
325
  pCol->node.resType.bytes = pColSchema->bytes;
X
Xiaoyu Wang 已提交
326 327 328
  if (TSDB_DATA_TYPE_TIMESTAMP == pCol->node.resType.type) {
    pCol->node.resType.precision = pTable->pMeta->tableInfo.precision;
  }
329 330 331 332
}

static void setColumnInfoByExpr(const STableNode* pTable, SExprNode* pExpr, SColumnNode* pCol) {
  pCol->pProjectRef = (SNode*)pExpr;
333 334 335 336
  if (NULL == pExpr->pAssociation) {
    pExpr->pAssociation = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
  }
  taosArrayPush(pExpr->pAssociation, &pCol);
337 338
  if (NULL != pTable) {
    strcpy(pCol->tableAlias, pTable->tableAlias);
339
  } else if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
340 341 342 343 344
    SColumnNode* pProjCol = (SColumnNode*)pExpr;
    strcpy(pCol->tableAlias, pProjCol->tableAlias);
    pCol->tableId = pProjCol->tableId;
    pCol->colId = pProjCol->colId;
    pCol->colType = pProjCol->colType;
345
  }
346
  strcpy(pCol->colName, pExpr->aliasName);
X
Xiaoyu Wang 已提交
347 348 349
  if ('\0' == pCol->node.aliasName[0]) {
    strcpy(pCol->node.aliasName, pCol->colName);
  }
350 351 352
  pCol->node.resType = pExpr->resType;
}

X
Xiaoyu Wang 已提交
353
static int32_t createColumnsByTable(STranslateContext* pCxt, const STableNode* pTable, SNodeList* pList) {
354 355
  if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
    const STableMeta* pMeta = ((SRealTableNode*)pTable)->pMeta;
dengyihao's avatar
dengyihao 已提交
356 357
    int32_t           nums =
        pMeta->tableInfo.numOfColumns + ((TSDB_SUPER_TABLE == pMeta->tableType) ? pMeta->tableInfo.numOfTags : 0);
358 359
    for (int32_t i = 0; i < nums; ++i) {
      SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
360
      if (NULL == pCol) {
361
        return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
362
      }
H
Haojun Liao 已提交
363
      setColumnInfoBySchema((SRealTableNode*)pTable, pMeta->schema + i, (i >= pMeta->tableInfo.numOfColumns), pCol);
364 365 366 367
      nodesListAppend(pList, (SNode*)pCol);
    }
  } else {
    SNodeList* pProjectList = getProjectList(((STempTableNode*)pTable)->pSubquery);
dengyihao's avatar
dengyihao 已提交
368
    SNode*     pNode;
369 370
    FOREACH(pNode, pProjectList) {
      SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
371
      if (NULL == pCol) {
372
        return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
373
      }
374
      setColumnInfoByExpr(pTable, (SExprNode*)pNode, pCol);
375 376 377
      nodesListAppend(pList, (SNode*)pCol);
    }
  }
X
Xiaoyu Wang 已提交
378
  return TSDB_CODE_SUCCESS;
379 380
}

X
Xiaoyu Wang 已提交
381 382 383 384
static bool isInternalPrimaryKey(const SColumnNode* pCol) {
  return PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId && 0 == strcmp(pCol->colName, PK_TS_COL_INTERNAL_NAME);
}

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
static bool isTimeOrderQuery(SNode* pStmt) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    return ((SSelectStmt*)pStmt)->isTimeOrderQuery;
  } else {
    return false;
  }
}

static bool isPrimaryKeyImpl(STempTableNode* pTable, SNode* pExpr) {
  if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
    return (PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pExpr)->colId);
  } else if (QUERY_NODE_FUNCTION == nodeType(pExpr)) {
    SFunctionNode* pFunc = (SFunctionNode*)pExpr;
    if (FUNCTION_TYPE_SELECT_VALUE == pFunc->funcType) {
      return isPrimaryKeyImpl(pTable, nodesListGetNode(pFunc->pParameterList, 0));
    } else if (FUNCTION_TYPE_WSTARTTS == pFunc->funcType || FUNCTION_TYPE_WENDTS == pFunc->funcType) {
      return true;
    }
  }
  return false;
}

static bool isPrimaryKey(STempTableNode* pTable, SNode* pExpr) {
  if (!isTimeOrderQuery(pTable->pSubquery)) {
    return false;
  }
  return isPrimaryKeyImpl(pTable, pExpr);
}

414 415 416 417
static bool findAndSetColumn(SColumnNode* pCol, const STableNode* pTable) {
  bool found = false;
  if (QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
    const STableMeta* pMeta = ((SRealTableNode*)pTable)->pMeta;
X
Xiaoyu Wang 已提交
418
    if (isInternalPrimaryKey(pCol)) {
X
bugfix  
Xiaoyu Wang 已提交
419 420 421
      setColumnInfoBySchema((SRealTableNode*)pTable, pMeta->schema, false, pCol);
      return true;
    }
422 423 424
    int32_t nums = pMeta->tableInfo.numOfTags + pMeta->tableInfo.numOfColumns;
    for (int32_t i = 0; i < nums; ++i) {
      if (0 == strcmp(pCol->colName, pMeta->schema[i].name)) {
H
Haojun Liao 已提交
425
        setColumnInfoBySchema((SRealTableNode*)pTable, pMeta->schema + i, (i >= pMeta->tableInfo.numOfColumns), pCol);
426 427 428 429 430 431
        found = true;
        break;
      }
    }
  } else {
    SNodeList* pProjectList = getProjectList(((STempTableNode*)pTable)->pSubquery);
dengyihao's avatar
dengyihao 已提交
432
    SNode*     pNode;
433 434
    FOREACH(pNode, pProjectList) {
      SExprNode* pExpr = (SExprNode*)pNode;
X
Xiaoyu Wang 已提交
435
      if (0 == strcmp(pCol->colName, pExpr->aliasName) ||
436
          (isPrimaryKey((STempTableNode*)pTable, pNode) && isInternalPrimaryKey(pCol))) {
437
        setColumnInfoByExpr(pTable, pExpr, pCol);
438 439 440 441 442 443 444 445
        found = true;
        break;
      }
    }
  }
  return found;
}

446
static EDealRes translateColumnWithPrefix(STranslateContext* pCxt, SColumnNode* pCol) {
447
  SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
dengyihao's avatar
dengyihao 已提交
448 449
  size_t  nums = taosArrayGetSize(pTables);
  bool    foundTable = false;
450 451 452
  for (size_t i = 0; i < nums; ++i) {
    STableNode* pTable = taosArrayGetP(pTables, i);
    if (belongTable(pCxt->pParseCxt->db, pCol, pTable)) {
453
      foundTable = true;
454 455
      if (findAndSetColumn(pCol, pTable)) {
        break;
456
      }
457
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_COLUMN, pCol->colName);
458
    }
459
  }
460
  if (!foundTable) {
461
    return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_TABLE_NOT_EXIST, pCol->tableAlias);
462
  }
463
  return DEAL_RES_CONTINUE;
464
}
465

466
static EDealRes translateColumnWithoutPrefix(STranslateContext* pCxt, SColumnNode* pCol) {
467
  SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
dengyihao's avatar
dengyihao 已提交
468 469
  size_t  nums = taosArrayGetSize(pTables);
  bool    found = false;
470
  bool    isInternalPk = isInternalPrimaryKey(pCol);
471 472 473 474
  for (size_t i = 0; i < nums; ++i) {
    STableNode* pTable = taosArrayGetP(pTables, i);
    if (findAndSetColumn(pCol, pTable)) {
      if (found) {
475
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_AMBIGUOUS_COLUMN, pCol->colName);
476 477
      }
      found = true;
478 479 480
      if (isInternalPk) {
        break;
      }
481
    }
482 483
  }
  if (!found) {
484
    if (isInternalPk) {
485 486 487
      if (NULL != pCxt->pCurrStmt->pWindow) {
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_NOT_ALLOWED_WIN_QUERY);
      }
X
Xiaoyu Wang 已提交
488 489 490 491
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_INTERNAL_PK);
    } else {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_COLUMN, pCol->colName);
    }
492
  }
493
  return DEAL_RES_CONTINUE;
494 495
}

496
static bool translateColumnUseAlias(STranslateContext* pCxt, SColumnNode* pCol) {
497
  SNodeList* pProjectionList = pCxt->pCurrStmt->pProjectionList;
dengyihao's avatar
dengyihao 已提交
498
  SNode*     pNode;
499 500 501
  FOREACH(pNode, pProjectionList) {
    SExprNode* pExpr = (SExprNode*)pNode;
    if (0 == strcmp(pCol->colName, pExpr->aliasName)) {
dengyihao's avatar
dengyihao 已提交
502 503
      setColumnInfoByExpr(NULL, pExpr, pCol);
      return true;
504 505 506 507 508
    }
  }
  return false;
}

509
static EDealRes translateColumn(STranslateContext* pCxt, SColumnNode* pCol) {
X
Xiaoyu Wang 已提交
510
  // count(*)/first(*)/last(*) and so on
511
  if (0 == strcmp(pCol->colName, "*")) {
512
    return DEAL_RES_CONTINUE;
513
  }
X
Xiaoyu Wang 已提交
514 515

  EDealRes res = DEAL_RES_CONTINUE;
516
  if ('\0' != pCol->tableAlias[0]) {
X
Xiaoyu Wang 已提交
517 518 519 520 521 522 523
    res = translateColumnWithPrefix(pCxt, pCol);
  } else {
    bool found = false;
    if (SQL_CLAUSE_ORDER_BY == pCxt->currClause) {
      found = translateColumnUseAlias(pCxt, pCol);
    }
    res = (found ? DEAL_RES_CONTINUE : translateColumnWithoutPrefix(pCxt, pCol));
524
  }
X
Xiaoyu Wang 已提交
525
  return res;
526 527
}

528 529 530 531 532 533 534 535 536 537 538 539
static int32_t parseTimeFromValueNode(SValueNode* pVal) {
  if (IS_SIGNED_NUMERIC_TYPE(pVal->node.resType.type)) {
    return TSDB_CODE_SUCCESS;
  } else if (IS_UNSIGNED_NUMERIC_TYPE(pVal->node.resType.type)) {
    pVal->datum.i = pVal->datum.u;
    return TSDB_CODE_SUCCESS;
  } else if (IS_FLOAT_TYPE(pVal->node.resType.type)) {
    pVal->datum.i = pVal->datum.d;
    return TSDB_CODE_SUCCESS;
  } else if (TSDB_DATA_TYPE_BOOL == pVal->node.resType.type) {
    pVal->datum.i = pVal->datum.b;
    return TSDB_CODE_SUCCESS;
540
  } else if (IS_VAR_DATA_TYPE(pVal->node.resType.type) || TSDB_DATA_TYPE_TIMESTAMP == pVal->node.resType.type) {
541 542 543 544 545
    if (TSDB_CODE_SUCCESS == taosParseTime(pVal->literal, &pVal->datum.i, pVal->node.resType.bytes,
                                           pVal->node.resType.precision, tsDaylight)) {
      return TSDB_CODE_SUCCESS;
    }
    char* pEnd = NULL;
wafwerar's avatar
wafwerar 已提交
546
    pVal->datum.i = taosStr2Int64(pVal->literal, &pEnd, 10);
547 548 549 550 551 552
    return (NULL != pEnd && '\0' == *pEnd) ? TSDB_CODE_SUCCESS : TSDB_CODE_FAILED;
  } else {
    return TSDB_CODE_FAILED;
  }
}

553 554
static EDealRes translateValueImpl(STranslateContext* pCxt, SValueNode* pVal, SDataType targetDt) {
  uint8_t precision = (NULL != pCxt->pCurrStmt ? pCxt->pCurrStmt->precision : targetDt.precision);
555
  pVal->node.resType.precision = precision;
556 557 558
  if (pVal->placeholderNo > 0) {
    return DEAL_RES_CONTINUE;
  }
559
  if (pVal->isDuration) {
dengyihao's avatar
dengyihao 已提交
560 561
    if (parseNatualDuration(pVal->literal, strlen(pVal->literal), &pVal->datum.i, &pVal->unit, precision) !=
        TSDB_CODE_SUCCESS) {
562
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal);
563
    }
564
    *(int64_t*)&pVal->typeData = pVal->datum.i;
565
  } else {
566
    switch (targetDt.type) {
567 568 569 570
      case TSDB_DATA_TYPE_NULL:
        break;
      case TSDB_DATA_TYPE_BOOL:
        pVal->datum.b = (0 == strcasecmp(pVal->literal, "true"));
D
dapan1121 已提交
571
        *(bool*)&pVal->typeData = pVal->datum.b;
572
        break;
X
Xiaoyu Wang 已提交
573
      case TSDB_DATA_TYPE_TINYINT: {
D
dapan1121 已提交
574
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
575
        pVal->datum.i = taosStr2Int64(pVal->literal, &endPtr, 10);
D
dapan1121 已提交
576 577 578
        *(int8_t*)&pVal->typeData = pVal->datum.i;
        break;
      }
X
Xiaoyu Wang 已提交
579
      case TSDB_DATA_TYPE_SMALLINT: {
D
dapan1121 已提交
580
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
581
        pVal->datum.i = taosStr2Int64(pVal->literal, &endPtr, 10);
D
dapan1121 已提交
582 583 584
        *(int16_t*)&pVal->typeData = pVal->datum.i;
        break;
      }
X
Xiaoyu Wang 已提交
585
      case TSDB_DATA_TYPE_INT: {
D
dapan1121 已提交
586
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
587
        pVal->datum.i = taosStr2Int64(pVal->literal, &endPtr, 10);
D
dapan1121 已提交
588 589 590
        *(int32_t*)&pVal->typeData = pVal->datum.i;
        break;
      }
591 592
      case TSDB_DATA_TYPE_BIGINT: {
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
593
        pVal->datum.i = taosStr2Int64(pVal->literal, &endPtr, 10);
X
Xiaoyu Wang 已提交
594
        *(int64_t*)&pVal->typeData = pVal->datum.i;
D
dapan1121 已提交
595 596
        break;
      }
X
Xiaoyu Wang 已提交
597
      case TSDB_DATA_TYPE_UTINYINT: {
D
dapan1121 已提交
598
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
599
        pVal->datum.u = taosStr2UInt64(pVal->literal, &endPtr, 10);
X
Xiaoyu Wang 已提交
600
        *(uint8_t*)&pVal->typeData = pVal->datum.u;
D
dapan1121 已提交
601 602
        break;
      }
X
Xiaoyu Wang 已提交
603
      case TSDB_DATA_TYPE_USMALLINT: {
D
dapan1121 已提交
604
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
605
        pVal->datum.u = taosStr2UInt64(pVal->literal, &endPtr, 10);
X
Xiaoyu Wang 已提交
606
        *(uint16_t*)&pVal->typeData = pVal->datum.u;
D
dapan1121 已提交
607 608
        break;
      }
X
Xiaoyu Wang 已提交
609
      case TSDB_DATA_TYPE_UINT: {
D
dapan1121 已提交
610
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
611
        pVal->datum.u = taosStr2UInt64(pVal->literal, &endPtr, 10);
X
Xiaoyu Wang 已提交
612
        *(uint32_t*)&pVal->typeData = pVal->datum.u;
613 614
        break;
      }
X
Xiaoyu Wang 已提交
615
      case TSDB_DATA_TYPE_UBIGINT: {
616
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
617
        pVal->datum.u = taosStr2UInt64(pVal->literal, &endPtr, 10);
X
Xiaoyu Wang 已提交
618
        *(uint64_t*)&pVal->typeData = pVal->datum.u;
D
dapan1121 已提交
619 620
        break;
      }
X
Xiaoyu Wang 已提交
621
      case TSDB_DATA_TYPE_FLOAT: {
D
dapan1121 已提交
622
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
623
        pVal->datum.d = taosStr2Double(pVal->literal, &endPtr);
X
Xiaoyu Wang 已提交
624
        *(float*)&pVal->typeData = pVal->datum.d;
625 626
        break;
      }
627 628
      case TSDB_DATA_TYPE_DOUBLE: {
        char* endPtr = NULL;
wafwerar's avatar
wafwerar 已提交
629
        pVal->datum.d = taosStr2Double(pVal->literal, &endPtr);
X
Xiaoyu Wang 已提交
630
        *(double*)&pVal->typeData = pVal->datum.d;
631 632
        break;
      }
633 634
      case TSDB_DATA_TYPE_VARCHAR:
      case TSDB_DATA_TYPE_VARBINARY: {
635
        pVal->datum.p = taosMemoryCalloc(1, targetDt.bytes + VARSTR_HEADER_SIZE + 1);
X
Xiaoyu Wang 已提交
636
        if (NULL == pVal->datum.p) {
637
          return generateDealNodeErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
638
        }
639 640
        varDataSetLen(pVal->datum.p, targetDt.bytes);
        strncpy(varDataVal(pVal->datum.p), pVal->literal, targetDt.bytes);
641 642 643
        break;
      }
      case TSDB_DATA_TYPE_TIMESTAMP: {
644
        if (TSDB_CODE_SUCCESS != parseTimeFromValueNode(pVal)) {
645
          return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal);
646
        }
X
Xiaoyu Wang 已提交
647
        *(int64_t*)&pVal->typeData = pVal->datum.i;
648 649
        break;
      }
650
      case TSDB_DATA_TYPE_NCHAR: {
651 652
        int32_t bytes = targetDt.bytes * TSDB_NCHAR_SIZE;
        pVal->datum.p = taosMemoryCalloc(1, bytes + VARSTR_HEADER_SIZE + 1);
653 654 655 656 657 658
        if (NULL == pVal->datum.p) {
          return generateDealNodeErrMsg(pCxt, TSDB_CODE_OUT_OF_MEMORY);
          ;
        }

        int32_t output = 0;
659
        if (!taosMbsToUcs4(pVal->literal, pVal->node.resType.bytes, (TdUcs4*)varDataVal(pVal->datum.p), bytes,
660 661 662 663 664 665
                           &output)) {
          return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal);
        }
        varDataSetLen(pVal->datum.p, output);
        break;
      }
666 667 668
      case TSDB_DATA_TYPE_JSON:
      case TSDB_DATA_TYPE_DECIMAL:
      case TSDB_DATA_TYPE_BLOB:
669
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal);
670 671 672 673
      default:
        break;
    }
  }
674
  pVal->node.resType = targetDt;
675
  pVal->translate = true;
676
  return DEAL_RES_CONTINUE;
677 678
}

679 680 681 682
static EDealRes translateValue(STranslateContext* pCxt, SValueNode* pVal) {
  return translateValueImpl(pCxt, pVal, pVal->node.resType);
}

683
static EDealRes translateOperator(STranslateContext* pCxt, SOperatorNode* pOp) {
X
Xiaoyu Wang 已提交
684
  if (nodesIsUnaryOp(pOp)) {
D
dapan1121 已提交
685 686 687 688 689 690
    if (OP_TYPE_MINUS == pOp->opType) {
      if (!IS_MATHABLE_TYPE(((SExprNode*)(pOp->pLeft))->resType.type)) {
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pLeft))->aliasName);
      }
      pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE;
      pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes;
691 692 693
    } else {
      pOp->node.resType.type = TSDB_DATA_TYPE_BOOL;
      pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
X
Xiaoyu Wang 已提交
694
    }
X
Xiaoyu Wang 已提交
695 696
    return DEAL_RES_CONTINUE;
  }
697 698 699
  SDataType ldt = ((SExprNode*)(pOp->pLeft))->resType;
  SDataType rdt = ((SExprNode*)(pOp->pRight))->resType;
  if (nodesIsArithmeticOp(pOp)) {
dengyihao's avatar
dengyihao 已提交
700 701
    if (TSDB_DATA_TYPE_JSON == ldt.type || TSDB_DATA_TYPE_BLOB == ldt.type || TSDB_DATA_TYPE_JSON == rdt.type ||
        TSDB_DATA_TYPE_BLOB == rdt.type) {
702
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName);
703
    }
704
    if ((TSDB_DATA_TYPE_TIMESTAMP == ldt.type && TSDB_DATA_TYPE_TIMESTAMP == rdt.type) ||
705 706
        (TSDB_DATA_TYPE_TIMESTAMP == ldt.type && (IS_VAR_DATA_TYPE(rdt.type) || IS_FLOAT_TYPE(rdt.type))) ||
        (TSDB_DATA_TYPE_TIMESTAMP == rdt.type && (IS_VAR_DATA_TYPE(ldt.type) || IS_FLOAT_TYPE(ldt.type)))) {
707 708 709
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName);
    }

710 711 712
    if ((TSDB_DATA_TYPE_TIMESTAMP == ldt.type && IS_INTEGER_TYPE(rdt.type)) ||
        (TSDB_DATA_TYPE_TIMESTAMP == rdt.type && IS_INTEGER_TYPE(ldt.type)) ||
        (TSDB_DATA_TYPE_TIMESTAMP == ldt.type && TSDB_DATA_TYPE_BOOL == rdt.type) ||
H
Hongze Cheng 已提交
713
        (TSDB_DATA_TYPE_TIMESTAMP == rdt.type && TSDB_DATA_TYPE_BOOL == ldt.type)) {
714 715 716 717 718 719
      pOp->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
      pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
    } else {
      pOp->node.resType.type = TSDB_DATA_TYPE_DOUBLE;
      pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_DOUBLE].bytes;
    }
720
  } else if (nodesIsComparisonOp(pOp)) {
L
Liu Jicong 已提交
721
    if (TSDB_DATA_TYPE_BLOB == ldt.type || TSDB_DATA_TYPE_JSON == rdt.type || TSDB_DATA_TYPE_BLOB == rdt.type) {
722
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName);
723
    }
D
dapan1121 已提交
724 725 726
    if (OP_TYPE_IN == pOp->opType || OP_TYPE_NOT_IN == pOp->opType) {
      ((SExprNode*)pOp->pRight)->resType = ((SExprNode*)pOp->pLeft)->resType;
    }
X
Xiaoyu Wang 已提交
727
    if (nodesIsRegularOp(pOp)) {
728 729 730
      if (!IS_STR_DATA_TYPE(((SExprNode*)(pOp->pLeft))->resType.type)) {
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pLeft))->aliasName);
      }
X
Xiaoyu Wang 已提交
731 732 733 734
      if (QUERY_NODE_VALUE != nodeType(pOp->pRight) || !IS_STR_DATA_TYPE(((SExprNode*)(pOp->pRight))->resType.type)) {
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName);
      }
    }
735 736
    pOp->node.resType.type = TSDB_DATA_TYPE_BOOL;
    pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
L
Liu Jicong 已提交
737
  } else if (nodesIsJsonOp(pOp)) {
738 739 740 741 742
    if (TSDB_DATA_TYPE_JSON != ldt.type || TSDB_DATA_TYPE_BINARY != rdt.type) {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)(pOp->pRight))->aliasName);
    }
    pOp->node.resType.type = TSDB_DATA_TYPE_JSON;
    pOp->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_JSON].bytes;
743
  }
744
  return DEAL_RES_CONTINUE;
745 746
}

747
static EDealRes haveAggOrNonstdFunction(SNode* pNode, void* pContext) {
748
  if (isAggFunc(pNode)) {
X
Xiaoyu Wang 已提交
749 750
    *((bool*)pContext) = true;
    return DEAL_RES_END;
751 752 753
  } else if (isNonstandardSQLFunc(pNode)) {
    *((bool*)pContext) = true;
    return DEAL_RES_END;
X
Xiaoyu Wang 已提交
754 755 756 757
  }
  return DEAL_RES_CONTINUE;
}

758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
static int32_t findTable(STranslateContext* pCxt, const char* pTableAlias, STableNode** pOutput) {
  SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
  size_t  nums = taosArrayGetSize(pTables);
  for (size_t i = 0; i < nums; ++i) {
    STableNode* pTable = taosArrayGetP(pTables, i);
    if (NULL == pTableAlias || 0 == strcmp(pTable->tableAlias, pTableAlias)) {
      *pOutput = pTable;
      return TSDB_CODE_SUCCESS;
    }
  }
  return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_TABLE_NOT_EXIST, pTableAlias);
}

static bool isCountStar(SFunctionNode* pFunc) {
  if (FUNCTION_TYPE_COUNT != pFunc->funcType || 1 != LIST_LENGTH(pFunc->pParameterList)) {
    return false;
  }
  SNode* pPara = nodesListGetNode(pFunc->pParameterList, 0);
  return (QUERY_NODE_COLUMN == nodeType(pPara) && 0 == strcmp(((SColumnNode*)pPara)->colName, "*"));
}

// count(*) is rewritten as count(ts) for scannning optimization
static int32_t rewriteCountStar(STranslateContext* pCxt, SFunctionNode* pCount) {
  SColumnNode* pCol = nodesListGetNode(pCount->pParameterList, 0);
  STableNode*  pTable = NULL;
  int32_t      code = findTable(pCxt, ('\0' == pCol->tableAlias[0] ? NULL : pCol->tableAlias), &pTable);
  if (TSDB_CODE_SUCCESS == code && QUERY_NODE_REAL_TABLE == nodeType(pTable)) {
    setColumnInfoBySchema((SRealTableNode*)pTable, ((SRealTableNode*)pTable)->pMeta->schema, false, pCol);
  }
  return code;
}

790 791 792 793 794 795
static bool hasInvalidFuncNesting(SNodeList* pParameterList) {
  bool hasInvalidFunc = false;
  nodesWalkExprs(pParameterList, haveAggOrNonstdFunction, &hasInvalidFunc);
  return hasInvalidFunc;
}

796
static EDealRes translateFunction(STranslateContext* pCxt, SFunctionNode* pFunc) {
X
Xiaoyu Wang 已提交
797 798 799 800 801
  SFmGetFuncInfoParam param = {.pCtg = pCxt->pParseCxt->pCatalog,
                               .pRpc = pCxt->pParseCxt->pTransporter,
                               .pMgmtEps = &pCxt->pParseCxt->mgmtEpSet,
                               .pErrBuf = pCxt->msgBuf.buf,
                               .errBufLen = pCxt->msgBuf.len};
802
  pCxt->errCode = fmGetFuncInfo(&param, pFunc);
803
  if (TSDB_CODE_SUCCESS == pCxt->errCode && fmIsAggFunc(pFunc->funcId)) {
X
Xiaoyu Wang 已提交
804 805 806
    if (beforeHaving(pCxt->currClause)) {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_ILLEGAL_USE_AGG_FUNCTION);
    }
807
    if (hasInvalidFuncNesting(pFunc->pParameterList)) {
X
Xiaoyu Wang 已提交
808 809
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_AGG_FUNC_NESTING);
    }
810 811 812
    if (pCxt->pCurrStmt->hasNonstdSQLFunc) {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
    }
813

X
Xiaoyu Wang 已提交
814
    pCxt->pCurrStmt->hasAggFuncs = true;
815 816 817
    if (isCountStar(pFunc)) {
      pCxt->errCode = rewriteCountStar(pCxt, pFunc);
    }
X
Xiaoyu Wang 已提交
818 819 820 821

    if (fmIsRepeatScanFunc(pFunc->funcId)) {
      pCxt->pCurrStmt->hasRepeatScanFuncs = true;
    }
X
Xiaoyu Wang 已提交
822
  }
823 824 825 826 827 828 829 830 831 832 833 834 835
  if (TSDB_CODE_SUCCESS == pCxt->errCode && fmIsScanPseudoColumnFunc(pFunc->funcId)) {
    if (0 == LIST_LENGTH(pFunc->pParameterList)) {
      if (QUERY_NODE_REAL_TABLE != nodeType(pCxt->pCurrStmt->pFromTable)) {
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_TBNAME);
      }
    } else {
      SValueNode* pVal = nodesListGetNode(pFunc->pParameterList, 0);
      STableNode* pTable = NULL;
      pCxt->errCode = findTable(pCxt, pVal->literal, &pTable);
      if (TSDB_CODE_SUCCESS == pCxt->errCode && (NULL == pTable || QUERY_NODE_REAL_TABLE != nodeType(pTable))) {
        return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_TBNAME);
      }
    }
836 837
  }
  if (TSDB_CODE_SUCCESS == pCxt->errCode && fmIsNonstandardSQLFunc(pFunc->funcId)) {
838 839 840 841 842
    if (SQL_CLAUSE_SELECT != pCxt->currClause || pCxt->pCurrStmt->hasNonstdSQLFunc || pCxt->pCurrStmt->hasAggFuncs) {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
    }
    if (hasInvalidFuncNesting(pFunc->pParameterList)) {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_AGG_FUNC_NESTING);
843 844
    }
    pCxt->pCurrStmt->hasNonstdSQLFunc = true;
845
  }
846
  return TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_CONTINUE : DEAL_RES_ERROR;
847 848 849 850
}

static EDealRes translateExprSubquery(STranslateContext* pCxt, SNode* pNode) {
  return (TSDB_CODE_SUCCESS == translateSubquery(pCxt, pNode) ? DEAL_RES_CONTINUE : DEAL_RES_ERROR);
851 852
}

853 854 855 856 857 858
static EDealRes translateLogicCond(STranslateContext* pCxt, SLogicConditionNode* pCond) {
  pCond->node.resType.type = TSDB_DATA_TYPE_BOOL;
  pCond->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
  return DEAL_RES_CONTINUE;
}

859
static EDealRes doTranslateExpr(SNode* pNode, void* pContext) {
860 861 862 863 864 865 866 867
  STranslateContext* pCxt = (STranslateContext*)pContext;
  switch (nodeType(pNode)) {
    case QUERY_NODE_COLUMN:
      return translateColumn(pCxt, (SColumnNode*)pNode);
    case QUERY_NODE_VALUE:
      return translateValue(pCxt, (SValueNode*)pNode);
    case QUERY_NODE_OPERATOR:
      return translateOperator(pCxt, (SOperatorNode*)pNode);
868
    case QUERY_NODE_FUNCTION:
869
      return translateFunction(pCxt, (SFunctionNode*)pNode);
870 871
    case QUERY_NODE_LOGIC_CONDITION:
      return translateLogicCond(pCxt, (SLogicConditionNode*)pNode);
872
    case QUERY_NODE_TEMP_TABLE:
873
      return translateExprSubquery(pCxt, ((STempTableNode*)pNode)->pSubquery);
874 875 876
    default:
      break;
  }
877
  return DEAL_RES_CONTINUE;
878 879 880
}

static int32_t translateExpr(STranslateContext* pCxt, SNode* pNode) {
X
Xiaoyu Wang 已提交
881
  nodesWalkExprPostOrder(pNode, doTranslateExpr, pCxt);
882 883 884 885
  return pCxt->errCode;
}

static int32_t translateExprList(STranslateContext* pCxt, SNodeList* pList) {
X
Xiaoyu Wang 已提交
886
  nodesWalkExprsPostOrder(pList, doTranslateExpr, pCxt);
887 888 889
  return pCxt->errCode;
}

890
static SNodeList* getGroupByList(STranslateContext* pCxt) {
891
  if (isDistinctOrderBy(pCxt)) {
892 893 894 895 896 897 898 899 900 901 902 903 904
    return pCxt->pCurrStmt->pProjectionList;
  }
  return pCxt->pCurrStmt->pGroupByList;
}

static SNode* getGroupByNode(SNode* pNode) {
  if (QUERY_NODE_GROUPING_SET == nodeType(pNode)) {
    return nodesListGetNode(((SGroupingSetNode*)pNode)->pParameterList, 0);
  }
  return pNode;
}

static int32_t getGroupByErrorCode(STranslateContext* pCxt) {
905
  if (isDistinctOrderBy(pCxt)) {
906 907 908 909 910
    return TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION;
  }
  return TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION;
}

911 912 913 914 915 916 917 918 919 920 921 922 923
typedef struct SCheckExprForGroupByCxt {
  STranslateContext* pTranslateCxt;
  int32_t            selectFuncNum;
  bool               hasSelectValFunc;
} SCheckExprForGroupByCxt;

static EDealRes rewriteColToSelectValFunc(STranslateContext* pCxt, bool* pHasSelectValFunc, SNode** pNode) {
  SFunctionNode* pFunc = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == pFunc) {
    pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY;
    return DEAL_RES_ERROR;
  }
  strcpy(pFunc->functionName, "_select_value");
X
Xiaoyu Wang 已提交
924
  strcpy(pFunc->node.aliasName, ((SExprNode*)*pNode)->aliasName);
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
  pCxt->errCode = nodesListMakeAppend(&pFunc->pParameterList, *pNode);
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
    translateFunction(pCxt, pFunc);
  }
  if (TSDB_CODE_SUCCESS == pCxt->errCode) {
    *pNode = (SNode*)pFunc;
    if (NULL != pHasSelectValFunc) {
      *pHasSelectValFunc = true;
    }
  } else {
    nodesDestroyNode(pFunc);
  }
  return TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR;
}

static EDealRes doCheckExprForGroupBy(SNode** pNode, void* pContext) {
  SCheckExprForGroupByCxt* pCxt = (SCheckExprForGroupByCxt*)pContext;
  if (!nodesIsExprNode(*pNode) || isAliasColumn(*pNode)) {
943 944
    return DEAL_RES_CONTINUE;
  }
945 946 947 948 949
  pCxt->selectFuncNum += isSelectFunc(*pNode) ? 1 : 0;
  if (pCxt->selectFuncNum > 1 && pCxt->hasSelectValFunc) {
    return generateDealNodeErrMsg(pCxt->pTranslateCxt, getGroupByErrorCode(pCxt->pTranslateCxt));
  }
  if (isAggFunc(*pNode) && !isDistinctOrderBy(pCxt->pTranslateCxt)) {
950 951 952
    return DEAL_RES_IGNORE_CHILD;
  }
  SNode* pGroupNode;
953 954
  FOREACH(pGroupNode, getGroupByList(pCxt->pTranslateCxt)) {
    if (nodesEqualNode(getGroupByNode(pGroupNode), *pNode)) {
955 956 957
      return DEAL_RES_IGNORE_CHILD;
    }
  }
958
  if (isScanPseudoColumnFunc(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode)) {
959 960 961 962 963 964 965 966
    if (pCxt->selectFuncNum > 1) {
      return generateDealNodeErrMsg(pCxt->pTranslateCxt, getGroupByErrorCode(pCxt->pTranslateCxt));
    } else {
      return rewriteColToSelectValFunc(pCxt->pTranslateCxt, &pCxt->hasSelectValFunc, pNode);
    }
  }
  if (isAggFunc(*pNode) && isDistinctOrderBy(pCxt->pTranslateCxt)) {
    return generateDealNodeErrMsg(pCxt->pTranslateCxt, getGroupByErrorCode(pCxt->pTranslateCxt));
967 968 969 970
  }
  return DEAL_RES_CONTINUE;
}

971 972 973 974 975 976
static int32_t checkExprForGroupBy(STranslateContext* pCxt, SNode** pNode) {
  SCheckExprForGroupByCxt cxt = {.pTranslateCxt = pCxt, .selectFuncNum = 0, .hasSelectValFunc = false};
  nodesRewriteExpr(pNode, doCheckExprForGroupBy, &cxt);
  if (cxt.selectFuncNum != 1 && cxt.hasSelectValFunc) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, getGroupByErrorCode(pCxt));
  }
977 978 979 980
  return pCxt->errCode;
}

static int32_t checkExprListForGroupBy(STranslateContext* pCxt, SNodeList* pList) {
981 982 983
  if (NULL == getGroupByList(pCxt)) {
    return TSDB_CODE_SUCCESS;
  }
984 985 986 987 988 989 990 991 992 993 994 995
  SCheckExprForGroupByCxt cxt = {.pTranslateCxt = pCxt, .selectFuncNum = 0, .hasSelectValFunc = false};
  nodesRewriteExprs(pList, doCheckExprForGroupBy, &cxt);
  if (cxt.selectFuncNum != 1 && cxt.hasSelectValFunc) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, getGroupByErrorCode(pCxt));
  }
  return pCxt->errCode;
}

static EDealRes rewriteColsToSelectValFuncImpl(SNode** pNode, void* pContext) {
  if (isAggFunc(*pNode)) {
    return DEAL_RES_IGNORE_CHILD;
  }
996
  if (isScanPseudoColumnFunc(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode)) {
997 998 999 1000 1001 1002 1003 1004 1005 1006
    return rewriteColToSelectValFunc((STranslateContext*)pContext, NULL, pNode);
  }
  return DEAL_RES_CONTINUE;
}

static int32_t rewriteColsToSelectValFunc(STranslateContext* pCxt, SSelectStmt* pSelect) {
  nodesRewriteExprs(pSelect->pProjectionList, rewriteColsToSelectValFuncImpl, pCxt);
  if (TSDB_CODE_SUCCESS == pCxt->errCode && !pSelect->isDistinct) {
    nodesRewriteExprs(pSelect->pOrderByList, rewriteColsToSelectValFuncImpl, pCxt);
  }
1007 1008 1009
  return pCxt->errCode;
}

1010 1011
typedef struct CheckAggColCoexistCxt {
  STranslateContext* pTranslateCxt;
dengyihao's avatar
dengyihao 已提交
1012 1013
  bool               existAggFunc;
  bool               existCol;
1014
  bool               existNonstdFunc;
1015
  int32_t            selectFuncNum;
1016 1017 1018 1019
} CheckAggColCoexistCxt;

static EDealRes doCheckAggColCoexist(SNode* pNode, void* pContext) {
  CheckAggColCoexistCxt* pCxt = (CheckAggColCoexistCxt*)pContext;
1020 1021
  pCxt->selectFuncNum += isSelectFunc(pNode) ? 1 : 0;
  if (isAggFunc(pNode)) {
1022 1023 1024
    pCxt->existAggFunc = true;
    return DEAL_RES_IGNORE_CHILD;
  }
1025 1026 1027 1028
  if (isNonstandardSQLFunc(pNode)) {
    pCxt->existNonstdFunc = true;
    return DEAL_RES_IGNORE_CHILD;
  }
1029
  if (isScanPseudoColumnFunc(pNode) || QUERY_NODE_COLUMN == nodeType(pNode)) {
1030 1031 1032 1033 1034 1035 1036 1037 1038
    pCxt->existCol = true;
  }
  return DEAL_RES_CONTINUE;
}

static int32_t checkAggColCoexist(STranslateContext* pCxt, SSelectStmt* pSelect) {
  if (NULL != pSelect->pGroupByList) {
    return TSDB_CODE_SUCCESS;
  }
1039 1040
  CheckAggColCoexistCxt cxt = {
      .pTranslateCxt = pCxt, .existAggFunc = false, .existCol = false, .existNonstdFunc = false};
X
Xiaoyu Wang 已提交
1041
  nodesWalkExprs(pSelect->pProjectionList, doCheckAggColCoexist, &cxt);
1042
  if (!pSelect->isDistinct) {
X
Xiaoyu Wang 已提交
1043
    nodesWalkExprs(pSelect->pOrderByList, doCheckAggColCoexist, &cxt);
1044
  }
1045 1046
  if (1 == cxt.selectFuncNum) {
    return rewriteColsToSelectValFunc(pCxt, pSelect);
1047 1048
  }
  if ((cxt.selectFuncNum > 1 || cxt.existAggFunc || NULL != pSelect->pWindow) && cxt.existCol) {
1049
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NOT_SINGLE_GROUP);
1050
  }
1051 1052 1053
  if (cxt.existNonstdFunc && cxt.existCol) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_NOT_ALLOWED_FUNC);
  }
1054 1055 1056
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1057 1058
static int32_t toVgroupsInfo(SArray* pVgs, SVgroupsInfo** pVgsInfo) {
  size_t vgroupNum = taosArrayGetSize(pVgs);
wafwerar's avatar
wafwerar 已提交
1059
  *pVgsInfo = taosMemoryCalloc(1, sizeof(SVgroupsInfo) + sizeof(SVgroupInfo) * vgroupNum);
X
Xiaoyu Wang 已提交
1060 1061 1062 1063 1064
  if (NULL == *pVgsInfo) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  (*pVgsInfo)->numOfVgroups = vgroupNum;
  for (int32_t i = 0; i < vgroupNum; ++i) {
dengyihao's avatar
dengyihao 已提交
1065
    SVgroupInfo* vg = taosArrayGet(pVgs, i);
X
Xiaoyu Wang 已提交
1066 1067 1068 1069 1070
    (*pVgsInfo)->vgroups[i] = *vg;
  }
  return TSDB_CODE_SUCCESS;
}

1071 1072 1073 1074 1075 1076 1077
static int32_t addMnodeToVgroupList(const SEpSet* pEpSet, SArray** pVgroupList) {
  if (NULL == *pVgroupList) {
    *pVgroupList = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVgroupInfo));
    if (NULL == *pVgroupList) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
X
Xiaoyu Wang 已提交
1078
  SVgroupInfo vg = {.vgId = MNODE_HANDLE};
1079 1080 1081 1082 1083
  memcpy(&vg.epSet, pEpSet, sizeof(SEpSet));
  taosArrayPush(*pVgroupList, &vg);
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1084
static int32_t setSysTableVgroupList(STranslateContext* pCxt, SName* pName, SRealTableNode* pRealTable) {
D
dapan1121 已提交
1085 1086 1087
  if (0 != strcmp(pRealTable->table.tableName, TSDB_INS_TABLE_USER_TABLES)) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
1088 1089 1090

  int32_t code = TSDB_CODE_SUCCESS;
  SArray* vgroupList = NULL;
1091 1092
  if ('\0' != pRealTable->qualDbName[0]) {
    // todo release after mnode can be processed
1093 1094 1095
    if (0 != strcmp(pRealTable->qualDbName, TSDB_INFORMATION_SCHEMA_DB)) {
      code = getDBVgInfo(pCxt, pRealTable->qualDbName, &vgroupList);
    }
X
Xiaoyu Wang 已提交
1096 1097 1098 1099
  } else {
    code = getDBVgInfoImpl(pCxt, pName, &vgroupList);
  }

1100
  // todo release after mnode can be processed
1101 1102 1103
  if (TSDB_CODE_SUCCESS == code) {
    code = addMnodeToVgroupList(&pCxt->pParseCxt->mgmtEpSet, &vgroupList);
  }
1104

X
Xiaoyu Wang 已提交
1105 1106 1107 1108 1109 1110 1111 1112
  if (TSDB_CODE_SUCCESS == code) {
    code = toVgroupsInfo(vgroupList, &pRealTable->pVgroupList);
  }
  taosArrayDestroy(vgroupList);

  return code;
}

X
Xiaoyu Wang 已提交
1113 1114
static int32_t setTableVgroupList(STranslateContext* pCxt, SName* pName, SRealTableNode* pRealTable) {
  if (pCxt->pParseCxt->topicQuery) {
X
Xiaoyu Wang 已提交
1115 1116 1117
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
1118
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1119 1120
  if (TSDB_SUPER_TABLE == pRealTable->pMeta->tableType) {
    SArray* vgroupList = NULL;
X
Xiaoyu Wang 已提交
1121
    code = getTableDistVgInfo(pCxt, pName, &vgroupList);
X
Xiaoyu Wang 已提交
1122 1123
    if (TSDB_CODE_SUCCESS == code) {
      code = toVgroupsInfo(vgroupList, &pRealTable->pVgroupList);
X
Xiaoyu Wang 已提交
1124 1125
    }
    taosArrayDestroy(vgroupList);
X
Xiaoyu Wang 已提交
1126
  } else if (TSDB_SYSTEM_TABLE == pRealTable->pMeta->tableType) {
X
Xiaoyu Wang 已提交
1127
    code = setSysTableVgroupList(pCxt, pName, pRealTable);
X
Xiaoyu Wang 已提交
1128
  } else {
wafwerar's avatar
wafwerar 已提交
1129
    pRealTable->pVgroupList = taosMemoryCalloc(1, sizeof(SVgroupsInfo) + sizeof(SVgroupInfo));
X
Xiaoyu Wang 已提交
1130 1131 1132 1133
    if (NULL == pRealTable->pVgroupList) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    pRealTable->pVgroupList->numOfVgroups = 1;
X
Xiaoyu Wang 已提交
1134
    code = getTableHashVgroupImpl(pCxt, pName, pRealTable->pVgroupList->vgroups);
1135
  }
X
Xiaoyu Wang 已提交
1136
  return code;
1137 1138
}

X
Xiaoyu Wang 已提交
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
static uint8_t getStmtPrecision(SNode* pStmt) {
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt)) {
    return ((SSelectStmt*)pStmt)->precision;
  }
  return 0;
}

static uint8_t getJoinTablePrecision(SJoinTableNode* pJoinTable) {
  uint8_t lp = ((STableNode*)pJoinTable->pLeft)->precision;
  uint8_t rp = ((STableNode*)pJoinTable->pRight)->precision;
  return (lp > rp ? rp : lp);
}

1152 1153 1154 1155 1156
static int32_t translateTable(STranslateContext* pCxt, SNode* pTable) {
  int32_t code = TSDB_CODE_SUCCESS;
  switch (nodeType(pTable)) {
    case QUERY_NODE_REAL_TABLE: {
      SRealTableNode* pRealTable = (SRealTableNode*)pTable;
1157
      pRealTable->ratio = (NULL != pCxt->pExplainOpt ? pCxt->pExplainOpt->ratio : 1.0);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
      // The SRealTableNode created through ROLLUP already has STableMeta.
      if (NULL == pRealTable->pMeta) {
        SName name;
        code = getTableMetaImpl(
            pCxt, toName(pCxt->pParseCxt->acctId, pRealTable->table.dbName, pRealTable->table.tableName, &name),
            &(pRealTable->pMeta));
        if (TSDB_CODE_SUCCESS != code) {
          return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_TABLE_NOT_EXIST, pRealTable->table.tableName);
        }
        code = setTableVgroupList(pCxt, &name, pRealTable);
1168
      }
X
Xiaoyu Wang 已提交
1169
      pRealTable->table.precision = pRealTable->pMeta->tableInfo.precision;
1170 1171
      if (TSDB_CODE_SUCCESS == code) {
        code = addNamespace(pCxt, pRealTable);
1172
      }
1173 1174 1175 1176 1177 1178
      break;
    }
    case QUERY_NODE_TEMP_TABLE: {
      STempTableNode* pTempTable = (STempTableNode*)pTable;
      code = translateSubquery(pCxt, pTempTable->pSubquery);
      if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1179
        pTempTable->table.precision = getStmtPrecision(pTempTable->pSubquery);
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
        code = addNamespace(pCxt, pTempTable);
      }
      break;
    }
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoinTable = (SJoinTableNode*)pTable;
      code = translateTable(pCxt, pJoinTable->pLeft);
      if (TSDB_CODE_SUCCESS == code) {
        code = translateTable(pCxt, pJoinTable->pRight);
      }
      if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1191
        pJoinTable->table.precision = getJoinTablePrecision(pJoinTable);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
        code = translateExpr(pCxt, pJoinTable->pOnCond);
      }
      break;
    }
    default:
      break;
  }
  return code;
}

X
Xiaoyu Wang 已提交
1202 1203 1204 1205 1206 1207
static int32_t createAllColumns(STranslateContext* pCxt, SNodeList** pCols) {
  *pCols = nodesMakeList();
  if (NULL == *pCols) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY);
  }
  SArray* pTables = taosArrayGetP(pCxt->pNsLevel, pCxt->currLevel);
dengyihao's avatar
dengyihao 已提交
1208
  size_t  nums = taosArrayGetSize(pTables);
X
Xiaoyu Wang 已提交
1209 1210
  for (size_t i = 0; i < nums; ++i) {
    STableNode* pTable = taosArrayGetP(pTables, i);
X
Xiaoyu Wang 已提交
1211
    int32_t     code = createColumnsByTable(pCxt, pTable, *pCols);
X
Xiaoyu Wang 已提交
1212 1213
    if (TSDB_CODE_SUCCESS != code) {
      return code;
1214
    }
X
Xiaoyu Wang 已提交
1215 1216 1217 1218
  }
  return TSDB_CODE_SUCCESS;
}

1219 1220
static bool isMultiResFunc(SNode* pNode) {
  if (QUERY_NODE_FUNCTION != nodeType(pNode) || !fmIsMultiResFunc(((SFunctionNode*)pNode)->funcId)) {
X
Xiaoyu Wang 已提交
1221 1222 1223
    return false;
  }
  SNodeList* pParameterList = ((SFunctionNode*)pNode)->pParameterList;
1224 1225
  if (LIST_LENGTH(pParameterList) > 1) {
    return true;
X
Xiaoyu Wang 已提交
1226 1227 1228 1229 1230
  }
  SNode* pParam = nodesListGetNode(pParameterList, 0);
  return (QUERY_NODE_COLUMN == nodeType(pParam) ? 0 == strcmp(((SColumnNode*)pParam)->colName, "*") : false);
}

1231
static SNode* createMultiResFunc(SFunctionNode* pSrcFunc, SExprNode* pExpr) {
X
Xiaoyu Wang 已提交
1232 1233 1234 1235 1236
  SFunctionNode* pFunc = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == pFunc) {
    return NULL;
  }
  pFunc->pParameterList = nodesMakeList();
H
Hongze Cheng 已提交
1237 1238
  if (NULL == pFunc->pParameterList ||
      TSDB_CODE_SUCCESS != nodesListStrictAppend(pFunc->pParameterList, nodesCloneNode(pExpr))) {
X
Xiaoyu Wang 已提交
1239 1240 1241 1242
    nodesDestroyNode(pFunc);
    return NULL;
  }

1243
  pFunc->node.resType = pExpr->resType;
X
Xiaoyu Wang 已提交
1244 1245 1246
  pFunc->funcId = pSrcFunc->funcId;
  pFunc->funcType = pSrcFunc->funcType;
  strcpy(pFunc->functionName, pSrcFunc->functionName);
H
Hongze Cheng 已提交
1247
  char    buf[TSDB_FUNC_NAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_COL_NAME_LEN];
X
Xiaoyu Wang 已提交
1248
  int32_t len = 0;
1249 1250
  if (QUERY_NODE_COLUMN == nodeType(pExpr)) {
    SColumnNode* pCol = (SColumnNode*)pExpr;
X
Xiaoyu Wang 已提交
1251
    len = snprintf(buf, sizeof(buf), "%s(%s.%s)", pSrcFunc->functionName, pCol->tableAlias, pCol->colName);
1252
  } else {
X
Xiaoyu Wang 已提交
1253
    len = snprintf(buf, sizeof(buf), "%s(%s)", pSrcFunc->functionName, pExpr->aliasName);
X
Xiaoyu Wang 已提交
1254
  }
X
Xiaoyu Wang 已提交
1255
  strncpy(pFunc->node.aliasName, buf, TMIN(len, sizeof(pFunc->node.aliasName) - 1));
X
Xiaoyu Wang 已提交
1256

1257
  return (SNode*)pFunc;
1258 1259
}

1260 1261
static int32_t createTableAllCols(STranslateContext* pCxt, SColumnNode* pCol, SNodeList** pOutput) {
  STableNode* pTable = NULL;
H
Hongze Cheng 已提交
1262
  int32_t     code = findTable(pCxt, pCol->tableAlias, &pTable);
1263 1264 1265 1266 1267
  if (TSDB_CODE_SUCCESS == code && NULL == *pOutput) {
    *pOutput = nodesMakeList();
    if (NULL == *pOutput) {
      code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY);
    }
1268
  }
1269
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1270
    code = createColumnsByTable(pCxt, pTable, *pOutput);
1271 1272
  }
  return code;
1273 1274
}

1275
static bool isStar(SNode* pNode) {
H
Hongze Cheng 已提交
1276 1277
  return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' == ((SColumnNode*)pNode)->tableAlias[0]) &&
         (0 == strcmp(((SColumnNode*)pNode)->colName, "*"));
1278 1279 1280
}

static bool isTableStar(SNode* pNode) {
H
Hongze Cheng 已提交
1281 1282
  return (QUERY_NODE_COLUMN == nodeType(pNode)) && ('\0' != ((SColumnNode*)pNode)->tableAlias[0]) &&
         (0 == strcmp(((SColumnNode*)pNode)->colName, "*"));
1283 1284 1285 1286 1287 1288
}

static int32_t createMultiResFuncsParas(STranslateContext* pCxt, SNodeList* pSrcParas, SNodeList** pOutput) {
  int32_t code = TSDB_CODE_SUCCESS;

  SNodeList* pExprs = NULL;
H
Hongze Cheng 已提交
1289
  SNode*     pPara = NULL;
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
  FOREACH(pPara, pSrcParas) {
    if (isStar(pPara)) {
      code = createAllColumns(pCxt, &pExprs);
      // The syntax definition ensures that * and other parameters do not appear at the same time
      break;
    } else if (isTableStar(pPara)) {
      code = createTableAllCols(pCxt, (SColumnNode*)pPara, &pExprs);
    } else {
      code = nodesListMakeStrictAppend(&pExprs, nodesCloneNode(pPara));
    }
    if (TSDB_CODE_SUCCESS != code) {
      break;
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pExprs;
  } else {
    nodesDestroyList(pExprs);
  }

  return code;
}

static int32_t createMultiResFuncs(SFunctionNode* pSrcFunc, SNodeList* pExprs, SNodeList** pOutput) {
  SNodeList* pFuncs = nodesMakeList();
  if (NULL == pFuncs) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = TSDB_CODE_SUCCESS;
H
Hongze Cheng 已提交
1321
  SNode*  pExpr = NULL;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
  FOREACH(pExpr, pExprs) {
    code = nodesListStrictAppend(pFuncs, createMultiResFunc(pSrcFunc, (SExprNode*)pExpr));
    if (TSDB_CODE_SUCCESS != code) {
      break;
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pFuncs;
  } else {
    nodesDestroyList(pFuncs);
  }

  return code;
}

static int32_t createMultiResFuncsFromStar(STranslateContext* pCxt, SFunctionNode* pSrcFunc, SNodeList** pOutput) {
  SNodeList* pExprs = NULL;
H
Hongze Cheng 已提交
1340
  int32_t    code = createMultiResFuncsParas(pCxt, pSrcFunc->pParameterList, &pExprs);
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
  if (TSDB_CODE_SUCCESS == code) {
    code = createMultiResFuncs(pSrcFunc, pExprs, pOutput);
  }

  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyList(pExprs);
  }

  return code;
}

X
Xiaoyu Wang 已提交
1352
static int32_t translateStar(STranslateContext* pCxt, SSelectStmt* pSelect) {
dengyihao's avatar
dengyihao 已提交
1353
  if (NULL == pSelect->pProjectionList) {  // select * ...
X
Xiaoyu Wang 已提交
1354
    return createAllColumns(pCxt, &pSelect->pProjectionList);
1355
  } else {
X
Xiaoyu Wang 已提交
1356 1357
    SNode* pNode = NULL;
    WHERE_EACH(pNode, pSelect->pProjectionList) {
X
Xiaoyu Wang 已提交
1358
      int32_t code = TSDB_CODE_SUCCESS;
1359
      if (isMultiResFunc(pNode)) {
X
Xiaoyu Wang 已提交
1360
        SNodeList* pFuncs = NULL;
X
Xiaoyu Wang 已提交
1361 1362 1363 1364 1365
        code = createMultiResFuncsFromStar(pCxt, (SFunctionNode*)pNode, &pFuncs);
        if (TSDB_CODE_SUCCESS == code) {
          INSERT_LIST(pSelect->pProjectionList, pFuncs);
          ERASE_NODE(pSelect->pProjectionList);
          continue;
X
Xiaoyu Wang 已提交
1366
        }
1367 1368
      } else if (isTableStar(pNode)) {
        SNodeList* pCols = NULL;
X
Xiaoyu Wang 已提交
1369 1370 1371 1372 1373
        code = createTableAllCols(pCxt, (SColumnNode*)pNode, &pCols);
        if (TSDB_CODE_SUCCESS == code) {
          INSERT_LIST(pSelect->pProjectionList, pCols);
          ERASE_NODE(pSelect->pProjectionList);
          continue;
1374
        }
X
Xiaoyu Wang 已提交
1375
      }
X
Xiaoyu Wang 已提交
1376 1377 1378
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
X
Xiaoyu Wang 已提交
1379 1380
      WHERE_NEXT;
    }
1381
  }
1382
  return TSDB_CODE_SUCCESS;
1383 1384
}

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
static int32_t getPositionValue(const SValueNode* pVal) {
  switch (pVal->node.resType.type) {
    case TSDB_DATA_TYPE_NULL:
    case TSDB_DATA_TYPE_TIMESTAMP:
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
    case TSDB_DATA_TYPE_JSON:
      return -1;
    case TSDB_DATA_TYPE_BOOL:
      return (pVal->datum.b ? 1 : 0);
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
      return pVal->datum.i;
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
      return pVal->datum.d;
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
dengyihao's avatar
dengyihao 已提交
1408
      return pVal->datum.u;
1409 1410 1411 1412 1413 1414
    default:
      break;
  }
  return -1;
}

dengyihao's avatar
dengyihao 已提交
1415 1416
static int32_t translateOrderByPosition(STranslateContext* pCxt, SNodeList* pProjectionList, SNodeList* pOrderByList,
                                        bool* pOther) {
1417
  *pOther = false;
X
Xiaoyu Wang 已提交
1418 1419
  SNode* pNode = NULL;
  WHERE_EACH(pNode, pOrderByList) {
1420 1421 1422
    SNode* pExpr = ((SOrderByExprNode*)pNode)->pExpr;
    if (QUERY_NODE_VALUE == nodeType(pExpr)) {
      SValueNode* pVal = (SValueNode*)pExpr;
1423
      if (DEAL_RES_ERROR == translateValue(pCxt, pVal)) {
X
Xiaoyu Wang 已提交
1424
        return pCxt->errCode;
1425
      }
X
Xiaoyu Wang 已提交
1426
      int32_t pos = getPositionValue(pVal);
1427 1428 1429 1430
      if (pos < 0) {
        ERASE_NODE(pOrderByList);
        continue;
      } else if (0 == pos || pos > LIST_LENGTH(pProjectionList)) {
1431
        return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_WRONG_NUMBER_OF_SELECT);
1432 1433
      } else {
        SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
1434
        if (NULL == pCol) {
1435
          return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_OUT_OF_MEMORY);
X
Xiaoyu Wang 已提交
1436
        }
1437 1438 1439
        setColumnInfoByExpr(NULL, (SExprNode*)nodesListGetNode(pProjectionList, pos - 1), pCol);
        ((SOrderByExprNode*)pNode)->pExpr = (SNode*)pCol;
        nodesDestroyNode(pExpr);
1440 1441 1442 1443
      }
    } else {
      *pOther = true;
    }
X
Xiaoyu Wang 已提交
1444
    WHERE_NEXT;
1445
  }
X
Xiaoyu Wang 已提交
1446
  return TSDB_CODE_SUCCESS;
1447 1448
}

1449
static int32_t translateOrderBy(STranslateContext* pCxt, SSelectStmt* pSelect) {
dengyihao's avatar
dengyihao 已提交
1450
  bool    other;
X
Xiaoyu Wang 已提交
1451 1452 1453
  int32_t code = translateOrderByPosition(pCxt, pSelect->pProjectionList, pSelect->pOrderByList, &other);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
1454 1455 1456 1457 1458
  }
  if (!other) {
    return TSDB_CODE_SUCCESS;
  }
  pCxt->currClause = SQL_CLAUSE_ORDER_BY;
X
Xiaoyu Wang 已提交
1459
  code = translateExprList(pCxt, pSelect->pOrderByList);
1460
  if (TSDB_CODE_SUCCESS == code) {
1461 1462 1463
    code = checkExprListForGroupBy(pCxt, pSelect->pOrderByList);
  }
  return code;
1464 1465 1466
}

static int32_t translateSelectList(STranslateContext* pCxt, SSelectStmt* pSelect) {
X
Xiaoyu Wang 已提交
1467 1468 1469 1470
  pCxt->currClause = SQL_CLAUSE_SELECT;
  int32_t code = translateExprList(pCxt, pSelect->pProjectionList);
  if (TSDB_CODE_SUCCESS == code) {
    code = translateStar(pCxt, pSelect);
1471
  }
1472
  if (TSDB_CODE_SUCCESS == code) {
1473 1474
    code = checkExprListForGroupBy(pCxt, pSelect->pProjectionList);
  }
1475 1476 1477
  return code;
}

1478 1479
static int32_t translateHaving(STranslateContext* pCxt, SSelectStmt* pSelect) {
  if (NULL == pSelect->pGroupByList && NULL != pSelect->pHaving) {
1480
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION);
1481
  }
1482
  pCxt->currClause = SQL_CLAUSE_HAVING;
1483 1484
  int32_t code = translateExpr(pCxt, pSelect->pHaving);
  if (TSDB_CODE_SUCCESS == code) {
1485
    code = checkExprForGroupBy(pCxt, &pSelect->pHaving);
1486 1487
  }
  return code;
1488 1489
}

X
Xiaoyu Wang 已提交
1490 1491 1492 1493
static int32_t translateGroupBy(STranslateContext* pCxt, SSelectStmt* pSelect) {
  if (NULL != pSelect->pGroupByList && NULL != pSelect->pWindow) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_GROUPBY_WINDOW_COEXIST);
  }
X
Xiaoyu Wang 已提交
1494 1495 1496 1497 1498 1499
  if (NULL != pSelect->pGroupByList) {
    pCxt->currClause = SQL_CLAUSE_GROUP_BY;
    pSelect->isTimeOrderQuery = false;
    return translateExprList(pCxt, pSelect->pGroupByList);
  }
  return TSDB_CODE_SUCCESS;
1500 1501
}

X
Xiaoyu Wang 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
static EDealRes isPrimaryKeyCondImpl(SNode* pNode, void* pContext) {
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    *((bool*)pContext) = ((PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pNode)->colId) ? true : false);
    return *((bool*)pContext) ? DEAL_RES_CONTINUE : DEAL_RES_END;
  }
  return DEAL_RES_CONTINUE;
}

static bool isPrimaryKeyCond(SNode* pNode) {
  bool isPrimaryKeyCond = false;
  nodesWalkExpr(pNode, isPrimaryKeyCondImpl, &isPrimaryKeyCond);
  return isPrimaryKeyCond;
}

static int32_t getTimeRangeFromLogicCond(STranslateContext* pCxt, SLogicConditionNode* pLogicCond,
                                         STimeWindow* pTimeRange) {
  SNodeList* pPrimaryKeyConds = NULL;
  SNode*     pCond = NULL;
  FOREACH(pCond, pLogicCond->pParameterList) {
    if (isPrimaryKeyCond(pCond)) {
      if (TSDB_CODE_SUCCESS != nodesListMakeAppend(&pPrimaryKeyConds, pCond)) {
        nodesClearList(pPrimaryKeyConds);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
    }
  }

  if (NULL == pPrimaryKeyConds) {
    *pTimeRange = TSWINDOW_INITIALIZER;
    return TSDB_CODE_SUCCESS;
  }

  SLogicConditionNode* pPrimaryKeyLogicCond = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  if (NULL == pPrimaryKeyLogicCond) {
    nodesClearList(pPrimaryKeyConds);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pPrimaryKeyLogicCond->condType = LOGIC_COND_TYPE_AND;
  pPrimaryKeyLogicCond->pParameterList = pPrimaryKeyConds;
  bool    isStrict = false;
  int32_t code = filterGetTimeRange((SNode*)pPrimaryKeyLogicCond, pTimeRange, &isStrict);
  nodesClearList(pPrimaryKeyConds);
  pPrimaryKeyLogicCond->pParameterList = NULL;
  nodesDestroyNode(pPrimaryKeyLogicCond);
  return code;
}

static int32_t getTimeRange(STranslateContext* pCxt, SNode* pWhere, STimeWindow* pTimeRange) {
  if (NULL == pWhere) {
    *pTimeRange = TSWINDOW_INITIALIZER;
    return TSDB_CODE_SUCCESS;
  }

  if (QUERY_NODE_LOGIC_CONDITION == nodeType(pWhere) &&
      LOGIC_COND_TYPE_AND == ((SLogicConditionNode*)pWhere)->condType) {
    return getTimeRangeFromLogicCond(pCxt, (SLogicConditionNode*)pWhere, pTimeRange);
  }

  if (isPrimaryKeyCond(pWhere)) {
    bool isStrict = false;
    return filterGetTimeRange(pWhere, pTimeRange, &isStrict);
  } else {
    *pTimeRange = TSWINDOW_INITIALIZER;
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t checkFill(STranslateContext* pCxt, SIntervalWindowNode* pInterval) {
  SFillNode* pFill = (SFillNode*)pInterval->pFill;
  if (TSWINDOW_IS_EQUAL(pFill->timeRange, TSWINDOW_INITIALIZER) ||
      TSWINDOW_IS_EQUAL(pFill->timeRange, TSWINDOW_DESC_INITIALIZER)) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE);
  }

  int64_t     timeRange = TABS(pFill->timeRange.skey - pFill->timeRange.ekey);
  int64_t     intervalRange = 0;
  SValueNode* pInter = (SValueNode*)pInterval->pInterval;
  if (TIME_IS_VAR_DURATION(pInter->unit)) {
    int64_t f = 1;
    if (pInter->unit == 'n') {
      f = 30L * MILLISECOND_PER_DAY;
    } else if (pInter->unit == 'y') {
      f = 365L * MILLISECOND_PER_DAY;
    }
    intervalRange = pInter->datum.i * f;
  } else {
    intervalRange = pInter->datum.i;
  }
  if ((timeRange == 0) || (timeRange / intervalRange) >= MAX_INTERVAL_TIME_WINDOW) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_FILL_TIME_RANGE);
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t translateFill(STranslateContext* pCxt, SNode* pWhere, SIntervalWindowNode* pInterval) {
  if (NULL == pInterval->pFill) {
    return TSDB_CODE_SUCCESS;
  }

  int32_t code = getTimeRange(pCxt, pWhere, &(((SFillNode*)pInterval->pFill)->timeRange));
  if (TSDB_CODE_SUCCESS == code) {
    code = checkFill(pCxt, pInterval);
  }
  return code;
}

X
Xiaoyu Wang 已提交
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
static int64_t getMonthsFromTimeVal(int64_t val, int32_t fromPrecision, char unit) {
  int64_t days = convertTimeFromPrecisionToUnit(val, fromPrecision, 'd');
  switch (unit) {
    case 'b':
    case 'u':
    case 'a':
    case 's':
    case 'm':
    case 'h':
    case 'd':
    case 'w':
      return days / 28;
    case 'n':
      return val;
    case 'y':
      return val * 12;
    default:
      break;
  }
  return -1;
}

X
Xiaoyu Wang 已提交
1631
static int32_t checkIntervalWindow(STranslateContext* pCxt, SNode* pWhere, SIntervalWindowNode* pInterval) {
X
Xiaoyu Wang 已提交
1632 1633 1634
  uint8_t precision = ((SColumnNode*)pInterval->pCol)->node.resType.precision;

  SValueNode* pInter = (SValueNode*)pInterval->pInterval;
X
Xiaoyu Wang 已提交
1635
  bool        valInter = TIME_IS_VAR_DURATION(pInter->unit);
H
Hongze Cheng 已提交
1636
  if (pInter->datum.i <= 0 ||
X
Xiaoyu Wang 已提交
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
      (!valInter && convertTimePrecision(pInter->datum.i, precision, TSDB_TIME_PRECISION_MICRO) < tsMinIntervalTime)) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_VALUE_TOO_SMALL, tsMinIntervalTime);
  }

  if (NULL != pInterval->pOffset) {
    SValueNode* pOffset = (SValueNode*)pInterval->pOffset;
    if (pOffset->datum.i <= 0) {
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_OFFSET_NEGATIVE);
    }
    if (pInter->unit == 'n' && pOffset->unit == 'y') {
H
Hongze Cheng 已提交
1647
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_OFFSET_UNIT);
X
Xiaoyu Wang 已提交
1648
    }
X
Xiaoyu Wang 已提交
1649
    bool fixed = !TIME_IS_VAR_DURATION(pOffset->unit) && !valInter;
X
Xiaoyu Wang 已提交
1650
    if ((fixed && pOffset->datum.i >= pInter->datum.i) ||
H
Hongze Cheng 已提交
1651 1652
        (!fixed && getMonthsFromTimeVal(pOffset->datum.i, precision, pOffset->unit) >=
                       getMonthsFromTimeVal(pInter->datum.i, precision, pInter->unit))) {
X
Xiaoyu Wang 已提交
1653 1654 1655 1656 1657 1658
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_OFFSET_TOO_BIG);
    }
  }

  if (NULL != pInterval->pSliding) {
    const static int32_t INTERVAL_SLIDING_FACTOR = 100;
H
Hongze Cheng 已提交
1659

X
Xiaoyu Wang 已提交
1660
    SValueNode* pSliding = (SValueNode*)pInterval->pSliding;
X
Xiaoyu Wang 已提交
1661
    if (TIME_IS_VAR_DURATION(pSliding->unit)) {
X
Xiaoyu Wang 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_SLIDING_UNIT);
    }
    if ((pSliding->datum.i < convertTimePrecision(tsMinSlidingTime, TSDB_TIME_PRECISION_MILLI, precision)) ||
        (pInter->datum.i / pSliding->datum.i > INTERVAL_SLIDING_FACTOR)) {
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_SLIDING_TOO_SMALL);
    }
    if (pSliding->datum.i > pInter->datum.i) {
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_SLIDING_TOO_BIG);
    }
  }

X
Xiaoyu Wang 已提交
1673
  return translateFill(pCxt, pWhere, pInterval);
X
Xiaoyu Wang 已提交
1674 1675
}

X
Xiaoyu Wang 已提交
1676 1677 1678
static EDealRes checkStateExpr(SNode* pNode, void* pContext) {
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    STranslateContext* pCxt = pContext;
L
Liu Jicong 已提交
1679
    SColumnNode*       pCol = (SColumnNode*)pNode;
1680 1681 1682

    int32_t type = pCol->node.resType.type;
    if (!IS_INTEGER_TYPE(type) && type != TSDB_DATA_TYPE_BOOL && !IS_VAR_DATA_TYPE(type)) {
X
Xiaoyu Wang 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_STATE_WIN_TYPE);
    }
    if (COLUMN_TYPE_TAG == pCol->colType) {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_STATE_WIN_COL);
    }
    if (TSDB_SUPER_TABLE == pCol->tableType) {
      return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_INVALID_STATE_WIN_TABLE);
    }
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1695
static int32_t checkStateWindow(STranslateContext* pCxt, SStateWindowNode* pState) {
X
Xiaoyu Wang 已提交
1696
  nodesWalkExprPostOrder(pState->pExpr, checkStateExpr, pCxt);
X
Xiaoyu Wang 已提交
1697
  // todo check for "function not support for state_window"
X
Xiaoyu Wang 已提交
1698
  return pCxt->errCode;
X
Xiaoyu Wang 已提交
1699 1700 1701 1702 1703 1704 1705 1706
}

static int32_t checkSessionWindow(STranslateContext* pCxt, SSessionWindowNode* pSession) {
  if ('y' == pSession->pGap->unit || 'n' == pSession->pGap->unit || 0 == pSession->pGap->datum.i) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_SESSION_GAP);
  }
  if (PRIMARYKEY_TIMESTAMP_COL_ID != pSession->pCol->colId) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INTER_SESSION_COL);
X
Xiaoyu Wang 已提交
1707
  }
X
Xiaoyu Wang 已提交
1708
  // todo check for "function not support for session"
X
Xiaoyu Wang 已提交
1709 1710 1711
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1712 1713
static int32_t checkWindow(STranslateContext* pCxt, SSelectStmt* pSelect) {
  switch (nodeType(pSelect->pWindow)) {
X
Xiaoyu Wang 已提交
1714
    case QUERY_NODE_STATE_WINDOW:
X
Xiaoyu Wang 已提交
1715
      return checkStateWindow(pCxt, (SStateWindowNode*)pSelect->pWindow);
X
Xiaoyu Wang 已提交
1716
    case QUERY_NODE_SESSION_WINDOW:
X
Xiaoyu Wang 已提交
1717
      return checkSessionWindow(pCxt, (SSessionWindowNode*)pSelect->pWindow);
X
Xiaoyu Wang 已提交
1718
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
1719
      return checkIntervalWindow(pCxt, pSelect->pWhere, (SIntervalWindowNode*)pSelect->pWindow);
X
Xiaoyu Wang 已提交
1720 1721 1722
    default:
      break;
  }
X
Xiaoyu Wang 已提交
1723 1724 1725
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1726 1727
static int32_t translateWindow(STranslateContext* pCxt, SSelectStmt* pSelect) {
  if (NULL == pSelect->pWindow) {
X
Xiaoyu Wang 已提交
1728 1729
    return TSDB_CODE_SUCCESS;
  }
1730
  pCxt->currClause = SQL_CLAUSE_WINDOW;
X
Xiaoyu Wang 已提交
1731
  int32_t code = translateExpr(pCxt, pSelect->pWindow);
X
Xiaoyu Wang 已提交
1732
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1733
    code = checkWindow(pCxt, pSelect);
X
Xiaoyu Wang 已提交
1734 1735
  }
  return code;
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
}

static int32_t translatePartitionBy(STranslateContext* pCxt, SNodeList* pPartitionByList) {
  pCxt->currClause = SQL_CLAUSE_PARTITION_BY;
  return translateExprList(pCxt, pPartitionByList);
}

static int32_t translateWhere(STranslateContext* pCxt, SNode* pWhere) {
  pCxt->currClause = SQL_CLAUSE_WHERE;
  return translateExpr(pCxt, pWhere);
}

X
Xiaoyu Wang 已提交
1748
static int32_t translateFrom(STranslateContext* pCxt, SSelectStmt* pSelect) {
1749
  pCxt->currClause = SQL_CLAUSE_FROM;
dengyihao's avatar
dengyihao 已提交
1750
  int32_t code = translateTable(pCxt, pSelect->pFromTable);
X
Xiaoyu Wang 已提交
1751 1752 1753 1754
  if (TSDB_CODE_SUCCESS == code) {
    pSelect->precision = ((STableNode*)pSelect->pFromTable)->precision;
  }
  return code;
1755 1756
}

X
Xiaoyu Wang 已提交
1757
static int32_t checkLimit(STranslateContext* pCxt, SSelectStmt* pSelect) {
L
Liu Jicong 已提交
1758 1759
  if ((NULL != pSelect->pLimit && pSelect->pLimit->offset < 0) ||
      (NULL != pSelect->pSlimit && pSelect->pSlimit->offset < 0)) {
X
Xiaoyu Wang 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_OFFSET_LESS_ZERO);
  }

  if (NULL != pSelect->pSlimit && NULL == pSelect->pPartitionByList) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_SLIMIT_LEAK_PARTITION_BY);
  }

  return TSDB_CODE_SUCCESS;
}

1770 1771 1772 1773 1774
static int32_t createPrimaryKeyColByTable(STranslateContext* pCxt, STableNode* pTable, SNode** pPrimaryKey) {
  SColumnNode* pCol = nodesMakeNode(QUERY_NODE_COLUMN);
  if (NULL == pCol) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1775 1776 1777 1778
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(pCol->colName, PK_TS_COL_INTERNAL_NAME);
  if (!findAndSetColumn(pCol, pTable)) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_TIMELINE_FUNC);
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
  }
  *pPrimaryKey = (SNode*)pCol;
  return TSDB_CODE_SUCCESS;
}

static int32_t createPrimaryKeyCol(STranslateContext* pCxt, SNode** pPrimaryKey) {
  STableNode* pTable = NULL;
  int32_t     code = findTable(pCxt, NULL, &pTable);
  if (TSDB_CODE_SUCCESS == code) {
    code = createPrimaryKeyColByTable(pCxt, pTable, pPrimaryKey);
  }
  return code;
}

static EDealRes rewriteTimelineFuncImpl(SNode* pNode, void* pContext) {
  STranslateContext* pCxt = pContext;
  if (isTimelineFunc(pNode)) {
    SFunctionNode* pFunc = (SFunctionNode*)pNode;
    SNode*         pPrimaryKey = NULL;
    pCxt->errCode = createPrimaryKeyCol(pCxt, &pPrimaryKey);
    if (TSDB_CODE_SUCCESS == pCxt->errCode) {
      pCxt->errCode = nodesListMakeStrictAppend(&pFunc->pParameterList, pPrimaryKey);
    }
    return TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR;
  }
  return DEAL_RES_CONTINUE;
}

static int32_t rewriteTimelineFunc(STranslateContext* pCxt, SSelectStmt* pSelect) {
  nodesWalkSelectStmt(pSelect, SQL_CLAUSE_FROM, rewriteTimelineFuncImpl, pCxt);
  return pCxt->errCode;
}

1812
static int32_t translateSelect(STranslateContext* pCxt, SSelectStmt* pSelect) {
1813
  pCxt->pCurrStmt = pSelect;
X
Xiaoyu Wang 已提交
1814
  int32_t code = translateFrom(pCxt, pSelect);
1815
  if (TSDB_CODE_SUCCESS == code) {
1816
    code = translateWhere(pCxt, pSelect->pWhere);
1817
  }
1818
  if (TSDB_CODE_SUCCESS == code) {
1819
    code = translatePartitionBy(pCxt, pSelect->pPartitionByList);
1820
  }
1821
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1822
    code = translateWindow(pCxt, pSelect);
1823
  }
1824
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1825
    code = translateGroupBy(pCxt, pSelect);
1826 1827
  }
  if (TSDB_CODE_SUCCESS == code) {
1828
    code = translateHaving(pCxt, pSelect);
1829 1830 1831 1832 1833
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = translateSelectList(pCxt, pSelect);
  }
  if (TSDB_CODE_SUCCESS == code) {
1834
    code = translateOrderBy(pCxt, pSelect);
1835
  }
1836 1837 1838
  if (TSDB_CODE_SUCCESS == code) {
    code = checkAggColCoexist(pCxt, pSelect);
  }
X
Xiaoyu Wang 已提交
1839 1840 1841
  if (TSDB_CODE_SUCCESS == code) {
    code = checkLimit(pCxt, pSelect);
  }
1842 1843 1844
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteTimelineFunc(pCxt, pSelect);
  }
1845 1846 1847
  return code;
}

1848
static SNode* createSetOperProject(const char* pTableAlias, SNode* pNode) {
X
Xiaoyu Wang 已提交
1849 1850 1851 1852 1853
  SColumnNode* pCol = nodesMakeNode(QUERY_NODE_COLUMN);
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType = ((SExprNode*)pNode)->resType;
1854
  strcpy(pCol->tableAlias, pTableAlias);
X
Xiaoyu Wang 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
  strcpy(pCol->colName, ((SExprNode*)pNode)->aliasName);
  strcpy(pCol->node.aliasName, pCol->colName);
  return (SNode*)pCol;
}

static bool dataTypeEqual(const SDataType* l, const SDataType* r) {
  return (l->type == r->type && l->bytes == l->bytes && l->precision == r->precision && l->scale == l->scale);
}

static int32_t createCastFunc(STranslateContext* pCxt, SNode* pExpr, SDataType dt, SNode** pCast) {
  SFunctionNode* pFunc = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == pFunc) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  strcpy(pFunc->functionName, "cast");
  pFunc->node.resType = dt;
  if (TSDB_CODE_SUCCESS != nodesListMakeAppend(&pFunc->pParameterList, pExpr)) {
    nodesDestroyNode(pFunc);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  if (DEAL_RES_ERROR == translateFunction(pCxt, pFunc)) {
X
Xiaoyu Wang 已提交
1876 1877
    nodesClearList(pFunc->pParameterList);
    pFunc->pParameterList = NULL;
X
Xiaoyu Wang 已提交
1878
    nodesDestroyNode(pFunc);
X
Xiaoyu Wang 已提交
1879
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_WRONG_VALUE_TYPE, ((SExprNode*)pExpr)->aliasName);
X
Xiaoyu Wang 已提交
1880 1881 1882 1883 1884
  }
  *pCast = (SNode*)pFunc;
  return TSDB_CODE_SUCCESS;
}

1885
static int32_t translateSetOperatorImpl(STranslateContext* pCxt, SSetOperator* pSetOperator) {
X
Xiaoyu Wang 已提交
1886 1887 1888
  SNodeList* pLeftProjections = getProjectList(pSetOperator->pLeft);
  SNodeList* pRightProjections = getProjectList(pSetOperator->pRight);
  if (LIST_LENGTH(pLeftProjections) != LIST_LENGTH(pRightProjections)) {
X
Xiaoyu Wang 已提交
1889
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INCORRECT_NUM_OF_COL);
X
Xiaoyu Wang 已提交
1890
  }
X
Xiaoyu Wang 已提交
1891

X
Xiaoyu Wang 已提交
1892 1893 1894 1895 1896 1897
  SNode* pLeft = NULL;
  SNode* pRight = NULL;
  FORBOTH(pLeft, pLeftProjections, pRight, pRightProjections) {
    SExprNode* pLeftExpr = (SExprNode*)pLeft;
    SExprNode* pRightExpr = (SExprNode*)pRight;
    if (!dataTypeEqual(&pLeftExpr->resType, &pRightExpr->resType)) {
L
Liu Jicong 已提交
1898
      SNode*  pRightFunc = NULL;
X
Xiaoyu Wang 已提交
1899 1900 1901 1902 1903 1904 1905 1906 1907
      int32_t code = createCastFunc(pCxt, pRight, pLeftExpr->resType, &pRightFunc);
      if (TSDB_CODE_SUCCESS != code) {
        return code;
      }
      REPLACE_LIST2_NODE(pRightFunc);
      pRightExpr = (SExprNode*)pRightFunc;
    }
    strcpy(pRightExpr->aliasName, pLeftExpr->aliasName);
    pRightExpr->aliasName[strlen(pLeftExpr->aliasName)] = '\0';
1908 1909
    if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pSetOperator->pProjectionList,
                                                       createSetOperProject(pSetOperator->stmtName, pLeft))) {
X
Xiaoyu Wang 已提交
1910 1911 1912
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
1913 1914 1915 1916 1917
  return TSDB_CODE_SUCCESS;
}

static int32_t translateSetOperator(STranslateContext* pCxt, SSetOperator* pSetOperator) {
  int32_t code = translateQuery(pCxt, pSetOperator->pLeft);
X
Xiaoyu Wang 已提交
1918 1919 1920
  if (TSDB_CODE_SUCCESS == code) {
    code = resetTranslateNamespace(pCxt);
  }
1921 1922 1923 1924 1925 1926 1927 1928 1929
  if (TSDB_CODE_SUCCESS == code) {
    code = translateQuery(pCxt, pSetOperator->pRight);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = translateSetOperatorImpl(pCxt, pSetOperator);
  }
  return code;
}

1930 1931
static int64_t getUnitPerMinute(uint8_t precision) {
  switch (precision) {
H
Hongze Cheng 已提交
1932 1933 1934 1935 1936 1937 1938 1939
    case TSDB_TIME_PRECISION_MILLI:
      return MILLISECOND_PER_MINUTE;
    case TSDB_TIME_PRECISION_MICRO:
      return MILLISECOND_PER_MINUTE * 1000L;
    case TSDB_TIME_PRECISION_NANO:
      return NANOSECOND_PER_MINUTE;
    default:
      break;
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
  }
  return MILLISECOND_PER_MINUTE;
}

static int64_t getBigintFromValueNode(SValueNode* pVal) {
  if (pVal->isDuration) {
    return pVal->datum.i / getUnitPerMinute(pVal->node.resType.precision);
  }
  return pVal->datum.i;
}

X
Xiaoyu Wang 已提交
1951 1952
static int32_t buildCreateDbRetentions(const SNodeList* pRetentions, SCreateDbReq* pReq) {
  if (NULL != pRetentions) {
X
Xiaoyu Wang 已提交
1953
    pReq->pRetensions = taosArrayInit(LIST_LENGTH(pRetentions), sizeof(SRetention));
X
Xiaoyu Wang 已提交
1954 1955 1956 1957 1958
    if (NULL == pReq->pRetensions) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    SValueNode* pFreq = NULL;
    SValueNode* pKeep = NULL;
dengyihao's avatar
dengyihao 已提交
1959 1960
    SNode*      pNode = NULL;
    int32_t     index = 0;
X
Xiaoyu Wang 已提交
1961
    FOREACH(pNode, pRetentions) {
X
Xiaoyu Wang 已提交
1962 1963 1964
      pFreq = (SValueNode*)nodesListGetNode(((SNodeListNode*)pNode)->pNodeList, 0);
      pKeep = (SValueNode*)nodesListGetNode(((SNodeListNode*)pNode)->pNodeList, 1);
      SRetention retention = {
dengyihao's avatar
dengyihao 已提交
1965
          .freq = pFreq->datum.i, .freqUnit = pFreq->unit, .keep = pKeep->datum.i, .keepUnit = pKeep->unit};
X
Xiaoyu Wang 已提交
1966
      taosArrayPush(pReq->pRetensions, &retention);
X
Xiaoyu Wang 已提交
1967
    }
1968
    pReq->numOfRetensions = taosArrayGetSize(pReq->pRetensions);
X
Xiaoyu Wang 已提交
1969 1970 1971 1972 1973
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t buildCreateDbReq(STranslateContext* pCxt, SCreateDatabaseStmt* pStmt, SCreateDbReq* pReq) {
1974 1975 1976
  SName name = {0};
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName));
  tNameGetFullDbName(&name, pReq->db);
X
Xiaoyu Wang 已提交
1977
  pReq->numOfVgroups = pStmt->pOptions->numOfVgroups;
S
Shengliang Guan 已提交
1978 1979 1980 1981
  pReq->numOfStables = pStmt->pOptions->singleStable;
  pReq->buffer = pStmt->pOptions->buffer;
  pReq->pageSize = pStmt->pOptions->pagesize;
  pReq->pages = pStmt->pOptions->pages;
X
Xiaoyu Wang 已提交
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
  pReq->daysPerFile = pStmt->pOptions->daysPerFile;
  pReq->daysToKeep0 = pStmt->pOptions->keep[0];
  pReq->daysToKeep1 = pStmt->pOptions->keep[1];
  pReq->daysToKeep2 = pStmt->pOptions->keep[2];
  pReq->minRows = pStmt->pOptions->minRowsPerBlock;
  pReq->maxRows = pStmt->pOptions->maxRowsPerBlock;
  pReq->fsyncPeriod = pStmt->pOptions->fsyncPeriod;
  pReq->walLevel = pStmt->pOptions->walLevel;
  pReq->precision = pStmt->pOptions->precision;
  pReq->compression = pStmt->pOptions->compressionLevel;
  pReq->replications = pStmt->pOptions->replica;
S
Shengliang Guan 已提交
1993
  pReq->strict = pStmt->pOptions->strict;
X
Xiaoyu Wang 已提交
1994
  pReq->cacheLastRow = pStmt->pOptions->cachelast;
1995
  pReq->ignoreExist = pStmt->ignoreExists;
X
Xiaoyu Wang 已提交
1996 1997 1998
  return buildCreateDbRetentions(pStmt->pOptions->pRetentions, pReq);
}

X
Xiaoyu Wang 已提交
1999
static int32_t checkRangeOption(STranslateContext* pCxt, const char* pName, int32_t val, int32_t minVal,
dengyihao's avatar
dengyihao 已提交
2000
                                int32_t maxVal) {
X
Xiaoyu Wang 已提交
2001
  if (val >= 0 && (val < minVal || val > maxVal)) {
X
Xiaoyu Wang 已提交
2002
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_RANGE_OPTION, pName, val, minVal, maxVal);
X
Xiaoyu Wang 已提交
2003 2004 2005 2006
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
2007 2008 2009
static int32_t checkDbDaysOption(STranslateContext* pCxt, SDatabaseOptions* pOptions) {
  if (NULL != pOptions->pDaysPerFile) {
    if (DEAL_RES_ERROR == translateValue(pCxt, pOptions->pDaysPerFile)) {
X
Xiaoyu Wang 已提交
2010 2011
      return pCxt->errCode;
    }
X
Xiaoyu Wang 已提交
2012 2013 2014 2015
    if (TIME_UNIT_MINUTE != pOptions->pDaysPerFile->unit && TIME_UNIT_HOUR != pOptions->pDaysPerFile->unit &&
        TIME_UNIT_DAY != pOptions->pDaysPerFile->unit) {
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_OPTION_UNIT, "daysPerFile",
                                  pOptions->pDaysPerFile->unit);
X
Xiaoyu Wang 已提交
2016
    }
X
Xiaoyu Wang 已提交
2017
    pOptions->daysPerFile = getBigintFromValueNode(pOptions->pDaysPerFile);
X
Xiaoyu Wang 已提交
2018
  }
X
Xiaoyu Wang 已提交
2019
  return checkRangeOption(pCxt, "daysPerFile", pOptions->daysPerFile, TSDB_MIN_DAYS_PER_FILE, TSDB_MAX_DAYS_PER_FILE);
X
Xiaoyu Wang 已提交
2020 2021
}

X
Xiaoyu Wang 已提交
2022 2023
static int32_t checkDbKeepOption(STranslateContext* pCxt, SDatabaseOptions* pOptions) {
  if (NULL == pOptions->pKeep) {
X
Xiaoyu Wang 已提交
2024 2025 2026
    return TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
2027
  int32_t numOfKeep = LIST_LENGTH(pOptions->pKeep);
X
Xiaoyu Wang 已提交
2028
  if (numOfKeep > 3 || numOfKeep < 1) {
X
Xiaoyu Wang 已提交
2029
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_KEEP_NUM);
X
Xiaoyu Wang 已提交
2030 2031 2032
  }

  SNode* pNode = NULL;
X
Xiaoyu Wang 已提交
2033 2034 2035
  FOREACH(pNode, pOptions->pKeep) {
    SValueNode* pVal = (SValueNode*)pNode;
    if (DEAL_RES_ERROR == translateValue(pCxt, pVal)) {
X
Xiaoyu Wang 已提交
2036 2037
      return pCxt->errCode;
    }
X
Xiaoyu Wang 已提交
2038 2039 2040 2041
    if (pVal->isDuration && TIME_UNIT_MINUTE != pVal->unit && TIME_UNIT_HOUR != pVal->unit &&
        TIME_UNIT_DAY != pVal->unit) {
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_KEEP_UNIT, pVal->unit);
    }
X
Xiaoyu Wang 已提交
2042 2043 2044
    if (!pVal->isDuration) {
      pVal->datum.i = pVal->datum.i * 1440;
    }
X
Xiaoyu Wang 已提交
2045 2046
  }

X
Xiaoyu Wang 已提交
2047 2048 2049 2050 2051 2052
  pOptions->keep[0] = getBigintFromValueNode((SValueNode*)nodesListGetNode(pOptions->pKeep, 0));

  if (numOfKeep < 2) {
    pOptions->keep[1] = pOptions->keep[0];
  } else {
    pOptions->keep[1] = getBigintFromValueNode((SValueNode*)nodesListGetNode(pOptions->pKeep, 1));
X
Xiaoyu Wang 已提交
2053
  }
X
Xiaoyu Wang 已提交
2054 2055 2056 2057
  if (numOfKeep < 3) {
    pOptions->keep[2] = pOptions->keep[1];
  } else {
    pOptions->keep[2] = getBigintFromValueNode((SValueNode*)nodesListGetNode(pOptions->pKeep, 2));
X
Xiaoyu Wang 已提交
2058 2059
  }

X
Xiaoyu Wang 已提交
2060 2061 2062 2063
  if (pOptions->keep[0] < TSDB_MIN_KEEP || pOptions->keep[1] < TSDB_MIN_KEEP || pOptions->keep[2] < TSDB_MIN_KEEP ||
      pOptions->keep[0] > TSDB_MAX_KEEP || pOptions->keep[1] > TSDB_MAX_KEEP || pOptions->keep[2] > TSDB_MAX_KEEP) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_KEEP_VALUE, pOptions->keep[0], pOptions->keep[1],
                                pOptions->keep[2], TSDB_MIN_KEEP, TSDB_MAX_KEEP);
2064 2065
  }

X
Xiaoyu Wang 已提交
2066 2067
  if (!((pOptions->keep[0] <= pOptions->keep[1]) && (pOptions->keep[1] <= pOptions->keep[2]))) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_KEEP_ORDER);
X
Xiaoyu Wang 已提交
2068 2069
  }

X
Xiaoyu Wang 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083
  return TSDB_CODE_SUCCESS;
}

static int32_t checkDbPrecisionOption(STranslateContext* pCxt, SDatabaseOptions* pOptions) {
  if ('\0' != pOptions->precisionStr[0]) {
    if (0 == strcmp(pOptions->precisionStr, TSDB_TIME_PRECISION_MILLI_STR)) {
      pOptions->precision = TSDB_TIME_PRECISION_MILLI;
    } else if (0 == strcmp(pOptions->precisionStr, TSDB_TIME_PRECISION_MICRO_STR)) {
      pOptions->precision = TSDB_TIME_PRECISION_MICRO;
    } else if (0 == strcmp(pOptions->precisionStr, TSDB_TIME_PRECISION_NANO_STR)) {
      pOptions->precision = TSDB_TIME_PRECISION_NANO;
    } else {
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_STR_OPTION, "precision", pOptions->precisionStr);
    }
X
Xiaoyu Wang 已提交
2084
  }
X
Xiaoyu Wang 已提交
2085 2086
  return TSDB_CODE_SUCCESS;
}
X
Xiaoyu Wang 已提交
2087

X
Xiaoyu Wang 已提交
2088
static int32_t checkDbEnumOption(STranslateContext* pCxt, const char* pName, int32_t val, int32_t v1, int32_t v2) {
X
Xiaoyu Wang 已提交
2089
  if (val >= 0 && val != v1 && val != v2) {
X
Xiaoyu Wang 已提交
2090 2091
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ENUM_OPTION, pName, val, v1, v2);
  }
X
Xiaoyu Wang 已提交
2092 2093 2094 2095 2096 2097 2098 2099 2100
  return TSDB_CODE_SUCCESS;
}

static int32_t checkDbRetentionsOption(STranslateContext* pCxt, SNodeList* pRetentions) {
  if (NULL == pRetentions) {
    return TSDB_CODE_SUCCESS;
  }

  if (LIST_LENGTH(pRetentions) > 3) {
X
Xiaoyu Wang 已提交
2101
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_RETENTIONS_OPTION);
X
Xiaoyu Wang 已提交
2102 2103
  }

X
Xiaoyu Wang 已提交
2104 2105 2106 2107 2108 2109
  SNode* pRetention = NULL;
  FOREACH(pRetention, pRetentions) {
    SNode* pNode = NULL;
    FOREACH(pNode, ((SNodeListNode*)pRetention)->pNodeList) {
      SValueNode* pVal = (SValueNode*)pNode;
      if (DEAL_RES_ERROR == translateValue(pCxt, pVal)) {
X
Xiaoyu Wang 已提交
2110 2111 2112 2113
        return pCxt->errCode;
      }
    }
  }
X
Xiaoyu Wang 已提交
2114

X
Xiaoyu Wang 已提交
2115
  return TSDB_CODE_SUCCESS;
2116 2117
}

2118
static int32_t checkOptionsDependency(STranslateContext* pCxt, const char* pDbName, SDatabaseOptions* pOptions) {
X
Xiaoyu Wang 已提交
2119 2120
  int32_t daysPerFile = pOptions->daysPerFile;
  int32_t daysToKeep0 = pOptions->keep[0];
2121 2122 2123
  if (-1 == daysPerFile && -1 == daysToKeep0) {
    return TSDB_CODE_SUCCESS;
  } else if (-1 == daysPerFile || -1 == daysToKeep0) {
X
Xiaoyu Wang 已提交
2124
    SDbCfgInfo dbCfg;
L
Liu Jicong 已提交
2125
    int32_t    code = getDBCfg(pCxt, pDbName, &dbCfg);
X
Xiaoyu Wang 已提交
2126 2127 2128 2129
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
    daysPerFile = (-1 == daysPerFile ? dbCfg.daysPerFile : daysPerFile);
X
Xiaoyu Wang 已提交
2130
    daysToKeep0 = (-1 == daysToKeep0 ? dbCfg.daysToKeep0 : daysToKeep0);
X
Xiaoyu Wang 已提交
2131 2132 2133 2134 2135 2136 2137
  }
  if (daysPerFile > daysToKeep0) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_DAYS_VALUE);
  }
  return TSDB_CODE_SUCCESS;
}

2138
static int32_t checkDatabaseOptions(STranslateContext* pCxt, const char* pDbName, SDatabaseOptions* pOptions) {
S
Shengliang Guan 已提交
2139 2140
  int32_t code =
      checkRangeOption(pCxt, "buffer", pOptions->buffer, TSDB_MIN_BUFFER_PER_VNODE, TSDB_MAX_BUFFER_PER_VNODE);
X
Xiaoyu Wang 已提交
2141
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2142
    code = checkRangeOption(pCxt, "cacheLast", pOptions->cachelast, TSDB_MIN_DB_CACHE_LAST_ROW,
dengyihao's avatar
dengyihao 已提交
2143
                            TSDB_MAX_DB_CACHE_LAST_ROW);
X
Xiaoyu Wang 已提交
2144 2145
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2146
    code = checkRangeOption(pCxt, "compression", pOptions->compressionLevel, TSDB_MIN_COMP_LEVEL, TSDB_MAX_COMP_LEVEL);
X
Xiaoyu Wang 已提交
2147 2148
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2149
    code = checkDbDaysOption(pCxt, pOptions);
X
Xiaoyu Wang 已提交
2150 2151
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2152
    code = checkRangeOption(pCxt, "fsyncPeriod", pOptions->fsyncPeriod, TSDB_MIN_FSYNC_PERIOD, TSDB_MAX_FSYNC_PERIOD);
X
Xiaoyu Wang 已提交
2153 2154
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2155
    code = checkRangeOption(pCxt, "maxRowsPerBlock", pOptions->maxRowsPerBlock, TSDB_MIN_MAXROWS_FBLOCK,
S
Shengliang Guan 已提交
2156
                            TSDB_MAX_MAXROWS_FBLOCK);
X
Xiaoyu Wang 已提交
2157 2158
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2159
    code = checkRangeOption(pCxt, "minRowsPerBlock", pOptions->minRowsPerBlock, TSDB_MIN_MINROWS_FBLOCK,
S
Shengliang Guan 已提交
2160
                            TSDB_MAX_MINROWS_FBLOCK);
X
Xiaoyu Wang 已提交
2161 2162
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2163
    code = checkDbKeepOption(pCxt, pOptions);
X
Xiaoyu Wang 已提交
2164 2165
  }
  if (TSDB_CODE_SUCCESS == code) {
S
Shengliang Guan 已提交
2166
    code = checkRangeOption(pCxt, "pages", pOptions->pages, TSDB_MIN_PAGES_PER_VNODE, TSDB_MAX_PAGES_PER_VNODE);
X
Xiaoyu Wang 已提交
2167 2168
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2169 2170
    code = checkRangeOption(pCxt, "pagesize", pOptions->pagesize, TSDB_MIN_PAGESIZE_PER_VNODE,
                            TSDB_MAX_PAGESIZE_PER_VNODE);
X
Xiaoyu Wang 已提交
2171 2172
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2173
    code = checkDbPrecisionOption(pCxt, pOptions);
X
Xiaoyu Wang 已提交
2174 2175
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2176
    code = checkDbEnumOption(pCxt, "replications", pOptions->replica, TSDB_MIN_DB_REPLICA, TSDB_MAX_DB_REPLICA);
X
Xiaoyu Wang 已提交
2177 2178
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2179
    code = checkDbEnumOption(pCxt, "strict", pOptions->strict, TSDB_DB_STRICT_OFF, TSDB_DB_STRICT_ON);
X
Xiaoyu Wang 已提交
2180 2181
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2182
    code = checkDbEnumOption(pCxt, "walLevel", pOptions->walLevel, TSDB_MIN_WAL_LEVEL, TSDB_MAX_WAL_LEVEL);
X
Xiaoyu Wang 已提交
2183 2184
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2185
    code = checkRangeOption(pCxt, "vgroups", pOptions->numOfVgroups, TSDB_MIN_VNODES_PER_DB, TSDB_MAX_VNODES_PER_DB);
X
Xiaoyu Wang 已提交
2186 2187
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2188 2189
    code = checkDbEnumOption(pCxt, "singleStable", pOptions->singleStable, TSDB_DB_SINGLE_STABLE_ON,
                             TSDB_DB_SINGLE_STABLE_OFF);
X
Xiaoyu Wang 已提交
2190
  }
2191
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2192
    code = checkDbRetentionsOption(pCxt, pOptions->pRetentions);
2193
  }
X
Xiaoyu Wang 已提交
2194
  if (TSDB_CODE_SUCCESS == code) {
2195
    code = checkOptionsDependency(pCxt, pDbName, pOptions);
X
Xiaoyu Wang 已提交
2196
  }
X
Xiaoyu Wang 已提交
2197 2198 2199 2200
  return code;
}

static int32_t checkCreateDatabase(STranslateContext* pCxt, SCreateDatabaseStmt* pStmt) {
2201
  return checkDatabaseOptions(pCxt, pStmt->dbName, pStmt->pOptions);
X
Xiaoyu Wang 已提交
2202 2203
}

H
Hongze Cheng 已提交
2204
typedef int32_t (*FSerializeFunc)(void* pBuf, int32_t bufLen, void* pReq);
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222

static int32_t buildCmdMsg(STranslateContext* pCxt, int16_t msgType, FSerializeFunc func, void* pReq) {
  pCxt->pCmdMsg = taosMemoryMalloc(sizeof(SCmdMsgInfo));
  if (NULL == pCxt->pCmdMsg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pCxt->pCmdMsg->epSet = pCxt->pParseCxt->mgmtEpSet;
  pCxt->pCmdMsg->msgType = msgType;
  pCxt->pCmdMsg->msgLen = func(NULL, 0, pReq);
  pCxt->pCmdMsg->pMsg = taosMemoryMalloc(pCxt->pCmdMsg->msgLen);
  if (NULL == pCxt->pCmdMsg->pMsg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  func(pCxt->pCmdMsg->pMsg, pCxt->pCmdMsg->msgLen, pReq);

  return TSDB_CODE_SUCCESS;
}

2223 2224 2225
static int32_t translateCreateDatabase(STranslateContext* pCxt, SCreateDatabaseStmt* pStmt) {
  SCreateDbReq createReq = {0};

X
Xiaoyu Wang 已提交
2226 2227 2228
  int32_t code = checkCreateDatabase(pCxt, pStmt);
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCreateDbReq(pCxt, pStmt, &createReq);
2229
  }
X
Xiaoyu Wang 已提交
2230 2231

  if (TSDB_CODE_SUCCESS == code) {
2232
    code = buildCmdMsg(pCxt, TDMT_MND_CREATE_DB, (FSerializeFunc)tSerializeSCreateDbReq, &createReq);
2233 2234
  }

X
Xiaoyu Wang 已提交
2235
  return code;
2236 2237
}

2238 2239
static int32_t translateDropDatabase(STranslateContext* pCxt, SDropDatabaseStmt* pStmt) {
  SDropDbReq dropReq = {0};
H
Hongze Cheng 已提交
2240
  SName      name = {0};
2241 2242 2243 2244
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName));
  tNameGetFullDbName(&name, dropReq.db);
  dropReq.ignoreNotExists = pStmt->ignoreNotExists;

2245
  return buildCmdMsg(pCxt, TDMT_MND_DROP_DB, (FSerializeFunc)tSerializeSDropDbReq, &dropReq);
2246 2247
}

2248 2249 2250 2251
static void buildAlterDbReq(STranslateContext* pCxt, SAlterDatabaseStmt* pStmt, SAlterDbReq* pReq) {
  SName name = {0};
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName));
  tNameGetFullDbName(&name, pReq->db);
S
Shengliang Guan 已提交
2252 2253 2254 2255
  pReq->buffer = pStmt->pOptions->buffer;
  pReq->pageSize = -1;
  pReq->pages = pStmt->pOptions->pages;
  pReq->daysPerFile = -1;
X
Xiaoyu Wang 已提交
2256 2257 2258
  pReq->daysToKeep0 = pStmt->pOptions->keep[0];
  pReq->daysToKeep1 = pStmt->pOptions->keep[1];
  pReq->daysToKeep2 = pStmt->pOptions->keep[2];
S
Shengliang Guan 已提交
2259
  pReq->fsyncPeriod = pStmt->pOptions->fsyncPeriod;
X
Xiaoyu Wang 已提交
2260
  pReq->walLevel = pStmt->pOptions->walLevel;
S
Shengliang Guan 已提交
2261 2262 2263
  pReq->strict = pStmt->pOptions->strict;
  pReq->cacheLastRow = pStmt->pOptions->cachelast;
  pReq->replications = pStmt->pOptions->replica;
2264 2265 2266 2267
  return;
}

static int32_t translateAlterDatabase(STranslateContext* pCxt, SAlterDatabaseStmt* pStmt) {
2268
  int32_t code = checkDatabaseOptions(pCxt, pStmt->dbName, pStmt->pOptions);
X
Xiaoyu Wang 已提交
2269 2270 2271 2272
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

2273 2274 2275
  SAlterDbReq alterReq = {0};
  buildAlterDbReq(pCxt, pStmt, &alterReq);

2276
  return buildCmdMsg(pCxt, TDMT_MND_ALTER_DB, (FSerializeFunc)tSerializeSAlterDbReq, &alterReq);
2277 2278
}

X
Xiaoyu Wang 已提交
2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
static int32_t calcTypeBytes(SDataType dt) {
  if (TSDB_DATA_TYPE_BINARY == dt.type) {
    return dt.bytes + VARSTR_HEADER_SIZE;
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
    return dt.bytes * TSDB_NCHAR_SIZE + VARSTR_HEADER_SIZE;
  } else {
    return dt.bytes;
  }
}

X
Xiaoyu Wang 已提交
2289
static int32_t columnDefNodeToField(SNodeList* pList, SArray** pArray) {
2290 2291 2292 2293
  *pArray = taosArrayInit(LIST_LENGTH(pList), sizeof(SField));
  SNode* pNode;
  FOREACH(pNode, pList) {
    SColumnDefNode* pCol = (SColumnDefNode*)pNode;
dengyihao's avatar
dengyihao 已提交
2294
    SField          field = {.type = pCol->dataType.type, .bytes = calcTypeBytes(pCol->dataType)};
2295
    strcpy(field.name, pCol->colName);
2296
    if (pCol->sma) {
H
Hongze Cheng 已提交
2297
      field.flags |= COL_SMA_ON;
2298
    }
X
Xiaoyu Wang 已提交
2299 2300 2301 2302 2303
    taosArrayPush(*pArray, &field);
  }
  return TSDB_CODE_SUCCESS;
}

2304
static SColumnDefNode* findColDef(SNodeList* pCols, const SColumnNode* pCol) {
X
Xiaoyu Wang 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313
  SNode* pColDef = NULL;
  FOREACH(pColDef, pCols) {
    if (0 == strcmp(pCol->colName, ((SColumnDefNode*)pColDef)->colName)) {
      return (SColumnDefNode*)pColDef;
    }
  }
  return NULL;
}

X
Xiaoyu Wang 已提交
2314 2315 2316 2317
static int32_t checTableFactorOption(STranslateContext* pCxt, float val) {
  if (val < TSDB_MIN_ROLLUP_FILE_FACTOR || val > TSDB_MAX_ROLLUP_FILE_FACTOR) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_F_RANGE_OPTION, "file_factor", val,
                                TSDB_MIN_ROLLUP_FILE_FACTOR, TSDB_MAX_ROLLUP_FILE_FACTOR);
X
Xiaoyu Wang 已提交
2318 2319 2320 2321 2322
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t checkTableSmaOption(STranslateContext* pCxt, SCreateTableStmt* pStmt) {
X
Xiaoyu Wang 已提交
2323 2324
  if (NULL != pStmt->pOptions->pSma) {
    SNode* pNode = NULL;
dengyihao's avatar
dengyihao 已提交
2325
    FOREACH(pNode, pStmt->pCols) { ((SColumnDefNode*)pNode)->sma = false; }
X
Xiaoyu Wang 已提交
2326
    FOREACH(pNode, pStmt->pOptions->pSma) {
dengyihao's avatar
dengyihao 已提交
2327
      SColumnNode*    pSmaCol = (SColumnNode*)pNode;
2328
      SColumnDefNode* pColDef = findColDef(pStmt->pCols, pSmaCol);
X
Xiaoyu Wang 已提交
2329 2330 2331 2332
      if (NULL == pColDef) {
        return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COLUMN, pSmaCol->colName);
      }
      pSmaCol->node.resType = pColDef->dataType;
2333
      pColDef->sma = true;
X
Xiaoyu Wang 已提交
2334 2335
    }
  }
X
Xiaoyu Wang 已提交
2336 2337 2338 2339 2340 2341 2342 2343 2344
  return TSDB_CODE_SUCCESS;
}

static int32_t checkTableRollupOption(STranslateContext* pCxt, SNodeList* pFuncs) {
  if (NULL == pFuncs) {
    return TSDB_CODE_SUCCESS;
  }

  if (1 != LIST_LENGTH(pFuncs)) {
X
Xiaoyu Wang 已提交
2345
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ROLLUP_OPTION);
X
Xiaoyu Wang 已提交
2346
  }
X
Xiaoyu Wang 已提交
2347 2348 2349
  return TSDB_CODE_SUCCESS;
}

2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
static int32_t checkTableTagsSchema(STranslateContext* pCxt, SHashObj* pHash, SNodeList* pTags) {
  int32_t ntags = LIST_LENGTH(pTags);
  if (0 == ntags) {
    return TSDB_CODE_SUCCESS;
  } else if (ntags > TSDB_MAX_TAGS) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_TAGS_NUM);
  }

  int32_t code = TSDB_CODE_SUCCESS;
  int32_t tagsSize = 0;
  SNode*  pNode = NULL;
  FOREACH(pNode, pTags) {
    SColumnDefNode* pTag = (SColumnDefNode*)pNode;
    int32_t         len = strlen(pTag->colName);
    if (NULL != taosHashGet(pHash, pTag->colName, len)) {
      code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DUPLICATED_COLUMN);
    }
    if (TSDB_CODE_SUCCESS == code && pTag->dataType.type == TSDB_DATA_TYPE_JSON && ntags > 1) {
      code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_ONLY_ONE_JSON_TAG);
    }
    if (TSDB_CODE_SUCCESS == code) {
      if ((TSDB_DATA_TYPE_VARCHAR == pTag->dataType.type && pTag->dataType.bytes > TSDB_MAX_BINARY_LEN) ||
          (TSDB_DATA_TYPE_NCHAR == pTag->dataType.type && pTag->dataType.bytes > TSDB_MAX_NCHAR_LEN)) {
        code = code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
      }
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = taosHashPut(pHash, pTag->colName, len, &pTag, POINTER_BYTES);
    }
    if (TSDB_CODE_SUCCESS == code) {
      tagsSize += pTag->dataType.bytes;
    } else {
      break;
    }
  }

  if (TSDB_CODE_SUCCESS == code && tagsSize > TSDB_MAX_TAGS_LEN) {
    code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_TAGS_LENGTH, TSDB_MAX_TAGS_LEN);
  }

  return code;
}

static int32_t checkTableColsSchema(STranslateContext* pCxt, SHashObj* pHash, SNodeList* pCols) {
  int32_t ncols = LIST_LENGTH(pCols);
  if (ncols < TSDB_MIN_COLUMNS) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COLUMNS_NUM);
  } else if (ncols > TSDB_MAX_COLUMNS) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_TOO_MANY_COLUMNS);
  }

  int32_t code = TSDB_CODE_SUCCESS;

  bool    first = true;
  int32_t rowSize = 0;
  SNode*  pNode = NULL;
  FOREACH(pNode, pCols) {
    SColumnDefNode* pCol = (SColumnDefNode*)pNode;
    if (first) {
      first = false;
      if (TSDB_DATA_TYPE_TIMESTAMP != pCol->dataType.type) {
        code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_FIRST_COLUMN);
      }
    }
    int32_t len = strlen(pCol->colName);
    if (TSDB_CODE_SUCCESS == code && NULL != taosHashGet(pHash, pCol->colName, len)) {
      code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DUPLICATED_COLUMN);
    }
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2419 2420
      if ((TSDB_DATA_TYPE_VARCHAR == pCol->dataType.type && calcTypeBytes(pCol->dataType) > TSDB_MAX_BINARY_LEN) ||
          (TSDB_DATA_TYPE_NCHAR == pCol->dataType.type && calcTypeBytes(pCol->dataType) > TSDB_MAX_NCHAR_LEN)) {
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
        code = code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_VAR_COLUMN_LEN);
      }
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = taosHashPut(pHash, pCol->colName, len, &pCol, POINTER_BYTES);
    }
    if (TSDB_CODE_SUCCESS == code) {
      rowSize += pCol->dataType.bytes;
    } else {
      break;
    }
  }

  if (TSDB_CODE_SUCCESS == code && rowSize > TSDB_MAX_BYTES_PER_ROW) {
    code = generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ROW_LENGTH, TSDB_MAX_BYTES_PER_ROW);
  }

  return code;
}

static int32_t checkTableSchema(STranslateContext* pCxt, SCreateTableStmt* pStmt) {
  SHashObj* pHash = taosHashInit(LIST_LENGTH(pStmt->pTags) + LIST_LENGTH(pStmt->pCols),
                                 taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
  if (NULL == pHash) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = checkTableTagsSchema(pCxt, pHash, pStmt->pTags);
  if (TSDB_CODE_SUCCESS == code) {
    code = checkTableColsSchema(pCxt, pHash, pStmt->pCols);
  }

  taosHashCleanup(pHash);
  return code;
}

X
Xiaoyu Wang 已提交
2457
static int32_t checkCreateTable(STranslateContext* pCxt, SCreateTableStmt* pStmt) {
X
Xiaoyu Wang 已提交
2458
  int32_t code = checkRangeOption(pCxt, "delay", pStmt->pOptions->delay, TSDB_MIN_ROLLUP_DELAY, TSDB_MAX_ROLLUP_DELAY);
X
Xiaoyu Wang 已提交
2459
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2460
    code = checTableFactorOption(pCxt, pStmt->pOptions->filesFactor);
X
Xiaoyu Wang 已提交
2461 2462
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2463
    code = checkTableRollupOption(pCxt, pStmt->pOptions->pRollupFuncs);
X
Xiaoyu Wang 已提交
2464 2465
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2466
    code = checkRangeOption(pCxt, "ttl", pStmt->pOptions->ttl, TSDB_MIN_TABLE_TTL, INT32_MAX);
X
Xiaoyu Wang 已提交
2467 2468
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
2469
    code = checkTableSmaOption(pCxt, pStmt);
X
Xiaoyu Wang 已提交
2470
  }
2471
  if (TSDB_CODE_SUCCESS == code) {
2472
    code = checkTableSchema(pCxt, pStmt);
2473
  }
X
Xiaoyu Wang 已提交
2474 2475 2476
  return code;
}

2477 2478 2479
static void toSchema(const SColumnDefNode* pCol, col_id_t colId, SSchema* pSchema) {
  int8_t flags = 0;
  if (pCol->sma) {
H
Hongze Cheng 已提交
2480
    flags |= COL_SMA_ON;
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
  }
  pSchema->colId = colId;
  pSchema->type = pCol->dataType.type;
  pSchema->bytes = calcTypeBytes(pCol->dataType);
  pSchema->flags = flags;
  strcpy(pSchema->name, pCol->colName);
}

typedef struct SSampleAstInfo {
  const char* pDbName;
  const char* pTableName;
L
Liu Jicong 已提交
2492 2493 2494 2495
  SNodeList*  pFuncs;
  SNode*      pInterval;
  SNode*      pOffset;
  SNode*      pSliding;
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
  STableMeta* pRollupTableMeta;
} SSampleAstInfo;

static int32_t buildSampleAst(STranslateContext* pCxt, SSampleAstInfo* pInfo, char** pAst, int32_t* pLen) {
  SSelectStmt* pSelect = nodesMakeNode(QUERY_NODE_SELECT_STMT);
  if (NULL == pSelect) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  sprintf(pSelect->stmtName, "%p", pSelect);

  SRealTableNode* pTable = nodesMakeNode(QUERY_NODE_REAL_TABLE);
  if (NULL == pTable) {
    nodesDestroyNode(pSelect);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  strcpy(pTable->table.dbName, pInfo->pDbName);
  strcpy(pTable->table.tableName, pInfo->pTableName);
wafwerar's avatar
wafwerar 已提交
2513
  TSWAP(pTable->pMeta, pInfo->pRollupTableMeta);
2514 2515
  pSelect->pFromTable = (SNode*)pTable;

wafwerar's avatar
wafwerar 已提交
2516
  TSWAP(pSelect->pProjectionList, pInfo->pFuncs);
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
  SFunctionNode* pFunc = nodesMakeNode(QUERY_NODE_FUNCTION);
  if (NULL == pSelect->pProjectionList || NULL == pFunc) {
    nodesDestroyNode(pSelect);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  strcpy(pFunc->functionName, "_wstartts");
  nodesListPushFront(pSelect->pProjectionList, pFunc);
  SNode* pProject = NULL;
  FOREACH(pProject, pSelect->pProjectionList) { sprintf(((SExprNode*)pProject)->aliasName, "#%p", pProject); }

  SIntervalWindowNode* pInterval = nodesMakeNode(QUERY_NODE_INTERVAL_WINDOW);
  if (NULL == pInterval) {
    nodesDestroyNode(pSelect);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pSelect->pWindow = (SNode*)pInterval;
wafwerar's avatar
wafwerar 已提交
2533 2534 2535
  TSWAP(pInterval->pInterval, pInfo->pInterval);
  TSWAP(pInterval->pOffset, pInfo->pOffset);
  TSWAP(pInterval->pSliding, pInfo->pSliding);
2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
  pInterval->pCol = nodesMakeNode(QUERY_NODE_COLUMN);
  if (NULL == pInterval->pCol) {
    nodesDestroyNode(pSelect);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  ((SColumnNode*)pInterval->pCol)->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  strcpy(((SColumnNode*)pInterval->pCol)->colName, PK_TS_COL_INTERNAL_NAME);

  int32_t code = translateQuery(pCxt, (SNode*)pSelect);
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesNodeToString(pSelect, false, pAst, pLen);
  }
  nodesDestroyNode(pSelect);
  return code;
}

static void clearSampleAstInfo(SSampleAstInfo* pInfo) {
  nodesDestroyList(pInfo->pFuncs);
  nodesDestroyNode(pInfo->pInterval);
  nodesDestroyNode(pInfo->pOffset);
  nodesDestroyNode(pInfo->pSliding);
}

static SNode* makeIntervalVal(SRetention* pRetension, int8_t precision) {
  SValueNode* pVal = nodesMakeNode(QUERY_NODE_VALUE);
  if (NULL == pVal) {
    return NULL;
  }
  int64_t timeVal = convertTimeFromPrecisionToUnit(pRetension->freq, precision, pRetension->freqUnit);
L
Liu Jicong 已提交
2565 2566
  char    buf[20] = {0};
  int32_t len = snprintf(buf, sizeof(buf), "%" PRId64 "%c", timeVal, pRetension->freqUnit);
2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606
  pVal->literal = strndup(buf, len);
  if (NULL == pVal->literal) {
    nodesDestroyNode(pVal);
    return NULL;
  }
  pVal->isDuration = true;
  pVal->node.resType.type = TSDB_DATA_TYPE_BIGINT;
  pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes;
  pVal->node.resType.precision = precision;
  return (SNode*)pVal;
}

static SNode* createColumnFromDef(SColumnDefNode* pDef) {
  SColumnNode* pCol = nodesMakeNode(QUERY_NODE_COLUMN);
  if (NULL == pCol) {
    return NULL;
  }
  strcpy(pCol->colName, pDef->colName);
  return (SNode*)pCol;
}

static SNode* createRollupFunc(SNode* pSrcFunc, SColumnDefNode* pColDef) {
  SFunctionNode* pFunc = nodesCloneNode(pSrcFunc);
  if (NULL == pFunc) {
    return NULL;
  }
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pFunc->pParameterList, createColumnFromDef(pColDef))) {
    nodesDestroyNode(pFunc);
    return NULL;
  }
  return (SNode*)pFunc;
}

static SNodeList* createRollupFuncs(SCreateTableStmt* pStmt) {
  SNodeList* pFuncs = nodesMakeList();
  if (NULL == pFuncs) {
    return NULL;
  }

  SNode* pFunc = NULL;
X
Xiaoyu Wang 已提交
2607
  FOREACH(pFunc, pStmt->pOptions->pRollupFuncs) {
2608
    SNode* pCol = NULL;
L
Liu Jicong 已提交
2609
    bool   primaryKey = true;
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
    FOREACH(pCol, pStmt->pCols) {
      if (primaryKey) {
        primaryKey = false;
        continue;
      }
      if (TSDB_CODE_SUCCESS != nodesListStrictAppend(pFuncs, createRollupFunc(pFunc, (SColumnDefNode*)pCol))) {
        nodesDestroyList(pFuncs);
        return NULL;
      }
    }
  }

  return pFuncs;
}

static STableMeta* createRollupTableMeta(SCreateTableStmt* pStmt, int8_t precision) {
L
Liu Jicong 已提交
2626
  int32_t     numOfField = LIST_LENGTH(pStmt->pCols) + LIST_LENGTH(pStmt->pTags);
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
  STableMeta* pMeta = taosMemoryCalloc(1, sizeof(STableMeta) + numOfField * sizeof(SSchema));
  if (NULL == pMeta) {
    return NULL;
  }
  pMeta->tableType = TSDB_SUPER_TABLE;
  pMeta->tableInfo.numOfTags = LIST_LENGTH(pStmt->pTags);
  pMeta->tableInfo.precision = precision;
  pMeta->tableInfo.numOfColumns = LIST_LENGTH(pStmt->pCols);

  int32_t index = 0;
L
Liu Jicong 已提交
2637
  SNode*  pCol = NULL;
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650
  FOREACH(pCol, pStmt->pCols) {
    toSchema((SColumnDefNode*)pCol, index + 1, pMeta->schema + index);
    ++index;
  }
  SNode* pTag = NULL;
  FOREACH(pTag, pStmt->pTags) {
    toSchema((SColumnDefNode*)pTag, index + 1, pMeta->schema + index);
    ++index;
  }

  return pMeta;
}

L
Liu Jicong 已提交
2651 2652
static int32_t buildSampleAstInfoByTable(STranslateContext* pCxt, SCreateTableStmt* pStmt, SRetention* pRetension,
                                         int8_t precision, SSampleAstInfo* pInfo) {
2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
  pInfo->pDbName = pStmt->dbName;
  pInfo->pTableName = pStmt->tableName;
  pInfo->pFuncs = createRollupFuncs(pStmt);
  pInfo->pInterval = makeIntervalVal(pRetension, precision);
  pInfo->pRollupTableMeta = createRollupTableMeta(pStmt, precision);
  if (NULL == pInfo->pFuncs || NULL == pInfo->pInterval || NULL == pInfo->pRollupTableMeta) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  return TSDB_CODE_SUCCESS;
}

L
Liu Jicong 已提交
2664 2665
static int32_t getRollupAst(STranslateContext* pCxt, SCreateTableStmt* pStmt, SRetention* pRetension, int8_t precision,
                            char** pAst, int32_t* pLen) {
2666
  SSampleAstInfo info = {0};
L
Liu Jicong 已提交
2667
  int32_t        code = buildSampleAstInfoByTable(pCxt, pStmt, pRetension, precision, &info);
2668 2669 2670 2671 2672 2673 2674 2675 2676
  if (TSDB_CODE_SUCCESS == code) {
    code = buildSampleAst(pCxt, &info, pAst, pLen);
  }
  clearSampleAstInfo(&info);
  return code;
}

static int32_t buildRollupAst(STranslateContext* pCxt, SCreateTableStmt* pStmt, SMCreateStbReq* pReq) {
  SDbCfgInfo dbCfg = {0};
L
Liu Jicong 已提交
2677 2678
  int32_t    code = getDBCfg(pCxt, pStmt->dbName, &dbCfg);
  int32_t    num = taosArrayGetSize(dbCfg.pRetensions);
2679
  if (TSDB_CODE_SUCCESS != code || num < 2) {
X
Xiaoyu Wang 已提交
2680 2681
    return code;
  }
2682
  for (int32_t i = 1; i < num; ++i) {
L
Liu Jicong 已提交
2683
    SRetention*       pRetension = taosArrayGet(dbCfg.pRetensions, i);
2684 2685
    STranslateContext cxt = {0};
    initTranslateContext(pCxt->pParseCxt, &cxt);
L
Liu Jicong 已提交
2686 2687
    code = getRollupAst(&cxt, pStmt, pRetension, dbCfg.precision, 1 == i ? &pReq->pAst1 : &pReq->pAst2,
                        1 == i ? &pReq->ast1Len : &pReq->ast2Len);
2688 2689 2690 2691 2692 2693 2694
    destroyTranslateContext(&cxt);
    if (TSDB_CODE_SUCCESS != code) {
      break;
    }
  }
  return code;
}
X
Xiaoyu Wang 已提交
2695

2696 2697
static int32_t buildCreateStbReq(STranslateContext* pCxt, SCreateTableStmt* pStmt, SMCreateStbReq* pReq) {
  pReq->igExists = pStmt->ignoreExists;
X
Xiaoyu Wang 已提交
2698 2699
  pReq->xFilesFactor = pStmt->pOptions->filesFactor;
  pReq->delay = pStmt->pOptions->delay;
X
Xiaoyu Wang 已提交
2700
  pReq->ttl = pStmt->pOptions->ttl;
2701 2702 2703 2704
  columnDefNodeToField(pStmt->pCols, &pReq->pColumns);
  columnDefNodeToField(pStmt->pTags, &pReq->pTags);
  pReq->numOfColumns = LIST_LENGTH(pStmt->pCols);
  pReq->numOfTags = LIST_LENGTH(pStmt->pTags);
X
Xiaoyu Wang 已提交
2705 2706 2707 2708 2709 2710 2711
  if ('\0' != pStmt->pOptions->comment[0]) {
    pReq->comment = strdup(pStmt->pOptions->comment);
    if (NULL == pReq->comment) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    pReq->commentLen = strlen(pStmt->pOptions->comment) + 1;
  }
2712

X
Xiaoyu Wang 已提交
2713 2714
  SName tableName;
  tNameExtractFullName(toName(pCxt->pParseCxt->acctId, pStmt->dbName, pStmt->tableName, &tableName), pReq->name);
2715 2716 2717

  return buildRollupAst(pCxt, pStmt, pReq);
}
2718

2719 2720
static int32_t translateCreateSuperTable(STranslateContext* pCxt, SCreateTableStmt* pStmt) {
  SMCreateStbReq createReq = {0};
L
Liu Jicong 已提交
2721
  int32_t        code = checkCreateTable(pCxt, pStmt);
2722 2723 2724 2725 2726 2727
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCreateStbReq(pCxt, pStmt, &createReq);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCmdMsg(pCxt, TDMT_MND_CREATE_STB, (FSerializeFunc)tSerializeSMCreateStbReq, &createReq);
  }
X
Xiaoyu Wang 已提交
2728
  tFreeSMCreateStbReq(&createReq);
2729
  return code;
2730 2731
}

2732 2733 2734 2735 2736
static int32_t doTranslateDropSuperTable(STranslateContext* pCxt, const SName* pTableName, bool ignoreNotExists) {
  SMDropStbReq dropReq = {0};
  tNameExtractFullName(pTableName, dropReq.name);
  dropReq.igNotExists = ignoreNotExists;

2737
  return buildCmdMsg(pCxt, TDMT_MND_DROP_STB, (FSerializeFunc)tSerializeSMDropStbReq, &dropReq);
2738 2739 2740 2741 2742 2743
}

static int32_t translateDropTable(STranslateContext* pCxt, SDropTableStmt* pStmt) {
  SDropTableClause* pClause = nodesListGetNode(pStmt->pTables, 0);

  STableMeta* pTableMeta = NULL;
dengyihao's avatar
dengyihao 已提交
2744
  SName       tableName;
dengyihao's avatar
dengyihao 已提交
2745
  int32_t     code = getTableMetaImpl(
H
Hongze Cheng 已提交
2746
          pCxt, toName(pCxt->pParseCxt->acctId, pClause->dbName, pClause->tableName, &tableName), &pTableMeta);
D
dapan1121 已提交
2747
  if ((TSDB_CODE_PAR_TABLE_NOT_EXIST == code || TSDB_CODE_VND_TB_NOT_EXIST == code) && pClause->ignoreNotExists) {
X
Xiaoyu Wang 已提交
2748 2749
    return TSDB_CODE_SUCCESS;
  }
2750 2751 2752 2753
  if (TSDB_CODE_SUCCESS == code) {
    if (TSDB_SUPER_TABLE == pTableMeta->tableType) {
      code = doTranslateDropSuperTable(pCxt, &tableName, pClause->ignoreNotExists);
    } else {
X
Xiaoyu Wang 已提交
2754
      // todo : drop normal table or child table
2755 2756
      code = TSDB_CODE_FAILED;
    }
wafwerar's avatar
wafwerar 已提交
2757
    taosMemoryFreeClear(pTableMeta);
2758 2759 2760 2761 2762 2763
  }

  return code;
}

static int32_t translateDropSuperTable(STranslateContext* pCxt, SDropSuperTableStmt* pStmt) {
X
Xiaoyu Wang 已提交
2764
  SName tableName;
L
Liu Jicong 已提交
2765 2766
  return doTranslateDropSuperTable(pCxt, toName(pCxt->pParseCxt->acctId, pStmt->dbName, pStmt->tableName, &tableName),
                                   pStmt->ignoreNotExists);
2767 2768
}

S
Shengliang Guan 已提交
2769
static int32_t setAlterTableField(SAlterTableStmt* pStmt, SMAlterStbReq* pAlterReq) {
X
Xiaoyu Wang 已提交
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
  if (TSDB_ALTER_TABLE_UPDATE_OPTIONS == pStmt->alterType) {
    pAlterReq->ttl = pStmt->pOptions->ttl;
    if ('\0' != pStmt->pOptions->comment[0]) {
      pAlterReq->comment = strdup(pStmt->pOptions->comment);
      if (NULL == pAlterReq->comment) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      pAlterReq->commentLen = strlen(pStmt->pOptions->comment) + 1;
    }
    return TSDB_CODE_SUCCESS;
  }

2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793
  pAlterReq->pFields = taosArrayInit(2, sizeof(TAOS_FIELD));
  if (NULL == pAlterReq->pFields) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  switch (pStmt->alterType) {
    case TSDB_ALTER_TABLE_ADD_TAG:
    case TSDB_ALTER_TABLE_DROP_TAG:
    case TSDB_ALTER_TABLE_ADD_COLUMN:
    case TSDB_ALTER_TABLE_DROP_COLUMN:
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES: {
X
Xiaoyu Wang 已提交
2794
      TAOS_FIELD field = {.type = pStmt->dataType.type, .bytes = calcTypeBytes(pStmt->dataType)};
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
      strcpy(field.name, pStmt->colName);
      taosArrayPush(pAlterReq->pFields, &field);
      break;
    }
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME: {
      TAOS_FIELD oldField = {0};
      strcpy(oldField.name, pStmt->colName);
      taosArrayPush(pAlterReq->pFields, &oldField);
      TAOS_FIELD newField = {0};
X
Xiaoyu Wang 已提交
2805
      strcpy(newField.name, pStmt->newColName);
2806 2807 2808 2809 2810 2811 2812
      taosArrayPush(pAlterReq->pFields, &newField);
      break;
    }
    default:
      break;
  }

2813
  pAlterReq->numOfFields = taosArrayGetSize(pAlterReq->pFields);
2814 2815 2816
  return TSDB_CODE_SUCCESS;
}

2817
static int32_t translateAlterTable(STranslateContext* pCxt, SAlterTableStmt* pStmt) {
S
Shengliang Guan 已提交
2818 2819
  SMAlterStbReq alterReq = {0};
  SName         tableName;
X
Xiaoyu Wang 已提交
2820
  tNameExtractFullName(toName(pCxt->pParseCxt->acctId, pStmt->dbName, pStmt->tableName, &tableName), alterReq.name);
2821
  alterReq.alterType = pStmt->alterType;
X
Xiaoyu Wang 已提交
2822
  if (TSDB_ALTER_TABLE_UPDATE_TAG_VAL == pStmt->alterType) {
2823
    return TSDB_CODE_FAILED;
2824 2825 2826 2827 2828
  } else {
    if (TSDB_CODE_SUCCESS != setAlterTableField(pStmt, &alterReq)) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
2829

2830
  return buildCmdMsg(pCxt, TDMT_MND_ALTER_STB, (FSerializeFunc)tSerializeSMAlterStbReq, &alterReq);
2831 2832
}

2833
static int32_t translateUseDatabase(STranslateContext* pCxt, SUseDatabaseStmt* pStmt) {
X
Xiaoyu Wang 已提交
2834
  SUseDbReq usedbReq = {0};
dengyihao's avatar
dengyihao 已提交
2835
  SName     name = {0};
2836 2837
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName));
  tNameExtractFullName(&name, usedbReq.db);
X
Xiaoyu Wang 已提交
2838
  int32_t code = getDBVgVersion(pCxt, usedbReq.db, &usedbReq.vgVersion, &usedbReq.dbId, &usedbReq.numOfTable);
2839 2840
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCmdMsg(pCxt, TDMT_MND_USE_DB, (FSerializeFunc)tSerializeSUseDbReq, &usedbReq);
2841
  }
2842
  return code;
2843 2844
}

2845 2846 2847 2848 2849 2850 2851
static int32_t translateCreateUser(STranslateContext* pCxt, SCreateUserStmt* pStmt) {
  SCreateUserReq createReq = {0};
  strcpy(createReq.user, pStmt->useName);
  createReq.createType = 0;
  createReq.superUser = 0;
  strcpy(createReq.pass, pStmt->password);

2852
  return buildCmdMsg(pCxt, TDMT_MND_CREATE_USER, (FSerializeFunc)tSerializeSCreateUserReq, &createReq);
2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
}

static int32_t translateAlterUser(STranslateContext* pCxt, SAlterUserStmt* pStmt) {
  SAlterUserReq alterReq = {0};
  strcpy(alterReq.user, pStmt->useName);
  alterReq.alterType = pStmt->alterType;
  alterReq.superUser = 0;
  strcpy(alterReq.pass, pStmt->password);
  if (NULL != pCxt->pParseCxt->db) {
    strcpy(alterReq.dbname, pCxt->pParseCxt->db);
  }

2865
  return buildCmdMsg(pCxt, TDMT_MND_ALTER_USER, (FSerializeFunc)tSerializeSAlterUserReq, &alterReq);
2866 2867 2868 2869 2870 2871
}

static int32_t translateDropUser(STranslateContext* pCxt, SDropUserStmt* pStmt) {
  SDropUserReq dropReq = {0};
  strcpy(dropReq.user, pStmt->useName);

2872
  return buildCmdMsg(pCxt, TDMT_MND_DROP_USER, (FSerializeFunc)tSerializeSDropUserReq, &dropReq);
2873 2874 2875 2876 2877 2878 2879
}

static int32_t translateCreateDnode(STranslateContext* pCxt, SCreateDnodeStmt* pStmt) {
  SCreateDnodeReq createReq = {0};
  strcpy(createReq.fqdn, pStmt->fqdn);
  createReq.port = pStmt->port;

2880
  return buildCmdMsg(pCxt, TDMT_MND_CREATE_DNODE, (FSerializeFunc)tSerializeSCreateDnodeReq, &createReq);
2881 2882 2883 2884 2885 2886 2887 2888
}

static int32_t translateDropDnode(STranslateContext* pCxt, SDropDnodeStmt* pStmt) {
  SDropDnodeReq dropReq = {0};
  dropReq.dnodeId = pStmt->dnodeId;
  strcpy(dropReq.fqdn, pStmt->fqdn);
  dropReq.port = pStmt->port;

2889
  return buildCmdMsg(pCxt, TDMT_MND_DROP_DNODE, (FSerializeFunc)tSerializeSDropDnodeReq, &dropReq);
2890 2891
}

2892 2893 2894 2895 2896 2897
static int32_t translateAlterDnode(STranslateContext* pCxt, SAlterDnodeStmt* pStmt) {
  SMCfgDnodeReq cfgReq = {0};
  cfgReq.dnodeId = pStmt->dnodeId;
  strcpy(cfgReq.config, pStmt->config);
  strcpy(cfgReq.value, pStmt->value);

2898
  return buildCmdMsg(pCxt, TDMT_MND_CONFIG_DNODE, (FSerializeFunc)tSerializeSMCfgDnodeReq, &cfgReq);
2899 2900
}

2901
static int32_t nodeTypeToShowType(ENodeType nt) {
2902 2903 2904 2905 2906 2907 2908 2909
  switch (nt) {
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
      return TSDB_MGMT_TABLE_CONNS;
    case QUERY_NODE_SHOW_LICENCE_STMT:
      return TSDB_MGMT_TABLE_GRANTS;
    case QUERY_NODE_SHOW_QUERIES_STMT:
      return TSDB_MGMT_TABLE_QUERIES;
    case QUERY_NODE_SHOW_VARIABLE_STMT:
L
Liu Jicong 已提交
2910
      return 0;  // todo
2911 2912 2913
    default:
      break;
  }
2914 2915 2916
  return 0;
}

2917
static int32_t translateShow(STranslateContext* pCxt, SShowStmt* pStmt) {
dengyihao's avatar
dengyihao 已提交
2918
  SShowReq showReq = {.type = nodeTypeToShowType(nodeType(pStmt))};
2919
  return buildCmdMsg(pCxt, TDMT_MND_SHOW, (FSerializeFunc)tSerializeSShowReq, &showReq);
2920 2921
}

X
Xiaoyu Wang 已提交
2922 2923
static int32_t getSmaIndexDstVgId(STranslateContext* pCxt, char* pTableName, int32_t* pVgId) {
  SVgroupInfo vg = {0};
dengyihao's avatar
dengyihao 已提交
2924
  int32_t     code = getTableHashVgroup(pCxt, pCxt->pParseCxt->db, pTableName, &vg);
X
Xiaoyu Wang 已提交
2925 2926 2927 2928 2929
  if (TSDB_CODE_SUCCESS == code) {
    *pVgId = vg.vgId;
  }
  return code;
}
X
Xiaoyu Wang 已提交
2930

X
Xiaoyu Wang 已提交
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943
static int32_t getSmaIndexSql(STranslateContext* pCxt, char** pSql, int32_t* pLen) {
  *pSql = strdup(pCxt->pParseCxt->pSql);
  if (NULL == *pSql) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  *pLen = pCxt->pParseCxt->sqlLen + 1;
  return TSDB_CODE_SUCCESS;
}

static int32_t getSmaIndexExpr(STranslateContext* pCxt, SCreateIndexStmt* pStmt, char** pExpr, int32_t* pLen) {
  return nodesListToString(pStmt->pOptions->pFuncs, false, pExpr, pLen);
}

2944 2945 2946 2947 2948 2949 2950 2951
static int32_t buildSampleAstInfoByIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt, SSampleAstInfo* pInfo) {
  pInfo->pDbName = pCxt->pParseCxt->db;
  pInfo->pTableName = pStmt->tableName;
  pInfo->pFuncs = nodesCloneList(pStmt->pOptions->pFuncs);
  pInfo->pInterval = nodesCloneNode(pStmt->pOptions->pInterval);
  pInfo->pOffset = nodesCloneNode(pStmt->pOptions->pOffset);
  pInfo->pSliding = nodesCloneNode(pStmt->pOptions->pSliding);
  if (NULL == pInfo->pFuncs || NULL == pInfo->pInterval ||
L
Liu Jicong 已提交
2952 2953
      (NULL != pStmt->pOptions->pOffset && NULL == pInfo->pOffset) ||
      (NULL != pStmt->pOptions->pSliding && NULL == pInfo->pSliding)) {
X
Xiaoyu Wang 已提交
2954
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
2955
  }
2956 2957
  return TSDB_CODE_SUCCESS;
}
X
Xiaoyu Wang 已提交
2958

2959 2960
static int32_t getSmaIndexAst(STranslateContext* pCxt, SCreateIndexStmt* pStmt, char** pAst, int32_t* pLen) {
  SSampleAstInfo info = {0};
L
Liu Jicong 已提交
2961
  int32_t        code = buildSampleAstInfoByIndex(pCxt, pStmt, &info);
X
Xiaoyu Wang 已提交
2962
  if (TSDB_CODE_SUCCESS == code) {
2963
    code = buildSampleAst(pCxt, &info, pAst, pLen);
X
Xiaoyu Wang 已提交
2964
  }
2965
  clearSampleAstInfo(&info);
X
Xiaoyu Wang 已提交
2966 2967 2968 2969
  return code;
}

static int32_t buildCreateSmaReq(STranslateContext* pCxt, SCreateIndexStmt* pStmt, SMCreateSmaReq* pReq) {
X
Xiaoyu Wang 已提交
2970 2971
  SName name;
  tNameExtractFullName(toName(pCxt->pParseCxt->acctId, pCxt->pParseCxt->db, pStmt->indexName, &name), pReq->name);
X
Xiaoyu Wang 已提交
2972
  strcpy(name.tname, pStmt->tableName);
X
Xiaoyu Wang 已提交
2973 2974 2975 2976 2977 2978
  name.tname[strlen(pStmt->tableName)] = '\0';
  tNameExtractFullName(&name, pReq->stb);
  pReq->igExists = pStmt->ignoreExists;
  pReq->interval = ((SValueNode*)pStmt->pOptions->pInterval)->datum.i;
  pReq->intervalUnit = ((SValueNode*)pStmt->pOptions->pInterval)->unit;
  pReq->offset = (NULL != pStmt->pOptions->pOffset ? ((SValueNode*)pStmt->pOptions->pOffset)->datum.i : 0);
dengyihao's avatar
dengyihao 已提交
2979 2980 2981 2982
  pReq->sliding =
      (NULL != pStmt->pOptions->pSliding ? ((SValueNode*)pStmt->pOptions->pSliding)->datum.i : pReq->interval);
  pReq->slidingUnit =
      (NULL != pStmt->pOptions->pSliding ? ((SValueNode*)pStmt->pOptions->pSliding)->unit : pReq->intervalUnit);
X
Xiaoyu Wang 已提交
2983 2984 2985 2986 2987 2988 2989 2990 2991

  int32_t code = getSmaIndexDstVgId(pCxt, pStmt->tableName, &pReq->dstVgId);
  if (TSDB_CODE_SUCCESS == code) {
    code = getSmaIndexSql(pCxt, &pReq->sql, &pReq->sqlLen);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = getSmaIndexExpr(pCxt, pStmt, &pReq->expr, &pReq->exprLen);
  }
  if (TSDB_CODE_SUCCESS == code) {
2992
    code = getSmaIndexAst(pCxt, pStmt, &pReq->ast, &pReq->astLen);
X
Xiaoyu Wang 已提交
2993 2994 2995 2996 2997 2998 2999
  }

  return code;
}

static int32_t translateCreateSmaIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) {
  if (DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pInterval) ||
dengyihao's avatar
dengyihao 已提交
3000 3001 3002 3003
      (NULL != pStmt->pOptions->pOffset &&
       DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pOffset)) ||
      (NULL != pStmt->pOptions->pSliding &&
       DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pSliding))) {
X
Xiaoyu Wang 已提交
3004
    return pCxt->errCode;
X
Xiaoyu Wang 已提交
3005 3006
  }

X
Xiaoyu Wang 已提交
3007
  SMCreateSmaReq createSmaReq = {0};
H
Hongze Cheng 已提交
3008
  int32_t        code = buildCreateSmaReq(pCxt, pStmt, &createSmaReq);
3009 3010
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCmdMsg(pCxt, TDMT_MND_CREATE_SMA, (FSerializeFunc)tSerializeSMCreateSmaReq, &createSmaReq);
X
Xiaoyu Wang 已提交
3011
  }
X
Xiaoyu Wang 已提交
3012
  tFreeSMCreateSmaReq(&createSmaReq);
3013
  return code;
X
Xiaoyu Wang 已提交
3014
}
3015

dengyihao's avatar
dengyihao 已提交
3016 3017 3018 3019
static int32_t buildCreateFullTextReq(STranslateContext* pCxt, SCreateIndexStmt* pStmt, SMCreateFullTextReq* pReq) {
  // impl later
  return 0;
}
3020

dengyihao's avatar
dengyihao 已提交
3021 3022
static int32_t translateCreateFullTextIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) {
  SMCreateFullTextReq createFTReq = {0};
H
Hongze Cheng 已提交
3023
  int32_t             code = buildCreateFullTextReq(pCxt, pStmt, &createFTReq);
3024 3025
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCmdMsg(pCxt, TDMT_MND_CREATE_INDEX, (FSerializeFunc)tSerializeSMCreateFullTextReq, &createFTReq);
dengyihao's avatar
dengyihao 已提交
3026 3027
  }
  tFreeSMCreateFullTextReq(&createFTReq);
3028
  return code;
dengyihao's avatar
dengyihao 已提交
3029
}
X
Xiaoyu Wang 已提交
3030 3031 3032 3033

static int32_t translateCreateIndex(STranslateContext* pCxt, SCreateIndexStmt* pStmt) {
  if (INDEX_TYPE_SMA == pStmt->indexType) {
    return translateCreateSmaIndex(pCxt, pStmt);
dengyihao's avatar
dengyihao 已提交
3034
  } else if (INDEX_TYPE_FULLTEXT == pStmt->indexType) {
dengyihao's avatar
dengyihao 已提交
3035
    return translateCreateFullTextIndex(pCxt, pStmt);
X
Xiaoyu Wang 已提交
3036
  }
dengyihao's avatar
dengyihao 已提交
3037
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
3038 3039
}

3040
static int32_t translateDropIndex(STranslateContext* pCxt, SDropIndexStmt* pStmt) {
X
Xiaoyu Wang 已提交
3041 3042
  SEncoder      encoder = {0};
  int32_t       contLen = 0;
3043 3044 3045
  SVDropTSmaReq dropSmaReq = {0};
  strcpy(dropSmaReq.indexName, pStmt->indexName);

wafwerar's avatar
wafwerar 已提交
3046
  pCxt->pCmdMsg = taosMemoryMalloc(sizeof(SCmdMsgInfo));
3047 3048 3049
  if (NULL == pCxt->pCmdMsg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
3050

3051 3052 3053 3054 3055 3056
  int32_t ret = 0;
  tEncodeSize(tEncodeSVDropTSmaReq, &dropSmaReq, contLen, ret);
  if (ret < 0) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

3057 3058
  pCxt->pCmdMsg->epSet = pCxt->pParseCxt->mgmtEpSet;
  pCxt->pCmdMsg->msgType = TDMT_VND_DROP_SMA;
3059
  pCxt->pCmdMsg->msgLen = contLen;
wafwerar's avatar
wafwerar 已提交
3060
  pCxt->pCmdMsg->pMsg = taosMemoryMalloc(pCxt->pCmdMsg->msgLen);
3061 3062 3063 3064
  if (NULL == pCxt->pCmdMsg->pMsg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  void* pBuf = pCxt->pCmdMsg->pMsg;
3065 3066 3067 3068 3069
  if (tEncodeSVDropTSmaReq(&encoder, &dropSmaReq) < 0) {
    tEncoderClear(&encoder);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  tEncoderClear(&encoder);
3070 3071 3072
  return TSDB_CODE_SUCCESS;
}

3073 3074 3075
static int16_t getCreateComponentNodeMsgType(ENodeType type) {
  switch (type) {
    case QUERY_NODE_CREATE_QNODE_STMT:
3076
      return TDMT_MND_CREATE_QNODE;
3077
    case QUERY_NODE_CREATE_BNODE_STMT:
3078
      return TDMT_MND_CREATE_BNODE;
3079
    case QUERY_NODE_CREATE_SNODE_STMT:
3080
      return TDMT_MND_CREATE_SNODE;
3081
    case QUERY_NODE_CREATE_MNODE_STMT:
3082
      return TDMT_MND_CREATE_MNODE;
3083 3084 3085 3086 3087 3088 3089
    default:
      break;
  }
  return -1;
}

static int32_t translateCreateComponentNode(STranslateContext* pCxt, SCreateComponentNodeStmt* pStmt) {
dengyihao's avatar
dengyihao 已提交
3090
  SMCreateQnodeReq createReq = {.dnodeId = pStmt->dnodeId};
H
Hongze Cheng 已提交
3091 3092
  return buildCmdMsg(pCxt, getCreateComponentNodeMsgType(nodeType(pStmt)),
                     (FSerializeFunc)tSerializeSCreateDropMQSBNodeReq, &createReq);
X
Xiaoyu Wang 已提交
3093 3094
}

3095 3096 3097
static int16_t getDropComponentNodeMsgType(ENodeType type) {
  switch (type) {
    case QUERY_NODE_DROP_QNODE_STMT:
3098
      return TDMT_MND_DROP_QNODE;
3099
    case QUERY_NODE_DROP_BNODE_STMT:
3100
      return TDMT_MND_DROP_BNODE;
3101
    case QUERY_NODE_DROP_SNODE_STMT:
3102
      return TDMT_MND_DROP_SNODE;
3103
    case QUERY_NODE_DROP_MNODE_STMT:
3104
      return TDMT_MND_DROP_MNODE;
3105 3106 3107 3108 3109 3110 3111
    default:
      break;
  }
  return -1;
}

static int32_t translateDropComponentNode(STranslateContext* pCxt, SDropComponentNodeStmt* pStmt) {
dengyihao's avatar
dengyihao 已提交
3112
  SDDropQnodeReq dropReq = {.dnodeId = pStmt->dnodeId};
H
Hongze Cheng 已提交
3113 3114
  return buildCmdMsg(pCxt, getDropComponentNodeMsgType(nodeType(pStmt)),
                     (FSerializeFunc)tSerializeSCreateDropMQSBNodeReq, &dropReq);
3115 3116
}

X
Xiaoyu Wang 已提交
3117 3118
static int32_t buildCreateTopicReq(STranslateContext* pCxt, SCreateTopicStmt* pStmt, SCMCreateTopicReq* pReq) {
  SName name;
L
Liu Jicong 已提交
3119 3120
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->topicName, strlen(pStmt->topicName));
  tNameGetFullDbName(&name, pReq->name);
X
Xiaoyu Wang 已提交
3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131
  pReq->igExists = pStmt->ignoreExists;
  pReq->withTbName = pStmt->pOptions->withTable;
  pReq->withSchema = pStmt->pOptions->withSchema;
  pReq->withTag = pStmt->pOptions->withTag;

  pReq->sql = strdup(pCxt->pParseCxt->pSql);
  if (NULL == pReq->sql) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = TSDB_CODE_SUCCESS;
3132

L
Liu Jicong 已提交
3133
  const char* dbName;
3134
  if (NULL != pStmt->pQuery) {
L
Liu Jicong 已提交
3135
    dbName = ((SRealTableNode*)(((SSelectStmt*)pStmt->pQuery)->pFromTable))->table.dbName;
X
Xiaoyu Wang 已提交
3136
    pCxt->pParseCxt->topicQuery = true;
X
Xiaoyu Wang 已提交
3137
    code = translateQuery(pCxt, pStmt->pQuery);
3138
    if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3139
      code = nodesNodeToString(pStmt->pQuery, false, &pReq->ast, NULL);
3140 3141
    }
  } else {
L
Liu Jicong 已提交
3142
    dbName = pStmt->subscribeDbName;
3143
  }
L
Liu Jicong 已提交
3144 3145
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, dbName, strlen(dbName));
  tNameGetFullDbName(&name, pReq->subscribeDbName);
dengyihao's avatar
dengyihao 已提交
3146

X
Xiaoyu Wang 已提交
3147 3148 3149 3150 3151 3152
  return code;
}

static int32_t checkCreateTopic(STranslateContext* pCxt, SCreateTopicStmt* pStmt) {
  if (NULL == pStmt->pQuery) {
    return TSDB_CODE_SUCCESS;
3153 3154
  }

X
Xiaoyu Wang 已提交
3155 3156
  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt->pQuery)) {
    SSelectStmt* pSelect = (SSelectStmt*)pStmt->pQuery;
L
Liu Jicong 已提交
3157 3158 3159
    if (!pSelect->isDistinct && QUERY_NODE_REAL_TABLE == nodeType(pSelect->pFromTable) &&
        NULL == pSelect->pGroupByList && NULL == pSelect->pLimit && NULL == pSelect->pSlimit &&
        NULL == pSelect->pOrderByList && NULL == pSelect->pPartitionByList) {
X
Xiaoyu Wang 已提交
3160 3161 3162 3163 3164 3165 3166 3167 3168
      return TSDB_CODE_SUCCESS;
    }
  }

  return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_TOPIC_QUERY);
}

static int32_t translateCreateTopic(STranslateContext* pCxt, SCreateTopicStmt* pStmt) {
  SCMCreateTopicReq createReq = {0};
L
Liu Jicong 已提交
3169
  int32_t           code = checkCreateTopic(pCxt, pStmt);
X
Xiaoyu Wang 已提交
3170 3171 3172 3173 3174 3175
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCreateTopicReq(pCxt, pStmt, &createReq);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCmdMsg(pCxt, TDMT_MND_CREATE_TOPIC, (FSerializeFunc)tSerializeSCMCreateTopicReq, &createReq);
  }
3176
  tFreeSCMCreateTopicReq(&createReq);
3177
  return code;
3178 3179 3180 3181 3182
}

static int32_t translateDropTopic(STranslateContext* pCxt, SDropTopicStmt* pStmt) {
  SMDropTopicReq dropReq = {0};

X
Xiaoyu Wang 已提交
3183
  SName name;
X
Xiaoyu Wang 已提交
3184 3185
  tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->topicName, strlen(pStmt->topicName));
  tNameGetFullDbName(&name, dropReq.name);
3186 3187
  dropReq.igNotExists = pStmt->ignoreNotExists;

3188
  return buildCmdMsg(pCxt, TDMT_MND_DROP_TOPIC, (FSerializeFunc)tSerializeSMDropTopicReq, &dropReq);
3189 3190
}

3191 3192 3193 3194 3195
static int32_t translateAlterLocal(STranslateContext* pCxt, SAlterLocalStmt* pStmt) {
  // todo
  return TSDB_CODE_SUCCESS;
}

3196 3197 3198 3199 3200 3201 3202
static int32_t translateExplain(STranslateContext* pCxt, SExplainStmt* pStmt) {
  if (pStmt->analyze) {
    pCxt->pExplainOpt = pStmt->pOptions;
  }
  return translateQuery(pCxt, pStmt->pQuery);
}

3203 3204 3205 3206
static int32_t translateDescribe(STranslateContext* pCxt, SDescribeStmt* pStmt) {
  return getTableMeta(pCxt, pStmt->dbName, pStmt->tableName, &pStmt->pMeta);
}

3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218
static int32_t translateKillConnection(STranslateContext* pCxt, SKillStmt* pStmt) {
  SKillConnReq killReq = {0};
  killReq.connId = pStmt->targetId;
  return buildCmdMsg(pCxt, TDMT_MND_KILL_CONN, (FSerializeFunc)tSerializeSKillQueryReq, &killReq);
}

static int32_t translateKillQuery(STranslateContext* pCxt, SKillStmt* pStmt) {
  SKillQueryReq killReq = {0};
  killReq.queryId = pStmt->targetId;
  return buildCmdMsg(pCxt, TDMT_MND_KILL_QUERY, (FSerializeFunc)tSerializeSKillQueryReq, &killReq);
}

3219 3220 3221 3222 3223 3224
static int32_t translateKillTransaction(STranslateContext* pCxt, SKillStmt* pStmt) {
  SKillTransReq killReq = {0};
  killReq.transId = pStmt->targetId;
  return buildCmdMsg(pCxt, TDMT_MND_KILL_TRANS, (FSerializeFunc)tSerializeSKillTransReq, &killReq);
}

X
Xiaoyu Wang 已提交
3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235
static int32_t checkCreateStream(STranslateContext* pCxt, SCreateStreamStmt* pStmt) {
  if (NULL == pStmt->pQuery) {
    return TSDB_CODE_SUCCESS;
  }

  if (QUERY_NODE_SELECT_STMT == nodeType(pStmt->pQuery)) {
    SSelectStmt* pSelect = (SSelectStmt*)pStmt->pQuery;
    if (QUERY_NODE_REAL_TABLE == nodeType(pSelect->pFromTable)) {
      return TSDB_CODE_SUCCESS;
    }
  }
3236

X
Xiaoyu Wang 已提交
3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
  return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_STREAM_QUERY);
}

static void getSourceDatabase(SNode* pStmt, int32_t acctId, char* pDbFName) {
  SName name = {.type = TSDB_DB_NAME_T, .acctId = acctId};
  strcpy(name.dbname, ((SRealTableNode*)(((SSelectStmt*)pStmt)->pFromTable))->table.dbName);
  tNameGetFullDbName(&name, pDbFName);
}

static int32_t buildCreateStreamReq(STranslateContext* pCxt, SCreateStreamStmt* pStmt, SCMCreateStreamReq* pReq) {
  pReq->igExists = pStmt->ignoreExists;
3248

X
Xiaoyu Wang 已提交
3249
  SName name;
X
Xiaoyu Wang 已提交
3250
  tNameExtractFullName(toName(pCxt->pParseCxt->acctId, pCxt->pParseCxt->db, pStmt->streamName, &name), pReq->name);
3251 3252 3253 3254

  if ('\0' != pStmt->targetTabName[0]) {
    strcpy(name.dbname, pStmt->targetDbName);
    strcpy(name.tname, pStmt->targetTabName);
X
Xiaoyu Wang 已提交
3255
    tNameExtractFullName(&name, pReq->targetStbFullName);
3256 3257 3258 3259
  }

  int32_t code = translateQuery(pCxt, pStmt->pQuery);
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3260 3261
    getSourceDatabase(pStmt->pQuery, pCxt->pParseCxt->acctId, pReq->sourceDB);
    code = nodesNodeToString(pStmt->pQuery, false, &pReq->ast, NULL);
3262 3263 3264
  }

  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3265 3266
    pReq->sql = strdup(pCxt->pParseCxt->pSql);
    if (NULL == pReq->sql) {
3267 3268 3269 3270
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

X
Xiaoyu Wang 已提交
3271
  if (TSDB_CODE_SUCCESS == code && NULL != pStmt->pOptions->pWatermark) {
H
Hongze Cheng 已提交
3272 3273
    code = (DEAL_RES_ERROR == translateValue(pCxt, (SValueNode*)pStmt->pOptions->pWatermark)) ? pCxt->errCode
                                                                                              : TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
3274 3275
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3276 3277
    pReq->triggerType = pStmt->pOptions->triggerType;
    pReq->watermark = (NULL != pStmt->pOptions->pWatermark ? ((SValueNode*)pStmt->pOptions->pWatermark)->datum.i : 0);
X
Xiaoyu Wang 已提交
3278 3279
  }

X
Xiaoyu Wang 已提交
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289
  return code;
}

static int32_t translateCreateStream(STranslateContext* pCxt, SCreateStreamStmt* pStmt) {
  SCMCreateStreamReq createReq = {0};

  int32_t code = checkCreateStream(pCxt, pStmt);
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCreateStreamReq(pCxt, pStmt, &createReq);
  }
3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCmdMsg(pCxt, TDMT_MND_CREATE_STREAM, (FSerializeFunc)tSerializeSCMCreateStreamReq, &createReq);
  }

  tFreeSCMCreateStreamReq(&createReq);
  return code;
}

static int32_t translateDropStream(STranslateContext* pCxt, SDropStreamStmt* pStmt) {
  // todo
  return TSDB_CODE_SUCCESS;
}
3302

L
Liu Jicong 已提交
3303
static int32_t readFromFile(char* pName, int32_t* len, char** buf) {
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
  int64_t filesize = 0;
  if (taosStatFile(pName, &filesize, NULL) < 0) {
    return TAOS_SYSTEM_ERROR(errno);
  }

  *len = filesize;

  if (*len <= 0) {
    return TSDB_CODE_TSC_FILE_EMPTY;
  }

  *buf = taosMemoryCalloc(1, *len);
  if (*buf == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  TdFilePtr tfile = taosOpenFile(pName, O_RDONLY | O_BINARY);
  if (NULL == tfile) {
    taosMemoryFreeClear(*buf);
    return TAOS_SYSTEM_ERROR(errno);
  }

  int64_t s = taosReadFile(tfile, *buf, *len);
  if (s != *len) {
    taosCloseFile(&tfile);
    taosMemoryFreeClear(*buf);
    return TSDB_CODE_TSC_APP_ERROR;
  }
  taosCloseFile(&tfile);
  return TSDB_CODE_SUCCESS;
}

static int32_t translateCreateFunction(STranslateContext* pCxt, SCreateFunctionStmt* pStmt) {
3337 3338 3339
  if (fmIsBuiltinFunc(pStmt->funcName)) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_FUNCTION_NAME);
  }
3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354
  SCreateFuncReq req = {0};
  strcpy(req.name, pStmt->funcName);
  req.igExists = pStmt->ignoreExists;
  req.funcType = pStmt->isAgg ? TSDB_FUNC_TYPE_AGGREGATE : TSDB_FUNC_TYPE_SCALAR;
  req.scriptType = TSDB_FUNC_SCRIPT_BIN_LIB;
  req.outputType = pStmt->outputDt.type;
  req.outputLen = pStmt->outputDt.bytes;
  req.bufSize = pStmt->bufSize;
  int32_t code = readFromFile(pStmt->libraryPath, &req.codeLen, &req.pCode);
  if (TSDB_CODE_SUCCESS == code) {
    code = buildCmdMsg(pCxt, TDMT_MND_CREATE_FUNC, (FSerializeFunc)tSerializeSCreateFuncReq, &req);
  }
  return code;
}

S
slzhou 已提交
3355 3356 3357 3358 3359 3360 3361
static int32_t translateDropFunction(STranslateContext* pCxt, SDropFunctionStmt* pStmt) {
  SDropFuncReq req = {0};
  strcpy(req.name, pStmt->funcName);
  req.igNotExists = pStmt->ignoreNotExists;
  return buildCmdMsg(pCxt, TDMT_MND_DROP_FUNC, (FSerializeFunc)tSerializeSDropFuncReq, &req);
}

3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373
static int32_t translateGrant(STranslateContext* pCxt, SGrantStmt* pStmt) {
  SAlterUserReq req = {0};
  if (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_ALL) ||
      (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_READ) &&
       PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_WRITE))) {
    req.alterType = TSDB_ALTER_USER_ADD_ALL_DB;
  } else if (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_READ)) {
    req.alterType = TSDB_ALTER_USER_ADD_READ_DB;
  } else if (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_WRITE)) {
    req.alterType = TSDB_ALTER_USER_ADD_WRITE_DB;
  }
  strcpy(req.user, pStmt->userName);
D
fix bug  
dapan 已提交
3374
  sprintf(req.dbname, "%d.%s", pCxt->pParseCxt->acctId, pStmt->dbName);
3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
  return buildCmdMsg(pCxt, TDMT_MND_ALTER_USER, (FSerializeFunc)tSerializeSAlterUserReq, &req);
}

static int32_t translateRevoke(STranslateContext* pCxt, SRevokeStmt* pStmt) {
  SAlterUserReq req = {0};
  if (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_ALL) ||
      (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_READ) &&
       PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_WRITE))) {
    req.alterType = TSDB_ALTER_USER_REMOVE_ALL_DB;
  } else if (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_READ)) {
    req.alterType = TSDB_ALTER_USER_REMOVE_READ_DB;
  } else if (PRIVILEGE_TYPE_TEST_MASK(pStmt->privileges, PRIVILEGE_TYPE_WRITE)) {
    req.alterType = TSDB_ALTER_USER_REMOVE_WRITE_DB;
  }
  strcpy(req.user, pStmt->userName);
X
Xiaoyu Wang 已提交
3390
  sprintf(req.dbname, "%d.%s", pCxt->pParseCxt->acctId, pStmt->dbName);
3391 3392 3393
  return buildCmdMsg(pCxt, TDMT_MND_ALTER_USER, (FSerializeFunc)tSerializeSAlterUserReq, &req);
}

3394 3395 3396 3397 3398 3399
static int32_t translateQuery(STranslateContext* pCxt, SNode* pNode) {
  int32_t code = TSDB_CODE_SUCCESS;
  switch (nodeType(pNode)) {
    case QUERY_NODE_SELECT_STMT:
      code = translateSelect(pCxt, (SSelectStmt*)pNode);
      break;
3400 3401 3402
    case QUERY_NODE_SET_OPERATOR:
      code = translateSetOperator(pCxt, (SSetOperator*)pNode);
      break;
3403 3404 3405
    case QUERY_NODE_CREATE_DATABASE_STMT:
      code = translateCreateDatabase(pCxt, (SCreateDatabaseStmt*)pNode);
      break;
3406 3407 3408
    case QUERY_NODE_DROP_DATABASE_STMT:
      code = translateDropDatabase(pCxt, (SDropDatabaseStmt*)pNode);
      break;
3409 3410 3411
    case QUERY_NODE_ALTER_DATABASE_STMT:
      code = translateAlterDatabase(pCxt, (SAlterDatabaseStmt*)pNode);
      break;
3412
    case QUERY_NODE_CREATE_TABLE_STMT:
3413
      code = translateCreateSuperTable(pCxt, (SCreateTableStmt*)pNode);
3414
      break;
3415 3416 3417 3418 3419 3420
    case QUERY_NODE_DROP_TABLE_STMT:
      code = translateDropTable(pCxt, (SDropTableStmt*)pNode);
      break;
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:
      code = translateDropSuperTable(pCxt, (SDropSuperTableStmt*)pNode);
      break;
3421 3422 3423
    case QUERY_NODE_ALTER_TABLE_STMT:
      code = translateAlterTable(pCxt, (SAlterTableStmt*)pNode);
      break;
3424 3425 3426 3427 3428 3429 3430 3431 3432
    case QUERY_NODE_CREATE_USER_STMT:
      code = translateCreateUser(pCxt, (SCreateUserStmt*)pNode);
      break;
    case QUERY_NODE_ALTER_USER_STMT:
      code = translateAlterUser(pCxt, (SAlterUserStmt*)pNode);
      break;
    case QUERY_NODE_DROP_USER_STMT:
      code = translateDropUser(pCxt, (SDropUserStmt*)pNode);
      break;
3433 3434 3435
    case QUERY_NODE_USE_DATABASE_STMT:
      code = translateUseDatabase(pCxt, (SUseDatabaseStmt*)pNode);
      break;
3436 3437 3438 3439 3440 3441
    case QUERY_NODE_CREATE_DNODE_STMT:
      code = translateCreateDnode(pCxt, (SCreateDnodeStmt*)pNode);
      break;
    case QUERY_NODE_DROP_DNODE_STMT:
      code = translateDropDnode(pCxt, (SDropDnodeStmt*)pNode);
      break;
3442 3443 3444
    case QUERY_NODE_ALTER_DNODE_STMT:
      code = translateAlterDnode(pCxt, (SAlterDnodeStmt*)pNode);
      break;
3445 3446 3447
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
    case QUERY_NODE_SHOW_QUERIES_STMT:
    case QUERY_NODE_SHOW_TOPICS_STMT:
3448
      code = translateShow(pCxt, (SShowStmt*)pNode);
3449
      break;
X
Xiaoyu Wang 已提交
3450 3451 3452
    case QUERY_NODE_CREATE_INDEX_STMT:
      code = translateCreateIndex(pCxt, (SCreateIndexStmt*)pNode);
      break;
3453 3454 3455
    case QUERY_NODE_DROP_INDEX_STMT:
      code = translateDropIndex(pCxt, (SDropIndexStmt*)pNode);
      break;
X
Xiaoyu Wang 已提交
3456
    case QUERY_NODE_CREATE_QNODE_STMT:
3457 3458 3459 3460
    case QUERY_NODE_CREATE_BNODE_STMT:
    case QUERY_NODE_CREATE_SNODE_STMT:
    case QUERY_NODE_CREATE_MNODE_STMT:
      code = translateCreateComponentNode(pCxt, (SCreateComponentNodeStmt*)pNode);
X
Xiaoyu Wang 已提交
3461
      break;
3462
    case QUERY_NODE_DROP_QNODE_STMT:
3463 3464 3465 3466
    case QUERY_NODE_DROP_BNODE_STMT:
    case QUERY_NODE_DROP_SNODE_STMT:
    case QUERY_NODE_DROP_MNODE_STMT:
      code = translateDropComponentNode(pCxt, (SDropComponentNodeStmt*)pNode);
3467 3468 3469 3470 3471 3472 3473
      break;
    case QUERY_NODE_CREATE_TOPIC_STMT:
      code = translateCreateTopic(pCxt, (SCreateTopicStmt*)pNode);
      break;
    case QUERY_NODE_DROP_TOPIC_STMT:
      code = translateDropTopic(pCxt, (SDropTopicStmt*)pNode);
      break;
3474 3475 3476
    case QUERY_NODE_ALTER_LOCAL_STMT:
      code = translateAlterLocal(pCxt, (SAlterLocalStmt*)pNode);
      break;
3477 3478 3479
    case QUERY_NODE_EXPLAIN_STMT:
      code = translateExplain(pCxt, (SExplainStmt*)pNode);
      break;
3480 3481 3482
    case QUERY_NODE_DESCRIBE_STMT:
      code = translateDescribe(pCxt, (SDescribeStmt*)pNode);
      break;
3483 3484 3485 3486 3487 3488
    case QUERY_NODE_KILL_CONNECTION_STMT:
      code = translateKillConnection(pCxt, (SKillStmt*)pNode);
      break;
    case QUERY_NODE_KILL_QUERY_STMT:
      code = translateKillQuery(pCxt, (SKillStmt*)pNode);
      break;
3489 3490 3491
    case QUERY_NODE_KILL_TRANSACTION_STMT:
      code = translateKillTransaction(pCxt, (SKillStmt*)pNode);
      break;
3492 3493 3494 3495 3496 3497
    case QUERY_NODE_CREATE_STREAM_STMT:
      code = translateCreateStream(pCxt, (SCreateStreamStmt*)pNode);
      break;
    case QUERY_NODE_DROP_STREAM_STMT:
      code = translateDropStream(pCxt, (SDropStreamStmt*)pNode);
      break;
3498 3499 3500
    case QUERY_NODE_CREATE_FUNCTION_STMT:
      code = translateCreateFunction(pCxt, (SCreateFunctionStmt*)pNode);
      break;
S
slzhou 已提交
3501 3502 3503
    case QUERY_NODE_DROP_FUNCTION_STMT:
      code = translateDropFunction(pCxt, (SDropFunctionStmt*)pNode);
      break;
3504 3505 3506 3507 3508 3509
    case QUERY_NODE_GRANT_STMT:
      code = translateGrant(pCxt, (SGrantStmt*)pNode);
      break;
    case QUERY_NODE_REVOKE_STMT:
      code = translateRevoke(pCxt, (SRevokeStmt*)pNode);
      break;
3510 3511 3512 3513 3514 3515 3516 3517
    default:
      break;
  }
  return code;
}

static int32_t translateSubquery(STranslateContext* pCxt, SNode* pNode) {
  ++(pCxt->currLevel);
dengyihao's avatar
dengyihao 已提交
3518
  ESqlClause   currClause = pCxt->currClause;
3519
  SSelectStmt* pCurrStmt = pCxt->pCurrStmt;
dengyihao's avatar
dengyihao 已提交
3520
  int32_t      code = translateQuery(pCxt, pNode);
3521
  --(pCxt->currLevel);
3522
  pCxt->currClause = currClause;
3523
  pCxt->pCurrStmt = pCurrStmt;
3524 3525 3526
  return code;
}

X
Xiaoyu Wang 已提交
3527 3528
static int32_t extractQueryResultSchema(const SNodeList* pProjections, int32_t* numOfCols, SSchema** pSchema) {
  *numOfCols = LIST_LENGTH(pProjections);
3529 3530 3531
  *pSchema = taosMemoryCalloc((*numOfCols), sizeof(SSchema));
  if (NULL == (*pSchema)) {
    return TSDB_CODE_OUT_OF_MEMORY;
3532 3533
  }

dengyihao's avatar
dengyihao 已提交
3534
  SNode*  pNode;
3535
  int32_t index = 0;
X
Xiaoyu Wang 已提交
3536
  FOREACH(pNode, pProjections) {
3537 3538 3539 3540
    SExprNode* pExpr = (SExprNode*)pNode;
    (*pSchema)[index].type = pExpr->resType.type;
    (*pSchema)[index].bytes = pExpr->resType.bytes;
    (*pSchema)[index].colId = index + 1;
3541 3542 3543 3544 3545
    if ('\0' != pExpr->userAlias[0]) {
      strcpy((*pSchema)[index].name, pExpr->userAlias);
    } else {
      strcpy((*pSchema)[index].name, pExpr->aliasName);
    }
dengyihao's avatar
dengyihao 已提交
3546
    index += 1;
3547
  }
3548

3549 3550 3551
  return TSDB_CODE_SUCCESS;
}

H
Hongze Cheng 已提交
3552
static int8_t extractResultTsPrecision(const SSelectStmt* pSelect) { return pSelect->precision; }
3553

3554 3555 3556 3557 3558
static int32_t extractExplainResultSchema(int32_t* numOfCols, SSchema** pSchema) {
  *numOfCols = 1;
  *pSchema = taosMemoryCalloc((*numOfCols), sizeof(SSchema));
  if (NULL == (*pSchema)) {
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
3559
  }
3560 3561
  (*pSchema)[0].type = TSDB_DATA_TYPE_BINARY;
  (*pSchema)[0].bytes = TSDB_EXPLAIN_RESULT_ROW_SIZE;
3562
  strcpy((*pSchema)[0].name, TSDB_EXPLAIN_RESULT_COLUMN_NAME);
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
  return TSDB_CODE_SUCCESS;
}

static int32_t extractDescribeResultSchema(int32_t* numOfCols, SSchema** pSchema) {
  *numOfCols = DESCRIBE_RESULT_COLS;
  *pSchema = taosMemoryCalloc((*numOfCols), sizeof(SSchema));
  if (NULL == (*pSchema)) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  (*pSchema)[0].type = TSDB_DATA_TYPE_BINARY;
  (*pSchema)[0].bytes = DESCRIBE_RESULT_FIELD_LEN;
3575
  strcpy((*pSchema)[0].name, "field");
3576 3577 3578

  (*pSchema)[1].type = TSDB_DATA_TYPE_BINARY;
  (*pSchema)[1].bytes = DESCRIBE_RESULT_TYPE_LEN;
3579
  strcpy((*pSchema)[1].name, "type");
3580 3581 3582

  (*pSchema)[2].type = TSDB_DATA_TYPE_INT;
  (*pSchema)[2].bytes = tDataTypes[TSDB_DATA_TYPE_INT].bytes;
3583
  strcpy((*pSchema)[2].name, "length");
3584 3585 3586

  (*pSchema)[3].type = TSDB_DATA_TYPE_BINARY;
  (*pSchema)[3].bytes = DESCRIBE_RESULT_NOTE_LEN;
3587
  strcpy((*pSchema)[3].name, "note");
3588

X
Xiaoyu Wang 已提交
3589 3590 3591
  return TSDB_CODE_SUCCESS;
}

3592 3593 3594 3595 3596 3597 3598
int32_t extractResultSchema(const SNode* pRoot, int32_t* numOfCols, SSchema** pSchema) {
  if (NULL == pRoot) {
    return TSDB_CODE_SUCCESS;
  }

  switch (nodeType(pRoot)) {
    case QUERY_NODE_SELECT_STMT:
X
Xiaoyu Wang 已提交
3599 3600
    case QUERY_NODE_SET_OPERATOR:
      return extractQueryResultSchema(getProjectList(pRoot), numOfCols, pSchema);
3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
    case QUERY_NODE_EXPLAIN_STMT:
      return extractExplainResultSchema(numOfCols, pSchema);
    case QUERY_NODE_DESCRIBE_STMT:
      return extractDescribeResultSchema(numOfCols, pSchema);
    default:
      break;
  }

  return TSDB_CODE_FAILED;
}

3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628
static const char* getSysDbName(ENodeType type) {
  switch (type) {
    case QUERY_NODE_SHOW_DATABASES_STMT:
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_STABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
    case QUERY_NODE_SHOW_DNODES_STMT:
    case QUERY_NODE_SHOW_VGROUPS_STMT:
    case QUERY_NODE_SHOW_MNODES_STMT:
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
    case QUERY_NODE_SHOW_STREAMS_STMT:
    case QUERY_NODE_SHOW_BNODES_STMT:
    case QUERY_NODE_SHOW_SNODES_STMT:
    case QUERY_NODE_SHOW_LICENCE_STMT:
3629
    case QUERY_NODE_SHOW_CLUSTER_STMT:
3630 3631 3632
      return TSDB_INFORMATION_SCHEMA_DB;
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
    case QUERY_NODE_SHOW_QUERIES_STMT:
X
Xiaoyu Wang 已提交
3633
    case QUERY_NODE_SHOW_TOPICS_STMT:
3634
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
3635 3636 3637 3638 3639 3640 3641
      return TSDB_PERFORMANCE_SCHEMA_DB;
    default:
      break;
  }
  return NULL;
}

X
Xiaoyu Wang 已提交
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
static const char* getSysTableName(ENodeType type) {
  switch (type) {
    case QUERY_NODE_SHOW_DATABASES_STMT:
      return TSDB_INS_TABLE_USER_DATABASES;
    case QUERY_NODE_SHOW_TABLES_STMT:
      return TSDB_INS_TABLE_USER_TABLES;
    case QUERY_NODE_SHOW_STABLES_STMT:
      return TSDB_INS_TABLE_USER_STABLES;
    case QUERY_NODE_SHOW_USERS_STMT:
      return TSDB_INS_TABLE_USER_USERS;
    case QUERY_NODE_SHOW_DNODES_STMT:
      return TSDB_INS_TABLE_DNODES;
    case QUERY_NODE_SHOW_VGROUPS_STMT:
      return TSDB_INS_TABLE_VGROUPS;
    case QUERY_NODE_SHOW_MNODES_STMT:
      return TSDB_INS_TABLE_MNODES;
    case QUERY_NODE_SHOW_MODULES_STMT:
      return TSDB_INS_TABLE_MODULES;
    case QUERY_NODE_SHOW_QNODES_STMT:
      return TSDB_INS_TABLE_QNODES;
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
      return TSDB_INS_TABLE_USER_FUNCTIONS;
    case QUERY_NODE_SHOW_INDEXES_STMT:
      return TSDB_INS_TABLE_USER_INDEXES;
    case QUERY_NODE_SHOW_STREAMS_STMT:
      return TSDB_INS_TABLE_USER_STREAMS;
3668 3669 3670 3671
    case QUERY_NODE_SHOW_BNODES_STMT:
      return TSDB_INS_TABLE_BNODES;
    case QUERY_NODE_SHOW_SNODES_STMT:
      return TSDB_INS_TABLE_SNODES;
3672 3673
    case QUERY_NODE_SHOW_LICENCE_STMT:
      return TSDB_INS_TABLE_LICENCES;
3674 3675
    case QUERY_NODE_SHOW_CLUSTER_STMT:
      return TSDB_INS_TABLE_CLUSTER;
3676
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
3677
      return TSDB_PERFS_TABLE_CONNECTIONS;
3678
    case QUERY_NODE_SHOW_QUERIES_STMT:
3679
      return TSDB_PERFS_TABLE_QUERIES;
X
Xiaoyu Wang 已提交
3680 3681
    case QUERY_NODE_SHOW_TOPICS_STMT:
      return TSDB_PERFS_TABLE_TOPICS;
3682 3683
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
      return TSDB_PERFS_TABLE_TRANS;
X
Xiaoyu Wang 已提交
3684 3685 3686 3687 3688 3689 3690 3691 3692
    default:
      break;
  }
  return NULL;
}

static int32_t createSelectStmtForShow(ENodeType showType, SSelectStmt** pStmt) {
  SSelectStmt* pSelect = nodesMakeNode(QUERY_NODE_SELECT_STMT);
  if (NULL == pSelect) {
X
Xiaoyu Wang 已提交
3693 3694
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
3695
  sprintf(pSelect->stmtName, "%p", pSelect);
X
Xiaoyu Wang 已提交
3696

X
Xiaoyu Wang 已提交
3697 3698
  SRealTableNode* pTable = nodesMakeNode(QUERY_NODE_REAL_TABLE);
  if (NULL == pTable) {
X
Xiaoyu Wang 已提交
3699
    nodesDestroyNode(pSelect);
X
Xiaoyu Wang 已提交
3700 3701
    return TSDB_CODE_OUT_OF_MEMORY;
  }
3702
  strcpy(pTable->table.dbName, getSysDbName(showType));
X
Xiaoyu Wang 已提交
3703
  strcpy(pTable->table.tableName, getSysTableName(showType));
X
Xiaoyu Wang 已提交
3704
  strcpy(pTable->table.tableAlias, pTable->table.tableName);
X
Xiaoyu Wang 已提交
3705 3706 3707
  pSelect->pFromTable = (SNode*)pTable;

  *pStmt = pSelect;
X
Xiaoyu Wang 已提交
3708 3709 3710 3711

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
3712 3713 3714 3715 3716 3717 3718 3719 3720 3721
static int32_t createOperatorNode(EOperatorType opType, const char* pColName, SNode* pRight, SNode** pOp) {
  if (NULL == pRight) {
    return TSDB_CODE_SUCCESS;
  }

  SOperatorNode* pOper = nodesMakeNode(QUERY_NODE_OPERATOR);
  if (NULL == pOper) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
3722
  pOper->opType = opType;
X
Xiaoyu Wang 已提交
3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763
  pOper->pLeft = nodesMakeNode(QUERY_NODE_COLUMN);
  pOper->pRight = nodesCloneNode(pRight);
  if (NULL == pOper->pLeft || NULL == pOper->pRight) {
    nodesDestroyNode(pOper);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  strcpy(((SColumnNode*)pOper->pLeft)->colName, pColName);

  *pOp = (SNode*)pOper;
  return TSDB_CODE_SUCCESS;
}

static const char* getTbNameColName(ENodeType type) {
  return (QUERY_NODE_SHOW_STABLES_STMT == type ? "stable_name" : "table_name");
}

static int32_t createLogicCondNode(SNode* pCond1, SNode* pCond2, SNode** pCond) {
  SLogicConditionNode* pCondition = nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
  if (NULL == pCondition) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pCondition->condType = LOGIC_COND_TYPE_AND;
  pCondition->pParameterList = nodesMakeList();
  if (NULL == pCondition->pParameterList) {
    nodesDestroyNode(pCondition);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  if (TSDB_CODE_SUCCESS != nodesListAppend(pCondition->pParameterList, pCond1) ||
      TSDB_CODE_SUCCESS != nodesListAppend(pCondition->pParameterList, pCond2)) {
    nodesDestroyNode(pCondition);
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  *pCond = (SNode*)pCondition;
  return TSDB_CODE_SUCCESS;
}

static int32_t createShowCondition(const SShowStmt* pShow, SSelectStmt* pSelect) {
  SNode* pDbCond = NULL;
  SNode* pTbCond = NULL;
  if (TSDB_CODE_SUCCESS != createOperatorNode(OP_TYPE_EQUAL, "db_name", pShow->pDbName, &pDbCond) ||
dengyihao's avatar
dengyihao 已提交
3764 3765
      TSDB_CODE_SUCCESS !=
          createOperatorNode(OP_TYPE_LIKE, getTbNameColName(nodeType(pShow)), pShow->pTbNamePattern, &pTbCond)) {
X
Xiaoyu Wang 已提交
3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780
    nodesDestroyNode(pDbCond);
    nodesDestroyNode(pTbCond);
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  if (NULL != pDbCond && NULL != pTbCond) {
    if (TSDB_CODE_SUCCESS != createLogicCondNode(pDbCond, pTbCond, &pSelect->pWhere)) {
      nodesDestroyNode(pDbCond);
      nodesDestroyNode(pTbCond);
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  } else {
    pSelect->pWhere = (NULL == pDbCond ? pTbCond : pDbCond);
  }

X
Xiaoyu Wang 已提交
3781
  if (NULL != pShow->pDbName) {
3782
    strcpy(((SRealTableNode*)pSelect->pFromTable)->qualDbName, ((SValueNode*)pShow->pDbName)->literal);
X
Xiaoyu Wang 已提交
3783 3784
  }

X
Xiaoyu Wang 已提交
3785 3786 3787 3788 3789
  return TSDB_CODE_SUCCESS;
}

static int32_t rewriteShow(STranslateContext* pCxt, SQuery* pQuery) {
  SSelectStmt* pStmt = NULL;
dengyihao's avatar
dengyihao 已提交
3790
  int32_t      code = createSelectStmtForShow(nodeType(pQuery->pRoot), &pStmt);
X
Xiaoyu Wang 已提交
3791 3792 3793 3794
  if (TSDB_CODE_SUCCESS == code) {
    code = createShowCondition((SShowStmt*)pQuery->pRoot, pStmt);
  }
  if (TSDB_CODE_SUCCESS == code) {
D
dapan1121 已提交
3795
    pQuery->showRewrite = true;
X
Xiaoyu Wang 已提交
3796 3797 3798 3799
    nodesDestroyNode(pQuery->pRoot);
    pQuery->pRoot = (SNode*)pStmt;
  }
  return code;
3800 3801
}

X
Xiaoyu Wang 已提交
3802
typedef struct SVgroupCreateTableBatch {
3803 3804
  SVCreateTbBatchReq req;
  SVgroupInfo        info;
D
dapan1121 已提交
3805
  char               dbName[TSDB_DB_NAME_LEN];
X
Xiaoyu Wang 已提交
3806
} SVgroupCreateTableBatch;
3807

X
Xiaoyu Wang 已提交
3808
static void destroyCreateTbReq(SVCreateTbReq* pReq) {
wafwerar's avatar
wafwerar 已提交
3809
  taosMemoryFreeClear(pReq->name);
H
Hongze Cheng 已提交
3810
  taosMemoryFreeClear(pReq->ntb.schema.pSchema);
X
Xiaoyu Wang 已提交
3811 3812
}

dengyihao's avatar
dengyihao 已提交
3813
static int32_t buildNormalTableBatchReq(int32_t acctId, const SCreateTableStmt* pStmt, const SVgroupInfo* pVgroupInfo,
X
Xiaoyu Wang 已提交
3814
                                        SVgroupCreateTableBatch* pBatch) {
dengyihao's avatar
dengyihao 已提交
3815 3816
  char  dbFName[TSDB_DB_FNAME_LEN] = {0};
  SName name = {.type = TSDB_DB_NAME_T, .acctId = acctId};
3817
  strcpy(name.dbname, pStmt->dbName);
X
Xiaoyu Wang 已提交
3818 3819
  tNameGetFullDbName(&name, dbFName);

3820 3821
  SVCreateTbReq req = {0};
  req.type = TD_NORMAL_TABLE;
3822
  req.name = strdup(pStmt->tableName);
H
Hongze Cheng 已提交
3823
  req.ntb.schema.nCols = LIST_LENGTH(pStmt->pCols);
H
Hongze Cheng 已提交
3824
  req.ntb.schema.sver = 1;
H
Hongze Cheng 已提交
3825 3826
  req.ntb.schema.pSchema = taosMemoryCalloc(req.ntb.schema.nCols, sizeof(SSchema));
  if (NULL == req.name || NULL == req.ntb.schema.pSchema) {
X
Xiaoyu Wang 已提交
3827
    destroyCreateTbReq(&req);
3828 3829
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
3830 3831 3832
  if (pStmt->ignoreExists) {
    req.flags |= TD_CREATE_IF_NOT_EXISTS;
  }
dengyihao's avatar
dengyihao 已提交
3833
  SNode*   pCol;
3834 3835
  col_id_t index = 0;
  FOREACH(pCol, pStmt->pCols) {
H
Hongze Cheng 已提交
3836
    toSchema((SColumnDefNode*)pCol, index + 1, req.ntb.schema.pSchema + index);
3837
    ++index;
3838 3839
  }
  pBatch->info = *pVgroupInfo;
3840
  strcpy(pBatch->dbName, pStmt->dbName);
3841
  pBatch->req.pArray = taosArrayInit(1, sizeof(struct SVCreateTbReq));
3842
  if (NULL == pBatch->req.pArray) {
X
Xiaoyu Wang 已提交
3843
    destroyCreateTbReq(&req);
3844
    return TSDB_CODE_OUT_OF_MEMORY;
3845 3846
  }
  taosArrayPush(pBatch->req.pArray, &req);
3847

3848 3849 3850
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
3851
static int32_t serializeVgroupCreateTableBatch(SVgroupCreateTableBatch* pTbBatch, SArray* pBufArray) {
H
Hongze Cheng 已提交
3852 3853
  int      tlen;
  SEncoder coder = {0};
H
Hongze Cheng 已提交
3854

wafwerar's avatar
wafwerar 已提交
3855 3856
  int32_t ret = 0;
  tEncodeSize(tEncodeSVCreateTbBatchReq, &pTbBatch->req, tlen, ret);
X
Xiaoyu Wang 已提交
3857
  tlen += sizeof(SMsgHead);
wafwerar's avatar
wafwerar 已提交
3858
  void* buf = taosMemoryMalloc(tlen);
3859
  if (NULL == buf) {
3860
    return TSDB_CODE_OUT_OF_MEMORY;
3861 3862 3863 3864
  }
  ((SMsgHead*)buf)->vgId = htonl(pTbBatch->info.vgId);
  ((SMsgHead*)buf)->contLen = htonl(tlen);
  void* pBuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
H
Hongze Cheng 已提交
3865

H
Hongze Cheng 已提交
3866
  tEncoderInit(&coder, pBuf, tlen - sizeof(SMsgHead));
H
Hongze Cheng 已提交
3867
  tEncodeSVCreateTbBatchReq(&coder, &pTbBatch->req);
H
Hongze Cheng 已提交
3868
  tEncoderClear(&coder);
3869

wafwerar's avatar
wafwerar 已提交
3870
  SVgDataBlocks* pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
3871 3872 3873
  if (NULL == pVgData) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
dengyihao's avatar
dengyihao 已提交
3874
  pVgData->vg = pTbBatch->info;
3875
  pVgData->pData = buf;
dengyihao's avatar
dengyihao 已提交
3876 3877
  pVgData->size = tlen;
  pVgData->numOfTables = (int32_t)taosArrayGetSize(pTbBatch->req.pArray);
3878
  taosArrayPush(pBufArray, &pVgData);
3879 3880

  return TSDB_CODE_SUCCESS;
3881 3882
}

X
Xiaoyu Wang 已提交
3883
static void destroyCreateTbReqBatch(SVgroupCreateTableBatch* pTbBatch) {
3884
  size_t size = taosArrayGetSize(pTbBatch->req.pArray);
dengyihao's avatar
dengyihao 已提交
3885
  for (int32_t i = 0; i < size; ++i) {
3886
    SVCreateTbReq* pTableReq = taosArrayGet(pTbBatch->req.pArray, i);
wafwerar's avatar
wafwerar 已提交
3887
    taosMemoryFreeClear(pTableReq->name);
3888 3889

    if (pTableReq->type == TSDB_NORMAL_TABLE) {
H
Hongze Cheng 已提交
3890
      taosMemoryFreeClear(pTableReq->ntb.schema.pSchema);
3891
    } else if (pTableReq->type == TSDB_CHILD_TABLE) {
H
Hongze Cheng 已提交
3892
      taosMemoryFreeClear(pTableReq->ctb.pTag);
3893 3894 3895 3896 3897 3898
    }
  }

  taosArrayDestroy(pTbBatch->req.pArray);
}

X
Xiaoyu Wang 已提交
3899
static int32_t rewriteToVnodeModifyOpStmt(SQuery* pQuery, SArray* pBufArray) {
3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910
  SVnodeModifOpStmt* pNewStmt = nodesMakeNode(QUERY_NODE_VNODE_MODIF_STMT);
  if (pNewStmt == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pNewStmt->sqlNodeType = nodeType(pQuery->pRoot);
  pNewStmt->pDataBlocks = pBufArray;
  nodesDestroyNode(pQuery->pRoot);
  pQuery->pRoot = (SNode*)pNewStmt;
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
3911 3912 3913 3914
static void destroyCreateTbReqArray(SArray* pArray) {
  size_t size = taosArrayGetSize(pArray);
  for (size_t i = 0; i < size; ++i) {
    SVgDataBlocks* pVg = taosArrayGetP(pArray, i);
wafwerar's avatar
wafwerar 已提交
3915 3916
    taosMemoryFreeClear(pVg->pData);
    taosMemoryFreeClear(pVg);
X
Xiaoyu Wang 已提交
3917 3918 3919 3920
  }
  taosArrayDestroy(pArray);
}

dengyihao's avatar
dengyihao 已提交
3921 3922
static int32_t buildCreateTableDataBlock(int32_t acctId, const SCreateTableStmt* pStmt, const SVgroupInfo* pInfo,
                                         SArray** pBufArray) {
X
Xiaoyu Wang 已提交
3923 3924 3925 3926 3927
  *pBufArray = taosArrayInit(1, POINTER_BYTES);
  if (NULL == *pBufArray) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
3928 3929
  SVgroupCreateTableBatch tbatch = {0};
  int32_t                 code = buildNormalTableBatchReq(acctId, pStmt, pInfo, &tbatch);
3930
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3931
    code = serializeVgroupCreateTableBatch(&tbatch, *pBufArray);
3932
  }
X
Xiaoyu Wang 已提交
3933

3934
  destroyCreateTbReqBatch(&tbatch);
X
Xiaoyu Wang 已提交
3935
  if (TSDB_CODE_SUCCESS != code) {
X
Xiaoyu Wang 已提交
3936
    destroyCreateTbReqArray(*pBufArray);
X
Xiaoyu Wang 已提交
3937
  }
3938 3939 3940 3941 3942 3943
  return code;
}

static int32_t rewriteCreateTable(STranslateContext* pCxt, SQuery* pQuery) {
  SCreateTableStmt* pStmt = (SCreateTableStmt*)pQuery->pRoot;

dengyihao's avatar
dengyihao 已提交
3944
  int32_t     code = checkCreateTable(pCxt, pStmt);
3945
  SVgroupInfo info = {0};
3946 3947 3948
  if (TSDB_CODE_SUCCESS == code) {
    code = getTableHashVgroup(pCxt, pStmt->dbName, pStmt->tableName, &info);
  }
X
Xiaoyu Wang 已提交
3949
  SArray* pBufArray = NULL;
3950
  if (TSDB_CODE_SUCCESS == code) {
D
dapan1121 已提交
3951
    code = buildCreateTableDataBlock(pCxt->pParseCxt->acctId, pStmt, &info, &pBufArray);
3952 3953
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
3954
    code = rewriteToVnodeModifyOpStmt(pQuery, pBufArray);
X
Xiaoyu Wang 已提交
3955 3956 3957
    if (TSDB_CODE_SUCCESS != code) {
      destroyCreateTbReqArray(pBufArray);
    }
3958 3959 3960 3961 3962
  }

  return code;
}

X
Xiaoyu Wang 已提交
3963 3964
static void addCreateTbReqIntoVgroup(int32_t acctId, SHashObj* pVgroupHashmap, SCreateSubTableClause* pStmt, SKVRow row,
                                     uint64_t suid, SVgroupInfo* pVgInfo) {
dengyihao's avatar
dengyihao 已提交
3965 3966
  char  dbFName[TSDB_DB_FNAME_LEN] = {0};
  SName name = {.type = TSDB_DB_NAME_T, .acctId = acctId};
X
Xiaoyu Wang 已提交
3967
  strcpy(name.dbname, pStmt->dbName);
X
Xiaoyu Wang 已提交
3968 3969
  tNameGetFullDbName(&name, dbFName);

3970
  struct SVCreateTbReq req = {0};
dengyihao's avatar
dengyihao 已提交
3971
  req.type = TD_CHILD_TABLE;
X
Xiaoyu Wang 已提交
3972
  req.name = strdup(pStmt->tableName);
H
Hongze Cheng 已提交
3973 3974
  req.ctb.suid = suid;
  req.ctb.pTag = row;
X
Xiaoyu Wang 已提交
3975 3976 3977
  if (pStmt->ignoreExists) {
    req.flags |= TD_CREATE_IF_NOT_EXISTS;
  }
3978

X
Xiaoyu Wang 已提交
3979
  SVgroupCreateTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId));
3980
  if (pTableBatch == NULL) {
X
Xiaoyu Wang 已提交
3981
    SVgroupCreateTableBatch tBatch = {0};
3982
    tBatch.info = *pVgInfo;
X
Xiaoyu Wang 已提交
3983
    strcpy(tBatch.dbName, pStmt->dbName);
3984 3985 3986 3987 3988 3989 3990 3991 3992 3993

    tBatch.req.pArray = taosArrayInit(4, sizeof(struct SVCreateTbReq));
    taosArrayPush(tBatch.req.pArray, &req);

    taosHashPut(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId), &tBatch, sizeof(tBatch));
  } else {  // add to the correct vgroup
    taosArrayPush(pTableBatch->req.pArray, &req);
  }
}

dengyihao's avatar
dengyihao 已提交
3994 3995
static int32_t addValToKVRow(STranslateContext* pCxt, SValueNode* pVal, const SSchema* pSchema,
                             SKVRowBuilder* pBuilder) {
L
Liu Jicong 已提交
3996 3997
  if (pSchema->type == TSDB_DATA_TYPE_JSON) {
    if (pVal->literal && strlen(pVal->literal) > (TSDB_MAX_JSON_TAG_LEN - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE) {
3998
      return buildSyntaxErrMsg(&pCxt->msgBuf, "json string too long than 4095", pVal->literal);
3999
    }
4000 4001

    return parseJsontoTagData(pVal->literal, pBuilder, &pCxt->msgBuf, pSchema->colId);
4002 4003
  }

L
Liu Jicong 已提交
4004
  if (pVal->node.resType.type == TSDB_DATA_TYPE_NULL) {
4005
    // todo
L
Liu Jicong 已提交
4006
  } else {
X
Xiaoyu Wang 已提交
4007
    tdAddColToKVRow(pBuilder, pSchema->colId, nodesGetValueFromNode(pVal),
L
Liu Jicong 已提交
4008
                    IS_VAR_DATA_TYPE(pSchema->type) ? varDataTLen(pVal->datum.p) : TYPE_BYTES[pSchema->type]);
4009
  }
4010 4011

  return TSDB_CODE_SUCCESS;
4012 4013
}

4014 4015 4016 4017 4018 4019 4020
static int32_t createValueFromFunction(STranslateContext* pCxt, SFunctionNode* pFunc, SValueNode** pVal) {
  if (DEAL_RES_ERROR == translateFunction(pCxt, pFunc)) {
    return pCxt->errCode;
  }
  return scalarCalculateConstants((SNode*)pFunc, (SNode**)pVal);
}

4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031
static SDataType schemaToDataType(SSchema* pSchema) {
  SDataType dt = {.type = pSchema->type, .bytes = pSchema->bytes, .precision = 0, .scale = 0};
  if (TSDB_DATA_TYPE_VARCHAR == dt.type || TSDB_DATA_TYPE_BINARY == dt.type || TSDB_DATA_TYPE_VARBINARY == dt.type) {
    dt.bytes -= VARSTR_HEADER_SIZE;
  } else if (TSDB_DATA_TYPE_NCHAR == dt.type) {
    dt.bytes = (dt.bytes - VARSTR_HEADER_SIZE) / TSDB_NCHAR_SIZE;
  }
  return dt;
}

static int32_t translateTagVal(STranslateContext* pCxt, SSchema* pSchema, SNode* pNode, SValueNode** pVal) {
4032 4033 4034
  if (QUERY_NODE_FUNCTION == nodeType(pNode)) {
    return createValueFromFunction(pCxt, (SFunctionNode*)pNode, pVal);
  } else if (QUERY_NODE_VALUE == nodeType(pNode)) {
4035 4036 4037
    return (DEAL_RES_ERROR == translateValueImpl(pCxt, (SValueNode*)pNode, schemaToDataType(pSchema))
                ? pCxt->errCode
                : TSDB_CODE_SUCCESS);
4038 4039 4040 4041 4042
  } else {
    return TSDB_CODE_FAILED;
  }
}

dengyihao's avatar
dengyihao 已提交
4043 4044
static int32_t buildKVRowForBindTags(STranslateContext* pCxt, SCreateSubTableClause* pStmt, STableMeta* pSuperTableMeta,
                                     SKVRowBuilder* pBuilder) {
4045
  int32_t numOfTags = getNumOfTags(pSuperTableMeta);
dengyihao's avatar
dengyihao 已提交
4046 4047
  if (LIST_LENGTH(pStmt->pValsOfTags) != LIST_LENGTH(pStmt->pSpecificTags) ||
      numOfTags < LIST_LENGTH(pStmt->pValsOfTags)) {
4048
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_TAGS_NOT_MATCHED);
4049
  }
4050

4051
  SSchema* pTagSchema = getTableTagSchema(pSuperTableMeta);
4052 4053
  SNode *  pTag, *pNode;
  FORBOTH(pTag, pStmt->pSpecificTags, pNode, pStmt->pValsOfTags) {
4054
    SColumnNode* pCol = (SColumnNode*)pTag;
dengyihao's avatar
dengyihao 已提交
4055
    SSchema*     pSchema = NULL;
4056 4057
    for (int32_t i = 0; i < numOfTags; ++i) {
      if (0 == strcmp(pCol->colName, pTagSchema[i].name)) {
dengyihao's avatar
dengyihao 已提交
4058 4059
        pSchema = pTagSchema + i;
        break;
4060 4061 4062
      }
    }
    if (NULL == pSchema) {
4063
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_TAG_NAME, pCol->colName);
4064
    }
4065
    SValueNode* pVal = NULL;
4066
    int32_t     code = translateTagVal(pCxt, pSchema, pNode, &pVal);
4067 4068 4069 4070 4071 4072 4073 4074 4075 4076
    if (TSDB_CODE_SUCCESS == code) {
      if (NULL == pVal) {
        pVal = (SValueNode*)pNode;
      } else {
        REPLACE_LIST2_NODE(pVal);
      }
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = addValToKVRow(pCxt, pVal, pSchema, pBuilder);
    }
4077 4078 4079 4080
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
  }
4081

4082 4083
  return TSDB_CODE_SUCCESS;
}
4084

dengyihao's avatar
dengyihao 已提交
4085 4086
static int32_t buildKVRowForAllTags(STranslateContext* pCxt, SCreateSubTableClause* pStmt, STableMeta* pSuperTableMeta,
                                    SKVRowBuilder* pBuilder) {
4087
  if (getNumOfTags(pSuperTableMeta) != LIST_LENGTH(pStmt->pValsOfTags)) {
4088
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_TAGS_NOT_MATCHED);
4089 4090 4091
  }

  SSchema* pTagSchema = getTableTagSchema(pSuperTableMeta);
4092
  SNode*   pNode;
dengyihao's avatar
dengyihao 已提交
4093
  int32_t  index = 0;
4094 4095
  FOREACH(pNode, pStmt->pValsOfTags) {
    SValueNode* pVal = NULL;
4096
    int32_t     code = translateTagVal(pCxt, pTagSchema + index, pNode, &pVal);
4097 4098 4099 4100 4101 4102 4103 4104 4105 4106
    if (TSDB_CODE_SUCCESS == code) {
      if (NULL == pVal) {
        pVal = (SValueNode*)pNode;
      } else {
        REPLACE_NODE(pVal);
      }
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = addValToKVRow(pCxt, pVal, pTagSchema + index++, pBuilder);
    }
4107 4108
    if (TSDB_CODE_SUCCESS != code) {
      return code;
4109
    }
4110
  }
4111

4112 4113 4114
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
4115 4116
static int32_t checkCreateSubTable(STranslateContext* pCxt, SCreateSubTableClause* pStmt) {
  if (0 != strcmp(pStmt->dbName, pStmt->useDbName)) {
dengyihao's avatar
dengyihao 已提交
4117
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_CORRESPONDING_STABLE_ERR);
X
Xiaoyu Wang 已提交
4118 4119 4120
  }
  return TSDB_CODE_SUCCESS;
}
4121
static int32_t rewriteCreateSubTable(STranslateContext* pCxt, SCreateSubTableClause* pStmt, SHashObj* pVgroupHashmap) {
X
Xiaoyu Wang 已提交
4122 4123
  int32_t code = checkCreateSubTable(pCxt, pStmt);

4124
  STableMeta* pSuperTableMeta = NULL;
X
Xiaoyu Wang 已提交
4125 4126 4127
  if (TSDB_CODE_SUCCESS == code) {
    code = getTableMeta(pCxt, pStmt->useDbName, pStmt->useTableName, &pSuperTableMeta);
  }
X
Xiaoyu Wang 已提交
4128

4129 4130 4131 4132 4133 4134 4135
  SKVRowBuilder kvRowBuilder = {0};
  if (TSDB_CODE_SUCCESS == code) {
    code = tdInitKVRowBuilder(&kvRowBuilder);
  }

  if (TSDB_CODE_SUCCESS == code) {
    if (NULL != pStmt->pSpecificTags) {
dengyihao's avatar
dengyihao 已提交
4136
      code = buildKVRowForBindTags(pCxt, pStmt, pSuperTableMeta, &kvRowBuilder);
4137
    } else {
dengyihao's avatar
dengyihao 已提交
4138
      code = buildKVRowForAllTags(pCxt, pStmt, pSuperTableMeta, &kvRowBuilder);
4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149
    }
  }

  SKVRow row = NULL;
  if (TSDB_CODE_SUCCESS == code) {
    row = tdGetKVRowFromBuilder(&kvRowBuilder);
    if (NULL == row) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    } else {
      tdSortKVRowByColIdx(row);
    }
dengyihao's avatar
dengyihao 已提交
4150
  }
4151 4152 4153

  SVgroupInfo info = {0};
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
4154
    code = getTableHashVgroup(pCxt, pStmt->dbName, pStmt->tableName, &info);
4155 4156
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
4157
    addCreateTbReqIntoVgroup(pCxt->pParseCxt->acctId, pVgroupHashmap, pStmt, row, pSuperTableMeta->uid, &info);
4158 4159
  }

wafwerar's avatar
wafwerar 已提交
4160
  taosMemoryFreeClear(pSuperTableMeta);
4161 4162 4163 4164
  tdDestroyKVRowBuilder(&kvRowBuilder);
  return code;
}

X
Xiaoyu Wang 已提交
4165
static SArray* serializeVgroupsCreateTableBatch(int32_t acctId, SHashObj* pVgroupHashmap) {
4166 4167 4168 4169 4170
  SArray* pBufArray = taosArrayInit(taosHashGetSize(pVgroupHashmap), sizeof(void*));
  if (NULL == pBufArray) {
    return NULL;
  }

X
Xiaoyu Wang 已提交
4171 4172
  int32_t                  code = TSDB_CODE_SUCCESS;
  SVgroupCreateTableBatch* pTbBatch = NULL;
4173 4174 4175 4176 4177 4178
  do {
    pTbBatch = taosHashIterate(pVgroupHashmap, pTbBatch);
    if (pTbBatch == NULL) {
      break;
    }

X
Xiaoyu Wang 已提交
4179
    serializeVgroupCreateTableBatch(pTbBatch, pBufArray);
4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194
    destroyCreateTbReqBatch(pTbBatch);
  } while (true);

  return pBufArray;
}

static int32_t rewriteCreateMultiTable(STranslateContext* pCxt, SQuery* pQuery) {
  SCreateMultiTableStmt* pStmt = (SCreateMultiTableStmt*)pQuery->pRoot;

  SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
  if (NULL == pVgroupHashmap) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
4195
  SNode*  pNode;
4196 4197 4198 4199 4200 4201 4202 4203
  FOREACH(pNode, pStmt->pSubTables) {
    code = rewriteCreateSubTable(pCxt, (SCreateSubTableClause*)pNode, pVgroupHashmap);
    if (TSDB_CODE_SUCCESS != code) {
      taosHashCleanup(pVgroupHashmap);
      return code;
    }
  }

X
Xiaoyu Wang 已提交
4204 4205 4206 4207 4208 4209
  SArray* pBufArray = serializeVgroupsCreateTableBatch(pCxt->pParseCxt->acctId, pVgroupHashmap);
  taosHashCleanup(pVgroupHashmap);
  if (NULL == pBufArray) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
4210
  return rewriteToVnodeModifyOpStmt(pQuery, pBufArray);
X
Xiaoyu Wang 已提交
4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243
}

typedef struct SVgroupDropTableBatch {
  SVDropTbBatchReq req;
  SVgroupInfo      info;
  char             dbName[TSDB_DB_NAME_LEN];
} SVgroupDropTableBatch;

static void addDropTbReqIntoVgroup(SHashObj* pVgroupHashmap, SDropTableClause* pClause, SVgroupInfo* pVgInfo) {
  SVDropTbReq            req = {.name = pClause->tableName, .igNotExists = pClause->ignoreNotExists};
  SVgroupDropTableBatch* pTableBatch = taosHashGet(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId));
  if (NULL == pTableBatch) {
    SVgroupDropTableBatch tBatch = {0};
    tBatch.info = *pVgInfo;
    tBatch.req.pArray = taosArrayInit(TARRAY_MIN_SIZE, sizeof(SVDropTbReq));
    taosArrayPush(tBatch.req.pArray, &req);

    taosHashPut(pVgroupHashmap, &pVgInfo->vgId, sizeof(pVgInfo->vgId), &tBatch, sizeof(tBatch));
  } else {  // add to the correct vgroup
    taosArrayPush(pTableBatch->req.pArray, &req);
  }
}

static int32_t buildDropTableVgroupHashmap(STranslateContext* pCxt, SDropTableClause* pClause, bool* pIsSuperTable,
                                           SHashObj* pVgroupHashmap) {
  STableMeta* pTableMeta = NULL;
  int32_t     code = getTableMeta(pCxt, pClause->dbName, pClause->tableName, &pTableMeta);

  if (TSDB_CODE_SUCCESS == code && TSDB_SUPER_TABLE == pTableMeta->tableType) {
    *pIsSuperTable = true;
    goto over;
  }

4244 4245 4246 4247
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code && pClause->ignoreNotExists) {
    code = TSDB_CODE_SUCCESS;
  }

X
Xiaoyu Wang 已提交
4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265
  *pIsSuperTable = false;

  SVgroupInfo info = {0};
  if (TSDB_CODE_SUCCESS == code) {
    code = getTableHashVgroup(pCxt, pClause->dbName, pClause->tableName, &info);
  }
  if (TSDB_CODE_SUCCESS == code) {
    addDropTbReqIntoVgroup(pVgroupHashmap, pClause, &info);
  }

over:
  taosMemoryFreeClear(pTableMeta);
  return code;
}

static void destroyDropTbReqBatch(SVgroupDropTableBatch* pTbBatch) { taosArrayDestroy(pTbBatch->req.pArray); }

static int32_t serializeVgroupDropTableBatch(SVgroupDropTableBatch* pTbBatch, SArray* pBufArray) {
H
Hongze Cheng 已提交
4266 4267
  int      tlen;
  SEncoder coder = {0};
X
Xiaoyu Wang 已提交
4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279

  int32_t ret = 0;
  tEncodeSize(tEncodeSVDropTbBatchReq, &pTbBatch->req, tlen, ret);
  tlen += sizeof(SMsgHead);
  void* buf = taosMemoryMalloc(tlen);
  if (NULL == buf) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  ((SMsgHead*)buf)->vgId = htonl(pTbBatch->info.vgId);
  ((SMsgHead*)buf)->contLen = htonl(tlen);
  void* pBuf = POINTER_SHIFT(buf, sizeof(SMsgHead));

H
Hongze Cheng 已提交
4280
  tEncoderInit(&coder, pBuf, tlen - sizeof(SMsgHead));
X
Xiaoyu Wang 已提交
4281
  tEncodeSVDropTbBatchReq(&coder, &pTbBatch->req);
H
Hongze Cheng 已提交
4282
  tEncoderClear(&coder);
X
Xiaoyu Wang 已提交
4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344

  SVgDataBlocks* pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
  if (NULL == pVgData) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pVgData->vg = pTbBatch->info;
  pVgData->pData = buf;
  pVgData->size = tlen;
  pVgData->numOfTables = (int32_t)taosArrayGetSize(pTbBatch->req.pArray);
  taosArrayPush(pBufArray, &pVgData);

  return TSDB_CODE_SUCCESS;
}

static SArray* serializeVgroupsDropTableBatch(int32_t acctId, SHashObj* pVgroupHashmap) {
  SArray* pBufArray = taosArrayInit(taosHashGetSize(pVgroupHashmap), sizeof(void*));
  if (NULL == pBufArray) {
    return NULL;
  }

  int32_t                code = TSDB_CODE_SUCCESS;
  SVgroupDropTableBatch* pTbBatch = NULL;
  do {
    pTbBatch = taosHashIterate(pVgroupHashmap, pTbBatch);
    if (pTbBatch == NULL) {
      break;
    }

    serializeVgroupDropTableBatch(pTbBatch, pBufArray);
    destroyDropTbReqBatch(pTbBatch);
  } while (true);

  return pBufArray;
}

static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) {
  SDropTableStmt* pStmt = (SDropTableStmt*)pQuery->pRoot;

  SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
  if (NULL == pVgroupHashmap) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  bool   isSuperTable = false;
  SNode* pNode;
  FOREACH(pNode, pStmt->pTables) {
    int32_t code = buildDropTableVgroupHashmap(pCxt, (SDropTableClause*)pNode, &isSuperTable, pVgroupHashmap);
    if (TSDB_CODE_SUCCESS != code) {
      taosHashCleanup(pVgroupHashmap);
      return code;
    }
    if (isSuperTable && LIST_LENGTH(pStmt->pTables) > 1) {
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_DROP_STABLE);
    }
  }

  if (isSuperTable) {
    taosHashCleanup(pVgroupHashmap);
    return TSDB_CODE_SUCCESS;
  }

  SArray* pBufArray = serializeVgroupsDropTableBatch(pCxt->pParseCxt->acctId, pVgroupHashmap);
X
Xiaoyu Wang 已提交
4345
  taosHashCleanup(pVgroupHashmap);
4346 4347 4348 4349
  if (NULL == pBufArray) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
4350 4351 4352
  return rewriteToVnodeModifyOpStmt(pQuery, pBufArray);
}

X
Xiaoyu Wang 已提交
4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481
static SSchema* getColSchema(STableMeta* pTableMeta, const char* pTagName) {
  int32_t numOfFields = getNumOfTags(pTableMeta) + getNumOfColumns(pTableMeta);
  for (int32_t i = 0; i < numOfFields; ++i) {
    SSchema* pTagSchema = pTableMeta->schema + i;
    if (0 == strcmp(pTagName, pTagSchema->name)) {
      return pTagSchema;
    }
  }
  return NULL;
}

static int32_t buildUpdateTagValReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, STableMeta* pTableMeta,
                                    SVAlterTbReq* pReq) {
  SSchema* pSchema = getColSchema(pTableMeta, pStmt->colName);
  if (NULL == pSchema) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
  }

  pReq->tagName = strdup(pStmt->colName);
  if (NULL == pReq->tagName) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  if (DEAL_RES_ERROR == translateValueImpl(pCxt, pStmt->pVal, schemaToDataType(pSchema))) {
    return pCxt->errCode;
  }

  pReq->isNull = (TSDB_DATA_TYPE_NULL == pStmt->pVal->node.resType.type);
  pReq->nTagVal = pStmt->pVal->node.resType.bytes;
  char* pVal = nodesGetValueFromNode(pStmt->pVal);
  pReq->pTagVal = IS_VAR_DATA_TYPE(pStmt->pVal->node.resType.type) ? pVal + VARSTR_HEADER_SIZE : pVal;

  return TSDB_CODE_SUCCESS;
}

static int32_t buildAddColReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, STableMeta* pTableMeta,
                              SVAlterTbReq* pReq) {
  if (NULL != getColSchema(pTableMeta, pStmt->colName)) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DUPLICATED_COLUMN);
  }

  pReq->colName = strdup(pStmt->colName);
  if (NULL == pReq->colName) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pReq->type = pStmt->dataType.type;
  pReq->flags = COL_SMA_ON;
  pReq->bytes = pStmt->dataType.bytes;
  return TSDB_CODE_SUCCESS;
}

static int32_t buildDropColReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, STableMeta* pTableMeta,
                               SVAlterTbReq* pReq) {
  SSchema* pSchema = getColSchema(pTableMeta, pStmt->colName);
  if (NULL == pSchema) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COLUMN, pStmt->colName);
  } else if (PRIMARYKEY_TIMESTAMP_COL_ID == pSchema->colId) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_CANNOT_DROP_PRIMARY_KEY);
  }

  pReq->colName = strdup(pStmt->colName);
  if (NULL == pReq->colName) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t buildUpdateColReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, STableMeta* pTableMeta,
                                 SVAlterTbReq* pReq) {
  pReq->colModBytes = calcTypeBytes(pStmt->dataType);

  SSchema* pSchema = getColSchema(pTableMeta, pStmt->colName);
  if (NULL == pSchema) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COLUMN, pStmt->colName);
  } else if (!IS_VAR_DATA_TYPE(pSchema->type) || pSchema->bytes >= pReq->colModBytes) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_MODIFY_COL);
  }

  pReq->colName = strdup(pStmt->colName);
  if (NULL == pReq->colName) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t buildRenameColReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, STableMeta* pTableMeta,
                                 SVAlterTbReq* pReq) {
  if (NULL == getColSchema(pTableMeta, pStmt->colName)) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_COLUMN, pStmt->colName);
  }
  if (NULL != getColSchema(pTableMeta, pStmt->newColName)) {
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_DUPLICATED_COLUMN);
  }

  pReq->colName = strdup(pStmt->colName);
  pReq->colNewName = strdup(pStmt->newColName);
  if (NULL == pReq->colName || NULL == pReq->colNewName) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t buildUpdateOptionsReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, SVAlterTbReq* pReq) {
  int32_t code = TSDB_CODE_SUCCESS;

  if (-1 != pStmt->pOptions->ttl) {
    code = checkRangeOption(pCxt, "ttl", pStmt->pOptions->ttl, TSDB_MIN_TABLE_TTL, INT32_MAX);
    if (TSDB_CODE_SUCCESS == code) {
      pReq->updateTTL = true;
      pReq->newTTL = pStmt->pOptions->ttl;
    }
  }

  if (TSDB_CODE_SUCCESS == code && '\0' != pStmt->pOptions->comment[0]) {
    pReq->updateComment = true;
    pReq->newComment = strdup(pStmt->pOptions->comment);
    if (NULL == pReq->newComment) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  return code;
}

static int32_t buildAlterTbReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, STableMeta* pTableMeta,
                               SVAlterTbReq* pReq) {
X
Xiaoyu Wang 已提交
4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494
  pReq->tbName = strdup(pStmt->tableName);
  if (NULL == pReq->tbName) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  pReq->action = pStmt->alterType;

  switch (pStmt->alterType) {
    case TSDB_ALTER_TABLE_ADD_TAG:
    case TSDB_ALTER_TABLE_DROP_TAG:
    case TSDB_ALTER_TABLE_UPDATE_TAG_NAME:
    case TSDB_ALTER_TABLE_UPDATE_TAG_BYTES:
      return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
    case TSDB_ALTER_TABLE_UPDATE_TAG_VAL:
X
Xiaoyu Wang 已提交
4495
      return buildUpdateTagValReq(pCxt, pStmt, pTableMeta, pReq);
X
Xiaoyu Wang 已提交
4496
    case TSDB_ALTER_TABLE_ADD_COLUMN:
X
Xiaoyu Wang 已提交
4497
      return buildAddColReq(pCxt, pStmt, pTableMeta, pReq);
X
Xiaoyu Wang 已提交
4498
    case TSDB_ALTER_TABLE_DROP_COLUMN:
X
Xiaoyu Wang 已提交
4499
      return buildDropColReq(pCxt, pStmt, pTableMeta, pReq);
X
Xiaoyu Wang 已提交
4500
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES:
X
Xiaoyu Wang 已提交
4501
      return buildUpdateColReq(pCxt, pStmt, pTableMeta, pReq);
X
Xiaoyu Wang 已提交
4502
    case TSDB_ALTER_TABLE_UPDATE_OPTIONS:
X
Xiaoyu Wang 已提交
4503
      return buildUpdateOptionsReq(pCxt, pStmt, pReq);
X
Xiaoyu Wang 已提交
4504
    case TSDB_ALTER_TABLE_UPDATE_COLUMN_NAME:
X
Xiaoyu Wang 已提交
4505
      return buildRenameColReq(pCxt, pStmt, pTableMeta, pReq);
X
Xiaoyu Wang 已提交
4506 4507 4508 4509
    default:
      break;
  }

X
Xiaoyu Wang 已提交
4510
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564
}

static int32_t serializeAlterTbReq(STranslateContext* pCxt, SAlterTableStmt* pStmt, SVAlterTbReq* pReq,
                                   SArray* pArray) {
  SVgroupInfo vg = {0};
  int32_t     code = getTableHashVgroup(pCxt, pStmt->dbName, pStmt->tableName, &vg);
  int         tlen = 0;
  if (TSDB_CODE_SUCCESS == code) {
    tEncodeSize(tEncodeSVAlterTbReq, pReq, tlen, code);
  }
  if (TSDB_CODE_SUCCESS == code) {
    tlen += sizeof(SMsgHead);
    void* pMsg = taosMemoryMalloc(tlen);
    if (NULL == pMsg) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    ((SMsgHead*)pMsg)->vgId = htonl(vg.vgId);
    ((SMsgHead*)pMsg)->contLen = htonl(tlen);
    void*    pBuf = POINTER_SHIFT(pMsg, sizeof(SMsgHead));
    SEncoder coder = {0};
    tEncoderInit(&coder, pBuf, tlen - sizeof(SMsgHead));
    tEncodeSVAlterTbReq(&coder, pReq);
    tEncoderClear(&coder);

    SVgDataBlocks* pVgData = taosMemoryCalloc(1, sizeof(SVgDataBlocks));
    if (NULL == pVgData) {
      taosMemoryFree(pMsg);
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    pVgData->vg = vg;
    pVgData->pData = pMsg;
    pVgData->size = tlen;
    pVgData->numOfTables = 1;
    taosArrayPush(pArray, &pVgData);
  }

  return code;
}

static int32_t buildModifyVnodeArray(STranslateContext* pCxt, SAlterTableStmt* pStmt, SVAlterTbReq* pReq,
                                     SArray** pArray) {
  SArray* pTmpArray = taosArrayInit(1, sizeof(void*));
  if (NULL == pTmpArray) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = serializeAlterTbReq(pCxt, pStmt, pReq, pTmpArray);
  if (TSDB_CODE_SUCCESS == code) {
    *pArray = pTmpArray;
  } else {
    taosArrayDestroy(pTmpArray);
  }

  return code;
4565 4566
}

4567
static int32_t rewriteAlterTable(STranslateContext* pCxt, SQuery* pQuery) {
X
Xiaoyu Wang 已提交
4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578
  SAlterTableStmt* pStmt = (SAlterTableStmt*)pQuery->pRoot;

  STableMeta* pTableMeta = NULL;
  int32_t     code = getTableMeta(pCxt, pStmt->dbName, pStmt->tableName, &pTableMeta);
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

  if (TSDB_SUPER_TABLE == pTableMeta->tableType) {
    return TSDB_CODE_SUCCESS;
  } else if (TSDB_CHILD_TABLE != pTableMeta->tableType && TSDB_NORMAL_TABLE != pTableMeta->tableType) {
X
Xiaoyu Wang 已提交
4579
    return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_ALTER_TABLE);
X
Xiaoyu Wang 已提交
4580 4581
  }

X
Xiaoyu Wang 已提交
4582
  SVAlterTbReq req = {0};
X
Xiaoyu Wang 已提交
4583
  code = buildAlterTbReq(pCxt, pStmt, pTableMeta, &req);
X
Xiaoyu Wang 已提交
4584 4585 4586 4587 4588 4589 4590 4591 4592 4593

  SArray* pArray = NULL;
  if (TSDB_CODE_SUCCESS == code) {
    code = buildModifyVnodeArray(pCxt, pStmt, &req, &pArray);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteToVnodeModifyOpStmt(pQuery, pArray);
  }

  return code;
4594 4595
}

4596 4597 4598
static int32_t rewriteQuery(STranslateContext* pCxt, SQuery* pQuery) {
  int32_t code = TSDB_CODE_SUCCESS;
  switch (nodeType(pQuery->pRoot)) {
4599
    case QUERY_NODE_SHOW_LICENCE_STMT:
X
Xiaoyu Wang 已提交
4600
    case QUERY_NODE_SHOW_DATABASES_STMT:
X
Xiaoyu Wang 已提交
4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_STABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
    case QUERY_NODE_SHOW_DNODES_STMT:
    case QUERY_NODE_SHOW_VGROUPS_STMT:
    case QUERY_NODE_SHOW_MNODES_STMT:
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
    case QUERY_NODE_SHOW_STREAMS_STMT:
4612 4613
    case QUERY_NODE_SHOW_BNODES_STMT:
    case QUERY_NODE_SHOW_SNODES_STMT:
4614 4615
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
    case QUERY_NODE_SHOW_QUERIES_STMT:
4616
    case QUERY_NODE_SHOW_CLUSTER_STMT:
X
Xiaoyu Wang 已提交
4617
    case QUERY_NODE_SHOW_TOPICS_STMT:
4618
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
X
Xiaoyu Wang 已提交
4619
      code = rewriteShow(pCxt, pQuery);
X
Xiaoyu Wang 已提交
4620
      break;
4621 4622 4623 4624 4625 4626 4627 4628
    case QUERY_NODE_CREATE_TABLE_STMT:
      if (NULL == ((SCreateTableStmt*)pQuery->pRoot)->pTags) {
        code = rewriteCreateTable(pCxt, pQuery);
      }
      break;
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
      code = rewriteCreateMultiTable(pCxt, pQuery);
      break;
X
Xiaoyu Wang 已提交
4629 4630 4631
    case QUERY_NODE_DROP_TABLE_STMT:
      code = rewriteDropTable(pCxt, pQuery);
      break;
4632
    case QUERY_NODE_ALTER_TABLE_STMT:
X
Xiaoyu Wang 已提交
4633
      code = rewriteAlterTable(pCxt, pQuery);
4634
      break;
4635 4636 4637 4638 4639 4640
    default:
      break;
  }
  return code;
}

4641 4642 4643
static int32_t setQuery(STranslateContext* pCxt, SQuery* pQuery) {
  switch (nodeType(pQuery->pRoot)) {
    case QUERY_NODE_SELECT_STMT:
X
Xiaoyu Wang 已提交
4644
    case QUERY_NODE_SET_OPERATOR:
4645
    case QUERY_NODE_EXPLAIN_STMT:
X
Xiaoyu Wang 已提交
4646
      pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
4647 4648 4649 4650
      pQuery->haveResultSet = true;
      pQuery->msgType = TDMT_VND_QUERY;
      break;
    case QUERY_NODE_VNODE_MODIF_STMT:
X
Xiaoyu Wang 已提交
4651
      pQuery->execMode = QUERY_EXEC_MODE_SCHEDULE;
4652 4653
      pQuery->msgType = TDMT_VND_CREATE_TABLE;
      break;
4654
    case QUERY_NODE_DESCRIBE_STMT:
X
Xiaoyu Wang 已提交
4655
      pQuery->execMode = QUERY_EXEC_MODE_LOCAL;
4656 4657 4658
      pQuery->haveResultSet = true;
      break;
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
X
Xiaoyu Wang 已提交
4659
      pQuery->execMode = QUERY_EXEC_MODE_LOCAL;
4660
      break;
4661
    default:
X
Xiaoyu Wang 已提交
4662
      pQuery->execMode = QUERY_EXEC_MODE_RPC;
X
Xiaoyu Wang 已提交
4663
      if (NULL != pCxt->pCmdMsg) {
wafwerar's avatar
wafwerar 已提交
4664
        TSWAP(pQuery->pCmdMsg, pCxt->pCmdMsg);
X
Xiaoyu Wang 已提交
4665 4666
        pQuery->msgType = pQuery->pCmdMsg->msgType;
      }
4667 4668
      break;
  }
4669 4670 4671 4672 4673

  if (pQuery->haveResultSet) {
    if (TSDB_CODE_SUCCESS != extractResultSchema(pQuery->pRoot, &pQuery->numOfResCols, &pQuery->pResSchema)) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
4674

4675 4676 4677
    if (nodeType(pQuery->pRoot) == QUERY_NODE_SELECT_STMT) {
      pQuery->precision = extractResultTsPrecision((SSelectStmt*)pQuery->pRoot);
    }
4678
  }
dengyihao's avatar
dengyihao 已提交
4679

X
Xiaoyu Wang 已提交
4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703
  if (NULL != pCxt->pDbs) {
    pQuery->pDbList = taosArrayInit(taosHashGetSize(pCxt->pDbs), TSDB_DB_FNAME_LEN);
    if (NULL == pQuery->pDbList) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    SFullDatabaseName* pDb = taosHashIterate(pCxt->pDbs, NULL);
    while (NULL != pDb) {
      taosArrayPush(pQuery->pDbList, pDb->fullDbName);
      pDb = taosHashIterate(pCxt->pDbs, pDb);
    }
  }

  if (NULL != pCxt->pTables) {
    pQuery->pTableList = taosArrayInit(taosHashGetSize(pCxt->pTables), sizeof(SName));
    if (NULL == pQuery->pTableList) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    SName* pTable = taosHashIterate(pCxt->pTables, NULL);
    while (NULL != pTable) {
      taosArrayPush(pQuery->pTableList, pTable);
      pTable = taosHashIterate(pCxt->pTables, pTable);
    }
  }

X
bugfix  
Xiaoyu Wang 已提交
4704
  return TSDB_CODE_SUCCESS;
4705 4706
}

X
Xiaoyu Wang 已提交
4707
int32_t translate(SParseContext* pParseCxt, SQuery* pQuery) {
4708
  STranslateContext cxt = {0};
4709 4710

  int32_t code = initTranslateContext(pParseCxt, &cxt);
4711 4712
  if (TSDB_CODE_SUCCESS == code) {
    code = fmFuncMgtInit();
X
Xiaoyu Wang 已提交
4713
  }
4714 4715 4716
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteQuery(&cxt, pQuery);
  }
X
Xiaoyu Wang 已提交
4717 4718
  if (TSDB_CODE_SUCCESS == code) {
    code = translateQuery(&cxt, pQuery->pRoot);
4719
  }
4720
  if (TSDB_CODE_SUCCESS == code) {
4721
    code = setQuery(&cxt, pQuery);
4722
  }
4723
  destroyTranslateContext(&cxt);
X
Xiaoyu Wang 已提交
4724
  return code;
4725
}