planner.h 5.0 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

#ifndef _TD_PLANNER_H_
#define _TD_PLANNER_H_

#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
23
#include "tmsg.h"
L
Liu Jicong 已提交
24
#include "tarray.h"
25

26 27
#define QUERY_TYPE_MERGE       1
#define QUERY_TYPE_PARTIAL     2
28
#define QUERY_TYPE_SCAN        3
X
Xiaoyu Wang 已提交
29
#define QUERY_TYPE_MODIFY      4
30

31
enum OPERATOR_TYPE_E {
32 33 34 35 36
  OP_Unknown,
#define INCLUDE_AS_ENUM
#include "plannerOp.h"
#undef INCLUDE_AS_ENUM
  OP_TotalNum
37 38
};

X
Xiaoyu Wang 已提交
39 40 41 42 43 44 45
enum DATASINK_TYPE_E {
  DSINK_Unknown,
  DSINK_Dispatch,
  DSINK_Insert,
  DSINK_TotalNum
};

46 47 48
struct SEpSet;
struct SQueryStmtInfo;

49 50 51 52 53 54 55 56 57 58 59 60
typedef SSchema SSlotSchema;

typedef struct SDataBlockSchema {
  SSlotSchema        *pSchema;
  int32_t             numOfCols;    // number of columns
} SDataBlockSchema;

typedef struct SQueryNodeBasicInfo {
  int32_t     type;          // operator type
  const char *name;          // operator name
} SQueryNodeBasicInfo;

X
Xiaoyu Wang 已提交
61 62 63 64 65 66 67 68 69 70
typedef struct SDataSink {
  SQueryNodeBasicInfo info;
} SDataSink;

typedef struct SDataDispatcher {
  SDataSink sink;
} SDataDispatcher;

typedef struct SDataInserter {
  SDataSink sink;
X
Xiaoyu Wang 已提交
71 72 73
  int32_t   numOfTables;
  uint32_t  size;
  char     *pData;
X
Xiaoyu Wang 已提交
74 75
} SDataInserter;

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
typedef struct SPhyNode {
  SQueryNodeBasicInfo info;
  SArray             *pTargets;      // target list to be computed or scanned at this node
  SArray             *pConditions;   // implicitly-ANDed qual conditions
  SDataBlockSchema    targetSchema;
  // children plan to generated result for current node to process
  // in case of join, multiple plan nodes exist.
  SArray             *pChildren;
  struct SPhyNode    *pParent;
} SPhyNode;

typedef struct SScanPhyNode {
  SPhyNode    node;
  uint64_t    uid;  // unique id of the table
  int8_t      tableType;
} SScanPhyNode;

typedef SScanPhyNode SSystemTableScanPhyNode;
typedef SScanPhyNode STagScanPhyNode;

typedef struct STableScanPhyNode {
  SScanPhyNode scan;
  uint8_t      scanFlag;         // denotes reversed scan of data or not
  STimeWindow  window;
  SArray      *pTagsConditions; // implicitly-ANDed tag qual conditions
} STableScanPhyNode;

typedef STableScanPhyNode STableSeqScanPhyNode;

typedef struct SProjectPhyNode {
  SPhyNode node;
} SProjectPhyNode;

typedef struct SExchangePhyNode {
  SPhyNode    node;
111
  uint64_t    srcTemplateId; // template id of datasource suplans
X
Xiaoyu Wang 已提交
112
  SArray     *pSrcEndPoints;  // SEpAddrMsg, scheduler fill by calling qSetSuplanExecutionNode
113 114 115 116 117 118 119 120
} SExchangePhyNode;

typedef struct SSubplanId {
  uint64_t queryId;
  uint64_t templateId;
  uint64_t subplanId;
} SSubplanId;

121
typedef struct SSubplan {
122
  SSubplanId id;          // unique id of the subplan
123
  int32_t    type;         // QUERY_TYPE_MERGE|QUERY_TYPE_PARTIAL|QUERY_TYPE_SCAN|QUERY_TYPE_MODIFY
X
Xiaoyu Wang 已提交
124
  int32_t    level;        // the execution level of current subplan, starting from 0.
125
  SEpSet     execEpSet;    // for the scan/modify subplan, the optional execution node
X
Xiaoyu Wang 已提交
126 127 128 129
  SArray    *pChildern;    // the datasource subplan,from which to fetch the result
  SArray    *pParents;     // the data destination subplan, get data from current subplan
  SPhyNode  *pNode;        // physical plan of current subplan
  SDataSink *pDataSink;    // data of the subplan flow into the datasink
130
} SSubplan;
131

132
typedef struct SQueryDag {
X
Xiaoyu Wang 已提交
133
  uint64_t queryId;
X
Xiaoyu Wang 已提交
134
  int32_t  numOfSubplans;
L
Liu Jicong 已提交
135
  SArray  *pSubplans; // SArray*<SArray*<SSubplan*>>. The execution level of subplan, starting from 0.
136
} SQueryDag;
137

X
Xiaoyu Wang 已提交
138 139
struct SQueryNode;

140
/**
141
 * Create the physical plan for the query, according to the AST.
142
 */
X
Xiaoyu Wang 已提交
143
int32_t qCreateQueryDag(const struct SQueryNode* pQueryInfo, struct SQueryDag** pDag);
144

145 146 147
// Set datasource of this subplan, multiple calls may be made to a subplan.
// @subplan subplan to be schedule
// @templateId templateId of a group of datasource subplans of this @subplan
D
dapan 已提交
148 149
// @ep one execution location of this group of datasource subplans 
int32_t qSetSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SEpAddr* ep);
150

X
Xiaoyu Wang 已提交
151
int32_t qExplainQuery(const struct SQueryNode* pQueryInfo, struct SEpSet* pQnode, char** str);
152 153

/**
154
 * Convert to subplan to string for the scheduler to send to the executor
155
 */
156
int32_t qSubPlanToString(const SSubplan* subplan, char** str, int32_t* len);
X
Xiaoyu Wang 已提交
157 158

int32_t qStringToSubplan(const char* str, SSubplan** subplan);
159

X
Xiaoyu Wang 已提交
160 161
void qDestroySubplan(SSubplan* pSubplan);

162 163 164 165 166
/**
 * Destroy the physical plan.
 * @param pQueryPhyNode
 * @return
 */
X
Xiaoyu Wang 已提交
167
void qDestroyQueryDag(SQueryDag* pDag);
168

L
Liu Jicong 已提交
169 170 171
char* qDagToString(const SQueryDag* pDag);
SQueryDag* qStringToDag(const char* pStr);

H
Hongze Cheng 已提交
172 173 174 175
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
176
#endif /*_TD_PLANNER_H_*/