planLogicCreater.c 55.6 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;
X
Xiaoyu Wang 已提交
40 41
} SRewriteExprCxt;

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

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

X
Xiaoyu Wang 已提交
122
  return DEAL_RES_CONTINUE;
X
Xiaoyu Wang 已提交
123 124
}

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

X
Xiaoyu Wang 已提交
139
  return DEAL_RES_CONTINUE;
140 141
}

D
dapan1121 已提交
142 143
static int32_t rewriteExprForSelect(SNode* pExpr, SSelectStmt* pSelect, ESqlClause clause) {
  nodesWalkExpr(pExpr, doNameExpr, NULL);
X
Xiaoyu Wang 已提交
144
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = NULL, .pOutputs = NULL};
D
dapan1121 已提交
145 146 147 148 149 150 151 152
  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 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
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 已提交
170
                                     SNodeList** pRewriteExprs) {
X
Xiaoyu Wang 已提交
171
  nodesWalkExprs(pExprs, doNameExpr, NULL);
X
Xiaoyu Wang 已提交
172
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs, .pOutputs = NULL};
X
Xiaoyu Wang 已提交
173
  if (NULL != pRewriteExprs) {
X
Xiaoyu Wang 已提交
174 175 176 177 178
    cxt.pOutputs = taosMemoryCalloc(LIST_LENGTH(pExprs), sizeof(bool));
    if (NULL == cxt.pOutputs) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
X
Xiaoyu Wang 已提交
179
  nodesRewriteSelectStmt(pSelect, clause, doRewriteExpr, &cxt);
X
Xiaoyu Wang 已提交
180 181
  if (TSDB_CODE_SUCCESS == cxt.errCode && NULL != pRewriteExprs) {
    cxt.errCode = cloneRewriteExprs(pExprs, cxt.pOutputs, pRewriteExprs);
X
Xiaoyu Wang 已提交
182 183
  }
  taosMemoryFree(cxt.pOutputs);
X
Xiaoyu Wang 已提交
184
  return cxt.errCode;
185 186
}

X
Xiaoyu Wang 已提交
187 188
static int32_t rewriteExpr(SNodeList* pExprs, SNode** pTarget) {
  nodesWalkExprs(pExprs, doNameExpr, NULL);
X
Xiaoyu Wang 已提交
189
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs, .pOutputs = NULL};
X
Xiaoyu Wang 已提交
190 191 192 193
  nodesRewriteExpr(pTarget, doRewriteExpr, &cxt);
  return cxt.errCode;
}

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

201 202 203 204 205
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 已提交
206
    }
207
  }
208 209
  if (TSDB_CODE_SUCCESS != nodesListAppend(pNewRoot->pChildren, (SNode*)*pOldRoot)) {
    return TSDB_CODE_OUT_OF_MEMORY;
210 211
  }

212 213
  (*pOldRoot)->pParent = pNewRoot;
  *pOldRoot = pNewRoot;
214

215 216
  return TSDB_CODE_SUCCESS;
}
217

X
Xiaoyu Wang 已提交
218 219
static int32_t createRootLogicNode(SLogicPlanContext* pCxt, void* pStmt, uint8_t precision, FCreateLogicNode func,
                                   SLogicNode** pRoot) {
X
Xiaoyu Wang 已提交
220
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
221
  int32_t     code = func(pCxt, pStmt, &pNode);
X
Xiaoyu Wang 已提交
222
  if (TSDB_CODE_SUCCESS == code && NULL != pNode) {
X
Xiaoyu Wang 已提交
223
    pNode->precision = precision;
X
Xiaoyu Wang 已提交
224
    code = pushLogicNode(pCxt, pRoot, pNode);
225
    pCxt->pCurrRoot = pNode;
226
  }
X
Xiaoyu Wang 已提交
227
  if (TSDB_CODE_SUCCESS != code) {
228
    nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
229
  }
X
Xiaoyu Wang 已提交
230
  return code;
231 232
}

X
Xiaoyu Wang 已提交
233 234 235 236
static int32_t createSelectRootLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, FCreateSelectLogicNode func,
                                         SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSelect, pSelect->precision, (FCreateLogicNode)func, pRoot);
}
237

238
static EScanType getScanType(SLogicPlanContext* pCxt, SNodeList* pScanPseudoCols, SNodeList* pScanCols,
239
                             int8_t tableType, bool tagScan) {
X
Xiaoyu Wang 已提交
240 241 242 243
  if (pCxt->pPlanCxt->topicQuery || pCxt->pPlanCxt->streamQuery) {
    return SCAN_TYPE_STREAM;
  }

244 245 246 247
  if (TSDB_SYSTEM_TABLE == tableType) {
    return SCAN_TYPE_SYSTEM_TABLE;
  }

248 249 250 251
  if (tagScan) {
    return SCAN_TYPE_TAG;
  }

X
Xiaoyu Wang 已提交
252
  if (NULL == pScanCols) {
253 254 255 256
    return NULL == pScanPseudoCols
               ? SCAN_TYPE_TABLE
               : ((FUNCTION_TYPE_BLOCK_DIST_INFO == ((SFunctionNode*)nodesListGetNode(pScanPseudoCols, 0))->funcType)
                      ? SCAN_TYPE_BLOCK_INFO
257
                      : SCAN_TYPE_TABLE);
X
Xiaoyu Wang 已提交
258 259
  }

260
  return SCAN_TYPE_TABLE;
X
Xiaoyu Wang 已提交
261 262
}

263
static SNode* createFirstCol(uint64_t tableId, const SSchema* pSchema) {
264
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
265 266 267
  if (NULL == pCol) {
    return NULL;
  }
268 269
  pCol->node.resType.type = pSchema->type;
  pCol->node.resType.bytes = pSchema->bytes;
270
  pCol->tableId = tableId;
271
  pCol->colId = pSchema->colId;
272
  pCol->colType = COLUMN_TYPE_COLUMN;
273
  strcpy(pCol->colName, pSchema->name);
274
  return (SNode*)pCol;
275 276
}

277
static int32_t addPrimaryKeyCol(uint64_t tableId, const SSchema* pSchema, SNodeList** pCols) {
X
Xiaoyu Wang 已提交
278
  bool   found = false;
279 280 281 282 283 284 285 286 287
  SNode* pCol = NULL;
  FOREACH(pCol, *pCols) {
    if (PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pCol)->colId) {
      found = true;
      break;
    }
  }

  if (!found) {
288
    return nodesListMakeStrictAppend(pCols, createFirstCol(tableId, pSchema));
289 290 291 292
  }
  return TSDB_CODE_SUCCESS;
}

293 294 295 296 297 298 299 300 301 302 303 304 305 306
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 已提交
307 308
static int32_t makeScanLogicNode(SLogicPlanContext* pCxt, SRealTableNode* pRealTable, bool hasRepeatScanFuncs,
                                 SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
309
  SScanLogicNode* pScan = (SScanLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SCAN);
X
Xiaoyu Wang 已提交
310
  if (NULL == pScan) {
311
    return TSDB_CODE_OUT_OF_MEMORY;
312 313
  }

wafwerar's avatar
wafwerar 已提交
314
  TSWAP(pScan->pVgroupList, pRealTable->pVgroupList);
X
Xiaoyu Wang 已提交
315
  TSWAP(pScan->pSmaIndexes, pRealTable->pSmaIndexes);
X
Xiaoyu Wang 已提交
316
  pScan->tableId = pRealTable->pMeta->uid;
X
Xiaoyu Wang 已提交
317
  pScan->stableId = pRealTable->pMeta->suid;
X
Xiaoyu Wang 已提交
318 319
  pScan->tableType = pRealTable->pMeta->tableType;
  pScan->scanSeq[0] = hasRepeatScanFuncs ? 2 : 1;
X
Xiaoyu Wang 已提交
320
  pScan->scanSeq[1] = 0;
X
Xiaoyu Wang 已提交
321
  pScan->scanRange = TSWINDOW_INITIALIZER;
X
Xiaoyu Wang 已提交
322 323 324 325
  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 已提交
326
  pScan->showRewrite = pCxt->pPlanCxt->showRewrite;
327
  pScan->ratio = pRealTable->ratio;
328
  pScan->dataRequired = FUNC_DATA_REQUIRED_DATA_LOAD;
329
  pScan->cacheLastMode = pRealTable->cacheLastMode;
330

X
Xiaoyu Wang 已提交
331 332 333 334 335 336 337 338 339 340
  *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);

341 342 343
  pScan->node.groupAction = GROUP_ACTION_NONE;
  pScan->node.resultDataOrder = DATA_ORDER_LEVEL_IN_BLOCK;

X
Xiaoyu Wang 已提交
344
  // set columns to scan
X
Xiaoyu Wang 已提交
345 346 347 348
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, COLLECT_COL_TYPE_COL,
                               &pScan->pScanCols);
  }
349

350
  if (TSDB_CODE_SUCCESS == code) {
351 352
    code = nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, COLLECT_COL_TYPE_TAG,
                               &pScan->pScanPseudoCols);
353 354 355
  }

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

359 360
  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
361
    code = rewriteExprsForSelect(pScan->pScanPseudoCols, pSelect, SQL_CLAUSE_FROM, NULL);
362 363
  }

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

366 367 368 369
  if (NULL != pScan->pScanCols) {
    pScan->hasNormalCols = true;
  }

370 371
  if (TSDB_CODE_SUCCESS == code) {
    code = addDefaultScanCol(pRealTable->pMeta, &pScan->pScanCols);
372 373
  }

374 375
  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
376
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
377 378
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
379
    code = createColumnByRewriteExprs(pScan->pScanPseudoCols, &pScan->node.pTargets);
380
  }
381

X
Xiaoyu Wang 已提交
382 383 384
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
385
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
386
  }
387

388 389
  pCxt->hasScan = true;

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

X
Xiaoyu Wang 已提交
393 394
static int32_t createSubqueryLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, STempTableNode* pTable,
                                       SLogicNode** pLogicNode) {
395
  return createQueryLogicNode(pCxt, pTable->pSubquery, pLogicNode);
396 397
}

X
Xiaoyu Wang 已提交
398 399
static int32_t createJoinLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SJoinTableNode* pJoinTable,
                                   SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
400
  SJoinLogicNode* pJoin = (SJoinLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_JOIN);
X
Xiaoyu Wang 已提交
401
  if (NULL == pJoin) {
402 403
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
404 405

  pJoin->joinType = pJoinTable->joinType;
X
Xiaoyu Wang 已提交
406
  pJoin->isSingleTableJoin = pJoinTable->table.singleTable;
407
  pJoin->inputTsOrder = ORDER_ASC;
408 409
  pJoin->node.groupAction = GROUP_ACTION_CLEAR;
  pJoin->node.requireDataOrder = DATA_ORDER_LEVEL_GLOBAL;
410
  pJoin->node.resultDataOrder = DATA_ORDER_LEVEL_GLOBAL;
X
Xiaoyu Wang 已提交
411

412
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
413 414 415

  // set left and right node
  pJoin->node.pChildren = nodesMakeList();
X
Xiaoyu Wang 已提交
416
  if (NULL == pJoin->node.pChildren) {
417
    code = TSDB_CODE_OUT_OF_MEMORY;
418 419
  }

X
Xiaoyu Wang 已提交
420
  SLogicNode* pLeft = NULL;
421
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
422 423 424 425
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pLeft, &pLeft);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pLeft);
    }
426
  }
427

X
Xiaoyu Wang 已提交
428
  SLogicNode* pRight = NULL;
429
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
430 431 432 433 434
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pRight, &pRight);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pRight);
    }
  }
X
Xiaoyu Wang 已提交
435 436

  // set on conditions
X
Xiaoyu Wang 已提交
437
  if (TSDB_CODE_SUCCESS == code && NULL != pJoinTable->pOnCond) {
X
Xiaoyu Wang 已提交
438
    pJoin->pOnConditions = nodesCloneNode(pJoinTable->pOnCond);
X
Xiaoyu Wang 已提交
439 440 441
    if (NULL == pJoin->pOnConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
442 443
  }

X
Xiaoyu Wang 已提交
444
  // set the output
X
Xiaoyu Wang 已提交
445 446
  if (TSDB_CODE_SUCCESS == code) {
    pJoin->node.pTargets = nodesCloneList(pLeft->pTargets);
D
dapan1121 已提交
447
    if (NULL == pJoin->node.pTargets) {
X
Xiaoyu Wang 已提交
448 449 450 451 452 453 454 455 456
      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;
457
  } else {
458
    nodesDestroyNode((SNode*)pJoin);
459 460
  }

461
  return code;
X
Xiaoyu Wang 已提交
462
}
463

X
Xiaoyu Wang 已提交
464 465
static int32_t doCreateLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                        SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
466 467
  switch (nodeType(pTable)) {
    case QUERY_NODE_REAL_TABLE:
468
      return createScanLogicNode(pCxt, pSelect, (SRealTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
469
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
470
      return createSubqueryLogicNode(pCxt, pSelect, (STempTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
471
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
472
      return createJoinLogicNode(pCxt, pSelect, (SJoinTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
473 474
    default:
      break;
475
  }
X
Xiaoyu Wang 已提交
476 477 478
  return TSDB_CODE_FAILED;
}

X
Xiaoyu Wang 已提交
479 480
static int32_t createLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                      SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
481
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
482
  int32_t     code = doCreateLogicNodeByTable(pCxt, pSelect, pTable, &pNode);
X
Xiaoyu Wang 已提交
483 484 485
  if (TSDB_CODE_SUCCESS == code) {
    pNode->pConditions = nodesCloneNode(pSelect->pWhere);
    if (NULL != pSelect->pWhere && NULL == pNode->pConditions) {
486
      nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
487 488
      return TSDB_CODE_OUT_OF_MEMORY;
    }
489
    pNode->precision = pSelect->precision;
X
Xiaoyu Wang 已提交
490
    *pLogicNode = pNode;
491
    pCxt->pCurrRoot = pNode;
X
Xiaoyu Wang 已提交
492 493 494 495
  }
  return code;
}

X
Xiaoyu Wang 已提交
496
static SColumnNode* createColumnByExpr(const char* pStmtName, SExprNode* pExpr) {
497
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
498 499 500 501
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType = pExpr->resType;
X
Xiaoyu Wang 已提交
502
  snprintf(pCol->colName, sizeof(pCol->colName), "%s", pExpr->aliasName);
X
Xiaoyu Wang 已提交
503
  if (NULL != pStmtName) {
X
Xiaoyu Wang 已提交
504
    snprintf(pCol->tableAlias, sizeof(pCol->tableAlias), "%s", pStmtName);
X
Xiaoyu Wang 已提交
505
  }
X
Xiaoyu Wang 已提交
506
  return pCol;
507 508
}

509 510 511 512 513 514 515 516 517 518 519 520 521
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;
}

522
static EGroupAction getGroupAction(SLogicPlanContext* pCxt, SSelectStmt* pSelect) {
X
Xiaoyu Wang 已提交
523 524
  return (pCxt->pPlanCxt->streamQuery || NULL != pSelect->pLimit || NULL != pSelect->pSlimit) ? GROUP_ACTION_KEEP
                                                                                              : GROUP_ACTION_NONE;
525 526
}

527 528 529 530 531
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;
}

532
static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
533
  if (!pSelect->hasAggFuncs && NULL == pSelect->pGroupByList) {
534
    return TSDB_CODE_SUCCESS;
535 536
  }

X
Xiaoyu Wang 已提交
537
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
538 539 540
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
541

542
  pAgg->hasLastRow = pSelect->hasLastRowFunc;
X
Xiaoyu Wang 已提交
543
  pAgg->hasLast = pSelect->hasLastFunc;
544
  pAgg->hasTimeLineFunc = pSelect->hasTimeLineFunc;
545
  pAgg->onlyHasKeepOrderFunc = pSelect->onlyHasKeepOrderFunc;
546
  pAgg->node.groupAction = getGroupAction(pCxt, pSelect);
547 548
  pAgg->node.requireDataOrder = getRequireDataOrder(pAgg->hasTimeLineFunc, pSelect);
  pAgg->node.resultDataOrder = pAgg->onlyHasKeepOrderFunc ? pAgg->node.requireDataOrder : DATA_ORDER_LEVEL_NONE;
549

X
Xiaoyu Wang 已提交
550 551
  int32_t code = TSDB_CODE_SUCCESS;

X
Xiaoyu Wang 已提交
552
  // set grouyp keys, agg funcs and having conditions
553
  if (TSDB_CODE_SUCCESS == code) {
554 555 556
    code = nodesCollectFuncs(pSelect, SQL_CLAUSE_GROUP_BY, fmIsAggFunc, &pAgg->pAggFuncs);
  }

X
Xiaoyu Wang 已提交
557
  // rewrite the expression in subsequent clauses
558
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
X
Xiaoyu Wang 已提交
559
    code = rewriteExprsForSelect(pAgg->pAggFuncs, pSelect, SQL_CLAUSE_GROUP_BY, NULL);
560 561 562
  }

  if (NULL != pSelect->pGroupByList) {
X
Xiaoyu Wang 已提交
563 564 565
    pAgg->pGroupKeys = nodesCloneList(pSelect->pGroupByList);
    if (NULL == pAgg->pGroupKeys) {
      code = TSDB_CODE_OUT_OF_MEMORY;
566
    }
567
  }
X
Xiaoyu Wang 已提交
568 569

  // rewrite the expression in subsequent clauses
X
Xiaoyu Wang 已提交
570
  SNodeList* pOutputGroupKeys = NULL;
571
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
572
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_GROUP_BY, &pOutputGroupKeys);
573
  }
574

575
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pHaving) {
X
Xiaoyu Wang 已提交
576
    pAgg->node.pConditions = nodesCloneNode(pSelect->pHaving);
577 578 579
    if (NULL == pAgg->node.pConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
580
  }
581

X
Xiaoyu Wang 已提交
582
  // set the output
583
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
X
Xiaoyu Wang 已提交
584
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
585 586
  }

X
Xiaoyu Wang 已提交
587 588 589 590 591 592 593 594 595
  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);

596 597 598
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
599
    nodesDestroyNode((SNode*)pAgg);
600 601 602
  }

  return code;
X
Xiaoyu Wang 已提交
603
}
604

605 606
static int32_t createIndefRowsFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  // top/bottom are both an aggregate function and a indefinite rows function
607
  if (!pSelect->hasIndefiniteRowsFunc || pSelect->hasAggFuncs || NULL != pSelect->pWindow) {
608 609 610 611 612 613 614 615 616
    return TSDB_CODE_SUCCESS;
  }

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

617
  pIdfRowsFunc->isTailFunc = pSelect->hasTailFunc;
618
  pIdfRowsFunc->isUniqueFunc = pSelect->hasUniqueFunc;
619
  pIdfRowsFunc->isTimeLineFunc = pSelect->hasTimeLineFunc;
620
  pIdfRowsFunc->node.groupAction = getGroupAction(pCxt, pSelect);
621
  pIdfRowsFunc->node.requireDataOrder = getRequireDataOrder(pIdfRowsFunc->isTimeLineFunc, pSelect);
622
  pIdfRowsFunc->node.resultDataOrder = pIdfRowsFunc->node.requireDataOrder;
623

X
Xiaoyu Wang 已提交
624 625
  // indefinite rows functions and _select_values functions
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, fmIsVectorFunc, &pIdfRowsFunc->pFuncs);
626
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
627
    code = rewriteExprsForSelect(pIdfRowsFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT, NULL);
628 629 630 631
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
632
    code = createColumnByRewriteExprs(pIdfRowsFunc->pFuncs, &pIdfRowsFunc->node.pTargets);
633 634 635 636 637
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pIdfRowsFunc;
  } else {
638
    nodesDestroyNode((SNode*)pIdfRowsFunc);
639 640 641 642 643
  }

  return code;
}

X
Xiaoyu Wang 已提交
644 645 646
static bool isInterpFunc(int32_t funcId) {
  return fmIsInterpFunc(funcId) || fmIsInterpPseudoColumnFunc(funcId) || fmIsGroupKeyFunc(funcId);
}
X
Xiaoyu Wang 已提交
647

X
Xiaoyu Wang 已提交
648 649 650 651 652 653 654 655 656 657
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;
  }

658
  pInterpFunc->node.groupAction = getGroupAction(pCxt, pSelect);
659
  pInterpFunc->node.requireDataOrder = getRequireDataOrder(true, pSelect);
660 661
  pInterpFunc->node.resultDataOrder = pInterpFunc->node.requireDataOrder;

X
Xiaoyu Wang 已提交
662
  // interp functions and _group_key functions
X
Xiaoyu Wang 已提交
663
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, isInterpFunc, &pInterpFunc->pFuncs);
X
Xiaoyu Wang 已提交
664
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
665
    code = rewriteExprsForSelect(pInterpFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT, NULL);
X
Xiaoyu Wang 已提交
666 667
  }

X
Xiaoyu Wang 已提交
668 669 670 671 672 673 674 675 676
  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 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  }

  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 已提交
697 698
static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SWindowLogicNode* pWindow,
                                             SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
699 700 701
  if (pCxt->pPlanCxt->streamQuery) {
    pWindow->triggerType = pCxt->pPlanCxt->triggerType;
    pWindow->watermark = pCxt->pPlanCxt->watermark;
702
    pWindow->igExpired = pCxt->pPlanCxt->igExpired;
5
54liuyao 已提交
703
  }
704
  pWindow->inputTsOrder = ORDER_ASC;
X
Xiaoyu Wang 已提交
705
  pWindow->outputTsOrder = ORDER_ASC;
5
54liuyao 已提交
706

707
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_WINDOW, fmIsWindowClauseFunc, &pWindow->pFuncs);
X
Xiaoyu Wang 已提交
708
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
709
    code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW, NULL);
X
Xiaoyu Wang 已提交
710 711 712
  }

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

X
Xiaoyu Wang 已提交
716 717
  pSelect->hasAggFuncs = false;

X
Xiaoyu Wang 已提交
718 719 720
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pWindow;
  } else {
721
    nodesDestroyNode((SNode*)pWindow);
X
Xiaoyu Wang 已提交
722 723 724 725 726
  }

  return code;
}

X
Xiaoyu Wang 已提交
727 728
static int32_t createWindowLogicNodeByState(SLogicPlanContext* pCxt, SStateWindowNode* pState, SSelectStmt* pSelect,
                                            SLogicNode** pLogicNode) {
729
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
730 731 732 733 734
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_STATE;
735
  pWindow->node.groupAction = getGroupAction(pCxt, pSelect);
736 737 738 739
  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;
740
  pWindow->pStateExpr = nodesCloneNode(pState->pExpr);
741
  pWindow->pTspk = nodesCloneNode(pState->pCol);
742
  if (NULL == pWindow->pStateExpr || NULL == pWindow->pTspk) {
743
    nodesDestroyNode((SNode*)pWindow);
744 745
    return TSDB_CODE_OUT_OF_MEMORY;
  }
D
dapan1121 已提交
746 747 748 749 750
  // 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 已提交
751

D
dapan1121 已提交
752
  return code;
753 754
}

X
Xiaoyu Wang 已提交
755 756
static int32_t createWindowLogicNodeBySession(SLogicPlanContext* pCxt, SSessionWindowNode* pSession,
                                              SSelectStmt* pSelect, SLogicNode** pLogicNode) {
757
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
X
Xiaoyu Wang 已提交
758 759 760 761 762 763
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_SESSION;
  pWindow->sessionGap = ((SValueNode*)pSession->pGap)->datum.i;
764
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? SESSION_ALGO_STREAM_SINGLE : SESSION_ALGO_MERGE;
765
  pWindow->node.groupAction = getGroupAction(pCxt, pSelect);
766 767 768 769
  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 已提交
770

771
  pWindow->pTspk = nodesCloneNode((SNode*)pSession->pCol);
772
  if (NULL == pWindow->pTspk) {
773
    nodesDestroyNode((SNode*)pWindow);
774 775
    return TSDB_CODE_OUT_OF_MEMORY;
  }
5
54liuyao 已提交
776
  pWindow->pTsEnd = nodesCloneNode((SNode*)pSession->pCol);
777

X
Xiaoyu Wang 已提交
778 779 780
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
781 782
static int32_t createWindowLogicNodeByInterval(SLogicPlanContext* pCxt, SIntervalWindowNode* pInterval,
                                               SSelectStmt* pSelect, SLogicNode** pLogicNode) {
783
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
784 785 786
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
787 788 789

  pWindow->winType = WINDOW_TYPE_INTERVAL;
  pWindow->interval = ((SValueNode*)pInterval->pInterval)->datum.i;
X
Xiaoyu Wang 已提交
790
  pWindow->intervalUnit = ((SValueNode*)pInterval->pInterval)->unit;
X
Xiaoyu Wang 已提交
791
  pWindow->offset = (NULL != pInterval->pOffset ? ((SValueNode*)pInterval->pOffset)->datum.i : 0);
792
  pWindow->sliding = (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->datum.i : pWindow->interval);
X
Xiaoyu Wang 已提交
793 794
  pWindow->slidingUnit =
      (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->unit : pWindow->intervalUnit);
795
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? INTERVAL_ALGO_STREAM_SINGLE : INTERVAL_ALGO_HASH;
796
  pWindow->node.groupAction = (NULL != pInterval->pFill ? GROUP_ACTION_KEEP : getGroupAction(pCxt, pSelect));
797 798 799 800 801 802
  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);
803

X
bugfix  
Xiaoyu Wang 已提交
804 805
  pWindow->pTspk = nodesCloneNode(pInterval->pCol);
  if (NULL == pWindow->pTspk) {
806
    nodesDestroyNode((SNode*)pWindow);
X
bugfix  
Xiaoyu Wang 已提交
807 808 809
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
810
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
X
Xiaoyu Wang 已提交
811 812
}

813
static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
814
  if (NULL == pSelect->pWindow) {
815
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
816 817 818
  }

  switch (nodeType(pSelect->pWindow)) {
819 820
    case QUERY_NODE_STATE_WINDOW:
      return createWindowLogicNodeByState(pCxt, (SStateWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
821 822
    case QUERY_NODE_SESSION_WINDOW:
      return createWindowLogicNodeBySession(pCxt, (SSessionWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
823
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
824
      return createWindowLogicNodeByInterval(pCxt, (SIntervalWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
825 826 827 828
    default:
      break;
  }

829
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
830 831
}

832 833 834
static EDealRes needFillValueImpl(SNode* pNode, void* pContext) {
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    SColumnNode* pCol = (SColumnNode*)pNode;
835 836
    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) {
837 838 839 840 841 842 843 844 845 846 847 848 849
      *(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 已提交
850
static int32_t partFillExprs(SSelectStmt* pSelect, SNodeList** pFillExprs, SNodeList** pNotFillExprs) {
851 852
  int32_t code = TSDB_CODE_SUCCESS;
  SNode*  pProject = NULL;
X
Xiaoyu Wang 已提交
853
  FOREACH(pProject, pSelect->pProjectionList) {
854 855
    if (needFillValue(pProject)) {
      code = nodesListMakeStrictAppend(pFillExprs, nodesCloneNode(pProject));
X
Xiaoyu Wang 已提交
856
    } else if (QUERY_NODE_VALUE != nodeType(pProject)) {
857 858 859 860 861 862 863 864
      code = nodesListMakeStrictAppend(pNotFillExprs, nodesCloneNode(pProject));
    }
    if (TSDB_CODE_SUCCESS != code) {
      NODES_DESTORY_LIST(*pFillExprs);
      NODES_DESTORY_LIST(*pNotFillExprs);
      break;
    }
  }
X
Xiaoyu Wang 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
  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;
      }
    }
  }
881 882 883
  return code;
}

X
Xiaoyu Wang 已提交
884 885 886 887 888 889 890
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);
891 892 893
  if (FILL_MODE_NONE == pFillNode->mode) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
894

895
  SFillLogicNode* pFill = (SFillLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_FILL);
X
Xiaoyu Wang 已提交
896 897 898 899
  if (NULL == pFill) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

900
  pFill->node.groupAction = getGroupAction(pCxt, pSelect);
901 902
  pFill->node.requireDataOrder = getRequireDataOrder(true, pSelect);
  pFill->node.resultDataOrder = pFill->node.requireDataOrder;
X
Xiaoyu Wang 已提交
903
  pFill->inputTsOrder = ORDER_ASC;
904

X
Xiaoyu Wang 已提交
905
  int32_t code = partFillExprs(pSelect, &pFill->pFillExprs, &pFill->pNotFillExprs);
906
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
907
    code = rewriteExprsForSelect(pFill->pFillExprs, pSelect, SQL_CLAUSE_FILL, NULL);
908 909
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
910
    code = rewriteExprsForSelect(pFill->pNotFillExprs, pSelect, SQL_CLAUSE_FILL, NULL);
911 912 913 914 915 916
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = createColumnByRewriteExprs(pFill->pFillExprs, &pFill->node.pTargets);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = createColumnByRewriteExprs(pFill->pNotFillExprs, &pFill->node.pTargets);
917
  }
X
Xiaoyu Wang 已提交
918 919

  pFill->mode = pFillNode->mode;
X
Xiaoyu Wang 已提交
920
  pFill->timeRange = pFillNode->timeRange;
X
Xiaoyu Wang 已提交
921 922 923 924 925 926 927 928 929
  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 {
930
    nodesDestroyNode((SNode*)pFill);
X
Xiaoyu Wang 已提交
931 932 933 934 935
  }

  return code;
}

936 937 938 939 940 941 942 943
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 已提交
944 945 946 947 948
static int32_t createSortLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pOrderByList) {
    return TSDB_CODE_SUCCESS;
  }

949
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
950 951 952 953
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

954
  pSort->groupSort = pSelect->groupSort;
955 956
  pSort->node.groupAction = pSort->groupSort ? GROUP_ACTION_KEEP : GROUP_ACTION_CLEAR;
  pSort->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
957 958 959
  pSort->node.resultDataOrder = isPrimaryKeySort(pSelect->pOrderByList)
                                    ? (pSort->groupSort ? DATA_ORDER_LEVEL_IN_GROUP : DATA_ORDER_LEVEL_GLOBAL)
                                    : DATA_ORDER_LEVEL_NONE;
960

961
  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_ORDER_BY, NULL, COLLECT_COL_TYPE_ALL, &pSort->node.pTargets);
962 963 964 965
  if (TSDB_CODE_SUCCESS == code && NULL == pSort->node.pTargets) {
    code = nodesListMakeStrictAppend(&pSort->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
  }
X
Xiaoyu Wang 已提交
966 967 968 969 970 971 972 973 974 975 976

  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 {
977
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
978 979 980 981 982
  }

  return code;
}

X
Xiaoyu Wang 已提交
983 984
static int32_t createColumnByProjections(SLogicPlanContext* pCxt, const char* pStmtName, SNodeList* pExprs,
                                         SNodeList** pCols) {
X
Xiaoyu Wang 已提交
985
  SNodeList* pList = nodesMakeList();
X
Xiaoyu Wang 已提交
986
  if (NULL == pList) {
987 988 989
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
990
  SNode* pNode;
X
Xiaoyu Wang 已提交
991
  FOREACH(pNode, pExprs) {
992
    if (TSDB_CODE_SUCCESS != nodesListAppend(pList, (SNode*)createColumnByExpr(pStmtName, (SExprNode*)pNode))) {
X
Xiaoyu Wang 已提交
993 994
      nodesDestroyList(pList);
      return TSDB_CODE_OUT_OF_MEMORY;
995 996
    }
  }
997

X
Xiaoyu Wang 已提交
998 999
  *pCols = pList;
  return TSDB_CODE_SUCCESS;
1000 1001
}

X
Xiaoyu Wang 已提交
1002
static int32_t createProjectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1003
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
X
Xiaoyu Wang 已提交
1004
  if (NULL == pProject) {
1005 1006 1007
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
1008 1009
  TSWAP(pProject->node.pLimit, pSelect->pLimit);
  TSWAP(pProject->node.pSlimit, pSelect->pSlimit);
1010
  pProject->ignoreGroupId = (NULL == pSelect->pPartitionByList);
1011 1012
  pProject->node.groupAction =
      (!pSelect->isSubquery && pCxt->pPlanCxt->streamQuery) ? GROUP_ACTION_KEEP : GROUP_ACTION_CLEAR;
1013 1014
  pProject->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pProject->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;
1015

X
Xiaoyu Wang 已提交
1016
  int32_t code = TSDB_CODE_SUCCESS;
1017

X
Xiaoyu Wang 已提交
1018
  pProject->pProjections = nodesCloneList(pSelect->pProjectionList);
X
Xiaoyu Wang 已提交
1019 1020
  if (NULL == pProject->pProjections) {
    code = TSDB_CODE_OUT_OF_MEMORY;
1021
  }
X
Xiaoyu Wang 已提交
1022
  strcpy(pProject->stmtName, pSelect->stmtName);
1023 1024

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

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

1034
  return code;
1035 1036
}

1037
static int32_t createPartitionLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
1038
  if (NULL == pSelect->pPartitionByList) {
1039 1040 1041
    return TSDB_CODE_SUCCESS;
  }

1042
  SPartitionLogicNode* pPartition = (SPartitionLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PARTITION);
1043 1044 1045 1046
  if (NULL == pPartition) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1047 1048 1049 1050
  pPartition->node.groupAction = GROUP_ACTION_SET;
  pPartition->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pPartition->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;

1051 1052
  int32_t code =
      nodesCollectColumns(pSelect, SQL_CLAUSE_PARTITION_BY, NULL, COLLECT_COL_TYPE_ALL, &pPartition->node.pTargets);
1053
  if (TSDB_CODE_SUCCESS == code && NULL == pPartition->node.pTargets) {
1054 1055
    code = nodesListMakeStrictAppend(&pPartition->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
1056
  }
1057 1058 1059 1060 1061 1062 1063 1064

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

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
  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;
    }
  }

1079 1080 1081
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pPartition;
  } else {
1082
    nodesDestroyNode((SNode*)pPartition);
1083 1084 1085
  }

  return code;
1086 1087 1088 1089 1090 1091 1092
}

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

1093 1094 1095 1096 1097
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1098
  pAgg->node.groupAction = GROUP_ACTION_CLEAR;
1099 1100 1101
  pAgg->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pAgg->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;

1102 1103
  int32_t code = TSDB_CODE_SUCCESS;
  // set grouyp keys, agg funcs and having conditions
1104
  SNodeList* pGroupKeys = NULL;
1105
  SNode*     pProjection = NULL;
1106 1107 1108 1109 1110 1111 1112 1113 1114
  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;
1115 1116 1117 1118
  }

  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1119
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_DISTINCT, NULL);
1120 1121 1122 1123
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1124
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
1125 1126 1127 1128 1129
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1130
    nodesDestroyNode((SNode*)pAgg);
1131 1132 1133
  }

  return code;
1134 1135
}

1136 1137 1138 1139 1140 1141
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) {
1142
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1143
  int32_t     code = createLogicNodeByTable(pCxt, pSelect, pSelect->pFromTable, &pRoot);
1144
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1145
    code = createSelectRootLogicNode(pCxt, pSelect, createPartitionLogicNode, &pRoot);
1146
  }
1147
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1148
    code = createSelectRootLogicNode(pCxt, pSelect, createWindowLogicNode, &pRoot);
1149
  }
X
Xiaoyu Wang 已提交
1150
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1151
    code = createSelectRootLogicNode(pCxt, pSelect, createFillLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1152
  }
1153
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1154
    code = createSelectRootLogicNode(pCxt, pSelect, createAggLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1155
  }
1156
  if (TSDB_CODE_SUCCESS == code) {
1157
    code = createSelectRootLogicNode(pCxt, pSelect, createIndefRowsFuncLogicNode, &pRoot);
1158
  }
X
Xiaoyu Wang 已提交
1159 1160 1161
  if (TSDB_CODE_SUCCESS == code) {
    code = createSelectRootLogicNode(pCxt, pSelect, createInterpFuncLogicNode, &pRoot);
  }
1162
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1163
    code = createSelectRootLogicNode(pCxt, pSelect, createDistinctLogicNode, &pRoot);
1164
  }
X
Xiaoyu Wang 已提交
1165
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1166
    code = createSelectRootLogicNode(pCxt, pSelect, createSortLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1167
  }
1168
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1169
    code = createSelectRootLogicNode(pCxt, pSelect, createProjectLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
1170
  }
1171 1172 1173 1174

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1175
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
1176
  }
1177 1178

  return code;
1179 1180
}

1181 1182 1183 1184 1185 1186 1187 1188
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 已提交
1189 1190 1191
static int32_t createSetOpRootLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, FCreateSetOpLogicNode func,
                                        SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSetOperator, pSetOperator->precision, (FCreateLogicNode)func, pRoot);
X
Xiaoyu Wang 已提交
1192 1193 1194 1195 1196 1197 1198
}

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

1199
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
1200 1201 1202 1203
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

X
Xiaoyu Wang 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
  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 {
1223
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
1224 1225 1226 1227 1228
  }

  return code;
}

X
Xiaoyu Wang 已提交
1229 1230
static int32_t createSetOpProjectLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                           SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1231 1232 1233 1234 1235
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
  if (NULL == pProject) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1236
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1237
    TSWAP(pProject->node.pLimit, pSetOperator->pLimit);
X
Xiaoyu Wang 已提交
1238
  }
1239
  pProject->ignoreGroupId = true;
X
Xiaoyu Wang 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248

  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) {
1249 1250
    code = createColumnByProjections(pCxt, pSetOperator->stmtName, pSetOperator->pProjectionList,
                                     &pProject->node.pTargets);
X
Xiaoyu Wang 已提交
1251 1252 1253 1254 1255
  }

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

  return code;
}

X
Xiaoyu Wang 已提交
1262 1263 1264 1265 1266 1267
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;
  }

1268
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1269
    TSWAP(pAgg->node.pSlimit, pSetOperator->pLimit);
1270 1271
  }

X
Xiaoyu Wang 已提交
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
  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 已提交
1285
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1286 1287 1288 1289 1290
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1291
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1292 1293 1294 1295 1296
  }

  return code;
}

X
Xiaoyu Wang 已提交
1297 1298
static int32_t createSetOpLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, SLogicNode** pLogicNode) {
  SLogicNode* pSetOp = NULL;
X
Xiaoyu Wang 已提交
1299
  int32_t     code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1300 1301 1302 1303
  switch (pSetOperator->opType) {
    case SET_OP_TYPE_UNION_ALL:
      code = createSetOpProjectLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1304 1305 1306
    case SET_OP_TYPE_UNION:
      code = createSetOpAggLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1307
    default:
1308
      code = TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
      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 {
1330
    nodesDestroyNode((SNode*)pSetOp);
X
Xiaoyu Wang 已提交
1331 1332 1333 1334 1335
  }

  return code;
}

X
Xiaoyu Wang 已提交
1336 1337
static int32_t createSetOperatorLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                          SLogicNode** pLogicNode) {
1338
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1339
  int32_t     code = createSetOpLogicNode(pCxt, pSetOperator, &pRoot);
1340
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1341
    code = createSetOpRootLogicNode(pCxt, pSetOperator, createSetOpSortLogicNode, &pRoot);
1342 1343 1344 1345 1346
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1347
    nodesDestroyNode((SNode*)pRoot);
1348 1349 1350 1351 1352
  }

  return code;
}

1353
static int32_t getMsgType(ENodeType sqlType) {
X
Xiaoyu Wang 已提交
1354 1355 1356 1357 1358 1359
  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 已提交
1360 1361
    case QUERY_NODE_ALTER_TABLE_STMT:
      return TDMT_VND_ALTER_TABLE;
X
Xiaoyu Wang 已提交
1362 1363
    case QUERY_NODE_FLUSH_DATABASE_STMT:
      return TDMT_VND_COMMIT;
X
Xiaoyu Wang 已提交
1364 1365 1366 1367
    default:
      break;
  }
  return TDMT_VND_SUBMIT;
1368 1369
}

1370
static int32_t createVnodeModifLogicNode(SLogicPlanContext* pCxt, SVnodeModifOpStmt* pStmt, SLogicNode** pLogicNode) {
1371
  SVnodeModifyLogicNode* pModif = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
1372 1373 1374
  if (NULL == pModif) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1375
  pModif->modifyType = MODIFY_TABLE_TYPE_INSERT;
wafwerar's avatar
wafwerar 已提交
1376
  TSWAP(pModif->pDataBlocks, pStmt->pDataBlocks);
1377
  pModif->msgType = getMsgType(pStmt->sqlNodeType);
1378 1379
  *pLogicNode = (SLogicNode*)pModif;
  return TSDB_CODE_SUCCESS;
1380 1381
}

X
Xiaoyu Wang 已提交
1382 1383 1384 1385 1386 1387
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 已提交
1388 1389 1390 1391 1392 1393
  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;
D
dapan1121 已提交
1394
    pScan->scanRange = pDelete->timeRange;
X
Xiaoyu Wang 已提交
1395 1396 1397 1398 1399 1400
    pScan->pScanCols = nodesCloneList(((SFunctionNode*)pDelete->pCountFunc)->pParameterList);
    if (NULL == pScan->pScanCols) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

X
Xiaoyu Wang 已提交
1401 1402
  if (TSDB_CODE_SUCCESS == code && NULL != pDelete->pTagCond) {
    pScan->pTagCond = nodesCloneNode(pDelete->pTagCond);
X
Xiaoyu Wang 已提交
1403 1404 1405 1406 1407 1408 1409
    if (NULL == pScan->pTagCond) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1410
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
X
Xiaoyu Wang 已提交
1411 1412 1413 1414 1415
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
1416
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
1417 1418 1419
  }

  return code;
X
Xiaoyu Wang 已提交
1420 1421 1422
}

static int32_t createDeleteAggLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1423 1424 1425 1426 1427 1428
  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));
1429 1430 1431 1432 1433 1434
  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 已提交
1435 1436 1437
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExpr(pAgg->pAggFuncs, &pDelete->pCountFunc);
  }
1438 1439 1440 1441 1442 1443
  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 已提交
1444 1445
  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1446
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1447 1448 1449 1450 1451
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1452
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1453 1454 1455
  }

  return code;
X
Xiaoyu Wang 已提交
1456 1457
}

X
Xiaoyu Wang 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
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;
1470
  snprintf(pModify->tableName, sizeof(pModify->tableName), "%s", pRealTable->table.tableName);
1471
  strcpy(pModify->tsColName, pRealTable->pMeta->schema->name);
X
Xiaoyu Wang 已提交
1472
  pModify->deleteTimeRange = pDelete->timeRange;
X
Xiaoyu Wang 已提交
1473
  pModify->pAffectedRows = nodesCloneNode(pDelete->pCountFunc);
1474 1475 1476
  pModify->pStartTs = nodesCloneNode(pDelete->pFirstFunc);
  pModify->pEndTs = nodesCloneNode(pDelete->pLastFunc);
  if (NULL == pModify->pAffectedRows || NULL == pModify->pStartTs || NULL == pModify->pEndTs) {
1477
    nodesDestroyNode((SNode*)pModify);
X
Xiaoyu Wang 已提交
1478 1479 1480 1481 1482
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  *pLogicNode = (SLogicNode*)pModify;
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1483 1484 1485 1486
}

static int32_t createDeleteLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1487
  int32_t     code = createDeleteScanLogicNode(pCxt, pDelete, &pRoot);
X
Xiaoyu Wang 已提交
1488 1489 1490 1491
  if (TSDB_CODE_SUCCESS == code) {
    code = createDeleteRootLogicNode(pCxt, pDelete, createDeleteAggLogicNode, &pRoot);
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1492
    code = createDeleteRootLogicNode(pCxt, pDelete, createVnodeModifLogicNodeByDelete, &pRoot);
X
Xiaoyu Wang 已提交
1493 1494 1495 1496 1497
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1498
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
1499 1500 1501 1502 1503
  }

  return code;
}

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
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;
1520
  pModify->stableId = pRealTable->pMeta->suid;
1521
  pModify->tableType = pRealTable->pMeta->tableType;
1522
  snprintf(pModify->tableName, sizeof(pModify->tableName), "%s", pRealTable->table.tableName);
1523
  TSWAP(pModify->pVgroupList, pRealTable->pVgroupList);
1524 1525 1526 1527 1528
  pModify->pInsertCols = nodesCloneList(pInsert->pCols);
  if (NULL == pModify->pInsertCols) {
    nodesDestroyNode((SNode*)pModify);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

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

1550
static int32_t createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1551 1552
  switch (nodeType(pStmt)) {
    case QUERY_NODE_SELECT_STMT:
1553
      return createSelectLogicNode(pCxt, (SSelectStmt*)pStmt, pLogicNode);
1554
    case QUERY_NODE_VNODE_MODIF_STMT:
1555
      return createVnodeModifLogicNode(pCxt, (SVnodeModifOpStmt*)pStmt, pLogicNode);
1556 1557
    case QUERY_NODE_EXPLAIN_STMT:
      return createQueryLogicNode(pCxt, ((SExplainStmt*)pStmt)->pQuery, pLogicNode);
1558 1559
    case QUERY_NODE_SET_OPERATOR:
      return createSetOperatorLogicNode(pCxt, (SSetOperator*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1560 1561
    case QUERY_NODE_DELETE_STMT:
      return createDeleteLogicNode(pCxt, (SDeleteStmt*)pStmt, pLogicNode);
1562 1563
    case QUERY_NODE_INSERT_STMT:
      return createInsertLogicNode(pCxt, (SInsertStmt*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1564 1565 1566
    default:
      break;
  }
1567
  return TSDB_CODE_FAILED;
1568 1569
}

X
Xiaoyu Wang 已提交
1570 1571 1572 1573 1574 1575 1576 1577
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); }

1578
static void setLogicSubplanType(bool hasScan, SLogicSubplan* pSubplan) {
1579
  if (QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY != nodeType(pSubplan->pNode)) {
1580
    pSubplan->subplanType = hasScan ? SUBPLAN_TYPE_SCAN : SUBPLAN_TYPE_MERGE;
1581 1582 1583 1584 1585 1586 1587 1588
  } 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 已提交
1589
int32_t createLogicPlan(SPlanContext* pCxt, SLogicSubplan** pLogicSubplan) {
1590
  SLogicPlanContext cxt = {.pPlanCxt = pCxt, .pCurrRoot = NULL, .hasScan = false};
X
Xiaoyu Wang 已提交
1591 1592 1593 1594

  SLogicSubplan* pSubplan = (SLogicSubplan*)nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN);
  if (NULL == pSubplan) {
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
1595
  }
X
Xiaoyu Wang 已提交
1596 1597 1598 1599 1600 1601 1602
  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);
1603
    setLogicSubplanType(cxt.hasScan, pSubplan);
1604
    code = adjustLogicNodeDataRequirement(pSubplan->pNode, DATA_ORDER_LEVEL_NONE);
X
Xiaoyu Wang 已提交
1605 1606 1607 1608 1609
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicSubplan = pSubplan;
  } else {
1610
    nodesDestroyNode((SNode*)pSubplan);
X
Xiaoyu Wang 已提交
1611 1612 1613
  }

  return code;
1614
}