planner.h 5.3 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

X
Xiaoyu Wang 已提交
23
#include "query.h"
H
Hongze Cheng 已提交
24
#include "tmsg.h"
L
Liu Jicong 已提交
25
#include "tarray.h"
L
Liu Jicong 已提交
26
#include "trpc.h"
27

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

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

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

48 49 50
struct SEpSet;
struct SQueryStmtInfo;

51 52 53
typedef SSchema SSlotSchema;

typedef struct SDataBlockSchema {
X
Xiaoyu Wang 已提交
54 55 56 57
  SSlotSchema *pSchema;
  int32_t      numOfCols;    // number of columns
  int32_t      resultRowSize;
  int16_t      precision;
58 59 60 61 62 63 64
} SDataBlockSchema;

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

X
Xiaoyu Wang 已提交
65 66
typedef struct SDataSink {
  SQueryNodeBasicInfo info;
X
Xiaoyu Wang 已提交
67
  SDataBlockSchema schema;
X
Xiaoyu Wang 已提交
68 69 70 71 72 73 74 75
} SDataSink;

typedef struct SDataDispatcher {
  SDataSink sink;
} SDataDispatcher;

typedef struct SDataInserter {
  SDataSink sink;
X
Xiaoyu Wang 已提交
76 77 78
  int32_t   numOfTables;
  uint32_t  size;
  char     *pData;
X
Xiaoyu Wang 已提交
79 80
} SDataInserter;

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 111 112 113 114 115
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;
116
  uint64_t    srcTemplateId; // template id of datasource suplans
S
Shengliang Guan 已提交
117
  SArray     *pSrcEndPoints;  // SEpAddr, scheduler fill by calling qSetSuplanExecutionNode
118 119 120 121 122 123 124 125
} SExchangePhyNode;

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

126
typedef struct SSubplan {
H
Haojun Liao 已提交
127
  SSubplanId id;           // unique id of the subplan
128
  int32_t    type;         // QUERY_TYPE_MERGE|QUERY_TYPE_PARTIAL|QUERY_TYPE_SCAN|QUERY_TYPE_MODIFY
H
Haojun Liao 已提交
129 130
  int32_t    msgType;      // message type for subplan, used to denote the send message type to vnode.
  int32_t    level;        // the execution level of current subplan, starting from 0 in a top-down manner.
X
Xiaoyu Wang 已提交
131
  SQueryNodeAddr     execNode;    // for the scan/modify subplan, the optional execution node
H
Haojun Liao 已提交
132
  SArray    *pChildren;    // the datasource subplan,from which to fetch the result
X
Xiaoyu Wang 已提交
133 134 135
  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
136
} SSubplan;
137

138
typedef struct SQueryDag {
X
Xiaoyu Wang 已提交
139
  uint64_t queryId;
X
Xiaoyu Wang 已提交
140
  int32_t  numOfSubplans;
L
Liu Jicong 已提交
141
  SArray  *pSubplans; // SArray*<SArray*<SSubplan*>>. The execution level of subplan, starting from 0.
142
} SQueryDag;
143

X
Xiaoyu Wang 已提交
144 145
struct SQueryNode;

H
Haojun Liao 已提交
146 147 148 149 150 151 152
 /**
  * Create the physical plan for the query, according to the AST.
  * @param pQueryInfo
  * @param pDag
  * @param requestId
  * @return
  */
153
int32_t qCreateQueryDag(const struct SQueryNode* pQueryInfo, struct SQueryDag** pDag, uint64_t requestId);
154

155 156 157
// 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 已提交
158
// @ep one execution location of this group of datasource subplans 
159
void qSetSubplanExecutionNode(SSubplan* subplan, uint64_t templateId, SQueryNodeAddr* ep);
160

X
Xiaoyu Wang 已提交
161
int32_t qExplainQuery(const struct SQueryNode* pQueryInfo, struct SEpSet* pQnode, char** str);
162 163

/**
164
 * Convert to subplan to string for the scheduler to send to the executor
165
 */
166
int32_t qSubPlanToString(const SSubplan* subplan, char** str, int32_t* len);
X
Xiaoyu Wang 已提交
167 168

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

X
Xiaoyu Wang 已提交
170 171
void qDestroySubplan(SSubplan* pSubplan);

172 173 174 175 176
/**
 * Destroy the physical plan.
 * @param pQueryPhyNode
 * @return
 */
X
Xiaoyu Wang 已提交
177
void qDestroyQueryDag(SQueryDag* pDag);
178

L
Liu Jicong 已提交
179 180 181
char* qDagToString(const SQueryDag* pDag);
SQueryDag* qStringToDag(const char* pStr);

H
Hongze Cheng 已提交
182 183 184 185
#ifdef __cplusplus
}
#endif

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