nodes.h 8.5 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 24
/*
 * 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_NODES_H_
#define _TD_NODES_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "tdef.h"

25 26 27 28 29 30 31 32 33 34 35 36 37
#define nodeType(nodeptr) (((const SNode*)(nodeptr))->type)
#define setNodeType(nodeptr, type) (((SNode*)(nodeptr))->type = (type))

#define LIST_LENGTH(l) (NULL != (l) ? (l)->length : 0)

#define FOREACH(node, list)	\
  for (SListCell* cell = (NULL != (list) ? (list)->pHead : NULL); (NULL != cell ? (node = cell->pNode, true) : (node = NULL, false)); cell = cell->pNext)

#define FORBOTH(node1, list1, node2, list2) \
  for (SListCell* cell1 = (NULL != (list1) ? (list1)->pHead : NULL), *cell2 = (NULL != (list2) ? (list2)->pHead : NULL); \
    (NULL == cell1 ? (node1 = NULL, false) : (node1 = cell1->pNode, true)), (NULL == cell2 ? (node2 = NULL, false) : (node2 = cell2->pNode, true)), (node1 != NULL && node2 != NULL); \
    cell1 = cell1->pNext, cell2 = cell2->pNext)

38 39 40 41 42 43 44 45 46 47 48 49
typedef enum ENodeType {
  QUERY_NODE_COLUMN = 1,
  QUERY_NODE_VALUE,
  QUERY_NODE_OPERATOR,
  QUERY_NODE_LOGIC_CONDITION,
  QUERY_NODE_IS_NULL_CONDITION,
  QUERY_NODE_FUNCTION,
  QUERY_NODE_REAL_TABLE,
  QUERY_NODE_TEMP_TABLE,
  QUERY_NODE_JOIN_TABLE,
  QUERY_NODE_GROUPING_SET,
  QUERY_NODE_ORDER_BY_EXPR,
50
  QUERY_NODE_LIMIT,
51 52 53
  QUERY_NODE_STATE_WINDOW,
  QUERY_NODE_SESSION_WINDOW,
  QUERY_NODE_INTERVAL_WINDOW,
54 55
  QUERY_NODE_NODE_LIST,
  QUERY_NODE_FILL,
56

57 58 59
  // only for parser
  QUERY_NODE_TARGET_EXPR,

60
  QUERY_NODE_SET_OPERATOR,
61 62
  QUERY_NODE_SELECT_STMT,
  QUERY_NODE_SHOW_STMT
63 64 65 66 67 68 69 70 71 72
} ENodeType;

/**
 * The first field of a node of any type is guaranteed to be the ENodeType.
 * Hence the type of any node can be gotten by casting it to SNode. 
 */
typedef struct SNode {
  ENodeType type;
} SNode;

73 74 75 76 77 78 79
typedef struct SListCell {
  SNode* pNode;
  struct SListCell* pNext;
} SListCell;

typedef struct SNodeList {
  int16_t length;
80 81
  SListCell* pHead;
  SListCell* pTail;
82 83
} SNodeList;

84 85 86 87 88 89 90 91 92 93 94
typedef struct SDataType {
  uint8_t type;
  uint8_t precision;
  uint8_t scale;
  int32_t bytes;
} SDataType;

typedef struct SExprNode {
  ENodeType nodeType;
  SDataType resType;
  char aliasName[TSDB_COL_NAME_LEN];
95
  SNodeList* pAssociationList;
96 97 98 99 100 101 102 103 104 105 106 107 108
} SExprNode;

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

typedef struct SColumnNode {
  SExprNode node; // QUERY_NODE_COLUMN
  int16_t colId;
  EColumnType colType; // column or tag
  char dbName[TSDB_DB_NAME_LEN];
  char tableName[TSDB_TABLE_NAME_LEN];
109
  char tableAlias[TSDB_TABLE_NAME_LEN];
110
  char colName[TSDB_COL_NAME_LEN];
111
  SNode* pProjectRef;
112 113 114
} SColumnNode;

typedef struct SValueNode {
115
  SExprNode node; // QUERY_NODE_VALUE
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  char* literal;
} SValueNode;

typedef enum EOperatorType {
  // arithmetic operator
  OP_TYPE_ADD = 1,
  OP_TYPE_SUB,
  OP_TYPE_MULTI,
  OP_TYPE_DIV,
  OP_TYPE_MOD,

  // comparison operator
  OP_TYPE_GREATER_THAN,
  OP_TYPE_GREATER_EQUAL,
  OP_TYPE_LOWER_THAN,
  OP_TYPE_LOWER_EQUAL,
  OP_TYPE_EQUAL,
  OP_TYPE_NOT_EQUAL,
  OP_TYPE_IN,
  OP_TYPE_NOT_IN,
  OP_TYPE_LIKE,
  OP_TYPE_NOT_LIKE,
  OP_TYPE_MATCH,
  OP_TYPE_NMATCH,

  // json operator
  OP_TYPE_JSON_GET_VALUE,
  OP_TYPE_JSON_CONTAINS
} EOperatorType;

typedef struct SOperatorNode {
147
  SExprNode node; // QUERY_NODE_OPERATOR
148 149 150 151 152 153 154 155 156 157 158 159 160 161
  EOperatorType opType;
  SNode* pLeft;
  SNode* pRight;
} SOperatorNode;

typedef enum ELogicConditionType {
  LOGIC_COND_TYPE_AND,
  LOGIC_COND_TYPE_OR,
  LOGIC_COND_TYPE_NOT,
} ELogicConditionType;

typedef struct SLogicConditionNode {
  ENodeType type; // QUERY_NODE_LOGIC_CONDITION
  ELogicConditionType condType;
162
  SNodeList* pParameterList;
163 164 165 166 167
} SLogicConditionNode;

typedef struct SIsNullCondNode {
  ENodeType type; // QUERY_NODE_IS_NULL_CONDITION
  SNode* pExpr;
168
  bool isNull;
169 170
} SIsNullCondNode;

171 172 173 174 175
typedef struct SNodeListNode {
  ENodeType type; // QUERY_NODE_NODE_LIST
  SNodeList* pNodeList;
} SNodeListNode;

176 177 178 179
typedef struct SFunctionNode {
  SExprNode type; // QUERY_NODE_FUNCTION
  char functionName[TSDB_FUNC_NAME_LEN];
  int32_t funcId;
180
  SNodeList* pParameterList;
181 182 183 184
} SFunctionNode;

typedef struct STableNode {
  ENodeType type;
185
  char dbName[TSDB_DB_NAME_LEN];
186
  char tableName[TSDB_TABLE_NAME_LEN];
187
  char tableAlias[TSDB_TABLE_NAME_LEN];
188 189
} STableNode;

190 191
struct STableMeta;

192
typedef struct SRealTableNode {
193
  STableNode table; // QUERY_NODE_REAL_TABLE
194
  struct STableMeta* pMeta;
195 196 197
} SRealTableNode;

typedef struct STempTableNode {
198
  STableNode table; // QUERY_NODE_TEMP_TABLE
199 200 201 202 203 204 205 206
  SNode* pSubquery;
} STempTableNode;

typedef enum EJoinType {
  JOIN_TYPE_INNER = 1
} EJoinType;

typedef struct SJoinTableNode {
207
  STableNode table; // QUERY_NODE_JOIN_TABLE
208 209 210 211 212 213 214 215 216 217 218 219 220
  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;
221
  SNodeList* pParameterList; 
222 223 224 225 226 227 228 229
} SGroupingSetNode;

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

typedef enum ENullOrder {
230 231
  NULL_ORDER_DEFAULT = 1,
  NULL_ORDER_FIRST,
232 233 234 235 236 237 238 239 240 241
  NULL_ORDER_LAST
} ENullOrder;

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

242 243
typedef struct SLimitNode {
  ENodeType type; // QUERY_NODE_LIMIT
244 245
  uint64_t limit;
  uint64_t offset;
246
} SLimitNode;
247 248 249 250 251 252 253 254 255 256 257 258 259 260

typedef struct SStateWindowNode {
  ENodeType type; // QUERY_NODE_STATE_WINDOW
  SNode* pCol;
} SStateWindowNode;

typedef struct SSessionWindowNode {
  ENodeType type; // QUERY_NODE_SESSION_WINDOW
  int64_t gap;             // gap between two session window(in microseconds)
  SNode* pCol;
} SSessionWindowNode;

typedef struct SIntervalWindowNode {
  ENodeType type; // QUERY_NODE_INTERVAL_WINDOW
261 262 263 264
  SNode* pInterval; // SValueNode
  SNode* pOffset;   // SValueNode
  SNode* pSliding;  // SValueNode
  SNode* pFill;
265 266
} SIntervalWindowNode;

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
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;

282 283 284
typedef struct SSelectStmt {
  ENodeType type; // QUERY_NODE_SELECT_STMT
  bool isDistinct;
285
  SNodeList* pProjectionList; // SNode
286
  SNode* pFromTable;
287
  SNode* pWhere;
288
  SNodeList* pPartitionByList; // SNode
289
  SNode* pWindow;
290
  SNodeList* pGroupByList; // SGroupingSetNode
291
  SNode* pHaving;
292
  SNodeList* pOrderByList; // SOrderByExprNode
293 294
  SNode* pLimit;
  SNode* pSlimit;
295 296 297 298 299 300 301 302 303 304 305
} SSelectStmt;

typedef enum ESetOperatorType {
  SET_OP_TYPE_UNION_ALL = 1
} ESetOperatorType;

typedef struct SSetOperator {
  ENodeType type; // QUERY_NODE_SET_OPERATOR
  ESetOperatorType opType;
  SNode* pLeft;
  SNode* pRight;
306 307
  SNodeList* pOrderByList; // SOrderByExprNode
  SNode* pLimit;
308 309
} SSetOperator;

310 311 312 313 314 315 316
SNode* nodesMakeNode(ENodeType type);
void nodesDestroyNode(SNode* pNode);

SNodeList* nodesMakeList();
SNodeList* nodesListAppend(SNodeList* pList, SNode* pNode);
void nodesDestroyList(SNodeList* pList);

317 318
typedef bool (*FQueryNodeWalker)(SNode* pNode, void* pContext);

319 320 321 322
void nodesWalkNode(SNode* pNode, FQueryNodeWalker walker, void* pContext);
void nodesWalkList(SNodeList* pList, FQueryNodeWalker walker, void* pContext);
void nodesWalkNodePostOrder(SNode* pNode, FQueryNodeWalker walker, void* pContext);
void nodesWalkListPostOrder(SNodeList* pList, FQueryNodeWalker walker, void* pContext);
323

324
bool nodesWalkStmt(SNode* pNode, FQueryNodeWalker walker, void* pContext);
325

326
bool nodesEqualNode(const SNode* a, const SNode* b);
327

328
void nodesCloneNode(const SNode* pNode);
329

330 331
int32_t nodesNodeToString(const SNode* pNode, char** pStr, int32_t* pLen);
int32_t nodesStringToNode(const char* pStr, SNode** pNode);
332

333 334 335 336
bool nodesIsArithmeticOp(const SOperatorNode* pOp);
bool nodesIsComparisonOp(const SOperatorNode* pOp);
bool nodesIsJsonOp(const SOperatorNode* pOp);

337 338
bool nodesIsTimeorderQuery(const SNode* pQuery);
bool nodesIsTimelineQuery(const SNode* pQuery);
339 340 341 342 343 344

#ifdef __cplusplus
}
#endif

#endif /*_TD_NODES_H_*/