planner.c 3.4 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 16
 */

#include "parser.h"
17
#include "plannerInt.h"
18

19 20
static void extractResSchema(struct SQueryDag* const* pDag, SSchema** pResSchema, int32_t* numOfCols);

21 22 23 24 25 26 27
static void destroyDataSinkNode(SDataSink* pSinkNode) {
  if (pSinkNode == NULL) {
    return;
  }
  tfree(pSinkNode);
}

X
Xiaoyu Wang 已提交
28
void qDestroySubplan(SSubplan* pSubplan) {
29 30 31 32 33 34 35 36 37
  if (pSubplan == NULL) {
    return;
  }

  taosArrayDestroy(pSubplan->pChildren);
  taosArrayDestroy(pSubplan->pParents);
  destroyDataSinkNode(pSubplan->pDataSink);
  // todo destroy pNode
  tfree(pSubplan);
X
Xiaoyu Wang 已提交
38 39
}

40
void qDestroyQueryDag(struct SQueryDag* pDag) {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  if (pDag == NULL) {
    return;
  }

  size_t size = taosArrayGetSize(pDag->pSubplans);
  for(size_t i = 0; i < size; ++i) {
    SArray* pa = taosArrayGetP(pDag->pSubplans, i);

    size_t t = taosArrayGetSize(pa);
    for(int32_t j = 0; j < t; ++j) {
      SSubplan* pSubplan = taosArrayGetP(pa, j);
      qDestroySubplan(pSubplan);
    }
    taosArrayDestroy(pa);
  }

  taosArrayDestroy(pDag->pSubplans);
  tfree(pDag);
59 60
}

61
int32_t qCreateQueryDag(const struct SQueryNode* pNode, struct SQueryDag** pDag, SSchema** pResSchema, int32_t* numOfCols, SArray* pNodeList, uint64_t requestId) {
62 63
  SQueryPlanNode* pLogicPlan;
  int32_t code = createQueryPlan(pNode, &pLogicPlan);
64
  if (TSDB_CODE_SUCCESS != code) {
65
    destroyQueryPlan(pLogicPlan);
66
    return code;
67
  }
H
Haojun Liao 已提交
68

69
  if (pLogicPlan->info.type != QNODE_MODIFY) {
H
Haojun Liao 已提交
70 71 72
//   char* str = NULL;
//   queryPlanToString(pLogicPlan, &str);
//   printf("%s\n", str);
H
Haojun Liao 已提交
73
  }
H
Haojun Liao 已提交
74

75
  code = optimizeQueryPlan(pLogicPlan);
76
  if (TSDB_CODE_SUCCESS != code) {
77
    destroyQueryPlan(pLogicPlan);
78
    return code;
79
  }
H
Haojun Liao 已提交
80

81
  code = createDag(pLogicPlan, NULL, pDag, pNodeList, requestId);
82
  if (TSDB_CODE_SUCCESS != code) {
83
    destroyQueryPlan(pLogicPlan);
84 85
    qDestroyQueryDag(*pDag);
    return code;
86
  }
H
Haojun Liao 已提交
87

88 89
  extractResSchema(pDag, pResSchema, numOfCols);

90
  destroyQueryPlan(pLogicPlan);
H
Haojun Liao 已提交
91
  return TSDB_CODE_SUCCESS;
92
}
X
Xiaoyu Wang 已提交
93

H
Haojun Liao 已提交
94 95
// extract the final result schema
void extractResSchema(struct SQueryDag* const* pDag, SSchema** pResSchema, int32_t* numOfCols) {
96 97
  SArray* pTopSubplan = taosArrayGetP((*pDag)->pSubplans, 0);

H
Haojun Liao 已提交
98
  SSubplan*         pPlan = taosArrayGetP(pTopSubplan, 0);
99 100 101
  SDataBlockSchema* pDataBlockSchema = &(pPlan->pDataSink->schema);

  *numOfCols = pDataBlockSchema->numOfCols;
H
Haojun Liao 已提交
102 103 104 105
  if (*numOfCols > 0) {
    *pResSchema = calloc(pDataBlockSchema->numOfCols, sizeof(SSchema));
    memcpy((*pResSchema), pDataBlockSchema->pSchema, pDataBlockSchema->numOfCols * sizeof(SSchema));
  }
106 107
}

H
Haojun Liao 已提交
108 109
void qSetSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SDownstreamSource* pSource) {
  setSubplanExecutionNode(subplan, templateId, pSource);
110 111
}

112 113
int32_t qSubPlanToString(const SSubplan *subplan, char** str, int32_t* len) {
  return subPlanToString(subplan, str, len);
X
Xiaoyu Wang 已提交
114 115 116 117 118
}

int32_t qStringToSubplan(const char* str, SSubplan** subplan) {
  return stringToSubplan(str, subplan);
}