planner.c 5.1 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
static void dumpQueryPlan(SQueryPlan* pPlan) {
22
  if (0 == (qDebugFlag & DEBUG_DEBUG)) {
23 24
    return;
  }
X
Xiaoyu Wang 已提交
25
  char* pStr = NULL;
26
  nodesNodeToString((SNode*)pPlan, false, &pStr, NULL);
27
  planDebugL("QID:0x%" PRIx64 " Query Plan: %s", pPlan->queryId, pStr);
X
Xiaoyu Wang 已提交
28 29 30
  taosMemoryFree(pStr);
}

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

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

52 53
  nodesDestroyNode((SNode*)pLogicSubplan);
  nodesDestroyNode((SNode*)pLogicPlan);
X
Xiaoyu Wang 已提交
54
  terrno = code;
X
Xiaoyu Wang 已提交
55
  return code;
56 57
}

X
Xiaoyu Wang 已提交
58 59 60 61
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) {
62
      return nodesListMakeStrictAppend(&pExchange->pSrcEndPoints, nodesCloneNode((SNode*)pSource));
X
Xiaoyu Wang 已提交
63 64 65 66 67 68 69 70 71 72
    }
  } 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 已提交
73
      }
74
      return nodesListMakeStrictAppend(&pExchange->pSrcEndPoints, nodesCloneNode((SNode*)pSource));
X
Xiaoyu Wang 已提交
75 76 77 78 79 80 81 82 83 84 85
    }
  }

  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 已提交
86

X
Xiaoyu Wang 已提交
87
int32_t qSetSubplanExecutionNode(SSubplan* subplan, int32_t groupId, SDownstreamSourceNode* pSource) {
X
Xiaoyu Wang 已提交
88
  planDebug("QID:0x%" PRIx64 " set subplan execution node, groupId:%d", subplan->id.queryId, groupId);
X
Xiaoyu Wang 已提交
89
  return setSubplanExecutionNode(subplan->pNode, groupId, pSource);
X
Xiaoyu Wang 已提交
90
}
H
Haojun Liao 已提交
91

92 93 94 95 96 97 98 99 100 101 102 103 104
static void clearSubplanExecutionNode(SPhysiNode* pNode) {
  if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == nodeType(pNode)) {
    SExchangePhysiNode* pExchange = (SExchangePhysiNode*)pNode;
    NODES_DESTORY_LIST(pExchange->pSrcEndPoints);
  } else if (QUERY_NODE_PHYSICAL_PLAN_MERGE == nodeType(pNode)) {
    SMergePhysiNode* pMerge = (SMergePhysiNode*)pNode;
    pMerge->numOfChannels = LIST_LENGTH(pMerge->node.pChildren);
    SNode* pChild = NULL;
    FOREACH(pChild, pMerge->node.pChildren) { NODES_DESTORY_LIST(((SExchangePhysiNode*)pChild)->pSrcEndPoints); }
  }

  SNode* pChild = NULL;
  FOREACH(pChild, pNode->pChildren) { clearSubplanExecutionNode((SPhysiNode*)pChild); }
105 106
}

X
Xiaoyu Wang 已提交
107 108 109 110
void qClearSubplanExecutionNode(SSubplan* pSubplan) {
  planDebug("QID:0x%" PRIx64 " clear subplan execution node, groupId:%d", pSubplan->id.queryId, pSubplan->id.groupId);
  clearSubplanExecutionNode(pSubplan->pNode);
}
111

X
Xiaoyu Wang 已提交
112
int32_t qSubPlanToString(const SSubplan* pSubplan, char** pStr, int32_t* pLen) {
D
dapan1121 已提交
113
  if (SUBPLAN_TYPE_MODIFY == pSubplan->subplanType && NULL == pSubplan->pNode) {
X
Xiaoyu Wang 已提交
114 115 116
    SDataInserterNode* insert = (SDataInserterNode*)pSubplan->pDataSink;
    *pLen = insert->size;
    *pStr = insert->pData;
117 118 119
    insert->pData = NULL;
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
120
  return nodesNodeToString((const SNode*)pSubplan, false, pStr, pLen);
X
Xiaoyu Wang 已提交
121
}
H
Haojun Liao 已提交
122

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

X
Xiaoyu Wang 已提交
125
char* qQueryPlanToString(const SQueryPlan* pPlan) {
X
Xiaoyu Wang 已提交
126
  char*   pStr = NULL;
X
Xiaoyu Wang 已提交
127
  int32_t len = 0;
128
  if (TSDB_CODE_SUCCESS != nodesNodeToString((SNode*)pPlan, false, &pStr, &len)) {
X
Xiaoyu Wang 已提交
129 130 131
    return NULL;
  }
  return pStr;
132 133
}

X
Xiaoyu Wang 已提交
134
SQueryPlan* qStringToQueryPlan(const char* pStr) {
X
Xiaoyu Wang 已提交
135 136 137 138 139
  SQueryPlan* pPlan = NULL;
  if (TSDB_CODE_SUCCESS != nodesStringToNode(pStr, (SNode**)&pPlan)) {
    return NULL;
  }
  return pPlan;
X
Xiaoyu Wang 已提交
140 141
}

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