planner.c 7.2 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

20
typedef struct SCollectPlaceholderValuesCxt {
X
Xiaoyu Wang 已提交
21
  int32_t    errCode;
D
dapan1121 已提交
22
  SArray* pValues;
23 24 25 26 27
} SCollectPlaceholderValuesCxt;

static EDealRes collectPlaceholderValuesImpl(SNode* pNode, void* pContext) {
  if (QUERY_NODE_VALUE == nodeType(pNode) && ((SValueNode*)pNode)->placeholderNo > 0) {
    SCollectPlaceholderValuesCxt* pCxt = pContext;
D
dapan1121 已提交
28
    taosArrayInsert(pCxt->pValues, ((SValueNode*)pNode)->placeholderNo - 1, &pNode);
29 30 31 32 33 34
    return TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR;
  }
  return DEAL_RES_CONTINUE;
}

static int32_t collectPlaceholderValues(SPlanContext* pCxt, SQueryPlan* pPlan) {
D
dapan1121 已提交
35 36 37
  pPlan->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);

  SCollectPlaceholderValuesCxt cxt = {.errCode = TSDB_CODE_SUCCESS, .pValues = pPlan->pPlaceholderValues};
38 39 40 41
  nodesWalkPhysiPlan((SNode*)pPlan, collectPlaceholderValuesImpl, &cxt);
  return cxt.errCode;
}

42
int32_t qCreateQueryPlan(SPlanContext* pCxt, SQueryPlan** pPlan, SArray* pExecNodeList) {
X
Xiaoyu Wang 已提交
43 44
  SLogicNode*      pLogicNode = NULL;
  SLogicSubplan*   pLogicSubplan = NULL;
X
Xiaoyu Wang 已提交
45 46
  SQueryLogicPlan* pLogicPlan = NULL;

X
Xiaoyu Wang 已提交
47 48
  int32_t code = createLogicPlan(pCxt, &pLogicNode);
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
49
    code = optimizeLogicPlan(pCxt, pLogicNode);
X
Xiaoyu Wang 已提交
50
  }
X
Xiaoyu Wang 已提交
51 52
  if (TSDB_CODE_SUCCESS == code) {
    code = splitLogicPlan(pCxt, pLogicNode, &pLogicSubplan);
X
Xiaoyu Wang 已提交
53 54
  }
  if (TSDB_CODE_SUCCESS == code) {
X
Xiaoyu Wang 已提交
55
    code = scaleOutLogicPlan(pCxt, pLogicSubplan, &pLogicPlan);
X
Xiaoyu Wang 已提交
56
  }
X
Xiaoyu Wang 已提交
57 58 59
  if (TSDB_CODE_SUCCESS == code) {
    code = createPhysiPlan(pCxt, pLogicPlan, pPlan, pExecNodeList);
  }
D
dapan1121 已提交
60
  if (TSDB_CODE_SUCCESS == code && pCxt->placeholderNum > 0) {
61 62
    code = collectPlaceholderValues(pCxt, *pPlan);
  }
X
Xiaoyu Wang 已提交
63

64
  nodesDestroyNode(pLogicNode);
X
Xiaoyu Wang 已提交
65 66
  nodesDestroyNode(pLogicSubplan);
  nodesDestroyNode(pLogicPlan);
X
Xiaoyu Wang 已提交
67
  terrno = code;
X
Xiaoyu Wang 已提交
68
  return code;
69 70
}

X
Xiaoyu Wang 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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) {
      if (NULL == pExchange->pSrcEndPoints) {
        pExchange->pSrcEndPoints = nodesMakeList();
        if (NULL == pExchange->pSrcEndPoints) {
          return TSDB_CODE_OUT_OF_MEMORY;
        }
      }
      if (TSDB_CODE_SUCCESS != nodesListStrictAppend(pExchange->pSrcEndPoints, nodesCloneNode(pSource))) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      return TSDB_CODE_SUCCESS;
    }
  }

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

X
Xiaoyu Wang 已提交
97 98
int32_t qSetSubplanExecutionNode(SSubplan* subplan, int32_t groupId, SDownstreamSourceNode* pSource) {
  return setSubplanExecutionNode(subplan->pNode, groupId, pSource);
X
Xiaoyu Wang 已提交
99
}
H
Haojun Liao 已提交
100

D
dapan1121 已提交
101 102
static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam) {
  if (pParam->is_null && 1 == *(pParam->is_null)) {
103 104 105 106 107
    pVal->node.resType.type = TSDB_DATA_TYPE_NULL;
    pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_NULL].bytes;
    return TSDB_CODE_SUCCESS;
  }
  pVal->node.resType.type = pParam->buffer_type;
D
dapan1121 已提交
108
  pVal->node.resType.bytes = NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  switch (pParam->buffer_type) {
    case TSDB_DATA_TYPE_BOOL:
      pVal->datum.b = *((bool*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_TINYINT:
      pVal->datum.i = *((int8_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      pVal->datum.i = *((int16_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_INT:
      pVal->datum.i = *((int32_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_BIGINT:
      pVal->datum.i = *((int64_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_FLOAT:
      pVal->datum.d = *((float*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      pVal->datum.d = *((double*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
D
dapan1121 已提交
133
    case TSDB_DATA_TYPE_NCHAR:
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
      if (NULL == pVal->datum.p) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
      varDataSetLen(pVal->datum.p, pVal->node.resType.bytes);
      strncpy(varDataVal(pVal->datum.p), (const char*)pParam->buffer, pVal->node.resType.bytes);
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      pVal->datum.i = *((int64_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      pVal->datum.u = *((uint8_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      pVal->datum.u = *((uint16_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_UINT:
      pVal->datum.u = *((uint32_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      pVal->datum.u = *((uint64_t*)pParam->buffer);
      break;
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
    case TSDB_DATA_TYPE_MEDIUMBLOB:
      // todo
    default:
      break;
  }
  pVal->translate = true;
  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
168 169
int32_t qStmtBindParam(SQueryPlan* pPlan, TAOS_MULTI_BIND* pParams, int32_t colIdx) {
  if (colIdx < 0) {
D
dapan1121 已提交
170 171 172
    int32_t size = taosArrayGetSize(pPlan->pPlaceholderValues);
    for (int32_t i = 0; i < size; ++i) {
      setValueByBindParam((SValueNode*)taosArrayGetP(pPlan->pPlaceholderValues, i), pParams + i);
D
dapan1121 已提交
173 174
    }
  } else {
D
dapan1121 已提交
175
    setValueByBindParam((SValueNode*)taosArrayGetP(pPlan->pPlaceholderValues, colIdx), pParams);
176 177 178 179
  }
  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
180 181 182 183 184
int32_t qSubPlanToString(const SSubplan* pSubplan, char** pStr, int32_t* pLen) {
  if (SUBPLAN_TYPE_MODIFY == pSubplan->subplanType) {
    SDataInserterNode* insert = (SDataInserterNode*)pSubplan->pDataSink;
    *pLen = insert->size;
    *pStr = insert->pData;
185 186 187
    insert->pData = NULL;
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
188
  return nodesNodeToString((const SNode*)pSubplan, false, pStr, pLen);
X
Xiaoyu Wang 已提交
189
}
H
Haojun Liao 已提交
190

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

X
Xiaoyu Wang 已提交
193
char* qQueryPlanToString(const SQueryPlan* pPlan) {
X
Xiaoyu Wang 已提交
194
  char*   pStr = NULL;
X
Xiaoyu Wang 已提交
195 196 197 198 199
  int32_t len = 0;
  if (TSDB_CODE_SUCCESS != nodesNodeToString(pPlan, false, &pStr, &len)) {
    return NULL;
  }
  return pStr;
200 201
}

X
Xiaoyu Wang 已提交
202
SQueryPlan* qStringToQueryPlan(const char* pStr) {
X
Xiaoyu Wang 已提交
203 204 205 206 207
  SQueryPlan* pPlan = NULL;
  if (TSDB_CODE_SUCCESS != nodesStringToNode(pStr, (SNode**)&pPlan)) {
    return NULL;
  }
  return pPlan;
X
Xiaoyu Wang 已提交
208 209
}

X
Xiaoyu Wang 已提交
210
void qDestroyQueryPlan(SQueryPlan* pPlan) { nodesDestroyNode(pPlan); }