nodesUtilFuncs.c 62.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16
#include "cmdnodes.h"
X
Xiaoyu Wang 已提交
17
#include "nodesUtil.h"
X
Xiaoyu Wang 已提交
18
#include "plannodes.h"
19
#include "querynodes.h"
X
Xiaoyu Wang 已提交
20
#include "taos.h"
21
#include "taoserror.h"
X
Xiaoyu Wang 已提交
22
#include "thash.h"
23

X
Xiaoyu Wang 已提交
24 25 26 27 28 29 30 31 32
static SNode* makeNode(ENodeType type, size_t size) {
  SNode* p = taosMemoryCalloc(1, size);
  if (NULL == p) {
    return NULL;
  }
  setNodeType(p, type);
  return p;
}

33
SNode* nodesMakeNode(ENodeType type) {
34 35
  switch (type) {
    case QUERY_NODE_COLUMN:
X
Xiaoyu Wang 已提交
36
      return makeNode(type, sizeof(SColumnNode));
37
    case QUERY_NODE_VALUE:
X
Xiaoyu Wang 已提交
38
      return makeNode(type, sizeof(SValueNode));
39
    case QUERY_NODE_OPERATOR:
X
Xiaoyu Wang 已提交
40
      return makeNode(type, sizeof(SOperatorNode));
41
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
42
      return makeNode(type, sizeof(SLogicConditionNode));
43
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
44
      return makeNode(type, sizeof(SFunctionNode));
45
    case QUERY_NODE_REAL_TABLE:
X
Xiaoyu Wang 已提交
46
      return makeNode(type, sizeof(SRealTableNode));
47
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
48
      return makeNode(type, sizeof(STempTableNode));
49
    case QUERY_NODE_JOIN_TABLE:
X
Xiaoyu Wang 已提交
50
      return makeNode(type, sizeof(SJoinTableNode));
51
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
52
      return makeNode(type, sizeof(SGroupingSetNode));
53
    case QUERY_NODE_ORDER_BY_EXPR:
X
Xiaoyu Wang 已提交
54
      return makeNode(type, sizeof(SOrderByExprNode));
55
    case QUERY_NODE_LIMIT:
X
Xiaoyu Wang 已提交
56
      return makeNode(type, sizeof(SLimitNode));
57
    case QUERY_NODE_STATE_WINDOW:
X
Xiaoyu Wang 已提交
58
      return makeNode(type, sizeof(SStateWindowNode));
59
    case QUERY_NODE_SESSION_WINDOW:
X
Xiaoyu Wang 已提交
60
      return makeNode(type, sizeof(SSessionWindowNode));
61
    case QUERY_NODE_INTERVAL_WINDOW:
X
Xiaoyu Wang 已提交
62
      return makeNode(type, sizeof(SIntervalWindowNode));
63
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
64
      return makeNode(type, sizeof(SNodeListNode));
65
    case QUERY_NODE_FILL:
X
Xiaoyu Wang 已提交
66
      return makeNode(type, sizeof(SFillNode));
67
    case QUERY_NODE_RAW_EXPR:
X
Xiaoyu Wang 已提交
68
      return makeNode(type, sizeof(SRawExprNode));
69
    case QUERY_NODE_TARGET:
X
Xiaoyu Wang 已提交
70
      return makeNode(type, sizeof(STargetNode));
71
    case QUERY_NODE_DATABLOCK_DESC:
X
Xiaoyu Wang 已提交
72
      return makeNode(type, sizeof(SDataBlockDescNode));
73
    case QUERY_NODE_SLOT_DESC:
X
Xiaoyu Wang 已提交
74
      return makeNode(type, sizeof(SSlotDescNode));
75
    case QUERY_NODE_COLUMN_DEF:
X
Xiaoyu Wang 已提交
76
      return makeNode(type, sizeof(SColumnDefNode));
X
Xiaoyu Wang 已提交
77
    case QUERY_NODE_DOWNSTREAM_SOURCE:
X
Xiaoyu Wang 已提交
78
      return makeNode(type, sizeof(SDownstreamSourceNode));
X
Xiaoyu Wang 已提交
79
    case QUERY_NODE_DATABASE_OPTIONS:
X
Xiaoyu Wang 已提交
80
      return makeNode(type, sizeof(SDatabaseOptions));
X
Xiaoyu Wang 已提交
81
    case QUERY_NODE_TABLE_OPTIONS:
X
Xiaoyu Wang 已提交
82
      return makeNode(type, sizeof(STableOptions));
X
Xiaoyu Wang 已提交
83
    case QUERY_NODE_INDEX_OPTIONS:
X
Xiaoyu Wang 已提交
84
      return makeNode(type, sizeof(SIndexOptions));
85
    case QUERY_NODE_EXPLAIN_OPTIONS:
X
Xiaoyu Wang 已提交
86
      return makeNode(type, sizeof(SExplainOptions));
87
    case QUERY_NODE_STREAM_OPTIONS:
X
Xiaoyu Wang 已提交
88
      return makeNode(type, sizeof(SStreamOptions));
89
    case QUERY_NODE_LEFT_VALUE:
X
Xiaoyu Wang 已提交
90
      return makeNode(type, sizeof(SLeftValueNode));
91 92
    case QUERY_NODE_COLUMN_REF:
      return makeNode(type, sizeof(SColumnDefNode));
93
    case QUERY_NODE_SET_OPERATOR:
X
Xiaoyu Wang 已提交
94
      return makeNode(type, sizeof(SSetOperator));
95
    case QUERY_NODE_SELECT_STMT:
X
Xiaoyu Wang 已提交
96
      return makeNode(type, sizeof(SSelectStmt));
97
    case QUERY_NODE_VNODE_MODIF_STMT:
X
Xiaoyu Wang 已提交
98
      return makeNode(type, sizeof(SVnodeModifOpStmt));
99
    case QUERY_NODE_CREATE_DATABASE_STMT:
X
Xiaoyu Wang 已提交
100
      return makeNode(type, sizeof(SCreateDatabaseStmt));
101
    case QUERY_NODE_DROP_DATABASE_STMT:
X
Xiaoyu Wang 已提交
102
      return makeNode(type, sizeof(SDropDatabaseStmt));
103
    case QUERY_NODE_ALTER_DATABASE_STMT:
X
Xiaoyu Wang 已提交
104
      return makeNode(type, sizeof(SAlterDatabaseStmt));
105
    case QUERY_NODE_CREATE_TABLE_STMT:
X
Xiaoyu Wang 已提交
106
      return makeNode(type, sizeof(SCreateTableStmt));
107
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE:
X
Xiaoyu Wang 已提交
108
      return makeNode(type, sizeof(SCreateSubTableClause));
109
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
X
Xiaoyu Wang 已提交
110
      return makeNode(type, sizeof(SCreateMultiTableStmt));
111
    case QUERY_NODE_DROP_TABLE_CLAUSE:
X
Xiaoyu Wang 已提交
112
      return makeNode(type, sizeof(SDropTableClause));
113
    case QUERY_NODE_DROP_TABLE_STMT:
X
Xiaoyu Wang 已提交
114
      return makeNode(type, sizeof(SDropTableStmt));
115
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:
X
Xiaoyu Wang 已提交
116
      return makeNode(type, sizeof(SDropSuperTableStmt));
117
    case QUERY_NODE_ALTER_TABLE_STMT:
X
Xiaoyu Wang 已提交
118
      return makeNode(type, sizeof(SAlterTableStmt));
119
    case QUERY_NODE_CREATE_USER_STMT:
X
Xiaoyu Wang 已提交
120
      return makeNode(type, sizeof(SCreateUserStmt));
121
    case QUERY_NODE_ALTER_USER_STMT:
X
Xiaoyu Wang 已提交
122
      return makeNode(type, sizeof(SAlterUserStmt));
123
    case QUERY_NODE_DROP_USER_STMT:
X
Xiaoyu Wang 已提交
124
      return makeNode(type, sizeof(SDropUserStmt));
125
    case QUERY_NODE_USE_DATABASE_STMT:
X
Xiaoyu Wang 已提交
126
      return makeNode(type, sizeof(SUseDatabaseStmt));
127
    case QUERY_NODE_CREATE_DNODE_STMT:
X
Xiaoyu Wang 已提交
128
      return makeNode(type, sizeof(SCreateDnodeStmt));
129
    case QUERY_NODE_DROP_DNODE_STMT:
X
Xiaoyu Wang 已提交
130
      return makeNode(type, sizeof(SDropDnodeStmt));
131
    case QUERY_NODE_ALTER_DNODE_STMT:
X
Xiaoyu Wang 已提交
132
      return makeNode(type, sizeof(SAlterDnodeStmt));
X
Xiaoyu Wang 已提交
133
    case QUERY_NODE_CREATE_INDEX_STMT:
X
Xiaoyu Wang 已提交
134
      return makeNode(type, sizeof(SCreateIndexStmt));
135
    case QUERY_NODE_DROP_INDEX_STMT:
X
Xiaoyu Wang 已提交
136
      return makeNode(type, sizeof(SDropIndexStmt));
X
Xiaoyu Wang 已提交
137
    case QUERY_NODE_CREATE_QNODE_STMT:
138 139 140
    case QUERY_NODE_CREATE_BNODE_STMT:
    case QUERY_NODE_CREATE_SNODE_STMT:
    case QUERY_NODE_CREATE_MNODE_STMT:
X
Xiaoyu Wang 已提交
141
      return makeNode(type, sizeof(SCreateComponentNodeStmt));
142
    case QUERY_NODE_DROP_QNODE_STMT:
143 144 145
    case QUERY_NODE_DROP_BNODE_STMT:
    case QUERY_NODE_DROP_SNODE_STMT:
    case QUERY_NODE_DROP_MNODE_STMT:
X
Xiaoyu Wang 已提交
146
      return makeNode(type, sizeof(SDropComponentNodeStmt));
147
    case QUERY_NODE_CREATE_TOPIC_STMT:
X
Xiaoyu Wang 已提交
148
      return makeNode(type, sizeof(SCreateTopicStmt));
149
    case QUERY_NODE_DROP_TOPIC_STMT:
X
Xiaoyu Wang 已提交
150 151 152
      return makeNode(type, sizeof(SDropTopicStmt));
    case QUERY_NODE_DROP_CGROUP_STMT:
      return makeNode(type, sizeof(SDropCGroupStmt));
153 154
    case QUERY_NODE_ALTER_LOCAL_STMT:
      return makeNode(type, sizeof(SAlterLocalStmt));
155
    case QUERY_NODE_EXPLAIN_STMT:
X
Xiaoyu Wang 已提交
156
      return makeNode(type, sizeof(SExplainStmt));
157
    case QUERY_NODE_DESCRIBE_STMT:
X
Xiaoyu Wang 已提交
158
      return makeNode(type, sizeof(SDescribeStmt));
159
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:
X
Xiaoyu Wang 已提交
160
      return makeNode(type, sizeof(SNode));
161
    case QUERY_NODE_COMPACT_STMT:
162
      break;
163
    case QUERY_NODE_CREATE_FUNCTION_STMT:
X
Xiaoyu Wang 已提交
164
      return makeNode(type, sizeof(SCreateFunctionStmt));
165
    case QUERY_NODE_DROP_FUNCTION_STMT:
X
Xiaoyu Wang 已提交
166
      return makeNode(type, sizeof(SDropFunctionStmt));
167
    case QUERY_NODE_CREATE_STREAM_STMT:
X
Xiaoyu Wang 已提交
168
      return makeNode(type, sizeof(SCreateStreamStmt));
169
    case QUERY_NODE_DROP_STREAM_STMT:
X
Xiaoyu Wang 已提交
170
      return makeNode(type, sizeof(SDropStreamStmt));
X
Xiaoyu Wang 已提交
171 172
    case QUERY_NODE_BALANCE_VGROUP_STMT:
      return makeNode(type, sizeof(SBalanceVgroupStmt));
173
    case QUERY_NODE_MERGE_VGROUP_STMT:
X
Xiaoyu Wang 已提交
174
      return makeNode(type, sizeof(SMergeVgroupStmt));
175
    case QUERY_NODE_REDISTRIBUTE_VGROUP_STMT:
X
Xiaoyu Wang 已提交
176
      return makeNode(type, sizeof(SRedistributeVgroupStmt));
177
    case QUERY_NODE_SPLIT_VGROUP_STMT:
X
Xiaoyu Wang 已提交
178
      return makeNode(type, sizeof(SSplitVgroupStmt));
179 180
    case QUERY_NODE_SYNCDB_STMT:
      break;
181
    case QUERY_NODE_GRANT_STMT:
X
Xiaoyu Wang 已提交
182
      return makeNode(type, sizeof(SGrantStmt));
183
    case QUERY_NODE_REVOKE_STMT:
X
Xiaoyu Wang 已提交
184
      return makeNode(type, sizeof(SRevokeStmt));
185
    case QUERY_NODE_SHOW_DNODES_STMT:
X
Xiaoyu Wang 已提交
186
    case QUERY_NODE_SHOW_MNODES_STMT:
X
Xiaoyu Wang 已提交
187 188
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
189 190
    case QUERY_NODE_SHOW_SNODES_STMT:
    case QUERY_NODE_SHOW_BNODES_STMT:
191
    case QUERY_NODE_SHOW_CLUSTER_STMT:
192
    case QUERY_NODE_SHOW_DATABASES_STMT:
X
Xiaoyu Wang 已提交
193 194
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
195
    case QUERY_NODE_SHOW_STABLES_STMT:
X
Xiaoyu Wang 已提交
196
    case QUERY_NODE_SHOW_STREAMS_STMT:
197 198
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
S
Shengliang Guan 已提交
199
    case QUERY_NODE_SHOW_LICENCE_STMT:
200
    case QUERY_NODE_SHOW_VGROUPS_STMT:
S
Shengliang Guan 已提交
201
    case QUERY_NODE_SHOW_TOPICS_STMT:
202 203 204
    case QUERY_NODE_SHOW_CONSUMERS_STMT:
    case QUERY_NODE_SHOW_SUBSCRIBES_STMT:
    case QUERY_NODE_SHOW_SMAS_STMT:
205
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
206 207
    case QUERY_NODE_SHOW_QUERIES_STMT:
    case QUERY_NODE_SHOW_VNODES_STMT:
208 209
    case QUERY_NODE_SHOW_APPS_STMT:
    case QUERY_NODE_SHOW_SCORES_STMT:
210 211
    case QUERY_NODE_SHOW_VARIABLES_STMT:
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
212
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
213
    case QUERY_NODE_SHOW_SUBSCRIPTIONS_STMT:
214
      return makeNode(type, sizeof(SShowStmt));
215 216
    case QUERY_NODE_SHOW_DNODE_VARIABLES_STMT:
      return makeNode(type, sizeof(SShowDnodeVariablesStmt));
217
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
218
      return makeNode(type, sizeof(SShowCreateDatabaseStmt));
219 220
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
221
      return makeNode(type, sizeof(SShowCreateTableStmt));
222 223
    case QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT:
      return makeNode(type, sizeof(SShowTableDistributedStmt));
224
    case QUERY_NODE_KILL_QUERY_STMT:
X
Xiaoyu Wang 已提交
225
      return makeNode(type, sizeof(SKillQueryStmt));
226
    case QUERY_NODE_KILL_TRANSACTION_STMT:
X
Xiaoyu Wang 已提交
227
    case QUERY_NODE_KILL_CONNECTION_STMT:
X
Xiaoyu Wang 已提交
228
      return makeNode(type, sizeof(SKillStmt));
X
Xiaoyu Wang 已提交
229 230
    case QUERY_NODE_DELETE_STMT:
      return makeNode(type, sizeof(SDeleteStmt));
231 232
    case QUERY_NODE_QUERY:
      return makeNode(type, sizeof(SQuery));
X
Xiaoyu Wang 已提交
233
    case QUERY_NODE_LOGIC_PLAN_SCAN:
X
Xiaoyu Wang 已提交
234
      return makeNode(type, sizeof(SScanLogicNode));
X
Xiaoyu Wang 已提交
235
    case QUERY_NODE_LOGIC_PLAN_JOIN:
X
Xiaoyu Wang 已提交
236
      return makeNode(type, sizeof(SJoinLogicNode));
X
Xiaoyu Wang 已提交
237
    case QUERY_NODE_LOGIC_PLAN_AGG:
X
Xiaoyu Wang 已提交
238
      return makeNode(type, sizeof(SAggLogicNode));
X
Xiaoyu Wang 已提交
239
    case QUERY_NODE_LOGIC_PLAN_PROJECT:
X
Xiaoyu Wang 已提交
240
      return makeNode(type, sizeof(SProjectLogicNode));
X
Xiaoyu Wang 已提交
241 242
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY:
      return makeNode(type, sizeof(SVnodeModifyLogicNode));
X
Xiaoyu Wang 已提交
243
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
X
Xiaoyu Wang 已提交
244
      return makeNode(type, sizeof(SExchangeLogicNode));
X
Xiaoyu Wang 已提交
245 246
    case QUERY_NODE_LOGIC_PLAN_MERGE:
      return makeNode(type, sizeof(SMergeLogicNode));
X
Xiaoyu Wang 已提交
247
    case QUERY_NODE_LOGIC_PLAN_WINDOW:
X
Xiaoyu Wang 已提交
248
      return makeNode(type, sizeof(SWindowLogicNode));
X
Xiaoyu Wang 已提交
249
    case QUERY_NODE_LOGIC_PLAN_FILL:
X
Xiaoyu Wang 已提交
250
      return makeNode(type, sizeof(SFillLogicNode));
X
Xiaoyu Wang 已提交
251
    case QUERY_NODE_LOGIC_PLAN_SORT:
X
Xiaoyu Wang 已提交
252
      return makeNode(type, sizeof(SSortLogicNode));
253
    case QUERY_NODE_LOGIC_PLAN_PARTITION:
X
Xiaoyu Wang 已提交
254
      return makeNode(type, sizeof(SPartitionLogicNode));
255 256
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC:
      return makeNode(type, sizeof(SIndefRowsFuncLogicNode));
X
Xiaoyu Wang 已提交
257 258
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC:
      return makeNode(type, sizeof(SInterpFuncLogicNode));
X
Xiaoyu Wang 已提交
259
    case QUERY_NODE_LOGIC_SUBPLAN:
X
Xiaoyu Wang 已提交
260
      return makeNode(type, sizeof(SLogicSubplan));
X
Xiaoyu Wang 已提交
261
    case QUERY_NODE_LOGIC_PLAN:
X
Xiaoyu Wang 已提交
262
      return makeNode(type, sizeof(SQueryLogicPlan));
X
Xiaoyu Wang 已提交
263
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
X
Xiaoyu Wang 已提交
264
      return makeNode(type, sizeof(STagScanPhysiNode));
X
Xiaoyu Wang 已提交
265
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
X
Xiaoyu Wang 已提交
266
      return makeNode(type, sizeof(STableScanPhysiNode));
267
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
X
Xiaoyu Wang 已提交
268
      return makeNode(type, sizeof(STableSeqScanPhysiNode));
X
Xiaoyu Wang 已提交
269 270
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
      return makeNode(type, sizeof(STableMergeScanPhysiNode));
271
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN:
X
Xiaoyu Wang 已提交
272
      return makeNode(type, sizeof(SStreamScanPhysiNode));
X
Xiaoyu Wang 已提交
273
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
X
Xiaoyu Wang 已提交
274
      return makeNode(type, sizeof(SSystemTableScanPhysiNode));
275 276
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
      return makeNode(type, sizeof(SBlockDistScanPhysiNode));
X
Xiaoyu Wang 已提交
277 278
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN:
      return makeNode(type, sizeof(SLastRowScanPhysiNode));
X
Xiaoyu Wang 已提交
279
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT:
X
Xiaoyu Wang 已提交
280
      return makeNode(type, sizeof(SProjectPhysiNode));
281
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN:
X
Xiaoyu Wang 已提交
282
      return makeNode(type, sizeof(SJoinPhysiNode));
283
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG:
X
Xiaoyu Wang 已提交
284
      return makeNode(type, sizeof(SAggPhysiNode));
285
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE:
X
Xiaoyu Wang 已提交
286
      return makeNode(type, sizeof(SExchangePhysiNode));
X
Xiaoyu Wang 已提交
287 288
    case QUERY_NODE_PHYSICAL_PLAN_MERGE:
      return makeNode(type, sizeof(SMergePhysiNode));
289
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
X
Xiaoyu Wang 已提交
290
      return makeNode(type, sizeof(SSortPhysiNode));
291 292
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT:
      return makeNode(type, sizeof(SGroupSortPhysiNode));
X
Xiaoyu Wang 已提交
293
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
X
Xiaoyu Wang 已提交
294
      return makeNode(type, sizeof(SIntervalPhysiNode));
295 296
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
      return makeNode(type, sizeof(SMergeAlignedIntervalPhysiNode));
X
Xiaoyu Wang 已提交
297
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
X
Xiaoyu Wang 已提交
298
      return makeNode(type, sizeof(SStreamIntervalPhysiNode));
299 300 301 302
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
      return makeNode(type, sizeof(SStreamFinalIntervalPhysiNode));
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
      return makeNode(type, sizeof(SStreamSemiIntervalPhysiNode));
X
Xiaoyu Wang 已提交
303
    case QUERY_NODE_PHYSICAL_PLAN_FILL:
X
Xiaoyu Wang 已提交
304
      return makeNode(type, sizeof(SFillPhysiNode));
305
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
X
Xiaoyu Wang 已提交
306
      return makeNode(type, sizeof(SSessionWinodwPhysiNode));
307
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
X
Xiaoyu Wang 已提交
308
      return makeNode(type, sizeof(SStreamSessionWinodwPhysiNode));
309 310 311 312
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
      return makeNode(type, sizeof(SStreamSemiSessionWinodwPhysiNode));
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
      return makeNode(type, sizeof(SStreamFinalSessionWinodwPhysiNode));
313
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
X
Xiaoyu Wang 已提交
314
      return makeNode(type, sizeof(SStateWinodwPhysiNode));
315
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE:
5
54liuyao 已提交
316
      return makeNode(type, sizeof(SStreamStateWinodwPhysiNode));
317
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION:
X
Xiaoyu Wang 已提交
318
      return makeNode(type, sizeof(SPartitionPhysiNode));
319 320
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC:
      return makeNode(type, sizeof(SIndefRowsFuncPhysiNode));
X
Xiaoyu Wang 已提交
321 322
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC:
      return makeNode(type, sizeof(SInterpFuncLogicNode));
323
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
X
Xiaoyu Wang 已提交
324
      return makeNode(type, sizeof(SDataDispatcherNode));
325
    case QUERY_NODE_PHYSICAL_PLAN_INSERT:
X
Xiaoyu Wang 已提交
326
      return makeNode(type, sizeof(SDataInserterNode));
X
Xiaoyu Wang 已提交
327 328
    case QUERY_NODE_PHYSICAL_PLAN_DELETE:
      return makeNode(type, sizeof(SDataDeleterNode));
X
Xiaoyu Wang 已提交
329
    case QUERY_NODE_PHYSICAL_SUBPLAN:
X
Xiaoyu Wang 已提交
330
      return makeNode(type, sizeof(SSubplan));
X
Xiaoyu Wang 已提交
331
    case QUERY_NODE_PHYSICAL_PLAN:
X
Xiaoyu Wang 已提交
332
      return makeNode(type, sizeof(SQueryPlan));
333 334 335
    default:
      break;
  }
336
  nodesError("nodesMakeNode unknown node = %s", nodesNodeName(type));
X
Xiaoyu Wang 已提交
337
  return NULL;
338 339
}

X
Xiaoyu Wang 已提交
340 341 342 343 344 345 346 347 348 349 350 351
static void destroyVgDataBlockArray(SArray* pArray) {
  size_t size = taosArrayGetSize(pArray);
  for (size_t i = 0; i < size; ++i) {
    SVgDataBlocks* pVg = taosArrayGetP(pArray, i);
    taosMemoryFreeClear(pVg->pData);
    taosMemoryFreeClear(pVg);
  }
  taosArrayDestroy(pArray);
}

static void destroyLogicNode(SLogicNode* pNode) {
  nodesDestroyList(pNode->pTargets);
X
Xiaoyu Wang 已提交
352 353 354 355
  nodesDestroyNode(pNode->pConditions);
  nodesDestroyList(pNode->pChildren);
  nodesDestroyNode(pNode->pLimit);
  nodesDestroyNode(pNode->pSlimit);
X
Xiaoyu Wang 已提交
356 357 358 359 360
}

static void destroyPhysiNode(SPhysiNode* pNode) {
  nodesDestroyList(pNode->pChildren);
  nodesDestroyNode(pNode->pConditions);
361
  nodesDestroyNode((SNode*)pNode->pOutputDataBlockDesc);
X
Xiaoyu Wang 已提交
362 363 364 365 366 367
}

static void destroyWinodwPhysiNode(SWinodwPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pExprs);
  nodesDestroyList(pNode->pFuncs);
368
  nodesDestroyNode(pNode->pTspk);
5
54liuyao 已提交
369
  nodesDestroyNode(pNode->pTsEnd);
X
Xiaoyu Wang 已提交
370 371 372 373 374
}

static void destroyScanPhysiNode(SScanPhysiNode* pNode) {
  destroyPhysiNode((SPhysiNode*)pNode);
  nodesDestroyList(pNode->pScanCols);
X
Xiaoyu Wang 已提交
375
  nodesDestroyList(pNode->pScanPseudoCols);
X
Xiaoyu Wang 已提交
376 377
}

378
static void destroyDataSinkNode(SDataSinkNode* pNode) { nodesDestroyNode((SNode*)pNode->pInputDataBlockDesc); }
X
Xiaoyu Wang 已提交
379

X
Xiaoyu Wang 已提交
380 381
static void destroyExprNode(SExprNode* pExpr) { taosArrayDestroy(pExpr->pAssociation); }

382
void nodesDestroyNode(SNode* pNode) {
X
Xiaoyu Wang 已提交
383 384 385 386 387
  if (NULL == pNode) {
    return;
  }

  switch (nodeType(pNode)) {
X
Xiaoyu Wang 已提交
388
    case QUERY_NODE_COLUMN:  // pProjectRef is weak reference, no need to release
X
Xiaoyu Wang 已提交
389
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
390
      break;
D
dapan1121 已提交
391
    case QUERY_NODE_VALUE: {
X
Xiaoyu Wang 已提交
392
      SValueNode* pValue = (SValueNode*)pNode;
X
Xiaoyu Wang 已提交
393
      destroyExprNode((SExprNode*)pNode);
wafwerar's avatar
wafwerar 已提交
394
      taosMemoryFreeClear(pValue->literal);
D
dapan1121 已提交
395
      if (IS_VAR_DATA_TYPE(pValue->node.resType.type)) {
wafwerar's avatar
wafwerar 已提交
396
        taosMemoryFreeClear(pValue->datum.p);
D
dapan1121 已提交
397
      }
X
Xiaoyu Wang 已提交
398 399 400 401
      break;
    }
    case QUERY_NODE_OPERATOR: {
      SOperatorNode* pOp = (SOperatorNode*)pNode;
X
Xiaoyu Wang 已提交
402
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
403 404
      nodesDestroyNode(pOp->pLeft);
      nodesDestroyNode(pOp->pRight);
D
dapan1121 已提交
405 406 407
      break;
    }
    case QUERY_NODE_LOGIC_CONDITION:
X
Xiaoyu Wang 已提交
408
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
409
      nodesDestroyList(((SLogicConditionNode*)pNode)->pParameterList);
D
dapan1121 已提交
410 411
      break;
    case QUERY_NODE_FUNCTION:
X
Xiaoyu Wang 已提交
412
      destroyExprNode((SExprNode*)pNode);
X
Xiaoyu Wang 已提交
413
      nodesDestroyList(((SFunctionNode*)pNode)->pParameterList);
X
Xiaoyu Wang 已提交
414 415
      break;
    case QUERY_NODE_REAL_TABLE: {
X
Xiaoyu Wang 已提交
416
      SRealTableNode* pReal = (SRealTableNode*)pNode;
wafwerar's avatar
wafwerar 已提交
417 418
      taosMemoryFreeClear(pReal->pMeta);
      taosMemoryFreeClear(pReal->pVgroupList);
X
Xiaoyu Wang 已提交
419 420 421
      break;
    }
    case QUERY_NODE_TEMP_TABLE:
X
Xiaoyu Wang 已提交
422
      nodesDestroyNode(((STempTableNode*)pNode)->pSubquery);
D
dapan1121 已提交
423
      break;
X
Xiaoyu Wang 已提交
424 425 426 427 428
    case QUERY_NODE_JOIN_TABLE: {
      SJoinTableNode* pJoin = (SJoinTableNode*)pNode;
      nodesDestroyNode(pJoin->pLeft);
      nodesDestroyNode(pJoin->pRight);
      nodesDestroyNode(pJoin->pOnCond);
D
dapan1121 已提交
429
      break;
X
Xiaoyu Wang 已提交
430
    }
D
dapan1121 已提交
431
    case QUERY_NODE_GROUPING_SET:
X
Xiaoyu Wang 已提交
432 433 434 435 436
      nodesDestroyList(((SGroupingSetNode*)pNode)->pParameterList);
      break;
    case QUERY_NODE_ORDER_BY_EXPR:
      nodesDestroyNode(((SOrderByExprNode*)pNode)->pExpr);
      break;
X
Xiaoyu Wang 已提交
437
    case QUERY_NODE_LIMIT:  // no pointer field
D
dapan1121 已提交
438
      break;
X
Xiaoyu Wang 已提交
439
    case QUERY_NODE_STATE_WINDOW:
440
      nodesDestroyNode(((SStateWindowNode*)pNode)->pExpr);
X
Xiaoyu Wang 已提交
441 442 443
      break;
    case QUERY_NODE_SESSION_WINDOW: {
      SSessionWindowNode* pSession = (SSessionWindowNode*)pNode;
444 445
      nodesDestroyNode((SNode*)pSession->pCol);
      nodesDestroyNode((SNode*)pSession->pGap);
D
dapan1121 已提交
446
      break;
X
Xiaoyu Wang 已提交
447 448 449 450 451 452 453 454 455 456
    }
    case QUERY_NODE_INTERVAL_WINDOW: {
      SIntervalWindowNode* pJoin = (SIntervalWindowNode*)pNode;
      nodesDestroyNode(pJoin->pCol);
      nodesDestroyNode(pJoin->pInterval);
      nodesDestroyNode(pJoin->pOffset);
      nodesDestroyNode(pJoin->pSliding);
      nodesDestroyNode(pJoin->pFill);
      break;
    }
D
dapan1121 已提交
457
    case QUERY_NODE_NODE_LIST:
X
Xiaoyu Wang 已提交
458 459
      nodesDestroyList(((SNodeListNode*)pNode)->pNodeList);
      break;
X
Xiaoyu Wang 已提交
460 461 462 463
    case QUERY_NODE_FILL: {
      SFillNode* pFill = (SFillNode*)pNode;
      nodesDestroyNode(pFill->pValues);
      nodesDestroyNode(pFill->pWStartTs);
X
Xiaoyu Wang 已提交
464
      break;
X
Xiaoyu Wang 已提交
465
    }
X
Xiaoyu Wang 已提交
466 467
    case QUERY_NODE_RAW_EXPR:
      nodesDestroyNode(((SRawExprNode*)pNode)->pNode);
X
Xiaoyu Wang 已提交
468
      break;
X
Xiaoyu Wang 已提交
469 470 471 472 473 474
    case QUERY_NODE_TARGET:
      nodesDestroyNode(((STargetNode*)pNode)->pExpr);
      break;
    case QUERY_NODE_DATABLOCK_DESC:
      nodesDestroyList(((SDataBlockDescNode*)pNode)->pSlots);
      break;
X
Xiaoyu Wang 已提交
475 476 477
    case QUERY_NODE_SLOT_DESC:          // no pointer field
    case QUERY_NODE_COLUMN_DEF:         // no pointer field
    case QUERY_NODE_DOWNSTREAM_SOURCE:  // no pointer field
X
Xiaoyu Wang 已提交
478
      break;
X
Xiaoyu Wang 已提交
479 480
    case QUERY_NODE_DATABASE_OPTIONS: {
      SDatabaseOptions* pOptions = (SDatabaseOptions*)pNode;
481
      nodesDestroyNode((SNode*)pOptions->pDaysPerFile);
X
Xiaoyu Wang 已提交
482 483
      nodesDestroyList(pOptions->pKeep);
      nodesDestroyList(pOptions->pRetentions);
X
Xiaoyu Wang 已提交
484
      break;
X
Xiaoyu Wang 已提交
485
    }
X
Xiaoyu Wang 已提交
486
    case QUERY_NODE_TABLE_OPTIONS: {
X
Xiaoyu Wang 已提交
487 488 489
      STableOptions* pOptions = (STableOptions*)pNode;
      nodesDestroyList(pOptions->pSma);
      nodesDestroyList(pOptions->pRollupFuncs);
X
Xiaoyu Wang 已提交
490
      break;
X
Xiaoyu Wang 已提交
491
    }
492
    case QUERY_NODE_INDEX_OPTIONS: {
X
Xiaoyu Wang 已提交
493 494 495 496 497
      SIndexOptions* pOptions = (SIndexOptions*)pNode;
      nodesDestroyList(pOptions->pFuncs);
      nodesDestroyNode(pOptions->pInterval);
      nodesDestroyNode(pOptions->pOffset);
      nodesDestroyNode(pOptions->pSliding);
498 499
      break;
    }
500 501 502 503 504 505 506
    case QUERY_NODE_EXPLAIN_OPTIONS:  // no pointer field
      break;
    case QUERY_NODE_STREAM_OPTIONS:
      nodesDestroyNode(((SStreamOptions*)pNode)->pWatermark);
      break;
    case QUERY_NODE_LEFT_VALUE:  // no pointer field
      break;
X
Xiaoyu Wang 已提交
507 508 509 510 511 512 513 514
    case QUERY_NODE_SET_OPERATOR: {
      SSetOperator* pStmt = (SSetOperator*)pNode;
      nodesDestroyNode(pStmt->pLeft);
      nodesDestroyNode(pStmt->pRight);
      nodesDestroyList(pStmt->pOrderByList);
      nodesDestroyNode(pStmt->pLimit);
      break;
    }
X
Xiaoyu Wang 已提交
515
    case QUERY_NODE_SELECT_STMT: {
X
Xiaoyu Wang 已提交
516
      SSelectStmt* pStmt = (SSelectStmt*)pNode;
X
Xiaoyu Wang 已提交
517 518 519 520 521 522 523
      nodesDestroyList(pStmt->pProjectionList);
      nodesDestroyNode(pStmt->pFromTable);
      nodesDestroyNode(pStmt->pWhere);
      nodesDestroyList(pStmt->pPartitionByList);
      nodesDestroyNode(pStmt->pWindow);
      nodesDestroyList(pStmt->pGroupByList);
      nodesDestroyNode(pStmt->pHaving);
X
Xiaoyu Wang 已提交
524 525 526
      nodesDestroyNode(pStmt->pRange);
      nodesDestroyNode(pStmt->pEvery);
      nodesDestroyNode(pStmt->pFill);
X
Xiaoyu Wang 已提交
527
      nodesDestroyList(pStmt->pOrderByList);
528 529
      nodesDestroyNode((SNode*)pStmt->pLimit);
      nodesDestroyNode((SNode*)pStmt->pSlimit);
X
Xiaoyu Wang 已提交
530 531
      break;
    }
X
Xiaoyu Wang 已提交
532 533 534 535
    case QUERY_NODE_VNODE_MODIF_STMT:
      destroyVgDataBlockArray(((SVnodeModifOpStmt*)pNode)->pDataBlocks);
      break;
    case QUERY_NODE_CREATE_DATABASE_STMT:
536
      nodesDestroyNode((SNode*)((SCreateDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
537
      break;
X
Xiaoyu Wang 已提交
538
    case QUERY_NODE_DROP_DATABASE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
539 540
      break;
    case QUERY_NODE_ALTER_DATABASE_STMT:
541
      nodesDestroyNode((SNode*)((SAlterDatabaseStmt*)pNode)->pOptions);
X
Xiaoyu Wang 已提交
542
      break;
X
Xiaoyu Wang 已提交
543
    case QUERY_NODE_CREATE_TABLE_STMT: {
X
Xiaoyu Wang 已提交
544
      SCreateTableStmt* pStmt = (SCreateTableStmt*)pNode;
X
Xiaoyu Wang 已提交
545 546
      nodesDestroyList(pStmt->pCols);
      nodesDestroyList(pStmt->pTags);
547
      nodesDestroyNode((SNode*)pStmt->pOptions);
X
Xiaoyu Wang 已提交
548 549 550
      break;
    }
    case QUERY_NODE_CREATE_SUBTABLE_CLAUSE: {
X
Xiaoyu Wang 已提交
551
      SCreateSubTableClause* pStmt = (SCreateSubTableClause*)pNode;
X
Xiaoyu Wang 已提交
552 553
      nodesDestroyList(pStmt->pSpecificTags);
      nodesDestroyList(pStmt->pValsOfTags);
wmmhello's avatar
wmmhello 已提交
554
      nodesDestroyNode((SNode*)pStmt->pOptions);
X
Xiaoyu Wang 已提交
555 556 557
      break;
    }
    case QUERY_NODE_CREATE_MULTI_TABLE_STMT:
X
Xiaoyu Wang 已提交
558 559
      nodesDestroyList(((SCreateMultiTableStmt*)pNode)->pSubTables);
      break;
X
Xiaoyu Wang 已提交
560
    case QUERY_NODE_DROP_TABLE_CLAUSE:  // no pointer field
X
Xiaoyu Wang 已提交
561 562
      break;
    case QUERY_NODE_DROP_TABLE_STMT:
563
      nodesDestroyList(((SDropTableStmt*)pNode)->pTables);
X
Xiaoyu Wang 已提交
564
      break;
X
Xiaoyu Wang 已提交
565
    case QUERY_NODE_DROP_SUPER_TABLE_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
566 567 568
      break;
    case QUERY_NODE_ALTER_TABLE_STMT: {
      SAlterTableStmt* pStmt = (SAlterTableStmt*)pNode;
569 570
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode((SNode*)pStmt->pVal);
X
Xiaoyu Wang 已提交
571 572
      break;
    }
X
Xiaoyu Wang 已提交
573 574 575 576 577 578 579
    case QUERY_NODE_CREATE_USER_STMT:   // no pointer field
    case QUERY_NODE_ALTER_USER_STMT:    // no pointer field
    case QUERY_NODE_DROP_USER_STMT:     // no pointer field
    case QUERY_NODE_USE_DATABASE_STMT:  // no pointer field
    case QUERY_NODE_CREATE_DNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_DNODE_STMT:    // no pointer field
    case QUERY_NODE_ALTER_DNODE_STMT:   // no pointer field
580
      break;
581
    case QUERY_NODE_CREATE_INDEX_STMT: {
X
Xiaoyu Wang 已提交
582
      SCreateIndexStmt* pStmt = (SCreateIndexStmt*)pNode;
583
      nodesDestroyNode((SNode*)pStmt->pOptions);
584 585 586
      nodesDestroyList(pStmt->pCols);
      break;
    }
X
Xiaoyu Wang 已提交
587 588 589
    case QUERY_NODE_DROP_INDEX_STMT:    // no pointer field
    case QUERY_NODE_CREATE_QNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_QNODE_STMT:    // no pointer field
590 591 592 593 594 595
    case QUERY_NODE_CREATE_BNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_BNODE_STMT:    // no pointer field
    case QUERY_NODE_CREATE_SNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_SNODE_STMT:    // no pointer field
    case QUERY_NODE_CREATE_MNODE_STMT:  // no pointer field
    case QUERY_NODE_DROP_MNODE_STMT:    // no pointer field
X
Xiaoyu Wang 已提交
596 597 598 599
      break;
    case QUERY_NODE_CREATE_TOPIC_STMT:
      nodesDestroyNode(((SCreateTopicStmt*)pNode)->pQuery);
      break;
X
Xiaoyu Wang 已提交
600
    case QUERY_NODE_DROP_TOPIC_STMT:   // no pointer field
601
    case QUERY_NODE_DROP_CGROUP_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
602
    case QUERY_NODE_ALTER_LOCAL_STMT:  // no pointer field
X
Xiaoyu Wang 已提交
603
      break;
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    case QUERY_NODE_EXPLAIN_STMT: {
      SExplainStmt* pStmt = (SExplainStmt*)pNode;
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode(pStmt->pQuery);
      break;
    }
    case QUERY_NODE_DESCRIBE_STMT:
      taosMemoryFree(((SDescribeStmt*)pNode)->pMeta);
      break;
    case QUERY_NODE_RESET_QUERY_CACHE_STMT:  // no pointer field
    case QUERY_NODE_COMPACT_STMT:            // no pointer field
    case QUERY_NODE_CREATE_FUNCTION_STMT:    // no pointer field
    case QUERY_NODE_DROP_FUNCTION_STMT:      // no pointer field
      break;
    case QUERY_NODE_CREATE_STREAM_STMT: {
      SCreateStreamStmt* pStmt = (SCreateStreamStmt*)pNode;
      nodesDestroyNode((SNode*)pStmt->pOptions);
      nodesDestroyNode(pStmt->pQuery);
      break;
    }
    case QUERY_NODE_DROP_STREAM_STMT:     // no pointer field
    case QUERY_NODE_BALANCE_VGROUP_STMT:  // no pointer field
    case QUERY_NODE_MERGE_VGROUP_STMT:    // no pointer field
      break;
    case QUERY_NODE_REDISTRIBUTE_VGROUP_STMT:
      nodesDestroyList(((SRedistributeVgroupStmt*)pNode)->pDnodes);
      break;
    case QUERY_NODE_SPLIT_VGROUP_STMT:  // no pointer field
    case QUERY_NODE_SYNCDB_STMT:        // no pointer field
    case QUERY_NODE_GRANT_STMT:         // no pointer field
    case QUERY_NODE_REVOKE_STMT:        // no pointer field
      break;
X
Xiaoyu Wang 已提交
636 637 638 639
    case QUERY_NODE_SHOW_DNODES_STMT:
    case QUERY_NODE_SHOW_MNODES_STMT:
    case QUERY_NODE_SHOW_MODULES_STMT:
    case QUERY_NODE_SHOW_QNODES_STMT:
640 641 642 643
    case QUERY_NODE_SHOW_SNODES_STMT:
    case QUERY_NODE_SHOW_BNODES_STMT:
    case QUERY_NODE_SHOW_CLUSTER_STMT:
    case QUERY_NODE_SHOW_DATABASES_STMT:
X
Xiaoyu Wang 已提交
644 645
    case QUERY_NODE_SHOW_FUNCTIONS_STMT:
    case QUERY_NODE_SHOW_INDEXES_STMT:
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    case QUERY_NODE_SHOW_STABLES_STMT:
    case QUERY_NODE_SHOW_STREAMS_STMT:
    case QUERY_NODE_SHOW_TABLES_STMT:
    case QUERY_NODE_SHOW_USERS_STMT:
    case QUERY_NODE_SHOW_LICENCE_STMT:
    case QUERY_NODE_SHOW_VGROUPS_STMT:
    case QUERY_NODE_SHOW_TOPICS_STMT:
    case QUERY_NODE_SHOW_CONSUMERS_STMT:
    case QUERY_NODE_SHOW_SUBSCRIBES_STMT:
    case QUERY_NODE_SHOW_SMAS_STMT:
    case QUERY_NODE_SHOW_CONNECTIONS_STMT:
    case QUERY_NODE_SHOW_QUERIES_STMT:
    case QUERY_NODE_SHOW_VNODES_STMT:
    case QUERY_NODE_SHOW_APPS_STMT:
    case QUERY_NODE_SHOW_SCORES_STMT:
661 662
    case QUERY_NODE_SHOW_VARIABLES_STMT:
    case QUERY_NODE_SHOW_LOCAL_VARIABLES_STMT:
663 664
    case QUERY_NODE_SHOW_TRANSACTIONS_STMT:
    case QUERY_NODE_SHOW_SUBSCRIPTIONS_STMT: {
X
Xiaoyu Wang 已提交
665 666
      SShowStmt* pStmt = (SShowStmt*)pNode;
      nodesDestroyNode(pStmt->pDbName);
667
      nodesDestroyNode(pStmt->pTbName);
X
Xiaoyu Wang 已提交
668 669
      break;
    }
670 671
    case QUERY_NODE_SHOW_DNODE_VARIABLES_STMT:  // no pointer field
      break;
672 673 674 675 676
    case QUERY_NODE_SHOW_CREATE_DATABASE_STMT:
      taosMemoryFreeClear(((SShowCreateDatabaseStmt*)pNode)->pCfg);
      break;
    case QUERY_NODE_SHOW_CREATE_TABLE_STMT:
    case QUERY_NODE_SHOW_CREATE_STABLE_STMT:
D
dapan1121 已提交
677
      taosMemoryFreeClear(((SShowCreateTableStmt*)pNode)->pCfg);
678 679 680 681 682
      break;
    case QUERY_NODE_SHOW_TABLE_DISTRIBUTED_STMT:  // no pointer field
    case QUERY_NODE_KILL_CONNECTION_STMT:         // no pointer field
    case QUERY_NODE_KILL_QUERY_STMT:              // no pointer field
    case QUERY_NODE_KILL_TRANSACTION_STMT:        // no pointer field
683 684 685 686 687 688
      break;
    case QUERY_NODE_DELETE_STMT: {
      SDeleteStmt* pStmt = (SDeleteStmt*)pNode;
      nodesDestroyNode(pStmt->pFromTable);
      nodesDestroyNode(pStmt->pWhere);
      nodesDestroyNode(pStmt->pCountFunc);
X
Xiaoyu Wang 已提交
689
      nodesDestroyNode(pStmt->pTagCond);
690 691
      break;
    }
692 693 694 695 696 697 698 699 700 701 702 703
    case QUERY_NODE_QUERY: {
      SQuery* pQuery = (SQuery*)pNode;
      nodesDestroyNode(pQuery->pRoot);
      taosMemoryFreeClear(pQuery->pResSchema);
      if (NULL != pQuery->pCmdMsg) {
        taosMemoryFreeClear(pQuery->pCmdMsg->pMsg);
        taosMemoryFreeClear(pQuery->pCmdMsg);
      }
      taosArrayDestroy(pQuery->pDbList);
      taosArrayDestroy(pQuery->pTableList);
      break;
    }
X
Xiaoyu Wang 已提交
704 705 706 707
    case QUERY_NODE_LOGIC_PLAN_SCAN: {
      SScanLogicNode* pLogicNode = (SScanLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pScanCols);
X
Xiaoyu Wang 已提交
708
      nodesDestroyList(pLogicNode->pScanPseudoCols);
X
Xiaoyu Wang 已提交
709
      taosMemoryFreeClear(pLogicNode->pVgroupList);
X
Xiaoyu Wang 已提交
710 711 712 713 714
      nodesDestroyList(pLogicNode->pDynamicScanFuncs);
      nodesDestroyNode(pLogicNode->pTagCond);
      nodesDestroyNode(pLogicNode->pTagIndexCond);
      taosArrayDestroy(pLogicNode->pSmaIndexes);
      nodesDestroyList(pLogicNode->pPartTags);
X
Xiaoyu Wang 已提交
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_JOIN: {
      SJoinLogicNode* pLogicNode = (SJoinLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyNode(pLogicNode->pOnConditions);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_AGG: {
      SAggLogicNode* pLogicNode = (SAggLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pAggFuncs);
      nodesDestroyList(pLogicNode->pGroupKeys);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_PROJECT: {
      SProjectLogicNode* pLogicNode = (SProjectLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pProjections);
      break;
    }
X
Xiaoyu Wang 已提交
736 737
    case QUERY_NODE_LOGIC_PLAN_VNODE_MODIFY: {
      SVnodeModifyLogicNode* pLogicNode = (SVnodeModifyLogicNode*)pNode;
X
Xiaoyu Wang 已提交
738 739
      destroyLogicNode((SLogicNode*)pLogicNode);
      destroyVgDataBlockArray(pLogicNode->pDataBlocks);
X
bugfix  
Xiaoyu Wang 已提交
740
      // pVgDataBlocks is weak reference
X
Xiaoyu Wang 已提交
741 742 743 744 745
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_EXCHANGE:
      destroyLogicNode((SLogicNode*)pNode);
      break;
746 747 748 749 750 751 752
    case QUERY_NODE_LOGIC_PLAN_MERGE: {
      SMergeLogicNode* pLogicNode = (SMergeLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pMergeKeys);
      nodesDestroyList(pLogicNode->pInputs);
      break;
    }
X
Xiaoyu Wang 已提交
753 754 755 756 757
    case QUERY_NODE_LOGIC_PLAN_WINDOW: {
      SWindowLogicNode* pLogicNode = (SWindowLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pFuncs);
      nodesDestroyNode(pLogicNode->pTspk);
5
54liuyao 已提交
758
      nodesDestroyNode(pLogicNode->pTsEnd);
X
Xiaoyu Wang 已提交
759 760
      break;
    }
761 762 763 764 765 766 767
    case QUERY_NODE_LOGIC_PLAN_FILL: {
      SFillLogicNode* pLogicNode = (SFillLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyNode(pLogicNode->pWStartTs);
      nodesDestroyNode(pLogicNode->pValues);
      break;
    }
X
Xiaoyu Wang 已提交
768 769 770 771 772 773 774 775 776 777 778 779
    case QUERY_NODE_LOGIC_PLAN_SORT: {
      SSortLogicNode* pLogicNode = (SSortLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pSortKeys);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_PARTITION: {
      SPartitionLogicNode* pLogicNode = (SPartitionLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pPartitionKeys);
      break;
    }
780 781 782
    case QUERY_NODE_LOGIC_PLAN_INDEF_ROWS_FUNC: {
      SIndefRowsFuncLogicNode* pLogicNode = (SIndefRowsFuncLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
X
Xiaoyu Wang 已提交
783 784 785 786 787 788 789
      nodesDestroyList(pLogicNode->pFuncs);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN_INTERP_FUNC: {
      SInterpFuncLogicNode* pLogicNode = (SInterpFuncLogicNode*)pNode;
      destroyLogicNode((SLogicNode*)pLogicNode);
      nodesDestroyList(pLogicNode->pFuncs);
X
Xiaoyu Wang 已提交
790 791
      nodesDestroyNode(pLogicNode->pFillValues);
      nodesDestroyNode(pLogicNode->pTimeSeries);
792 793
      break;
    }
X
Xiaoyu Wang 已提交
794 795 796
    case QUERY_NODE_LOGIC_SUBPLAN: {
      SLogicSubplan* pSubplan = (SLogicSubplan*)pNode;
      nodesDestroyList(pSubplan->pChildren);
797
      nodesDestroyNode((SNode*)pSubplan->pNode);
X
bugfix  
Xiaoyu Wang 已提交
798
      nodesClearList(pSubplan->pParents);
X
Xiaoyu Wang 已提交
799 800 801 802 803 804 805 806
      taosMemoryFreeClear(pSubplan->pVgroupList);
      break;
    }
    case QUERY_NODE_LOGIC_PLAN:
      nodesDestroyList(((SQueryLogicPlan*)pNode)->pTopSubplans);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_TAG_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_SYSTABLE_SCAN:
807
    case QUERY_NODE_PHYSICAL_PLAN_BLOCK_DIST_SCAN:
X
Xiaoyu Wang 已提交
808
    case QUERY_NODE_PHYSICAL_PLAN_LAST_ROW_SCAN:
X
Xiaoyu Wang 已提交
809 810
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      break;
X
Xiaoyu Wang 已提交
811 812 813 814 815 816 817 818 819 820
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_SEQ_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_TABLE_MERGE_SCAN:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN: {
      STableScanPhysiNode* pPhyNode = (STableScanPhysiNode*)pNode;
      destroyScanPhysiNode((SScanPhysiNode*)pNode);
      nodesDestroyList(pPhyNode->pDynamicScanFuncs);
      nodesDestroyList(pPhyNode->pPartitionTags);
      break;
    }
X
Xiaoyu Wang 已提交
821 822 823 824 825 826
    case QUERY_NODE_PHYSICAL_PLAN_PROJECT: {
      SProjectPhysiNode* pPhyNode = (SProjectPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pProjections);
      break;
    }
827
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_JOIN: {
X
Xiaoyu Wang 已提交
828 829 830 831 832 833
      SJoinPhysiNode* pPhyNode = (SJoinPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyNode(pPhyNode->pOnConditions);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
834
    case QUERY_NODE_PHYSICAL_PLAN_HASH_AGG: {
X
Xiaoyu Wang 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847
      SAggPhysiNode* pPhyNode = (SAggPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pAggFuncs);
      nodesDestroyList(pPhyNode->pGroupKeys);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_EXCHANGE: {
      SExchangePhysiNode* pPhyNode = (SExchangePhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pSrcEndPoints);
      break;
    }
848 849 850 851 852 853 854
    case QUERY_NODE_PHYSICAL_PLAN_MERGE: {
      SMergePhysiNode* pPhyNode = (SMergePhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pMergeKeys);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
855 856
    case QUERY_NODE_PHYSICAL_PLAN_SORT:
    case QUERY_NODE_PHYSICAL_PLAN_GROUP_SORT: {
X
Xiaoyu Wang 已提交
857 858
      SSortPhysiNode* pPhyNode = (SSortPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
859 860
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pSortKeys);
X
Xiaoyu Wang 已提交
861
      nodesDestroyList(pPhyNode->pTargets);
X
Xiaoyu Wang 已提交
862 863
      break;
    }
X
Xiaoyu Wang 已提交
864
    case QUERY_NODE_PHYSICAL_PLAN_HASH_INTERVAL:
865
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_ALIGNED_INTERVAL:
X
Xiaoyu Wang 已提交
866
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_INTERVAL:
867 868
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_INTERVAL:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_INTERVAL:
X
Xiaoyu Wang 已提交
869
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pNode);
X
Xiaoyu Wang 已提交
870
      break;
871 872 873 874 875 876 877 878
    case QUERY_NODE_PHYSICAL_PLAN_FILL: {
      SFillPhysiNode* pPhyNode = (SFillPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyNode(pPhyNode->pWStartTs);
      nodesDestroyNode(pPhyNode->pValues);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
879 880
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_SESSION:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION:
881
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_SEMI_SESSION:
882
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_FINAL_SESSION:
X
Xiaoyu Wang 已提交
883 884
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pNode);
      break;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    case QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE:
    case QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE: {
      SStateWinodwPhysiNode* pPhyNode = (SStateWinodwPhysiNode*)pNode;
      destroyWinodwPhysiNode((SWinodwPhysiNode*)pPhyNode);
      nodesDestroyNode(pPhyNode->pStateKey);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_PARTITION: {
      SPartitionPhysiNode* pPhyNode = (SPartitionPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pPartitionKeys);
      nodesDestroyList(pPhyNode->pTargets);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_INDEF_ROWS_FUNC: {
      SIndefRowsFuncPhysiNode* pPhyNode = (SIndefRowsFuncPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
X
Xiaoyu Wang 已提交
904 905 906 907 908 909 910 911
      nodesDestroyList(pPhyNode->pFuncs);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN_INTERP_FUNC: {
      SInterpFuncPhysiNode* pPhyNode = (SInterpFuncPhysiNode*)pNode;
      destroyPhysiNode((SPhysiNode*)pPhyNode);
      nodesDestroyList(pPhyNode->pExprs);
      nodesDestroyList(pPhyNode->pFuncs);
X
Xiaoyu Wang 已提交
912 913
      nodesDestroyNode(pPhyNode->pFillValues);
      nodesDestroyNode(pPhyNode->pTimeSeries);
914 915
      break;
    }
X
Xiaoyu Wang 已提交
916 917 918 919 920 921 922 923 924
    case QUERY_NODE_PHYSICAL_PLAN_DISPATCH:
      destroyDataSinkNode((SDataSinkNode*)pNode);
      break;
    case QUERY_NODE_PHYSICAL_PLAN_INSERT: {
      SDataInserterNode* pSink = (SDataInserterNode*)pNode;
      destroyDataSinkNode((SDataSinkNode*)pSink);
      taosMemoryFreeClear(pSink->pData);
      break;
    }
925 926 927 928 929 930
    case QUERY_NODE_PHYSICAL_PLAN_DELETE: {
      SDataDeleterNode* pSink = (SDataDeleterNode*)pNode;
      destroyDataSinkNode((SDataSinkNode*)pSink);
      nodesDestroyNode(pSink->pAffectedRows);
      break;
    }
X
Xiaoyu Wang 已提交
931 932 933
    case QUERY_NODE_PHYSICAL_SUBPLAN: {
      SSubplan* pSubplan = (SSubplan*)pNode;
      nodesDestroyList(pSubplan->pChildren);
934 935
      nodesDestroyNode((SNode*)pSubplan->pNode);
      nodesDestroyNode((SNode*)pSubplan->pDataSink);
X
Xiaoyu Wang 已提交
936 937
      nodesDestroyNode((SNode*)pSubplan->pTagCond);
      nodesDestroyNode((SNode*)pSubplan->pTagIndexCond);
X
bugfix  
Xiaoyu Wang 已提交
938 939 940 941 942 943 944
      nodesClearList(pSubplan->pParents);
      break;
    }
    case QUERY_NODE_PHYSICAL_PLAN: {
      SQueryPlan* pPlan = (SQueryPlan*)pNode;
      if (NULL != pPlan->pSubplans) {
        // only need to destroy the top-level subplans, because they will recurse to all the subplans below
X
Xiaoyu Wang 已提交
945
        bool   first = true;
X
bugfix  
Xiaoyu Wang 已提交
946 947 948 949 950 951 952 953 954 955 956 957
        SNode* pElement = NULL;
        FOREACH(pElement, pPlan->pSubplans) {
          if (first) {
            first = false;
            nodesDestroyNode(pElement);
          } else {
            nodesClearList(((SNodeListNode*)pElement)->pNodeList);
            taosMemoryFreeClear(pElement);
          }
        }
        nodesClearList(pPlan->pSubplans);
      }
X
Xiaoyu Wang 已提交
958 959
      break;
    }
960 961 962
    default:
      break;
  }
X
Xiaoyu Wang 已提交
963 964
  taosMemoryFreeClear(pNode);
  return;
965 966
}

967
SNodeList* nodesMakeList() {
wafwerar's avatar
wafwerar 已提交
968
  SNodeList* p = taosMemoryCalloc(1, sizeof(SNodeList));
969 970 971 972 973 974
  if (NULL == p) {
    return NULL;
  }
  return p;
}

975
int32_t nodesListAppend(SNodeList* pList, SNode* pNode) {
976
  if (NULL == pList || NULL == pNode) {
X
Xiaoyu Wang 已提交
977
    return TSDB_CODE_FAILED;
978
  }
wafwerar's avatar
wafwerar 已提交
979
  SListCell* p = taosMemoryCalloc(1, sizeof(SListCell));
980
  if (NULL == p) {
X
Xiaoyu Wang 已提交
981 982
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_OUT_OF_MEMORY;
983 984
  }
  p->pNode = pNode;
985 986 987 988 989 990
  if (NULL == pList->pHead) {
    pList->pHead = p;
  }
  if (NULL != pList->pTail) {
    pList->pTail->pNext = p;
  }
991
  p->pPrev = pList->pTail;
992
  pList->pTail = p;
993
  ++(pList->length);
X
Xiaoyu Wang 已提交
994
  return TSDB_CODE_SUCCESS;
995 996
}

997
int32_t nodesListStrictAppend(SNodeList* pList, SNode* pNode) {
998
  if (NULL == pNode) {
X
Xiaoyu Wang 已提交
999
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1000 1001 1002 1003 1004 1005 1006 1007 1008
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  int32_t code = nodesListAppend(pList, pNode);
  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyNode(pNode);
  }
  return code;
}

1009
int32_t nodesListMakeAppend(SNodeList** pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  if (NULL == *pList) {
    *pList = nodesMakeList();
    if (NULL == *pList) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return nodesListAppend(*pList, pNode);
}

1020
int32_t nodesListMakeStrictAppend(SNodeList** pList, SNode* pNode) {
S
Shengliang Guan 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  if (NULL == *pList) {
    *pList = nodesMakeList();
    if (NULL == *pList) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return TSDB_CODE_OUT_OF_MEMORY;
    }
  }
  return nodesListStrictAppend(*pList, pNode);
}

X
Xiaoyu Wang 已提交
1031
int32_t nodesListAppendList(SNodeList* pTarget, SNodeList* pSrc) {
X
Xiaoyu Wang 已提交
1032
  if (NULL == pTarget || NULL == pSrc) {
X
Xiaoyu Wang 已提交
1033
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  }

  if (NULL == pTarget->pHead) {
    pTarget->pHead = pSrc->pHead;
  } else {
    pTarget->pTail->pNext = pSrc->pHead;
    if (NULL != pSrc->pHead) {
      pSrc->pHead->pPrev = pTarget->pTail;
    }
  }
X
Xiaoyu Wang 已提交
1044 1045
  pTarget->pTail = pSrc->pTail;
  pTarget->length += pSrc->length;
wafwerar's avatar
wafwerar 已提交
1046
  taosMemoryFreeClear(pSrc);
X
Xiaoyu Wang 已提交
1047

X
Xiaoyu Wang 已提交
1048 1049 1050
  return TSDB_CODE_SUCCESS;
}

1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
int32_t nodesListStrictAppendList(SNodeList* pTarget, SNodeList* pSrc) {
  if (NULL == pSrc) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  int32_t code = nodesListAppendList(pTarget, pSrc);
  if (TSDB_CODE_SUCCESS != code) {
    nodesDestroyList(pSrc);
  }
  return code;
}

1062
int32_t nodesListPushFront(SNodeList* pList, SNode* pNode) {
X
Xiaoyu Wang 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
  if (NULL == pList || NULL == pNode) {
    return TSDB_CODE_FAILED;
  }
  SListCell* p = taosMemoryCalloc(1, sizeof(SListCell));
  if (NULL == p) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  p->pNode = pNode;
  if (NULL != pList->pHead) {
    pList->pHead->pPrev = p;
    p->pNext = pList->pHead;
  }
  pList->pHead = p;
  ++(pList->length);
  return TSDB_CODE_SUCCESS;
}

1081 1082 1083 1084 1085
SListCell* nodesListErase(SNodeList* pList, SListCell* pCell) {
  if (NULL == pCell->pPrev) {
    pList->pHead = pCell->pNext;
  } else {
    pCell->pPrev->pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
1086 1087 1088 1089
  }
  if (NULL == pCell->pNext) {
    pList->pTail = pCell->pPrev;
  } else {
1090 1091 1092
    pCell->pNext->pPrev = pCell->pPrev;
  }
  SListCell* pNext = pCell->pNext;
X
Xiaoyu Wang 已提交
1093
  nodesDestroyNode(pCell->pNode);
wafwerar's avatar
wafwerar 已提交
1094
  taosMemoryFreeClear(pCell);
1095 1096 1097 1098
  --(pList->length);
  return pNext;
}

X
Xiaoyu Wang 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
void nodesListInsertList(SNodeList* pTarget, SListCell* pPos, SNodeList* pSrc) {
  if (NULL == pTarget || NULL == pPos || NULL == pSrc) {
    return;
  }

  if (NULL == pPos->pPrev) {
    pTarget->pHead = pSrc->pHead;
  } else {
    pPos->pPrev->pNext = pSrc->pHead;
  }
  pSrc->pHead->pPrev = pPos->pPrev;
  pSrc->pTail->pNext = pPos;
  pPos->pPrev = pSrc->pTail;

  pTarget->length += pSrc->length;
  taosMemoryFreeClear(pSrc);
}

1117
SNode* nodesListGetNode(SNodeList* pList, int32_t index) {
1118 1119 1120 1121 1122 1123 1124 1125 1126
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return node;
    }
  }
  return NULL;
}

X
Xiaoyu Wang 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
SListCell* nodesListGetCell(SNodeList* pList, int32_t index) {
  SNode* node;
  FOREACH(node, pList) {
    if (0 == index--) {
      return cell;
    }
  }
  return NULL;
}

1137
void nodesDestroyList(SNodeList* pList) {
1138 1139 1140
  if (NULL == pList) {
    return;
  }
X
Xiaoyu Wang 已提交
1141

D
dapan1121 已提交
1142 1143 1144
  SListCell* pNext = pList->pHead;
  while (NULL != pNext) {
    pNext = nodesListErase(pList, pNext);
1145
  }
wafwerar's avatar
wafwerar 已提交
1146
  taosMemoryFreeClear(pList);
1147 1148
}

X
Xiaoyu Wang 已提交
1149 1150 1151 1152 1153 1154 1155 1156 1157
void nodesClearList(SNodeList* pList) {
  if (NULL == pList) {
    return;
  }

  SListCell* pNext = pList->pHead;
  while (NULL != pNext) {
    SListCell* tmp = pNext;
    pNext = pNext->pNext;
wafwerar's avatar
wafwerar 已提交
1158
    taosMemoryFreeClear(tmp);
X
Xiaoyu Wang 已提交
1159
  }
wafwerar's avatar
wafwerar 已提交
1160
  taosMemoryFreeClear(pList);
X
Xiaoyu Wang 已提交
1161 1162
}

X
Xiaoyu Wang 已提交
1163
void* nodesGetValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_BOOL:
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
    case TSDB_DATA_TYPE_FLOAT:
1176
    case TSDB_DATA_TYPE_DOUBLE:
D
dapan1121 已提交
1177
      return (void*)&pNode->typeData;
D
dapan1121 已提交
1178 1179
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
1180
    case TSDB_DATA_TYPE_VARBINARY:
wmmhello's avatar
wmmhello 已提交
1181
    case TSDB_DATA_TYPE_JSON:
X
Xiaoyu Wang 已提交
1182
      return (void*)pNode->datum.p;
D
dapan1121 已提交
1183 1184 1185 1186 1187 1188 1189
    default:
      break;
  }

  return NULL;
}

X
Xiaoyu Wang 已提交
1190
int32_t nodesSetValueNodeValue(SValueNode* pNode, void* value) {
D
dapan1121 已提交
1191 1192 1193
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_BOOL:
      pNode->datum.b = *(bool*)value;
D
dapan1121 已提交
1194
      *(bool*)&pNode->typeData = pNode->datum.b;
D
dapan1121 已提交
1195 1196 1197
      break;
    case TSDB_DATA_TYPE_TINYINT:
      pNode->datum.i = *(int8_t*)value;
D
dapan1121 已提交
1198
      *(int8_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1199 1200 1201
      break;
    case TSDB_DATA_TYPE_SMALLINT:
      pNode->datum.i = *(int16_t*)value;
D
dapan1121 已提交
1202
      *(int16_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1203 1204 1205
      break;
    case TSDB_DATA_TYPE_INT:
      pNode->datum.i = *(int32_t*)value;
D
dapan1121 已提交
1206
      *(int32_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1207 1208 1209
      break;
    case TSDB_DATA_TYPE_BIGINT:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
1210
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1211 1212 1213
      break;
    case TSDB_DATA_TYPE_TIMESTAMP:
      pNode->datum.i = *(int64_t*)value;
D
dapan1121 已提交
1214
      *(int64_t*)&pNode->typeData = pNode->datum.i;
D
dapan1121 已提交
1215 1216 1217
      break;
    case TSDB_DATA_TYPE_UTINYINT:
      pNode->datum.u = *(int8_t*)value;
D
dapan1121 已提交
1218
      *(int8_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1219 1220 1221
      break;
    case TSDB_DATA_TYPE_USMALLINT:
      pNode->datum.u = *(int16_t*)value;
D
dapan1121 已提交
1222
      *(int16_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1223 1224 1225
      break;
    case TSDB_DATA_TYPE_UINT:
      pNode->datum.u = *(int32_t*)value;
D
dapan1121 已提交
1226
      *(int32_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1227 1228 1229
      break;
    case TSDB_DATA_TYPE_UBIGINT:
      pNode->datum.u = *(uint64_t*)value;
D
dapan1121 已提交
1230
      *(uint64_t*)&pNode->typeData = pNode->datum.u;
D
dapan1121 已提交
1231 1232 1233
      break;
    case TSDB_DATA_TYPE_FLOAT:
      pNode->datum.d = *(float*)value;
D
dapan1121 已提交
1234
      *(float*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
1235 1236 1237
      break;
    case TSDB_DATA_TYPE_DOUBLE:
      pNode->datum.d = *(double*)value;
D
dapan1121 已提交
1238
      *(double*)&pNode->typeData = pNode->datum.d;
D
dapan1121 已提交
1239 1240 1241 1242
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
wmmhello's avatar
wmmhello 已提交
1243
    case TSDB_DATA_TYPE_JSON:
D
dapan1121 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252
      pNode->datum.p = (char*)value;
      break;
    default:
      return TSDB_CODE_QRY_APP_ERROR;
  }

  return TSDB_CODE_SUCCESS;
}

X
Xiaoyu Wang 已提交
1253
char* nodesGetStrValueFromNode(SValueNode* pNode) {
D
dapan1121 已提交
1254
  switch (pNode->node.resType.type) {
D
dapan1121 已提交
1255
    case TSDB_DATA_TYPE_BOOL: {
X
Xiaoyu Wang 已提交
1256
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1257 1258 1259
      if (NULL == buf) {
        return NULL;
      }
1260

D
dapan1121 已提交
1261 1262
      sprintf(buf, "%s", pNode->datum.b ? "true" : "false");
      return buf;
D
dapan1121 已提交
1263
    }
D
dapan1121 已提交
1264 1265 1266 1267 1268
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP: {
X
Xiaoyu Wang 已提交
1269
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1270 1271 1272
      if (NULL == buf) {
        return NULL;
      }
1273

D
dapan1121 已提交
1274 1275 1276 1277 1278 1279 1280
      sprintf(buf, "%" PRId64, pNode->datum.i);
      return buf;
    }
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT: {
X
Xiaoyu Wang 已提交
1281
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1282 1283 1284
      if (NULL == buf) {
        return NULL;
      }
1285

D
dapan1121 已提交
1286 1287 1288 1289 1290
      sprintf(buf, "%" PRIu64, pNode->datum.u);
      return buf;
    }
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE: {
X
Xiaoyu Wang 已提交
1291
      void* buf = taosMemoryMalloc(MAX_NUM_STR_SIZE);
D
dapan1121 已提交
1292 1293 1294
      if (NULL == buf) {
        return NULL;
      }
1295

D
dapan1121 已提交
1296 1297 1298 1299 1300 1301
      sprintf(buf, "%e", pNode->datum.d);
      return buf;
    }
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY: {
D
dapan1121 已提交
1302
      int32_t bufSize = varDataLen(pNode->datum.p) + 2 + 1;
X
Xiaoyu Wang 已提交
1303
      void*   buf = taosMemoryMalloc(bufSize);
D
dapan1121 已提交
1304 1305 1306
      if (NULL == buf) {
        return NULL;
      }
1307

D
dapan1121 已提交
1308
      snprintf(buf, bufSize, "'%s'", varDataVal(pNode->datum.p));
D
dapan1121 已提交
1309 1310 1311 1312 1313 1314 1315 1316 1317
      return buf;
    }
    default:
      break;
  }

  return NULL;
}

1318 1319
bool nodesIsExprNode(const SNode* pNode) {
  ENodeType type = nodeType(pNode);
X
Xiaoyu Wang 已提交
1320
  return (QUERY_NODE_COLUMN == type || QUERY_NODE_VALUE == type || QUERY_NODE_OPERATOR == type ||
X
Xiaoyu Wang 已提交
1321
          QUERY_NODE_FUNCTION == type || QUERY_NODE_LOGIC_CONDITION == type);
1322 1323
}

X
Xiaoyu Wang 已提交
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
bool nodesIsUnaryOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_MINUS:
    case OP_TYPE_IS_NULL:
    case OP_TYPE_IS_NOT_NULL:
    case OP_TYPE_IS_TRUE:
    case OP_TYPE_IS_FALSE:
    case OP_TYPE_IS_UNKNOWN:
    case OP_TYPE_IS_NOT_TRUE:
    case OP_TYPE_IS_NOT_FALSE:
    case OP_TYPE_IS_NOT_UNKNOWN:
      return true;
    default:
      break;
  }
  return false;
}

1342 1343 1344 1345 1346 1347
bool nodesIsArithmeticOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_ADD:
    case OP_TYPE_SUB:
    case OP_TYPE_MULTI:
    case OP_TYPE_DIV:
1348
    case OP_TYPE_REM:
1349 1350 1351 1352 1353 1354
      return true;
    default:
      break;
  }
  return false;
}
1355

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
bool nodesIsComparisonOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_GREATER_THAN:
    case OP_TYPE_GREATER_EQUAL:
    case OP_TYPE_LOWER_THAN:
    case OP_TYPE_LOWER_EQUAL:
    case OP_TYPE_EQUAL:
    case OP_TYPE_NOT_EQUAL:
    case OP_TYPE_IN:
    case OP_TYPE_NOT_IN:
    case OP_TYPE_LIKE:
    case OP_TYPE_NOT_LIKE:
    case OP_TYPE_MATCH:
    case OP_TYPE_NMATCH:
1370
    case OP_TYPE_JSON_CONTAINS:
X
Xiaoyu Wang 已提交
1371 1372 1373 1374 1375 1376 1377 1378
    case OP_TYPE_IS_NULL:
    case OP_TYPE_IS_NOT_NULL:
    case OP_TYPE_IS_TRUE:
    case OP_TYPE_IS_FALSE:
    case OP_TYPE_IS_UNKNOWN:
    case OP_TYPE_IS_NOT_TRUE:
    case OP_TYPE_IS_NOT_FALSE:
    case OP_TYPE_IS_NOT_UNKNOWN:
1379 1380 1381 1382 1383
      return true;
    default:
      break;
  }
  return false;
1384 1385
}

1386 1387 1388
bool nodesIsJsonOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_JSON_GET_VALUE:
wmmhello's avatar
wmmhello 已提交
1389
    case OP_TYPE_JSON_CONTAINS:
1390 1391 1392 1393 1394 1395
      return true;
    default:
      break;
  }
  return false;
}
1396

X
Xiaoyu Wang 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
bool nodesIsRegularOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_LIKE:
    case OP_TYPE_NOT_LIKE:
    case OP_TYPE_MATCH:
    case OP_TYPE_NMATCH:
      return true;
    default:
      break;
  }
  return false;
}

X
Xiaoyu Wang 已提交
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
bool nodesIsBitwiseOp(const SOperatorNode* pOp) {
  switch (pOp->opType) {
    case OP_TYPE_BIT_AND:
    case OP_TYPE_BIT_OR:
      return true;
    default:
      break;
  }
  return false;
}

X
Xiaoyu Wang 已提交
1421
typedef struct SCollectColumnsCxt {
1422 1423 1424 1425 1426
  int32_t         errCode;
  const char*     pTableAlias;
  ECollectColType collectType;
  SNodeList*      pCols;
  SHashObj*       pColHash;
X
Xiaoyu Wang 已提交
1427 1428
} SCollectColumnsCxt;

1429
static EDealRes doCollect(SCollectColumnsCxt* pCxt, SColumnNode* pCol, SNode* pNode) {
X
Xiaoyu Wang 已提交
1430
  char    name[TSDB_TABLE_NAME_LEN + TSDB_COL_NAME_LEN];
1431 1432 1433 1434 1435 1436 1437
  int32_t len = 0;
  if ('\0' == pCol->tableAlias[0]) {
    len = sprintf(name, "%s", pCol->colName);
  }
  len = sprintf(name, "%s.%s", pCol->tableAlias, pCol->colName);
  if (NULL == taosHashGet(pCxt->pColHash, name, len)) {
    pCxt->errCode = taosHashPut(pCxt->pColHash, name, len, NULL, 0);
X
Xiaoyu Wang 已提交
1438
    if (TSDB_CODE_SUCCESS == pCxt->errCode) {
1439
      pCxt->errCode = nodesListStrictAppend(pCxt->pCols, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1440 1441 1442 1443 1444 1445
    }
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

1446 1447 1448 1449 1450 1451
static bool isCollectType(ECollectColType collectType, EColumnType colType) {
  return COLLECT_COL_TYPE_ALL == collectType
             ? true
             : (COLLECT_COL_TYPE_TAG == collectType ? COLUMN_TYPE_TAG == colType : COLUMN_TYPE_TAG != colType);
}

X
Xiaoyu Wang 已提交
1452 1453
static EDealRes collectColumns(SNode* pNode, void* pContext) {
  SCollectColumnsCxt* pCxt = (SCollectColumnsCxt*)pContext;
X
Xiaoyu Wang 已提交
1454
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
X
Xiaoyu Wang 已提交
1455
    SColumnNode* pCol = (SColumnNode*)pNode;
1456 1457
    if (isCollectType(pCxt->collectType, pCol->colType) &&
        (NULL == pCxt->pTableAlias || 0 == strcmp(pCxt->pTableAlias, pCol->tableAlias))) {
1458
      return doCollect(pCxt, pCol, pNode);
X
Xiaoyu Wang 已提交
1459 1460 1461 1462 1463
    }
  }
  return DEAL_RES_CONTINUE;
}

1464 1465
int32_t nodesCollectColumns(SSelectStmt* pSelect, ESqlClause clause, const char* pTableAlias, ECollectColType type,
                            SNodeList** pCols) {
X
Xiaoyu Wang 已提交
1466
  if (NULL == pSelect || NULL == pCols) {
1467
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1468 1469 1470
  }

  SCollectColumnsCxt cxt = {
X
Xiaoyu Wang 已提交
1471 1472
      .errCode = TSDB_CODE_SUCCESS,
      .pTableAlias = pTableAlias,
1473
      .collectType = type,
1474
      .pCols = (NULL == *pCols ? nodesMakeList() : *pCols),
X
Xiaoyu Wang 已提交
1475
      .pColHash = taosHashInit(128, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK)};
1476
  if (NULL == cxt.pCols || NULL == cxt.pColHash) {
X
Xiaoyu Wang 已提交
1477 1478
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1479
  *pCols = NULL;
X
Xiaoyu Wang 已提交
1480
  nodesWalkSelectStmt(pSelect, clause, collectColumns, &cxt);
1481
  taosHashCleanup(cxt.pColHash);
X
Xiaoyu Wang 已提交
1482
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
1483
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1484 1485
    return cxt.errCode;
  }
1486 1487 1488
  if (LIST_LENGTH(cxt.pCols) > 0) {
    *pCols = cxt.pCols;
  } else {
1489
    nodesDestroyList(cxt.pCols);
X
Xiaoyu Wang 已提交
1490
  }
1491

X
Xiaoyu Wang 已提交
1492 1493 1494 1495
  return TSDB_CODE_SUCCESS;
}

typedef struct SCollectFuncsCxt {
X
Xiaoyu Wang 已提交
1496
  int32_t         errCode;
X
Xiaoyu Wang 已提交
1497
  FFuncClassifier classifier;
X
Xiaoyu Wang 已提交
1498
  SNodeList*      pFuncs;
X
Xiaoyu Wang 已提交
1499 1500 1501 1502 1503
} SCollectFuncsCxt;

static EDealRes collectFuncs(SNode* pNode, void* pContext) {
  SCollectFuncsCxt* pCxt = (SCollectFuncsCxt*)pContext;
  if (QUERY_NODE_FUNCTION == nodeType(pNode) && pCxt->classifier(((SFunctionNode*)pNode)->funcId)) {
1504
    pCxt->errCode = nodesListStrictAppend(pCxt->pFuncs, nodesCloneNode(pNode));
X
Xiaoyu Wang 已提交
1505 1506 1507 1508 1509
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1510
int32_t nodesCollectFuncs(SSelectStmt* pSelect, ESqlClause clause, FFuncClassifier classifier, SNodeList** pFuncs) {
X
Xiaoyu Wang 已提交
1511
  if (NULL == pSelect || NULL == pFuncs) {
1512
    return TSDB_CODE_FAILED;
X
Xiaoyu Wang 已提交
1513 1514
  }

1515 1516
  SCollectFuncsCxt cxt = {
      .errCode = TSDB_CODE_SUCCESS, .classifier = classifier, .pFuncs = (NULL == *pFuncs ? nodesMakeList() : *pFuncs)};
X
Xiaoyu Wang 已提交
1517 1518 1519
  if (NULL == cxt.pFuncs) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
1520
  *pFuncs = NULL;
X
Xiaoyu Wang 已提交
1521
  nodesWalkSelectStmt(pSelect, clause, collectFuncs, &cxt);
X
Xiaoyu Wang 已提交
1522 1523 1524 1525
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
    nodesDestroyList(cxt.pFuncs);
    return cxt.errCode;
  }
X
Xiaoyu Wang 已提交
1526 1527 1528 1529 1530
  if (LIST_LENGTH(cxt.pFuncs) > 0) {
    *pFuncs = cxt.pFuncs;
  } else {
    nodesDestroyList(cxt.pFuncs);
  }
X
Xiaoyu Wang 已提交
1531

1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
  return TSDB_CODE_SUCCESS;
}

typedef struct SCollectSpecialNodesCxt {
  int32_t    errCode;
  ENodeType  type;
  SNodeList* pNodes;
} SCollectSpecialNodesCxt;

static EDealRes collectSpecialNodes(SNode* pNode, void* pContext) {
  SCollectSpecialNodesCxt* pCxt = (SCollectSpecialNodesCxt*)pContext;
  if (pCxt->type == nodeType(pNode)) {
    pCxt->errCode = nodesListStrictAppend(pCxt->pNodes, nodesCloneNode(pNode));
    return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR);
  }
  return DEAL_RES_CONTINUE;
}

int32_t nodesCollectSpecialNodes(SSelectStmt* pSelect, ESqlClause clause, ENodeType type, SNodeList** pNodes) {
  if (NULL == pSelect || NULL == pNodes) {
    return TSDB_CODE_FAILED;
  }

  SCollectSpecialNodesCxt cxt = {
      .errCode = TSDB_CODE_SUCCESS, .type = type, .pNodes = (NULL == *pNodes ? nodesMakeList() : *pNodes)};
  if (NULL == cxt.pNodes) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  *pNodes = NULL;
  nodesWalkSelectStmt(pSelect, SQL_CLAUSE_GROUP_BY, collectSpecialNodes, &cxt);
  if (TSDB_CODE_SUCCESS != cxt.errCode) {
    nodesDestroyList(cxt.pNodes);
    return cxt.errCode;
  }
  if (LIST_LENGTH(cxt.pNodes) > 0) {
    *pNodes = cxt.pNodes;
  } else {
    nodesDestroyList(cxt.pNodes);
  }

X
Xiaoyu Wang 已提交
1572 1573
  return TSDB_CODE_SUCCESS;
}
D
dapan1121 已提交
1574

1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
static EDealRes hasColumn(SNode* pNode, void* pContext) {
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    *(bool*)pContext = true;
    return DEAL_RES_END;
  }
  return DEAL_RES_CONTINUE;
}

bool nodesExprHasColumn(SNode* pNode) {
  bool hasCol = false;
  nodesWalkExprPostOrder(pNode, hasColumn, &hasCol);
  return hasCol;
}

bool nodesExprsHasColumn(SNodeList* pList) {
  bool hasCol = false;
  nodesWalkExprsPostOrder(pList, hasColumn, &hasCol);
  return hasCol;
}

X
Xiaoyu Wang 已提交
1595
char* nodesGetFillModeString(EFillMode mode) {
D
dapan1121 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613
  switch (mode) {
    case FILL_MODE_NONE:
      return "none";
    case FILL_MODE_VALUE:
      return "value";
    case FILL_MODE_PREV:
      return "prev";
    case FILL_MODE_NULL:
      return "null";
    case FILL_MODE_LINEAR:
      return "linear";
    case FILL_MODE_NEXT:
      return "next";
    default:
      return "unknown";
  }
}

X
Xiaoyu Wang 已提交
1614
char* nodesGetNameFromColumnNode(SNode* pNode) {
D
dapan1121 已提交
1615 1616 1617
  if (NULL == pNode || QUERY_NODE_COLUMN != pNode->type) {
    return "NULL";
  }
X
Xiaoyu Wang 已提交
1618 1619

  return ((SColumnNode*)pNode)->colName;
D
dapan1121 已提交
1620 1621
}

D
dapan1121 已提交
1622 1623 1624 1625 1626
int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots) {
  if (NULL == pSlots || pSlots->length <= 0) {
    return 0;
  }

X
Xiaoyu Wang 已提交
1627
  SNode*  pNode = NULL;
D
dapan1121 已提交
1628 1629 1630 1631 1632 1633
  int32_t num = 0;
  FOREACH(pNode, pSlots) {
    if (QUERY_NODE_SLOT_DESC != pNode->type) {
      continue;
    }

X
Xiaoyu Wang 已提交
1634
    SSlotDescNode* descNode = (SSlotDescNode*)pNode;
D
dapan1121 已提交
1635 1636 1637 1638 1639 1640 1641 1642
    if (descNode->output) {
      ++num;
    }
  }

  return num;
}

X
Xiaoyu Wang 已提交
1643
void nodesValueNodeToVariant(const SValueNode* pNode, SVariant* pVal) {
D
dapan1121 已提交
1644 1645 1646 1647
  pVal->nType = pNode->node.resType.type;
  pVal->nLen = pNode->node.resType.bytes;
  switch (pNode->node.resType.type) {
    case TSDB_DATA_TYPE_NULL:
X
Xiaoyu Wang 已提交
1648
      break;
D
dapan1121 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
    case TSDB_DATA_TYPE_BOOL:
      pVal->i = pNode->datum.b;
      break;
    case TSDB_DATA_TYPE_TINYINT:
    case TSDB_DATA_TYPE_SMALLINT:
    case TSDB_DATA_TYPE_INT:
    case TSDB_DATA_TYPE_BIGINT:
    case TSDB_DATA_TYPE_TIMESTAMP:
      pVal->i = pNode->datum.i;
      break;
    case TSDB_DATA_TYPE_UTINYINT:
    case TSDB_DATA_TYPE_USMALLINT:
    case TSDB_DATA_TYPE_UINT:
    case TSDB_DATA_TYPE_UBIGINT:
      pVal->u = pNode->datum.u;
      break;
    case TSDB_DATA_TYPE_FLOAT:
    case TSDB_DATA_TYPE_DOUBLE:
      pVal->d = pNode->datum.d;
      break;
    case TSDB_DATA_TYPE_NCHAR:
    case TSDB_DATA_TYPE_VARCHAR:
    case TSDB_DATA_TYPE_VARBINARY:
D
dapan1121 已提交
1672 1673 1674
      pVal->pz = taosMemoryMalloc(pVal->nLen + VARSTR_HEADER_SIZE + 1);
      memcpy(pVal->pz, pNode->datum.p, pVal->nLen + VARSTR_HEADER_SIZE);
      pVal->pz[pVal->nLen + VARSTR_HEADER_SIZE] = 0;
D
dapan1121 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683
      break;
    case TSDB_DATA_TYPE_JSON:
    case TSDB_DATA_TYPE_DECIMAL:
    case TSDB_DATA_TYPE_BLOB:
      // todo
    default:
      break;
  }
}
X
Xiaoyu Wang 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693

int32_t nodesMergeConds(SNode** pDst, SNodeList** pSrc) {
  if (NULL == *pSrc) {
    return TSDB_CODE_SUCCESS;
  }

  if (1 == LIST_LENGTH(*pSrc)) {
    *pDst = nodesListGetNode(*pSrc, 0);
    nodesClearList(*pSrc);
  } else {
1694
    SLogicConditionNode* pLogicCond = (SLogicConditionNode*)nodesMakeNode(QUERY_NODE_LOGIC_CONDITION);
X
Xiaoyu Wang 已提交
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
    if (NULL == pLogicCond) {
      return TSDB_CODE_OUT_OF_MEMORY;
    }
    pLogicCond->node.resType.type = TSDB_DATA_TYPE_BOOL;
    pLogicCond->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BOOL].bytes;
    pLogicCond->condType = LOGIC_COND_TYPE_AND;
    pLogicCond->pParameterList = *pSrc;
    *pDst = (SNode*)pLogicCond;
  }
  *pSrc = NULL;

  return TSDB_CODE_SUCCESS;
}

typedef struct SClassifyConditionCxt {
  bool hasPrimaryKey;
  bool hasTagIndexCol;
X
Xiaoyu Wang 已提交
1712
  bool hasTagCol;
X
Xiaoyu Wang 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723
  bool hasOtherCol;
} SClassifyConditionCxt;

static EDealRes classifyConditionImpl(SNode* pNode, void* pContext) {
  SClassifyConditionCxt* pCxt = (SClassifyConditionCxt*)pContext;
  if (QUERY_NODE_COLUMN == nodeType(pNode)) {
    SColumnNode* pCol = (SColumnNode*)pNode;
    if (PRIMARYKEY_TIMESTAMP_COL_ID == pCol->colId) {
      pCxt->hasPrimaryKey = true;
    } else if (pCol->hasIndex) {
      pCxt->hasTagIndexCol = true;
X
Xiaoyu Wang 已提交
1724 1725 1726
      pCxt->hasTagCol = true;
    } else if (COLUMN_TYPE_TAG == pCol->colType) {
      pCxt->hasTagCol = true;
X
Xiaoyu Wang 已提交
1727 1728 1729 1730 1731 1732 1733 1734
    } else {
      pCxt->hasOtherCol = true;
    }
    return *((bool*)pContext) ? DEAL_RES_CONTINUE : DEAL_RES_END;
  }
  return DEAL_RES_CONTINUE;
}

X
Xiaoyu Wang 已提交
1735 1736 1737 1738 1739 1740
typedef enum EConditionType {
  COND_TYPE_PRIMARY_KEY = 1,
  COND_TYPE_TAG_INDEX,
  COND_TYPE_TAG,
  COND_TYPE_NORMAL
} EConditionType;
X
Xiaoyu Wang 已提交
1741 1742 1743 1744 1745

static EConditionType classifyCondition(SNode* pNode) {
  SClassifyConditionCxt cxt = {.hasPrimaryKey = false, .hasTagIndexCol = false, .hasOtherCol = false};
  nodesWalkExpr(pNode, classifyConditionImpl, &cxt);
  return cxt.hasOtherCol ? COND_TYPE_NORMAL
X
Xiaoyu Wang 已提交
1746
                         : (cxt.hasPrimaryKey && cxt.hasTagCol
X
Xiaoyu Wang 已提交
1747
                                ? COND_TYPE_NORMAL
X
Xiaoyu Wang 已提交
1748 1749
                                : (cxt.hasPrimaryKey ? COND_TYPE_PRIMARY_KEY
                                                     : (cxt.hasTagIndexCol ? COND_TYPE_TAG_INDEX : COND_TYPE_TAG)));
X
Xiaoyu Wang 已提交
1750 1751
}

X
Xiaoyu Wang 已提交
1752 1753
static int32_t partitionLogicCond(SNode** pCondition, SNode** pPrimaryKeyCond, SNode** pTagIndexCond, SNode** pTagCond,
                                  SNode** pOtherCond) {
X
Xiaoyu Wang 已提交
1754 1755 1756 1757 1758
  SLogicConditionNode* pLogicCond = (SLogicConditionNode*)(*pCondition);

  int32_t code = TSDB_CODE_SUCCESS;

  SNodeList* pPrimaryKeyConds = NULL;
X
Xiaoyu Wang 已提交
1759
  SNodeList* pTagIndexConds = NULL;
X
Xiaoyu Wang 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
  SNodeList* pTagConds = NULL;
  SNodeList* pOtherConds = NULL;
  SNode*     pCond = NULL;
  FOREACH(pCond, pLogicCond->pParameterList) {
    switch (classifyCondition(pCond)) {
      case COND_TYPE_PRIMARY_KEY:
        if (NULL != pPrimaryKeyCond) {
          code = nodesListMakeAppend(&pPrimaryKeyConds, nodesCloneNode(pCond));
        }
        break;
      case COND_TYPE_TAG_INDEX:
X
Xiaoyu Wang 已提交
1771 1772 1773 1774 1775 1776 1777 1778
        if (NULL != pTagIndexCond) {
          code = nodesListMakeAppend(&pTagIndexConds, nodesCloneNode(pCond));
        }
        if (NULL != pTagCond) {
          code = nodesListMakeAppend(&pTagConds, nodesCloneNode(pCond));
        }
        break;
      case COND_TYPE_TAG:
X
Xiaoyu Wang 已提交
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
        if (NULL != pTagCond) {
          code = nodesListMakeAppend(&pTagConds, nodesCloneNode(pCond));
        }
        break;
      case COND_TYPE_NORMAL:
      default:
        if (NULL != pOtherCond) {
          code = nodesListMakeAppend(&pOtherConds, nodesCloneNode(pCond));
        }
        break;
    }
    if (TSDB_CODE_SUCCESS != code) {
      break;
    }
  }

  SNode* pTempPrimaryKeyCond = NULL;
X
Xiaoyu Wang 已提交
1796
  SNode* pTempTagIndexCond = NULL;
X
Xiaoyu Wang 已提交
1797 1798 1799 1800 1801
  SNode* pTempTagCond = NULL;
  SNode* pTempOtherCond = NULL;
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempPrimaryKeyCond, &pPrimaryKeyConds);
  }
X
Xiaoyu Wang 已提交
1802 1803 1804
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempTagIndexCond, &pTagIndexConds);
  }
X
Xiaoyu Wang 已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempTagCond, &pTagConds);
  }
  if (TSDB_CODE_SUCCESS == code) {
    code = nodesMergeConds(&pTempOtherCond, &pOtherConds);
  }

  if (TSDB_CODE_SUCCESS == code) {
    if (NULL != pPrimaryKeyCond) {
      *pPrimaryKeyCond = pTempPrimaryKeyCond;
    }
X
Xiaoyu Wang 已提交
1816 1817 1818
    if (NULL != pTagIndexCond) {
      *pTagIndexCond = pTempTagIndexCond;
    }
X
Xiaoyu Wang 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
    if (NULL != pTagCond) {
      *pTagCond = pTempTagCond;
    }
    if (NULL != pOtherCond) {
      *pOtherCond = pTempOtherCond;
    }
    nodesDestroyNode(*pCondition);
    *pCondition = NULL;
  } else {
    nodesDestroyList(pPrimaryKeyConds);
X
Xiaoyu Wang 已提交
1829
    nodesDestroyList(pTagIndexConds);
X
Xiaoyu Wang 已提交
1830 1831 1832
    nodesDestroyList(pTagConds);
    nodesDestroyList(pOtherConds);
    nodesDestroyNode(pTempPrimaryKeyCond);
X
Xiaoyu Wang 已提交
1833
    nodesDestroyNode(pTempTagIndexCond);
X
Xiaoyu Wang 已提交
1834 1835 1836 1837 1838 1839 1840
    nodesDestroyNode(pTempTagCond);
    nodesDestroyNode(pTempOtherCond);
  }

  return code;
}

X
Xiaoyu Wang 已提交
1841 1842
int32_t nodesPartitionCond(SNode** pCondition, SNode** pPrimaryKeyCond, SNode** pTagIndexCond, SNode** pTagCond,
                           SNode** pOtherCond) {
X
Xiaoyu Wang 已提交
1843 1844
  if (QUERY_NODE_LOGIC_CONDITION == nodeType(*pCondition) &&
      LOGIC_COND_TYPE_AND == ((SLogicConditionNode*)*pCondition)->condType) {
X
Xiaoyu Wang 已提交
1845
    return partitionLogicCond(pCondition, pPrimaryKeyCond, pTagIndexCond, pTagCond, pOtherCond);
X
Xiaoyu Wang 已提交
1846 1847 1848 1849 1850 1851 1852 1853 1854
  }

  switch (classifyCondition(*pCondition)) {
    case COND_TYPE_PRIMARY_KEY:
      if (NULL != pPrimaryKeyCond) {
        *pPrimaryKeyCond = *pCondition;
      }
      break;
    case COND_TYPE_TAG_INDEX:
X
Xiaoyu Wang 已提交
1855 1856 1857 1858
      if (NULL != pTagIndexCond) {
        *pTagIndexCond = *pCondition;
      }
      if (NULL != pTagCond) {
1859 1860 1861 1862 1863 1864 1865 1866
        SNode* pTempCond = *pCondition;
        if (NULL != pTagIndexCond) {
          pTempCond = nodesCloneNode(*pCondition);
          if (NULL == pTempCond) {
            return TSDB_CODE_OUT_OF_MEMORY;
          }
        }
        *pTagCond = pTempCond;
X
Xiaoyu Wang 已提交
1867 1868 1869
      }
      break;
    case COND_TYPE_TAG:
X
Xiaoyu Wang 已提交
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
      if (NULL != pTagCond) {
        *pTagCond = *pCondition;
      }
      break;
    case COND_TYPE_NORMAL:
    default:
      if (NULL != pOtherCond) {
        *pOtherCond = *pCondition;
      }
      break;
  }
  *pCondition = NULL;

  return TSDB_CODE_SUCCESS;
}