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

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

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

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

X
Xiaoyu Wang 已提交
61 62 63
static int32_t setSubplanExecutionNode(SPhysiNode* pNode, int32_t groupId, SDownstreamSourceNode* pSource) {
  if (QUERY_NODE_PHYSICAL_PLAN_EXCHANGE == nodeType(pNode)) {
    SExchangePhysiNode* pExchange = (SExchangePhysiNode*)pNode;
64
    if (groupId >= pExchange->srcStartGroupId && groupId <= pExchange->srcEndGroupId) {
65
      return nodesListMakeStrictAppend(&pExchange->pSrcEndPoints, nodesCloneNode((SNode*)pSource));
X
Xiaoyu Wang 已提交
66 67 68 69 70 71 72 73 74 75
    }
  } 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 已提交
76
      }
77
      return nodesListMakeStrictAppend(&pExchange->pSrcEndPoints, nodesCloneNode((SNode*)pSource));
X
Xiaoyu Wang 已提交
78 79 80 81 82 83 84 85 86 87 88
    }
  }

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

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

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

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

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

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

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
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 已提交
143
SQueryPlan* qStringToQueryPlan(const char* pStr) {
X
Xiaoyu Wang 已提交
144 145 146 147 148
  SQueryPlan* pPlan = NULL;
  if (TSDB_CODE_SUCCESS != nodesStringToNode(pStr, (SNode**)&pPlan)) {
    return NULL;
  }
  return pPlan;
X
Xiaoyu Wang 已提交
149 150
}

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