planLogicCreater.c 39.9 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;
X
Xiaoyu Wang 已提交
22 23
} SLogicPlanContext;

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

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

typedef struct SRewriteExprCxt {
X
Xiaoyu Wang 已提交
34
  int32_t    errCode;
X
Xiaoyu Wang 已提交
35 36 37 38 39 40 41 42 43
  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 已提交
44 45
      SNode*           pExpr;
      int32_t          index = 0;
X
Xiaoyu Wang 已提交
46 47 48 49 50 51
      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 已提交
52 53 54
          if (NULL == pCol) {
            return DEAL_RES_ERROR;
          }
X
Xiaoyu Wang 已提交
55 56 57 58
          SExprNode* pToBeRewrittenExpr = (SExprNode*)(*pNode);
          pCol->node.resType = pToBeRewrittenExpr->resType;
          strcpy(pCol->node.aliasName, pToBeRewrittenExpr->aliasName);
          strcpy(pCol->colName, ((SExprNode*)pExpr)->aliasName);
X
Xiaoyu Wang 已提交
59 60 61
          if (QUERY_NODE_FUNCTION == nodeType(pExpr) && FUNCTION_TYPE_WSTARTTS == ((SFunctionNode*)pExpr)->funcType) {
            pCol->colId = PRIMARYKEY_TIMESTAMP_COL_ID;
          }
X
Xiaoyu Wang 已提交
62 63 64 65 66 67 68 69 70 71
          nodesDestroyNode(*pNode);
          *pNode = (SNode*)pCol;
          return DEAL_RES_IGNORE_CHILD;
        }
        ++index;
      }
      break;
    }
    default:
      break;
H
Haojun Liao 已提交
72 73
  }

X
Xiaoyu Wang 已提交
74
  return DEAL_RES_CONTINUE;
X
Xiaoyu Wang 已提交
75 76
}

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

X
Xiaoyu Wang 已提交
91
  return DEAL_RES_CONTINUE;
92 93
}

X
Xiaoyu Wang 已提交
94
static int32_t rewriteExprsForSelect(SNodeList* pExprs, SSelectStmt* pSelect, ESqlClause clause) {
X
Xiaoyu Wang 已提交
95
  nodesWalkExprs(pExprs, doNameExpr, NULL);
X
Xiaoyu Wang 已提交
96
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs};
X
Xiaoyu Wang 已提交
97 98
  nodesRewriteSelectStmt(pSelect, clause, doRewriteExpr, &cxt);
  return cxt.errCode;
99 100
}

X
Xiaoyu Wang 已提交
101 102 103 104 105 106 107
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 已提交
108
static int32_t rewriteExprs(SNodeList* pExprs, SNodeList* pTarget) {
X
Xiaoyu Wang 已提交
109
  nodesWalkExprs(pExprs, doNameExpr, NULL);
X
Xiaoyu Wang 已提交
110
  SRewriteExprCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pExprs = pExprs};
X
Xiaoyu Wang 已提交
111 112 113 114
  nodesRewriteExprs(pTarget, doRewriteExpr, &cxt);
  return cxt.errCode;
}

115 116 117 118 119
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 已提交
120
    }
121
  }
122 123
  if (TSDB_CODE_SUCCESS != nodesListAppend(pNewRoot->pChildren, (SNode*)*pOldRoot)) {
    return TSDB_CODE_OUT_OF_MEMORY;
124 125
  }

126 127
  (*pOldRoot)->pParent = pNewRoot;
  *pOldRoot = pNewRoot;
128

129 130
  return TSDB_CODE_SUCCESS;
}
131

X
Xiaoyu Wang 已提交
132 133
static int32_t createRootLogicNode(SLogicPlanContext* pCxt, void* pStmt, uint8_t precision, FCreateLogicNode func,
                                   SLogicNode** pRoot) {
X
Xiaoyu Wang 已提交
134
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
135
  int32_t     code = func(pCxt, pStmt, &pNode);
X
Xiaoyu Wang 已提交
136
  if (TSDB_CODE_SUCCESS == code && NULL != pNode) {
X
Xiaoyu Wang 已提交
137
    pNode->precision = precision;
X
Xiaoyu Wang 已提交
138
    code = pushLogicNode(pCxt, pRoot, pNode);
139
  }
X
Xiaoyu Wang 已提交
140
  if (TSDB_CODE_SUCCESS != code) {
141
    nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
142
  }
X
Xiaoyu Wang 已提交
143
  return code;
144 145
}

X
Xiaoyu Wang 已提交
146 147 148 149
static int32_t createSelectRootLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, FCreateSelectLogicNode func,
                                         SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSelect, pSelect->precision, (FCreateLogicNode)func, pRoot);
}
150 151

static EScanType getScanType(SLogicPlanContext* pCxt, SNodeList* pScanPseudoCols, SNodeList* pScanCols,
X
Xiaoyu Wang 已提交
152
                             int8_t tableType) {
X
Xiaoyu Wang 已提交
153 154 155 156
  if (pCxt->pPlanCxt->topicQuery || pCxt->pPlanCxt->streamQuery) {
    return SCAN_TYPE_STREAM;
  }

X
Xiaoyu Wang 已提交
157 158
  if (NULL == pScanCols) {
    // select count(*) from t
159 160 161 162 163
    return NULL == pScanPseudoCols
               ? SCAN_TYPE_TABLE
               : ((FUNCTION_TYPE_BLOCK_DIST_INFO == ((SFunctionNode*)nodesListGetNode(pScanPseudoCols, 0))->funcType)
                      ? SCAN_TYPE_BLOCK_INFO
                      : SCAN_TYPE_TAG);
X
Xiaoyu Wang 已提交
164 165
  }

X
Xiaoyu Wang 已提交
166
  if (TSDB_SYSTEM_TABLE == tableType) {
X
Xiaoyu Wang 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179
    return SCAN_TYPE_SYSTEM_TABLE;
  }

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

  return SCAN_TYPE_TAG;
}

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

X
Xiaoyu Wang 已提交
242 243 244 245 246 247 248 249 250 251
  *pLogicNode = (SLogicNode*)pScan;

  return TSDB_CODE_SUCCESS;
}

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

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

258
  if (TSDB_CODE_SUCCESS == code) {
259 260
    code = nodesCollectColumns(pSelect, SQL_CLAUSE_FROM, pRealTable->table.tableAlias, COLLECT_COL_TYPE_TAG,
                               &pScan->pScanPseudoCols);
261 262 263
  }

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

267 268
  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
269
    code = rewriteExprsForSelect(pScan->pScanPseudoCols, pSelect, SQL_CLAUSE_FROM);
270 271
  }

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

274
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
275
    code = addPrimaryKeyCol(pScan->tableId, &pScan->pScanCols);
276 277
  }

278 279
  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
280
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
281 282
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
283
    code = createColumnByRewriteExprs(pScan->pScanPseudoCols, &pScan->node.pTargets);
284
  }
285

X
Xiaoyu Wang 已提交
286 287 288
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
289
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
290
  }
291

X
Xiaoyu Wang 已提交
292
  return code;
X
Xiaoyu Wang 已提交
293
}
294

X
Xiaoyu Wang 已提交
295 296
static int32_t createSubqueryLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, STempTableNode* pTable,
                                       SLogicNode** pLogicNode) {
297
  return createQueryLogicNode(pCxt, pTable->pSubquery, pLogicNode);
298 299
}

X
Xiaoyu Wang 已提交
300 301
static int32_t createJoinLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SJoinTableNode* pJoinTable,
                                   SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
302
  SJoinLogicNode* pJoin = (SJoinLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_JOIN);
X
Xiaoyu Wang 已提交
303
  if (NULL == pJoin) {
304 305
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
306 307

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

310
  int32_t code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
311 312 313

  // set left and right node
  pJoin->node.pChildren = nodesMakeList();
X
Xiaoyu Wang 已提交
314
  if (NULL == pJoin->node.pChildren) {
315
    code = TSDB_CODE_OUT_OF_MEMORY;
316 317
  }

X
Xiaoyu Wang 已提交
318
  SLogicNode* pLeft = NULL;
319
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
320 321 322 323
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pLeft, &pLeft);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pLeft);
    }
324
  }
325

X
Xiaoyu Wang 已提交
326
  SLogicNode* pRight = NULL;
327
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
328 329 330 331 332
    code = doCreateLogicNodeByTable(pCxt, pSelect, pJoinTable->pRight, &pRight);
    if (TSDB_CODE_SUCCESS == code) {
      code = nodesListStrictAppend(pJoin->node.pChildren, (SNode*)pRight);
    }
  }
X
Xiaoyu Wang 已提交
333 334

  // set on conditions
X
Xiaoyu Wang 已提交
335
  if (TSDB_CODE_SUCCESS == code && NULL != pJoinTable->pOnCond) {
X
Xiaoyu Wang 已提交
336
    pJoin->pOnConditions = nodesCloneNode(pJoinTable->pOnCond);
X
Xiaoyu Wang 已提交
337 338 339
    if (NULL == pJoin->pOnConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
340 341
  }

X
Xiaoyu Wang 已提交
342
  // set the output
X
Xiaoyu Wang 已提交
343 344
  if (TSDB_CODE_SUCCESS == code) {
    pJoin->node.pTargets = nodesCloneList(pLeft->pTargets);
D
dapan1121 已提交
345
    if (NULL == pJoin->node.pTargets) {
X
Xiaoyu Wang 已提交
346 347 348 349 350 351 352 353 354
      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;
355
  } else {
356
    nodesDestroyNode((SNode*)pJoin);
357 358
  }

359
  return code;
X
Xiaoyu Wang 已提交
360
}
361

X
Xiaoyu Wang 已提交
362 363
static int32_t doCreateLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                        SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
364 365
  switch (nodeType(pTable)) {
    case QUERY_NODE_REAL_TABLE:
X
Xiaoyu Wang 已提交
366
      return createScanLogicNode(pCxt, pSelect, (SRealTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
367
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
368
      return createSubqueryLogicNode(pCxt, pSelect, (STempTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
369
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
370
      return createJoinLogicNode(pCxt, pSelect, (SJoinTableNode*)pTable, pLogicNode);
X
Xiaoyu Wang 已提交
371 372
    default:
      break;
373
  }
X
Xiaoyu Wang 已提交
374 375 376
  return TSDB_CODE_FAILED;
}

X
Xiaoyu Wang 已提交
377 378
static int32_t createLogicNodeByTable(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SNode* pTable,
                                      SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
379
  SLogicNode* pNode = NULL;
X
Xiaoyu Wang 已提交
380
  int32_t     code = doCreateLogicNodeByTable(pCxt, pSelect, pTable, &pNode);
X
Xiaoyu Wang 已提交
381 382 383
  if (TSDB_CODE_SUCCESS == code) {
    pNode->pConditions = nodesCloneNode(pSelect->pWhere);
    if (NULL != pSelect->pWhere && NULL == pNode->pConditions) {
384
      nodesDestroyNode((SNode*)pNode);
X
Xiaoyu Wang 已提交
385 386
      return TSDB_CODE_OUT_OF_MEMORY;
    }
387
    pNode->precision = pSelect->precision;
X
Xiaoyu Wang 已提交
388 389 390 391 392
    *pLogicNode = pNode;
  }
  return code;
}

X
Xiaoyu Wang 已提交
393
static SColumnNode* createColumnByExpr(const char* pStmtName, SExprNode* pExpr) {
394
  SColumnNode* pCol = (SColumnNode*)nodesMakeNode(QUERY_NODE_COLUMN);
X
Xiaoyu Wang 已提交
395 396 397 398 399
  if (NULL == pCol) {
    return NULL;
  }
  pCol->node.resType = pExpr->resType;
  strcpy(pCol->colName, pExpr->aliasName);
X
Xiaoyu Wang 已提交
400 401 402
  if (NULL != pStmtName) {
    strcpy(pCol->tableAlias, pStmtName);
  }
X
Xiaoyu Wang 已提交
403
  return pCol;
404 405
}

406
static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
407
  if (!pSelect->hasAggFuncs && NULL == pSelect->pGroupByList) {
408
    return TSDB_CODE_SUCCESS;
409 410
  }

X
Xiaoyu Wang 已提交
411
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
412 413 414
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
415

X
Xiaoyu Wang 已提交
416 417
  int32_t code = TSDB_CODE_SUCCESS;

X
Xiaoyu Wang 已提交
418 419 420
  // set grouyp keys, agg funcs and having conditions
  if (NULL != pSelect->pGroupByList) {
    pAgg->pGroupKeys = nodesCloneList(pSelect->pGroupByList);
421 422 423
    if (NULL == pAgg->pGroupKeys) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
424 425
  }

X
Xiaoyu Wang 已提交
426
  // rewrite the expression in subsequent clauses
427
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
428
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_GROUP_BY);
429
  }
X
Xiaoyu Wang 已提交
430

X
Xiaoyu Wang 已提交
431 432
  if (TSDB_CODE_SUCCESS == code && pSelect->hasAggFuncs) {
    code = nodesCollectFuncs(pSelect, SQL_CLAUSE_GROUP_BY, fmIsAggFunc, &pAgg->pAggFuncs);
X
Xiaoyu Wang 已提交
433 434 435
  }

  // rewrite the expression in subsequent clauses
436
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
437
    code = rewriteExprsForSelect(pAgg->pAggFuncs, pSelect, SQL_CLAUSE_GROUP_BY);
438
  }
439

440
  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pHaving) {
X
Xiaoyu Wang 已提交
441
    pAgg->node.pConditions = nodesCloneNode(pSelect->pHaving);
442 443 444
    if (NULL == pAgg->node.pConditions) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
X
Xiaoyu Wang 已提交
445
  }
446

X
Xiaoyu Wang 已提交
447
  // set the output
448
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pGroupKeys) {
X
Xiaoyu Wang 已提交
449
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
450 451
  }
  if (TSDB_CODE_SUCCESS == code && NULL != pAgg->pAggFuncs) {
X
Xiaoyu Wang 已提交
452
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
453 454 455 456 457
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
458
    nodesDestroyNode((SNode*)pAgg);
459 460 461
  }

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

464 465
static int32_t createIndefRowsFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  // top/bottom are both an aggregate function and a indefinite rows function
466
  if (!pSelect->hasIndefiniteRowsFunc || pSelect->hasAggFuncs || NULL != pSelect->pWindow) {
467 468 469 470 471 472 473 474 475
    return TSDB_CODE_SUCCESS;
  }

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

X
Xiaoyu Wang 已提交
476 477
  // indefinite rows functions and _select_values functions
  int32_t code = nodesCollectFuncs(pSelect, SQL_CLAUSE_SELECT, fmIsVectorFunc, &pIdfRowsFunc->pFuncs);
478
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
479
    code = rewriteExprsForSelect(pIdfRowsFunc->pFuncs, pSelect, SQL_CLAUSE_SELECT);
480 481 482 483
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
484
    code = createColumnByRewriteExprs(pIdfRowsFunc->pFuncs, &pIdfRowsFunc->node.pTargets);
485 486 487 488 489
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pIdfRowsFunc;
  } else {
490
    nodesDestroyNode((SNode*)pIdfRowsFunc);
491 492 493 494 495
  }

  return code;
}

X
Xiaoyu Wang 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
static int32_t createInterpFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (!pSelect->hasInterpFunc) {
    return TSDB_CODE_SUCCESS;
  }

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

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

  if (TSDB_CODE_SUCCESS == code && NULL != pSelect->pRange) {
    // SRangeNode* pRange = (SRangeNode*)pSelect->pRange;
    // pInterpFunc->timeRange.skey = ((SValueNode*)pRange->pStart)->datum.i;
    // pInterpFunc->timeRange.ekey = ((SValueNode*)pRange->pEnd)->datum.i;
  }

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

X
Xiaoyu Wang 已提交
539 540 541 542 543
  if (pCxt->pPlanCxt->streamQuery) {
    pWindow->triggerType = pCxt->pPlanCxt->triggerType;
    pWindow->watermark = pCxt->pPlanCxt->watermark;
  }

5
54liuyao 已提交
544
  if (pCxt->pPlanCxt->rSmaQuery) {
L
Liu Jicong 已提交
545
    /*pWindow->filesFactor = pCxt->pPlanCxt->filesFactor;*/
5
54liuyao 已提交
546 547
  }

X
Xiaoyu Wang 已提交
548
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
549
    code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW);
X
Xiaoyu Wang 已提交
550 551 552
  }

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

X
Xiaoyu Wang 已提交
556 557
  pSelect->hasAggFuncs = false;

X
Xiaoyu Wang 已提交
558 559 560
  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pWindow;
  } else {
561
    nodesDestroyNode((SNode*)pWindow);
X
Xiaoyu Wang 已提交
562 563 564 565 566
  }

  return code;
}

X
Xiaoyu Wang 已提交
567 568
static int32_t createWindowLogicNodeByState(SLogicPlanContext* pCxt, SStateWindowNode* pState, SSelectStmt* pSelect,
                                            SLogicNode** pLogicNode) {
569
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
570 571 572 573 574 575 576
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

577 578
  pWindow->pTspk = nodesCloneNode(pState->pCol);
  if (NULL == pWindow->pTspk) {
579
    nodesDestroyNode((SNode*)pWindow);
580 581 582
    return TSDB_CODE_OUT_OF_MEMORY;
  }

583 584 585
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
586 587
static int32_t createWindowLogicNodeBySession(SLogicPlanContext* pCxt, SSessionWindowNode* pSession,
                                              SSelectStmt* pSelect, SLogicNode** pLogicNode) {
588
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
X
Xiaoyu Wang 已提交
589 590 591 592 593 594
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

597
  pWindow->pTspk = nodesCloneNode((SNode*)pSession->pCol);
598
  if (NULL == pWindow->pTspk) {
599
    nodesDestroyNode((SNode*)pWindow);
600 601
    return TSDB_CODE_OUT_OF_MEMORY;
  }
5
54liuyao 已提交
602
  pWindow->pTsEnd = nodesCloneNode((SNode*)pSession->pCol);
603

X
Xiaoyu Wang 已提交
604 605 606
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
}

X
Xiaoyu Wang 已提交
607 608
static int32_t createWindowLogicNodeByInterval(SLogicPlanContext* pCxt, SIntervalWindowNode* pInterval,
                                               SSelectStmt* pSelect, SLogicNode** pLogicNode) {
609
  SWindowLogicNode* pWindow = (SWindowLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_WINDOW);
610 611 612
  if (NULL == pWindow) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
613 614 615

  pWindow->winType = WINDOW_TYPE_INTERVAL;
  pWindow->interval = ((SValueNode*)pInterval->pInterval)->datum.i;
X
Xiaoyu Wang 已提交
616
  pWindow->intervalUnit = ((SValueNode*)pInterval->pInterval)->unit;
X
Xiaoyu Wang 已提交
617
  pWindow->offset = (NULL != pInterval->pOffset ? ((SValueNode*)pInterval->pOffset)->datum.i : 0);
618
  pWindow->sliding = (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->datum.i : pWindow->interval);
X
Xiaoyu Wang 已提交
619 620
  pWindow->slidingUnit =
      (NULL != pInterval->pSliding ? ((SValueNode*)pInterval->pSliding)->unit : pWindow->intervalUnit);
621
  pWindow->windowAlgo = pCxt->pPlanCxt->streamQuery ? INTERVAL_ALGO_STREAM_SINGLE : INTERVAL_ALGO_HASH;
622

X
bugfix  
Xiaoyu Wang 已提交
623 624
  pWindow->pTspk = nodesCloneNode(pInterval->pCol);
  if (NULL == pWindow->pTspk) {
625
    nodesDestroyNode((SNode*)pWindow);
X
bugfix  
Xiaoyu Wang 已提交
626 627 628
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
629
  return createWindowLogicNodeFinalize(pCxt, pSelect, pWindow, pLogicNode);
X
Xiaoyu Wang 已提交
630 631
}

632
static int32_t createWindowLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
633
  if (NULL == pSelect->pWindow) {
634
    return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
635 636 637
  }

  switch (nodeType(pSelect->pWindow)) {
638 639
    case QUERY_NODE_STATE_WINDOW:
      return createWindowLogicNodeByState(pCxt, (SStateWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
640 641
    case QUERY_NODE_SESSION_WINDOW:
      return createWindowLogicNodeBySession(pCxt, (SSessionWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
642
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
643
      return createWindowLogicNodeByInterval(pCxt, (SIntervalWindowNode*)pSelect->pWindow, pSelect, pLogicNode);
X
Xiaoyu Wang 已提交
644 645 646 647
    default:
      break;
  }

648
  return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
649 650
}

X
Xiaoyu Wang 已提交
651 652 653 654 655 656 657 658
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);

659
  SFillLogicNode* pFill = (SFillLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_FILL);
X
Xiaoyu Wang 已提交
660 661 662 663 664 665 666
  if (NULL == pFill) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_WINDOW, NULL, COLLECT_COL_TYPE_ALL, &pFill->node.pTargets);

  pFill->mode = pFillNode->mode;
X
Xiaoyu Wang 已提交
667
  pFill->timeRange = pFillNode->timeRange;
X
Xiaoyu Wang 已提交
668 669 670 671 672 673 674 675 676
  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 {
677
    nodesDestroyNode((SNode*)pFill);
X
Xiaoyu Wang 已提交
678 679 680 681 682
  }

  return code;
}

X
Xiaoyu Wang 已提交
683 684 685 686 687
static int32_t createSortLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pOrderByList) {
    return TSDB_CODE_SUCCESS;
  }

688
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
689 690 691 692
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

693
  int32_t code = nodesCollectColumns(pSelect, SQL_CLAUSE_ORDER_BY, NULL, COLLECT_COL_TYPE_ALL, &pSort->node.pTargets);
X
Xiaoyu Wang 已提交
694 695 696 697 698 699 700 701 702 703 704

  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 {
705
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
706 707 708 709 710
  }

  return code;
}

X
Xiaoyu Wang 已提交
711 712
static int32_t createColumnByProjections(SLogicPlanContext* pCxt, const char* pStmtName, SNodeList* pExprs,
                                         SNodeList** pCols) {
X
Xiaoyu Wang 已提交
713
  SNodeList* pList = nodesMakeList();
X
Xiaoyu Wang 已提交
714
  if (NULL == pList) {
715 716 717
    return TSDB_CODE_OUT_OF_MEMORY;
  }

X
Xiaoyu Wang 已提交
718
  SNode* pNode;
X
Xiaoyu Wang 已提交
719
  FOREACH(pNode, pExprs) {
720
    if (TSDB_CODE_SUCCESS != nodesListAppend(pList, (SNode*)createColumnByExpr(pStmtName, (SExprNode*)pNode))) {
X
Xiaoyu Wang 已提交
721 722
      nodesDestroyList(pList);
      return TSDB_CODE_OUT_OF_MEMORY;
723 724
    }
  }
725

X
Xiaoyu Wang 已提交
726 727
  *pCols = pList;
  return TSDB_CODE_SUCCESS;
728 729
}

X
Xiaoyu Wang 已提交
730
static int32_t createProjectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
731
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
X
Xiaoyu Wang 已提交
732
  if (NULL == pProject) {
733 734 735
    return TSDB_CODE_OUT_OF_MEMORY;
  }

736
  if (NULL != pSelect->pLimit) {
X
Xiaoyu Wang 已提交
737 738
    pProject->limit = pSelect->pLimit->limit;
    pProject->offset = pSelect->pLimit->offset;
739 740 741 742 743 744 745 746 747 748 749 750 751
  } else {
    pProject->limit = -1;
    pProject->offset = -1;
  }

  if (NULL != pSelect->pSlimit) {
    pProject->slimit = ((SLimitNode*)pSelect->pSlimit)->limit;
    pProject->soffset = ((SLimitNode*)pSelect->pSlimit)->offset;
  } else {
    pProject->slimit = -1;
    pProject->soffset = -1;
  }

X
Xiaoyu Wang 已提交
752
  int32_t code = TSDB_CODE_SUCCESS;
753

X
Xiaoyu Wang 已提交
754
  pProject->pProjections = nodesCloneList(pSelect->pProjectionList);
X
Xiaoyu Wang 已提交
755 756
  if (NULL == pProject->pProjections) {
    code = TSDB_CODE_OUT_OF_MEMORY;
757
  }
X
Xiaoyu Wang 已提交
758
  strcpy(pProject->stmtName, pSelect->stmtName);
759 760

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

764
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
765 766
    *pLogicNode = (SLogicNode*)pProject;
  } else {
767
    nodesDestroyNode((SNode*)pProject);
768
  }
769

770
  return code;
771 772
}

773 774 775 776 777
static int32_t createPartitionLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  if (NULL == pSelect->pPartitionByList) {
    return TSDB_CODE_SUCCESS;
  }

778
  SPartitionLogicNode* pPartition = (SPartitionLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PARTITION);
779 780 781 782
  if (NULL == pPartition) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

783 784
  int32_t code =
      nodesCollectColumns(pSelect, SQL_CLAUSE_PARTITION_BY, NULL, COLLECT_COL_TYPE_ALL, &pPartition->node.pTargets);
785 786 787 788 789 790 791 792 793 794 795

  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 {
796
    nodesDestroyNode((SNode*)pPartition);
797 798 799
  }

  return code;
800 801 802 803 804 805 806
}

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

807 808 809 810 811 812 813 814 815 816 817 818 819 820
  SAggLogicNode* pAgg = (SAggLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_AGG);
  if (NULL == pAgg) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

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

  // rewrite the expression in subsequent clauses
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
821
    code = rewriteExprsForSelect(pAgg->pGroupKeys, pSelect, SQL_CLAUSE_DISTINCT);
822 823 824 825
  }

  // set the output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
826
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
827 828 829 830 831
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
832
    nodesDestroyNode((SNode*)pAgg);
833 834 835
  }

  return code;
836 837
}

838 839
static int32_t createSelectLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) {
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
840
  int32_t     code = createLogicNodeByTable(pCxt, pSelect, pSelect->pFromTable, &pRoot);
841
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
842
    code = createSelectRootLogicNode(pCxt, pSelect, createPartitionLogicNode, &pRoot);
843
  }
844
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
845
    code = createSelectRootLogicNode(pCxt, pSelect, createWindowLogicNode, &pRoot);
846
  }
X
Xiaoyu Wang 已提交
847
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
848
    code = createSelectRootLogicNode(pCxt, pSelect, createFillLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
849
  }
850
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
851
    code = createSelectRootLogicNode(pCxt, pSelect, createAggLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
852
  }
853
  if (TSDB_CODE_SUCCESS == code) {
854
    code = createSelectRootLogicNode(pCxt, pSelect, createIndefRowsFuncLogicNode, &pRoot);
855
  }
X
Xiaoyu Wang 已提交
856 857 858
  if (TSDB_CODE_SUCCESS == code) {
    code = createSelectRootLogicNode(pCxt, pSelect, createInterpFuncLogicNode, &pRoot);
  }
859
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
860
    code = createSelectRootLogicNode(pCxt, pSelect, createDistinctLogicNode, &pRoot);
861
  }
X
Xiaoyu Wang 已提交
862
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
863
    code = createSelectRootLogicNode(pCxt, pSelect, createSortLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
864
  }
865
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
866
    code = createSelectRootLogicNode(pCxt, pSelect, createProjectLogicNode, &pRoot);
X
Xiaoyu Wang 已提交
867
  }
868 869 870 871

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
872
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
873
  }
874 875

  return code;
876 877
}

X
Xiaoyu Wang 已提交
878 879 880
static int32_t createSetOpRootLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, FCreateSetOpLogicNode func,
                                        SLogicNode** pRoot) {
  return createRootLogicNode(pCxt, pSetOperator, pSetOperator->precision, (FCreateLogicNode)func, pRoot);
X
Xiaoyu Wang 已提交
881 882 883 884 885 886 887
}

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

888
  SSortLogicNode* pSort = (SSortLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_SORT);
X
Xiaoyu Wang 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
  if (NULL == pSort) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  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 {
910
    nodesDestroyNode((SNode*)pSort);
X
Xiaoyu Wang 已提交
911 912 913 914 915
  }

  return code;
}

X
Xiaoyu Wang 已提交
916 917
static int32_t createSetOpProjectLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                           SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
  SProjectLogicNode* pProject = (SProjectLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_PROJECT);
  if (NULL == pProject) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  if (NULL != pSetOperator->pLimit) {
    pProject->limit = ((SLimitNode*)pSetOperator->pLimit)->limit;
    pProject->offset = ((SLimitNode*)pSetOperator->pLimit)->offset;
  } else {
    pProject->limit = -1;
    pProject->offset = -1;
  }

  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) {
939 940
    code = createColumnByProjections(pCxt, pSetOperator->stmtName, pSetOperator->pProjectionList,
                                     &pProject->node.pTargets);
X
Xiaoyu Wang 已提交
941 942 943 944 945
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pProject;
  } else {
946
    nodesDestroyNode((SNode*)pProject);
X
Xiaoyu Wang 已提交
947 948 949 950 951
  }

  return code;
}

X
Xiaoyu Wang 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
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;
  }

  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 已提交
971
    code = createColumnByRewriteExprs(pAgg->pGroupKeys, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
972 973 974 975 976
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
977
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
978 979 980 981 982
  }

  return code;
}

X
Xiaoyu Wang 已提交
983 984
static int32_t createSetOpLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator, SLogicNode** pLogicNode) {
  SLogicNode* pSetOp = NULL;
X
Xiaoyu Wang 已提交
985
  int32_t     code = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
986 987 988 989
  switch (pSetOperator->opType) {
    case SET_OP_TYPE_UNION_ALL:
      code = createSetOpProjectLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
990 991 992
    case SET_OP_TYPE_UNION:
      code = createSetOpAggLogicNode(pCxt, pSetOperator, &pSetOp);
      break;
X
Xiaoyu Wang 已提交
993
    default:
994
      code = TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
      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 {
1016
    nodesDestroyNode((SNode*)pSetOp);
X
Xiaoyu Wang 已提交
1017 1018 1019 1020 1021
  }

  return code;
}

X
Xiaoyu Wang 已提交
1022 1023
static int32_t createSetOperatorLogicNode(SLogicPlanContext* pCxt, SSetOperator* pSetOperator,
                                          SLogicNode** pLogicNode) {
1024
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1025
  int32_t     code = createSetOpLogicNode(pCxt, pSetOperator, &pRoot);
1026
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1027
    code = createSetOpRootLogicNode(pCxt, pSetOperator, createSetOpSortLogicNode, &pRoot);
1028 1029 1030 1031 1032
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1033
    nodesDestroyNode((SNode*)pRoot);
1034 1035 1036 1037 1038
  }

  return code;
}

1039
static int32_t getMsgType(ENodeType sqlType) {
X
Xiaoyu Wang 已提交
1040 1041 1042 1043 1044 1045
  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 已提交
1046 1047
    case QUERY_NODE_ALTER_TABLE_STMT:
      return TDMT_VND_ALTER_TABLE;
X
Xiaoyu Wang 已提交
1048 1049 1050 1051
    default:
      break;
  }
  return TDMT_VND_SUBMIT;
1052 1053
}

1054
static int32_t createVnodeModifLogicNode(SLogicPlanContext* pCxt, SVnodeModifOpStmt* pStmt, SLogicNode** pLogicNode) {
1055
  SVnodeModifyLogicNode* pModif = (SVnodeModifyLogicNode*)nodesMakeNode(QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY);
1056 1057 1058
  if (NULL == pModif) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
X
Xiaoyu Wang 已提交
1059
  pModif->modifyType = MODIFY_TABLE_TYPE_INSERT;
wafwerar's avatar
wafwerar 已提交
1060
  TSWAP(pModif->pDataBlocks, pStmt->pDataBlocks);
1061
  pModif->msgType = getMsgType(pStmt->sqlNodeType);
1062 1063
  *pLogicNode = (SLogicNode*)pModif;
  return TSDB_CODE_SUCCESS;
1064 1065
}

X
Xiaoyu Wang 已提交
1066 1067 1068 1069 1070 1071
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 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  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;
    }
  }

  if (TSDB_CODE_SUCCESS == code && NULL != pDelete->pTagIndexCond) {
    pScan->pTagCond = nodesCloneNode(pDelete->pTagIndexCond);
    if (NULL == pScan->pTagCond) {
      code = TSDB_CODE_OUT_OF_MEMORY;
    }
  }

  // set output
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1093
    code = createColumnByRewriteExprs(pScan->pScanCols, &pScan->node.pTargets);
X
Xiaoyu Wang 已提交
1094 1095 1096 1097 1098
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pScan;
  } else {
1099
    nodesDestroyNode((SNode*)pScan);
X
Xiaoyu Wang 已提交
1100 1101 1102
  }

  return code;
X
Xiaoyu Wang 已提交
1103 1104 1105
}

static int32_t createDeleteAggLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
  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 已提交
1117
    code = createColumnByRewriteExprs(pAgg->pAggFuncs, &pAgg->node.pTargets);
X
Xiaoyu Wang 已提交
1118 1119 1120 1121 1122
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = (SLogicNode*)pAgg;
  } else {
1123
    nodesDestroyNode((SNode*)pAgg);
X
Xiaoyu Wang 已提交
1124 1125 1126
  }

  return code;
X
Xiaoyu Wang 已提交
1127 1128
}

X
Xiaoyu Wang 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
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 已提交
1144 1145
  pModify->pAffectedRows = nodesCloneNode(pDelete->pCountFunc);
  if (NULL == pModify->pAffectedRows) {
1146
    nodesDestroyNode((SNode*)pModify);
X
Xiaoyu Wang 已提交
1147 1148 1149 1150 1151
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  *pLogicNode = (SLogicNode*)pModify;
  return TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
1152 1153 1154 1155
}

static int32_t createDeleteLogicNode(SLogicPlanContext* pCxt, SDeleteStmt* pDelete, SLogicNode** pLogicNode) {
  SLogicNode* pRoot = NULL;
X
Xiaoyu Wang 已提交
1156
  int32_t     code = createDeleteScanLogicNode(pCxt, pDelete, &pRoot);
X
Xiaoyu Wang 已提交
1157 1158 1159 1160
  if (TSDB_CODE_SUCCESS == code) {
    code = createDeleteRootLogicNode(pCxt, pDelete, createDeleteAggLogicNode, &pRoot);
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
1161
    code = createDeleteRootLogicNode(pCxt, pDelete, createVnodeModifLogicNodeByDelete, &pRoot);
X
Xiaoyu Wang 已提交
1162 1163 1164 1165 1166
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicNode = pRoot;
  } else {
1167
    nodesDestroyNode((SNode*)pRoot);
X
Xiaoyu Wang 已提交
1168 1169 1170 1171 1172
  }

  return code;
}

1173
static int32_t createQueryLogicNode(SLogicPlanContext* pCxt, SNode* pStmt, SLogicNode** pLogicNode) {
X
Xiaoyu Wang 已提交
1174 1175
  switch (nodeType(pStmt)) {
    case QUERY_NODE_SELECT_STMT:
1176
      return createSelectLogicNode(pCxt, (SSelectStmt*)pStmt, pLogicNode);
1177
    case QUERY_NODE_VNODE_MODIF_STMT:
1178
      return createVnodeModifLogicNode(pCxt, (SVnodeModifOpStmt*)pStmt, pLogicNode);
1179 1180
    case QUERY_NODE_EXPLAIN_STMT:
      return createQueryLogicNode(pCxt, ((SExplainStmt*)pStmt)->pQuery, pLogicNode);
1181 1182
    case QUERY_NODE_SET_OPERATOR:
      return createSetOperatorLogicNode(pCxt, (SSetOperator*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1183 1184
    case QUERY_NODE_DELETE_STMT:
      return createDeleteLogicNode(pCxt, (SDeleteStmt*)pStmt, pLogicNode);
X
Xiaoyu Wang 已提交
1185 1186 1187
    default:
      break;
  }
1188
  return TSDB_CODE_FAILED;
1189 1190
}

X
Xiaoyu Wang 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199
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); }

int32_t createLogicPlan(SPlanContext* pCxt, SLogicSubplan** pLogicSubplan) {
X
Xiaoyu Wang 已提交
1200
  SLogicPlanContext cxt = {.pPlanCxt = pCxt};
X
Xiaoyu Wang 已提交
1201 1202 1203 1204

  SLogicSubplan* pSubplan = (SLogicSubplan*)nodesMakeNode(QUERY_NODE_LOGIC_SUBPLAN);
  if (NULL == pSubplan) {
    return TSDB_CODE_OUT_OF_MEMORY;
X
Xiaoyu Wang 已提交
1205
  }
X
Xiaoyu Wang 已提交
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
  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);
    if (QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY == nodeType(pSubplan->pNode)) {
      pSubplan->subplanType = SUBPLAN_TYPE_MODIFY;
    } else {
      pSubplan->subplanType = SUBPLAN_TYPE_SCAN;
    }
  }

  if (TSDB_CODE_SUCCESS == code) {
    *pLogicSubplan = pSubplan;
  } else {
1223
    nodesDestroyNode((SNode*)pSubplan);
X
Xiaoyu Wang 已提交
1224 1225 1226
  }

  return code;
1227
}