nodes.h 7.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 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
#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)

33
// only be use in FOREACH
34
#define ERASE_NODE(list) cell = nodesListErase(list, cell);
35 36 37

#define REPLACE_NODE(newNode) cell->pNode = (SNode*)(newNode)

38 39 40 41 42
#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)

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

46
typedef enum ENodeType {
47
  // Syntax nodes are used in parser and planner module, and some are also used in executor module, such as COLUMN, VALUE, OPERATOR, FUNCTION and so on.
48 49 50 51 52 53 54 55 56 57
  QUERY_NODE_COLUMN = 1,
  QUERY_NODE_VALUE,
  QUERY_NODE_OPERATOR,
  QUERY_NODE_LOGIC_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,
58
  QUERY_NODE_LIMIT,
59 60 61
  QUERY_NODE_STATE_WINDOW,
  QUERY_NODE_SESSION_WINDOW,
  QUERY_NODE_INTERVAL_WINDOW,
62 63
  QUERY_NODE_NODE_LIST,
  QUERY_NODE_FILL,
X
Xiaoyu Wang 已提交
64
  QUERY_NODE_RAW_EXPR, // Only be used in parser module.
X
Xiaoyu Wang 已提交
65
  QUERY_NODE_TARGET,
X
Xiaoyu Wang 已提交
66
  QUERY_NODE_DATABLOCK_DESC,
X
Xiaoyu Wang 已提交
67
  QUERY_NODE_SLOT_DESC,
X
Xiaoyu Wang 已提交
68
  QUERY_NODE_COLUMN_DEF,
X
Xiaoyu Wang 已提交
69
  QUERY_NODE_DOWNSTREAM_SOURCE,
X
Xiaoyu Wang 已提交
70 71 72
  QUERY_NODE_DATABASE_OPTIONS,
  QUERY_NODE_TABLE_OPTIONS,
  QUERY_NODE_INDEX_OPTIONS,
73

74
  // Statement nodes are used in parser and planner module.
75
  QUERY_NODE_SET_OPERATOR,
76
  QUERY_NODE_SELECT_STMT,
X
Xiaoyu Wang 已提交
77
  QUERY_NODE_VNODE_MODIF_STMT,
78
  QUERY_NODE_CREATE_DATABASE_STMT,
79
  QUERY_NODE_DROP_DATABASE_STMT,
80
  QUERY_NODE_ALTER_DATABASE_STMT,
X
Xiaoyu Wang 已提交
81
  QUERY_NODE_CREATE_TABLE_STMT,
82
  QUERY_NODE_CREATE_SUBTABLE_CLAUSE,
83
  QUERY_NODE_CREATE_MULTI_TABLE_STMT,
84 85
  QUERY_NODE_DROP_TABLE_CLAUSE,
  QUERY_NODE_DROP_TABLE_STMT,
86
  QUERY_NODE_DROP_SUPER_TABLE_STMT,
87
  QUERY_NODE_ALTER_TABLE_STMT,
88 89 90 91 92 93
  QUERY_NODE_CREATE_USER_STMT,
  QUERY_NODE_ALTER_USER_STMT,
  QUERY_NODE_DROP_USER_STMT,
  QUERY_NODE_USE_DATABASE_STMT,
  QUERY_NODE_CREATE_DNODE_STMT,
  QUERY_NODE_DROP_DNODE_STMT,
94
  QUERY_NODE_ALTER_DNODE_STMT,
X
Xiaoyu Wang 已提交
95
  QUERY_NODE_CREATE_INDEX_STMT,
96
  QUERY_NODE_DROP_INDEX_STMT,
X
Xiaoyu Wang 已提交
97
  QUERY_NODE_CREATE_QNODE_STMT,
98 99 100
  QUERY_NODE_DROP_QNODE_STMT,
  QUERY_NODE_CREATE_TOPIC_STMT,
  QUERY_NODE_DROP_TOPIC_STMT,
101
  QUERY_NODE_ALTER_LOCAL_STMT,
X
Xiaoyu Wang 已提交
102 103 104 105
  QUERY_NODE_SHOW_DATABASES_STMT,
  QUERY_NODE_SHOW_TABLES_STMT,
  QUERY_NODE_SHOW_STABLES_STMT,
  QUERY_NODE_SHOW_USERS_STMT,
106 107
  QUERY_NODE_SHOW_DNODES_STMT,
  QUERY_NODE_SHOW_VGROUPS_STMT,
X
Xiaoyu Wang 已提交
108
  QUERY_NODE_SHOW_MNODES_STMT,
X
Xiaoyu Wang 已提交
109 110 111 112 113
  QUERY_NODE_SHOW_MODULES_STMT,
  QUERY_NODE_SHOW_QNODES_STMT,
  QUERY_NODE_SHOW_FUNCTIONS_STMT,
  QUERY_NODE_SHOW_INDEXES_STMT,
  QUERY_NODE_SHOW_STREAMS_STMT,
X
Xiaoyu Wang 已提交
114

X
Xiaoyu Wang 已提交
115
  // logic plan node
X
Xiaoyu Wang 已提交
116
  QUERY_NODE_LOGIC_PLAN_SCAN,
X
Xiaoyu Wang 已提交
117 118
  QUERY_NODE_LOGIC_PLAN_JOIN,
  QUERY_NODE_LOGIC_PLAN_AGG,
X
Xiaoyu Wang 已提交
119
  QUERY_NODE_LOGIC_PLAN_PROJECT,
120
  QUERY_NODE_LOGIC_PLAN_VNODE_MODIF,
X
Xiaoyu Wang 已提交
121
  QUERY_NODE_LOGIC_PLAN_EXCHANGE,
X
Xiaoyu Wang 已提交
122
  QUERY_NODE_LOGIC_PLAN_WINDOW,
X
Xiaoyu Wang 已提交
123 124
  QUERY_NODE_LOGIC_SUBPLAN,
  QUERY_NODE_LOGIC_PLAN,
X
Xiaoyu Wang 已提交
125 126 127 128

  // physical plan node
  QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN,
  QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN,
X
Xiaoyu Wang 已提交
129 130
  QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN,
  QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN,
131
  QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN,
X
Xiaoyu Wang 已提交
132 133
  QUERY_NODE_PHYSICAL_PLAN_PROJECT,
  QUERY_NODE_PHYSICAL_PLAN_JOIN,
X
Xiaoyu Wang 已提交
134 135 136
  QUERY_NODE_PHYSICAL_PLAN_AGG,
  QUERY_NODE_PHYSICAL_PLAN_EXCHANGE,
  QUERY_NODE_PHYSICAL_PLAN_SORT,
X
Xiaoyu Wang 已提交
137
  QUERY_NODE_PHYSICAL_PLAN_INTERVAL,
X
Xiaoyu Wang 已提交
138
  QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW,
X
Xiaoyu Wang 已提交
139
  QUERY_NODE_PHYSICAL_PLAN_DISPATCH,
140
  QUERY_NODE_PHYSICAL_PLAN_INSERT,
X
Xiaoyu Wang 已提交
141 142
  QUERY_NODE_PHYSICAL_SUBPLAN,
  QUERY_NODE_PHYSICAL_PLAN
143 144 145 146 147 148 149 150 151 152
} 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;

153
typedef struct SListCell {
154
  struct SListCell* pPrev;
155
  struct SListCell* pNext;
156
  SNode* pNode;
157 158 159
} SListCell;

typedef struct SNodeList {
X
Xiaoyu Wang 已提交
160
  int32_t length;
161 162
  SListCell* pHead;
  SListCell* pTail;
163 164
} SNodeList;

X
Xiaoyu Wang 已提交
165 166 167 168
#define SNodeptr void*  

SNodeptr nodesMakeNode(ENodeType type);
void nodesDestroyNode(SNodeptr pNode);
169 170

SNodeList* nodesMakeList();
X
Xiaoyu Wang 已提交
171
int32_t nodesListAppend(SNodeList* pList, SNodeptr pNode);
172
int32_t nodesListStrictAppend(SNodeList* pList, SNodeptr pNode);
X
Xiaoyu Wang 已提交
173
int32_t nodesListMakeAppend(SNodeList** pList, SNodeptr pNode);
X
Xiaoyu Wang 已提交
174
int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc);
175
int32_t nodesListStrictAppendList(SNodeList* pTarget, SNodeList* pSrc);
176
SListCell* nodesListErase(SNodeList* pList, SListCell* pCell);
X
Xiaoyu Wang 已提交
177
SNodeptr nodesListGetNode(SNodeList* pList, int32_t index);
178
void nodesDestroyList(SNodeList* pList);
X
Xiaoyu Wang 已提交
179 180
// Only clear the linked list structure, without releasing the elements inside
void nodesClearList(SNodeList* pList);
181

182 183 184 185 186
typedef enum EDealRes {
  DEAL_RES_CONTINUE = 1,
  DEAL_RES_IGNORE_CHILD,
  DEAL_RES_ERROR,
} EDealRes;
187

188
typedef EDealRes (*FNodeWalker)(SNode* pNode, void* pContext);
X
Xiaoyu Wang 已提交
189
void nodesWalkNode(SNodeptr pNode, FNodeWalker walker, void* pContext);
190
void nodesWalkList(SNodeList* pList, FNodeWalker walker, void* pContext);
X
Xiaoyu Wang 已提交
191
void nodesWalkNodePostOrder(SNodeptr pNode, FNodeWalker walker, void* pContext);
192 193 194 195 196 197 198
void nodesWalkListPostOrder(SNodeList* pList, FNodeWalker walker, void* pContext);

typedef EDealRes (*FNodeRewriter)(SNode** pNode, void* pContext);
void nodesRewriteNode(SNode** pNode, FNodeRewriter rewriter, void* pContext);
void nodesRewriteList(SNodeList* pList, FNodeRewriter rewriter, void* pContext);
void nodesRewriteNodePostOrder(SNode** pNode, FNodeRewriter rewriter, void* pContext);
void nodesRewriteListPostOrder(SNodeList* pList, FNodeRewriter rewriter, void* pContext);
199

X
Xiaoyu Wang 已提交
200
bool nodesEqualNode(const SNodeptr a, const SNodeptr b);
201

X
Xiaoyu Wang 已提交
202
SNodeptr nodesCloneNode(const SNodeptr pNode);
X
Xiaoyu Wang 已提交
203
SNodeList* nodesCloneList(const SNodeList* pList);
204

205
const char* nodesNodeName(ENodeType type);
X
Xiaoyu Wang 已提交
206
int32_t nodesNodeToString(const SNodeptr pNode, bool format, char** pStr, int32_t* pLen);
207
int32_t nodesStringToNode(const char* pStr, SNode** pNode);
208

X
Xiaoyu Wang 已提交
209 210 211
int32_t nodesListToString(const SNodeList* pList, bool format, char** pStr, int32_t* pLen);
int32_t nodesStringToList(const char* pStr, SNodeList** pList);

212 213 214 215 216
#ifdef __cplusplus
}
#endif

#endif /*_TD_NODES_H_*/