planner.c 4.0 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
14 15
 */

X
Xiaoyu Wang 已提交
16
#include "planner.h"
17

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

X
Xiaoyu Wang 已提交
21 22
static void dumpQueryPlan(SQueryPlan* pPlan) {
  char* pStr = NULL;
23
  nodesNodeToString((SNode*)pPlan, false, &pStr, NULL);
X
Xiaoyu Wang 已提交
24 25 26 27
  planDebugL("Query Plan: %s", pStr);
  taosMemoryFree(pStr);
}

28
int32_t qCreateQueryPlan(SPlanContext* pCxt, SQueryPlan** pPlan, SArray* pExecNodeList) {
X
Xiaoyu Wang 已提交
29
  SLogicSubplan*   pLogicSubplan = NULL;
X
Xiaoyu Wang 已提交
30 31
  SQueryLogicPlan* pLogicPlan = NULL;

X
Xiaoyu Wang 已提交
32
  int32_t code = createLogicPlan(pCxt, &pLogicSubplan);
X
Xiaoyu Wang 已提交
33
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
34
    code = optimizeLogicPlan(pCxt, pLogicSubplan);
X
Xiaoyu Wang 已提交
35
  }
X
Xiaoyu Wang 已提交
36
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
37
    code = splitLogicPlan(pCxt, pLogicSubplan);
X
Xiaoyu Wang 已提交
38 39
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
40
    code = scaleOutLogicPlan(pCxt, pLogicSubplan, &pLogicPlan);
X
Xiaoyu Wang 已提交
41
  }
X
Xiaoyu Wang 已提交
42 43 44
  if (TSDB_CODE_SUCCESS == code) {
    code = createPhysiPlan(pCxt, pLogicPlan, pPlan, pExecNodeList);
  }
X
Xiaoyu Wang 已提交
45 46 47
  if (TSDB_CODE_SUCCESS == code) {
    dumpQueryPlan(*pPlan);
  }
X
Xiaoyu Wang 已提交
48

49 50
  nodesDestroyNode((SNode*)pLogicSubplan);
  nodesDestroyNode((SNode*)pLogicPlan);
X
Xiaoyu Wang 已提交
51
  terrno = code;
X
Xiaoyu Wang 已提交
52
  return code;
53 54
}

X
Xiaoyu Wang 已提交
55 56 57 58
static int32_t setSubplanExecutionNode(SPhysiNode* pNode, int32_t groupId, SDownstreamSourceNode* pSource) {
  if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == nodeType(pNode)) {
    SExchangePhysiNode* pExchange = (SExchangePhysiNode*)pNode;
    if (pExchange->srcGroupId == groupId) {
59
      return nodesListMakeStrictAppend(&pExchange->pSrcEndPoints, nodesCloneNode((SNode*)pSource));
X
Xiaoyu Wang 已提交
60 61 62 63 64 65 66 67 68 69
    }
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE == nodeType(pNode)) {
    SMergePhysiNode* pMerge = (SMergePhysiNode*)pNode;
    if (pMerge->srcGroupId == groupId) {
      SExchangePhysiNode* pExchange =
          (SExchangePhysiNode*)nodesListGetNode(pMerge->node.pChildren, pMerge->numOfChannels - 1);
      if (1 == pMerge->numOfChannels) {
        pMerge->numOfChannels = LIST_LENGTH(pMerge->node.pChildren);
      } else {
        --(pMerge->numOfChannels);
X
Xiaoyu Wang 已提交
70
      }
71
      return nodesListMakeStrictAppend(&pExchange->pSrcEndPoints, nodesCloneNode((SNode*)pSource));
X
Xiaoyu Wang 已提交
72 73 74 75 76 77 78 79 80 81 82
    }
  }

  SNode* pChild = NULL;
  FOREACH(pChild, pNode->pChildren) {
    if (TSDB_CODE_SUCCESS != setSubplanExecutionNode((SPhysiNode*)pChild, groupId, pSource)) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return TSDB_CODE_SUCCESS;
}
H
Haojun Liao 已提交
83

X
Xiaoyu Wang 已提交
84 85
int32_t qSetSubplanExecutionNode(SSubplan* subplan, int32_t groupId, SDownstreamSourceNode* pSource) {
  return setSubplanExecutionNode(subplan->pNode, groupId, pSource);
X
Xiaoyu Wang 已提交
86
}
H
Haojun Liao 已提交
87

X
Xiaoyu Wang 已提交
88
int32_t qSubPlanToString(const SSubplan* pSubplan, char** pStr, int32_t* pLen) {
D
dapan1121 已提交
89
  if (SUBPLAN_TYPE_MODIFY == pSubplan->subplanType && NULL == pSubplan->pNode) {
X
Xiaoyu Wang 已提交
90 91 92
    SDataInserterNode* insert = (SDataInserterNode*)pSubplan->pDataSink;
    *pLen = insert->size;
    *pStr = insert->pData;
93 94 95
    insert->pData = NULL;
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
96
  return nodesNodeToString((const SNode*)pSubplan, false, pStr, pLen);
X
Xiaoyu Wang 已提交
97
}
H
Haojun Liao 已提交
98

X
Xiaoyu Wang 已提交
99
int32_t qStringToSubplan(const char* pStr, SSubplan** pSubplan) { return nodesStringToNode(pStr, (SNode**)pSubplan); }
X
Xiaoyu Wang 已提交
100

X
Xiaoyu Wang 已提交
101
char* qQueryPlanToString(const SQueryPlan* pPlan) {
X
Xiaoyu Wang 已提交
102
  char*   pStr = NULL;
X
Xiaoyu Wang 已提交
103
  int32_t len = 0;
104
  if (TSDB_CODE_SUCCESS != nodesNodeToString((SNode*)pPlan, false, &pStr, &len)) {
X
Xiaoyu Wang 已提交
105 106 107
    return NULL;
  }
  return pStr;
108 109
}

X
Xiaoyu Wang 已提交
110
SQueryPlan* qStringToQueryPlan(const char* pStr) {
X
Xiaoyu Wang 已提交
111 112 113 114 115
  SQueryPlan* pPlan = NULL;
  if (TSDB_CODE_SUCCESS != nodesStringToNode(pStr, (SNode**)&pPlan)) {
    return NULL;
  }
  return pPlan;
X
Xiaoyu Wang 已提交
116 117
}

118
void qDestroyQueryPlan(SQueryPlan* pPlan) { nodesDestroyNode((SNode*)pPlan); }