querynodes.h 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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_QUERY_NODES_H_
#define _TD_QUERY_NODES_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "nodes.h"
X
Xiaoyu Wang 已提交
24
#include "tmsg.h"
D
dapan1121 已提交
25
#include "tvariant.h"
26

X
Xiaoyu Wang 已提交
27 28
#define TABLE_TOTAL_COL_NUM(pMeta) ((pMeta)->tableInfo.numOfColumns + (pMeta)->tableInfo.numOfTags)
#define TABLE_META_SIZE(pMeta) (NULL == (pMeta) ? 0 : (sizeof(STableMeta) + TABLE_TOTAL_COL_NUM((pMeta)) * sizeof(SSchema)))
X
Xiaoyu Wang 已提交
29 30
#define VGROUPS_INFO_SIZE(pInfo) (NULL == (pInfo) ? 0 : (sizeof(SVgroupsInfo) + (pInfo)->numOfVgroups * sizeof(SVgroupInfo)))

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
typedef struct SRawExprNode {
  ENodeType nodeType;
  char* p;
  uint32_t n;
  SNode* pNode;
} SRawExprNode;

typedef struct SDataType {
  uint8_t type;
  uint8_t precision;
  uint8_t scale;
  int32_t bytes;
} SDataType;

typedef struct SExprNode {
X
Xiaoyu Wang 已提交
46
  ENodeType type;
47 48 49 50 51 52 53 54 55 56 57 58
  SDataType resType;
  char aliasName[TSDB_COL_NAME_LEN];
  SNodeList* pAssociationList;
} SExprNode;

typedef enum EColumnType {
  COLUMN_TYPE_COLUMN = 1,
  COLUMN_TYPE_TAG
} EColumnType;

typedef struct SColumnNode {
  SExprNode node; // QUERY_NODE_COLUMN
X
Xiaoyu Wang 已提交
59
  uint64_t tableId;
X
Xiaoyu Wang 已提交
60
  int8_t tableType;
61
  col_id_t colId;
62 63 64 65 66 67
  EColumnType colType; // column or tag
  char dbName[TSDB_DB_NAME_LEN];
  char tableName[TSDB_TABLE_NAME_LEN];
  char tableAlias[TSDB_TABLE_NAME_LEN];
  char colName[TSDB_COL_NAME_LEN];
  SNode* pProjectRef;
X
Xiaoyu Wang 已提交
68
  int16_t dataBlockId;
X
Xiaoyu Wang 已提交
69
  int16_t slotId;
X
Xiaoyu Wang 已提交
70 71
} SColumnNode;

X
Xiaoyu Wang 已提交
72 73
typedef struct STargetNode {
  ENodeType type;
X
Xiaoyu Wang 已提交
74
  int16_t dataBlockId;
X
Xiaoyu Wang 已提交
75 76 77 78
  int16_t slotId;
  SNode* pExpr;
} STargetNode;

79 80 81 82
typedef struct SValueNode {
  SExprNode node; // QUERY_NODE_VALUE
  char* literal;
  bool isDuration;
83
  bool translate;
X
Xiaoyu Wang 已提交
84
  bool genByCalc;
85
  int16_t placeholderNo;
86 87 88 89 90 91 92
  union {
    bool b;
    int64_t i;
    uint64_t u;
    double d;
    char* p;
  } datum;
X
Xiaoyu Wang 已提交
93
  char unit;
94 95 96 97 98 99 100 101 102 103 104
} SValueNode;

typedef struct SOperatorNode {
  SExprNode node; // QUERY_NODE_OPERATOR
  EOperatorType opType;
  SNode* pLeft;
  SNode* pRight;
} SOperatorNode;


typedef struct SLogicConditionNode {
105
  SExprNode node; // QUERY_NODE_LOGIC_CONDITION
106 107 108 109 110 111
  ELogicConditionType condType;
  SNodeList* pParameterList;
} SLogicConditionNode;

typedef struct SNodeListNode {
  ENodeType type; // QUERY_NODE_NODE_LIST
X
Xiaoyu Wang 已提交
112
  SDataType dataType;
113 114 115 116 117 118 119 120 121 122 123 124
  SNodeList* pNodeList;
} SNodeListNode;

typedef struct SFunctionNode {
  SExprNode node; // QUERY_NODE_FUNCTION
  char functionName[TSDB_FUNC_NAME_LEN];
  int32_t funcId;
  int32_t funcType;
  SNodeList* pParameterList;
} SFunctionNode;

typedef struct STableNode {
X
Xiaoyu Wang 已提交
125
  SExprNode node;
126 127 128
  char dbName[TSDB_DB_NAME_LEN];
  char tableName[TSDB_TABLE_NAME_LEN];
  char tableAlias[TSDB_TABLE_NAME_LEN];
X
Xiaoyu Wang 已提交
129
  uint8_t precision;
130 131 132 133 134 135 136
} STableNode;

struct STableMeta;

typedef struct SRealTableNode {
  STableNode table; // QUERY_NODE_REAL_TABLE
  struct STableMeta* pMeta;
137
  SVgroupsInfo* pVgroupList;
X
Xiaoyu Wang 已提交
138
  char useDbName[TSDB_DB_NAME_LEN];
139
  double ratio;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
} SRealTableNode;

typedef struct STempTableNode {
  STableNode table; // QUERY_NODE_TEMP_TABLE
  SNode* pSubquery;
} STempTableNode;

typedef enum EJoinType {
  JOIN_TYPE_INNER = 1
} EJoinType;

typedef struct SJoinTableNode {
  STableNode table; // QUERY_NODE_JOIN_TABLE
  EJoinType joinType;
  SNode* pLeft;
  SNode* pRight;
  SNode* pOnCond;
} SJoinTableNode;

typedef enum EGroupingSetType {
  GP_TYPE_NORMAL = 1
} EGroupingSetType;

typedef struct SGroupingSetNode {
  ENodeType type; // QUERY_NODE_GROUPING_SET
  EGroupingSetType groupingSetType;
  SNodeList* pParameterList; 
} SGroupingSetNode;

typedef enum EOrder {
  ORDER_ASC = 1,
  ORDER_DESC
} EOrder;

typedef enum ENullOrder {
  NULL_ORDER_DEFAULT = 1,
  NULL_ORDER_FIRST,
  NULL_ORDER_LAST
} ENullOrder;

typedef struct SOrderByExprNode {
  ENodeType type; // QUERY_NODE_ORDER_BY_EXPR
  SNode* pExpr;
  EOrder order;
  ENullOrder nullOrder;
} SOrderByExprNode;

typedef struct SLimitNode {
  ENodeType type; // QUERY_NODE_LIMIT
189 190
  int64_t limit;
  int64_t offset;
191 192 193 194
} SLimitNode;

typedef struct SStateWindowNode {
  ENodeType type; // QUERY_NODE_STATE_WINDOW
195
  SNode* pCol; // timestamp primary key
196
  SNode* pExpr;
197 198 199 200
} SStateWindowNode;

typedef struct SSessionWindowNode {
  ENodeType type; // QUERY_NODE_SESSION_WINDOW
X
Xiaoyu Wang 已提交
201 202
  SColumnNode* pCol; // timestamp primary key
  SValueNode* pGap; // gap between two session window(in microseconds)
203 204 205 206
} SSessionWindowNode;

typedef struct SIntervalWindowNode {
  ENodeType type; // QUERY_NODE_INTERVAL_WINDOW
X
bugfix  
Xiaoyu Wang 已提交
207
  SNode* pCol; // timestamp primary key
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  SNode* pInterval; // SValueNode
  SNode* pOffset;   // SValueNode
  SNode* pSliding;  // SValueNode
  SNode* pFill;
} SIntervalWindowNode;

typedef enum EFillMode {
  FILL_MODE_NONE = 1,
  FILL_MODE_VALUE,
  FILL_MODE_PREV,
  FILL_MODE_NULL,
  FILL_MODE_LINEAR,
  FILL_MODE_NEXT
} EFillMode;

typedef struct SFillNode {
  ENodeType type; // QUERY_NODE_FILL
  EFillMode mode;
  SNode* pValues; // SNodeListNode
} SFillNode;

typedef struct SSelectStmt {
  ENodeType type; // QUERY_NODE_SELECT_STMT
  bool isDistinct;
X
Xiaoyu Wang 已提交
232
  SNodeList* pProjectionList;
233 234
  SNode* pFromTable;
  SNode* pWhere;
X
Xiaoyu Wang 已提交
235
  SNodeList* pPartitionByList;
236 237 238 239 240 241
  SNode* pWindow;
  SNodeList* pGroupByList; // SGroupingSetNode
  SNode* pHaving;
  SNodeList* pOrderByList; // SOrderByExprNode
  SNode* pLimit;
  SNode* pSlimit;
X
Xiaoyu Wang 已提交
242
  char stmtName[TSDB_TABLE_NAME_LEN];
X
Xiaoyu Wang 已提交
243
  uint8_t precision;
X
Xiaoyu Wang 已提交
244
  bool isEmptyResult;
245 246 247
} SSelectStmt;

typedef enum ESetOperatorType {
X
Xiaoyu Wang 已提交
248 249
  SET_OP_TYPE_UNION_ALL = 1,
  SET_OP_TYPE_UNION
250 251 252 253 254
} ESetOperatorType;

typedef struct SSetOperator {
  ENodeType type; // QUERY_NODE_SET_OPERATOR
  ESetOperatorType opType;
X
Xiaoyu Wang 已提交
255
  SNodeList* pProjectionList;
256 257 258 259 260 261
  SNode* pLeft;
  SNode* pRight;
  SNodeList* pOrderByList; // SOrderByExprNode
  SNode* pLimit;
} SSetOperator;

X
Xiaoyu Wang 已提交
262 263 264 265 266 267 268
typedef enum ESqlClause {
  SQL_CLAUSE_FROM = 1,
  SQL_CLAUSE_WHERE,
  SQL_CLAUSE_PARTITION_BY,
  SQL_CLAUSE_WINDOW,
  SQL_CLAUSE_GROUP_BY,
  SQL_CLAUSE_HAVING,
X
Xiaoyu Wang 已提交
269
  SQL_CLAUSE_DISTINCT,
X
Xiaoyu Wang 已提交
270 271 272 273
  SQL_CLAUSE_SELECT,
  SQL_CLAUSE_ORDER_BY
} ESqlClause;

X
Xiaoyu Wang 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287

typedef enum {
  PAYLOAD_TYPE_KV = 0,
  PAYLOAD_TYPE_RAW = 1,
} EPayloadType;

typedef struct SVgDataBlocks {
  SVgroupInfo vg;
  int32_t     numOfTables;  // number of tables in current submit block
  uint32_t    size;
  char       *pData;        // SMsgDesc + SSubmitReq + SSubmitBlk + ...
} SVgDataBlocks;

typedef struct SVnodeModifOpStmt {
D
stmt  
dapan1121 已提交
288 289 290 291 292 293
  ENodeType    nodeType;
  ENodeType    sqlNodeType;
  SArray*      pDataBlocks;         // data block for each vgroup, SArray<SVgDataBlocks*>.
  uint8_t      payloadType;         // EPayloadType. 0: K-V payload for non-prepare insert, 1: rawPayload for prepare insert
  uint32_t     insertType;          // insert data from [file|sql statement| bound statement]
  const char*  sql;                 // current sql statement position
X
Xiaoyu Wang 已提交
294 295
} SVnodeModifOpStmt;

296 297 298 299 300 301 302 303 304 305 306 307 308
typedef struct SExplainOptions {
  ENodeType type;
  bool verbose;
  double ratio;
} SExplainOptions;

typedef struct SExplainStmt {
  ENodeType type;
  bool analyze;
  SExplainOptions* pOptions;
  SNode* pQuery;
} SExplainStmt;

X
Xiaoyu Wang 已提交
309 310 311
void nodesWalkSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeWalker walker, void* pContext);
void nodesRewriteSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeRewriter rewriter, void* pContext);

X
Xiaoyu Wang 已提交
312
int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, SNodeList** pCols);
X
Xiaoyu Wang 已提交
313 314 315 316

typedef bool (*FFuncClassifier)(int32_t funcId);
int32_t nodesCollectFuncs(SSelectStmt* pSelect, FFuncClassifier classifier, SNodeList** pFuncs);

317 318
bool nodesIsExprNode(const SNode* pNode);

X
Xiaoyu Wang 已提交
319
bool nodesIsUnaryOp(const SOperatorNode* pOp);
320 321 322 323 324 325
bool nodesIsArithmeticOp(const SOperatorNode* pOp);
bool nodesIsComparisonOp(const SOperatorNode* pOp);
bool nodesIsJsonOp(const SOperatorNode* pOp);

bool nodesIsTimeorderQuery(const SNode* pQuery);
bool nodesIsTimelineQuery(const SNode* pQuery);
X
Xiaoyu Wang 已提交
326 327

void* nodesGetValueFromNode(SValueNode *pNode);
D
dapan1121 已提交
328
char* nodesGetStrValueFromNode(SValueNode *pNode);
D
dapan1121 已提交
329
char *getFillModeString(EFillMode mode);
D
dapan1121 已提交
330
void valueNodeToVariant(const SValueNode* pNode, SVariant* pVal);
331 332 333 334 335

#ifdef __cplusplus
}
#endif

D
dapan1121 已提交
336
#endif /*_TD_QUERY_NODES_H_*/