planLogicCreater.c 53.8 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 39 40 41 42 43 44 45 46
  SNodeList* pExprs;
} SRewriteExprCxt;

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

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

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

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

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

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

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

122 123 124 125 126
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 已提交
127
    }
128
  }
129 130
  if (TSDB_CODE_SUCCESS != nodesListAppend(pNewRoot->pChildren, (SNode*)*pOldRoot)) {
    return TSDB_CODE_OUT_OF_MEMORY;
131 132
  }

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

136 137
  return TSDB_CODE_SUCCESS;
}
138

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

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

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

165 166 167 168
  if (TSDB_SYSTEM_TABLE == tableType) {
    return SCAN_TYPE_SYSTEM_TABLE;
  }

X
Xiaoyu Wang 已提交
169
  if (NULL == pScanCols) {
170 171 172 173
    return NULL == pScanPseudoCols
               ? SCAN_TYPE_TABLE
               : ((FUNCTION_TYPE_BLOCK_DIST_INFO == ((SFunctionNode*)nodesListGetNode(pScanPseudoCols, 0))->funcType)
                      ? SCAN_TYPE_BLOCK_INFO
174
                      : SCAN_TYPE_TABLE);
X
Xiaoyu Wang 已提交
175 176
  }

177
  return SCAN_TYPE_TABLE;
X
Xiaoyu Wang 已提交
178 179
}

180 181
static SNode* createPrimaryKeyCol(uint64_t tableId) {
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
182 183 184 185 186 187 188 189 190
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP;
  pCol->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes;
  pCol->tableId = tableId;
  pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
  pCol->colType = COLUMN_TYPE_COLUMN;
  strcpy(pCol->colName, "#primarykey");
191
  return (SNode*)pCol;
192 193 194 195 196 197 198 199 200 201
}

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

X
Xiaoyu Wang 已提交
202
  bool   found = false;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  SNode* pCol = NULL;
  FOREACH(pCol, *pCols) {
    if (PRIMARYKEY_TIMESTAMP_COL_ID == ((SColumnNode*)pCol)->colId) {
      found = true;
      break;
    }
  }

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

X
Xiaoyu Wang 已提交
219 220
static int32_t makeScanLogicNode(SLogicPlanContext* pCxt, SRealTableNode* pRealTable, bool hasRepeatScanFuncs,
                                 SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
221
  SScanLogicNode* pScan = (SScanLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SCAN);
X
Xiaoyu Wang 已提交
222
  if (NULL == pScan) {
223
    return TSDB_CODE_OUT_OF_MEMORY;
224 225
  }

wafwerar's avatar
wafwerar 已提交
226
  TSWAP(pScan->pVgroupList, pRealTable->pVgroupList);
X
Xiaoyu Wang 已提交
227
  TSWAP(pScan->pSmaIndexes, pRealTable->pSmaIndexes);
X
Xiaoyu Wang 已提交
228
  pScan->tableId = pRealTable->pMeta->uid;
X
Xiaoyu Wang 已提交
229
  pScan->stableId = pRealTable->pMeta->suid;
X
Xiaoyu Wang 已提交
230 231
  pScan->tableType = pRealTable->pMeta->tableType;
  pScan->scanSeq[0] = hasRepeatScanFuncs ? 2 : 1;
X
Xiaoyu Wang 已提交
232
  pScan->scanSeq[1] = 0;
X
Xiaoyu Wang 已提交
233
  pScan->scanRange = TSWINDOW_INITIALIZER;
X
Xiaoyu Wang 已提交
234 235 236 237
  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 已提交
238
  pScan->showRewrite = pCxt->pPlanCxt->showRewrite;
239
  pScan->ratio = pRealTable->ratio;
240
  pScan->dataRequired = FUNC_DATA_REQUIRED_DATA_LOAD;
241
  pScan->cacheLastMode = pRealTable->cacheLastMode;
242

X
Xiaoyu Wang 已提交
243 244 245 246 247 248 249 250 251 252
  *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);

253 254 255
  pScan->node.groupAction = GROUP_ACTION_NONE;
  pScan->node.resultDataOrder = DATA_ORDER_LEVEL_IN_BLOCK;

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

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

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

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

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

278 279 280 281
  if (NULL != pScan->pScanCols) {
    pScan->hasNormalCols = true;
  }

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

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

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

300 301
  pCxt->hasScan = true;

X
Xiaoyu Wang 已提交
302
  return code;
X
Xiaoyu Wang 已提交
303
}
304

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

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

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

  pJoin->joinType = pJoinTable->joinType;
X
Xiaoyu Wang 已提交
341
  pJoin->isSingleTableJoin = pJoinTable->table.singleTable;
342 343 344
  pJoin->node.groupAction = GROUP_ACTION_CLEAR;
  pJoin->node.requireDataOrder = DATA_ORDER_LEVEL_GLOBAL;
  pJoin->node.requireDataOrder = DATA_ORDER_LEVEL_GLOBAL;
X
Xiaoyu Wang 已提交
345

346
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
347 348 349

  // set left and right node
  pJoin->node.pChildren = nodesMakeList();
X
Xiaoyu Wang 已提交
350
  if (NULL == pJoin->node.pChildren) {
351
    code = TSDB_CODE_OUT_OF_MEMORY;
352 353
  }

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

X
Xiaoyu Wang 已提交
362
  SLogicNode* pRight = NULL;
363
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
364 365 366 367 368
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pRight, &pRight);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pRight);
    }
  }
X
Xiaoyu Wang 已提交
369 370

  // set on conditions
X
Xiaoyu Wang 已提交
371
  if (TSDB_CODE_SUCCESS == code && NULL != pJoinTable->pOnCond) {
X
Xiaoyu Wang 已提交
372
    pJoin->pOnConditions = nodesCloneNode(pJoinTable->pOnCond);
X
Xiaoyu Wang 已提交
373 374 375
    if (NULL == pJoin->pOnConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
376 377
  }

X
Xiaoyu Wang 已提交
378
  // set the output
X
Xiaoyu Wang 已提交
379 380
  if (TSDB_CODE_SUCCESS == code) {
    pJoin->node.pTargets = nodesCloneList(pLeft->pTargets);
D
dapan1121 已提交
381
    if (NULL == pJoin->node.pTargets) {
X
Xiaoyu Wang 已提交
382 383 384 385 386 387 388 389 390
      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;
391
  } else {
392
    nodesDestroyNode((SNode*)pJoin);
393 394
  }

395
  return code;
X
Xiaoyu Wang 已提交
396
}
397

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

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

X
Xiaoyu Wang 已提交
430
static SColumnNode* createColumnByExpr(const char* pStmtName, SExprNode* pExpr) {
431
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
432 433 434 435 436
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType = pExpr->resType;
  strcpy(pCol->colName, pExpr->aliasName);
X
Xiaoyu Wang 已提交
437 438 439
  if (NULL != pStmtName) {
    strcpy(pCol->tableAlias, pStmtName);
  }
X
Xiaoyu Wang 已提交
440
  return pCol;
441 442
}

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

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

470
static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
471
  if (!pSelect->hasAggFuncs && NULL == pSelect->pGroupByList) {
472
    return TSDB_CODE_SUCCESS;
473 474
  }

X
Xiaoyu Wang 已提交
475
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
476 477 478
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
479

480
  pAgg->hasLastRow = pSelect->hasLastRowFunc;
481 482 483
  pAgg->node.groupAction = GROUP_ACTION_SET;
  pAgg->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pAgg->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;
484

X
Xiaoyu Wang 已提交
485 486
  int32_t code = TSDB_CODE_SUCCESS;

X
Xiaoyu Wang 已提交
487
  // set grouyp keys, agg funcs and having conditions
488
  if (TSDB_CODE_SUCCESS == code) {
489 490 491
    code = nodesCollectFuncs(pSelect, SQL_CLAUSE_GROUP_BY, fmIsAggFunc, &pAgg->pAggFuncs);
  }

X
Xiaoyu Wang 已提交
492
  // rewrite the expression in subsequent clauses
493
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
494 495 496 497
    code = rewriteExprsForSelect(pAgg->pAggFuncs, pSelect, SQL_CLAUSE_GROUP_BY);
  }

  if (NULL != pSelect->pGroupByList) {
498 499 500 501 502 503 504
    if (NULL != pAgg->pGroupKeys) {
      code = nodesListStrictAppendList(pAgg->pGroupKeys, nodesCloneList(pSelect->pGroupByList));
    } else {
      pAgg->pGroupKeys = nodesCloneList(pSelect->pGroupByList);
      if (NULL == pAgg->pGroupKeys) {
        code = TSDB_CODE_OUT_OF_MEMORY;
      }
505
    }
506
  }
X
Xiaoyu Wang 已提交
507 508

  // rewrite the expression in subsequent clauses
509
  if (TSDB_CODE_SUCCESS == code) {
510
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_GROUP_BY);
511
  }
512

513
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pHaving) {
X
Xiaoyu Wang 已提交
514
    pAgg->node.pConditions = nodesCloneNode(pSelect->pHaving);
515 516 517
    if (NULL == pAgg->node.pConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
518
  }
519

X
Xiaoyu Wang 已提交
520
  // set the output
521
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pGroupKeys) {
X
Xiaoyu Wang 已提交
522
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
523 524
  }
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
X
Xiaoyu Wang 已提交
525
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
526 527 528 529 530
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
531
    nodesDestroyNode((SNode*)pAgg);
532 533 534
  }

  return code;
X
Xiaoyu Wang 已提交
535
}
536

537 538
static int32_t createIndefRowsFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  // top/bottom are both an aggregate function and a indefinite rows function
539
  if (!pSelect->hasIndefiniteRowsFunc || pSelect->hasAggFuncs || NULL != pSelect->pWindow) {
540 541 542 543 544 545 546 547 548
    return TSDB_CODE_SUCCESS;
  }

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

549
  pIdfRowsFunc->isTailFunc = pSelect->hasTailFunc;
550
  pIdfRowsFunc->isUniqueFunc = pSelect->hasUniqueFunc;
551
  pIdfRowsFunc->isTimeLineFunc = pSelect->hasTimeLineFunc;
552 553 554 555
  pIdfRowsFunc->node.groupAction = GROUP_ACTION_KEEP;
  pIdfRowsFunc->node.requireDataOrder =
      pIdfRowsFunc->isTimeLineFunc ? DATA_ORDER_LEVEL_IN_GROUP : DATA_ORDER_LEVEL_NONE;
  pIdfRowsFunc->node.resultDataOrder = pIdfRowsFunc->node.requireDataOrder;
556

X
Xiaoyu Wang 已提交
557 558
  // indefinite rows functions and _select_values functions
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, fmIsVectorFunc, &pIdfRowsFunc->pFuncs);
559
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
560
    code = rewriteExprsForSelect(pIdfRowsFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT);
561 562 563 564
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
565
    code = createColumnByRewriteExprs(pIdfRowsFunc->pFuncs, &pIdfRowsFunc->node.pTargets);
566 567 568 569 570
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pIdfRowsFunc;
  } else {
571
    nodesDestroyNode((SNode*)pIdfRowsFunc);
572 573 574 575 576
  }

  return code;
}

X
Xiaoyu Wang 已提交
577 578 579 580 581 582 583 584 585 586
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;
  }

587 588 589 590
  pInterpFunc->node.groupAction = GROUP_ACTION_KEEP;
  pInterpFunc->node.requireDataOrder = DATA_ORDER_LEVEL_IN_GROUP;
  pInterpFunc->node.resultDataOrder = pInterpFunc->node.requireDataOrder;

X
Xiaoyu Wang 已提交
591 592 593 594 595
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, fmIsInterpFunc, &pInterpFunc->pFuncs);
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExprsForSelect(pInterpFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT);
  }

X
Xiaoyu Wang 已提交
596 597 598 599 600 601 602 603 604
  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 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
  }

  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 已提交
625 626
static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SWindowLogicNode* pWindow,
                                             SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
627
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_WINDOW, fmIsWindowClauseFunc, &pWindow->pFuncs);
X
Xiaoyu Wang 已提交
628

X
Xiaoyu Wang 已提交
629 630 631
  if (pCxt->pPlanCxt->streamQuery) {
    pWindow->triggerType = pCxt->pPlanCxt->triggerType;
    pWindow->watermark = pCxt->pPlanCxt->watermark;
632
    pWindow->igExpired = pCxt->pPlanCxt->igExpired;
5
54liuyao 已提交
633 634
  }

X
Xiaoyu Wang 已提交
635
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
636
    code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW);
X
Xiaoyu Wang 已提交
637 638 639
  }

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

X
Xiaoyu Wang 已提交
643 644
  pSelect->hasAggFuncs = false;

X
Xiaoyu Wang 已提交
645 646 647
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pWindow;
  } else {
648
    nodesDestroyNode((SNode*)pWindow);
X
Xiaoyu Wang 已提交
649 650 651 652 653
  }

  return code;
}

X
Xiaoyu Wang 已提交
654 655
static int32_t createWindowLogicNodeByState(SLogicPlanContext* pCxt, SStateWindowNode* pState, SSelectStmt* pSelect,
                                            SLogicNode** pLogicNode) {
656
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
657 658 659 660 661
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_STATE;
662 663 664
  pWindow->node.groupAction = GROUP_ACTION_KEEP;
  pWindow->node.requireDataOrder = DATA_ORDER_LEVEL_IN_GROUP;
  pWindow->node.resultDataOrder = DATA_ORDER_LEVEL_IN_GROUP;
665
  pWindow->pStateExpr = nodesCloneNode(pState->pExpr);
666
  pWindow->pTspk = nodesCloneNode(pState->pCol);
667
  if (NULL == pWindow->pStateExpr || NULL == pWindow->pTspk) {
668
    nodesDestroyNode((SNode*)pWindow);
669 670 671
    return TSDB_CODE_OUT_OF_MEMORY;
  }

672 673 674
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
675 676
static int32_t createWindowLogicNodeBySession(SLogicPlanContext* pCxt, SSessionWindowNode* pSession,
                                              SSelectStmt* pSelect, SLogicNode** pLogicNode) {
677
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
X
Xiaoyu Wang 已提交
678 679 680 681 682 683
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  pWindow->winType = WINDOW_TYPE_SESSION;
  pWindow->sessionGap = ((SValueNode*)pSession->pGap)->datum.i;
684
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? SESSION_ALGO_STREAM_SINGLE : SESSION_ALGO_MERGE;
685 686 687
  pWindow->node.groupAction = GROUP_ACTION_KEEP;
  pWindow->node.requireDataOrder = DATA_ORDER_LEVEL_IN_GROUP;
  pWindow->node.resultDataOrder = DATA_ORDER_LEVEL_IN_GROUP;
X
Xiaoyu Wang 已提交
688

689
  pWindow->pTspk = nodesCloneNode((SNode*)pSession->pCol);
690
  if (NULL == pWindow->pTspk) {
691
    nodesDestroyNode((SNode*)pWindow);
692 693
    return TSDB_CODE_OUT_OF_MEMORY;
  }
5
54liuyao 已提交
694
  pWindow->pTsEnd = nodesCloneNode((SNode*)pSession->pCol);
695

X
Xiaoyu Wang 已提交
696 697 698
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
699 700
static int32_t createWindowLogicNodeByInterval(SLogicPlanContext* pCxt, SIntervalWindowNode* pInterval,
                                               SSelectStmt* pSelect, SLogicNode** pLogicNode) {
701
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
702 703 704
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
705 706 707

  pWindow->winType = WINDOW_TYPE_INTERVAL;
  pWindow->interval = ((SValueNode*)pInterval->pInterval)->datum.i;
X
Xiaoyu Wang 已提交
708
  pWindow->intervalUnit = ((SValueNode*)pInterval->pInterval)->unit;
X
Xiaoyu Wang 已提交
709
  pWindow->offset = (NULL != pInterval->pOffset ? ((SValueNode*)pInterval->pOffset)->datum.i : 0);
710
  pWindow->sliding = (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->datum.i : pWindow->interval);
X
Xiaoyu Wang 已提交
711 712
  pWindow->slidingUnit =
      (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->unit : pWindow->intervalUnit);
713
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? INTERVAL_ALGO_STREAM_SINGLE : INTERVAL_ALGO_HASH;
714 715
  pWindow->node.groupAction = GROUP_ACTION_KEEP;
  pWindow->node.requireDataOrder = DATA_ORDER_LEVEL_IN_BLOCK;
716
  pWindow->node.resultDataOrder = DATA_ORDER_LEVEL_IN_GROUP;
717

X
bugfix  
Xiaoyu Wang 已提交
718 719
  pWindow->pTspk = nodesCloneNode(pInterval->pCol);
  if (NULL == pWindow->pTspk) {
720
    nodesDestroyNode((SNode*)pWindow);
X
bugfix  
Xiaoyu Wang 已提交
721 722 723
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
724
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
X
Xiaoyu Wang 已提交
725 726
}

727
static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
728
  if (NULL == pSelect->pWindow) {
729
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
730 731 732
  }

  switch (nodeType(pSelect->pWindow)) {
733 734
    case QUERY_NODE_STATE_WINDOW:
      return createWindowLogicNodeByState(pCxt, (SStateWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
735 736
    case QUERY_NODE_SESSION_WINDOW:
      return createWindowLogicNodeBySession(pCxt, (SSessionWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
737
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
738
      return createWindowLogicNodeByInterval(pCxt, (SIntervalWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
739 740 741 742
    default:
      break;
  }

743
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
744 745
}

X
Xiaoyu Wang 已提交
746 747 748 749 750 751 752
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);
753 754 755
  if (FILL_MODE_NONE == pFillNode->mode) {
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
756

757
  SFillLogicNode* pFill = (SFillLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_FILL);
X
Xiaoyu Wang 已提交
758 759 760 761
  if (NULL == pFill) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

762 763 764 765
  pFill->node.groupAction = GROUP_ACTION_KEEP;
  pFill->node.requireDataOrder = DATA_ORDER_LEVEL_IN_GROUP;
  pFill->node.resultDataOrder = DATA_ORDER_LEVEL_IN_GROUP;

X
Xiaoyu Wang 已提交
766
  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_WINDOW, NULL, COLLECT_COL_TYPE_ALL, &pFill->node.pTargets);
767 768 769 770
  if (TSDB_CODE_SUCCESS == code && NULL == pFill->node.pTargets) {
    code = nodesListMakeStrictAppend(&pFill->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
  }
X
Xiaoyu Wang 已提交
771 772

  pFill->mode = pFillNode->mode;
X
Xiaoyu Wang 已提交
773
  pFill->timeRange = pFillNode->timeRange;
X
Xiaoyu Wang 已提交
774 775 776 777 778 779 780 781 782
  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 {
783
    nodesDestroyNode((SNode*)pFill);
X
Xiaoyu Wang 已提交
784 785 786 787 788
  }

  return code;
}

X
Xiaoyu Wang 已提交
789 790 791 792 793
static int32_t createSortLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pOrderByList) {
    return TSDB_CODE_SUCCESS;
  }

794
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
795 796 797 798
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

799
  pSort->groupSort = pSelect->groupSort;
800 801 802
  pSort->node.groupAction = pSort->groupSort ? GROUP_ACTION_KEEP : GROUP_ACTION_CLEAR;
  pSort->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pSort->node.resultDataOrder = pSort->groupSort ? DATA_ORDER_LEVEL_IN_GROUP : DATA_ORDER_LEVEL_GLOBAL;
803

804
  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_ORDER_BY, NULL, COLLECT_COL_TYPE_ALL, &pSort->node.pTargets);
805 806 807 808
  if (TSDB_CODE_SUCCESS == code && NULL == pSort->node.pTargets) {
    code = nodesListMakeStrictAppend(&pSort->node.pTargets,
                                     nodesCloneNode(nodesListGetNode(pCxt->pCurrRoot->pTargets, 0)));
  }
X
Xiaoyu Wang 已提交
809 810 811 812 813 814 815 816 817 818 819

  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 {
820
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
821 822 823 824 825
  }

  return code;
}

X
Xiaoyu Wang 已提交
826 827
static int32_t createColumnByProjections(SLogicPlanContext* pCxt, const char* pStmtName, SNodeList* pExprs,
                                         SNodeList** pCols) {
X
Xiaoyu Wang 已提交
828
  SNodeList* pList = nodesMakeList();
X
Xiaoyu Wang 已提交
829
  if (NULL == pList) {
830 831 832
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
833
  SNode* pNode;
X
Xiaoyu Wang 已提交
834
  FOREACH(pNode, pExprs) {
835
    if (TSDB_CODE_SUCCESS != nodesListAppend(pList, (SNode*)createColumnByExpr(pStmtName, (SExprNode*)pNode))) {
X
Xiaoyu Wang 已提交
836 837
      nodesDestroyList(pList);
      return TSDB_CODE_OUT_OF_MEMORY;
838 839
    }
  }
840

X
Xiaoyu Wang 已提交
841 842
  *pCols = pList;
  return TSDB_CODE_SUCCESS;
843 844
}

X
Xiaoyu Wang 已提交
845
static int32_t createProjectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
846
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
X
Xiaoyu Wang 已提交
847
  if (NULL == pProject) {
848 849 850
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
851 852
  TSWAP(pProject->node.pLimit, pSelect->pLimit);
  TSWAP(pProject->node.pSlimit, pSelect->pSlimit);
853
  pProject->node.groupAction = GROUP_ACTION_CLEAR;
854 855
  pProject->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pProject->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;
856

X
Xiaoyu Wang 已提交
857
  int32_t code = TSDB_CODE_SUCCESS;
858

X
Xiaoyu Wang 已提交
859
  pProject->pProjections = nodesCloneList(pSelect->pProjectionList);
X
Xiaoyu Wang 已提交
860 861
  if (NULL == pProject->pProjections) {
    code = TSDB_CODE_OUT_OF_MEMORY;
862
  }
X
Xiaoyu Wang 已提交
863
  strcpy(pProject->stmtName, pSelect->stmtName);
864 865

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

869
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
870 871
    *pLogicNode = (SLogicNode*)pProject;
  } else {
872
    nodesDestroyNode((SNode*)pProject);
873
  }
874

875
  return code;
876 877
}

878
static int32_t createPartitionLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
879
  if (NULL == pSelect->pPartitionByList) {
880 881 882
    return TSDB_CODE_SUCCESS;
  }

883
  SPartitionLogicNode* pPartition = (SPartitionLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PARTITION);
884 885 886 887
  if (NULL == pPartition) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

888 889 890 891
  pPartition->node.groupAction = GROUP_ACTION_SET;
  pPartition->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pPartition->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;

892 893
  int32_t code =
      nodesCollectColumns(pSelect, SQL_CLAUSE_PARTITION_BY, NULL, COLLECT_COL_TYPE_ALL, &pPartition->node.pTargets);
894 895 896
  if (TSDB_CODE_SUCCESS == code && NULL == pPartition->node.pTargets) {
    code = nodesListMakeStrictAppend(&pPartition->node.pTargets, nodesListGetNode(pCxt->pCurrRoot->pTargets, 0));
  }
897 898 899 900 901 902 903 904 905 906 907

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

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pPartition;
  } else {
908
    nodesDestroyNode((SNode*)pPartition);
909 910 911
  }

  return code;
912 913 914 915 916 917 918
}

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

919 920 921 922 923
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

924 925 926 927
  pAgg->node.groupAction = GROUP_ACTION_SET;
  pAgg->node.requireDataOrder = DATA_ORDER_LEVEL_NONE;
  pAgg->node.resultDataOrder = DATA_ORDER_LEVEL_NONE;

928 929 930 931 932 933 934 935 936
  int32_t code = TSDB_CODE_SUCCESS;
  // set grouyp keys, agg funcs and having conditions
  pAgg->pGroupKeys = nodesCloneList(pSelect->pProjectionList);
  if (NULL == pAgg->pGroupKeys) {
    code = TSDB_CODE_OUT_OF_MEMORY;
  }

  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
937
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_DISTINCT);
938 939 940 941
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
942
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
943 944 945 946 947
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
948
    nodesDestroyNode((SNode*)pAgg);
949 950 951
  }

  return code;
952 953
}

954 955 956 957 958 959
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) {
960
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
961
  int32_t     code = createLogicNodeByTable(pCxt, pSelect, pSelect->pFromTable, &pRoot);
962
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
963
    code = createSelectRootLogicNode(pCxt, pSelect, createPartitionLogicNode, &pRoot);
964
  }
965
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
966
    code = createSelectRootLogicNode(pCxt, pSelect, createWindowLogicNode, &pRoot);
967
  }
X
Xiaoyu Wang 已提交
968
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
969
    code = createSelectRootLogicNode(pCxt, pSelect, createFillLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
970
  }
971
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
972
    code = createSelectRootLogicNode(pCxt, pSelect, createAggLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
973
  }
974
  if (TSDB_CODE_SUCCESS == code) {
975
    code = createSelectRootLogicNode(pCxt, pSelect, createIndefRowsFuncLogicNode, &pRoot);
976
  }
X
Xiaoyu Wang 已提交
977 978 979
  if (TSDB_CODE_SUCCESS == code) {
    code = createSelectRootLogicNode(pCxt, pSelect, createInterpFuncLogicNode, &pRoot);
  }
980
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
981
    code = createSelectRootLogicNode(pCxt, pSelect, createDistinctLogicNode, &pRoot);
982
  }
X
Xiaoyu Wang 已提交
983
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
984
    code = createSelectRootLogicNode(pCxt, pSelect, createSortLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
985
  }
986
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
987
    code = createSelectRootLogicNode(pCxt, pSelect, createProjectLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
988
  }
989 990 991 992

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
993
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
994
  }
995 996

  return code;
997 998
}

999 1000 1001 1002 1003 1004 1005 1006
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 已提交
1007 1008 1009
static int32_t createSetOpRootLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, FCreateSetOpLogicNode func,
                                        SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSetOperator, pSetOperator->precision, (FCreateLogicNode)func, pRoot);
X
Xiaoyu Wang 已提交
1010 1011 1012 1013 1014 1015 1016
}

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

1017
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
1018 1019 1020 1021
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

X
Xiaoyu Wang 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
  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 {
1041
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
1042 1043 1044 1045 1046
  }

  return code;
}

X
Xiaoyu Wang 已提交
1047 1048
static int32_t createSetOpProjectLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                           SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1049 1050 1051 1052 1053
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
  if (NULL == pProject) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

1054
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1055
    TSWAP(pProject->node.pLimit, pSetOperator->pLimit);
X
Xiaoyu Wang 已提交
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
  }

  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) {
1066 1067
    code = createColumnByProjections(pCxt, pSetOperator->stmtName, pSetOperator->pProjectionList,
                                     &pProject->node.pTargets);
X
Xiaoyu Wang 已提交
1068 1069 1070 1071 1072
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pProject;
  } else {
1073
    nodesDestroyNode((SNode*)pProject);
X
Xiaoyu Wang 已提交
1074 1075 1076 1077 1078
  }

  return code;
}

X
Xiaoyu Wang 已提交
1079 1080 1081 1082 1083 1084
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;
  }

1085
  if (NULL == pSetOperator->pOrderByList) {
X
Xiaoyu Wang 已提交
1086
    TSWAP(pAgg->node.pSlimit, pSetOperator->pLimit);
1087 1088
  }

X
Xiaoyu Wang 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
  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 已提交
1102
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1103 1104 1105 1106 1107
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1108
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1109 1110 1111 1112 1113
  }

  return code;
}

X
Xiaoyu Wang 已提交
1114 1115
static int32_t createSetOpLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, SLogicNode** pLogicNode) {
  SLogicNode* pSetOp = NULL;
X
Xiaoyu Wang 已提交
1116
  int32_t     code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1117 1118 1119 1120
  switch (pSetOperator->opType) {
    case SET_OP_TYPE_UNION_ALL:
      code = createSetOpProjectLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1121 1122 1123
    case SET_OP_TYPE_UNION:
      code = createSetOpAggLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
1124
    default:
1125
      code = TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
      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 {
1147
    nodesDestroyNode((SNode*)pSetOp);
X
Xiaoyu Wang 已提交
1148 1149 1150 1151 1152
  }

  return code;
}

X
Xiaoyu Wang 已提交
1153 1154
static int32_t createSetOperatorLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                          SLogicNode** pLogicNode) {
1155
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1156
  int32_t     code = createSetOpLogicNode(pCxt, pSetOperator, &pRoot);
1157
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1158
    code = createSetOpRootLogicNode(pCxt, pSetOperator, createSetOpSortLogicNode, &pRoot);
1159 1160 1161 1162 1163
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1164
    nodesDestroyNode((SNode*)pRoot);
1165 1166 1167 1168 1169
  }

  return code;
}

1170
static int32_t getMsgType(ENodeType sqlType) {
X
Xiaoyu Wang 已提交
1171 1172 1173 1174 1175 1176
  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 已提交
1177 1178
    case QUERY_NODE_ALTER_TABLE_STMT:
      return TDMT_VND_ALTER_TABLE;
X
Xiaoyu Wang 已提交
1179 1180
    case QUERY_NODE_FLUSH_DATABASE_STMT:
      return TDMT_VND_COMMIT;
X
Xiaoyu Wang 已提交
1181 1182 1183 1184
    default:
      break;
  }
  return TDMT_VND_SUBMIT;
1185 1186
}

1187
static int32_t createVnodeModifLogicNode(SLogicPlanContext* pCxt, SVnodeModifOpStmt* pStmt, SLogicNode** pLogicNode) {
1188
  SVnodeModifyLogicNode* pModif = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
1189 1190 1191
  if (NULL == pModif) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1192
  pModif->modifyType = MODIFY_TABLE_TYPE_INSERT;
wafwerar's avatar
wafwerar 已提交
1193
  TSWAP(pModif->pDataBlocks, pStmt->pDataBlocks);
1194
  pModif->msgType = getMsgType(pStmt->sqlNodeType);
1195 1196
  *pLogicNode = (SLogicNode*)pModif;
  return TSDB_CODE_SUCCESS;
1197 1198
}

X
Xiaoyu Wang 已提交
1199 1200 1201 1202 1203 1204
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 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
  SScanLogicNode* pScan = NULL;
  int32_t         code = makeScanLogicNode(pCxt, (SRealTableNode*)pDelete->pFromTable, false, (SLogicNode**)&pScan);

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

X
Xiaoyu Wang 已提交
1217 1218
  if (TSDB_CODE_SUCCESS == code && NULL != pDelete->pTagCond) {
    pScan->pTagCond = nodesCloneNode(pDelete->pTagCond);
X
Xiaoyu Wang 已提交
1219 1220 1221 1222 1223 1224 1225
    if (NULL == pScan->pTagCond) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1226
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
X
Xiaoyu Wang 已提交
1227 1228 1229 1230 1231
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
1232
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
1233 1234 1235
  }

  return code;
X
Xiaoyu Wang 已提交
1236 1237 1238
}

static int32_t createDeleteAggLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = nodesListMakeStrictAppend(&pAgg->pAggFuncs, nodesCloneNode(pDelete->pCountFunc));
  if (TSDB_CODE_SUCCESS == code) {
    code = rewriteExpr(pAgg->pAggFuncs, &pDelete->pCountFunc);
  }
  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1250
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1251 1252 1253 1254 1255
  }

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

  return code;
X
Xiaoyu Wang 已提交
1260 1261
}

X
Xiaoyu Wang 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
static int32_t createVnodeModifLogicNodeByDelete(SLogicPlanContext* pCxt, SDeleteStmt* pDelete,
                                                 SLogicNode** pLogicNode) {
  SVnodeModifyLogicNode* pModify = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
  if (NULL == pModify) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

  pModify->modifyType = MODIFY_TABLE_TYPE_DELETE;
  pModify->tableId = pRealTable->pMeta->uid;
  pModify->tableType = pRealTable->pMeta->tableType;
  snprintf(pModify->tableFName, sizeof(pModify->tableFName), "%d.%s.%s", pCxt->pPlanCxt->acctId,
           pRealTable->table.dbName, pRealTable->table.tableName);
  pModify->deleteTimeRange = pDelete->timeRange;
X
Xiaoyu Wang 已提交
1277 1278
  pModify->pAffectedRows = nodesCloneNode(pDelete->pCountFunc);
  if (NULL == pModify->pAffectedRows) {
1279
    nodesDestroyNode((SNode*)pModify);
X
Xiaoyu Wang 已提交
1280 1281 1282 1283 1284
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  *pLogicNode = (SLogicNode*)pModify;
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1285 1286 1287 1288
}

static int32_t createDeleteLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1289
  int32_t     code = createDeleteScanLogicNode(pCxt, pDelete, &pRoot);
X
Xiaoyu Wang 已提交
1290 1291 1292 1293
  if (TSDB_CODE_SUCCESS == code) {
    code = createDeleteRootLogicNode(pCxt, pDelete, createDeleteAggLogicNode, &pRoot);
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1294
    code = createDeleteRootLogicNode(pCxt, pDelete, createVnodeModifLogicNodeByDelete, &pRoot);
X
Xiaoyu Wang 已提交
1295 1296 1297 1298 1299
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1300
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
1301 1302 1303 1304 1305
  }

  return code;
}

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
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;
1322
  pModify->stableId = pRealTable->pMeta->suid;
1323 1324 1325
  pModify->tableType = pRealTable->pMeta->tableType;
  snprintf(pModify->tableFName, sizeof(pModify->tableFName), "%d.%s.%s", pCxt->pPlanCxt->acctId,
           pRealTable->table.dbName, pRealTable->table.tableName);
1326
  TSWAP(pModify->pVgroupList, pRealTable->pVgroupList);
1327 1328 1329 1330 1331
  pModify->pInsertCols = nodesCloneList(pInsert->pCols);
  if (NULL == pModify->pInsertCols) {
    nodesDestroyNode((SNode*)pModify);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352

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

1353
static int32_t createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1354 1355
  switch (nodeType(pStmt)) {
    case QUERY_NODE_SELECT_STMT:
1356
      return createSelectLogicNode(pCxt, (SSelectStmt*)pStmt, pLogicNode);
1357
    case QUERY_NODE_VNODE_MODIF_STMT:
1358
      return createVnodeModifLogicNode(pCxt, (SVnodeModifOpStmt*)pStmt, pLogicNode);
1359 1360
    case QUERY_NODE_EXPLAIN_STMT:
      return createQueryLogicNode(pCxt, ((SExplainStmt*)pStmt)->pQuery, pLogicNode);
1361 1362
    case QUERY_NODE_SET_OPERATOR:
      return createSetOperatorLogicNode(pCxt, (SSetOperator*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1363 1364
    case QUERY_NODE_DELETE_STMT:
      return createDeleteLogicNode(pCxt, (SDeleteStmt*)pStmt, pLogicNode);
1365 1366
    case QUERY_NODE_INSERT_STMT:
      return createInsertLogicNode(pCxt, (SInsertStmt*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1367 1368 1369
    default:
      break;
  }
1370
  return TSDB_CODE_FAILED;
1371 1372
}

X
Xiaoyu Wang 已提交
1373 1374 1375 1376 1377 1378 1379 1380
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); }

1381
static void setLogicSubplanType(bool hasScan, SLogicSubplan* pSubplan) {
1382
  if (QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY != nodeType(pSubplan->pNode)) {
1383
    pSubplan->subplanType = hasScan ? SUBPLAN_TYPE_SCAN : SUBPLAN_TYPE_MERGE;
1384 1385 1386 1387 1388 1389 1390 1391
  } else {
    SVnodeModifyLogicNode* pModify = (SVnodeModifyLogicNode*)pSubplan->pNode;
    pSubplan->subplanType = (MODIFY_TABLE_TYPE_INSERT == pModify->modifyType && NULL != pModify->node.pChildren)
                                ? SUBPLAN_TYPE_SCAN
                                : SUBPLAN_TYPE_MODIFY;
  }
}

1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
static int32_t adjustScanDataRequirement(SScanLogicNode* pScan, EDataOrderLevel requirement) {
  if (requirement <= DATA_ORDER_LEVEL_IN_BLOCK) {
    return TSDB_CODE_SUCCESS;
  }
  pScan->scanType = SCAN_TYPE_TABLE_MERGE;
  pScan->node.resultDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustJoinDataRequirement(SJoinLogicNode* pJoin, EDataOrderLevel requirement) {
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustAggDataRequirement(SAggLogicNode* pAgg, EDataOrderLevel requirement) {
  if (requirement > DATA_ORDER_LEVEL_NONE) {
    return TSDB_CODE_PLAN_INTERNAL_ERROR;
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustProjectDataRequirement(SProjectLogicNode* pProject, EDataOrderLevel requirement) {
  pProject->node.resultDataOrder = requirement;
  pProject->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustIntervalDataRequirement(SWindowLogicNode* pWindow, EDataOrderLevel requirement) {
1419
  if (requirement <= DATA_ORDER_LEVEL_IN_GROUP) {
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
    return TSDB_CODE_SUCCESS;
  }
  pWindow->node.resultDataOrder = requirement;
  pWindow->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustSessionDataRequirement(SWindowLogicNode* pWindow, EDataOrderLevel requirement) {
  if (requirement <= DATA_ORDER_LEVEL_IN_GROUP) {
    return TSDB_CODE_SUCCESS;
  }
  pWindow->node.resultDataOrder = requirement;
  pWindow->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustStateDataRequirement(SWindowLogicNode* pWindow, EDataOrderLevel requirement) {
  if (requirement <= DATA_ORDER_LEVEL_IN_GROUP) {
    return TSDB_CODE_SUCCESS;
  }
  pWindow->node.resultDataOrder = requirement;
  pWindow->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustWindowDataRequirement(SWindowLogicNode* pWindow, EDataOrderLevel requirement) {
  switch (pWindow->winType) {
    case WINDOW_TYPE_INTERVAL:
      return adjustIntervalDataRequirement(pWindow, requirement);
    case WINDOW_TYPE_SESSION:
      return adjustSessionDataRequirement(pWindow, requirement);
    case WINDOW_TYPE_STATE:
      return adjustStateDataRequirement(pWindow, requirement);
    default:
      break;
  }
  return TSDB_CODE_PLAN_INTERNAL_ERROR;
}

static int32_t adjustFillDataRequirement(SFillLogicNode* pFill, EDataOrderLevel requirement) {
  if (requirement <= DATA_ORDER_LEVEL_IN_GROUP) {
    return TSDB_CODE_SUCCESS;
  }
  pFill->node.resultDataOrder = requirement;
  pFill->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustSortDataRequirement(SSortLogicNode* pSort, EDataOrderLevel requirement) {
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustPartitionDataRequirement(SPartitionLogicNode* pPart, EDataOrderLevel requirement) {
  if (DATA_ORDER_LEVEL_GLOBAL == requirement) {
    return TSDB_CODE_PLAN_INTERNAL_ERROR;
  }
  pPart->node.resultDataOrder = requirement;
  pPart->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustIndefRowsDataRequirement(SIndefRowsFuncLogicNode* pIndef, EDataOrderLevel requirement) {
  if (requirement <= pIndef->node.resultDataOrder) {
    return TSDB_CODE_SUCCESS;
  }
  pIndef->node.resultDataOrder = requirement;
  pIndef->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

static int32_t adjustInterpDataRequirement(SInterpFuncLogicNode* pInterp, EDataOrderLevel requirement) {
  if (requirement <= DATA_ORDER_LEVEL_IN_GROUP) {
    return TSDB_CODE_SUCCESS;
  }
  pInterp->node.resultDataOrder = requirement;
  pInterp->node.requireDataOrder = requirement;
  return TSDB_CODE_SUCCESS;
}

1499
static int32_t adjustLogicNodeDataRequirementImpl(SLogicNode* pNode, EDataOrderLevel requirement) {
1500
  int32_t code = TSDB_CODE_SUCCESS;
1501 1502
  switch (nodeType(pNode)) {
    case QUERY_NODE_LOGIC_PLAN_SCAN:
1503 1504
      code = adjustScanDataRequirement((SScanLogicNode*)pNode, requirement);
      break;
1505
    case QUERY_NODE_LOGIC_PLAN_JOIN:
1506 1507
      code = adjustJoinDataRequirement((SJoinLogicNode*)pNode, requirement);
      break;
1508
    case QUERY_NODE_LOGIC_PLAN_AGG:
1509 1510
      code = adjustAggDataRequirement((SAggLogicNode*)pNode, requirement);
      break;
1511
    case QUERY_NODE_LOGIC_PLAN_PROJECT:
1512 1513
      code = adjustProjectDataRequirement((SProjectLogicNode*)pNode, requirement);
      break;
1514 1515 1516
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY:
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
    case QUERY_NODE_LOGIC_PLAN_MERGE:
1517
      break;
1518
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
1519 1520
      code = adjustWindowDataRequirement((SWindowLogicNode*)pNode, requirement);
      break;
1521
    case QUERY_NODE_LOGIC_PLAN_FILL:
1522 1523
      code = adjustFillDataRequirement((SFillLogicNode*)pNode, requirement);
      break;
1524
    case QUERY_NODE_LOGIC_PLAN_SORT:
1525 1526
      code = adjustSortDataRequirement((SSortLogicNode*)pNode, requirement);
      break;
1527
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
1528 1529
      code = adjustPartitionDataRequirement((SPartitionLogicNode*)pNode, requirement);
      break;
1530
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC:
1531 1532
      code = adjustIndefRowsDataRequirement((SIndefRowsFuncLogicNode*)pNode, requirement);
      break;
1533
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC:
1534 1535
      code = adjustInterpDataRequirement((SInterpFuncLogicNode*)pNode, requirement);
      break;
1536 1537 1538
    default:
      break;
  }
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
  if (TSDB_CODE_SUCCESS == code) {
    SNode* pChild = NULL;
    FOREACH(pChild, pNode->pChildren) {
      code = adjustLogicNodeDataRequirementImpl((SLogicNode*)pChild, pNode->requireDataOrder);
      if (TSDB_CODE_SUCCESS != code) {
        break;
      }
    }
  }
  return code;
1549 1550 1551 1552 1553 1554
}

static int32_t adjustLogicNodeDataRequirement(SLogicNode* pNode) {
  return adjustLogicNodeDataRequirementImpl(pNode, DATA_ORDER_LEVEL_NONE);
}

X
Xiaoyu Wang 已提交
1555
int32_t createLogicPlan(SPlanContext* pCxt, SLogicSubplan** pLogicSubplan) {
1556
  SLogicPlanContext cxt = {.pPlanCxt = pCxt, .pCurrRoot = NULL, .hasScan = false};
X
Xiaoyu Wang 已提交
1557 1558 1559 1560

  SLogicSubplan* pSubplan = (SLogicSubplan*)nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN);
  if (NULL == pSubplan) {
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
1561
  }
X
Xiaoyu Wang 已提交
1562 1563 1564 1565 1566 1567 1568
  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);
1569
    setLogicSubplanType(cxt.hasScan, pSubplan);
1570
    code = adjustLogicNodeDataRequirement(pSubplan->pNode);
X
Xiaoyu Wang 已提交
1571 1572 1573 1574 1575
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicSubplan = pSubplan;
  } else {
1576
    nodesDestroyNode((SNode*)pSubplan);
X
Xiaoyu Wang 已提交
1577 1578 1579
  }

  return code;
1580
}