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

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

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

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

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

38
void qDestroyQueryDag(struct SQueryDag* pDag) {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  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);
57 58
}

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

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

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

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

86
  destroyQueryPlan(pLogicPlan);
H
Haojun Liao 已提交
87
  return TSDB_CODE_SUCCESS;
88
}
X
Xiaoyu Wang 已提交
89

H
Haojun Liao 已提交
90 91
void qSetSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SDownstreamSource* pSource) {
  setSubplanExecutionNode(subplan, templateId, pSource);
92 93
}

94 95
int32_t qSubPlanToString(const SSubplan *subplan, char** str, int32_t* len) {
  return subPlanToString(subplan, str, len);
X
Xiaoyu Wang 已提交
96 97 98 99 100
}

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