planner.c 5.7 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"
20
#include "tglobal.h"
X
Xiaoyu Wang 已提交
21

X
Xiaoyu Wang 已提交
22
static void dumpQueryPlan(SQueryPlan* pPlan) {
23
  if (!tsQueryPlannerTrace) {
24 25
    return;
  }
X
Xiaoyu Wang 已提交
26
  char* pStr = NULL;
27
  nodesNodeToString((SNode*)pPlan, false, &pStr, NULL);
28
  planDebugL("QID:0x%" PRIx64 " Query Plan: %s", pPlan->queryId, pStr);
X
Xiaoyu Wang 已提交
29 30 31
  taosMemoryFree(pStr);
}

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

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

57 58
  nodesDestroyNode((SNode*)pLogicSubplan);
  nodesDestroyNode((SNode*)pLogicPlan);
X
Xiaoyu Wang 已提交
59
  terrno = code;
X
Xiaoyu Wang 已提交
60
  return code;
61 62
}

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

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

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

97 98 99 100 101 102 103 104 105 106 107 108 109
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); }
110 111
}

X
Xiaoyu Wang 已提交
112 113 114 115
void qClearSubplanExecutionNode(SSubplan* pSubplan) {
  planDebug("QID:0x%" PRIx64 " clear subplan execution node, groupId:%d", pSubplan->id.queryId, pSubplan->id.groupId);
  clearSubplanExecutionNode(pSubplan->pNode);
}
116

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

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

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
int32_t qSubPlanToMsg(const SSubplan* pSubplan, char** pStr, int32_t* pLen) {
  if (SUBPLAN_TYPE_MODIFY == pSubplan->subplanType && NULL == pSubplan->pNode) {
    SDataInserterNode* insert = (SDataInserterNode*)pSubplan->pDataSink;
    *pLen = insert->size;
    *pStr = insert->pData;
    insert->pData = NULL;
    return TSDB_CODE_SUCCESS;
  }
  return nodesNodeToMsg((const SNode*)pSubplan, pStr, pLen);
}

int32_t qMsgToSubplan(const char* pStr, int32_t len, SSubplan** pSubplan) {
  return nodesMsgToNode(pStr, len, (SNode**)pSubplan);
}

X
Xiaoyu Wang 已提交
145
char* qQueryPlanToString(const SQueryPlan* pPlan) {
X
Xiaoyu Wang 已提交
146
  char*   pStr = NULL;
X
Xiaoyu Wang 已提交
147
  int32_t len = 0;
148
  if (TSDB_CODE_SUCCESS != nodesNodeToString((SNode*)pPlan, false, &pStr, &len)) {
X
Xiaoyu Wang 已提交
149 150 151
    return NULL;
  }
  return pStr;
152 153
}

X
Xiaoyu Wang 已提交
154
SQueryPlan* qStringToQueryPlan(const char* pStr) {
X
Xiaoyu Wang 已提交
155 156 157 158 159
  SQueryPlan* pPlan = NULL;
  if (TSDB_CODE_SUCCESS != nodesStringToNode(pStr, (SNode**)&pPlan)) {
    return NULL;
  }
  return pPlan;
X
Xiaoyu Wang 已提交
160 161
}

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