planLogicCreater.c 45.7 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 "planInt.h"
17

X
Xiaoyu Wang 已提交
18 19 20
#include "functionMgt.h"

typedef struct SLogicPlanContext {
21
  SPlanContext* pPlanCxt;
22
  SLogicNode*   pCurrRoot;
X
Xiaoyu Wang 已提交
23 24
} SLogicPlanContext;

X
Xiaoyu Wang 已提交
25 26
typedef int32_t (*FCreateLogicNode)(SLogicPlanContext*, void*, SLogicNode**);
typedef int32_t (*FCreateSelectLogicNode)(SLogicPlanContext*, SSelectStmt*, SLogicNode**);
X
Xiaoyu Wang 已提交
27
typedef int32_t (*FCreateSetOpLogicNode)(SLogicPlanContext*, SSetOperator*, SLogicNode**);
X
Xiaoyu Wang 已提交
28
typedef int32_t (*FCreateDeleteLogicNode)(SLogicPlanContext*, SDeleteStmt*, SLogicNode**);
29
typedef int32_t (*FCreateInsertLogicNode)(SLogicPlanContext*, SInsertStmt*, SLogicNode**);
X
Xiaoyu Wang 已提交
30

X
Xiaoyu Wang 已提交
31 32
static int32_t doCreateLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                        SLogicNode** pLogicNode);
33
static int32_t createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt, SLogicNode** pLogicNode);
X
Xiaoyu Wang 已提交
34 35

typedef struct SRewriteExprCxt {
X
Xiaoyu Wang 已提交
36
  int32_t    errCode;
X
Xiaoyu Wang 已提交
37 38 39 40 41 42 43 44 45
  SNodeList* pExprs;
} SRewriteExprCxt;

static EDealRes doRewriteExpr(SNode** pNode, void* pContext) {
  switch (nodeType(*pNode)) {
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION:
    case QUERY_NODE_FUNCTION: {
      SRewriteExprCxt* pCxt = (SRewriteExprCxt*)pContext;
X
Xiaoyu Wang 已提交
46 47
      SNode*           pExpr;
      int32_t          index = 0;
X
Xiaoyu Wang 已提交
48 49 50 51 52 53
      FOREACH(pExpr, pCxt->pExprs) {
        if (QUERY_NODE_GROUPING_SET == nodeType(pExpr)) {
          pExpr = nodesListGetNode(((SGroupingSetNode*)pExpr)->pParameterList, 0);
        }
        if (nodesEqualNode(pExpr, *pNode)) {
          SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
54 55 56
          if (NULL == pCol) {
            return DEAL_RES_ERROR;
          }
X
Xiaoyu Wang 已提交
57 58 59 60
          SExprNode* pToBeRewrittenExpr = (SExprNode*)(*pNode);
          pCol->node.resType = pToBeRewrittenExpr->resType;
          strcpy(pCol->node.aliasName, pToBeRewrittenExpr->aliasName);
          strcpy(pCol->colName, ((SExprNode*)pExpr)->aliasName);
X
Xiaoyu Wang 已提交
61 62 63 64 65 66
          if (QUERY_NODE_FUNCTION == nodeType(pExpr)) {
            if (FUNCTION_TYPE_WSTARTTS == ((SFunctionNode*)pExpr)->funcType) {
              pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
            } else if (FUNCTION_TYPE_TBNAME == ((SFunctionNode*)pExpr)->funcType) {
              pCol->colType = COLUMN_TYPE_TBNAME;
            }
X
Xiaoyu Wang 已提交
67
          }
X
Xiaoyu Wang 已提交
68 69 70 71 72 73 74 75 76 77
          nodesDestroyNode(*pNode);
          *pNode = (SNode*)pCol;
          return DEAL_RES_IGNORE_CHILD;
        }
        ++index;
      }
      break;
    }
    default:
      break;
H
Haojun Liao 已提交
78 79
  }

X
Xiaoyu Wang 已提交
80
  return DEAL_RES_CONTINUE;
X
Xiaoyu Wang 已提交
81 82
}

X
Xiaoyu Wang 已提交
83 84 85 86 87
static EDealRes doNameExpr(SNode* pNode, void* pContext) {
  switch (nodeType(pNode)) {
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION:
    case QUERY_NODE_FUNCTION: {
88 89 90
      if ('\0' == ((SExprNode*)pNode)->aliasName[0]) {
        sprintf(((SExprNode*)pNode)->aliasName, "#expr_%p", pNode);
      }
X
Xiaoyu Wang 已提交
91
      return DEAL_RES_IGNORE_CHILD;
X
Xiaoyu Wang 已提交
92 93
    }
    default:
X
Xiaoyu Wang 已提交
94
      break;
X
Xiaoyu Wang 已提交
95
  }
96

X
Xiaoyu Wang 已提交
97
  return DEAL_RES_CONTINUE;
98 99
}

X
Xiaoyu Wang 已提交
100
static int32_t rewriteExprsForSelect(SNodeList* pExprs, SSelectStmt* pSelect, ESqlClause clause) {
X
Xiaoyu Wang 已提交
101
  nodesWalkExprs(pExprs, doNameExpr, NULL);
X
Xiaoyu Wang 已提交
102
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs};
X
Xiaoyu Wang 已提交
103 104
  nodesRewriteSelectStmt(pSelect, clause, doRewriteExpr, &cxt);
  return cxt.errCode;
105 106
}

X
Xiaoyu Wang 已提交
107 108 109 110 111 112 113
static int32_t rewriteExpr(SNodeList* pExprs, SNode** pTarget) {
  nodesWalkExprs(pExprs, doNameExpr, NULL);
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs};
  nodesRewriteExpr(pTarget, doRewriteExpr, &cxt);
  return cxt.errCode;
}

X
Xiaoyu Wang 已提交
114
static int32_t rewriteExprs(SNodeList* pExprs, SNodeList* pTarget) {
X
Xiaoyu Wang 已提交
115
  nodesWalkExprs(pExprs, doNameExpr, NULL);
X
Xiaoyu Wang 已提交
116
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs};
X
Xiaoyu Wang 已提交
117 118 119 120
  nodesRewriteExprs(pTarget, doRewriteExpr, &cxt);
  return cxt.errCode;
}

121 122 123 124 125
static int32_t pushLogicNode(SLogicPlanContext* pCxt, SLogicNode** pOldRoot, SLogicNode* pNewRoot) {
  if (NULL == pNewRoot->pChildren) {
    pNewRoot->pChildren = nodesMakeList();
    if (NULL == pNewRoot->pChildren) {
      return TSDB_CODE_OUT_OF_MEMORY;
H
Haojun Liao 已提交
126
    }
127
  }
128 129
  if (TSDB_CODE_SUCCESS != nodesListAppend(pNewRoot->pChildren, (SNode*)*pOldRoot)) {
    return TSDB_CODE_OUT_OF_MEMORY;
130 131
  }

132 133
  (*pOldRoot)->pParent = pNewRoot;
  *pOldRoot = pNewRoot;
134

135 136
  return TSDB_CODE_SUCCESS;
}
137

X
Xiaoyu Wang 已提交
138 139
static int32_t createRootLogicNode(SLogicPlanContext* pCxt, void* pStmt, uint8_t precision, FCreateLogicNode func,
                                   SLogicNode** pRoot) {
X
Xiaoyu Wang 已提交
140
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
141
  int32_t     code = func(pCxt, pStmt, &pNode);
X
Xiaoyu Wang 已提交
142
  if (TSDB_CODE_SUCCESS == code && NULL != pNode) {
X
Xiaoyu Wang 已提交
143
    pNode->precision = precision;
X
Xiaoyu Wang 已提交
144
    code = pushLogicNode(pCxt, pRoot, pNode);
145
    pCxt->pCurrRoot = pNode;
146
  }
X
Xiaoyu Wang 已提交
147
  if (TSDB_CODE_SUCCESS != code) {
148
    nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
149
  }
X
Xiaoyu Wang 已提交
150
  return code;
151 152
}

X
Xiaoyu Wang 已提交
153 154 155 156
static int32_t createSelectRootLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, FCreateSelectLogicNode func,
                                         SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSelect, pSelect->precision, (FCreateLogicNode)func, pRoot);
}
157

158 159
static EScanType getScanType(SLogicPlanContext* pCxt, SNodeList* pScanPseudoCols, SNodeList* pScanCols,
                             int8_t tableType) {
X
Xiaoyu Wang 已提交
160 161 162 163
  if (pCxt->pPlanCxt->topicQuery || pCxt->pPlanCxt->streamQuery) {
    return SCAN_TYPE_STREAM;
  }

X
Xiaoyu Wang 已提交
164 165
  if (NULL == pScanCols) {
    // select count(*) from t
166 167 168 169 170
    return NULL == pScanPseudoCols
               ? SCAN_TYPE_TABLE
               : ((FUNCTION_TYPE_BLOCK_DIST_INFO == ((SFunctionNode*)nodesListGetNode(pScanPseudoCols, 0))->funcType)
                      ? SCAN_TYPE_BLOCK_INFO
                      : SCAN_TYPE_TAG);
X
Xiaoyu Wang 已提交
171 172
  }

X
Xiaoyu Wang 已提交
173
  if (TSDB_SYSTEM_TABLE == tableType) {
X
Xiaoyu Wang 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186
    return SCAN_TYPE_SYSTEM_TABLE;
  }

  SNode* pCol = NULL;
  FOREACH(pCol, pScanCols) {
    if (COLUMN_TYPE_COLUMN == ((SColumnNode*)pCol)->colType) {
      return SCAN_TYPE_TABLE;
    }
  }

  return SCAN_TYPE_TAG;
}

187 188
static SNode* createPrimaryKeyCol(uint64_t tableId) {
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
189 190 191 192 193 194 195 196 197
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
  pCol->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
  pCol->tableId = tableId;
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  pCol->colType = COLUMN_TYPE_COLUMN;
  strcpy(pCol->colName, "#primarykey");
198
  return (SNode*)pCol;
199 200 201 202 203 204 205 206 207 208
}

static int32_t addPrimaryKeyCol(uint64_t tableId, SNodeList** pCols) {
  if (NULL == *pCols) {
    *pCols = nodesMakeList();
    if (NULL == *pCols) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }

X
Xiaoyu Wang 已提交
209
  bool   found = false;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  SNode* pCol = NULL;
  FOREACH(pCol, *pCols) {
    if (PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pCol)->colId) {
      found = true;
      break;
    }
  }

  if (!found) {
    if (TSDB_CODE_SUCCESS != nodesListStrictAppend(*pCols, createPrimaryKeyCol(tableId))) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
226 227
static int32_t makeScanLogicNode(SLogicPlanContext* pCxt, SRealTableNode* pRealTable, bool hasRepeatScanFuncs,
                                 SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
228
  SScanLogicNode* pScan = (SScanLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SCAN);
X
Xiaoyu Wang 已提交
229
  if (NULL == pScan) {
230
    return TSDB_CODE_OUT_OF_MEMORY;
231 232
  }

wafwerar's avatar
wafwerar 已提交
233
  TSWAP(pScan->pVgroupList, pRealTable->pVgroupList);
X
Xiaoyu Wang 已提交
234
  TSWAP(pScan->pSmaIndexes, pRealTable->pSmaIndexes);
X
Xiaoyu Wang 已提交
235
  pScan->tableId = pRealTable->pMeta->uid;
X
Xiaoyu Wang 已提交
236
  pScan->stableId = pRealTable->pMeta->suid;
X
Xiaoyu Wang 已提交
237 238
  pScan->tableType = pRealTable->pMeta->tableType;
  pScan->scanSeq[0] = hasRepeatScanFuncs ? 2 : 1;
X
Xiaoyu Wang 已提交
239
  pScan->scanSeq[1] = 0;
X
Xiaoyu Wang 已提交
240
  pScan->scanRange = TSWINDOW_INITIALIZER;
X
Xiaoyu Wang 已提交
241 242 243 244
  pScan->tableName.type = TSDB_TABLE_NAME_T;
  pScan->tableName.acctId = pCxt->pPlanCxt->acctId;
  strcpy(pScan->tableName.dbname, pRealTable->table.dbName);
  strcpy(pScan->tableName.tname, pRealTable->table.tableName);
D
dapan1121 已提交
245
  pScan->showRewrite = pCxt->pPlanCxt->showRewrite;
246
  pScan->ratio = pRealTable->ratio;
247
  pScan->dataRequired = FUNC_DATA_REQUIRED_DATA_LOAD;
248

X
Xiaoyu Wang 已提交
249 250 251 252 253 254 255 256 257 258
  *pLogicNode = (SLogicNode*)pScan;

  return TSDB_CODE_SUCCESS;
}

static int32_t createScanLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SRealTableNode* pRealTable,
                                   SLogicNode** pLogicNode) {
  SScanLogicNode* pScan = NULL;
  int32_t         code = makeScanLogicNode(pCxt, pRealTable, pSelect->hasRepeatScanFuncs, (SLogicNode**)&pScan);

X
Xiaoyu Wang 已提交
259
  // set columns to scan
X
Xiaoyu Wang 已提交
260 261 262 263
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, COLLECT_COL_TYPE_COL,
                               &pScan->pScanCols);
  }
264

265
  if (TSDB_CODE_SUCCESS == code) {
266 267
    code = nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, COLLECT_COL_TYPE_TAG,
                               &pScan->pScanPseudoCols);
268 269 270
  }

  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
271
    code = nodesCollectFuncs(pSelect, SQL_CLAUSE_FROM, fmIsScanPseudoColumnFunc, &pScan->pScanPseudoCols);
272 273
  }

274 275
  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
276
    code = rewriteExprsForSelect(pScan->pScanPseudoCols, pSelect, SQL_CLAUSE_FROM);
277 278
  }

279
  pScan->scanType = getScanType(pCxt, pScan->pScanPseudoCols, pScan->pScanCols, pScan->tableType);
X
Xiaoyu Wang 已提交
280

281
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
282
    code = addPrimaryKeyCol(pScan->tableId, &pScan->pScanCols);
283 284
  }

285 286
  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
287
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
288 289
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
290
    code = createColumnByRewriteExprs(pScan->pScanPseudoCols, &pScan->node.pTargets);
291
  }
292

X
Xiaoyu Wang 已提交
293 294 295
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
296
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
297
  }
298

X
Xiaoyu Wang 已提交
299
  return code;
X
Xiaoyu Wang 已提交
300
}
301

X
Xiaoyu Wang 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
static int32_t createColumnByLastRow(SNodeList* pFuncs, SNodeList** pOutput) {
  int32_t    code = TSDB_CODE_SUCCESS;
  SNodeList* pCols = NULL;
  SNode*     pFunc = NULL;
  FOREACH(pFunc, pFuncs) {
    SFunctionNode* pLastRow = (SFunctionNode*)pFunc;
    SColumnNode*   pCol = (SColumnNode*)nodesListGetNode(pLastRow->pParameterList, 0);
    snprintf(pCol->colName, sizeof(pCol->colName), "%s", pLastRow->node.aliasName);
    snprintf(pCol->node.aliasName, sizeof(pCol->colName), "%s", pLastRow->node.aliasName);
    NODES_CLEAR_LIST(pLastRow->pParameterList);
    code = nodesListMakeStrictAppend(&pCols, (SNode*)pCol);
    if (TSDB_CODE_SUCCESS != code) {
      break;
    }
  }
  if (TSDB_CODE_SUCCESS == code) {
    *pOutput = pCols;
  } else {
    nodesDestroyList(pCols);
  }
  return code;
}

X
Xiaoyu Wang 已提交
325 326
static int32_t createSubqueryLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, STempTableNode* pTable,
                                       SLogicNode** pLogicNode) {
327
  return createQueryLogicNode(pCxt, pTable->pSubquery, pLogicNode);
328 329
}

X
Xiaoyu Wang 已提交
330 331
static int32_t createJoinLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SJoinTableNode* pJoinTable,
                                   SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
332
  SJoinLogicNode* pJoin = (SJoinLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_JOIN);
X
Xiaoyu Wang 已提交
333
  if (NULL == pJoin) {
334 335
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
336 337

  pJoin->joinType = pJoinTable->joinType;
X
Xiaoyu Wang 已提交
338
  pJoin->isSingleTableJoin = pJoinTable->table.singleTable;
X
Xiaoyu Wang 已提交
339

340
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
341 342 343

  // set left and right node
  pJoin->node.pChildren = nodesMakeList();
X
Xiaoyu Wang 已提交
344
  if (NULL == pJoin->node.pChildren) {
345
    code = TSDB_CODE_OUT_OF_MEMORY;
346 347
  }

X
Xiaoyu Wang 已提交
348
  SLogicNode* pLeft = NULL;
349
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
350 351 352 353
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pLeft, &pLeft);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pLeft);
    }
354
  }
355

X
Xiaoyu Wang 已提交
356
  SLogicNode* pRight = NULL;
357
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
358 359 360 361 362
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pRight, &pRight);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pRight);
    }
  }
X
Xiaoyu Wang 已提交
363 364

  // set on conditions
X
Xiaoyu Wang 已提交
365
  if (TSDB_CODE_SUCCESS == code && NULL != pJoinTable->pOnCond) {
X
Xiaoyu Wang 已提交
366
    pJoin->pOnConditions = nodesCloneNode(pJoinTable->pOnCond);
X
Xiaoyu Wang 已提交
367 368 369
    if (NULL == pJoin->pOnConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
370 371
  }

X
Xiaoyu Wang 已提交
372
  // set the output
X
Xiaoyu Wang 已提交
373 374
  if (TSDB_CODE_SUCCESS == code) {
    pJoin->node.pTargets = nodesCloneList(pLeft->pTargets);
D
dapan1121 已提交
375
    if (NULL == pJoin->node.pTargets) {
X
Xiaoyu Wang 已提交
376 377 378 379 380 381 382 383 384
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppendList(pJoin->node.pTargets, nodesCloneList(pRight->pTargets));
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pJoin;
385
  } else {
386
    nodesDestroyNode((SNode*)pJoin);
387 388
  }

389
  return code;
X
Xiaoyu Wang 已提交
390
}
391

X
Xiaoyu Wang 已提交
392 393
static int32_t doCreateLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                        SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
394 395
  switch (nodeType(pTable)) {
    case QUERY_NODE_REAL_TABLE:
396
      return createScanLogicNode(pCxt, pSelect, (SRealTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
397
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
398
      return createSubqueryLogicNode(pCxt, pSelect, (STempTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
399
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
400
      return createJoinLogicNode(pCxt, pSelect, (SJoinTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
401 402
    default:
      break;
403
  }
X
Xiaoyu Wang 已提交
404 405 406
  return TSDB_CODE_FAILED;
}

X
Xiaoyu Wang 已提交
407 408
static int32_t createLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                      SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
409
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
410
  int32_t     code = doCreateLogicNodeByTable(pCxt, pSelect, pTable, &pNode);
X
Xiaoyu Wang 已提交
411 412 413
  if (TSDB_CODE_SUCCESS == code) {
    pNode->pConditions = nodesCloneNode(pSelect->pWhere);
    if (NULL != pSelect->pWhere && NULL == pNode->pConditions) {
414
      nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
415 416
      return TSDB_CODE_OUT_OF_MEMORY;
    }
417
    pNode->precision = pSelect->precision;
X
Xiaoyu Wang 已提交
418
    *pLogicNode = pNode;
419
    pCxt->pCurrRoot = pNode;
X
Xiaoyu Wang 已提交
420 421 422 423
  }
  return code;
}

X
Xiaoyu Wang 已提交
424
static SColumnNode* createColumnByExpr(const char* pStmtName, SExprNode* pExpr) {
425
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
426 427 428 429 430
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType = pExpr->resType;
  strcpy(pCol->colName, pExpr->aliasName);
X
Xiaoyu Wang 已提交
431 432 433
  if (NULL != pStmtName) {
    strcpy(pCol->tableAlias, pStmtName);
  }
X
Xiaoyu Wang 已提交
434
  return pCol;
435 436
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
static SNode* createGroupingSetNode(SNode* pExpr) {
  SGroupingSetNode* pGroupingSet = (SGroupingSetNode*)nodesMakeNode(QUERY_NODE_GROUPING_SET);
  if (NULL == pGroupingSet) {
    return NULL;
  }
  pGroupingSet->groupingSetType = GP_TYPE_NORMAL;
  if (TSDB_CODE_SUCCESS != nodesListMakeStrictAppend(&pGroupingSet->pParameterList, nodesCloneNode(pExpr))) {
    nodesDestroyNode((SNode*)pGroupingSet);
    return NULL;
  }
  return (SNode*)pGroupingSet;
}

static int32_t createGroupKeysFromPartKeys(SNodeList* pPartKeys, SNodeList** pOutput) {
  SNodeList* pGroupKeys = NULL;
  SNode*     pPartKey = NULL;
  FOREACH(pPartKey, pPartKeys) {
    int32_t code = nodesListMakeStrictAppend(&pGroupKeys, createGroupingSetNode(pPartKey));
    if (TSDB_CODE_SUCCESS != code) {
      nodesDestroyList(pGroupKeys);
      return code;
    }
  }
  *pOutput = pGroupKeys;
  return TSDB_CODE_SUCCESS;
}

464
static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
465
  if (!pSelect->hasAggFuncs && NULL == pSelect->pGroupByList) {
466
    return TSDB_CODE_SUCCESS;
467 468
  }

X
Xiaoyu Wang 已提交
469
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
470 471 472
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
473

474 475
  pAgg->hasLastRow = pSelect->hasLastRowFunc;

X
Xiaoyu Wang 已提交
476 477
  int32_t code = TSDB_CODE_SUCCESS;

X
Xiaoyu Wang 已提交
478
  // set grouyp keys, agg funcs and having conditions
479
  if (TSDB_CODE_SUCCESS == code) {
480 481 482
    code = nodesCollectFuncs(pSelect, SQL_CLAUSE_GROUP_BY, fmIsAggFunc, &pAgg->pAggFuncs);
  }

X
Xiaoyu Wang 已提交
483
  // rewrite the expression in subsequent clauses
484
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
485 486 487 488
    code = rewriteExprsForSelect(pAgg->pAggFuncs, pSelect, SQL_CLAUSE_GROUP_BY);
  }

  if (NULL != pSelect->pGroupByList) {
489 490 491 492 493 494 495
    if (NULL != pAgg->pGroupKeys) {
      code = nodesListStrictAppendList(pAgg->pGroupKeys, nodesCloneList(pSelect->pGroupByList));
    } else {
      pAgg->pGroupKeys = nodesCloneList(pSelect->pGroupByList);
      if (NULL == pAgg->pGroupKeys) {
        code = TSDB_CODE_OUT_OF_MEMORY;
      }
496
    }
497
  }
X
Xiaoyu Wang 已提交
498 499

  // rewrite the expression in subsequent clauses
500
  if (TSDB_CODE_SUCCESS == code) {
501
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_GROUP_BY);
502
  }
503

504
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pHaving) {
X
Xiaoyu Wang 已提交
505
    pAgg->node.pConditions = nodesCloneNode(pSelect->pHaving);
506 507 508
    if (NULL == pAgg->node.pConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
509
  }
510

X
Xiaoyu Wang 已提交
511
  // set the output
512
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pGroupKeys) {
X
Xiaoyu Wang 已提交
513
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
514 515
  }
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
X
Xiaoyu Wang 已提交
516
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
517 518 519 520 521
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
522
    nodesDestroyNode((SNode*)pAgg);
523 524 525
  }

  return code;
X
Xiaoyu Wang 已提交
526
}
527

528 529
static int32_t createIndefRowsFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  // top/bottom are both an aggregate function and a indefinite rows function
530
  if (!pSelect->hasIndefiniteRowsFunc || pSelect->hasAggFuncs || NULL != pSelect->pWindow) {
531 532 533 534 535 536 537 538 539
    return TSDB_CODE_SUCCESS;
  }

  SIndefRowsFuncLogicNode* pIdfRowsFunc =
      (SIndefRowsFuncLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC);
  if (NULL == pIdfRowsFunc) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

540
  pIdfRowsFunc->isTailFunc = pSelect->hasTailFunc;
541
  pIdfRowsFunc->isUniqueFunc = pSelect->hasUniqueFunc;
542
  pIdfRowsFunc->isTimeLineFunc = pSelect->hasTimeLineFunc;
543

X
Xiaoyu Wang 已提交
544 545
  // indefinite rows functions and _select_values functions
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, fmIsVectorFunc, &pIdfRowsFunc->pFuncs);
546
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
547
    code = rewriteExprsForSelect(pIdfRowsFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT);
548 549 550 551
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
552
    code = createColumnByRewriteExprs(pIdfRowsFunc->pFuncs, &pIdfRowsFunc->node.pTargets);
553 554 555 556 557
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pIdfRowsFunc;
  } else {
558
    nodesDestroyNode((SNode*)pIdfRowsFunc);
559 560 561 562 563
  }

  return code;
}

X
Xiaoyu Wang 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
static int32_t createInterpFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (!pSelect->hasInterpFunc) {
    return TSDB_CODE_SUCCESS;
  }

  SInterpFuncLogicNode* pInterpFunc = (SInterpFuncLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_INTERP_FUNC);
  if (NULL == pInterpFunc) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, fmIsInterpFunc, &pInterpFunc->pFuncs);
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExprsForSelect(pInterpFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT);
  }

X
Xiaoyu Wang 已提交
579 580 581 582 583 584 585 586 587
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pFill) {
    SFillNode* pFill = (SFillNode*)pSelect->pFill;
    pInterpFunc->timeRange = pFill->timeRange;
    pInterpFunc->fillMode = pFill->mode;
    pInterpFunc->pTimeSeries = nodesCloneNode(pFill->pWStartTs);
    pInterpFunc->pFillValues = nodesCloneNode(pFill->pValues);
    if (NULL == pInterpFunc->pTimeSeries || (NULL != pFill->pValues && NULL == pInterpFunc->pFillValues)) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  }

  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pEvery) {
    pInterpFunc->interval = ((SValueNode*)pSelect->pEvery)->datum.i;
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
    code = createColumnByRewriteExprs(pInterpFunc->pFuncs, &pInterpFunc->node.pTargets);
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pInterpFunc;
  } else {
    nodesDestroyNode((SNode*)pInterpFunc);
  }

  return code;
}

X
Xiaoyu Wang 已提交
608 609
static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SWindowLogicNode* pWindow,
                                             SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
610
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_WINDOW, fmIsWindowClauseFunc, &pWindow->pFuncs);
X
Xiaoyu Wang 已提交
611

X
Xiaoyu Wang 已提交
612 613 614
  if (pCxt->pPlanCxt->streamQuery) {
    pWindow->triggerType = pCxt->pPlanCxt->triggerType;
    pWindow->watermark = pCxt->pPlanCxt->watermark;
615
    pWindow->igExpired = pCxt->pPlanCxt->igExpired;
5
54liuyao 已提交
616 617
  }

X
Xiaoyu Wang 已提交
618
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
619
    code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW);
X
Xiaoyu Wang 已提交
620 621 622
  }

  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
623
    code = createColumnByRewriteExprs(pWindow->pFuncs, &pWindow->node.pTargets);
X
Xiaoyu Wang 已提交
624 625
  }

X
Xiaoyu Wang 已提交
626 627
  pSelect->hasAggFuncs = false;

X
Xiaoyu Wang 已提交
628 629 630
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pWindow;
  } else {
631
    nodesDestroyNode((SNode*)pWindow);
X
Xiaoyu Wang 已提交
632 633 634 635 636
  }

  return code;
}

X
Xiaoyu Wang 已提交
637 638
static int32_t createWindowLogicNodeByState(SLogicPlanContext* pCxt, SStateWindowNode* pState, SSelectStmt* pSelect,
                                            SLogicNode** pLogicNode) {
639
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
640 641 642 643 644 645 646
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_STATE;
  pWindow->pStateExpr = nodesCloneNode(pState->pExpr);

647 648
  pWindow->pTspk = nodesCloneNode(pState->pCol);
  if (NULL == pWindow->pTspk) {
649
    nodesDestroyNode((SNode*)pWindow);
650 651 652
    return TSDB_CODE_OUT_OF_MEMORY;
  }

653 654 655
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
656 657
static int32_t createWindowLogicNodeBySession(SLogicPlanContext* pCxt, SSessionWindowNode* pSession,
                                              SSelectStmt* pSelect, SLogicNode** pLogicNode) {
658
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
X
Xiaoyu Wang 已提交
659 660 661 662 663 664
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_SESSION;
  pWindow->sessionGap = ((SValueNode*)pSession->pGap)->datum.i;
665
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? SESSION_ALGO_STREAM_SINGLE : SESSION_ALGO_MERGE;
X
Xiaoyu Wang 已提交
666

667
  pWindow->pTspk = nodesCloneNode((SNode*)pSession->pCol);
668
  if (NULL == pWindow->pTspk) {
669
    nodesDestroyNode((SNode*)pWindow);
670 671
    return TSDB_CODE_OUT_OF_MEMORY;
  }
5
54liuyao 已提交
672
  pWindow->pTsEnd = nodesCloneNode((SNode*)pSession->pCol);
673

X
Xiaoyu Wang 已提交
674 675 676
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
677 678
static int32_t createWindowLogicNodeByInterval(SLogicPlanContext* pCxt, SIntervalWindowNode* pInterval,
                                               SSelectStmt* pSelect, SLogicNode** pLogicNode) {
679
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
680 681 682
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
683 684 685

  pWindow->winType = WINDOW_TYPE_INTERVAL;
  pWindow->interval = ((SValueNode*)pInterval->pInterval)->datum.i;
X
Xiaoyu Wang 已提交
686
  pWindow->intervalUnit = ((SValueNode*)pInterval->pInterval)->unit;
X
Xiaoyu Wang 已提交
687
  pWindow->offset = (NULL != pInterval->pOffset ? ((SValueNode*)pInterval->pOffset)->datum.i : 0);
688
  pWindow->sliding = (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->datum.i : pWindow->interval);
X
Xiaoyu Wang 已提交
689 690
  pWindow->slidingUnit =
      (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->unit : pWindow->intervalUnit);
691
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? INTERVAL_ALGO_STREAM_SINGLE : INTERVAL_ALGO_HASH;
692

X
bugfix  
Xiaoyu Wang 已提交
693 694
  pWindow->pTspk = nodesCloneNode(pInterval->pCol);
  if (NULL == pWindow->pTspk) {
695
    nodesDestroyNode((SNode*)pWindow);
X
bugfix  
Xiaoyu Wang 已提交
696 697 698
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
699
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
X
Xiaoyu Wang 已提交
700 701
}

702
static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
703
  if (NULL == pSelect->pWindow) {
704
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
705 706 707
  }

  switch (nodeType(pSelect->pWindow)) {
708 709
    case QUERY_NODE_STATE_WINDOW:
      return createWindowLogicNodeByState(pCxt, (SStateWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
710 711
    case QUERY_NODE_SESSION_WINDOW:
      return createWindowLogicNodeBySession(pCxt, (SSessionWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
712
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
713
      return createWindowLogicNodeByInterval(pCxt, (SIntervalWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
714 715 716 717
    default:
      break;
  }

718
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
719 720
}

X
Xiaoyu Wang 已提交
721 722 723 724 725 726 727
static int32_t createFillLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pWindow || QUERY_NODE_INTERVAL_WINDOW != nodeType(pSelect->pWindow) ||
      NULL == ((SIntervalWindowNode*)pSelect->pWindow)->pFill) {
    return TSDB_CODE_SUCCESS;
  }

  SFillNode* pFillNode = (SFillNode*)(((SIntervalWindowNode*)pSelect->pWindow)->pFill);
728 729 730
  if (FILL_MODE_NONE == pFillNode->mode) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
731

732
  SFillLogicNode* pFill = (SFillLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_FILL);
X
Xiaoyu Wang 已提交
733 734 735 736 737
  if (NULL == pFill) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_WINDOW, NULL, COLLECT_COL_TYPE_ALL, &pFill->node.pTargets);
738 739 740 741
  if (TSDB_CODE_SUCCESS == code && NULL == pFill->node.pTargets) {
    code = nodesListMakeStrictAppend(&pFill->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
  }
X
Xiaoyu Wang 已提交
742 743

  pFill->mode = pFillNode->mode;
X
Xiaoyu Wang 已提交
744
  pFill->timeRange = pFillNode->timeRange;
X
Xiaoyu Wang 已提交
745 746 747 748 749 750 751 752 753
  pFill->pValues = nodesCloneNode(pFillNode->pValues);
  pFill->pWStartTs = nodesCloneNode(pFillNode->pWStartTs);
  if ((NULL != pFillNode->pValues && NULL == pFill->pValues) || NULL == pFill->pWStartTs) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pFill;
  } else {
754
    nodesDestroyNode((SNode*)pFill);
X
Xiaoyu Wang 已提交
755 756 757 758 759
  }

  return code;
}

X
Xiaoyu Wang 已提交
760 761 762 763 764
static int32_t createSortLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pOrderByList) {
    return TSDB_CODE_SUCCESS;
  }

765
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
766 767 768 769
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

770 771
  pSort->groupSort = pSelect->groupSort;

772
  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_ORDER_BY, NULL, COLLECT_COL_TYPE_ALL, &pSort->node.pTargets);
773 774 775 776
  if (TSDB_CODE_SUCCESS == code && NULL == pSort->node.pTargets) {
    code = nodesListMakeStrictAppend(&pSort->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
  }
X
Xiaoyu Wang 已提交
777 778 779 780 781 782 783 784 785 786 787

  if (TSDB_CODE_SUCCESS == code) {
    pSort->pSortKeys = nodesCloneList(pSelect->pOrderByList);
    if (NULL == pSort->pSortKeys) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pSort;
  } else {
788
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
789 790 791 792 793
  }

  return code;
}

X
Xiaoyu Wang 已提交
794 795
static int32_t createColumnByProjections(SLogicPlanContext* pCxt, const char* pStmtName, SNodeList* pExprs,
                                         SNodeList** pCols) {
X
Xiaoyu Wang 已提交
796
  SNodeList* pList = nodesMakeList();
X
Xiaoyu Wang 已提交
797
  if (NULL == pList) {
798 799 800
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
801
  SNode* pNode;
X
Xiaoyu Wang 已提交
802
  FOREACH(pNode, pExprs) {
803
    if (TSDB_CODE_SUCCESS != nodesListAppend(pList, (SNode*)createColumnByExpr(pStmtName, (SExprNode*)pNode))) {
X
Xiaoyu Wang 已提交
804 805
      nodesDestroyList(pList);
      return TSDB_CODE_OUT_OF_MEMORY;
806 807
    }
  }
808

X
Xiaoyu Wang 已提交
809 810
  *pCols = pList;
  return TSDB_CODE_SUCCESS;
811 812
}

X
Xiaoyu Wang 已提交
813
static int32_t createProjectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
814
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
X
Xiaoyu Wang 已提交
815
  if (NULL == pProject) {
816 817 818
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
819 820
  TSWAP(pProject->node.pLimit, pSelect->pLimit);
  TSWAP(pProject->node.pSlimit, pSelect->pSlimit);
821

X
Xiaoyu Wang 已提交
822
  int32_t code = TSDB_CODE_SUCCESS;
823

X
Xiaoyu Wang 已提交
824
  pProject->pProjections = nodesCloneList(pSelect->pProjectionList);
X
Xiaoyu Wang 已提交
825 826
  if (NULL == pProject->pProjections) {
    code = TSDB_CODE_OUT_OF_MEMORY;
827
  }
X
Xiaoyu Wang 已提交
828
  strcpy(pProject->stmtName, pSelect->stmtName);
829 830

  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
831
    code = createColumnByProjections(pCxt, pSelect->stmtName, pSelect->pProjectionList, &pProject->node.pTargets);
832
  }
833

834
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
835 836
    *pLogicNode = (SLogicNode*)pProject;
  } else {
837
    nodesDestroyNode((SNode*)pProject);
838
  }
839

840
  return code;
841 842
}

843
static int32_t createPartitionLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
844
  if (NULL == pSelect->pPartitionByList) {
845 846 847
    return TSDB_CODE_SUCCESS;
  }

848
  SPartitionLogicNode* pPartition = (SPartitionLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PARTITION);
849 850 851 852
  if (NULL == pPartition) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

853 854
  int32_t code =
      nodesCollectColumns(pSelect, SQL_CLAUSE_PARTITION_BY, NULL, COLLECT_COL_TYPE_ALL, &pPartition->node.pTargets);
855 856 857
  if (TSDB_CODE_SUCCESS == code && NULL == pPartition->node.pTargets) {
    code = nodesListMakeStrictAppend(&pPartition->node.pTargets, nodesListGetNode(pCxt->pCurrRoot->pTargets, 0));
  }
858 859 860 861 862 863 864 865 866 867 868

  if (TSDB_CODE_SUCCESS == code) {
    pPartition->pPartitionKeys = nodesCloneList(pSelect->pPartitionByList);
    if (NULL == pPartition->pPartitionKeys) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pPartition;
  } else {
869
    nodesDestroyNode((SNode*)pPartition);
870 871 872
  }

  return code;
873 874 875 876 877 878 879
}

static int32_t createDistinctLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (!pSelect->isDistinct) {
    return TSDB_CODE_SUCCESS;
  }

880 881 882 883 884 885 886 887 888 889 890 891 892 893
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = TSDB_CODE_SUCCESS;
  // set grouyp keys, agg funcs and having conditions
  pAgg->pGroupKeys = nodesCloneList(pSelect->pProjectionList);
  if (NULL == pAgg->pGroupKeys) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
894
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_DISTINCT);
895 896 897 898
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
899
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
900 901 902 903 904
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
905
    nodesDestroyNode((SNode*)pAgg);
906 907 908
  }

  return code;
909 910
}

911 912 913 914 915 916
static int32_t createSelectWithoutFromLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect,
                                                SLogicNode** pLogicNode) {
  return createProjectLogicNode(pCxt, pSelect, pLogicNode);
}

static int32_t createSelectFromLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
917
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
918
  int32_t     code = createLogicNodeByTable(pCxt, pSelect, pSelect->pFromTable, &pRoot);
919
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
920
    code = createSelectRootLogicNode(pCxt, pSelect, createPartitionLogicNode, &pRoot);
921
  }
922
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
923
    code = createSelectRootLogicNode(pCxt, pSelect, createWindowLogicNode, &pRoot);
924
  }
X
Xiaoyu Wang 已提交
925
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
926
    code = createSelectRootLogicNode(pCxt, pSelect, createFillLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
927
  }
928
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
929
    code = createSelectRootLogicNode(pCxt, pSelect, createAggLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
930
  }
931
  if (TSDB_CODE_SUCCESS == code) {
932
    code = createSelectRootLogicNode(pCxt, pSelect, createIndefRowsFuncLogicNode, &pRoot);
933
  }
X
Xiaoyu Wang 已提交
934 935 936
  if (TSDB_CODE_SUCCESS == code) {
    code = createSelectRootLogicNode(pCxt, pSelect, createInterpFuncLogicNode, &pRoot);
  }
937
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
938
    code = createSelectRootLogicNode(pCxt, pSelect, createDistinctLogicNode, &pRoot);
939
  }
X
Xiaoyu Wang 已提交
940
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
941
    code = createSelectRootLogicNode(pCxt, pSelect, createSortLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
942
  }
943
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
944
    code = createSelectRootLogicNode(pCxt, pSelect, createProjectLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
945
  }
946 947 948 949

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
950
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
951
  }
952 953

  return code;
954 955
}

956 957 958 959 960 961 962 963
static int32_t createSelectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pFromTable) {
    return createSelectWithoutFromLogicNode(pCxt, pSelect, pLogicNode);
  } else {
    return createSelectFromLogicNode(pCxt, pSelect, pLogicNode);
  }
}

X
Xiaoyu Wang 已提交
964 965 966
static int32_t createSetOpRootLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, FCreateSetOpLogicNode func,
                                        SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSetOperator, pSetOperator->precision, (FCreateLogicNode)func, pRoot);
X
Xiaoyu Wang 已提交
967 968 969 970 971 972 973
}

static int32_t createSetOpSortLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, SLogicNode** pLogicNode) {
  if (NULL == pSetOperator->pOrderByList) {
    return TSDB_CODE_SUCCESS;
  }

974
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
975 976 977 978
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
979
  TSWAP(pSort->node.pLimit, pSetOperator->pLimit);
980

X
Xiaoyu Wang 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
  int32_t code = TSDB_CODE_SUCCESS;

  pSort->node.pTargets = nodesCloneList(pSetOperator->pProjectionList);
  if (NULL == pSort->node.pTargets) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

  if (TSDB_CODE_SUCCESS == code) {
    pSort->pSortKeys = nodesCloneList(pSetOperator->pOrderByList);
    if (NULL == pSort->pSortKeys) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pSort;
  } else {
998
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
999 1000 1001 1002 1003
  }

  return code;
}

X
Xiaoyu Wang 已提交
1004 1005
static int32_t createSetOpProjectLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                           SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1006 1007 1008 1009 1010
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
  if (NULL == pProject) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1011
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1012
    TSWAP(pProject->node.pLimit, pSetOperator->pLimit);
X
Xiaoyu Wang 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  }

  int32_t code = TSDB_CODE_SUCCESS;

  pProject->pProjections = nodesCloneList(pSetOperator->pProjectionList);
  if (NULL == pProject->pProjections) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

  if (TSDB_CODE_SUCCESS == code) {
1023 1024
    code = createColumnByProjections(pCxt, pSetOperator->stmtName, pSetOperator->pProjectionList,
                                     &pProject->node.pTargets);
X
Xiaoyu Wang 已提交
1025 1026 1027 1028 1029
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pProject;
  } else {
1030
    nodesDestroyNode((SNode*)pProject);
X
Xiaoyu Wang 已提交
1031 1032 1033 1034 1035
  }

  return code;
}

X
Xiaoyu Wang 已提交
1036 1037 1038 1039 1040 1041
static int32_t createSetOpAggLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, SLogicNode** pLogicNode) {
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1042
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1043
    TSWAP(pAgg->node.pSlimit, pSetOperator->pLimit);
1044 1045
  }

X
Xiaoyu Wang 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  int32_t code = TSDB_CODE_SUCCESS;
  pAgg->pGroupKeys = nodesCloneList(pSetOperator->pProjectionList);
  if (NULL == pAgg->pGroupKeys) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExprs(pAgg->pGroupKeys, pSetOperator->pOrderByList);
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1059
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1060 1061 1062 1063 1064
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1065
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1066 1067 1068 1069 1070
  }

  return code;
}

X
Xiaoyu Wang 已提交
1071 1072
static int32_t createSetOpLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, SLogicNode** pLogicNode) {
  SLogicNode* pSetOp = NULL;
X
Xiaoyu Wang 已提交
1073
  int32_t     code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1074 1075 1076 1077
  switch (pSetOperator->opType) {
    case SET_OP_TYPE_UNION_ALL:
      code = createSetOpProjectLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1078 1079 1080
    case SET_OP_TYPE_UNION:
      code = createSetOpAggLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1081
    default:
1082
      code = TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
      break;
  }

  SLogicNode* pLeft = NULL;
  if (TSDB_CODE_SUCCESS == code) {
    code = createQueryLogicNode(pCxt, pSetOperator->pLeft, &pLeft);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesListMakeStrictAppend(&pSetOp->pChildren, (SNode*)pLeft);
  }
  SLogicNode* pRight = NULL;
  if (TSDB_CODE_SUCCESS == code) {
    code = createQueryLogicNode(pCxt, pSetOperator->pRight, &pRight);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesListStrictAppend(pSetOp->pChildren, (SNode*)pRight);
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pSetOp;
  } else {
1104
    nodesDestroyNode((SNode*)pSetOp);
X
Xiaoyu Wang 已提交
1105 1106 1107 1108 1109
  }

  return code;
}

X
Xiaoyu Wang 已提交
1110 1111
static int32_t createSetOperatorLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                          SLogicNode** pLogicNode) {
1112
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1113
  int32_t     code = createSetOpLogicNode(pCxt, pSetOperator, &pRoot);
1114
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1115
    code = createSetOpRootLogicNode(pCxt, pSetOperator, createSetOpSortLogicNode, &pRoot);
1116 1117 1118 1119 1120
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1121
    nodesDestroyNode((SNode*)pRoot);
1122 1123 1124 1125 1126
  }

  return code;
}

1127
static int32_t getMsgType(ENodeType sqlType) {
X
Xiaoyu Wang 已提交
1128 1129 1130 1131 1132 1133
  switch (sqlType) {
    case QUERY_NODE_CREATE_TABLE_STMT:
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
      return TDMT_VND_CREATE_TABLE;
    case QUERY_NODE_DROP_TABLE_STMT:
      return TDMT_VND_DROP_TABLE;
X
Xiaoyu Wang 已提交
1134 1135
    case QUERY_NODE_ALTER_TABLE_STMT:
      return TDMT_VND_ALTER_TABLE;
X
Xiaoyu Wang 已提交
1136 1137
    case QUERY_NODE_FLUSH_DATABASE_STMT:
      return TDMT_VND_COMMIT;
X
Xiaoyu Wang 已提交
1138 1139 1140 1141
    default:
      break;
  }
  return TDMT_VND_SUBMIT;
1142 1143
}

1144
static int32_t createVnodeModifLogicNode(SLogicPlanContext* pCxt, SVnodeModifOpStmt* pStmt, SLogicNode** pLogicNode) {
1145
  SVnodeModifyLogicNode* pModif = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
1146 1147 1148
  if (NULL == pModif) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1149
  pModif->modifyType = MODIFY_TABLE_TYPE_INSERT;
wafwerar's avatar
wafwerar 已提交
1150
  TSWAP(pModif->pDataBlocks, pStmt->pDataBlocks);
1151
  pModif->msgType = getMsgType(pStmt->sqlNodeType);
1152 1153
  *pLogicNode = (SLogicNode*)pModif;
  return TSDB_CODE_SUCCESS;
1154 1155
}

X
Xiaoyu Wang 已提交
1156 1157 1158 1159 1160 1161
static int32_t createDeleteRootLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, FCreateDeleteLogicNode func,
                                         SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pDelete, pDelete->precision, (FCreateLogicNode)func, pRoot);
}

static int32_t createDeleteScanLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
  SScanLogicNode* pScan = NULL;
  int32_t         code = makeScanLogicNode(pCxt, (SRealTableNode*)pDelete->pFromTable, false, (SLogicNode**)&pScan);

  // set columns to scan
  if (TSDB_CODE_SUCCESS == code) {
    pScan->scanType = SCAN_TYPE_TABLE;
    pScan->pScanCols = nodesCloneList(((SFunctionNode*)pDelete->pCountFunc)->pParameterList);
    if (NULL == pScan->pScanCols) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

X
Xiaoyu Wang 已提交
1174 1175
  if (TSDB_CODE_SUCCESS == code && NULL != pDelete->pTagCond) {
    pScan->pTagCond = nodesCloneNode(pDelete->pTagCond);
X
Xiaoyu Wang 已提交
1176 1177 1178 1179 1180 1181 1182
    if (NULL == pScan->pTagCond) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1183
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
X
Xiaoyu Wang 已提交
1184 1185 1186 1187 1188
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
1189
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
1190 1191 1192
  }

  return code;
X
Xiaoyu Wang 已提交
1193 1194 1195
}

static int32_t createDeleteAggLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = nodesListMakeStrictAppend(&pAgg->pAggFuncs, nodesCloneNode(pDelete->pCountFunc));
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExpr(pAgg->pAggFuncs, &pDelete->pCountFunc);
  }
  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1207
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1208 1209 1210 1211 1212
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1213
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1214 1215 1216
  }

  return code;
X
Xiaoyu Wang 已提交
1217 1218
}

X
Xiaoyu Wang 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
static int32_t createVnodeModifLogicNodeByDelete(SLogicPlanContext* pCxt, SDeleteStmt* pDelete,
                                                 SLogicNode** pLogicNode) {
  SVnodeModifyLogicNode* pModify = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
  if (NULL == pModify) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  SRealTableNode* pRealTable = (SRealTableNode*)pDelete->pFromTable;

  pModify->modifyType = MODIFY_TABLE_TYPE_DELETE;
  pModify->tableId = pRealTable->pMeta->uid;
  pModify->tableType = pRealTable->pMeta->tableType;
  snprintf(pModify->tableFName, sizeof(pModify->tableFName), "%d.%s.%s", pCxt->pPlanCxt->acctId,
           pRealTable->table.dbName, pRealTable->table.tableName);
  pModify->deleteTimeRange = pDelete->timeRange;
X
Xiaoyu Wang 已提交
1234 1235
  pModify->pAffectedRows = nodesCloneNode(pDelete->pCountFunc);
  if (NULL == pModify->pAffectedRows) {
1236
    nodesDestroyNode((SNode*)pModify);
X
Xiaoyu Wang 已提交
1237 1238 1239 1240 1241
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  *pLogicNode = (SLogicNode*)pModify;
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1242 1243 1244 1245
}

static int32_t createDeleteLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1246
  int32_t     code = createDeleteScanLogicNode(pCxt, pDelete, &pRoot);
X
Xiaoyu Wang 已提交
1247 1248 1249 1250
  if (TSDB_CODE_SUCCESS == code) {
    code = createDeleteRootLogicNode(pCxt, pDelete, createDeleteAggLogicNode, &pRoot);
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1251
    code = createDeleteRootLogicNode(pCxt, pDelete, createVnodeModifLogicNodeByDelete, &pRoot);
X
Xiaoyu Wang 已提交
1252 1253 1254 1255 1256
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1257
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
1258 1259 1260 1261 1262
  }

  return code;
}

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
static int32_t creatInsertRootLogicNode(SLogicPlanContext* pCxt, SInsertStmt* pInsert, FCreateInsertLogicNode func,
                                        SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pInsert, pInsert->precision, (FCreateLogicNode)func, pRoot);
}

static int32_t createVnodeModifLogicNodeByInsert(SLogicPlanContext* pCxt, SInsertStmt* pInsert,
                                                 SLogicNode** pLogicNode) {
  SVnodeModifyLogicNode* pModify = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
  if (NULL == pModify) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  SRealTableNode* pRealTable = (SRealTableNode*)pInsert->pTable;

  pModify->modifyType = MODIFY_TABLE_TYPE_INSERT;
  pModify->tableId = pRealTable->pMeta->uid;
1279
  pModify->stableId = pRealTable->pMeta->suid;
1280 1281 1282
  pModify->tableType = pRealTable->pMeta->tableType;
  snprintf(pModify->tableFName, sizeof(pModify->tableFName), "%d.%s.%s", pCxt->pPlanCxt->acctId,
           pRealTable->table.dbName, pRealTable->table.tableName);
1283
  TSWAP(pModify->pVgroupList, pRealTable->pVgroupList);
1284 1285 1286 1287 1288
  pModify->pInsertCols = nodesCloneList(pInsert->pCols);
  if (NULL == pModify->pInsertCols) {
    nodesDestroyNode((SNode*)pModify);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309

  *pLogicNode = (SLogicNode*)pModify;
  return TSDB_CODE_SUCCESS;
}

static int32_t createInsertLogicNode(SLogicPlanContext* pCxt, SInsertStmt* pInsert, SLogicNode** pLogicNode) {
  SLogicNode* pRoot = NULL;
  int32_t     code = createQueryLogicNode(pCxt, pInsert->pQuery, &pRoot);
  if (TSDB_CODE_SUCCESS == code) {
    code = creatInsertRootLogicNode(pCxt, pInsert, createVnodeModifLogicNodeByInsert, &pRoot);
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
    nodesDestroyNode((SNode*)pRoot);
  }

  return code;
}

1310
static int32_t createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1311 1312
  switch (nodeType(pStmt)) {
    case QUERY_NODE_SELECT_STMT:
1313
      return createSelectLogicNode(pCxt, (SSelectStmt*)pStmt, pLogicNode);
1314
    case QUERY_NODE_VNODE_MODIF_STMT:
1315
      return createVnodeModifLogicNode(pCxt, (SVnodeModifOpStmt*)pStmt, pLogicNode);
1316 1317
    case QUERY_NODE_EXPLAIN_STMT:
      return createQueryLogicNode(pCxt, ((SExplainStmt*)pStmt)->pQuery, pLogicNode);
1318 1319
    case QUERY_NODE_SET_OPERATOR:
      return createSetOperatorLogicNode(pCxt, (SSetOperator*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1320 1321
    case QUERY_NODE_DELETE_STMT:
      return createDeleteLogicNode(pCxt, (SDeleteStmt*)pStmt, pLogicNode);
1322 1323
    case QUERY_NODE_INSERT_STMT:
      return createInsertLogicNode(pCxt, (SInsertStmt*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1324 1325 1326
    default:
      break;
  }
1327
  return TSDB_CODE_FAILED;
1328 1329
}

X
Xiaoyu Wang 已提交
1330 1331 1332 1333 1334 1335 1336 1337
static void doSetLogicNodeParent(SLogicNode* pNode, SLogicNode* pParent) {
  pNode->pParent = pParent;
  SNode* pChild;
  FOREACH(pChild, pNode->pChildren) { doSetLogicNodeParent((SLogicNode*)pChild, pNode); }
}

static void setLogicNodeParent(SLogicNode* pNode) { doSetLogicNodeParent(pNode, NULL); }

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
static void setLogicSubplanType(SLogicSubplan* pSubplan) {
  if (QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY != nodeType(pSubplan->pNode)) {
    pSubplan->subplanType = SUBPLAN_TYPE_SCAN;
  } else {
    SVnodeModifyLogicNode* pModify = (SVnodeModifyLogicNode*)pSubplan->pNode;
    pSubplan->subplanType = (MODIFY_TABLE_TYPE_INSERT == pModify->modifyType && NULL != pModify->node.pChildren)
                                ? SUBPLAN_TYPE_SCAN
                                : SUBPLAN_TYPE_MODIFY;
  }
}

X
Xiaoyu Wang 已提交
1349
int32_t createLogicPlan(SPlanContext* pCxt, SLogicSubplan** pLogicSubplan) {
X
Xiaoyu Wang 已提交
1350
  SLogicPlanContext cxt = {.pPlanCxt = pCxt};
X
Xiaoyu Wang 已提交
1351 1352 1353 1354

  SLogicSubplan* pSubplan = (SLogicSubplan*)nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN);
  if (NULL == pSubplan) {
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
1355
  }
X
Xiaoyu Wang 已提交
1356 1357 1358 1359 1360 1361 1362
  pSubplan->id.queryId = pCxt->queryId;
  pSubplan->id.groupId = 1;
  pSubplan->id.subplanId = 1;

  int32_t code = createQueryLogicNode(&cxt, pCxt->pAstRoot, &pSubplan->pNode);
  if (TSDB_CODE_SUCCESS == code) {
    setLogicNodeParent(pSubplan->pNode);
1363
    setLogicSubplanType(pSubplan);
X
Xiaoyu Wang 已提交
1364 1365 1366 1367 1368
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicSubplan = pSubplan;
  } else {
1369
    nodesDestroyNode((SNode*)pSubplan);
X
Xiaoyu Wang 已提交
1370 1371 1372
  }

  return code;
1373
}