planner.c 3.1 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
void qDestroyQueryDag(struct SQueryDag* pDag) {
22 23 24 25 26 27 28 29 30 31 32 33 34
  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);
    }
35

36 37 38 39 40
    taosArrayDestroy(pa);
  }

  taosArrayDestroy(pDag->pSubplans);
  tfree(pDag);
41 42
}

43
int32_t qCreateQueryDag(const struct SQueryNode* pNode, struct SQueryDag** pDag, SSchema** pResSchema, int32_t* numOfCols, SArray* pNodeList, uint64_t requestId) {
44 45
  SQueryPlanNode* pLogicPlan;
  int32_t code = createQueryPlan(pNode, &pLogicPlan);
46
  if (TSDB_CODE_SUCCESS != code) {
47
    destroyQueryPlan(pLogicPlan);
48
    return code;
49
  }
H
Haojun Liao 已提交
50

51
  if (pLogicPlan->info.type != QNODE_MODIFY) {
H
Haojun Liao 已提交
52 53 54
//   char* str = NULL;
//   queryPlanToString(pLogicPlan, &str);
//   printf("%s\n", str);
H
Haojun Liao 已提交
55
  }
H
Haojun Liao 已提交
56

57
  code = optimizeQueryPlan(pLogicPlan);
58
  if (TSDB_CODE_SUCCESS != code) {
59
    destroyQueryPlan(pLogicPlan);
60
    return code;
61
  }
H
Haojun Liao 已提交
62

63
  code = createDag(pLogicPlan, NULL, pDag, pNodeList, requestId);
64
  if (TSDB_CODE_SUCCESS != code) {
65
    destroyQueryPlan(pLogicPlan);
66 67
    qDestroyQueryDag(*pDag);
    return code;
68
  }
H
Haojun Liao 已提交
69

70 71
  extractResSchema(pDag, pResSchema, numOfCols);

72
  destroyQueryPlan(pLogicPlan);
H
Haojun Liao 已提交
73
  return TSDB_CODE_SUCCESS;
74
}
X
Xiaoyu Wang 已提交
75

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

H
Haojun Liao 已提交
80
  SSubplan*         pPlan = taosArrayGetP(pTopSubplan, 0);
81 82 83
  SDataBlockSchema* pDataBlockSchema = &(pPlan->pDataSink->schema);

  *numOfCols = pDataBlockSchema->numOfCols;
H
Haojun Liao 已提交
84 85 86 87
  if (*numOfCols > 0) {
    *pResSchema = calloc(pDataBlockSchema->numOfCols, sizeof(SSchema));
    memcpy((*pResSchema), pDataBlockSchema->pSchema, pDataBlockSchema->numOfCols * sizeof(SSchema));
  }
88 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);
}