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

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

static EDealRes collectPlaceholderValuesImpl(SNode* pNode, void* pContext) {
  if (QUERY_NODE_VALUE == nodeType(pNode) && ((SValueNode*)pNode)->placeholderNo > 0) {
    SCollectPlaceholderValuesCxt* pCxt = pContext;
D
dapan1121 已提交
29
    taosArrayInsert(pCxt->pValues, ((SValueNode*)pNode)->placeholderNo - 1, &pNode);
30 31 32 33 34 35
    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 已提交
36 37 38
  pPlan->pPlaceholderValues = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);

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

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

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

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

X
Xiaoyu Wang 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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 已提交
97

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

D
dapan1121 已提交
102 103
static int32_t setValueByBindParam(SValueNode* pVal, TAOS_MULTI_BIND* pParam) {
  if (pParam->is_null && 1 == *(pParam->is_null)) {
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;
  }
D
dapan1121 已提交
108
  int32_t inputSize = (NULL != pParam->length ? *(pParam->length) : tDataTypes[pParam->buffer_type].bytes);
109
  pVal->node.resType.type = pParam->buffer_type;
D
dapan1121 已提交
110
  pVal->node.resType.bytes = inputSize;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  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:
      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;
D
dapan1121 已提交
142 143 144 145 146 147
    case TSDB_DATA_TYPE_NCHAR: {
      pVal->node.resType.bytes *= TSDB_NCHAR_SIZE;
      pVal->datum.p = taosMemoryCalloc(1, pVal->node.resType.bytes + VARSTR_HEADER_SIZE + 1);
      if (NULL == pVal->datum.p) {
        return TSDB_CODE_OUT_OF_MEMORY;
      }
X
Xiaoyu Wang 已提交
148

D
dapan1121 已提交
149
      int32_t output = 0;
X
Xiaoyu Wang 已提交
150 151
      if (!taosMbsToUcs4(pParam->buffer, inputSize, (TdUcs4*)varDataVal(pVal->datum.p), pVal->node.resType.bytes,
                         &output)) {
D
dapan1121 已提交
152 153 154 155 156 157
        return errno;
      }
      varDataSetLen(pVal->datum.p, output);
      pVal->node.resType.bytes = output;
      break;
    }
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    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 已提交
185
static EDealRes updatePlanQueryId(SNode* pNode, void* pContext) {
X
Xiaoyu Wang 已提交
186 187
  int64_t queryId = *(uint64_t*)pContext;

D
dapan1121 已提交
188 189 190 191 192 193 194 195 196 197 198
  if (QUERY_NODE_PHYSICAL_PLAN == nodeType(pNode)) {
    SQueryPlan* planNode = (SQueryPlan*)pNode;
    planNode->queryId = queryId;
  } else if (QUERY_NODE_PHYSICAL_SUBPLAN == nodeType(pNode)) {
    SSubplan* subplanNode = (SSubplan*)pNode;
    subplanNode->id.queryId = queryId;
  }

  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static int32_t calcConstNode(SNode** pNode) {
  if (NULL == *pNode) {
    return TSDB_CODE_SUCCESS;
  }

  SNode*  pNew = NULL;
  int32_t code = scalarCalculateConstants(*pNode, &pNew);
  if (TSDB_CODE_SUCCESS == code) {
    *pNode = pNew;
  }
  return code;
}

static int32_t calcConstList(SNodeList* pList) {
  SNode* pNode = NULL;
  FOREACH(pNode, pList) {
    SNode*  pNew = NULL;
    int32_t code = scalarCalculateConstants(pNode, &pNew);
    if (TSDB_CODE_SUCCESS == code) {
      REPLACE_NODE(pNew);
    } else {
      return code;
    }
  }
  return TSDB_CODE_SUCCESS;
}

static bool isEmptyResultCond(SNode** pCond) {
  if (QUERY_NODE_VALUE != nodeType(*pCond)) {
    return false;
  }
  if (((SValueNode*)*pCond)->datum.b) {
    nodesDestroyNode(*pCond);
    *pCond = NULL;
    return false;
  }
  return true;
}

static int32_t calcConstSpecificPhysiNode(SPhysiNode* pPhyNode) {
  switch (nodeType(pPhyNode)) {
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
      return TSDB_CODE_SUCCESS;
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
      return calcConstList(((SProjectPhysiNode*)pPhyNode)->pProjections);
    case QUERY_NODE_PHYSICAL_PLAN_JOIN:
      return calcConstNode(&(((SJoinPhysiNode*)pPhyNode)->pOnConditions));
    case QUERY_NODE_PHYSICAL_PLAN_AGG:
      return calcConstList(((SAggPhysiNode*)pPhyNode)->pExprs);
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
      return calcConstList(((SSortPhysiNode*)pPhyNode)->pExprs);
    case QUERY_NODE_PHYSICAL_PLAN_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW:
    case QUERY_NODE_PHYSICAL_PLAN_STATE_WINDOW:
      return calcConstList(((SWinodwPhysiNode*)pPhyNode)->pExprs);
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
      return calcConstList(((SPartitionPhysiNode*)pPhyNode)->pExprs);
    default:
      break;
  }
  return TSDB_CODE_SUCCESS;
}

static int32_t calcConstSubplan(SPhysiNode* pPhyNode, bool* pEmptyResult) {
  int32_t code = calcConstNode(&pPhyNode->pConditions);
  if (TSDB_CODE_SUCCESS == code) {
    code = calcConstSpecificPhysiNode(pPhyNode);
  }
  if (TSDB_CODE_SUCCESS != code) {
    return code;
  }

  *pEmptyResult = isEmptyResultCond(&pPhyNode->pConditions);
  if (*pEmptyResult) {
    return TSDB_CODE_SUCCESS;
  }

  *pEmptyResult = true;

  bool   subEmptyResult = false;
  SNode* pChild = NULL;
  FOREACH(pChild, pPhyNode->pChildren) {
    code = calcConstSubplan((SPhysiNode*)pChild, &subEmptyResult);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
    if (!subEmptyResult) {
      *pEmptyResult = false;
    }
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t calcConstPhysiPlan(SQueryPlan* pPlan, bool* pEmptyResult) {
  *pEmptyResult = true;

  bool           subEmptyResult = false;
  SNodeListNode* pNode = nodesListGetNode(pPlan->pSubplans, 0);
  SNode*         pSubplan = NULL;
  FOREACH(pSubplan, pNode->pNodeList) {
    int32_t code = calcConstSubplan(((SSubplan*)pSubplan)->pNode, pEmptyResult);
    if (TSDB_CODE_SUCCESS != code) {
      return code;
    }
    if (!subEmptyResult) {
      *pEmptyResult = false;
    }
  }
  return TSDB_CODE_SUCCESS;
}

int32_t qStmtBindParam(SQueryPlan* pPlan, TAOS_MULTI_BIND* pParams, int32_t colIdx, uint64_t queryId,
                       bool* pEmptyResult) {
D
dapan1121 已提交
320
  int32_t size = taosArrayGetSize(pPlan->pPlaceholderValues);
D
dapan1121 已提交
321
  int32_t code = 0;
X
Xiaoyu Wang 已提交
322

D
dapan1121 已提交
323
  if (colIdx < 0) {
D
dapan1121 已提交
324
    for (int32_t i = 0; i < size; ++i) {
D
dapan1121 已提交
325 326 327 328
      code = setValueByBindParam((SValueNode*)taosArrayGetP(pPlan->pPlaceholderValues, i), pParams + i);
      if (code) {
        return code;
      }
D
dapan1121 已提交
329 330
    }
  } else {
D
dapan1121 已提交
331 332 333 334
    code = setValueByBindParam((SValueNode*)taosArrayGetP(pPlan->pPlaceholderValues, colIdx), pParams);
    if (code) {
      return code;
    }
335
  }
D
dapan1121 已提交
336 337 338

  if (colIdx < 0 || ((colIdx + 1) == size)) {
    nodesWalkPhysiPlan((SNode*)pPlan, updatePlanQueryId, &queryId);
X
Xiaoyu Wang 已提交
339
    code = calcConstPhysiPlan(pPlan, pEmptyResult);
D
dapan1121 已提交
340
  }
X
Xiaoyu Wang 已提交
341 342

  return code;
343 344
}

X
Xiaoyu Wang 已提交
345 346 347 348 349
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;
350 351 352
    insert->pData = NULL;
    return TSDB_CODE_SUCCESS;
  }
X
Xiaoyu Wang 已提交
353
  return nodesNodeToString((const SNode*)pSubplan, false, pStr, pLen);
X
Xiaoyu Wang 已提交
354
}
H
Haojun Liao 已提交
355

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

X
Xiaoyu Wang 已提交
358
char* qQueryPlanToString(const SQueryPlan* pPlan) {
X
Xiaoyu Wang 已提交
359
  char*   pStr = NULL;
X
Xiaoyu Wang 已提交
360 361 362 363 364
  int32_t len = 0;
  if (TSDB_CODE_SUCCESS != nodesNodeToString(pPlan, false, &pStr, &len)) {
    return NULL;
  }
  return pStr;
365 366
}

X
Xiaoyu Wang 已提交
367
SQueryPlan* qStringToQueryPlan(const char* pStr) {
X
Xiaoyu Wang 已提交
368 369 370 371 372
  SQueryPlan* pPlan = NULL;
  if (TSDB_CODE_SUCCESS != nodesStringToNode(pStr, (SNode**)&pPlan)) {
    return NULL;
  }
  return pPlan;
X
Xiaoyu Wang 已提交
373 374
}

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