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

52
  if (pLogicPlan->info.type != QNODE_MODIFY) {
53 54 55 56
    char* str = NULL;
    queryPlanToString(pLogicPlan, &str);
    qDebug("reqId:0x%"PRIx64": %s", requestId, str);
    tfree(str);
H
Haojun Liao 已提交
57
  }
H
Haojun Liao 已提交
58

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

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

72 73
  extractResSchema(pDag, pResSchema, numOfCols);

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

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

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

  *numOfCols = pDataBlockSchema->numOfCols;
H
Haojun Liao 已提交
86 87 88 89
  if (*numOfCols > 0) {
    *pResSchema = calloc(pDataBlockSchema->numOfCols, sizeof(SSchema));
    memcpy((*pResSchema), pDataBlockSchema->pSchema, pDataBlockSchema->numOfCols * sizeof(SSchema));
  }
90 91
}

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

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

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