physicalPlan.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

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

19 20 21 22 23 24
static const char* gOpName[] = {
  "Unknown",
#define INCLUDE_AS_NAME
#include "plannerOp.h"
#undef INCLUDE_AS_NAME
};
X
Xiaoyu Wang 已提交
25

26 27 28 29 30 31
typedef struct SPlanContext {
  struct SCatalog* pCatalog;
  struct SQueryDag* pDag;
  SSubplan* pCurrentSubplan;
  SSubplanId nextId;
} SPlanContext;
X
Xiaoyu Wang 已提交
32

33
static void toDataBlockSchema(SQueryPlanNode* pPlanNode, SDataBlockSchema* dataBlockSchema) {
X
Xiaoyu Wang 已提交
34 35 36 37
  SWAP(dataBlockSchema->pSchema, pPlanNode->pSchema, SSchema*);
  dataBlockSchema->numOfCols = pPlanNode->numOfCols;
}

38 39
static SPhyNode* initPhyNode(SQueryPlanNode* pPlanNode, int32_t type, int32_t size) {
  SPhyNode* node = (SPhyNode*)calloc(1, size);
X
Xiaoyu Wang 已提交
40
  node->info.type = type;
41
  node->info.name = gOpName[type];
X
Xiaoyu Wang 已提交
42
  SWAP(node->pTargets, pPlanNode->pExpr, SArray*);
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  toDataBlockSchema(pPlanNode, &(node->targetSchema));
}

static SPhyNode* createTagScanNode(SQueryPlanNode* pPlanNode) {
  return initPhyNode(pPlanNode, OP_TagScan, sizeof(STagScanPhyNode));
}

static SSubplan* initSubplan(SPlanContext* pCxt, int32_t type) {
  SSubplan* subplan = calloc(1, sizeof(SSubplan));
  subplan->id = pCxt->nextId;
  ++(pCxt->nextId.subplanId);
  subplan->type = type;
  subplan->level = 0;
  if (NULL != pCxt->pCurrentSubplan) {
    subplan->level = pCxt->pCurrentSubplan->level + 1;
    if (NULL == pCxt->pCurrentSubplan->pChildern) {
      pCxt->pCurrentSubplan->pChildern = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    }
    taosArrayPush(pCxt->pCurrentSubplan->pChildern, subplan);
    subplan->pParents = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
    taosArrayPush(subplan->pParents, pCxt->pCurrentSubplan);
  }
  pCxt->pCurrentSubplan = subplan;
  return subplan;
}

static uint8_t getScanFlag(SQueryPlanNode* pPlanNode) {
  // todo
  return MASTER_SCAN;
}

static SPhyNode* createTableScanNode(SPlanContext* pCxt, SQueryPlanNode* pPlanNode, SQueryTableInfo* pTable) {
  STableScanPhyNode* node = (STableScanPhyNode*)initPhyNode(pPlanNode, OP_TableScan, sizeof(STableScanPhyNode));
  node->scan.uid = pTable->pMeta->pTableMeta->uid;
  node->scan.tableType = pTable->pMeta->pTableMeta->tableType;
  node->scanFlag = getScanFlag(pPlanNode);
  node->window = pTable->window;
  // todo tag cond
}

static void vgroupToEpSet(const SVgroupMsg* vg, SEpSet* epSet) {
  // todo
X
Xiaoyu Wang 已提交
85 86
}

87 88 89 90 91 92 93 94
static void splitSubplanBySTable(SPlanContext* pCxt, SQueryPlanNode* pPlanNode, SQueryTableInfo* pTable) {
  SVgroupsInfo* vgroupList = pTable->pMeta->vgroupList;
  for (int32_t i = 0; i < pTable->pMeta->vgroupList->numOfVgroups; ++i) {
    SSubplan* subplan = initSubplan(pCxt, QUERY_TYPE_SCAN);
    vgroupToEpSet(&(pTable->pMeta->vgroupList->vgroups[i]), &subplan->execEpSet);
    subplan->pNode = createTableScanNode(pCxt, pPlanNode, pTable);
    // todo reset pCxt->pCurrentSubplan
  }
X
Xiaoyu Wang 已提交
95 96
}

97 98
static SPhyNode* createExchangeNode() {

99 100
}

101 102 103 104 105 106 107 108 109 110
static SPhyNode* createScanNode(SPlanContext* pCxt, SQueryPlanNode* pPlanNode) {
  SQueryTableInfo* pTable = (SQueryTableInfo*)pPlanNode->pExtInfo;
  if (TSDB_SUPER_TABLE == pTable->pMeta->pTableMeta->tableType) {
    splitSubplanBySTable(pCxt, pPlanNode, pTable);
    return createExchangeNode(pCxt, pTable);
  }
  return createTableScanNode(pCxt, pPlanNode, pTable);
}

static SPhyNode* createPhyNode(SPlanContext* pCxt, SQueryPlanNode* pPlanNode) {
X
Xiaoyu Wang 已提交
111 112 113 114 115 116
  SPhyNode* node = NULL;
  switch (pPlanNode->info.type) {
    case QNODE_TAGSCAN:
      node = createTagScanNode(pPlanNode);
      break;
    case QNODE_TABLESCAN:
117
      node = createScanNode(pCxt, pPlanNode);
X
Xiaoyu Wang 已提交
118 119 120 121 122 123 124 125
      break;
    default:
      assert(false);
  }
  if (pPlanNode->pChildren != NULL && taosArrayGetSize(pPlanNode->pChildren) > 0) {
    node->pChildren = taosArrayInit(4, POINTER_BYTES);
    size_t size = taosArrayGetSize(pPlanNode->pChildren);
    for(int32_t i = 0; i < size; ++i) {
126
      SPhyNode* child = createPhyNode(pCxt, taosArrayGet(pPlanNode->pChildren, i));
X
Xiaoyu Wang 已提交
127 128 129
      child->pParent = node;
      taosArrayPush(node->pChildren, &child);
    }
130
  }
X
Xiaoyu Wang 已提交
131
  return node;
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
static void createSubplanByLevel(SPlanContext* pCxt, SQueryPlanNode* pRoot) {
  SSubplan* subplan = initSubplan(pCxt, QUERY_TYPE_MERGE);
  subplan->pNode = createPhyNode(pCxt, pRoot);
  SArray* l0 = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
  taosArrayPush(l0, &subplan);
  taosArrayPush(pCxt->pDag->pSubplans, &l0);
  // todo deal subquery
}

int32_t createDag(SQueryPlanNode* pQueryNode, struct SCatalog* pCatalog, SQueryDag** pDag) {
  SPlanContext context = {
    .pCatalog = pCatalog,
    .pDag = calloc(1, sizeof(SQueryDag)),
    .pCurrentSubplan = NULL
  };
  if (NULL == context.pDag) {
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
  }
  context.pDag->pSubplans = taosArrayInit(TARRAY_MIN_SIZE, POINTER_BYTES);
  createSubplanByLevel(&context, pQueryNode);
  *pDag = context.pDag;
  return TSDB_CODE_SUCCESS;
156 157
}

158 159
int32_t subPlanToString(struct SSubplan *pPhyNode, char** str) {
  return TSDB_CODE_SUCCESS;
160
}