planLogicCreater.c 60.1 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;
23
  bool          hasScan;
X
Xiaoyu Wang 已提交
24 25
} SLogicPlanContext;

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

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

typedef struct SRewriteExprCxt {
X
Xiaoyu Wang 已提交
37
  int32_t    errCode;
X
Xiaoyu Wang 已提交
38
  SNodeList* pExprs;
X
Xiaoyu Wang 已提交
39
  bool*      pOutputs;
40
  bool       isPartitionBy;
X
Xiaoyu Wang 已提交
41 42
} SRewriteExprCxt;

43
static void setColumnInfo(SFunctionNode* pFunc, SColumnNode* pCol, bool isPartitionBy) {
44 45 46 47 48
  switch (pFunc->funcType) {
    case FUNCTION_TYPE_TBNAME:
      pCol->colType = COLUMN_TYPE_TBNAME;
      break;
    case FUNCTION_TYPE_WSTART:
49 50 51
      if (!isPartitionBy) {
        pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
      }
52 53
      pCol->colType = COLUMN_TYPE_WINDOW_START;
      break;
54
    case FUNCTION_TYPE_WEND:
55 56 57
      if (!isPartitionBy) {
        pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
      }
58
      pCol->colType = COLUMN_TYPE_WINDOW_END;
59 60
      break;
    case FUNCTION_TYPE_WDURATION:
61
      pCol->colType = COLUMN_TYPE_WINDOW_DURATION;
62 63 64 65 66 67 68 69 70
      break;
    case FUNCTION_TYPE_GROUP_KEY:
      pCol->colType = COLUMN_TYPE_GROUP_KEY;
      break;
    default:
      break;
  }
}

X
Xiaoyu Wang 已提交
71
static EDealRes doRewriteExpr(SNode** pNode, void* pContext) {
X
Xiaoyu Wang 已提交
72
  SRewriteExprCxt* pCxt = (SRewriteExprCxt*)pContext;
X
Xiaoyu Wang 已提交
73
  switch (nodeType(*pNode)) {
X
Xiaoyu Wang 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    case QUERY_NODE_COLUMN: {
      if (NULL != pCxt->pOutputs) {
        SNode*  pExpr;
        int32_t index = 0;
        FOREACH(pExpr, pCxt->pExprs) {
          if (QUERY_NODE_GROUPING_SET == nodeType(pExpr)) {
            pExpr = nodesListGetNode(((SGroupingSetNode*)pExpr)->pParameterList, 0);
          }
          if (nodesEqualNode(pExpr, *pNode)) {
            pCxt->pOutputs[index] = true;
            break;
          }
        }
      }
      break;
    }
X
Xiaoyu Wang 已提交
90 91
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION:
D
dapan1121 已提交
92 93
    case QUERY_NODE_FUNCTION:
    case QUERY_NODE_CASE_WHEN: {
X
Xiaoyu Wang 已提交
94 95
      SNode*  pExpr;
      int32_t index = 0;
X
Xiaoyu Wang 已提交
96 97 98 99 100 101
      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 已提交
102 103 104
          if (NULL == pCol) {
            return DEAL_RES_ERROR;
          }
X
Xiaoyu Wang 已提交
105 106 107
          SExprNode* pToBeRewrittenExpr = (SExprNode*)(*pNode);
          pCol->node.resType = pToBeRewrittenExpr->resType;
          strcpy(pCol->node.aliasName, pToBeRewrittenExpr->aliasName);
108
          strcpy(pCol->node.userAlias, ((SExprNode*)pExpr)->userAlias);
X
Xiaoyu Wang 已提交
109
          strcpy(pCol->colName, ((SExprNode*)pExpr)->aliasName);
X
Xiaoyu Wang 已提交
110
          if (QUERY_NODE_FUNCTION == nodeType(pExpr)) {
111
            setColumnInfo((SFunctionNode*)pExpr, pCol, pCxt->isPartitionBy);
X
Xiaoyu Wang 已提交
112
          }
X
Xiaoyu Wang 已提交
113 114
          nodesDestroyNode(*pNode);
          *pNode = (SNode*)pCol;
X
Xiaoyu Wang 已提交
115 116 117
          if (NULL != pCxt->pOutputs) {
            pCxt->pOutputs[index] = true;
          }
X
Xiaoyu Wang 已提交
118 119 120 121 122 123 124 125
          return DEAL_RES_IGNORE_CHILD;
        }
        ++index;
      }
      break;
    }
    default:
      break;
H
Haojun Liao 已提交
126 127
  }

X
Xiaoyu Wang 已提交
128
  return DEAL_RES_CONTINUE;
X
Xiaoyu Wang 已提交
129 130
}

X
Xiaoyu Wang 已提交
131 132 133 134 135
static EDealRes doNameExpr(SNode* pNode, void* pContext) {
  switch (nodeType(pNode)) {
    case QUERY_NODE_OPERATOR:
    case QUERY_NODE_LOGIC_CONDITION:
    case QUERY_NODE_FUNCTION: {
136 137 138
      if ('\0' == ((SExprNode*)pNode)->aliasName[0]) {
        sprintf(((SExprNode*)pNode)->aliasName, "#expr_%p", pNode);
      }
X
Xiaoyu Wang 已提交
139
      return DEAL_RES_IGNORE_CHILD;
X
Xiaoyu Wang 已提交
140 141
    }
    default:
X
Xiaoyu Wang 已提交
142
      break;
X
Xiaoyu Wang 已提交
143
  }
144

X
Xiaoyu Wang 已提交
145
  return DEAL_RES_CONTINUE;
146 147
}

D
dapan1121 已提交
148 149
static int32_t rewriteExprForSelect(SNode* pExpr, SSelectStmt* pSelect, ESqlClause clause) {
  nodesWalkExpr(pExpr, doNameExpr, NULL);
150 151
  bool isPartitionBy = (pSelect->pPartitionByList && pSelect->pPartitionByList->length > 0) ? true : false;
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = NULL, .pOutputs = NULL, .isPartitionBy = isPartitionBy};
D
dapan1121 已提交
152 153 154 155 156 157 158 159
  cxt.errCode = nodesListMakeAppend(&cxt.pExprs, pExpr);
  if (TSDB_CODE_SUCCESS == cxt.errCode) {
    nodesRewriteSelectStmt(pSelect, clause, doRewriteExpr, &cxt);
    nodesClearList(cxt.pExprs);
  }
  return cxt.errCode;
}

X
Xiaoyu Wang 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
static int32_t cloneRewriteExprs(SNodeList* pExprs, bool* pOutputs, SNodeList** pRewriteExpr) {
  int32_t code = TSDB_CODE_SUCCESS;
  int32_t index = 0;
  SNode*  pExpr = NULL;
  FOREACH(pExpr, pExprs) {
    if (pOutputs[index]) {
      code = nodesListMakeStrictAppend(pRewriteExpr, nodesCloneNode(pExpr));
      if (TSDB_CODE_SUCCESS != code) {
        NODES_DESTORY_LIST(*pRewriteExpr);
        break;
      }
    }
  }
  return code;
}

static int32_t rewriteExprsForSelect(SNodeList* pExprs, SSelectStmt* pSelect, ESqlClause clause,
X
Xiaoyu Wang 已提交
177
                                     SNodeList** pRewriteExprs) {
X
Xiaoyu Wang 已提交
178
  nodesWalkExprs(pExprs, doNameExpr, NULL);
179 180
  bool isPartitionBy = (pSelect->pPartitionByList && pSelect->pPartitionByList->length > 0) ? true : false;
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs, .pOutputs = NULL, .isPartitionBy = isPartitionBy};
X
Xiaoyu Wang 已提交
181
  if (NULL != pRewriteExprs) {
X
Xiaoyu Wang 已提交
182 183 184 185 186
    cxt.pOutputs = taosMemoryCalloc(LIST_LENGTH(pExprs), sizeof(bool));
    if (NULL == cxt.pOutputs) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
X
Xiaoyu Wang 已提交
187
  nodesRewriteSelectStmt(pSelect, clause, doRewriteExpr, &cxt);
X
Xiaoyu Wang 已提交
188 189
  if (TSDB_CODE_SUCCESS == cxt.errCode && NULL != pRewriteExprs) {
    cxt.errCode = cloneRewriteExprs(pExprs, cxt.pOutputs, pRewriteExprs);
X
Xiaoyu Wang 已提交
190 191
  }
  taosMemoryFree(cxt.pOutputs);
X
Xiaoyu Wang 已提交
192
  return cxt.errCode;
193 194
}

X
Xiaoyu Wang 已提交
195 196
static int32_t rewriteExpr(SNodeList* pExprs, SNode** pTarget) {
  nodesWalkExprs(pExprs, doNameExpr, NULL);
197
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs, .pOutputs = NULL, .isPartitionBy = false};
X
Xiaoyu Wang 已提交
198 199 200 201
  nodesRewriteExpr(pTarget, doRewriteExpr, &cxt);
  return cxt.errCode;
}

X
Xiaoyu Wang 已提交
202
static int32_t rewriteExprs(SNodeList* pExprs, SNodeList* pTarget) {
X
Xiaoyu Wang 已提交
203
  nodesWalkExprs(pExprs, doNameExpr, NULL);
204
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs, .pOutputs = NULL, .isPartitionBy = false};
X
Xiaoyu Wang 已提交
205 206 207 208
  nodesRewriteExprs(pTarget, doRewriteExpr, &cxt);
  return cxt.errCode;
}

209 210 211 212 213
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 已提交
214
    }
215
  }
216 217
  if (TSDB_CODE_SUCCESS != nodesListAppend(pNewRoot->pChildren, (SNode*)*pOldRoot)) {
    return TSDB_CODE_OUT_OF_MEMORY;
218 219
  }

220 221
  (*pOldRoot)->pParent = pNewRoot;
  *pOldRoot = pNewRoot;
222

223 224
  return TSDB_CODE_SUCCESS;
}
225

X
Xiaoyu Wang 已提交
226 227
static int32_t createRootLogicNode(SLogicPlanContext* pCxt, void* pStmt, uint8_t precision, FCreateLogicNode func,
                                   SLogicNode** pRoot) {
X
Xiaoyu Wang 已提交
228
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
229
  int32_t     code = func(pCxt, pStmt, &pNode);
X
Xiaoyu Wang 已提交
230
  if (TSDB_CODE_SUCCESS == code && NULL != pNode) {
X
Xiaoyu Wang 已提交
231
    pNode->precision = precision;
X
Xiaoyu Wang 已提交
232
    code = pushLogicNode(pCxt, pRoot, pNode);
233
    pCxt->pCurrRoot = pNode;
234
  }
X
Xiaoyu Wang 已提交
235
  if (TSDB_CODE_SUCCESS != code) {
236
    nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
237
  }
X
Xiaoyu Wang 已提交
238
  return code;
239 240
}

X
Xiaoyu Wang 已提交
241 242 243 244
static int32_t createSelectRootLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, FCreateSelectLogicNode func,
                                         SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSelect, pSelect->precision, (FCreateLogicNode)func, pRoot);
}
245

246
static EScanType getScanType(SLogicPlanContext* pCxt, SNodeList* pScanPseudoCols, SNodeList* pScanCols,
247
                             int8_t tableType, bool tagScan) {
X
Xiaoyu Wang 已提交
248 249 250 251
  if (pCxt->pPlanCxt->topicQuery || pCxt->pPlanCxt->streamQuery) {
    return SCAN_TYPE_STREAM;
  }

252 253 254 255
  if (TSDB_SYSTEM_TABLE == tableType) {
    return SCAN_TYPE_SYSTEM_TABLE;
  }

256 257 258 259
  if (tagScan) {
    return SCAN_TYPE_TAG;
  }

X
Xiaoyu Wang 已提交
260
  if (NULL == pScanCols) {
261 262 263
    if (NULL == pScanPseudoCols) {
      return SCAN_TYPE_TABLE;
    }
X
Xiaoyu Wang 已提交
264
    return FUNCTION_TYPE_BLOCK_DIST_INFO == ((SFunctionNode*)nodesListGetNode(pScanPseudoCols, 0))->funcType
265
               ? SCAN_TYPE_BLOCK_INFO
X
Xiaoyu Wang 已提交
266
               : SCAN_TYPE_TABLE;
X
Xiaoyu Wang 已提交
267 268
  }

269
  return SCAN_TYPE_TABLE;
X
Xiaoyu Wang 已提交
270 271
}

272
static SNode* createFirstCol(uint64_t tableId, const SSchema* pSchema) {
273
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
274 275 276
  if (NULL == pCol) {
    return NULL;
  }
277 278
  pCol->node.resType.type = pSchema->type;
  pCol->node.resType.bytes = pSchema->bytes;
279
  pCol->tableId = tableId;
280
  pCol->colId = pSchema->colId;
281
  pCol->colType = COLUMN_TYPE_COLUMN;
282
  strcpy(pCol->colName, pSchema->name);
283
  return (SNode*)pCol;
284 285
}

286
static int32_t addPrimaryKeyCol(uint64_t tableId, const SSchema* pSchema, SNodeList** pCols) {
X
Xiaoyu Wang 已提交
287
  bool   found = false;
288 289 290 291 292 293 294 295 296
  SNode* pCol = NULL;
  FOREACH(pCol, *pCols) {
    if (PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pCol)->colId) {
      found = true;
      break;
    }
  }

  if (!found) {
297
    return nodesListMakeStrictAppend(pCols, createFirstCol(tableId, pSchema));
298 299 300 301
  }
  return TSDB_CODE_SUCCESS;
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315
static int32_t addSystableFirstCol(uint64_t tableId, const SSchema* pSchema, SNodeList** pCols) {
  if (LIST_LENGTH(*pCols) > 0) {
    return TSDB_CODE_SUCCESS;
  }
  return nodesListMakeStrictAppend(pCols, createFirstCol(tableId, pSchema));
}

static int32_t addDefaultScanCol(const STableMeta* pMeta, SNodeList** pCols) {
  if (TSDB_SYSTEM_TABLE == pMeta->tableType) {
    return addSystableFirstCol(pMeta->uid, pMeta->schema, pCols);
  }
  return addPrimaryKeyCol(pMeta->uid, pMeta->schema, pCols);
}

X
Xiaoyu Wang 已提交
316 317
static int32_t makeScanLogicNode(SLogicPlanContext* pCxt, SRealTableNode* pRealTable, bool hasRepeatScanFuncs,
                                 SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
318
  SScanLogicNode* pScan = (SScanLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SCAN);
X
Xiaoyu Wang 已提交
319
  if (NULL == pScan) {
320
    return TSDB_CODE_OUT_OF_MEMORY;
321 322
  }

wafwerar's avatar
wafwerar 已提交
323
  TSWAP(pScan->pVgroupList, pRealTable->pVgroupList);
X
Xiaoyu Wang 已提交
324
  TSWAP(pScan->pSmaIndexes, pRealTable->pSmaIndexes);
X
Xiaoyu Wang 已提交
325
  pScan->tableId = pRealTable->pMeta->uid;
X
Xiaoyu Wang 已提交
326
  pScan->stableId = pRealTable->pMeta->suid;
X
Xiaoyu Wang 已提交
327 328
  pScan->tableType = pRealTable->pMeta->tableType;
  pScan->scanSeq[0] = hasRepeatScanFuncs ? 2 : 1;
X
Xiaoyu Wang 已提交
329
  pScan->scanSeq[1] = 0;
X
Xiaoyu Wang 已提交
330
  pScan->scanRange = TSWINDOW_INITIALIZER;
X
Xiaoyu Wang 已提交
331 332 333 334
  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 已提交
335
  pScan->showRewrite = pCxt->pPlanCxt->showRewrite;
336
  pScan->ratio = pRealTable->ratio;
337
  pScan->dataRequired = FUNC_DATA_REQUIRED_DATA_LOAD;
338
  pScan->cacheLastMode = pRealTable->cacheLastMode;
339

X
Xiaoyu Wang 已提交
340 341 342 343 344
  *pLogicNode = (SLogicNode*)pScan;

  return TSDB_CODE_SUCCESS;
}

345 346
static bool needScanDefaultCol(EScanType scanType) { return SCAN_TYPE_TABLE_COUNT != scanType; }

X
Xiaoyu Wang 已提交
347 348 349 350 351
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);

352 353
  pScan->node.groupAction = GROUP_ACTION_NONE;
  pScan->node.resultDataOrder = DATA_ORDER_LEVEL_IN_BLOCK;
354 355 356 357 358 359 360
  if (pCxt->pPlanCxt->streamQuery) {
    pScan->triggerType = pCxt->pPlanCxt->triggerType;
    pScan->watermark = pCxt->pPlanCxt->watermark;
    pScan->deleteMark = pCxt->pPlanCxt->deleteMark;
    pScan->igExpired = pCxt->pPlanCxt->igExpired;
    pScan->igCheckUpdate = pCxt->pPlanCxt->igCheckUpdate;
  }
361

X
Xiaoyu Wang 已提交
362
  // set columns to scan
X
Xiaoyu Wang 已提交
363 364 365 366
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, COLLECT_COL_TYPE_COL,
                               &pScan->pScanCols);
  }
367

368
  if (TSDB_CODE_SUCCESS == code) {
369 370
    code = nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, COLLECT_COL_TYPE_TAG,
                               &pScan->pScanPseudoCols);
371 372 373
  }

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

377 378
  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
379
    code = rewriteExprsForSelect(pScan->pScanPseudoCols, pSelect, SQL_CLAUSE_FROM, NULL);
380 381
  }

382
  pScan->scanType = getScanType(pCxt, pScan->pScanPseudoCols, pScan->pScanCols, pScan->tableType, pSelect->tagScan);
X
Xiaoyu Wang 已提交
383

384 385 386 387
  if (NULL != pScan->pScanCols) {
    pScan->hasNormalCols = true;
  }

388
  if (TSDB_CODE_SUCCESS == code && needScanDefaultCol(pScan->scanType)) {
389
    code = addDefaultScanCol(pRealTable->pMeta, &pScan->pScanCols);
390 391
  }

392 393 394 395 396 397 398 399 400 401 402 403 404 405
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pTags && NULL == pSelect->pPartitionByList) {
    pScan->pTags = nodesCloneList(pSelect->pTags);
    if (NULL == pScan->pTags) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pSubtable && NULL == pSelect->pPartitionByList) {
    pScan->pSubtable = nodesCloneNode(pSelect->pSubtable);
    if (NULL == pScan->pSubtable) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

406 407
  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
408
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
409 410
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
411
    code = createColumnByRewriteExprs(pScan->pScanPseudoCols, &pScan->node.pTargets);
412
  }
413

X
Xiaoyu Wang 已提交
414 415 416
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
417
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
418
  }
419

420 421
  pCxt->hasScan = true;

X
Xiaoyu Wang 已提交
422
  return code;
X
Xiaoyu Wang 已提交
423
}
424

X
Xiaoyu Wang 已提交
425 426
static int32_t createSubqueryLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, STempTableNode* pTable,
                                       SLogicNode** pLogicNode) {
427
  return createQueryLogicNode(pCxt, pTable->pSubquery, pLogicNode);
428 429
}

X
Xiaoyu Wang 已提交
430 431
static int32_t createJoinLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SJoinTableNode* pJoinTable,
                                   SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
432
  SJoinLogicNode* pJoin = (SJoinLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_JOIN);
X
Xiaoyu Wang 已提交
433
  if (NULL == pJoin) {
434 435
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
436 437

  pJoin->joinType = pJoinTable->joinType;
X
Xiaoyu Wang 已提交
438
  pJoin->isSingleTableJoin = pJoinTable->table.singleTable;
439
  pJoin->node.inputTsOrder = ORDER_ASC;
440 441
  pJoin->node.groupAction = GROUP_ACTION_CLEAR;
  pJoin->node.requireDataOrder = DATA_ORDER_LEVEL_GLOBAL;
442
  pJoin->node.resultDataOrder = DATA_ORDER_LEVEL_GLOBAL;
X
Xiaoyu Wang 已提交
443

444
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
445 446 447

  // set left and right node
  pJoin->node.pChildren = nodesMakeList();
X
Xiaoyu Wang 已提交
448
  if (NULL == pJoin->node.pChildren) {
449
    code = TSDB_CODE_OUT_OF_MEMORY;
450 451
  }

X
Xiaoyu Wang 已提交
452
  SLogicNode* pLeft = NULL;
453
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
454 455 456 457
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pLeft, &pLeft);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pLeft);
    }
458
  }
459

X
Xiaoyu Wang 已提交
460
  SLogicNode* pRight = NULL;
461
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
462 463 464 465 466
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pRight, &pRight);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pRight);
    }
  }
X
Xiaoyu Wang 已提交
467 468

  // set on conditions
X
Xiaoyu Wang 已提交
469
  if (TSDB_CODE_SUCCESS == code && NULL != pJoinTable->pOnCond) {
X
Xiaoyu Wang 已提交
470
    pJoin->pOnConditions = nodesCloneNode(pJoinTable->pOnCond);
X
Xiaoyu Wang 已提交
471 472 473
    if (NULL == pJoin->pOnConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
474 475
  }

X
Xiaoyu Wang 已提交
476
  // set the output
X
Xiaoyu Wang 已提交
477 478
  if (TSDB_CODE_SUCCESS == code) {
    pJoin->node.pTargets = nodesCloneList(pLeft->pTargets);
D
dapan1121 已提交
479
    if (NULL == pJoin->node.pTargets) {
X
Xiaoyu Wang 已提交
480 481 482 483 484 485 486 487 488
      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;
489
  } else {
490
    nodesDestroyNode((SNode*)pJoin);
491 492
  }

493
  return code;
X
Xiaoyu Wang 已提交
494
}
495

X
Xiaoyu Wang 已提交
496 497
static int32_t doCreateLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                        SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
498 499
  switch (nodeType(pTable)) {
    case QUERY_NODE_REAL_TABLE:
500
      return createScanLogicNode(pCxt, pSelect, (SRealTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
501
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
502
      return createSubqueryLogicNode(pCxt, pSelect, (STempTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
503
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
504
      return createJoinLogicNode(pCxt, pSelect, (SJoinTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
505 506
    default:
      break;
507
  }
X
Xiaoyu Wang 已提交
508 509 510
  return TSDB_CODE_FAILED;
}

X
Xiaoyu Wang 已提交
511 512
static int32_t createLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                      SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
513
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
514
  int32_t     code = doCreateLogicNodeByTable(pCxt, pSelect, pTable, &pNode);
X
Xiaoyu Wang 已提交
515 516 517
  if (TSDB_CODE_SUCCESS == code) {
    pNode->pConditions = nodesCloneNode(pSelect->pWhere);
    if (NULL != pSelect->pWhere && NULL == pNode->pConditions) {
518
      nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
519 520
      return TSDB_CODE_OUT_OF_MEMORY;
    }
521
    pNode->precision = pSelect->precision;
X
Xiaoyu Wang 已提交
522
    *pLogicNode = pNode;
523
    pCxt->pCurrRoot = pNode;
X
Xiaoyu Wang 已提交
524 525 526 527
  }
  return code;
}

X
Xiaoyu Wang 已提交
528
static SColumnNode* createColumnByExpr(const char* pStmtName, SExprNode* pExpr) {
529
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
530 531 532 533
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType = pExpr->resType;
X
Xiaoyu Wang 已提交
534
  snprintf(pCol->colName, sizeof(pCol->colName), "%s", pExpr->aliasName);
X
Xiaoyu Wang 已提交
535
  if (NULL != pStmtName) {
X
Xiaoyu Wang 已提交
536
    snprintf(pCol->tableAlias, sizeof(pCol->tableAlias), "%s", pStmtName);
X
Xiaoyu Wang 已提交
537
  }
X
Xiaoyu Wang 已提交
538
  return pCol;
539 540
}

541 542 543 544 545 546 547 548 549 550 551 552 553
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;
}

D
dapan1121 已提交
554
static EGroupAction getDistinctGroupAction(SLogicPlanContext* pCxt, SSelectStmt* pSelect) {
X
Xiaoyu Wang 已提交
555 556
  return (pCxt->pPlanCxt->streamQuery || NULL != pSelect->pLimit || NULL != pSelect->pSlimit) ? GROUP_ACTION_KEEP
                                                                                              : GROUP_ACTION_NONE;
557 558
}

D
dapan1121 已提交
559 560 561 562 563
static EGroupAction getGroupAction(SLogicPlanContext* pCxt, SSelectStmt* pSelect) {
  return ((pCxt->pPlanCxt->streamQuery || NULL != pSelect->pLimit || NULL != pSelect->pSlimit) && !pSelect->isDistinct) ? GROUP_ACTION_KEEP
                                                                                              : GROUP_ACTION_NONE;
}

564 565 566 567 568
static EDataOrderLevel getRequireDataOrder(bool needTimeline, SSelectStmt* pSelect) {
  return needTimeline ? (NULL != pSelect->pPartitionByList ? DATA_ORDER_LEVEL_IN_GROUP : DATA_ORDER_LEVEL_GLOBAL)
                      : DATA_ORDER_LEVEL_NONE;
}

569
static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
570
  if (!pSelect->hasAggFuncs && NULL == pSelect->pGroupByList) {
571
    return TSDB_CODE_SUCCESS;
572 573
  }

X
Xiaoyu Wang 已提交
574
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
575 576 577
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
578

579
  pAgg->hasLastRow = pSelect->hasLastRowFunc;
X
Xiaoyu Wang 已提交
580
  pAgg->hasLast = pSelect->hasLastFunc;
581
  pAgg->hasTimeLineFunc = pSelect->hasTimeLineFunc;
582
  pAgg->hasGroupKeyOptimized = false;
583
  pAgg->onlyHasKeepOrderFunc = pSelect->onlyHasKeepOrderFunc;
584
  pAgg->node.groupAction = getGroupAction(pCxt, pSelect);
585 586
  pAgg->node.requireDataOrder = getRequireDataOrder(pAgg->hasTimeLineFunc, pSelect);
  pAgg->node.resultDataOrder = pAgg->onlyHasKeepOrderFunc ? pAgg->node.requireDataOrder : DATA_ORDER_LEVEL_NONE;
587

X
Xiaoyu Wang 已提交
588 589
  int32_t code = TSDB_CODE_SUCCESS;

X
Xiaoyu Wang 已提交
590
  // set grouyp keys, agg funcs and having conditions
591
  if (TSDB_CODE_SUCCESS == code) {
592 593 594
    code = nodesCollectFuncs(pSelect, SQL_CLAUSE_GROUP_BY, fmIsAggFunc, &pAgg->pAggFuncs);
  }

X
Xiaoyu Wang 已提交
595
  // rewrite the expression in subsequent clauses
596
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
X
Xiaoyu Wang 已提交
597
    code = rewriteExprsForSelect(pAgg->pAggFuncs, pSelect, SQL_CLAUSE_GROUP_BY, NULL);
598 599 600
  }

  if (NULL != pSelect->pGroupByList) {
X
Xiaoyu Wang 已提交
601 602 603
    pAgg->pGroupKeys = nodesCloneList(pSelect->pGroupByList);
    if (NULL == pAgg->pGroupKeys) {
      code = TSDB_CODE_OUT_OF_MEMORY;
604
    }
605
  }
X
Xiaoyu Wang 已提交
606 607

  // rewrite the expression in subsequent clauses
X
Xiaoyu Wang 已提交
608
  SNodeList* pOutputGroupKeys = NULL;
609
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
610
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_GROUP_BY, &pOutputGroupKeys);
611
  }
612

613
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pHaving) {
X
Xiaoyu Wang 已提交
614
    pAgg->node.pConditions = nodesCloneNode(pSelect->pHaving);
615 616 617
    if (NULL == pAgg->node.pConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
618
  }
619

X
Xiaoyu Wang 已提交
620
  // set the output
621
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
X
Xiaoyu Wang 已提交
622
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
623 624
  }

X
Xiaoyu Wang 已提交
625 626 627 628 629 630 631 632 633
  if (TSDB_CODE_SUCCESS == code) {
    if (NULL != pOutputGroupKeys) {
      code = createColumnByRewriteExprs(pOutputGroupKeys, &pAgg->node.pTargets);
    } else if (NULL == pAgg->node.pTargets && NULL != pAgg->pGroupKeys) {
      code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
    }
  }
  nodesDestroyList(pOutputGroupKeys);

634 635 636
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
637
    nodesDestroyNode((SNode*)pAgg);
638 639 640
  }

  return code;
X
Xiaoyu Wang 已提交
641
}
642

643 644
static int32_t createIndefRowsFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  // top/bottom are both an aggregate function and a indefinite rows function
645
  if (!pSelect->hasIndefiniteRowsFunc || pSelect->hasAggFuncs || NULL != pSelect->pWindow) {
646 647 648 649 650 651 652 653 654
    return TSDB_CODE_SUCCESS;
  }

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

655
  pIdfRowsFunc->isTailFunc = pSelect->hasTailFunc;
656
  pIdfRowsFunc->isUniqueFunc = pSelect->hasUniqueFunc;
657
  pIdfRowsFunc->isTimeLineFunc = pSelect->hasTimeLineFunc;
658
  pIdfRowsFunc->node.groupAction = getGroupAction(pCxt, pSelect);
659
  pIdfRowsFunc->node.requireDataOrder = getRequireDataOrder(pIdfRowsFunc->isTimeLineFunc, pSelect);
660
  pIdfRowsFunc->node.resultDataOrder = pIdfRowsFunc->node.requireDataOrder;
661

X
Xiaoyu Wang 已提交
662 663
  // indefinite rows functions and _select_values functions
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, fmIsVectorFunc, &pIdfRowsFunc->pFuncs);
664
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
665
    code = rewriteExprsForSelect(pIdfRowsFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT, NULL);
666 667 668 669
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
670
    code = createColumnByRewriteExprs(pIdfRowsFunc->pFuncs, &pIdfRowsFunc->node.pTargets);
671 672 673 674 675
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pIdfRowsFunc;
  } else {
676
    nodesDestroyNode((SNode*)pIdfRowsFunc);
677 678 679 680 681
  }

  return code;
}

X
Xiaoyu Wang 已提交
682 683 684
static bool isInterpFunc(int32_t funcId) {
  return fmIsInterpFunc(funcId) || fmIsInterpPseudoColumnFunc(funcId) || fmIsGroupKeyFunc(funcId);
}
X
Xiaoyu Wang 已提交
685

X
Xiaoyu Wang 已提交
686 687 688 689 690 691 692 693 694 695
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;
  }

696
  pInterpFunc->node.groupAction = getGroupAction(pCxt, pSelect);
697
  pInterpFunc->node.requireDataOrder = getRequireDataOrder(true, pSelect);
698 699
  pInterpFunc->node.resultDataOrder = pInterpFunc->node.requireDataOrder;

X
Xiaoyu Wang 已提交
700
  // interp functions and _group_key functions
X
Xiaoyu Wang 已提交
701
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, isInterpFunc, &pInterpFunc->pFuncs);
X
Xiaoyu Wang 已提交
702
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
703
    code = rewriteExprsForSelect(pInterpFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT, NULL);
X
Xiaoyu Wang 已提交
704 705
  }

X
Xiaoyu Wang 已提交
706 707 708 709 710 711 712 713 714
  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 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
  }

  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 已提交
735 736
static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SWindowLogicNode* pWindow,
                                             SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
737 738 739
  if (pCxt->pPlanCxt->streamQuery) {
    pWindow->triggerType = pCxt->pPlanCxt->triggerType;
    pWindow->watermark = pCxt->pPlanCxt->watermark;
740
    pWindow->deleteMark = pCxt->pPlanCxt->deleteMark;
741
    pWindow->igExpired = pCxt->pPlanCxt->igExpired;
742
    pWindow->igCheckUpdate = pCxt->pPlanCxt->igCheckUpdate;
5
54liuyao 已提交
743
  }
744 745
  pWindow->node.inputTsOrder = ORDER_ASC;
  pWindow->node.outputTsOrder = ORDER_ASC;
5
54liuyao 已提交
746

747
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_WINDOW, fmIsWindowClauseFunc, &pWindow->pFuncs);
X
Xiaoyu Wang 已提交
748
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
749
    code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW, NULL);
X
Xiaoyu Wang 已提交
750 751 752
  }

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

756 757 758 759 760 761 762
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pHaving) {
    pWindow->node.pConditions = nodesCloneNode(pSelect->pHaving);
    if (NULL == pWindow->node.pConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

X
Xiaoyu Wang 已提交
763 764
  pSelect->hasAggFuncs = false;

X
Xiaoyu Wang 已提交
765 766 767
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pWindow;
  } else {
768
    nodesDestroyNode((SNode*)pWindow);
X
Xiaoyu Wang 已提交
769 770 771 772 773
  }

  return code;
}

X
Xiaoyu Wang 已提交
774 775
static int32_t createWindowLogicNodeByState(SLogicPlanContext* pCxt, SStateWindowNode* pState, SSelectStmt* pSelect,
                                            SLogicNode** pLogicNode) {
776
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
777 778 779 780 781
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_STATE;
782
  pWindow->node.groupAction = getGroupAction(pCxt, pSelect);
783 784 785 786
  pWindow->node.requireDataOrder =
      pCxt->pPlanCxt->streamQuery ? DATA_ORDER_LEVEL_IN_BLOCK : getRequireDataOrder(true, pSelect);
  pWindow->node.resultDataOrder =
      pCxt->pPlanCxt->streamQuery ? DATA_ORDER_LEVEL_GLOBAL : pWindow->node.requireDataOrder;
787
  pWindow->pStateExpr = nodesCloneNode(pState->pExpr);
788
  pWindow->pTspk = nodesCloneNode(pState->pCol);
789
  if (NULL == pWindow->pStateExpr || NULL == pWindow->pTspk) {
790
    nodesDestroyNode((SNode*)pWindow);
791 792
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
dapan1121 已提交
793 794 795 796 797
  // rewrite the expression in subsequent clauses
  int32_t code = rewriteExprForSelect(pWindow->pStateExpr, pSelect, SQL_CLAUSE_WINDOW);
  if (TSDB_CODE_SUCCESS == code) {
    code = createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
  }
X
Xiaoyu Wang 已提交
798

D
dapan1121 已提交
799
  return code;
800 801
}

X
Xiaoyu Wang 已提交
802 803
static int32_t createWindowLogicNodeBySession(SLogicPlanContext* pCxt, SSessionWindowNode* pSession,
                                              SSelectStmt* pSelect, SLogicNode** pLogicNode) {
804
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
X
Xiaoyu Wang 已提交
805 806 807 808 809 810
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_SESSION;
  pWindow->sessionGap = ((SValueNode*)pSession->pGap)->datum.i;
811
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? SESSION_ALGO_STREAM_SINGLE : SESSION_ALGO_MERGE;
812
  pWindow->node.groupAction = getGroupAction(pCxt, pSelect);
813 814 815 816
  pWindow->node.requireDataOrder =
      pCxt->pPlanCxt->streamQuery ? DATA_ORDER_LEVEL_IN_BLOCK : getRequireDataOrder(true, pSelect);
  pWindow->node.resultDataOrder =
      pCxt->pPlanCxt->streamQuery ? DATA_ORDER_LEVEL_GLOBAL : pWindow->node.requireDataOrder;
X
Xiaoyu Wang 已提交
817

818
  pWindow->pTspk = nodesCloneNode((SNode*)pSession->pCol);
819
  if (NULL == pWindow->pTspk) {
820
    nodesDestroyNode((SNode*)pWindow);
821 822
    return TSDB_CODE_OUT_OF_MEMORY;
  }
5
54liuyao 已提交
823
  pWindow->pTsEnd = nodesCloneNode((SNode*)pSession->pCol);
824

X
Xiaoyu Wang 已提交
825 826 827
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
828 829
static int32_t createWindowLogicNodeByInterval(SLogicPlanContext* pCxt, SIntervalWindowNode* pInterval,
                                               SSelectStmt* pSelect, SLogicNode** pLogicNode) {
830
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
831 832 833
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
834 835 836

  pWindow->winType = WINDOW_TYPE_INTERVAL;
  pWindow->interval = ((SValueNode*)pInterval->pInterval)->datum.i;
X
Xiaoyu Wang 已提交
837
  pWindow->intervalUnit = ((SValueNode*)pInterval->pInterval)->unit;
X
Xiaoyu Wang 已提交
838
  pWindow->offset = (NULL != pInterval->pOffset ? ((SValueNode*)pInterval->pOffset)->datum.i : 0);
839
  pWindow->sliding = (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->datum.i : pWindow->interval);
X
Xiaoyu Wang 已提交
840 841
  pWindow->slidingUnit =
      (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->unit : pWindow->intervalUnit);
842
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? INTERVAL_ALGO_STREAM_SINGLE : INTERVAL_ALGO_HASH;
843
  pWindow->node.groupAction = (NULL != pInterval->pFill ? GROUP_ACTION_KEEP : getGroupAction(pCxt, pSelect));
844 845 846 847 848 849
  pWindow->node.requireDataOrder =
      pCxt->pPlanCxt->streamQuery
          ? DATA_ORDER_LEVEL_IN_BLOCK
          : (pSelect->hasTimeLineFunc ? getRequireDataOrder(true, pSelect) : DATA_ORDER_LEVEL_IN_BLOCK);
  pWindow->node.resultDataOrder =
      pCxt->pPlanCxt->streamQuery ? DATA_ORDER_LEVEL_GLOBAL : getRequireDataOrder(true, pSelect);
850

X
bugfix  
Xiaoyu Wang 已提交
851 852
  pWindow->pTspk = nodesCloneNode(pInterval->pCol);
  if (NULL == pWindow->pTspk) {
853
    nodesDestroyNode((SNode*)pWindow);
X
bugfix  
Xiaoyu Wang 已提交
854 855 856
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
857
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
X
Xiaoyu Wang 已提交
858 859
}

X
Xiaoyu Wang 已提交
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
static int32_t createWindowLogicNodeByEvent(SLogicPlanContext* pCxt, SEventWindowNode* pEvent, SSelectStmt* pSelect,
                                            SLogicNode** pLogicNode) {
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_EVENT;
  pWindow->node.groupAction = getGroupAction(pCxt, pSelect);
  pWindow->node.requireDataOrder =
      pCxt->pPlanCxt->streamQuery ? DATA_ORDER_LEVEL_IN_BLOCK : getRequireDataOrder(true, pSelect);
  pWindow->node.resultDataOrder =
      pCxt->pPlanCxt->streamQuery ? DATA_ORDER_LEVEL_GLOBAL : pWindow->node.requireDataOrder;
  pWindow->pStartCond = nodesCloneNode(pEvent->pStartCond);
  pWindow->pEndCond = nodesCloneNode(pEvent->pEndCond);
  pWindow->pTspk = nodesCloneNode(pEvent->pCol);
X
Xiaoyu Wang 已提交
876
  if (NULL == pWindow->pStartCond || NULL == pWindow->pEndCond || NULL == pWindow->pTspk) {
X
Xiaoyu Wang 已提交
877 878 879
    nodesDestroyNode((SNode*)pWindow);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
880
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
X
Xiaoyu Wang 已提交
881 882
}

883
static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
884
  if (NULL == pSelect->pWindow) {
885
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
886 887 888
  }

  switch (nodeType(pSelect->pWindow)) {
889 890
    case QUERY_NODE_STATE_WINDOW:
      return createWindowLogicNodeByState(pCxt, (SStateWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
891 892
    case QUERY_NODE_SESSION_WINDOW:
      return createWindowLogicNodeBySession(pCxt, (SSessionWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
893
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
894
      return createWindowLogicNodeByInterval(pCxt, (SIntervalWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
895 896
    case QUERY_NODE_EVENT_WINDOW:
      return createWindowLogicNodeByEvent(pCxt, (SEventWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
897 898 899 900
    default:
      break;
  }

901
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
902 903
}

904 905 906
static EDealRes needFillValueImpl(SNode* pNode, void* pContext) {
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    SColumnNode* pCol = (SColumnNode*)pNode;
907 908
    if (COLUMN_TYPE_WINDOW_START != pCol->colType && COLUMN_TYPE_WINDOW_END != pCol->colType &&
        COLUMN_TYPE_WINDOW_DURATION != pCol->colType && COLUMN_TYPE_GROUP_KEY != pCol->colType) {
909 910 911 912 913 914 915 916 917 918 919 920 921
      *(bool*)pContext = true;
      return DEAL_RES_END;
    }
  }
  return DEAL_RES_CONTINUE;
}

static bool needFillValue(SNode* pNode) {
  bool hasFillCol = false;
  nodesWalkExpr(pNode, needFillValueImpl, &hasFillCol);
  return hasFillCol;
}

X
Xiaoyu Wang 已提交
922
static int32_t partFillExprs(SSelectStmt* pSelect, SNodeList** pFillExprs, SNodeList** pNotFillExprs) {
923 924
  int32_t code = TSDB_CODE_SUCCESS;
  SNode*  pProject = NULL;
X
Xiaoyu Wang 已提交
925
  FOREACH(pProject, pSelect->pProjectionList) {
926 927
    if (needFillValue(pProject)) {
      code = nodesListMakeStrictAppend(pFillExprs, nodesCloneNode(pProject));
X
Xiaoyu Wang 已提交
928
    } else if (QUERY_NODE_VALUE != nodeType(pProject)) {
929 930 931 932 933 934 935 936
      code = nodesListMakeStrictAppend(pNotFillExprs, nodesCloneNode(pProject));
    }
    if (TSDB_CODE_SUCCESS != code) {
      NODES_DESTORY_LIST(*pFillExprs);
      NODES_DESTORY_LIST(*pNotFillExprs);
      break;
    }
  }
X
Xiaoyu Wang 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
  if (!pSelect->isDistinct) {
    SNode* pOrderExpr = NULL;
    FOREACH(pOrderExpr, pSelect->pOrderByList) {
      SNode* pExpr = ((SOrderByExprNode*)pOrderExpr)->pExpr;
      if (needFillValue(pExpr)) {
        code = nodesListMakeStrictAppend(pFillExprs, nodesCloneNode(pExpr));
      } else if (QUERY_NODE_VALUE != nodeType(pExpr)) {
        code = nodesListMakeStrictAppend(pNotFillExprs, nodesCloneNode(pExpr));
      }
      if (TSDB_CODE_SUCCESS != code) {
        NODES_DESTORY_LIST(*pFillExprs);
        NODES_DESTORY_LIST(*pNotFillExprs);
        break;
      }
    }
  }
953 954 955
  return code;
}

X
Xiaoyu Wang 已提交
956 957 958 959 960 961 962
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);
963 964 965
  if (FILL_MODE_NONE == pFillNode->mode) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
966

967
  SFillLogicNode* pFill = (SFillLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_FILL);
X
Xiaoyu Wang 已提交
968 969 970 971
  if (NULL == pFill) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

972
  pFill->node.groupAction = getGroupAction(pCxt, pSelect);
973 974
  pFill->node.requireDataOrder = getRequireDataOrder(true, pSelect);
  pFill->node.resultDataOrder = pFill->node.requireDataOrder;
975
  pFill->node.inputTsOrder = 0;
976

X
Xiaoyu Wang 已提交
977
  int32_t code = partFillExprs(pSelect, &pFill->pFillExprs, &pFill->pNotFillExprs);
978
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
979
    code = rewriteExprsForSelect(pFill->pFillExprs, pSelect, SQL_CLAUSE_FILL, NULL);
980 981
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
982
    code = rewriteExprsForSelect(pFill->pNotFillExprs, pSelect, SQL_CLAUSE_FILL, NULL);
983 984 985 986 987 988
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = createColumnByRewriteExprs(pFill->pFillExprs, &pFill->node.pTargets);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = createColumnByRewriteExprs(pFill->pNotFillExprs, &pFill->node.pTargets);
989
  }
X
Xiaoyu Wang 已提交
990 991

  pFill->mode = pFillNode->mode;
X
Xiaoyu Wang 已提交
992
  pFill->timeRange = pFillNode->timeRange;
X
Xiaoyu Wang 已提交
993 994 995 996 997 998
  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;
  }

X
Xiaoyu Wang 已提交
999 1000 1001 1002
  if (TSDB_CODE_SUCCESS == code && 0 == LIST_LENGTH(pFill->node.pTargets)) {
    code = createColumnByRewriteExpr(pFill->pWStartTs, &pFill->node.pTargets);
  }

X
Xiaoyu Wang 已提交
1003 1004 1005
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pFill;
  } else {
1006
    nodesDestroyNode((SNode*)pFill);
X
Xiaoyu Wang 已提交
1007 1008 1009 1010 1011
  }

  return code;
}

1012 1013 1014 1015 1016 1017 1018 1019
static bool isPrimaryKeySort(SNodeList* pOrderByList) {
  SNode* pExpr = ((SOrderByExprNode*)nodesListGetNode(pOrderByList, 0))->pExpr;
  if (QUERY_NODE_COLUMN != nodeType(pExpr)) {
    return false;
  }
  return PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pExpr)->colId;
}

X
Xiaoyu Wang 已提交
1020 1021 1022 1023 1024
static int32_t createSortLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pOrderByList) {
    return TSDB_CODE_SUCCESS;
  }

1025
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
1026 1027 1028 1029
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1030
  pSort->groupSort = pSelect->groupSort;
1031 1032
  pSort->node.groupAction = pSort->groupSort ? GROUP_ACTION_KEEP : GROUP_ACTION_CLEAR;
  pSort->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
1033 1034 1035
  pSort->node.resultDataOrder = isPrimaryKeySort(pSelect->pOrderByList)
                                    ? (pSort->groupSort ? DATA_ORDER_LEVEL_IN_GROUP : DATA_ORDER_LEVEL_GLOBAL)
                                    : DATA_ORDER_LEVEL_NONE;
1036

1037
  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_ORDER_BY, NULL, COLLECT_COL_TYPE_ALL, &pSort->node.pTargets);
1038 1039 1040 1041
  if (TSDB_CODE_SUCCESS == code && NULL == pSort->node.pTargets) {
    code = nodesListMakeStrictAppend(&pSort->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
  }
X
Xiaoyu Wang 已提交
1042 1043 1044 1045 1046 1047

  if (TSDB_CODE_SUCCESS == code) {
    pSort->pSortKeys = nodesCloneList(pSelect->pOrderByList);
    if (NULL == pSort->pSortKeys) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    SNode*            pNode = NULL;
    SOrderByExprNode* firstSortKey = (SOrderByExprNode*)nodesListGetNode(pSort->pSortKeys, 0);
    if (firstSortKey->pExpr->type == QUERY_NODE_COLUMN) {
      SColumnNode* pCol = (SColumnNode*)firstSortKey->pExpr;
      int16_t      projIdx = 1;
      FOREACH(pNode, pSelect->pProjectionList) {
        SExprNode* pExpr = (SExprNode*)pNode;
        if (0 == strcmp(pCol->node.aliasName, pExpr->aliasName)) {
          pCol->projIdx = projIdx; break;
        }
        projIdx++;
      }
    }
X
Xiaoyu Wang 已提交
1061 1062 1063 1064 1065
  }

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

  return code;
}

X
Xiaoyu Wang 已提交
1072 1073
static int32_t createColumnByProjections(SLogicPlanContext* pCxt, const char* pStmtName, SNodeList* pExprs,
                                         SNodeList** pCols) {
X
Xiaoyu Wang 已提交
1074
  SNodeList* pList = nodesMakeList();
X
Xiaoyu Wang 已提交
1075
  if (NULL == pList) {
1076 1077 1078
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
1079
  SNode* pNode;
X
Xiaoyu Wang 已提交
1080
  FOREACH(pNode, pExprs) {
1081
    if (TSDB_CODE_SUCCESS != nodesListAppend(pList, (SNode*)createColumnByExpr(pStmtName, (SExprNode*)pNode))) {
X
Xiaoyu Wang 已提交
1082 1083
      nodesDestroyList(pList);
      return TSDB_CODE_OUT_OF_MEMORY;
1084 1085
    }
  }
1086

X
Xiaoyu Wang 已提交
1087 1088
  *pCols = pList;
  return TSDB_CODE_SUCCESS;
1089 1090
}

X
Xiaoyu Wang 已提交
1091
static int32_t createProjectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1092
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
X
Xiaoyu Wang 已提交
1093
  if (NULL == pProject) {
1094 1095 1096
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
1097 1098
  TSWAP(pProject->node.pLimit, pSelect->pLimit);
  TSWAP(pProject->node.pSlimit, pSelect->pSlimit);
1099
  pProject->ignoreGroupId = pSelect->isSubquery ? true : (NULL == pSelect->pPartitionByList);
1100 1101
  pProject->node.groupAction =
      (!pSelect->isSubquery && pCxt->pPlanCxt->streamQuery) ? GROUP_ACTION_KEEP : GROUP_ACTION_CLEAR;
1102 1103
  pProject->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pProject->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;
1104

X
Xiaoyu Wang 已提交
1105
  int32_t code = TSDB_CODE_SUCCESS;
1106

X
Xiaoyu Wang 已提交
1107
  pProject->pProjections = nodesCloneList(pSelect->pProjectionList);
X
Xiaoyu Wang 已提交
1108 1109
  if (NULL == pProject->pProjections) {
    code = TSDB_CODE_OUT_OF_MEMORY;
1110
  }
X
Xiaoyu Wang 已提交
1111
  strcpy(pProject->stmtName, pSelect->stmtName);
1112 1113

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

1117
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1118 1119
    *pLogicNode = (SLogicNode*)pProject;
  } else {
1120
    nodesDestroyNode((SNode*)pProject);
1121
  }
1122

1123
  return code;
1124 1125
}

1126
static int32_t createPartitionLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
1127
  if (NULL == pSelect->pPartitionByList) {
1128 1129 1130
    return TSDB_CODE_SUCCESS;
  }

1131
  SPartitionLogicNode* pPartition = (SPartitionLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PARTITION);
1132 1133 1134 1135
  if (NULL == pPartition) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1136 1137 1138 1139
  pPartition->node.groupAction = GROUP_ACTION_SET;
  pPartition->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pPartition->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;

1140 1141
  int32_t code =
      nodesCollectColumns(pSelect, SQL_CLAUSE_PARTITION_BY, NULL, COLLECT_COL_TYPE_ALL, &pPartition->node.pTargets);
1142
  if (TSDB_CODE_SUCCESS == code && NULL == pPartition->node.pTargets) {
1143 1144
    code = nodesListMakeStrictAppend(&pPartition->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
1145
  }
1146 1147 1148 1149 1150 1151 1152 1153

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

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pTags) {
    pPartition->pTags = nodesCloneList(pSelect->pTags);
    if (NULL == pPartition->pTags) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pSubtable) {
    pPartition->pSubtable = nodesCloneNode(pSelect->pSubtable);
    if (NULL == pPartition->pSubtable) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

1168 1169 1170 1171 1172 1173 1174 1175
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pHaving && !pSelect->hasAggFuncs && NULL == pSelect->pGroupByList &&
      NULL == pSelect->pWindow) {
    pPartition->node.pConditions = nodesCloneNode(pSelect->pHaving);
    if (NULL == pPartition->node.pConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

1176 1177 1178
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pPartition;
  } else {
1179
    nodesDestroyNode((SNode*)pPartition);
1180 1181 1182
  }

  return code;
1183 1184 1185 1186 1187 1188 1189
}

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

1190 1191 1192 1193 1194
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

D
dapan1121 已提交
1195
  pAgg->node.groupAction = GROUP_ACTION_CLEAR;//getDistinctGroupAction(pCxt, pSelect);
1196 1197 1198
  pAgg->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pAgg->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;

1199 1200
  int32_t code = TSDB_CODE_SUCCESS;
  // set grouyp keys, agg funcs and having conditions
1201
  SNodeList* pGroupKeys = NULL;
1202
  SNode*     pProjection = NULL;
1203 1204 1205 1206 1207 1208 1209 1210 1211
  FOREACH(pProjection, pSelect->pProjectionList) {
    code = nodesListMakeStrictAppend(&pGroupKeys, createGroupingSetNode(pProjection));
    if (TSDB_CODE_SUCCESS != code) {
      nodesDestroyList(pGroupKeys);
      break;
    }
  }
  if (TSDB_CODE_SUCCESS == code) {
    pAgg->pGroupKeys = pGroupKeys;
1212 1213 1214 1215
  }

  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1216
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_DISTINCT, NULL);
1217 1218 1219 1220
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1221
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
1222 1223 1224 1225 1226
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1227
    nodesDestroyNode((SNode*)pAgg);
1228 1229 1230
  }

  return code;
1231 1232
}

1233 1234 1235 1236 1237 1238
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) {
1239
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1240
  int32_t     code = createLogicNodeByTable(pCxt, pSelect, pSelect->pFromTable, &pRoot);
1241
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1242
    code = createSelectRootLogicNode(pCxt, pSelect, createPartitionLogicNode, &pRoot);
1243
  }
1244
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1245
    code = createSelectRootLogicNode(pCxt, pSelect, createWindowLogicNode, &pRoot);
1246
  }
X
Xiaoyu Wang 已提交
1247
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1248
    code = createSelectRootLogicNode(pCxt, pSelect, createFillLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1249
  }
1250
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1251
    code = createSelectRootLogicNode(pCxt, pSelect, createAggLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1252
  }
1253
  if (TSDB_CODE_SUCCESS == code) {
1254
    code = createSelectRootLogicNode(pCxt, pSelect, createIndefRowsFuncLogicNode, &pRoot);
1255
  }
X
Xiaoyu Wang 已提交
1256 1257 1258
  if (TSDB_CODE_SUCCESS == code) {
    code = createSelectRootLogicNode(pCxt, pSelect, createInterpFuncLogicNode, &pRoot);
  }
1259
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1260
    code = createSelectRootLogicNode(pCxt, pSelect, createDistinctLogicNode, &pRoot);
1261
  }
X
Xiaoyu Wang 已提交
1262
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1263
    code = createSelectRootLogicNode(pCxt, pSelect, createSortLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1264
  }
1265
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1266
    code = createSelectRootLogicNode(pCxt, pSelect, createProjectLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1267
  }
1268 1269 1270 1271

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1272
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
1273
  }
1274 1275

  return code;
1276 1277
}

1278 1279 1280 1281 1282 1283 1284 1285
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 已提交
1286 1287 1288
static int32_t createSetOpRootLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, FCreateSetOpLogicNode func,
                                        SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSetOperator, pSetOperator->precision, (FCreateLogicNode)func, pRoot);
X
Xiaoyu Wang 已提交
1289 1290 1291 1292 1293 1294 1295
}

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

1296
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
1297 1298 1299 1300
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

X
Xiaoyu Wang 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
  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 {
1320
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
1321 1322 1323 1324 1325
  }

  return code;
}

X
Xiaoyu Wang 已提交
1326 1327
static int32_t createSetOpProjectLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                           SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1328 1329 1330 1331 1332
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
  if (NULL == pProject) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1333
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1334
    TSWAP(pProject->node.pLimit, pSetOperator->pLimit);
X
Xiaoyu Wang 已提交
1335
  }
1336
  pProject->ignoreGroupId = true;
X
Xiaoyu Wang 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345

  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) {
1346 1347
    code = createColumnByProjections(pCxt, pSetOperator->stmtName, pSetOperator->pProjectionList,
                                     &pProject->node.pTargets);
X
Xiaoyu Wang 已提交
1348 1349 1350 1351 1352
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pProject;
  } else {
1353
    nodesDestroyNode((SNode*)pProject);
X
Xiaoyu Wang 已提交
1354 1355 1356 1357 1358
  }

  return code;
}

X
Xiaoyu Wang 已提交
1359 1360 1361 1362 1363 1364
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;
  }

1365
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1366
    TSWAP(pAgg->node.pSlimit, pSetOperator->pLimit);
1367 1368
  }

X
Xiaoyu Wang 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
  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 已提交
1382
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1383 1384 1385 1386 1387
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1388
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1389 1390 1391 1392 1393
  }

  return code;
}

X
Xiaoyu Wang 已提交
1394 1395
static int32_t createSetOpLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, SLogicNode** pLogicNode) {
  SLogicNode* pSetOp = NULL;
X
Xiaoyu Wang 已提交
1396
  int32_t     code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1397 1398 1399 1400
  switch (pSetOperator->opType) {
    case SET_OP_TYPE_UNION_ALL:
      code = createSetOpProjectLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1401 1402 1403
    case SET_OP_TYPE_UNION:
      code = createSetOpAggLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1404
    default:
1405
      code = TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
      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) {
X
Xiaoyu Wang 已提交
1425
    pSetOp->precision = pSetOperator->precision;
X
Xiaoyu Wang 已提交
1426 1427
    *pLogicNode = (SLogicNode*)pSetOp;
  } else {
1428
    nodesDestroyNode((SNode*)pSetOp);
X
Xiaoyu Wang 已提交
1429 1430 1431 1432 1433
  }

  return code;
}

X
Xiaoyu Wang 已提交
1434 1435
static int32_t createSetOperatorLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                          SLogicNode** pLogicNode) {
1436
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1437
  int32_t     code = createSetOpLogicNode(pCxt, pSetOperator, &pRoot);
1438
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1439
    code = createSetOpRootLogicNode(pCxt, pSetOperator, createSetOpSortLogicNode, &pRoot);
1440 1441 1442 1443 1444
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1445
    nodesDestroyNode((SNode*)pRoot);
1446 1447 1448 1449 1450
  }

  return code;
}

1451
static int32_t getMsgType(ENodeType sqlType) {
X
Xiaoyu Wang 已提交
1452 1453
  switch (sqlType) {
    case QUERY_NODE_CREATE_TABLE_STMT:
X
Xiaoyu Wang 已提交
1454
    case QUERY_NODE_CREATE_MULTI_TABLES_STMT:
X
Xiaoyu Wang 已提交
1455 1456 1457
      return TDMT_VND_CREATE_TABLE;
    case QUERY_NODE_DROP_TABLE_STMT:
      return TDMT_VND_DROP_TABLE;
X
Xiaoyu Wang 已提交
1458 1459
    case QUERY_NODE_ALTER_TABLE_STMT:
      return TDMT_VND_ALTER_TABLE;
X
Xiaoyu Wang 已提交
1460 1461
    case QUERY_NODE_FLUSH_DATABASE_STMT:
      return TDMT_VND_COMMIT;
X
Xiaoyu Wang 已提交
1462 1463 1464 1465
    default:
      break;
  }
  return TDMT_VND_SUBMIT;
1466 1467
}

X
Xiaoyu Wang 已提交
1468
static int32_t createVnodeModifLogicNode(SLogicPlanContext* pCxt, SVnodeModifyOpStmt* pStmt, SLogicNode** pLogicNode) {
1469
  SVnodeModifyLogicNode* pModif = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
1470 1471 1472
  if (NULL == pModif) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1473
  pModif->modifyType = MODIFY_TABLE_TYPE_INSERT;
wafwerar's avatar
wafwerar 已提交
1474
  TSWAP(pModif->pDataBlocks, pStmt->pDataBlocks);
1475
  pModif->msgType = getMsgType(pStmt->sqlNodeType);
1476 1477
  *pLogicNode = (SLogicNode*)pModif;
  return TSDB_CODE_SUCCESS;
1478 1479
}

X
Xiaoyu Wang 已提交
1480 1481 1482 1483 1484 1485
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 已提交
1486
  SScanLogicNode* pScan = NULL;
G
Ganlin Zhao 已提交
1487
  int32_t          code = makeScanLogicNode(pCxt, (SRealTableNode*)pDelete->pFromTable, false, (SLogicNode**)&pScan);
X
Xiaoyu Wang 已提交
1488 1489 1490 1491

  // set columns to scan
  if (TSDB_CODE_SUCCESS == code) {
    pScan->scanType = SCAN_TYPE_TABLE;
D
dapan1121 已提交
1492
    pScan->scanRange = pDelete->timeRange;
X
Xiaoyu Wang 已提交
1493 1494 1495 1496 1497 1498
    pScan->pScanCols = nodesCloneList(((SFunctionNode*)pDelete->pCountFunc)->pParameterList);
    if (NULL == pScan->pScanCols) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

X
Xiaoyu Wang 已提交
1499 1500
  if (TSDB_CODE_SUCCESS == code && NULL != pDelete->pTagCond) {
    pScan->pTagCond = nodesCloneNode(pDelete->pTagCond);
X
Xiaoyu Wang 已提交
1501 1502 1503 1504 1505 1506 1507
    if (NULL == pScan->pTagCond) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1508
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
X
Xiaoyu Wang 已提交
1509 1510 1511 1512 1513
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
1514
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
1515 1516 1517
  }

  return code;
X
Xiaoyu Wang 已提交
1518 1519 1520
}

static int32_t createDeleteAggLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1521 1522 1523 1524 1525 1526
  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));
1527 1528 1529 1530 1531 1532
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesListStrictAppend(pAgg->pAggFuncs, nodesCloneNode(pDelete->pFirstFunc));
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesListStrictAppend(pAgg->pAggFuncs, nodesCloneNode(pDelete->pLastFunc));
  }
X
Xiaoyu Wang 已提交
1533 1534 1535
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExpr(pAgg->pAggFuncs, &pDelete->pCountFunc);
  }
1536 1537 1538 1539 1540 1541
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExpr(pAgg->pAggFuncs, &pDelete->pFirstFunc);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExpr(pAgg->pAggFuncs, &pDelete->pLastFunc);
  }
X
Xiaoyu Wang 已提交
1542 1543
  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1544
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1545 1546 1547 1548 1549
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1550
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1551 1552 1553
  }

  return code;
X
Xiaoyu Wang 已提交
1554 1555
}

X
Xiaoyu Wang 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
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;
1568
  snprintf(pModify->tableName, sizeof(pModify->tableName), "%s", pRealTable->table.tableName);
1569
  strcpy(pModify->tsColName, pRealTable->pMeta->schema->name);
X
Xiaoyu Wang 已提交
1570
  pModify->deleteTimeRange = pDelete->timeRange;
X
Xiaoyu Wang 已提交
1571
  pModify->pAffectedRows = nodesCloneNode(pDelete->pCountFunc);
1572 1573 1574
  pModify->pStartTs = nodesCloneNode(pDelete->pFirstFunc);
  pModify->pEndTs = nodesCloneNode(pDelete->pLastFunc);
  if (NULL == pModify->pAffectedRows || NULL == pModify->pStartTs || NULL == pModify->pEndTs) {
1575
    nodesDestroyNode((SNode*)pModify);
X
Xiaoyu Wang 已提交
1576 1577 1578 1579 1580
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  *pLogicNode = (SLogicNode*)pModify;
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1581 1582 1583 1584
}

static int32_t createDeleteLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1585
  int32_t     code = createDeleteScanLogicNode(pCxt, pDelete, &pRoot);
X
Xiaoyu Wang 已提交
1586 1587 1588 1589
  if (TSDB_CODE_SUCCESS == code) {
    code = createDeleteRootLogicNode(pCxt, pDelete, createDeleteAggLogicNode, &pRoot);
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1590
    code = createDeleteRootLogicNode(pCxt, pDelete, createVnodeModifLogicNodeByDelete, &pRoot);
X
Xiaoyu Wang 已提交
1591 1592 1593 1594 1595
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1596
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
1597 1598 1599 1600 1601
  }

  return code;
}

1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
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;
1618
  pModify->stableId = pRealTable->pMeta->suid;
1619
  pModify->tableType = pRealTable->pMeta->tableType;
1620
  snprintf(pModify->tableName, sizeof(pModify->tableName), "%s", pRealTable->table.tableName);
1621
  TSWAP(pModify->pVgroupList, pRealTable->pVgroupList);
1622 1623 1624 1625 1626
  pModify->pInsertCols = nodesCloneList(pInsert->pCols);
  if (NULL == pModify->pInsertCols) {
    nodesDestroyNode((SNode*)pModify);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

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

1648
static int32_t createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1649 1650
  switch (nodeType(pStmt)) {
    case QUERY_NODE_SELECT_STMT:
1651
      return createSelectLogicNode(pCxt, (SSelectStmt*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1652 1653
    case QUERY_NODE_VNODE_MODIFY_STMT:
      return createVnodeModifLogicNode(pCxt, (SVnodeModifyOpStmt*)pStmt, pLogicNode);
1654 1655
    case QUERY_NODE_EXPLAIN_STMT:
      return createQueryLogicNode(pCxt, ((SExplainStmt*)pStmt)->pQuery, pLogicNode);
1656 1657
    case QUERY_NODE_SET_OPERATOR:
      return createSetOperatorLogicNode(pCxt, (SSetOperator*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1658 1659
    case QUERY_NODE_DELETE_STMT:
      return createDeleteLogicNode(pCxt, (SDeleteStmt*)pStmt, pLogicNode);
1660 1661
    case QUERY_NODE_INSERT_STMT:
      return createInsertLogicNode(pCxt, (SInsertStmt*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1662 1663 1664
    default:
      break;
  }
1665
  return TSDB_CODE_FAILED;
1666 1667
}

X
Xiaoyu Wang 已提交
1668 1669 1670 1671 1672 1673 1674 1675
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); }

1676
static void setLogicSubplanType(bool hasScan, SLogicSubplan* pSubplan) {
1677
  if (QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY != nodeType(pSubplan->pNode)) {
1678
    pSubplan->subplanType = hasScan ? SUBPLAN_TYPE_SCAN : SUBPLAN_TYPE_MERGE;
1679 1680 1681 1682 1683 1684 1685 1686
  } 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 已提交
1687
int32_t createLogicPlan(SPlanContext* pCxt, SLogicSubplan** pLogicSubplan) {
1688
  SLogicPlanContext cxt = {.pPlanCxt = pCxt, .pCurrRoot = NULL, .hasScan = false};
X
Xiaoyu Wang 已提交
1689 1690 1691 1692

  SLogicSubplan* pSubplan = (SLogicSubplan*)nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN);
  if (NULL == pSubplan) {
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
1693
  }
X
Xiaoyu Wang 已提交
1694 1695 1696 1697 1698 1699 1700
  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);
1701
    setLogicSubplanType(cxt.hasScan, pSubplan);
1702
    code = adjustLogicNodeDataRequirement(pSubplan->pNode, DATA_ORDER_LEVEL_NONE);
X
Xiaoyu Wang 已提交
1703 1704 1705 1706 1707
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicSubplan = pSubplan;
  } else {
1708
    nodesDestroyNode((SNode*)pSubplan);
X
Xiaoyu Wang 已提交
1709 1710 1711
  }

  return code;
1712
}